summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 16:16:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 16:16:13 +0000
commite90fcc54809db2591dc083f43ef54c6ec8c60847 (patch)
treef20bc206c3c2d5d59d37c46c5cf5d53a20642556
parentInitial commit. (diff)
downloadexim4-upstream.tar.xz
exim4-upstream.zip
Adding upstream version 4.96.upstream/4.96upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
-rw-r--r--.ctags2
-rw-r--r--.gitattributes1
-rw-r--r--.gitignore4
-rw-r--r--ABOUT9
-rw-r--r--ACKNOWLEDGMENTS475
-rw-r--r--CHANGES10
-rw-r--r--CONTRIBUTING56
-rw-r--r--LICENCE340
-rw-r--r--LICENSE.opendmarc29
-rw-r--r--Makefile115
-rw-r--r--NOTICE152
-rw-r--r--OS/Makefile-Base995
-rw-r--r--OS/Makefile-Default327
-rw-r--r--OS/Makefile-FreeBSD54
-rw-r--r--OS/Makefile-GNU30
-rw-r--r--OS/Makefile-Linux39
-rw-r--r--OS/Makefile-OpenBSD30
-rw-r--r--OS/Makefile-SunOS524
-rw-r--r--OS/eximon.conf-Default41
-rw-r--r--OS/os.Configuring215
-rw-r--r--OS/os.c-FreeBSD49
-rw-r--r--OS/os.c-GNU56
-rw-r--r--OS/os.c-Linux165
-rw-r--r--OS/os.c-SunOS516
-rw-r--r--OS/os.h-FreeBSD81
-rw-r--r--OS/os.h-GNU34
-rw-r--r--OS/os.h-Linux104
-rw-r--r--OS/os.h-OpenBSD65
-rw-r--r--OS/os.h-SunOS558
-rw-r--r--OS/unsupported/Makefile-AIX28
-rw-r--r--OS/unsupported/Makefile-BSDI21
-rw-r--r--OS/unsupported/Makefile-CYGWIN113
-rw-r--r--OS/unsupported/Makefile-DGUX32
-rw-r--r--OS/unsupported/Makefile-Darwin33
-rw-r--r--OS/unsupported/Makefile-DragonFly31
-rw-r--r--OS/unsupported/Makefile-GNUkFreeBSD29
-rw-r--r--OS/unsupported/Makefile-GNUkNetBSD29
-rw-r--r--OS/unsupported/Makefile-HI-OSF8
-rw-r--r--OS/unsupported/Makefile-HI-UX12
-rw-r--r--OS/unsupported/Makefile-HP-UX27
-rw-r--r--OS/unsupported/Makefile-HP-UX-915
-rw-r--r--OS/unsupported/Makefile-IRIX12
-rw-r--r--OS/unsupported/Makefile-IRIX613
-rw-r--r--OS/unsupported/Makefile-IRIX63216
-rw-r--r--OS/unsupported/Makefile-IRIX6516
-rw-r--r--OS/unsupported/Makefile-NetBSD27
-rw-r--r--OS/unsupported/Makefile-NetBSD-a.out24
-rw-r--r--OS/unsupported/Makefile-OSF110
-rw-r--r--OS/unsupported/Makefile-OpenUNIX17
-rw-r--r--OS/unsupported/Makefile-QNX30
-rw-r--r--OS/unsupported/Makefile-SCO28
-rw-r--r--OS/unsupported/Makefile-SCO_SV34
-rw-r--r--OS/unsupported/Makefile-SunOS416
-rw-r--r--OS/unsupported/Makefile-SunOS5-hal18
-rw-r--r--OS/unsupported/Makefile-ULTRIX18
-rw-r--r--OS/unsupported/Makefile-UNIX_SV24
-rw-r--r--OS/unsupported/Makefile-USG33
-rw-r--r--OS/unsupported/Makefile-Unixware732
-rw-r--r--OS/unsupported/Makefile-mips16
-rw-r--r--OS/unsupported/README14
-rw-r--r--OS/unsupported/os.c-BSDI19
-rw-r--r--OS/unsupported/os.c-HI-OSF35
-rw-r--r--OS/unsupported/os.c-HP-UX16
-rw-r--r--OS/unsupported/os.c-IRIX119
-rw-r--r--OS/unsupported/os.c-IRIX6119
-rw-r--r--OS/unsupported/os.c-IRIX632119
-rw-r--r--OS/unsupported/os.c-IRIX65119
-rw-r--r--OS/unsupported/os.c-OSF136
-rw-r--r--OS/unsupported/os.c-cygwin531
-rw-r--r--OS/unsupported/os.h-AIX27
-rw-r--r--OS/unsupported/os.h-BSDI15
-rw-r--r--OS/unsupported/os.h-DGUX28
-rw-r--r--OS/unsupported/os.h-Darwin64
-rw-r--r--OS/unsupported/os.h-DragonFly13
-rw-r--r--OS/unsupported/os.h-GNUkFreeBSD25
-rw-r--r--OS/unsupported/os.h-GNUkNetBSD25
-rw-r--r--OS/unsupported/os.h-HI-OSF12
-rw-r--r--OS/unsupported/os.h-HI-UX21
-rw-r--r--OS/unsupported/os.h-HP-UX34
-rw-r--r--OS/unsupported/os.h-HP-UX-923
-rw-r--r--OS/unsupported/os.h-IRIX17
-rw-r--r--OS/unsupported/os.h-IRIX616
-rw-r--r--OS/unsupported/os.h-IRIX63218
-rw-r--r--OS/unsupported/os.h-IRIX6516
-rw-r--r--OS/unsupported/os.h-NetBSD31
-rw-r--r--OS/unsupported/os.h-NetBSD-a.out5
-rw-r--r--OS/unsupported/os.h-OSF116
-rw-r--r--OS/unsupported/os.h-OpenUNIX19
-rw-r--r--OS/unsupported/os.h-QNX24
-rw-r--r--OS/unsupported/os.h-SCO21
-rw-r--r--OS/unsupported/os.h-SCO_SV19
-rw-r--r--OS/unsupported/os.h-SunOS439
-rw-r--r--OS/unsupported/os.h-SunOS5-hal14
-rw-r--r--OS/unsupported/os.h-ULTRIX18
-rw-r--r--OS/unsupported/os.h-UNIX_SV25
-rw-r--r--OS/unsupported/os.h-USG19
-rw-r--r--OS/unsupported/os.h-Unixware718
-rw-r--r--OS/unsupported/os.h-cygwin41
-rw-r--r--OS/unsupported/os.h-mips27
-rw-r--r--README350
-rw-r--r--README.DSN141
-rw-r--r--README.UPDATING885
-rw-r--r--conf2
-rw-r--r--doc/ChangeLog8277
-rw-r--r--doc/DANE-draft-notes11
-rw-r--r--doc/Exim3.upgrade671
-rw-r--r--doc/Exim4.upgrade1734
-rw-r--r--doc/GnuTLS-FAQ.txt414
-rw-r--r--doc/NewStuff1476
-rw-r--r--doc/OptionLists.txt1094
-rw-r--r--doc/README65
-rw-r--r--doc/README.SIEVE343
-rw-r--r--doc/cve-2016-966395
-rw-r--r--doc/cve-2019-1391746
-rw-r--r--doc/cve-2019-15846/cve.txt45
-rw-r--r--doc/cve-2019-15846/mitre.mbx84
-rw-r--r--doc/cve-2019-15846/posting-0.txt59
-rw-r--r--doc/cve-2019-15846/posting-1.txt59
-rw-r--r--doc/cve-2019-15846/posting-2.txt44
-rw-r--r--doc/cve-2019-15846/qualys.mbx175
-rw-r--r--doc/cve-2020-qualys2
-rw-r--r--doc/dbm.discuss.txt321
-rw-r--r--doc/exim.81810
-rw-r--r--doc/experimental-spec.txt665
-rw-r--r--doc/filter.txt1720
-rw-r--r--doc/openssl.txt165
-rw-r--r--doc/spec.txt39528
-rw-r--r--exim_monitor/EDITME179
-rw-r--r--exim_monitor/em_StripChart.c504
-rw-r--r--exim_monitor/em_TextPop.c767
-rw-r--r--exim_monitor/em_globals.c239
-rw-r--r--exim_monitor/em_hdr.h329
-rw-r--r--exim_monitor/em_init.c238
-rw-r--r--exim_monitor/em_log.c411
-rw-r--r--exim_monitor/em_main.c953
-rw-r--r--exim_monitor/em_menu.c930
-rw-r--r--exim_monitor/em_queue.c828
-rw-r--r--exim_monitor/em_strip.c266
-rw-r--r--exim_monitor/em_text.c73
-rw-r--r--exim_monitor/em_version.c65
-rw-r--r--exim_monitor/em_xs.c45
-rwxr-xr-xscripts/Configure10
-rwxr-xr-xscripts/Configure-Makefile360
-rwxr-xr-xscripts/Configure-config.h60
-rwxr-xr-xscripts/Configure-eximon51
-rwxr-xr-xscripts/Configure-os.c30
-rwxr-xr-xscripts/Configure-os.h51
-rwxr-xr-xscripts/MakeLinks133
-rwxr-xr-xscripts/arch-type74
-rwxr-xr-xscripts/exim_install472
-rwxr-xr-xscripts/lookups-Makefile194
-rwxr-xr-xscripts/newer21
-rwxr-xr-xscripts/os-type164
-rwxr-xr-xscripts/reversion134
-rw-r--r--scripts/source_checks52
-rw-r--r--src/EDITME1497
-rw-r--r--src/acl.c4896
-rw-r--r--src/aliases.default40
-rw-r--r--src/arc.c1903
-rw-r--r--src/auths/Makefile45
-rw-r--r--src/auths/README98
-rw-r--r--src/auths/auth-spa.c1524
-rw-r--r--src/auths/auth-spa.h92
-rw-r--r--src/auths/call_pam.c204
-rw-r--r--src/auths/call_pwcheck.c121
-rw-r--r--src/auths/call_radius.c223
-rw-r--r--src/auths/check_serv_cond.c124
-rw-r--r--src/auths/cram_md5.c360
-rw-r--r--src/auths/cram_md5.h31
-rw-r--r--src/auths/cyrus_sasl.c513
-rw-r--r--src/auths/cyrus_sasl.h36
-rw-r--r--src/auths/dovecot.c530
-rw-r--r--src/auths/dovecot.h30
-rw-r--r--src/auths/external.c155
-rw-r--r--src/auths/external.h32
-rw-r--r--src/auths/get_data.c259
-rw-r--r--src/auths/get_no64_data.c47
-rw-r--r--src/auths/gsasl_exim.c1024
-rw-r--r--src/auths/gsasl_exim.h53
-rw-r--r--src/auths/heimdal_gssapi.c618
-rw-r--r--src/auths/heimdal_gssapi.h39
-rw-r--r--src/auths/plaintext.c179
-rw-r--r--src/auths/plaintext.h31
-rw-r--r--src/auths/pwcheck.c449
-rw-r--r--src/auths/pwcheck.h27
-rw-r--r--src/auths/spa.c376
-rw-r--r--src/auths/spa.h38
-rw-r--r--src/auths/tls.c94
-rw-r--r--src/auths/tls.h30
-rw-r--r--src/auths/xtextdecode.c58
-rw-r--r--src/auths/xtextencode.c58
-rw-r--r--src/base64.c297
-rw-r--r--src/blob.h15
-rw-r--r--src/bmi_spam.c476
-rw-r--r--src/bmi_spam.h22
-rw-r--r--src/buildconfig.c984
-rw-r--r--src/child.c556
-rw-r--r--src/cnumber.h1
-rw-r--r--src/config.h.defaults235
-rw-r--r--src/configure.default1015
-rwxr-xr-xsrc/convert4r3.src1382
-rwxr-xr-xsrc/convert4r4.src2527
-rw-r--r--src/crypt16.c77
-rw-r--r--src/daemon.c2654
-rw-r--r--src/dane-openssl.c1719
-rw-r--r--src/dane.c48
-rw-r--r--src/danessl.h47
-rw-r--r--src/dbfn.c681
-rw-r--r--src/dbfunctions.h38
-rw-r--r--src/dcc.c490
-rw-r--r--src/dcc.h16
-rw-r--r--src/debug.c498
-rw-r--r--src/deliver.c8642
-rw-r--r--src/directory.c95
-rw-r--r--src/dkim.c895
-rw-r--r--src/dkim.h32
-rw-r--r--src/dkim_transport.c412
-rw-r--r--src/dmarc.c655
-rw-r--r--src/dmarc.h60
-rw-r--r--src/dns.c1332
-rw-r--r--src/dnsbl.c651
-rw-r--r--src/drtables.c818
-rw-r--r--src/dummies.c139
-rw-r--r--src/enq.c123
-rw-r--r--src/environment.c83
-rw-r--r--src/exicyclog.src350
-rw-r--r--src/exigrep.src380
-rw-r--r--src/exim.c6097
-rw-r--r--src/exim.h671
-rwxr-xr-xsrc/exim_checkaccess.src181
-rw-r--r--src/exim_dbmbuild.c536
-rw-r--r--src/exim_dbutil.c1423
-rw-r--r--src/exim_lock.c663
-rw-r--r--src/eximon.src221
-rw-r--r--src/eximstats.src4246
-rw-r--r--src/exinext.src262
-rw-r--r--src/exipick.src1841
-rw-r--r--src/exiqgrep.src216
-rw-r--r--src/exiqsumm.src178
-rw-r--r--src/exiwhat.src145
-rw-r--r--src/expand.c8837
-rw-r--r--src/filter.c2607
-rw-r--r--src/filtertest.c282
-rw-r--r--src/functions.h1305
-rw-r--r--src/globals.c1653
-rw-r--r--src/globals.h1120
-rw-r--r--src/hash.c895
-rw-r--r--src/hash.h83
-rw-r--r--src/header.c468
-rw-r--r--src/hintsdb.h810
-rw-r--r--src/hintsdb_structs.h189
-rw-r--r--src/host.c3431
-rw-r--r--src/imap_utf7.c211
-rw-r--r--src/ip.c853
-rw-r--r--src/local_scan.c64
-rw-r--r--src/local_scan.h239
-rw-r--r--src/log.c1554
-rw-r--r--src/lookupapi.h65
-rw-r--r--src/lookups/Makefile76
-rw-r--r--src/lookups/README181
-rw-r--r--src/lookups/cdb.c490
-rw-r--r--src/lookups/dbmdb.c284
-rw-r--r--src/lookups/dnsdb.c614
-rw-r--r--src/lookups/dsearch.c190
-rw-r--r--src/lookups/ibase.c561
-rw-r--r--src/lookups/json.c188
-rw-r--r--src/lookups/ldap.c1613
-rw-r--r--src/lookups/ldap.h13
-rw-r--r--src/lookups/lf_check_file.c113
-rw-r--r--src/lookups/lf_functions.h20
-rw-r--r--src/lookups/lf_quote.c63
-rw-r--r--src/lookups/lf_sqlperform.c175
-rw-r--r--src/lookups/lmdb.c162
-rw-r--r--src/lookups/lsearch.c487
-rw-r--r--src/lookups/mysql.c503
-rw-r--r--src/lookups/nis.c141
-rw-r--r--src/lookups/nisplus.c294
-rw-r--r--src/lookups/oracle.c628
-rw-r--r--src/lookups/passwd.c85
-rw-r--r--src/lookups/pgsql.c506
-rw-r--r--src/lookups/readsock.c341
-rw-r--r--src/lookups/redis.c471
-rw-r--r--src/lookups/spf.c159
-rw-r--r--src/lookups/sqlite.c200
-rw-r--r--src/lookups/testdb.c100
-rw-r--r--src/lookups/whoson.c98
-rw-r--r--src/lss.c142
-rw-r--r--src/macro_predef.c345
-rw-r--r--src/macro_predef.h28
-rw-r--r--src/macros.h1117
-rw-r--r--src/malware.c2328
-rw-r--r--src/match.c1352
-rw-r--r--src/md5.c355
-rw-r--r--src/memcheck.h277
-rw-r--r--src/mime.c803
-rw-r--r--src/mime.h44
-rw-r--r--src/moan.c874
-rw-r--r--src/mytypes.h150
-rw-r--r--src/os.c971
-rw-r--r--src/osfunctions.h43
-rw-r--r--src/parse.c2243
-rw-r--r--src/pdkim/Makefile19
-rw-r--r--src/pdkim/README9
-rw-r--r--src/pdkim/config.h4
-rw-r--r--src/pdkim/crypt_ver.h33
-rw-r--r--src/pdkim/pdkim.c2097
-rw-r--r--src/pdkim/pdkim.h369
-rw-r--r--src/pdkim/pdkim_hash.h38
-rw-r--r--src/pdkim/signing.c903
-rw-r--r--src/pdkim/signing.h97
-rw-r--r--src/perl.c201
-rw-r--r--src/priv.c76
-rw-r--r--src/queue.c1588
-rw-r--r--src/rda.c997
-rw-r--r--src/readconf.c4510
-rw-r--r--src/receive.c4528
-rw-r--r--src/regex.c217
-rw-r--r--src/retry.c934
-rw-r--r--src/rewrite.c825
-rw-r--r--src/rfc2047.c345
-rw-r--r--src/route.c2072
-rw-r--r--src/routers/Makefile43
-rw-r--r--src/routers/README57
-rw-r--r--src/routers/accept.c139
-rw-r--r--src/routers/accept.h31
-rw-r--r--src/routers/dnslookup.c477
-rw-r--r--src/routers/dnslookup.h42
-rw-r--r--src/routers/ipliteral.c202
-rw-r--r--src/routers/ipliteral.h34
-rw-r--r--src/routers/iplookup.c418
-rw-r--r--src/routers/iplookup.h42
-rw-r--r--src/routers/manualroute.c490
-rw-r--r--src/routers/manualroute.h39
-rw-r--r--src/routers/queryprogram.c538
-rw-r--r--src/routers/queryprogram.h40
-rw-r--r--src/routers/redirect.c801
-rw-r--r--src/routers/redirect.h70
-rw-r--r--src/routers/rf_change_domain.c85
-rw-r--r--src/routers/rf_expand_data.c48
-rw-r--r--src/routers/rf_functions.h31
-rw-r--r--src/routers/rf_get_errors_address.c132
-rw-r--r--src/routers/rf_get_munge_headers.c123
-rw-r--r--src/routers/rf_get_transport.c97
-rw-r--r--src/routers/rf_get_ugid.c80
-rw-r--r--src/routers/rf_lookup_hostlist.c262
-rw-r--r--src/routers/rf_queue_add.c109
-rw-r--r--src/routers/rf_self_action.c123
-rw-r--r--src/routers/rf_set_ugid.c44
-rw-r--r--src/search.c967
-rw-r--r--src/setenv.c58
-rw-r--r--src/sha_ver.h47
-rw-r--r--src/sieve.c3629
-rw-r--r--src/smtp_in.c6068
-rw-r--r--src/smtp_out.c936
-rw-r--r--src/spam.c614
-rw-r--r--src/spam.h40
-rw-r--r--src/spf.c420
-rw-r--r--src/spf.h38
-rw-r--r--src/spool_in.c1094
-rw-r--r--src/spool_mbox.c247
-rw-r--r--src/spool_out.c580
-rw-r--r--src/std-crypto.c1031
-rw-r--r--src/store.c1301
-rw-r--r--src/store.h89
-rw-r--r--src/string.c1859
-rw-r--r--src/structs.h960
-rw-r--r--src/tls-cipher-stdname.c393
-rw-r--r--src/tls-gnu.c4280
-rw-r--r--src/tls-openssl.c4894
-rw-r--r--src/tls.c843
-rw-r--r--src/tlscert-gnu.c482
-rw-r--r--src/tlscert-openssl.c532
-rw-r--r--src/tod.c239
-rw-r--r--src/transport-filter.src93
-rw-r--r--src/transport.c2409
-rw-r--r--src/transports/Makefile27
-rw-r--r--src/transports/README41
-rw-r--r--src/transports/appendfile.c3317
-rw-r--r--src/transports/appendfile.h100
-rw-r--r--src/transports/autoreply.c821
-rw-r--r--src/transports/autoreply.h45
-rw-r--r--src/transports/lmtp.c809
-rw-r--r--src/transports/lmtp.h32
-rw-r--r--src/transports/pipe.c1124
-rw-r--r--src/transports/pipe.h51
-rw-r--r--src/transports/queuefile.c286
-rw-r--r--src/transports/queuefile.h29
-rw-r--r--src/transports/smtp.c6071
-rw-r--r--src/transports/smtp.h252
-rw-r--r--src/transports/smtp_socks.c415
-rw-r--r--src/transports/tf_maildir.c585
-rw-r--r--src/transports/tf_maildir.h21
-rw-r--r--src/tree.c393
-rw-r--r--src/utf8.c278
-rw-r--r--src/valgrind.h4797
-rw-r--r--src/verify.c3610
-rw-r--r--src/version.c68
-rw-r--r--src/version.h7
-rw-r--r--src/version.sh3
-rw-r--r--util/.gitignore2
-rw-r--r--util/README40
-rwxr-xr-xutil/chunking_fixqueue_finalnewlines.pl160
-rwxr-xr-xutil/cramtest.pl60
-rw-r--r--util/gen_pkcs3.c268
-rwxr-xr-xutil/logargs.sh26
-rwxr-xr-xutil/mkcdb.pl93
-rwxr-xr-xutil/ocsp_fetch.pl84
-rw-r--r--util/proxy_protocol_client.pl251
-rw-r--r--util/ratelimit.pl159
-rwxr-xr-xutil/renew-opendmarc-tlds.sh128
-rwxr-xr-xutil/unknownuser.sh32
411 files changed, 262609 insertions, 0 deletions
diff --git a/.ctags b/.ctags
new file mode 100644
index 0000000..c764086
--- /dev/null
+++ b/.ctags
@@ -0,0 +1,2 @@
+--recurse
+--exclude=build-*
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..554385c
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+ACKNOWLEDGMENTS encoding=utf-8
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8965c11
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+Local
+build-*
+tags
+cscope.*
diff --git a/ABOUT b/ABOUT
new file mode 100644
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
index 0000000..22e9909
--- /dev/null
+++ b/ACKNOWLEDGMENTS
@@ -0,0 +1,475 @@
+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 website and mailing list
+ Managing the website 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 Mostly idle; some security bits still.
+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
+Jasper Wallace Patch for LibreSSL compatibility
+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
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
index 0000000..a5f7809
--- /dev/null
+++ b/CONTRIBUTING
@@ -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
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.
+
+ 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.)
+
+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.
+
+ 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.
+
+ 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
+
+ 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
index 0000000..e2ba06b
--- /dev/null
+++ b/LICENSE.opendmarc
@@ -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
index 0000000..b774b43
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,115 @@
+# 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) The Exim Maintainers 2022
+# Copyright (c) University of Cambridge, 1995 - 2018
+# 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
+
+# The buildname defaults to "<os-type>-<arch-type>". It can be
+# overridden by the "build" parameter when invoking make (e.g. make
+# build=xxx) This does not provide an override for the OS type and
+# architecture type used during the build process; 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.
+#
+# EXIM_BUILD_SUFFIX gets appended to the buildname. (This enables
+# parallel builds on a file system shared among different Linux distros
+# (same os-type, same arch-type). The ../test/runtest script honours the
+# EXIM_BUILD_SUFFIX when searching the Exim binary.)
+
+buildname=$${build:-`$(SHELL) scripts/os-type`-`$(SHELL) scripts/arch-type`}$${EXIM_BUILD_SUFFIX:+.$$EXIM_BUILD_SUFFIX}
+
+# 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.
+# If Local/Makefile-<buildname> exists, it is read too.
+
+all: Local/Makefile configure
+ @cd build-$(buildname); $(MAKE) SHELL=$(SHELL) $(MFLAGS)
+
+# This pair for the convenience of of the Debian maintainers
+exim: Local/Makefile configure
+ @cd build-$(buildname); $(MAKE) SHELL=$(SHELL) $(MFLAGS) exim
+utils: Local/Makefile configure
+ @cd build-$(buildname); $(MAKE) SHELL=$(SHELL) $(MFLAGS) utils
+
+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" >> $@
+ -bd=build-$(buildname); [ -d $$bd ] && echo -e "$$bd/config.h\n$$bd/Makefile" >> $@
+ find src Local OS exim_monitor -name "*.[cshyl]" -print \
+ -o -name "os.[ch]*" -print \
+ -o -name "*akefile*" -print \
+ -o -name config.h.defaults -print \
+ -o -name EDITME -print >> $@
+
+FRC:
+
+# End of top-level makefile
diff --git a/NOTICE b/NOTICE
new file mode 100644
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-Base b/OS/Makefile-Base
new file mode 100644
index 0000000..78f5516
--- /dev/null
+++ b/OS/Makefile-Base
@@ -0,0 +1,995 @@
+# 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
+# concatenates the configuration settings from Local/Makefile and other,
+# optional, Local/* files at the front of this file, to create Makefile in the
+# build directory.
+#
+# Copyright (c) The Exim Maintainers 1995 - 2022
+
+SHELL = $(MAKE_SHELL)
+SCRIPTS = ../scripts
+O = ../OS
+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: utils exim
+config: $(EDITME) checklocalmake Makefile os.c config.h version.h version.sh macro.c
+
+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: $(SCRIPTS)/Configure-os.h \
+ $(O)/os.h-FreeBSD \
+ $(O)/os.h-GNU \
+ $(O)/os.h-Linux \
+ $(O)/os.h-OpenBSD \
+ $(O)/os.h-SunOS5
+ $(SHELL) $(SCRIPTS)/Configure-os.h
+
+# Build the os.c file
+
+os.c: ../src/os.c \
+ $(SCRIPTS)/Configure-os.c \
+ $(O)/os.c-FreeBSD \
+ $(O)/os.c-GNU \
+ $(O)/os.c-Linux \
+ $(O)/os.c-SunOS5
+ $(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)"
+
+# Build the builtin-macros data struct
+
+MACRO_HSRC = macro_predef.h os.h globals.h config.h macros.h \
+ routers/accept.h routers/dnslookup.h routers/ipliteral.h \
+ routers/iplookup.h routers/manualroute.h routers/queryprogram.h \
+ routers/redirect.h
+
+OBJ_MACRO = macro_predef.o \
+ macro-globals.o macro-readconf.o macro-route.o macro-transport.o macro-drtables.o \
+ macro-acl.o macro-tls.o \
+ macro-appendfile.o macro-autoreply.o macro-lmtp.o macro-pipe.o macro-queuefile.o \
+ macro-smtp.o macro-accept.o macro-dnslookup.o macro-ipliteral.o macro-iplookup.o \
+ macro-manualroute.o macro-queryprogram.o macro-redirect.o \
+ macro-auth-spa.o macro-cram_md5.o macro-cyrus_sasl.o macro-dovecot.o macro-gsasl_exim.o \
+ macro-heimdal_gssapi.o macro-plaintext.o macro-spa.o macro-authtls.o macro-external.o \
+ macro-dkim.o macro-malware.o macro-signing.o
+
+$(OBJ_MACRO): $(MACRO_HSRC)
+
+macro_predef.o : macro_predef.c
+ @echo "$(CC) -DMACRO_PREDEF macro_predef.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ macro_predef.c
+macro-globals.o : globals.c
+ @echo "$(CC) -DMACRO_PREDEF globals.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ globals.c
+macro-readconf.o : readconf.c
+ @echo "$(CC) -DMACRO_PREDEF readconf.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ readconf.c
+macro-route.o : route.c
+ @echo "$(CC) -DMACRO_PREDEF route.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ route.c
+macro-transport.o: transport.c
+ @echo "$(CC) -DMACRO_PREDEF transport.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ transport.c
+macro-drtables.o : drtables.c
+ @echo "$(CC) -DMACRO_PREDEF drtables.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ drtables.c
+macro-acl.o: acl.c
+ @echo "$(CC) -DMACRO_PREDEF acl.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ acl.c
+macro-tls.o: tls.c tls-gnu.c tls-openssl.c
+ @echo "$(CC) -DMACRO_PREDEF tls.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ tls.c
+macro-appendfile.o : transports/appendfile.c
+ @echo "$(CC) -DMACRO_PREDEF transports/appendfile.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ transports/appendfile.c
+macro-autoreply.o : transports/autoreply.c
+ @echo "$(CC) -DMACRO_PREDEF transports/autoreply.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ transports/autoreply.c
+macro-lmtp.o: transports/lmtp.c
+ @echo "$(CC) -DMACRO_PREDEF transports/lmtp.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ transports/lmtp.c
+macro-pipe.o : transports/pipe.c
+ @echo "$(CC) -DMACRO_PREDEF transports/pipe.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ transports/pipe.c
+macro-queuefile.o : transports/queuefile.c
+ @echo "$(CC) -DMACRO_PREDEF transports/queuefile.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ transports/queuefile.c
+macro-smtp.o : transports/smtp.c
+ @echo "$(CC) -DMACRO_PREDEF transports/smtp.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ transports/smtp.c
+macro-accept.o : routers/accept.c
+ @echo "$(CC) -DMACRO_PREDEF routers/accept.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ routers/accept.c
+macro-dnslookup.o : routers/dnslookup.c
+ @echo "$(CC) -DMACRO_PREDEF routers/dnslookup.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ routers/dnslookup.c
+macro-ipliteral.o : routers/ipliteral.c
+ @echo "$(CC) -DMACRO_PREDEF routers/ipliteral.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ routers/ipliteral.c
+macro-iplookup.o : routers/iplookup.c
+ @echo "$(CC) -DMACRO_PREDEF routers/iplookup.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ routers/iplookup.c
+macro-manualroute.o : routers/manualroute.c
+ @echo "$(CC) -DMACRO_PREDEF routers/manualroute.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ routers/manualroute.c
+macro-queryprogram.o : routers/queryprogram.c
+ @echo "$(CC) -DMACRO_PREDEF routers/queryprogram.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ routers/queryprogram.c
+macro-redirect.o : routers/redirect.c
+ @echo "$(CC) -DMACRO_PREDEF routers/redirect.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ routers/redirect.c
+macro-auth-spa.o : auths/auth-spa.c
+ @echo "$(CC) -DMACRO_PREDEF auths/auth-spa.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ auths/auth-spa.c
+macro-cram_md5.o : auths/cram_md5.c
+ @echo "$(CC) -DMACRO_PREDEF auths/cram_md5.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ auths/cram_md5.c
+macro-cyrus_sasl.o : auths/cyrus_sasl.c
+ @echo "$(CC) -DMACRO_PREDEF auths/cyrus_sasl.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ auths/cyrus_sasl.c
+macro-dovecot.o: auths/dovecot.c
+ @echo "$(CC) -DMACRO_PREDEF auths/dovecot.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ auths/dovecot.c
+macro-external.o: auths/external.c
+ @echo "$(CC) -DMACRO_PREDEF auths/external.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ auths/external.c
+macro-gsasl_exim.o : auths/gsasl_exim.c
+ @echo "$(CC) -DMACRO_PREDEF auths/gsasl_exim.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ auths/gsasl_exim.c
+macro-heimdal_gssapi.o: auths/heimdal_gssapi.c
+ @echo "$(CC) -DMACRO_PREDEF auths/heimdal_gssapi.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ auths/heimdal_gssapi.c
+macro-plaintext.o : auths/plaintext.c
+ @echo "$(CC) -DMACRO_PREDEF auths/plaintext.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ auths/plaintext.c
+macro-spa.o : auths/spa.c
+ @echo "$(CC) -DMACRO_PREDEF auths/spa.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ auths/spa.c
+macro-authtls.o: auths/tls.c
+ @echo "$(CC) -DMACRO_PREDEF auths/tls.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ auths/tls.c
+macro-dkim.o: dkim.c
+ @echo "$(CC) -DMACRO_PREDEF dkim.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ dkim.c
+macro-malware.o: malware.c
+ @echo "$(CC) -DMACRO_PREDEF malware.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ malware.c
+macro-signing.o: pdkim/signing.c
+ @echo "$(CC) -DMACRO_PREDEF pdkim/signing.c"
+ $(FE)$(CC) -c $(CFLAGS) -DMACRO_PREDEF $(INCLUDE) -o $@ pdkim/signing.c
+
+macro_predef: $(OBJ_MACRO)
+ @echo "$(LNCC) -o $@"
+ $(FE)$(LNCC) -o $@ $(LFLAGS) $(OBJ_MACRO)
+
+macro.c: macro_predef
+ ./macro_predef > macro.c
+
+# 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 utils \
+ buildauths buildlookups buildpdkim buildrouters \
+ buildtransports checklocalmake clean
+
+
+utils: $(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
+
+
+# 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
+ @. ./version.sh && 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)?" \
+ -e "s?EXIM_RELEASE_VERSION?$${EXIM_RELEASE_VERSION}?" \
+ -e "s?EXIM_VARIANT_VERSION?$${EXIM_VARIANT_VERSION}?" \
+ ../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
+ @. ./version.sh && 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?EXIM_RELEASE_VERSION?$${EXIM_RELEASE_VERSION}?" \
+ -e "s?EXIM_VARIANT_VERSION?$${EXIM_VARIANT_VERSION}?" \
+ ../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
+ @. ./version.sh && 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)?" \
+ -e "s?EXIM_RELEASE_VERSION?$${EXIM_RELEASE_VERSION}?" \
+ -e "s?EXIM_VARIANT_VERSION?$${EXIM_VARIANT_VERSION}?" \
+ -e "s?RM_COMMAND?$(RM_COMMAND)?" \
+ ../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
+ @. ./version.sh && 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)?" \
+ -e "s?EXIM_RELEASE_VERSION?$${EXIM_RELEASE_VERSION}?" \
+ -e "s?EXIM_VARIANT_VERSION?$${EXIM_VARIANT_VERSION}?" \
+ ../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
+ @. ./version.sh && 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)?" \
+ -e "s?EXIM_RELEASE_VERSION?$${EXIM_RELEASE_VERSION}?" \
+ -e "s?EXIM_VARIANT_VERSION?$${EXIM_VARIANT_VERSION}?" \
+ ../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: config ../src/exigrep.src
+ @rm -f exigrep
+ @. ./version.sh && 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)?" \
+ -e "s?EXIM_RELEASE_VERSION?$${EXIM_RELEASE_VERSION}?" \
+ -e "s?EXIM_VARIANT_VERSION?$${EXIM_VARIANT_VERSION}?" \
+ ../src/exigrep.src > exigrep-t
+ @mv exigrep-t exigrep
+ @chmod a+x exigrep
+ @echo ">>> exigrep script built"
+
+eximstats: config ../src/eximstats.src
+ @rm -f eximstats
+ @. ./version.sh && sed \
+ -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+ -e "s?EXIM_RELEASE_VERSION?$${EXIM_RELEASE_VERSION}?" \
+ -e "s?EXIM_VARIANT_VERSION?$${EXIM_VARIANT_VERSION}?" \
+ ../src/eximstats.src > eximstats-t
+ @mv eximstats-t eximstats
+ @chmod a+x eximstats
+ @echo ">>> eximstats script built"
+
+exiqgrep: config ../src/exiqgrep.src
+ @rm -f exiqgrep
+ @. ./version.sh && 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)?" \
+ -e "s?EXIM_RELEASE_VERSION?$${EXIM_RELEASE_VERSION}?" \
+ -e "s?EXIM_VARIANT_VERSION?$${EXIM_VARIANT_VERSION}?" \
+ ../src/exiqgrep.src > exiqgrep-t
+ @mv exiqgrep-t exiqgrep
+ @chmod a+x exiqgrep
+ @echo ">>> exiqgrep script built"
+
+exiqsumm: config ../src/exiqsumm.src
+ @rm -f exiqsumm
+ @. ./version.sh && sed \
+ -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+ -e "s?EXIM_RELEASE_VERSION?$${EXIM_RELEASE_VERSION}?" \
+ -e "s?EXIM_VARIANT_VERSION?$${EXIM_VARIANT_VERSION}?" \
+ ../src/exiqsumm.src > exiqsumm-t
+ @mv exiqsumm-t exiqsumm
+ @chmod a+x exiqsumm
+ @echo ">>> exiqsumm script built"
+
+exipick: config ../src/exipick.src
+ @rm -f exipick
+ @. ./version.sh && sed \
+ -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+ -e "s?SPOOL_DIRECTORY?$(SPOOL_DIRECTORY)?" \
+ -e "s?BIN_DIRECTORY?$(BIN_DIRECTORY)?" \
+ -e "s?EXIM_RELEASE_VERSION?$${EXIM_RELEASE_VERSION}?" \
+ -e "s?EXIM_VARIANT_VERSION?$${EXIM_VARIANT_VERSION}?" \
+ ../src/exipick.src > exipick-t
+ @mv exipick-t exipick
+ @chmod a+x exipick
+ @echo ">>> exipick script built"
+
+transport-filter.pl: config ../src/transport-filter.src
+ @rm -f transport-filter.pl
+ @. ./version.sh && sed \
+ -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+ -e "s?EXIM_RELEASE_VERSION?$${EXIM_RELEASE_VERSION}?" \
+ -e "s?EXIM_VARIANT_VERSION?$${EXIM_VARIANT_VERSION}?" \
+ ../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: config ../src/convert4r3.src
+ @rm -f convert4r3
+ @. ./version.sh && sed \
+ -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+ -e "s?EXIM_RELEASE_VERSION?$${EXIM_RELEASE_VERSION}?" \
+ -e "s?EXIM_VARIANT_VERSION?$${EXIM_VARIANT_VERSION}?" \
+ ../src/convert4r3.src > convert4r3-t
+ @mv convert4r3-t convert4r3
+ @chmod a+x convert4r3
+ @echo ">>> convert4r3 script built"
+
+convert4r4: config ../src/convert4r4.src
+ @rm -f convert4r4
+ @. ./version.sh && sed \
+ -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+ -e "s?EXIM_RELEASE_VERSION?$${EXIM_RELEASE_VERSION}?" \
+ -e "s?EXIM_VARIANT_VERSION?$${EXIM_VARIANT_VERSION}?" \
+ ../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_EXPERIMENTAL = arc.o \
+ bmi_spam.o \
+ dane.o \
+ dcc.o \
+ dmarc.o \
+ imap_utf7.o \
+ spf.o \
+ utf8.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 base64.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 dkim_transport.o dnsbl.o hash.o \
+ header.o host.o ip.o log.o lss.o match.o md5.o moan.o \
+ os.o parse.o priv.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 \
+ environment.o macro.o \
+ $(OBJ_LOOKUPS) \
+ local_scan.o $(EXIM_PERL) $(OBJ_WITH_CONTENT_SCAN) \
+ $(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 util-md5.o
+
+exim_fixdb: $(OBJ_FIXDB)
+ @echo "$(LNCC) -o exim_fixdb"
+ $(FE)$(LNCC) $(CFLAGS) $(INCLUDE) -o exim_fixdb $(LFLAGS) $(OBJ_FIXDB) \
+ $(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) $(CFLAGS) $(INCLUDE) -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 os.h
+ @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 \
+ util-queue.o \
+ util-tod.o \
+ util-tree.o \
+ $(MONBIN)
+
+eximon.bin: $(EXIMON_EDITME) eximon $(OBJ_MONBIN) ../exim_monitor/em_version.c \
+ mytypes.h store.h macros.h
+ @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 everything 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 = blob.h \
+ config.h \
+ dbfunctions.h \
+ exim.h \
+ functions.h \
+ globals.h \
+ hash.h \
+ hintsdb.h \
+ hintsdb_structs.h \
+ local_scan.h \
+ macros.h \
+ mytypes.h \
+ sha_ver.h \
+ structs.h \
+ os.h
+PHDRS = ../config.h \
+ ../dbfunctions.h \
+ ../exim.h \
+ ../functions.h \
+ ../globals.h \
+ ../hintsdb.h \
+ ../hintsdb_structs.h \
+ ../local_scan.h \
+ ../macros.h \
+ ../mytypes.h \
+ ../structs.h \
+ ../os.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. The dependency
+# chain here avoids problems under parallel-make.
+
+version.sh:
+ @../scripts/reversion
+
+version.h: version.sh
+
+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) -DCOMPILE_UTILITY \
+ -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-md5.o: $(HDRS) md5.c
+ @echo "$(CC) -DCOMPILE_UTILITY queue.c"
+ $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) -DCOMPILE_UTILITY -o util-md5.o md5.c
+
+util-queue.o: $(HDRS) queue.c
+ @echo "$(CC) -DCOMPILE_UTILITY queue.c"
+ $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) -DCOMPILE_UTILITY -o util-queue.o queue.c
+
+util-tod.o: $(HDRS) tod.c
+ @echo "$(CC) -DCOMPILE_UTILITY tod.c"
+ $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) -DCOMPILE_UTILITY -o util-tod.o tod.c
+
+util-tree.o: $(HDRS) tree.c
+ @echo "$(CC) -DCOMPILE_UTILITY tree.c"
+ $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) -DCOMPILE_UTILITY -o util-tree.o tree.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) -DLOCAL_SCAN -c $(CFLAGS) -I. $(INCLUDE) -o local_scan.o ../$(LOCAL_SCAN_SOURCE)
+
+# Dependencies for the "ordinary" exim modules
+
+acl.o: $(HDRS) acl.c
+base64.o: $(HDRS) mime.h base64.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) transports/smtp.h deliver.c
+directory.o: $(HDRS) directory.c
+dns.o: $(HDRS) dns.c
+dnsbl.o: $(HDRS) dnsbl.c
+enq.o: $(HDRS) enq.c
+exim.o: $(HDRS) exim.c
+expand.o: $(HDRS) expand.c
+environment.o: $(HDRS) environment.c
+filter.o: $(HDRS) filter.c
+filtertest.o: $(HDRS) filtertest.c
+globals.o: $(HDRS) globals.c
+hash.o: $(HDRS) hash.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
+md5.o: $(HDRS) md5.c
+moan.o: $(HDRS) moan.c
+os.o: $(HDRS) $(OS_C_INCLUDES) os.c
+parse.o: $(HDRS) parse.c
+priv.o: $(HDRS) priv.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 \
+ tls-cipher-stdname.c
+tod.o: $(HDRS) tod.c
+transport.o: $(HDRS) transport.c
+tree.o: $(HDRS) tree.c
+verify.o: $(HDRS) transports/smtp.h verify.c
+dkim.o: $(HDRS) pdkim/pdkim.h dkim.c
+dkim_transport.o: $(HDRS) dkim_transport.c
+
+# Dependencies for WITH_CONTENT_SCAN modules
+
+malware.o: $(HDRS) malware.c
+mime.o: $(HDRS) mime.h mime.c
+regex.o: $(HDRS) regex.c
+spam.o: $(HDRS) spam.c
+spool_mbox.o: $(HDRS) spool_mbox.c
+
+
+# Dependencies for EXPERIMENTAL_* modules
+
+arc.o: $(HDRS) pdkim/pdkim.h arc.c
+bmi_spam.o: $(HDRS) bmi_spam.c
+dane.o: $(HDRS) dane.c dane-openssl.c
+dcc.o: $(HDRS) dcc.h dcc.c
+dmarc.o: $(HDRS) pdkim/pdkim.h dmarc.h dmarc.c
+imap_utf7.o: $(HDRS) imap_utf7.c
+spf.o: $(HDRS) spf.h spf.c
+utf8.o: $(HDRS) utf8.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/$(@:.o=.c)"
+ $(FE)$(CC) -o $@ -c $(CFLAGS) -DCOMPILE_UTILITY -I. -I../exim_monitor $(INCLUDE) $(XINCLUDE) \
+ ../exim_monitor/$(@:.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 utf8.o
+ $(CC) -c $(CFLAGS) $(INCLUDE) -DSTAND_ALONE dbfn.c
+ $(CC) -c $(CFLAGS) $(INCLUDE) -DCOMPILE_UTILITY store.c
+ $(LNCC) -o test_dbfn $(LFLAGS) dbfn.o \
+ dummies.o sa-globals.o sa-os.o store.o string.o \
+ tod.o version.o utf8.o $(LIBS) $(DBMLIB) $(LDFLAGS)
+ rm -f dbfn.o store.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 utf8.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 utf8.o $(LIBS) $(LDFLAGS)
+ rm -f os.o
+
+test_parse: config.h parse.c dummies.o sa-globals.o \
+ store.o string.o tod.o version.o utf8.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 \
+ utf8.o $(LDFLAGS)
+ rm -f parse.o
+
+test_string: config.h string.c dummies.o sa-globals.o store.o tod.o utf8.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 utf8.o $(LIBS) $(LDFLAGS)
+ rm -f string.o
+
+# End
diff --git a/OS/Makefile-Default b/OS/Makefile-Default
new file mode 100644
index 0000000..6e42db4
--- /dev/null
+++ b/OS/Makefile-Default
@@ -0,0 +1,327 @@
+##################################################
+# 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=-lpcre2-8
+
+
+# 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
+
+# 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
+
+
+# If you want to use local_scan() at all, the support code must be included
+# by uncommenting this line.
+
+# HAVE_LOCAL_SCAN=yes
+
+# 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
+
+# A very simple example points to a template function that doesn't actually do
+# any scanning, but just accepts the message. A compilable file must be
+# included in the build even if HAVE_LOCAL_SCAN is not defined.
+
+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 variable 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-FreeBSD b/OS/Makefile-FreeBSD
new file mode 100644
index 0000000..02253d9
--- /dev/null
+++ b/OS/Makefile-FreeBSD
@@ -0,0 +1,54 @@
+# Exim: OS-specific make file for FreeBSD
+# Copyright (c) The Exim Maintainers 2020
+
+CHOWN_COMMAND=/usr/sbin/chown
+#STRIP_COMMAND=/usr/bin/strip
+CHMOD_COMMAND=/bin/chmod
+
+
+# FreeBSD Ports no longer insert compatibility symlinks into /usr/bin for
+# scripting languages which traditionally have had them.
+PERL_COMMAND=/usr/local/bin/perl
+
+HAVE_SA_LEN=YES
+
+# crypt() is in a separate library
+LIBS=-lcrypt -lm -lutil
+
+# Dynamically 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
index 0000000..b49976f
--- /dev/null
+++ b/OS/Makefile-GNU
@@ -0,0 +1,30 @@
+# Exim: OS-specific make file for GNU and variants.
+# Copyright (c) The Exim Maintainers 2020
+
+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-Linux b/OS/Makefile-Linux
new file mode 100644
index 0000000..dfb2fa8
--- /dev/null
+++ b/OS/Makefile-Linux
@@ -0,0 +1,39 @@
+# Exim: OS-specific make file for Linux. This is for modern Linuxes,
+# which use libc6.
+# Copyright (c) The Exim Maintainers 2020
+#
+# 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
+
+# The system cc may be gcc or clang; do not force gcc
+CC=cc
+# 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 = -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-OpenBSD b/OS/Makefile-OpenBSD
new file mode 100644
index 0000000..6976326
--- /dev/null
+++ b/OS/Makefile-OpenBSD
@@ -0,0 +1,30 @@
+# Exim: OS-specific make file for OpenBSD
+# Copyright (c) The Exim Maintainers 2022
+
+CHOWN_COMMAND=/usr/sbin/chown
+CHGRP_COMMAND=/usr/sbin/chgrp
+CHMOD_COMMAND=/bin/chmod
+
+CC=cc
+CFLAGS=-O2 -Wall -Wno-parentheses -Wno-self-assign -Wno-logical-op-parentheses
+
+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 ships with a too-old Berkeley DB. NDBM is the default if we don't specify one.
+#USE_DB=yes
+
+# End
diff --git a/OS/Makefile-SunOS5 b/OS/Makefile-SunOS5
new file mode 100644
index 0000000..3b436f4
--- /dev/null
+++ b/OS/Makefile-SunOS5
@@ -0,0 +1,24 @@
+# Exim: OS-specific make file for SunOS5
+# Copyright (c) The Exim Maintainers 2020
+
+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
+
+OS_C_INCLUDES=setenv.c
+CFLAGS += -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED=1 -D__EXTENSIONS__
+
+# End
diff --git a/OS/eximon.conf-Default b/OS/eximon.conf-Default
new file mode 100644
index 0000000..2f874ef
--- /dev/null
+++ b/OS/eximon.conf-Default
@@ -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
index 0000000..465bc26
--- /dev/null
+++ b/OS/os.Configuring
@@ -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-FreeBSD b/OS/os.c-FreeBSD
new file mode 100644
index 0000000..02b7858
--- /dev/null
+++ b/OS/os.c-FreeBSD
@@ -0,0 +1,49 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Jeremy Harris 1995 - 2020 */
+/* Copyright (c) The Exim Maintainers 2021 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* FreeBSD-specific code. This is concatenated onto the generic
+src/os.c file. */
+
+
+/*************
+Sendfile shim
+*************/
+
+ssize_t
+os_sendfile(int out, int in, off_t * offp, size_t cnt)
+{
+off_t loff = offp ? *offp : 0;
+off_t written;
+
+if (sendfile(in, out, loff, cnt, NULL, &written, 0) < 0) return (ssize_t)-1;
+if (offp) *offp = loff + written;
+return (ssize_t)written;
+}
+
+/*************************************************
+TCP Fast Open: check that the ioctl is accepted
+*************************************************/
+
+#ifndef COMPILE_UTILITY
+void
+tfo_probe(void)
+{
+# ifdef TCP_FASTOPEN
+int sock;
+
+if ( (sock = socket(AF_INET, SOCK_STREAM, 0)) >= 0
+ && setsockopt(sock, IPPROTO_TCP, TCP_FASTOPEN, &on, sizeof(on) >= 0)
+ )
+ f.tcp_fastopen_ok = TRUE;
+close(sock);
+# endif
+}
+#endif
+
+
+/* End of os.c-Linux */
diff --git a/OS/os.c-GNU b/OS/os.c-GNU
new file mode 100644
index 0000000..dbd0149
--- /dev/null
+++ b/OS/os.c-GNU
@@ -0,0 +1,56 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 */
+/* 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-Linux b/OS/os.c-Linux
new file mode 100644
index 0000000..59d81f8
--- /dev/null
+++ b/OS/os.c-Linux
@@ -0,0 +1,165 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1997 - 2018 */
+/* 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 */
+
+
+/*************
+* Sendfile *
+*************/
+#include <sys/sendfile.h>
+
+ssize_t
+os_sendfile(int out, int in, off_t * off, size_t cnt)
+{
+return sendfile(out, in, off, cnt);
+}
+
+/* End of os.c-Linux */
diff --git a/OS/os.c-SunOS5 b/OS/os.c-SunOS5
new file mode 100644
index 0000000..1624869
--- /dev/null
+++ b/OS/os.c-SunOS5
@@ -0,0 +1,16 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 2016 */
+/* Copyright (c) Jeremy Harris 2016 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Solaris-specific code. This is concatenated onto the generic
+src/os.c file. */
+
+#if defined(MISSING_UNSETENV_3) && !defined(COMPILE_UTILITY)
+# include "setenv.c"
+#endif
+
+/* End of os.c-SunOS5 */
diff --git a/OS/os.h-FreeBSD b/OS/os.h-FreeBSD
new file mode 100644
index 0000000..06e217a
--- /dev/null
+++ b/OS/os.h-FreeBSD
@@ -0,0 +1,81 @@
+/* Exim: OS-specific C header file for FreeBSD */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 - 2021 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include <sys/types.h>
+#include <sys/param.h>
+
+#define HAVE_BSD_GETLOADAVG
+#define HAVE_SETCLASSRESOURCES
+#define HAVE_MMAP
+#define HAVE_SYS_MOUNT_H
+#define HAVE_GETIFADDRS
+#define SIOCGIFCONF_GIVES_ADDR
+#define HAVE_SRANDOMDEV
+#define HAVE_ARC4RANDOM
+#define EXIM_HAVE_OPENAT
+#define EXIM_HAVE_FUTIMENS
+
+/* Applications should not call arc4random_stir() explicitly after
+ * FreeBSD r227520 (approximately 1000002).
+ * Set NOT_HAVE_ARC4RANDOM_STIR if the version released is past
+ * that point. */
+#if __FreeBSD_version >= 1000002
+# define NOT_HAVE_ARC4RANDOM_STIR
+#endif
+
+typedef struct flock flock_t;
+
+/* iconv arg2 type: libiconv in Ports uses "const char* * inbuf" and was
+ * traditionally the only approach available. The iconv functionality
+ * in libc is "char ** restrict src".
+ *
+ * <https://www.freebsd.org/doc/en/books/porters-handbook/using-iconv.html>
+ * says that libc has iconv since 2013, in 10-CURRENT. FreeBSD man-pages
+ * shows it included in 10.0-RELEASE. Writing this in 2017, 10.3 is the
+ * oldest supported release, so we should assume non-libiconv by default.
+ * (Actually, people still using old releases past EOL; we shouldn't support
+ * them but I don't want to deal with howls of complaints because we dare
+ * to not support the unsupported, so guard this on FreeBSD 10+)
+ *
+ * Thus we no longer override iconv.
+ *
+ * However, if libiconv is installed, and anything adds /usr/local/include
+ * to include-path (likely) then we'll get that. So define a variable
+ * which makes the libiconv try to not interfere with OS iconv.
+ */
+#if __FreeBSD__ >= 10
+# define LIBICONV_PLUG
+#endif
+/* for more specific version constraints, look at __FreeBSD_version
+ * from <sys/param.h> */
+
+/* When using DKIM, setting OS_SENDFILE can increase
+performance on outgoing mail a bit. */
+
+#define OS_SENDFILE
+extern ssize_t os_sendfile(int, int, off_t *, size_t);
+
+#ifdef PID_T_FMT
+# undef PID_T_FMT
+#endif
+#define PID_T_FMT "%d"
+
+
+/*******************/
+
+#define EXIM_TFO_PROBE
+#define EXIM_TFO_FREEBSD
+
+
+/* for TCP state-variable values, for TFO logging */
+#include <netinet/tcp_fsm.h>
+#define TCP_SYN_RECV TCPS_SYN_RECEIVED
+
+/*******************/
+
+#define EXIM_HAVE_KEVENT
+
+/* End */
diff --git a/OS/os.h-GNU b/OS/os.h-GNU
new file mode 100644
index 0000000..4b22260
--- /dev/null
+++ b/OS/os.h-GNU
@@ -0,0 +1,34 @@
+/* Exim: OS-specific C header file for GNU/Hurd */
+/* Copyright (c) The Exim Maintainers 2020 - 2021 */
+
+#include <features.h>
+
+#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 */
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* setgroups(0, NULL) succeeds, and drops the gid group
+as well as any supplementary groups*/
+#define OS_SETGROUPS_ZERO_DROPS_ALL
+
+#if _POSIX_C_SOURCE >= 200809L || _ATFILE_SOURCE
+# define EXIM_HAVE_OPENAT
+#endif
+
+/* End */
diff --git a/OS/os.h-Linux b/OS/os.h-Linux
new file mode 100644
index 0000000..e6a8d28
--- /dev/null
+++ b/OS/os.h-Linux
@@ -0,0 +1,104 @@
+/* Exim: OS-specific C header file for Linux */
+/* Copyright (c) University of Cambridge 1995 - 2020 */
+/* Copyright (c) The Exim Maintainers 2021 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* 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>
+#include <sys/types.h>
+
+
+#define CRYPT_H
+#define GLIBC_IP_OPTIONS
+#define HAVE_MMAP
+#define HAVE_BSD_GETLOADAVG
+#define HAVE_SYS_STATVFS_H
+#define HAVE_GETIFADDRS
+#define NO_IP_VAR_H
+#define SIG_IGN_WORKS
+
+/* When using DKIM, setting OS_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. As of 2017 those are declared
+to be too old to build by default. */
+
+#define OS_SENDFILE
+extern ssize_t os_sendfile(int, int, off_t *, size_t);
+
+#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
+
+#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
+
+#if _POSIX_C_SOURCE >= 200809L || _ATFILE_SOURCE
+# define EXIM_HAVE_OPENAT
+# define EXIM_HAVE_FUTIMENS
+#endif
+
+/* TCP Fast Open support */
+
+#include <netinet/tcp.h> /* for TCP_FASTOPEN */
+#include <sys/socket.h> /* for MSG_FASTOPEN */
+#if defined(TCP_FASTOPEN) && !defined(MSG_FASTOPEN)
+# define MSG_FASTOPEN 0x20000000
+#endif
+#define EXIM_HAVE_TCPI_UNACKED
+#ifndef TCPI_OPT_SYN_DATA
+# define TCPI_OPT_SYN_DATA 32
+#endif
+
+/* "Abstract" Unix-socket names */
+#define EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+
+/* inotify(7) etc syscalls */
+#define EXIM_HAVE_INOTIFY
+
+/* Needed for uClibc */
+#ifndef NS_MAXMSG
+# define NS_MAXMSG 65535
+#endif
+
+/* End */
diff --git a/OS/os.h-OpenBSD b/OS/os.h-OpenBSD
new file mode 100644
index 0000000..c690b6e
--- /dev/null
+++ b/OS/os.h-OpenBSD
@@ -0,0 +1,65 @@
+/* Exim: OS-specific C header file for OpenBSD */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2021 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#define HAVE_BSD_GETLOADAVG
+#define HAVE_MMAP
+#define HAVE_SYS_MOUNT_H
+#define HAVE_GETIFADDRS
+#define EXIM_HAVE_OPENAT
+#define EXIM_HAVE_FUTIMENS
+#define HAVE_ARC4RANDOM
+/* In May 2014, OpenBSD 5.5 was released which cleaned up the arc4random_* API
+ which removed the arc4random_stir() function. Set NOT_HAVE_ARC4RANDOM_STIR
+ if the version released is past that point. */
+#include <sys/param.h>
+#if OpenBSD >= 201405
+# define NOT_HAVE_ARC4RANDOM_STIR
+#endif
+
+typedef struct flock flock_t;
+
+#define os_strsignal strsignal
+#define OS_STRSIGNAL
+
+typedef struct __res_state *res_state;
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+#ifndef EPROTO
+# define EPROTO 71
+#endif
+
+/* We need to force this; the automatic in buildconfig.c gets %ld */
+#ifdef OFF_T_FMT
+# undef OFF_T_FMT
+# undef LONGLONG_T
+#endif
+#define OFF_T_FMT "%lld"
+#define LONGLONG_T long long int
+
+#ifdef PID_T_FMT
+# undef PID_T_FMT
+#endif
+#define PID_T_FMT "%d"
+
+#ifdef INO_T_FMT
+# undef INO_T_FMT
+#endif
+#define INO_T_FMT "%llu"
+
+#ifdef TIME_T_FMT
+# undef TIME_T_FMT
+#endif
+#define TIME_T_FMT "%lld"
+
+/* seems arpa/nameser.h does not define this.
+Space-constrained devices could use much smaller; a few k. */
+#define NS_MAXMSG 65535
+
+#define EXIM_HAVE_KEVENT
+
+/* End */
diff --git a/OS/os.h-SunOS5 b/OS/os.h-SunOS5
new file mode 100644
index 0000000..7125321
--- /dev/null
+++ b/OS/os.h-SunOS5
@@ -0,0 +1,58 @@
+/* Exim: OS-specific C header file for SunOS5 aka Solaris */
+/* Copyright (c) The Exim Maintainers 2021 */
+
+#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 EXIM_HAVE_OPENAT
+#define EXIM_HAVE_FUTIMENS
+
+#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
+
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+#if _POSIX_C_SOURCE < 200112L
+# define MISSING_UNSETENV_3
+#endif
+
+#if _POSIX_C_SOURCE < 200809L
+# define MISSING_POSIX_MEMALIGN
+#endif
+
+
+/* SunOS5 doesn't accept getcwd(NULL, 0) to auto-allocate
+a buffer */
+
+#define OS_GETCWD
+
+
+#ifndef MIN
+# define MIN(a,b) (((a)<(b))?(a):(b))
+# define MAX(a,b) (((a)>(b))?(a):(b))
+#endif
+
+/* End */
diff --git a/OS/unsupported/Makefile-AIX b/OS/unsupported/Makefile-AIX
new file mode 100644
index 0000000..fc32aa2
--- /dev/null
+++ b/OS/unsupported/Makefile-AIX
@@ -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/unsupported/Makefile-BSDI b/OS/unsupported/Makefile-BSDI
new file mode 100644
index 0000000..d56aa9b
--- /dev/null
+++ b/OS/unsupported/Makefile-BSDI
@@ -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/unsupported/Makefile-CYGWIN b/OS/unsupported/Makefile-CYGWIN
new file mode 100644
index 0000000..5e608fe
--- /dev/null
+++ b/OS/unsupported/Makefile-CYGWIN
@@ -0,0 +1,113 @@
+# OS-specific file for Cygwin.
+
+# This file provided by Pierre A. Humblet <Pierre.Humblet@ieee.org>
+
+HAVE_IPV6 = yes
+HAVE_ICONV = yes
+# Use c99 to have %z
+CFLAGS= -g -Wall -std=c99 -U __STRICT_ANSI__
+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 normally 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
+
+#DISABLE_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_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
+
+# All modes are in octal and must start with 0
+EXIMDB_DIRECTORY_MODE = 01777
+EXIMDB_MODE = 0666
+EXIMDB_LOCKFILE_MODE = 0666
+INPUT_DIRECTORY_MODE = 01777
+LOG_DIRECTORY_MODE = 01777
+LOG_MODE = 0666
+MSGLOG_DIRECTORY_MODE = 01777
+SPOOL_DIRECTORY_MODE = 01777
+SPOOL_MODE = 0600
+
+# End
diff --git a/OS/unsupported/Makefile-DGUX b/OS/unsupported/Makefile-DGUX
new file mode 100644
index 0000000..667c63f
--- /dev/null
+++ b/OS/unsupported/Makefile-DGUX
@@ -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/unsupported/Makefile-Darwin b/OS/unsupported/Makefile-Darwin
new file mode 100644
index 0000000..517bbc4
--- /dev/null
+++ b/OS/unsupported/Makefile-Darwin
@@ -0,0 +1,33 @@
+# 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
+#
+# 2020/05/12 disable TLS resume support; it results in
+# "1 select() failure: No such file or directory"
+# being logged by the daeomn (sending the testsuite red...)
+
+CFLAGS=-O -no-cpp-precomp -DDISABLE_TLS_RESUME
+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/unsupported/Makefile-DragonFly b/OS/unsupported/Makefile-DragonFly
new file mode 100644
index 0000000..c49c59f
--- /dev/null
+++ b/OS/unsupported/Makefile-DragonFly
@@ -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/unsupported/Makefile-GNUkFreeBSD b/OS/unsupported/Makefile-GNUkFreeBSD
new file mode 100644
index 0000000..8019281
--- /dev/null
+++ b/OS/unsupported/Makefile-GNUkFreeBSD
@@ -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/unsupported/Makefile-GNUkNetBSD b/OS/unsupported/Makefile-GNUkNetBSD
new file mode 100644
index 0000000..8019281
--- /dev/null
+++ b/OS/unsupported/Makefile-GNUkNetBSD
@@ -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/unsupported/Makefile-HI-OSF b/OS/unsupported/Makefile-HI-OSF
new file mode 100644
index 0000000..da3d487
--- /dev/null
+++ b/OS/unsupported/Makefile-HI-OSF
@@ -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/unsupported/Makefile-HI-UX b/OS/unsupported/Makefile-HI-UX
new file mode 100644
index 0000000..870ee84
--- /dev/null
+++ b/OS/unsupported/Makefile-HI-UX
@@ -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/unsupported/Makefile-HP-UX b/OS/unsupported/Makefile-HP-UX
new file mode 100644
index 0000000..ea35144
--- /dev/null
+++ b/OS/unsupported/Makefile-HP-UX
@@ -0,0 +1,27 @@
+# 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
+
+OS_C_INCLUDES=setenv.c
+
+# End
diff --git a/OS/unsupported/Makefile-HP-UX-9 b/OS/unsupported/Makefile-HP-UX-9
new file mode 100644
index 0000000..1530009
--- /dev/null
+++ b/OS/unsupported/Makefile-HP-UX-9
@@ -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/unsupported/Makefile-IRIX b/OS/unsupported/Makefile-IRIX
new file mode 100644
index 0000000..7b95783
--- /dev/null
+++ b/OS/unsupported/Makefile-IRIX
@@ -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/unsupported/Makefile-IRIX6 b/OS/unsupported/Makefile-IRIX6
new file mode 100644
index 0000000..be01138
--- /dev/null
+++ b/OS/unsupported/Makefile-IRIX6
@@ -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/unsupported/Makefile-IRIX632 b/OS/unsupported/Makefile-IRIX632
new file mode 100644
index 0000000..b567fc6
--- /dev/null
+++ b/OS/unsupported/Makefile-IRIX632
@@ -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/unsupported/Makefile-IRIX65 b/OS/unsupported/Makefile-IRIX65
new file mode 100644
index 0000000..50e7745
--- /dev/null
+++ b/OS/unsupported/Makefile-IRIX65
@@ -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/unsupported/Makefile-NetBSD b/OS/unsupported/Makefile-NetBSD
new file mode 100644
index 0000000..35d03a2
--- /dev/null
+++ b/OS/unsupported/Makefile-NetBSD
@@ -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/unsupported/Makefile-NetBSD-a.out b/OS/unsupported/Makefile-NetBSD-a.out
new file mode 100644
index 0000000..e210efd
--- /dev/null
+++ b/OS/unsupported/Makefile-NetBSD-a.out
@@ -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/unsupported/Makefile-OSF1 b/OS/unsupported/Makefile-OSF1
new file mode 100644
index 0000000..811ca07
--- /dev/null
+++ b/OS/unsupported/Makefile-OSF1
@@ -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/unsupported/Makefile-OpenUNIX b/OS/unsupported/Makefile-OpenUNIX
new file mode 100644
index 0000000..e4d7261
--- /dev/null
+++ b/OS/unsupported/Makefile-OpenUNIX
@@ -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/unsupported/Makefile-QNX b/OS/unsupported/Makefile-QNX
new file mode 100644
index 0000000..3cf81c4
--- /dev/null
+++ b/OS/unsupported/Makefile-QNX
@@ -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/unsupported/Makefile-SCO b/OS/unsupported/Makefile-SCO
new file mode 100644
index 0000000..baa61d8
--- /dev/null
+++ b/OS/unsupported/Makefile-SCO
@@ -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/unsupported/Makefile-SCO_SV b/OS/unsupported/Makefile-SCO_SV
new file mode 100644
index 0000000..249b81a
--- /dev/null
+++ b/OS/unsupported/Makefile-SCO_SV
@@ -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/unsupported/Makefile-SunOS4 b/OS/unsupported/Makefile-SunOS4
new file mode 100644
index 0000000..c876998
--- /dev/null
+++ b/OS/unsupported/Makefile-SunOS4
@@ -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/unsupported/Makefile-SunOS5-hal b/OS/unsupported/Makefile-SunOS5-hal
new file mode 100644
index 0000000..05ea893
--- /dev/null
+++ b/OS/unsupported/Makefile-SunOS5-hal
@@ -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/unsupported/Makefile-ULTRIX b/OS/unsupported/Makefile-ULTRIX
new file mode 100644
index 0000000..9e912b3
--- /dev/null
+++ b/OS/unsupported/Makefile-ULTRIX
@@ -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/unsupported/Makefile-UNIX_SV b/OS/unsupported/Makefile-UNIX_SV
new file mode 100644
index 0000000..bfcfae1
--- /dev/null
+++ b/OS/unsupported/Makefile-UNIX_SV
@@ -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/unsupported/Makefile-USG b/OS/unsupported/Makefile-USG
new file mode 100644
index 0000000..753a2d7
--- /dev/null
+++ b/OS/unsupported/Makefile-USG
@@ -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/unsupported/Makefile-Unixware7 b/OS/unsupported/Makefile-Unixware7
new file mode 100644
index 0000000..88a8838
--- /dev/null
+++ b/OS/unsupported/Makefile-Unixware7
@@ -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/unsupported/Makefile-mips b/OS/unsupported/Makefile-mips
new file mode 100644
index 0000000..ff33139
--- /dev/null
+++ b/OS/unsupported/Makefile-mips
@@ -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/unsupported/README b/OS/unsupported/README
new file mode 100644
index 0000000..73790ae
--- /dev/null
+++ b/OS/unsupported/README
@@ -0,0 +1,14 @@
+Files in this directory are historical. They may have worked once but the
+project has no assurance that they still do.
+
+If you need to use one for a build for your platform, copy it up one directory
+level first. We'll reinstate it given a current version and evidence of testing.
+For the latter please look into the project regression testsuite, and please
+consider operating a buildfarm animal in the long term (it runs the testsuite).
+
+The buildfarm status page is:
+ https://buildfarm.exim.org/cgi-bin/show_status.pl
+There's a "register" link there with a link to how-to instructions. Please do
+monitor the status of your animal on an ongoing basis. The exim-users or
+exim-dev mailinglist are good places to ask for help and to discuss any regressions
+seen in test runs. There is also the #exim IRC channel on Freenode.
diff --git a/OS/unsupported/os.c-BSDI b/OS/unsupported/os.c-BSDI
new file mode 100644
index 0000000..03a7a1c
--- /dev/null
+++ b/OS/unsupported/os.c-BSDI
@@ -0,0 +1,19 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) 2016 Heiko Schlittermann <hs@schlittermann.de> */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* BSDI-specific code. This is concatenated onto the generic
+src/os.c file. */
+
+#ifndef OS_UNSETENV
+#define OS_UNSETENV
+
+int
+os_unsetenv(const uschar * name)
+{
+unsetenv(CS name);
+return 0;
+}
diff --git a/OS/unsupported/os.c-HI-OSF b/OS/unsupported/os.c-HI-OSF
new file mode 100644
index 0000000..5e3d336
--- /dev/null
+++ b/OS/unsupported/os.c-HI-OSF
@@ -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/unsupported/os.c-HP-UX b/OS/unsupported/os.c-HP-UX
new file mode 100644
index 0000000..fdd8708
--- /dev/null
+++ b/OS/unsupported/os.c-HP-UX
@@ -0,0 +1,16 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 2016 */
+/* Copyright (c) Jeremy Harris 2016 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* HP-UX-specific code. This is concatenated onto the generic
+src/os.c file. */
+
+#ifndef COMPILE_UTILITY
+# include "setenv.c"
+#endif
+
+/* End of os.c-SunHP-UX */
diff --git a/OS/unsupported/os.c-IRIX b/OS/unsupported/os.c-IRIX
new file mode 100644
index 0000000..1917238
--- /dev/null
+++ b/OS/unsupported/os.c-IRIX
@@ -0,0 +1,119 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* 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, GET_UNTAINTED);
+
+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 = CS 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/unsupported/os.c-IRIX6 b/OS/unsupported/os.c-IRIX6
new file mode 100644
index 0000000..1917238
--- /dev/null
+++ b/OS/unsupported/os.c-IRIX6
@@ -0,0 +1,119 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* 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, GET_UNTAINTED);
+
+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 = CS 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/unsupported/os.c-IRIX632 b/OS/unsupported/os.c-IRIX632
new file mode 100644
index 0000000..1917238
--- /dev/null
+++ b/OS/unsupported/os.c-IRIX632
@@ -0,0 +1,119 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* 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, GET_UNTAINTED);
+
+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 = CS 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/unsupported/os.c-IRIX65 b/OS/unsupported/os.c-IRIX65
new file mode 100644
index 0000000..1917238
--- /dev/null
+++ b/OS/unsupported/os.c-IRIX65
@@ -0,0 +1,119 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* 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, GET_UNTAINTED);
+
+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 = CS 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/unsupported/os.c-OSF1 b/OS/unsupported/os.c-OSF1
new file mode 100644
index 0000000..ad91b63
--- /dev/null
+++ b/OS/unsupported/os.c-OSF1
@@ -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/unsupported/os.c-cygwin b/OS/unsupported/os.c-cygwin
new file mode 100644
index 0000000..5ca05a8
--- /dev/null
+++ b/OS/unsupported/os.c-cygwin
@@ -0,0 +1,531 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Cygwin-specific code. December 2002. Updated Jan 2015.
+ This is prefixed to the 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);
+}
+
+#ifndef COMPILE_UTILITY /* Utilities don't need special code */
+
+#ifdef INCLUDE_PAM
+#include "../pam/pam.c"
+#endif
+#include <alloca.h>
+
+unsigned int cygwin_WinVersion;
+
+/* Conflict between Windows definitions and others */
+#ifdef NOERROR
+#undef NOERROR
+#endif
+#ifdef DELETE
+#undef DELETE
+#endif
+
+#include <windows.h>
+#include <ntstatus.h>
+#include <lmcons.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 fakesetugid = 1; /* when not privileged, setugid = noop */
+
+#undef setuid
+int cygwin_setuid(uid_t uid )
+{
+ int res = 0;
+ if (fakesetugid == 0) {
+ res = setuid(uid);
+ if (cygwin_debug)
+ fprintf(stderr, "setuid %u %u %d pid: %d\n",
+ uid, getuid(),res, getpid());
+ }
+ return res;
+}
+
+#undef setgid
+int cygwin_setgid(gid_t gid )
+{
+ int res = 0;
+ if (fakesetugid == 0) {
+ res = setgid(gid);
+ if (cygwin_debug)
+ fprintf(stderr, "setgid %u %u %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,
+};
+
+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;
+ if (ret == (CREATE_BIT))
+ break;
+ }
+ }
+ else
+ fprintf(stderr, "has_create_token_privilege %u\n", GetLastError());
+
+ if (hToken)
+ CloseHandle(hToken);
+
+ return ret;
+}
+
+/*
+ We use cygwin_premain to fake a few things
+ and to provide some debug info
+*/
+void cygwin_premain2(int argc, char ** argv, struct per_process * ptr)
+{
+ int i, res, is_daemon = 0, is_spoolwritable, is_privileged, is_eximuser;
+ uid_t myuid, systemuid;
+ gid_t mygid, adminsgid;
+ struct passwd * pwp = NULL;
+ struct stat buf;
+ char *cygenv;
+ 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') {
+ ssize_t size;
+ wchar_t *win32_path;
+ argv[i][1] = 'n'; /* Replace -c by -n */
+ cygwin_debug = TRUE;
+ fprintf(stderr, "CYGWIN = \"%s\".\n", cygenv);
+ if (((size = cygwin_conv_path(CCP_POSIX_TO_WIN_W,"/", win32_path, 0)) > 0)
+ && ((win32_path = store_malloc(size)) != NULL)
+ && (cygwin_conv_path(CCP_POSIX_TO_WIN_W,"/", win32_path, size) == 0)) {
+ fprintf(stderr, " Root / mapped to %ls.\n", win32_path);
+ store_free(win32_path);
+ }
+ }
+ else if (argv[i][1] == 'b' && argv[i][2] == 'd') {
+ is_daemon = 1;
+ cygwin_setpriority();
+ }
+ }
+ }
+
+ /* Nt/2000/XP
+ We initially set the exim uid & gid to those of the "exim user",
+ 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 allow exim imposed restrictions (bypassable by recompiling)
+ and to avoid exec that cause loss of privilege
+ If not privileged and unable to chown,
+ we set the exim uid to our uid.
+ If unprivileged and /var/spool/exim is writable and not running as listening daemon,
+ we fake all subsequent setuid. */
+
+ /* Get the system and admins uid from their sids */
+ if ((systemuid = cygwin_internal(CW_GET_UID_FROM_SID, & SystemSid)) == -1) {
+ fprintf(stderr, "Cannot map System sid. Aborting\n");
+ exit(1);
+ }
+ if ((adminsgid = cygwin_internal(CW_GET_GID_FROM_SID, & AdminsSid)) == -1) {
+ fprintf(stderr, "Cannot map Admins sid. Aborting\n");
+ exit(1);
+ }
+
+ priv_flags = get_privileges ();
+ is_privileged = !!(priv_flags & CREATE_BIT);
+
+ /* Call getpwnam for account exim after getting the local exim name */
+ char exim_username[DNLEN + UNLEN + 2];
+ if (cygwin_internal(CW_CYGNAME_FROM_WINNAME, "exim", exim_username, sizeof exim_username) != 0)
+ pwp = getpwnam (exim_username);
+
+ /* If cannot setuid to exim or and is not the daemon (which is assumed to be
+ able to chown or to be the exim user) set the exim ugid to our ugid to avoid
+ chown failures after creating files and to be able to setuid to exim in
+ exim.c ( "privilege not needed" ). */
+ if ((is_privileged == 0) && (!is_daemon)) {
+ exim_uid = myuid;
+ exim_gid = mygid;
+ }
+ else if (pwp != NULL) {
+ exim_uid = pwp->pw_uid; /* Set it according to passwd */
+ exim_gid = pwp->pw_gid;
+ is_eximuser = 1;
+ }
+ else {
+ exim_uid = systemuid;
+ exim_gid = adminsgid;
+ is_eximuser = 0;
+ }
+
+ res = stat("/var/spool/exim", &buf);
+ /* Check if writable (and can be stat) */
+ is_spoolwritable = ((res == 0) && ((buf.st_mode & S_IWOTH) != 0));
+
+ fakesetugid = (is_privileged == 0) && (is_daemon == 0) && (is_spoolwritable == 1);
+
+ if (is_privileged) { /* Can setuid */
+ if (cygwin_setgid(exim_gid) /* Setuid to exim */
+ || cygwin_setuid(exim_uid)) {
+ fprintf(stderr, "Unable to setuid/gid to exim. priv_flags: %x\n", priv_flags);
+ exit(0); /* Problem... Perhaps not in 544 */
+ }
+ }
+
+ /* Set the configuration file uid and gid to the system uid and admins gid. */
+ config_uid = systemuid;
+ config_gid = adminsgid;
+
+ /* Pretend we are root to avoid useless exec
+ and avoid exim set limitations.
+ We are limited by file access rights */
+ root_uid = getuid ();
+
+ if (cygwin_debug) {
+ fprintf(stderr, "Starting uid %u, gid %u, priv_flags %x, is_privileged %d, is_daemon %d, is_spoolwritable %d.\n",
+ myuid, mygid, priv_flags, is_privileged, is_daemon, is_spoolwritable);
+ fprintf(stderr, "root_uid %u, exim_uid %u, exim_gid %u, config_uid %u, config_gid %u, is_eximuser %d.\n",
+ root_uid, exim_uid, exim_gid, config_uid, config_gid, is_eximuser);
+ }
+ 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
+
+ Uses NtQuerySystemInformation.
+ This requires definitions that are not part of
+ standard include files.
+
+ This is discouraged starting with WinXP.
+
+*************************************************************/
+/* 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 */
+} cygwin_perf_t;
+
+static struct {
+ HANDLE handle;
+ pid_t pid;
+ cygwin_perf_t *perf;
+} cygwin_load = {NULL, 0, NULL};
+
+#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: %u (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: %u (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: %u (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;
+}
+
+/*****************************************************************
+ *
+ 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);
+
+ /* 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;
+ }
+}
+
+
+/*****************************************************************
+ *
+ 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;
+
+ if (!LoadNtdll()) {
+ log_write(0, LOG_MAIN, "Cannot obtain Load Average");
+ cygwin_load.perf = NULL;
+ return -1;
+ }
+
+ 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 %p\n", (void *) 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 %p\n", (void *) 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/unsupported/os.h-AIX b/OS/unsupported/os.h-AIX
new file mode 100644
index 0000000..5cd4501
--- /dev/null
+++ b/OS/unsupported/os.h-AIX
@@ -0,0 +1,27 @@
+/* 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;
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+
+/* End */
diff --git a/OS/unsupported/os.h-BSDI b/OS/unsupported/os.h-BSDI
new file mode 100644
index 0000000..a1705ec
--- /dev/null
+++ b/OS/unsupported/os.h-BSDI
@@ -0,0 +1,15 @@
+/* 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
+#define OS_UNSETENV
+
+typedef struct flock flock_t;
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-DGUX b/OS/unsupported/os.h-DGUX
new file mode 100644
index 0000000..9040f0e
--- /dev/null
+++ b/OS/unsupported/os.h-DGUX
@@ -0,0 +1,28 @@
+/* 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
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-Darwin b/OS/unsupported/os.h-Darwin
new file mode 100644
index 0000000..a85e92f
--- /dev/null
+++ b/OS/unsupported/os.h-Darwin
@@ -0,0 +1,64 @@
+/* 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
+#define EXIM_HAVE_OPENAT
+
+
+#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
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* seems arpa/nameser.h does not define this */
+#define NS_MAXMSG 65535
+
+/* There may be very many supplementary groups for the user. See notes
+in "man 2 getgroups". */
+#define _DARWIN_UNLIMITED_GETGROUPS
+#define EXIM_GROUPLIST_SIZE 64
+
+/* TCP Fast Open: Darwin uses a connectx() call
+rather than a modified sendto() */
+#define EXIM_TFO_CONNECTX
+
+/* MacOS, at least on the buildfarm animal, does not seem to push out
+the SMTP response to QUIT with our usual handling which is trying to get
+the client to FIN first so that the server does not get the TIME_WAIT */
+#define SERVERSIDE_CLOSE_NOWAIT
+
+/* End */
diff --git a/OS/unsupported/os.h-DragonFly b/OS/unsupported/os.h-DragonFly
new file mode 100644
index 0000000..4c2f1d5
--- /dev/null
+++ b/OS/unsupported/os.h-DragonFly
@@ -0,0 +1,13 @@
+/* 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;
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-GNUkFreeBSD b/OS/unsupported/os.h-GNUkFreeBSD
new file mode 100644
index 0000000..ab35031
--- /dev/null
+++ b/OS/unsupported/os.h-GNUkFreeBSD
@@ -0,0 +1,25 @@
+/* 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
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-GNUkNetBSD b/OS/unsupported/os.h-GNUkNetBSD
new file mode 100644
index 0000000..bc3bc25
--- /dev/null
+++ b/OS/unsupported/os.h-GNUkNetBSD
@@ -0,0 +1,25 @@
+/* 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
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-HI-OSF b/OS/unsupported/os.h-HI-OSF
new file mode 100644
index 0000000..0f50fb6
--- /dev/null
+++ b/OS/unsupported/os.h-HI-OSF
@@ -0,0 +1,12 @@
+/* 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 *
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-HI-UX b/OS/unsupported/os.h-HI-UX
new file mode 100644
index 0000000..f3df963
--- /dev/null
+++ b/OS/unsupported/os.h-HI-UX
@@ -0,0 +1,21 @@
+/* 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;
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-HP-UX b/OS/unsupported/os.h-HP-UX
new file mode 100644
index 0000000..4998734
--- /dev/null
+++ b/OS/unsupported/os.h-HP-UX
@@ -0,0 +1,34 @@
+/* Exim: OS-specific C header file for HP-UX versions greater than 9 */
+
+#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)
+
+/* Determined by sockaddr_un */
+
+struct sockaddr_storage
+{
+ short ss_family;
+ char __ss_padding[92];
+};
+
+/* End */
diff --git a/OS/unsupported/os.h-HP-UX-9 b/OS/unsupported/os.h-HP-UX-9
new file mode 100644
index 0000000..5a260d6
--- /dev/null
+++ b/OS/unsupported/os.h-HP-UX-9
@@ -0,0 +1,23 @@
+/* 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;
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-IRIX b/OS/unsupported/os.h-IRIX
new file mode 100644
index 0000000..1d4bf46
--- /dev/null
+++ b/OS/unsupported/os.h-IRIX
@@ -0,0 +1,17 @@
+/* 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
+
+/* End */
diff --git a/OS/unsupported/os.h-IRIX6 b/OS/unsupported/os.h-IRIX6
new file mode 100644
index 0000000..bf30767
--- /dev/null
+++ b/OS/unsupported/os.h-IRIX6
@@ -0,0 +1,16 @@
+/* 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
+
+/* End */
diff --git a/OS/unsupported/os.h-IRIX632 b/OS/unsupported/os.h-IRIX632
new file mode 100644
index 0000000..90f1c58
--- /dev/null
+++ b/OS/unsupported/os.h-IRIX632
@@ -0,0 +1,18 @@
+/* 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
+
+/* End */
diff --git a/OS/unsupported/os.h-IRIX65 b/OS/unsupported/os.h-IRIX65
new file mode 100644
index 0000000..4b248fe
--- /dev/null
+++ b/OS/unsupported/os.h-IRIX65
@@ -0,0 +1,16 @@
+/* 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
+
+/* End */
diff --git a/OS/unsupported/os.h-NetBSD b/OS/unsupported/os.h-NetBSD
new file mode 100644
index 0000000..0b9fc03
--- /dev/null
+++ b/OS/unsupported/os.h-NetBSD
@@ -0,0 +1,31 @@
+/* Exim: OS-specific C header file for NetBSD */
+/* Copyright (c) The Exim Maintainers 2021 */
+
+#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
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+#define EXIM_HAVE_KEVENT
+
+/* End */
diff --git a/OS/unsupported/os.h-NetBSD-a.out b/OS/unsupported/os.h-NetBSD-a.out
new file mode 100644
index 0000000..29a8fee
--- /dev/null
+++ b/OS/unsupported/os.h-NetBSD-a.out
@@ -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/unsupported/os.h-OSF1 b/OS/unsupported/os.h-OSF1
new file mode 100644
index 0000000..6b5fa49
--- /dev/null
+++ b/OS/unsupported/os.h-OSF1
@@ -0,0 +1,16 @@
+/* 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
+
+/* End */
diff --git a/OS/unsupported/os.h-OpenUNIX b/OS/unsupported/os.h-OpenUNIX
new file mode 100644
index 0000000..67d1063
--- /dev/null
+++ b/OS/unsupported/os.h-OpenUNIX
@@ -0,0 +1,19 @@
+/* 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
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-QNX b/OS/unsupported/os.h-QNX
new file mode 100644
index 0000000..798f799
--- /dev/null
+++ b/OS/unsupported/os.h-QNX
@@ -0,0 +1,24 @@
+/* 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;
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-SCO b/OS/unsupported/os.h-SCO
new file mode 100644
index 0000000..e5e915e
--- /dev/null
+++ b/OS/unsupported/os.h-SCO
@@ -0,0 +1,21 @@
+/* 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
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-SCO_SV b/OS/unsupported/os.h-SCO_SV
new file mode 100644
index 0000000..0ca29f7
--- /dev/null
+++ b/OS/unsupported/os.h-SCO_SV
@@ -0,0 +1,19 @@
+/* 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
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-SunOS4 b/OS/unsupported/os.h-SunOS4
new file mode 100644
index 0000000..6555620
--- /dev/null
+++ b/OS/unsupported/os.h-SunOS4
@@ -0,0 +1,39 @@
+/* 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
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-SunOS5-hal b/OS/unsupported/os.h-SunOS5-hal
new file mode 100644
index 0000000..cd9e877
--- /dev/null
+++ b/OS/unsupported/os.h-SunOS5-hal
@@ -0,0 +1,14 @@
+/* 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
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-ULTRIX b/OS/unsupported/os.h-ULTRIX
new file mode 100644
index 0000000..08db5ae
--- /dev/null
+++ b/OS/unsupported/os.h-ULTRIX
@@ -0,0 +1,18 @@
+/* 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;
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-UNIX_SV b/OS/unsupported/os.h-UNIX_SV
new file mode 100644
index 0000000..4943a07
--- /dev/null
+++ b/OS/unsupported/os.h-UNIX_SV
@@ -0,0 +1,25 @@
+/* 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
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-USG b/OS/unsupported/os.h-USG
new file mode 100644
index 0000000..e769220
--- /dev/null
+++ b/OS/unsupported/os.h-USG
@@ -0,0 +1,19 @@
+/* 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
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-Unixware7 b/OS/unsupported/os.h-Unixware7
new file mode 100644
index 0000000..4d3ed42
--- /dev/null
+++ b/OS/unsupported/os.h-Unixware7
@@ -0,0 +1,18 @@
+/* Exim: OS-specific C header file for Unixware 7 */
+
+#define NO_SYSEXITS
+
+#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/unsupported/os.h-cygwin b/OS/unsupported/os.h-cygwin
new file mode 100644
index 0000000..6ef59e0
--- /dev/null
+++ b/OS/unsupported/os.h-cygwin
@@ -0,0 +1,41 @@
+/* Exim: OS-specific C header file for Cygwin */
+
+/* This code was supplied by Pierre A. Humblet <Pierre.Humblet@ieee.org>
+ December 2002. Updated Jan 2015. */
+
+/* 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
+
+#define os_strsignal strsignal
+#define OS_STRSIGNAL
+#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
+/* 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}}
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/OS/unsupported/os.h-mips b/OS/unsupported/os.h-mips
new file mode 100644
index 0000000..325e3a1
--- /dev/null
+++ b/OS/unsupported/os.h-mips
@@ -0,0 +1,27 @@
+/* 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[];
+
+/* default is non-const */
+#define ICONV_ARG2_TYPE const char **
+
+/* End */
diff --git a/README b/README
new file mode 100644
index 0000000..d9379f7
--- /dev/null
+++ b/README
@@ -0,0 +1,350 @@
+THE EXIM MAIL TRANSFER AGENT VERSION 4
+--------------------------------------
+
+Copyright (c) 1995 - 2018 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 website at https://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>
+ Local/Makefile-<buildname>
+ 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
index 0000000..d700dd0
--- /dev/null
+++ b/README.DSN
@@ -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 possibility 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
index 0000000..72bc970
--- /dev/null
+++ b/README.UPDATING
@@ -0,0 +1,885 @@
+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.95
+-----------------
+
+Various length limits have been applied to Exim's parsing of its command-line.
+These are all set to be at least as long as any valid input, so we do not believe
+that any real use-cases have been affected by this.
+
+The names of various drivers (authenticators, routers, transports, ...) have
+always been limited to 64 characters, but before this release the names were
+silently truncated, inviting problems. Now the length limit should be enforced.
+If this affects you, then please rename to use shorter names.
+
+The default maximum number of recipients of a single email has changed from
+"unlimited" (ie: as much as CPU and memory will allow, until something breaks
+badly) to 50,000. You can raise or lower this as you see fit, but we strongly
+caution against using zero/unlimited.
+
+
+Exim version 4.94
+-----------------
+
+Some Transports now refuse to use tainted data in constructing their delivery
+location; this WILL BREAK configurations which are not updated accordingly.
+In particular: any Transport use of $local_part which has been relying upon
+check_local_user far away in the Router to make it safe, should be updated to
+replace $local_part with $local_part_data.
+
+Attempting to remove, in router or transport, a header name that ends with
+an asterisk (which is a standards-legal name) will now result in all headers
+named starting with the string before the asterisk being removed. We recommend
+staying away from such names, if they are private ones (and in case of future
+enhancements, alao header names that look like REs).
+
+
+Exim version 4.93
+-----------------
+
+For a detailed list of changes that might affect Exim's operation with
+an unchanged configuration, please see the doc/ChangeLog file.
+
+Build:
+
+ * SUPPORT_DMARC replaces EXPERIMENTAL_DMARC
+
+ * DISABLE_TLS replaces SUPPORT_TLS
+
+ * Bump the version for the local_scan API.
+
+Runtime:
+
+ * smtp transport option hosts_try_fastopen defaults to "*".
+
+ * DNSSec is requested (not required) for all queries. (This seemes to
+ ask for trouble if your resolver is a systemd-resolved.)
+
+ * Generic router option retry_use_local_part defaults to "true" under specific
+ pre-conditions.
+
+ * Introduce a tainting mechanism for values read from untrusted sources.
+
+ * Use longer file names for temporary spool files (this avoids
+ name conflicts with spool on a shared file system).
+
+ * Use dsn_from main config option (was ignored previously).
+
+
+Exim version 4.92
+-----------------
+
+ * Exim used to manually follow CNAME chains, to a limited depth. In this
+ day-and-age we expect the resolver to be doing this for us, so the loop
+ is limited to one retry unless the (new) config option dns_cname_loops
+ is changed.
+
+Exim version 4.91
+-----------------
+
+ * DANE and SPF have been promoted from Experimental to Supported status, thus
+ the options to enable them in Local/Makefile have been renamed.
+ See current src/EDITME for full details, including changes in dependencies,
+ but loosely: replace EXPERIMENTAL_SPF with SUPPORT_SPF and replace
+ EXPERIMENTAL_DANE with SUPPORT_DANE.
+
+ * Ancient ClamAV stream support, long deprecated by ClamAV, has been removed;
+ if you were building with WITH_OLD_CLAMAV_STREAM enabled then your problems
+ have marginally increased.
+
+ * A number of logging changes; if relying upon the previous DKIM additional
+ log-line, explicit log_selector configuration is needed to keep it.
+
+ * Other incompatible changes in EXPERIMENTAL_* features, read NewStuff and
+ ChangeLog carefully if relying upon an experimental feature such as DMARC.
+ Note that this includes changes to SPF as it was promoted into Supported.
+
+
+Exim version 4.89
+-----------------
+
+ * SMTP CHUNKING in Exim 4.88 did not ensure that received mails had a final
+ newline; attempts to deliver such messages onwards to non-chunking hosts
+ would probably hang, as Exim does not insert the newline before a ".".
+ In 4.89, the newline is added upon receipt. For already-received messages
+ in your queue, try util/chunking_fixqueue_finalnewlines.pl
+ to walk the queue, fixing any affected messages. Note that because a
+ delivery attempt will be hanging, attempts to lock the messages for fixing
+ them will stall; stopping all queue-runners temporarily is recommended.
+
+ * OpenSSL: oldest supported release series is now 1.0.2, which is the oldest
+ supported by the OpenSSL project. If you can build Exim with an older
+ release series, congratulations. If you can't, then upgrade.
+ The file doc/openssl.txt contains instructions for installing a current
+ OpenSSL outside the system library paths and building Exim to use it.
+
+ * FreeBSD: we now always use the system iconv in libc, as all versions of
+ FreeBSD supported by the FreeBSD project provide this functionality.
+
+
+Exim version 4.88
+-----------------
+
+ * The "demime" ACL condition, deprecated for the past 10 years, has
+ now been removed.
+
+ * Old GnuTLS configuration options "gnutls_require_kx", "gnutls_require_mac",
+ and "gnutls_require_protocols" have now been removed. (Inoperative from
+ 4.80, per below; logging warnings since 4.83, again per below).
+
+
+Exim version 4.83
+-----------------
+
+ * SPF condition results renamed "permerror" and "temperror". The old
+ names are still accepted for back-compatibility, 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
+ occurrence.
+
+ * 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 superseded 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
+ correctly 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/conf b/conf
new file mode 100644
index 0000000..1619c0d
--- /dev/null
+++ b/conf
@@ -0,0 +1,2 @@
+perl_startup = $| = 1; print "<${^TAINT}>\n";
+perl_taintmode = yes
diff --git a/doc/ChangeLog b/doc/ChangeLog
new file mode 100644
index 0000000..3e6da91
--- /dev/null
+++ b/doc/ChangeLog
@@ -0,0 +1,8277 @@
+This document describes *changes* to previous versions, that might
+affect Exim's operation, with an unchanged configuration file. For new
+options, and new features, see the NewStuff file next to this ChangeLog.
+
+Exim version 4.96
+-----------------
+
+JH/01 Move the wait-for-next-tick (needed for unique messmage IDs) from
+ after reception to before a subsequent reception. This should
+ mean slightly faster delivery, and also confirmation of reception
+ to senders.
+
+JH/02 Move from using the pcre library to pcre2. The former is no longer
+ being developed or supported (by the original developer).
+
+JH/03 Constification work in the filters module required a major version
+ bump for the local-scan API. Specifically, the "headers_charset"
+ global which is visible via the API is now const and may therefore
+ not be modified by local-scan code.
+
+JH/04 Fix ClamAV TCP use under FreeBSD. Previously the OS-specific shim for
+ sendfile() didi not account for the way the ClamAV driver code called it.
+
+JH/05 Bug 2819: speed up command-line messages being read in. Previously a
+ time check was being done for every character; replace that with one
+ per buffer.
+
+JH/06 Bug 2815: Fix ALPN sent by server under OpenSSL. Previously the string
+ sent was prefixed with a length byte.
+
+JH/07 Change the SMTP feature name for pipelining connect to be compliant with
+ RFC 5321. Previously Dovecot (at least) would log errors during
+ submission.
+
+JH/08 Remove stripping of the binaries from the FreeBSD build. This was added
+ in 4.61 without a reason logged. Binaries will be bigger, which might
+ matter on diskspace-constrained systems, but debug is easier.
+
+JH/09 Fix macro-definition during "-be" expansion testing. The move to
+ write-protected store for macros had not accounted for these runtime
+ additions; fix by removing this protection for "-be" mode.
+
+JH/10 Convert all uses of select() to poll(). FreeBSD 12.2 was found to be
+ handing out large-numbered file descriptors, violating the usual Unix
+ assumption (and required by Posix) that the lowest possible number will be
+ allocated by the kernel when a new one is needed. In the daemon, and any
+ child procesees, values higher than 1024 (being bigger than FD_SETSIZE)
+ are not useable for FD_SET() [and hence select()] and overwrite the stack.
+ Assorted crashes happen.
+
+JH/11 Fix use of $sender_host_name in daemon process. When used in certain
+ main-section options or in a connect ACL, the value from the first ever
+ connection was never replaced for subsequent connections. Found by
+ Wakko Warner.
+
+JH/12 Bug 2838: Fix for i32lp64 hard-align platforms. Found for SPARC Linux,
+ though only once PCRE2 was introduced: the memory accounting used under
+ debug offset allocations by an int, giving a hard trap in early startup.
+ Change to using a size_t. Debug and fix by John Paul Adrian Glaubitz.
+
+JH/13 Bug 2845: Fix handling of tls_require_ciphers for OpenSSL when a value
+ with underbars is given. The write-protection of configuration introduced
+ in 4.95 trapped when normalisation was applied to an option not needing
+ expansion action.
+
+JH/14 Bug 1895: TLS: Deprecate RFC 5114 Diffie-Hellman parameters.
+
+JH/15 Fix a resource leak in *BSD. An off-by-one error resulted in the daemon
+ failing to close the certificates directory, every hour or any time it
+ was touched.
+
+JH/16 Debugging initiated by an ACL control now continues through into routing
+ and transport processes. Previously debugging stopped any time Exim
+ re-execs, or for processing a queued message.
+
+JH/17 The "expand" debug selector now gives more detail, specifically on the
+ result of expansion operators and items.
+
+JH/18 Bug 2751: Fix include_directory in redirect routers. Previously a
+ bad comparison between the option value and the name of the file to
+ be included was done, and a mismatch was wrongly identified.
+ 4.88 to 4.95 are affected.
+
+JH/19 Support for Berkeley DB versions 1 and 2 is withdrawn.
+
+JH/20 When built with NDBM for hints DB's check for nonexistence of a name
+ supplied as the db file-pair basename. Previously, if a directory
+ path was given, for example via the autoreply "once" option, the DB
+ file.pag and file.dir files would be created in that directory's
+ parent.
+
+JH/21 Remove the "allow_insecure_tainted_data" main config option and the
+ "taint" log_selector. These were previously deprecated.
+
+JH/22 Fix static address-list lookups to properly return the matched item.
+ Previously only the domain part was returned.
+
+JH/23 Bug 2864: FreeBSD: fix transport hang after 4xx/5xx response. Previously
+ the call into OpenSSL to send a TLS Close was being repeated; this
+ resulted in the library waiting for the peer's Close. If that was never
+ sent we waited forever. Fix by tracking send calls.
+
+JH/24 The ${run} expansion item now expands its command string elements after
+ splitting. Previously it was before; the new ordering makes handling
+ zero-length arguments simpler. The old ordering can be obtained by
+ appending a new option "preexpand", after a comma, to the "run".
+
+JH/25 Taint-check exec arguments for transport-initiated external processes.
+ Previously, tainted values could be used. This affects "pipe", "lmtp" and
+ "queryprogram" transport, transport-filter, and ETRN commands.
+ The ${run} expansion is also affected: in "preexpand" mode no part of
+ the command line may be tainted, in default mode the executable name
+ may not be tainted.
+
+JH/26 Fix CHUNKING on a continued-transport. Previously the usabliility of
+ the the facility was not passed across execs, and only the first message
+ passed over a connection could use BDAT; any further ones using DATA.
+
+JH/27 Support the PIPECONNECT facility in the smtp transport when the helo_data
+ uses $sending_ip_address and an interface is specified.
+ Previously any use of the local address in the EHLO name disabled
+ PIPECONNECT, the common case being to use the rDNS of it.
+
+JH/28 OpenSSL: fix transport-required OCSP stapling verification under session
+ resumption. Previously verify failed because no certificate status is
+ passed on the wire for the restarted session. Fix by using the recorded
+ ocsp status of the stored session for the new connection.
+
+JH/29 TLS resumption: the key for session lookup in the client now includes
+ more info that a server could potentially use in configuring a TLS
+ session, avoiding oferring mismatching sessions to such a server.
+ Previously only the server IP was used.
+
+JH/30 Fix string_copyn() for limit greater than actual string length.
+ Previously the copied amount was the limit, which could result in a
+ overlapping memcpy for newly allocated destination soon after a
+ source string shorter than the limit. Found/investigated by KM.
+
+JH/31 Bug 2886: GnuTLS: Do not free the cached creds on transport connection
+ close; it may be needed for a subsequent connection. This caused a
+ SEGV on primary-MX defer. Found/investigated by Gedalya & Andreas.
+
+JH/32 Fix CHUNKING for a second message on a connection when the first was
+ rejected. Previously we did not reset the chunking-offered state, and
+ erroneously rejected the BDAT command. Investigation help from
+ Jesse Hathaway.
+
+JH/33 Fis ${srs_encode ...} to handle an empty sender address, now returning
+ an empty address. Previously the expansion returned an error.
+
+HS/01 Bug 2855: Handle a v4mapped sender address given us by a frontending
+ proxy. Previously these were misparsed, leading to paniclog entries.
+
+
+Exim version 4.95
+-----------------
+
+JH/01 Bug 1329: Fix format of Maildir-format filenames to match other mail-
+ related applications. Previously an "H" was used where available info
+ says that "M" should be, so change to match.
+
+JH/02 Bug 2587: Fix pam expansion condition. Tainted values are commonly used
+ as arguments, so an implementation trying to copy these into a local
+ buffer was taking a taint-enforcement trap. Fix by using dynamically
+ created buffers. Similar fix for radius expansion condition.
+
+JH/03 Bug 2586: Fix listcount expansion operator. Using tainted arguments is
+ reasonable, eg. to count headers. Fix by using dynamically created
+ buffers rather than a local. Do similar fixes for ACL actions "dcc",
+ "log_reject_target", "malware" and "spam"; the arguments are expanded
+ so could be handling tainted values.
+
+JH/04 Bug 2590: Fix -bi (newaliases). A previous code rearrangement had
+ broken the (no-op) support for this sendmail command. Restore it
+ to doing nothing, silently, and returning good status.
+
+JH/05 Bug 2593: Fix "vacation" in Exim filter. Previously, when a "once"
+ record path was given (or the default used) without a leading directory
+ path, an error occurred on trying to open it. Use the transport's working
+ directory.
+
+JH/06 Bug 2594: Change the name used for certificate name checks in the smtp
+ transport. Previously it was the name on the DNS A-record; use instead
+ the head of the CNAME chain leading there (if there is one). This seems
+ to align better with RFC 6125.
+
+JH/07 Bug 2597: Fix a resource leak. Using a lookup in obtaining a value for
+ smtp_accept_max_per_host allocated resources which were not released
+ when the limit was exceeded. This eventually crashed the daemon. Fix
+ by adding a release action in that path.
+
+JH/08 Bug 2598: Fix verify ACL condition. The options for the condition are
+ expanded; previously using tainted values was rejected. Fix by using
+ dynamically-created buffers.
+
+JH/09 Relax restrictions on ACL verify condition needing access to message
+ headers. Previously they were only permitted in data and non-smtp ACLs;
+ permit also mime, dkim, prdr quit and notquit. Applies to header-syntax,
+ not_blind, header_sender and header_names_ascii verification.
+
+JH/10 Bug 2603: Fix coding of string copying to only evaluate arguments once.
+ Previously a macro used one argument twice; when called with the
+ argument as an expression having side-effects, incorrect operation
+ resulted. Use an inlineable function.
+
+JH/11 Bug 2604: Fix request to cutthrough-deliver when a connection is already
+ held open for a verify callout. Previously this wan not accounted for
+ and a corrupt onward SMTP conversation resulted.
+
+JH/12 Bug 2607: Fix the ${srs_encode } expansion to handle quoted local_parts.
+ Previously they were embedded naively in the constructed address; when
+ needed, strip the quoting and quote the entire local_part.
+ Also make the inbound_srs expansion condition handle quoting.
+
+JH/13 Fix dsearch "subdir" filter to ignore ".". Previously only ".." was
+ excluded, not matching the documentation.
+
+JH/14 Bug 2606: Fix a segfault in sqlite lookups. When no, or a bad, filename
+ was given for the sqlite_dbfile a trap resulted.
+
+JH/15 Bug 2620: Fix "spam" ACL condition. Previously, tainted values for the
+ "name" argument resulted in a trap. There is no reason to disallow such;
+ this was a coding error.
+
+JH/16 Bug 2615: Fix pause during message reception, on systems that have been
+ suspended/resumed. The Linux CLOCK_MONOTONIC does not account for time
+ spent suspended, ignoring the POSIX definition. Previously we assumed
+ it did and a constant offset from real time could be used as a correction.
+ Change to using the same clock source for the start-of-message and the
+ post-message next-tick-wait. Also change to using CLOCK_BOOTTIME if it
+ exists, just to get a clock slightly more aligned to reality.
+
+JH/17 Bug 2295: Fix DKIM signing to always semicolon-terminate. Although the
+ RFC says it is optional some validators care. The missing char was not
+ intended but triggered by a line-wrap alignment. Discovery and fix by
+ Guillaume Outters, hacked on by JH.
+
+JH/18 Bug 2617: Fix a taint trap in parse_fix_phrase(). Previously when the
+ name being quoted was tainted a trap would be taken. Fix by using
+ dynamically created buffers. The routine could have been called by a
+ rewrite with the "h" flag, by using the "-F" command-line option, or
+ by using a "name=" option on a control=submission ACL modifier.
+
+JH/19 SPF: change the Authentication-Results expansion component to give
+ smtp.helo when the sender domain is empty. Previously it gave
+ "smtp.mailfrom=<>"
+
+JH/20 Bug 2631: ACL dnslist conditions now ignore and log any lookups returns
+ not in 127.0.0.0/8 to help in spotting list domains taken over by a
+ domain-parking registrar.
+
+JH/21 Bug 2630: Fix eol-replacement string for the ${readsocket } expansion.
+ Previously when a whitespace character was specified it was not inserted
+ after removing the newline.
+
+JH/22 Bug 2265: Force SNI usage for smtp transport DANE'd connections, to be
+ the domain part of the recipient address. This overrides any tls_sni
+ option set, which was previously used.
+
+JH/23 Logging: with the +tls_sni log_selector, do not wrap the received SNI
+ in quotes.
+
+JH/24 Bug 2634: Fix a taint trap seen on NetBSD: the testing coded for
+ is_tainted() had an off-by-one error in the overenthusiastic direction.
+ Find and fix by Gavan. Although NetBSD is not a supported platform for
+ 4.94 this bug could affect other platforms.
+
+PP/01 Fix default prime selection to be consistent.
+ One path used ike23 still, instead of exim.dev.20160529.3; now both
+ execution flows will use the same DH primes (currently
+ exim.dev.20160529.3).
+
+JH/25 OpenSSL: Fix back-compatibility behaviour surrounding tls_certificates
+ option in smtp transport, to match the documentation. Previously
+ verification was not being done in some cases where it should have been.
+
+JH/26 Bug 2646: fix a memory usage issue in ldap lookups. Previously, when more
+ than one server was defined and depending on the platform memory layout
+ details, an internal consistency trap could be hit while walking the list
+ of servers.
+
+JH/27 Bug 2648: fix the passing of an authenticator public-name through spool
+ files. The value is used by the authresults expansion item. Previously
+ if this was used in a router or transport, a crash could result.
+
+JH/28 Fix spurious logging of select error. Some platforms, notably FreeBSD,
+ have a sufficient incidence of EINTR returns from select that an
+ interaction with other operations done by the main daemon loop exposed
+ a bug in the error-handling. This was benign apart from the log
+ messages.
+
+JH/29 Bug 2675: add outgoing-interface I= element to deferred "==" log lines,
+ for consistency with delivered "=>" and failed "**" lines. While we're
+ there, handle PRX and TFO.
+
+JH/30 Bug 2677: fix matching of long addresses. Since 4.93 a limit of 256 was
+ applied. This resulted, if any header-line rewrite rules were configured,
+ in a panic-log triggerable by sending a message with a long address in
+ a header. Fix by increasing the arbitrary limit to larger than a single
+ (dewrapped) 5322 header line maximum size.
+
+JH/31 The ESMTP option name advertised for the SUPPORT_EARLY_PIPE build option
+ is changed from X_PIPE_CONNECT to PIPE_CONNECT. This is in line with
+ RFC 6648 which deprecates X- options in protocols as a general practice.
+ Changeover between the implementations is handled by the mechanisms
+ already coded.
+
+JH/32 Bug 2599: fix delay of delivery to a local address where there is also
+ a remote which uses callout/hold. Previously the local was queued.
+
+JH/33 Fix a taint trap in the ${listextract } expansion when the source data
+ was tainted.
+
+JH/34 Fix the placement of a multiple-message delivery marker in the delivery
+ log line. The asterisk is now consistently appended to the remote IP
+ (and port, if given), and will also be provided on defer and fail log
+ lines. Previously it could be placed on the local IP if that was being
+ logged, and was only provided on delivery lines.
+
+JH/35 Bug 2343: Harden exim_tidydb against corrupt wait- files.
+
+JH/36 Bug 2687: Fix interpretation of multiple ^ chars in a plaintext
+ authenticator client_send option. Previously the next char, after a pair
+ was collapsed, was taken verbatim (so ^^^foo became ^^foo; ^^^^foo became
+ ^^\x00foo). Fixed to get ^\x00foo and ^^foo respectively to match the
+ documentation. There is still no way to get a leading ^ immediately
+ after a NUL (ie. for the password of a PLAIN method authenticator.
+
+JH/37 Enforce the expected size, for fixed-size records read from hints-DB
+ files. For bad sizes read, delete the record and whine to paniclog.
+
+JH/38 When logging an AUTH failure, as server, do not include sensitive
+ information. Previously, the credentials would be included if given
+ as part of the AUTH command line and an ACL denied authentication.
+
+JH/39 Bug 2691: fix $local_part_data. When the matching list element
+ referred to a file, bad data was returned. This likely also affected
+ $domain_part_data.
+
+JH/40 The gsasl authenticator now supports caching of the salted password
+ generated by the client-side implementation. This required the addition
+ of a new variable: $auth4.
+
+JH/41 Fix daemon SIGHUP on FreeBSD. Previously, a named socket for IPC was
+ left undeleted; the attempt to re-create it then failed - resulting in
+ the usual "SIGHUP tp have daemon reload configuration" to not work.
+ This affected any platform not supporting "abstract" Unix-domain
+ sockets (i.e. not Linux).
+
+JH/42 Bug 2693: Harden against a peer which reneges on a 452 "too many
+ recipients" response to RCPT in a later response, with a 250. The
+ previous coding assumed this would not happen, and under PIPELINING
+ would result in both lost and duplicate recipients for a message.
+
+JH/43 Bug 2694: Fix weighted distribution of work to multiple spamd servers.
+ Previously the weighting was incorrectly applied. Similar fix for socks
+ proxies. Found and fixed by Heiko Schlichting.
+
+JH/44 Bug 2701: Fix list-expansion of dns_ipv4_lookup. Previously, it did
+ not handle sub-lists included using the +namedlist syntax. While
+ investigating, the same found for dns_trust_aa, dns_again_means_nonexist,
+ dnssec_require_domains, dnssec_request_domains, srv_fail_domains,
+ mx_fail_domains.
+
+JH/45 Use a (new) separate store pool-pair for DKIM verify working data.
+ Previously the permanent pool was used, so the sore could not be freed.
+ This meant a connection with many messages would use continually-growing
+ memory.
+
+JH/46 Use an exponentially-increasing block size when malloc'ing store. Do it
+ per-pool so as not to waste too much space. Previously a constant size
+ was used which resulted in O(n^2) behaviour; now we get O(n log n) making
+ DOS attacks harder. The cost is wasted memory use in the larger blocks.
+
+JH/47 Use explicit alloc/free for DNS lookup workspace. This permits using the
+ same space repeatedly, and a smaller process footprint.
+
+JH/48 Use a less bogus-looking filename for a temporary used for DH-parameters
+ for GnuTLS. Previously the name started "%s" which, while not a bug,
+ looked as if if might be one.
+
+JH/49 Bug 2710: when using SOCKS for additional messages after the first (a
+ "continued connection") make the $proxy_* variables available. Previously
+ the information was not passed across the exec() call for subsequent
+ transport executions. This also mean that the log lines for the
+ messages can show the proxy information.
+
+JH/50 Bug 2672: QT elements in log lines, unless disabled, now exclude the
+ receive time. With modern systems the difference is significant.
+ The historical behaviour can be restored by disabling (a new) log_selector
+ "queue_time_exclusive".
+
+JH/51 Taint-check ACL line. Previously, only filenames (for out-of-line ACL
+ content) were specifically tested for. Now, also cover expansions
+ resulting in ACL names and inline ACL content.
+
+JH/52 Fix ${ip6norm:} operator. Previously, any trailing line text was dropped,
+ making it unusable in complex expressions.
+
+JH/53 Bug 2743: fix immediate-delivery via named queue. Previously this would
+ fail with a taint-check on the spoolfile name, and leave the message
+ queued.
+
+HS/01 Enforce absolute PID file path name.
+
+HS/02 Handle SIGINT as we handle SIGTERM: terminate the Exim process.
+
+PP/01 Add a too-many-bad-recipients guard to the default config's RCPT ACL.
+
+PP/02 Bug 2643: Correct TLS DH constants.
+ A missing NUL termination in our code-generation tool had led to some
+ incorrect Diffie-Hellman constants in the Exim source.
+ Reported by kylon94, code-gen tool fix by Simon Arlott.
+
+PP/03 Impose security length checks on various command-line options.
+ Fixes CVE-2020-SPRSS reported by Qualys.
+
+PP/04 Fix Linux security issue CVE-2020-SLCWD and guard against PATH_MAX
+ better. Reported by Qualys.
+
+PP/05 Fix security issue CVE-2020-PFPSN and guard against cmdline invoker
+ providing a particularly obnoxious sender full name.
+ Reported by Qualys.
+
+PP/06 Fix CVE-2020-28016 (PFPZA): Heap out-of-bounds write in parse_fix_phrase()
+
+PP/07 Refuse to allocate too little memory, block negative/zero allocations.
+ Security guard.
+
+PP/08 Change default for recipients_max from unlimited to 50,000.
+
+PP/09 Fix security issue with too many recipients on a message (to remove a
+ known security problem if someone does set recipients_max to unlimited,
+ or if local additions add to the recipient list).
+ Fixes CVE-2020-RCPTL reported by Qualys.
+
+PP/10 Fix security issue in SMTP verb option parsing
+ Fixes CVE-2020-EXOPT reported by Qualys.
+
+PP/11 Fix security issue in BDAT state confusion.
+ Ensure we reset known-good where we know we need to not be reading BDAT
+ data, as a general case fix, and move the places where we switch to BDAT
+ mode until after various protocol state checks.
+ Fixes CVE-2020-BDATA reported by Qualys.
+
+HS/03 Die on "/../" in msglog file names
+
+QS/01 Creation of (database) files in $spool_dir: only uid=0 or the uid of
+ the Exim runtime user are allowed to create files.
+
+QS/02 PID file creation/deletion: only possible if uid=0 or uid is the Exim
+ runtime user.
+
+QS/03 When reading the output from interpreted forward files we do not
+ pass the pipe between the parent and the interpreting process to
+ executed child processes (if any).
+
+QS/04 Always die if requested from internal logging, even is logging is
+ disabled.
+
+JH/54 DMARC: recent versions of the OpenDMARC library appear to have broken
+ the API; compilation noo longer completes with DMARC support included.
+ This affects 1.4.1-1 on Fedora 33 (1.3.2-3 is functional); and has
+ been reported on other platforms.
+
+JH/55 TLS: as server, reject connections with ALPN indicating non-smtp use.
+
+JH/56 Make the majority of info read from config files readonly, for defence-in-
+ depth against exploits. Suggestion by Qualys.
+ Not supported on Solaris 10.
+
+JH/57 Fix control=fakreject for a custom message containing tainted data.
+ Previously this resulted in a log complaint, due to a re-expansion present
+ since fakereject was originally introduced.
+
+JH/58 GnuTLS: Fix certextract expansion. If a second modifier after a tag
+ modifier was given, a loop resulted.
+
+JH/59 DKIM: Fix small-message verification under TLS with chunking. If a
+ pipelined SMTP command followed the BDAT LAST then it would be
+ incorrectly treated as part of the message body, causing a verification
+ fail.
+
+JH/60 Bug 2805: Fix logging of domain-literals in Message_ID: headers. They
+ require looser validation rules than those for 821-level addresses,
+ which only permit IP addresses.
+
+
+Exim version 4.94
+-----------------
+
+JH/01 Avoid costly startup code when not strictly needed. This reduces time
+ for some exim process initialisations. It does mean that the logging
+ of TLS configuration problems is only done for the daemon startup.
+
+JH/02 Early-pipelining support code is now included unless disabled in Makefile.
+
+JH/03 DKIM verification defaults no long accept sha1 hashes, to conform to
+ RFC 8301. They can still be enabled, using the dkim_verify_hashes main
+ option.
+
+JH/04 Support CHUNKING from an smtp transport using a transport_filter, when
+ DKIM signing is being done. Previously a transport_filter would always
+ disable CHUNKING, falling back to traditional DATA.
+
+JH/05 Regard command-line recipients as tainted.
+
+JH/06 Bug 340: Remove the daemon pid file on exit, when due to SIGTERM.
+
+JH/07 Bug 2489: Fix crash in the "pam" expansion condition. It seems that the
+ PAM library frees one of the arguments given to it, despite the
+ documentation. Therefore a plain malloc must be used.
+
+JH/08 Bug 2491: Use tainted buffers for the transport smtp context. Previously
+ on-stack buffers were used, resulting in a taint trap when DSN information
+ copied from a received message was written into the buffer.
+
+JH/09 Bug 2493: Harden ARC verify against Outlook, whick has been seen to mix
+ the ordering of its ARC headers. This caused a crash.
+
+JH/10 Bug 2492: Use tainted memory for retry record when needed. Previously when
+ a new record was being constructed with information from the peer, a trap
+ was taken.
+
+JH/11 Bug 2494: Unset the default for dmarc_tld_file. Previously a naiive
+ installation would get error messages from DMARC verify, when it hit the
+ nonexistent file indicated by the default. Distros wanting DMARC enabled
+ should both provide the file and set the option.
+ Also enforce no DMARC verification for command-line sourced messages.
+
+JH/12 Fix an uninitialised flag in early-pipelining. Previously connections
+ could, depending on the platform, hang at the STARTTLS response.
+
+JH/13 Bug 2498: Reset a counter used for ARC verify before handling another
+ message on a connection. Previously if one message had ARC headers and
+ the following one did not, a crash could result when adding an
+ Authentication-Results: header.
+
+JH/14 Bug 2500: Rewind some of the common-coding in string handling between the
+ Exim main code and Exim-related utities. The introduction of taint
+ tracking also did many adjustments to string handling. Since then, eximon
+ frequently terminated with an assert failure.
+
+JH/15 When PIPELINING, synch after every hundred or so RCPT commands sent and
+ check for 452 responses. This slightly helps the inefficieny of doing
+ a large alias-expansion into a recipient-limited target. The max_rcpt
+ transport option still applies (and at the current default, will override
+ the new feature). The check is done for either cause of synch, and forces
+ a fast-retry of all 452'd recipients using a new MAIL FROM on the same
+ connection. The new facility is not tunable at this time.
+
+JH/16 Fix the variables set by the gsasl authenticator. Previously a pointer to
+ library live data was being used, so the results became garbage. Make
+ copies while it is still usable.
+
+JH/17 Logging: when the deliver_time selector ise set, include the DT= field
+ on delivery deferred (==) and failed (**) lines (if a delivery was
+ attemtped). Previously it was only on completion (=>) lines.
+
+JH/18 Authentication: the gsasl driver not provides the $authN variables in time
+ for the expansion of the server_scram_iter and server_scram_salt options.
+
+WB/01 SPF: DNS lookups for the obsolete SPF RR type done by the libspf2 library
+ are now specifically given a NO_DATA response without hitting the system
+ resolver. The library goes on to do the now-standard TXT lookup.
+ Use of dnsdb lookups is not affected.
+
+JH/19 Bug 2507: Modules: on handling a dynamic-module (lookups) open failure,
+ only retrieve the errormessage once. Previously two calls to dlerror()
+ were used, and the second one (for mainlog/paniclog) retrieved null
+ information.
+
+JH/20 Taint checking: disallow use of tainted data for
+ - the appendfile transport file and directory options
+ - the pipe transport command
+ - the autoreply transport file, log and once options
+ - file names used by the redirect router (including filter files)
+ - named-queue names
+ - paths used by single-key lookups
+ Previously this was permitted.
+
+JH/21 Bug 2501: Fix init call in the heimdal authenticator. Previously it
+ adjusted the size of a major service buffer; this failed because the
+ buffer was in use at the time. Change to a compile-time increase in the
+ buffer size, when this authenticator is compiled into exim.
+
+JH/22 Taint-checking: move to safe-mode taint checking on all platforms. The
+ previous fast-mode was untenable in the face of glibs using mmap to
+ support larger malloc requests.
+
+PP/01 Update the openssl_options possible values through OpenSSL 1.1.1c.
+ New values supported, if defined on system where compiled:
+ allow_no_dhe_kex, cryptopro_tlsext_bug, enable_middlebox_compat,
+ no_anti_replay, no_encrypt_then_mac, prioritize_chacha, tlsext_padding
+
+JH/23 Performance improvement in the initial phase of a two-pass queue run. By
+ running a limited number of proceses in parallel, a benefit is gained. The
+ amount varies with the platform hardware and load. The use of the option
+ queue_run_in_order means we cannot do this, as ordering becomes
+ indeterminate.
+
+JH/24 Bug 2524: fix the cyrus_sasl auth driver gssapi usage. A previous fix
+ had introduced a string-copy (for ensuring NUL-termination) which was not
+ appropriate for that case, which can include embedded NUL bytes in the
+ block of data. Investigation showed the copy to actually be needless, the
+ data being length-specified.
+
+JH/25 Fix use of concurrent TLS connections under GnuTLS. When a callout was
+ done during a receiving connection, and both used TLS, global info was
+ used rather than per-connection info for tracking the state of data
+ queued for transmission. This could result in a connection hang.
+
+JH/26 Fix use of the SIZE parameter on MAIL commands, on continued connections.
+ Previously, when delivering serveral messages down a single connection
+ only the first would provide a SIZE. This was due to the size information
+ not being properly tracked.
+
+JH/27 Bug 2530: When operating in a timezone with sub-minute offset, such as
+ TAI (at 37 seconds currently), pretend to be in UTC for time-related
+ expansion and logging. Previously, spurious values such as a future
+ minute could be seen.
+
+JH/28 Bug 2533: Fix expansion of ${tr } item. When called in some situations
+ it could crash from a null-deref. This could also affect the
+ ${addresses: } operator and ${readsock } item.
+
+JH/29 Bug 2537: Fix $mime_part_count. When a single connection had a non-mime
+ message following a mime one, the variable was not reset.
+
+JH/30 When an pipelined-connect fails at the first response, assume incorrect
+ cached capability (perhaps the peer reneged?) and immediately retry in
+ non-pipelined mode.
+
+JH/31 Fix spurious detection of timeout while writing to transport filter.
+
+JH/32 Bug 2541: Fix segfault on bad cmdline -f (sender) argument. Previously
+ an attempt to copy the string was made before checking it.
+
+JH/33 Fix the dsearch lookup to return an untainted result. Previously the
+ taint of the lookup key was maintained; we now regard the presence in the
+ filesystem as sufficient validation.
+
+JH/34 Fix the readsocket expansion to not segfault when an empty "options"
+ argument is supplied.
+
+JH/35 The dsearch lookup now requires that the directory is an absolute path.
+ Previously this was not checked, and nonempty relative paths made an
+ access under Exim's current working directory.
+
+JH/36 Bug 2554: Fix msg:defer event for the hosts_max_try_hardlimit case.
+ Previously no event was raised.
+
+JH/37 Bug 2552: Fix the check on spool space during reception to use the SIZE
+ parameter supplied by the sender MAIL FROM command. Previously it was
+ ignored, and only the check_spool_space option value for the required
+ leeway checked.
+
+JH/38 Fix $dkim_key_length. This should, after a DKIM verification, present
+ the size of the signing public-key. Previously it was instead giving
+ the size of the signature hash.
+
+JH/39 DKIM verification: the RFC 8301 restriction on sizes of RSA keys is now
+ the default. See the (new) dkim_verify_min_keysizes option.
+
+JH/40 Fix a memory-handling bug: when a connection carried multiple messages
+ and an ACL use a lookup for checking either the local_part or domain,
+ stale data could be accessed. Ensure that variable references are
+ dropped between messages.
+
+JH/41 Bug 2571: Fix SPA authenticator. Running as a server, an offset supplied
+ by the client was not checked as pointing within response data before
+ being used. A malicious client could thus cause an out-of-bounds read and
+ possibly gain authentication. Fix by adding the check.
+
+JH/42 Internationalisation: change the default for downconversion in the smtp
+ transport to be "if needed". Previously it was "as previously set" for
+ the message, which usually meant "if needed" for message-submission but
+ "no" for everything else. However, MTAs have been seen using SMTPUTF8
+ even when the envelope addresses did not need it, resulting in forwarding
+ failures to non-supporting MTAs. A downconvert in such cases will be
+ a no-op on the addresses, merely dropping the use of SMTPUTF8 by the
+ transport. The change does mean that addresses needing conversion will
+ be converted when previously a delivery failure would occur.
+
+JH/43 Fix possible long line in DSN. Previously when a very long SMTP error
+ response was received it would be used unchecked in a fail-DSN, violating
+ standards on line-length limits. Truncate if needed.
+
+HS/01 Remove parameters of the link to www.open-spf.org. The linked form
+ doesn't work. (Additionally add a new main config option to configure the
+ spf_smtp_comment)
+
+
+Exim version 4.93
+-----------------
+
+JH/01 OpenSSL: With debug enabled output keying information sufficient, server
+ side, to decode a TLS 1.3 packet capture.
+
+JH/02 OpenSSL: Suppress the sending of (stateful) TLS1.3 session tickets.
+ Previously the default library behaviour applied, sending two, each in
+ its own TCP segment.
+
+JH/03 Debug output for ACL now gives the config file name and line number for
+ each verb.
+
+JH/04 The default received_header_text now uses the RFC 8314 tls cipher clause.
+
+JH/05 DKIM: ensure that dkim_domain elements are lowercased before use.
+
+JH/06 Fix buggy handling of autoreply bounce_return_size_limit, and a possible
+ buffer overrun for (non-chunking) other transports.
+
+JH/07 GnuTLS: Our use of late (post-handshake) certificate verification, under
+ TLS1.3, means that a server rejecting a client certificate is not visible
+ to the client until the first read of encrypted data (typically the
+ response to EHLO). Add detection for that case and treat it as a failed
+ TLS connection attempt, so that the normal retry-in-clear can work (if
+ suitably configured).
+
+JB/01 Bug 2375: fix expansions of 822 addresses having comments in local-part
+ and/or domain. Found and fixed by Jason Betts.
+
+JH/08 Add hardening against SRV & TLSA lookups the hit CNAMEs (a nonvalid
+ configuration). If a CNAME target was not a wellformed name pattern, a
+ crash could result.
+
+JH/09 Logging: Fix initial listening-on line for multiple ports for an IP when
+ the OS reports them interleaved with other addresses.
+
+JH/10 OpenSSL: Fix aggregation of messages. Previously, when PIPELINING was
+ used both for input and for a verify callout, both encrypted, SMTP
+ responses being sent by the server could be lost. This resulted in
+ dropped connections and sometimes bounces generated by a peer sending
+ to this system.
+
+JH/11 Harden plaintext authenticator against a badly misconfigured client-send
+ string. Previously it was possible to cause undefined behaviour in a
+ library routine (usually a crash). Found by "zerons".
+
+JH/12 Bug 2384: fix "-bP smtp_receive_timeout". Previously it returned no
+ output.
+
+JH/13 Bug 2386: Fix builds with Dane under LibreSSL 2.9.0 onward. Some old
+ API was removed, so update to use the newer ones.
+
+JH/14 Bug 1891: Close the log file if receiving a non-smtp message, without
+ any timeout set, is taking a long time. Previously we would hang on to a
+ rotated logfile "forever" if the input was arriving with long gaps
+ (a previous attempt to fix addressed lack, for a long time, of initial
+ input).
+
+HS/01 Bug 2390: Use message_id for tempfile creation to avoid races in a
+ shared (NFS) environment. The length of the tempfile name is now
+ 4 + 16 ("hdr.$message_exim_id") which might break on file
+ systems which restrict the file name length to lower values.
+ (It was "hdr.$pid".)
+
+HS/02 Bug 2390: Use message_id for tempfile creation to avoid races in a
+ shared (NFS) environment.
+
+HS/03 Bug 2392: exigrep does case sensitive *option* processing (as it
+ did for all versions <4.90). Notably -M, -m, --invert, -I may be
+ affected.
+
+JH/15 Use unsigned when creating bitmasks in macros, to avoid build errors
+ on some platforms for bit 31.
+
+JH/16 GnuTLS: rework ciphersuite strings under recent library versions. Thanks
+ to changes apparently associated with TLS1.3 handling some of the APIs
+ previously used were either nonfunctional or inappropriate. Strings
+ like TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM__AEAD:256
+ and TLS1.2:ECDHE_SECP256R1__RSA_SHA256__AES_128_CBC__SHA256:128 replace
+ the previous TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256 .
+ This affects log line X= elements, the $tls_{in,out}_cipher variables,
+ and the use of specific cipher names in the encrypted= ACL condition.
+
+JH/17 OpenSSL: the default openssl_options now disables ssl_v3.
+
+JH/18 GnuTLS: fix $tls_out_ocsp under hosts_request_ocsp. Previously the
+ verification result was not updated unless hosts_require_ocsp applied.
+
+JH/19 Bug 2398: fix listing of a named-queue. Previously, even with the option
+ queue_list_requires_admin set to false, non-admin users were denied the
+ facility.
+
+JH/20 Bug 2389: fix server advertising of usable certificates, under GnuTLS in
+ directory-of-certs mode. Previously they were advertised despite the
+ documentation.
+
+JH/21 The smtp transport option "hosts_noproxy_tls" is now unset by default.
+ A single TCP connection by a client will now hold a TLS connection open
+ for multiple message deliveries, by default. Previously the default was to
+ not do so.
+
+JH/22 The smtp transport option "hosts_try_dane" now enables all hosts by
+ default. If built with the facility, DANE will be used. The facility
+ SUPPORT_DANE is now enabled in the prototype build Makefile "EDITME".
+
+JH/23 The build default is now for TLS to be included; the SUPPORT_TLS define
+ is replaced with DISABLE_TLS. Either USE_GNUTLS or (the new) USE_OPENSSL
+ must be defined and you must still, unless you define DISABLE_TLS, manage
+ the the include-dir and library-file requirements that go with that
+ choice. Non-TLS builds are still supported.
+
+JH/24 Fix duplicated logging of peer name/address, on a transport connection-
+ reject under TFO.
+
+JH/25 The smtp transport option "hosts_try_fastopen" now enables all hosts by
+ default. If the platform supports and has the facility enabled, it will
+ be requested on all coneections.
+
+JH/26 The PIPE_CONNECT facility is promoted from experimental status and is now
+ controlled by the build-time option SUPPORT_PIPE_CONNECT.
+
+PP/01 Unbreak heimdal_gssapi, broken in 4.92.
+
+JH/27 Bug 2404: Use the main-section configuration option "dsn_from" for
+ success-DSN messages. Previously the From: header was always the default
+ one for these; the option was ignored.
+
+JH/28 Fix the timeout on smtp response to apply to the whole response.
+ Previously it was reset for every read, so a teergrubing peer sending
+ single bytes within the time limit could extend the connection for a
+ long time. Credit to Qualsys Security Advisory Team for the discovery.
+
+JH/29 Fix DSN Final-Recipient: field. Previously it was the post-routing
+ delivery address, which leaked information of the results of local
+ forwarding. Change to the original envelope recipient address, per
+ standards.
+
+JH/30 Bug 2411: Fix DSN generation when RFC 3461 failure notification is
+ requested. Previously not bounce was generated and a log entry of
+ error ignored was made.
+
+JH/31 Avoid re-expansion in ${sort } expansion. (CVE-2019-13917)
+
+JH/32 Introduce a general tainting mechanism for values read from the input
+ channel, and values derived from them. Refuse to expand any tainted
+ values, to catch one form of exploit.
+
+JH/33 Bug 2413: Fix dkim_strict option. Previously the expansion result
+ was unused and the unexpanded text used for the test. Found and
+ fixed by Ruben Jenster.
+
+JH/34 Fix crash after TLS shutdown. When the TCP/SMTP channel was left open,
+ an attempt to use a TLS library read routine dereffed a nul pointer,
+ causing a segfault.
+
+JH/35 Bug 2409: filter out-of-spec chars from callout response before using
+ them in our smtp response.
+
+JH/36 Have the general router option retry_use_local_part default to true when
+ any of the restrictive preconditions are set (to anything). Previously it
+ was only for check_local user. The change removes one item of manual
+ configuration which is required for proper retries when a remote router
+ handles a subset of addresses for a domain.
+
+JH/37 Appendfile: when evaluating quota use (non-quota_size_regex) take the file
+ link count into consideration.
+
+HS/04 Fix handling of very log lines in -H files. If a -<key> <value> line
+ caused the extension of big_buffer, the following lines were ignored.
+
+JH/38 Bug 1395: Teach the DNS negative-cache about TTL value from the SOA in
+ accordance with RFC 2308. Previously there was no expiry, so a longlived
+ receive process (eg. due to ACL delays) versus a short SOA value could
+ surprise.
+
+HS/05 Handle trailing backslash gracefully. (CVE-2019-15846)
+
+JH/39 Promote DMARC support to mainline.
+
+JH/40 Bug 2452: Add a References: header to DSNs.
+
+JH/41 With GnuTLS 3.6.0 (and later) do not attempt to manage Diffie-Hellman
+ parameters. The relevant library call is documented as "Deprecated: This
+ function is unnecessary and discouraged on GnuTLS 3.6.0 or later. Since
+ 3.6.0, DH parameters are negotiated following RFC7919."
+
+HS/06 Change the default of dnssec_request_domains to "*"
+
+JH/42 Bug 2545: Fix CHUNKING for all RCPT commands rejected. Previously we
+ carried on and emitted a BDAT command, even when PIPELINING was not
+ active.
+
+JH/43 Bug 2465: Fix taint-handling in dsearch lookup. Previously a nontainted
+ buffer was used for the filename, resulting in a trap when tainted
+ arguments (eg. $domain) were used.
+
+JH/44 With OpenSSL 1.1.1 (onwards) disable renegotiation for TLS1.2 and below;
+ recommended to avoid a possible server-load attack. The feature can be
+ re-enabled via the openssl_options main cofiguration option.
+
+JH/45 local_scan API: documented the current smtp_printf() call. This changed
+ for version 4.90 - adding a "more data" boolean to the arguments.
+ Bumped the ABI version number also, this having been missed previously;
+ release versions 4.90 to 4.92.3 inclusive were effectively broken in
+ respect of usage of smtp_printf() by either local_scan code or libraries
+ accessed via the ${dlfunc } expansion item. Both will need coding
+ adjustment for any calls to smtp_printf() to match the new function
+ signature; a FALSE value for the new argument is always safe.
+
+JH/46 FreeBSD: fix use of the sendfile() syscall. The shim was not updating
+ the file-offset (which the Linux syscall does, and exim expects); this
+ resulted in an indefinite loop.
+
+JH/47 ARC: fix crash in signing, triggered when a configuration error failed
+ to do ARC verification. The Authentication-Results: header line added
+ by the configuration then had no ARC item.
+
+JH/48 Bug 2784: fix shutdown=no in the ${readsocket) expansion item. Previously
+ an incorrect mode was used for reading the result, resulting in it being
+ ignored.
+
+
+Exim version 4.92
+-----------------
+
+JH/01 Remove code calling the customisable local_scan function, unless a new
+ definition "HAVE_LOCAL_SCAN=yes" is present in the Local/Makefile.
+
+JH/02 Bug 1007: Avoid doing logging from signal-handlers, as that can result in
+ non-signal-safe functions being used.
+
+JH/03 Bug 2269: When presented with a received message having a stupidly large
+ number of DKIM-Signature headers, disable DKIM verification to avoid
+ a resource-consumption attack. The limit is set at twenty.
+
+JH/04 Add variables $arc_domains, $arc_oldest_pass for ARC verify. Fix the
+ report of oldest_pass in ${authres } in consequence, and separate out
+ some descriptions of reasons for verification fail.
+
+JH/05 Bug 2273: Cutthrough delivery left a window where the received messsage
+ files in the spool were present and unlocked. A queue-runner could spot
+ them, resulting in a duplicate delivery. Fix that by doing the unlock
+ after the unlink. Investigation by Tim Stewart. Take the opportunity to
+ add more error-checking on spoolfile handling while that code is being
+ messed with.
+
+PP/01 Refuse to open a spool data file (*-D) if it's a symlink.
+ No known attacks, no CVE, this is defensive hardening.
+
+JH/06 Bug 2275: The MIME ACL unlocked the received message files early, and
+ a queue-runner could start a delivery while other operations were ongoing.
+ Cutthrough delivery was a common victim, resulting in duplicate delivery.
+ Found and investigated by Tim Stewart. Fix by using the open message data
+ file handle rather than opening another, and not locally closing it (which
+ releases a lock) for that case, while creating the temporary .eml format
+ file for the MIME ACL. Also applies to "regex" and "spam" ACL conditions.
+
+JH/07 Bug 177: Make a random-recipient callout success visible in ACL, by setting
+ $sender_verify_failure/$recipient_verify_failure to "random".
+
+JH/08 When generating a selfsigned cert, use serial number 1 since zero is not
+ legitimate.
+
+JH/09 Bug 2274: Fix logging of cmdline args when starting in an unlinked cwd.
+ Previously this would segfault.
+
+JH/10 Fix ARC signing for case when DKIM signing failed. Previously this would
+ segfault.
+
+JH/11 Bug 2264: Exim now only follows CNAME chains one step by default. We'd
+ like zero, since the resolver should be doing this for us, But we need one
+ as a CNAME but no MX presence gets the CNAME returned; we need to check
+ that doesn't point to an MX to declare it "no MX returned" rather than
+ "error, loop". A new main option is added so the older capability of
+ following some limited number of chain links is maintained.
+
+JH/12 Add client-ip info to non-pass iprev ${authres } lines.
+
+JH/13 For receent Openssl versions (1.1 onward) use modern generic protocol
+ methods. These should support TLS 1.3; they arrived with TLS 1.3 and the
+ now-deprecated earlier definitions used only specified the range up to TLS
+ 1.2 (in the older-version library docs).
+
+JH/14 Bug 2284: Fix DKIM signing for body lines starting with a pair of dots.
+
+JH/15 Rework TLS client-side context management. Stop using a global, and
+ explicitly pass a context around. This enables future use of TLS for
+ connections to service-daemons (eg. malware scanning) while a client smtp
+ connection is using TLS; with cutthrough connections this is quite likely.
+
+JH/16 Fix ARC verification to do AS checks in reverse order.
+
+JH/17 Support a "tls" option on the ${readsocket } expansion item.
+
+JH/18 Bug 2287: Fix the protocol name (eg utf8esmtp) for multiple messages
+ using the SMTPUTF8 option on their MAIL FROM commands, in one connection.
+ Previously the "utf8" would be re-prepended for every additional message.
+
+JH/19 Reject MAIL FROM commands with SMTPUTF8 when the facility was not advertised.
+ Previously thery were accepted, resulting in issues when attempting to
+ forward messages to a non-supporting MTA.
+
+PP/02 Let -n work with printing macros too, not just options.
+
+JH/20 Bug 2296: Fix cutthrough for >1 address redirection. Previously only
+ one parent address was copied, and bogus data was used at delivery-logging
+ time. Either a crash (after delivery) or bogus log data could result.
+ Discovery and analysis by Tim Stewart.
+
+PP/03 Make ${utf8clean:} expansion operator detect incomplete final character.
+ Previously if the string ended mid-character, we did not insert the
+ promised '?' replacement.
+
+PP/04 Documentation: current string operators work on bytes, not codepoints.
+
+JH/21 Change as many as possible of the global flags into one-bit bitfields; these
+ should pack well giving a smaller memory footprint so better caching and
+ therefore performance. Group the declarations where this can't be done so
+ that the byte-sized flag variables are not interspersed among pointer
+ variables, giving a better chance of good packing by the compiler.
+
+JH/22 Bug 1896: Fix the envelope from for DMARC forensic reports to be possibly
+ non-null, to avoid issues with sites running BATV. Previously reports were
+ sent with an empty envelope sender so looked like bounces.
+
+JH/23 Bug 2318: Fix the noerror command within filters. It wasn't working.
+ The ignore_error flag wasn't being returned from the filter subprocess so
+ was not set for later routers. Investigation and fix by Matthias Kurz.
+
+JH/24 Bug 2310: Raise a msg:fail:internal event for each undelivered recipient,
+ and a msg:complete for the whole, when a message is manually removed using
+ -Mrm. Developement by Matthias Kurz, hacked on by JH.
+
+JH/25 Avoid fixed-size buffers for pathnames in DB access. This required using
+ a "Gnu special" function, asprintf() in the DB utility binary builds; I
+ hope that is portable enough.
+
+JH/26 Bug 2311: Fix DANE-TA verification under GnuTLS. Previously it was also
+ requiring a known-CA anchor certificate; make it now rely entirely on the
+ TLSA as an anchor. Checking the name on the leaf cert against the name
+ on the A-record for the host is still done for TA (but not for EE mode).
+
+JH/27 Fix logging of proxy address. Previously, a pointless "PRX=[]:0" would be
+ included in delivery lines for non-proxied connections, when compiled with
+ SUPPORT_SOCKS and running with proxy logging enabled.
+
+JH/28 Bug 2314: Fire msg:fail:delivery event even when error is being ignored.
+ Developement by Matthias Kurz, tweaked by JH. While in that bit of code,
+ move the existing event to fire before the normal logging of message
+ failure so that custom logging is bracketed by normal logging.
+
+JH/29 Bug 2322: A "fail" command in a non-system filter (file) now fires the
+ msg:fail:internal event. Developement by Matthias Kurz.
+
+JH/30 Bug 2329: Increase buffer size used for dns lookup from 2k, which was
+ far too small for todays use of crypto signatures stored there. Go all
+ the way to the max DNS message size of 64kB, even though this might be
+ overmuch for IOT constrained device use.
+
+JH/31 Fix a bad use of a copy function, which could be used to pointlessly
+ copy a string over itself. The library routine is documented as not
+ supporting overlapping copies, and on MacOS it actually raised a SIGABRT.
+
+JH/32 For main options check_spool_space and check_inode_space, where the
+ platform supports 64b integers, support more than the previous 2^31 kB
+ (i.e. more than 2 TB). Accept E, P and T multipliers in addition to
+ the previous G, M, k.
+
+JH/33 Bug 2338: Fix the cyrus-sasl authenticator to fill in the
+ $authenticated_fail_id variable on authentication failure. Previously
+ it was unset.
+
+JH/34 Increase RSA keysize of autogen selfsign cert from 1024 to 2048. RHEL 8.0
+ OpenSSL didn't want to use such a weak key. Do for GnuTLS also, and for
+ more-modern GnuTLS move from GNUTLS_SEC_PARAM_LOW to
+ GNUTLS_SEC_PARAM_MEDIUM.
+
+JH/35 OpenSSL: fail the handshake when SNI processing hits a problem, server
+ side. Previously we would continue as if no SNI had been received.
+
+JH/36 Harden the handling of string-lists. When a list consisted of a sole
+ "<" character, which should be a list-separator specification, we walked
+ off past the nul-terimation.
+
+JH/37 Bug 2341: Send "message delayed" warning MDNs (restricted to external
+ causes) even when the retry time is not yet met. Previously they were
+ not, meaning that when (say) an account was over-quota and temp-rejecting,
+ and multiple senders' messages were queued, only one sender would get
+ notified on each configured delay_warning cycle.
+
+JH/38 Bug 2351: Log failures to extract envelope addresses from message headers.
+
+JH/39 OpenSSL: clear the error stack after an SSL_accept(). With anon-auth
+ cipher-suites, an error can be left on the stack even for a succeeding
+ accept; this results in impossible error messages when a later operation
+ actually does fail.
+
+AM/01 Bug 2359: GnuTLS: repeat lowlevel read and write operations while they
+ return error codes indicating retry. Under TLS1.3 this becomes required.
+
+JH/40 Fix the feature-cache refresh for EXPERIMENTAL_PIPE_CONNECT. Previously
+ it only wrote the new authenticators, resulting in a lack of tracking of
+ peer changes of ESMTP extensions until the next cache flush.
+
+JH/41 Fix the loop reading a message header line to check for integer overflow,
+ and more-often against header_maxsize. Previously a crafted message could
+ induce a crash of the recive process; now the message is cleanly rejected.
+
+JH/42 Bug 2366: Fix the behaviour of the dkim_verify_signers option. It had
+ been totally disabled for all of 4.91. Discovery and fix by "Mad Alex".
+
+
+Exim version 4.91
+-----------------
+
+GF/01 DEFER rather than ERROR on redis cluster MOVED response.
+ When redis_servers is set to a list of > 1 element, and the Redis servers
+ in that list are in cluster configuration, convert the REDIS_REPLY_ERROR
+ case of MOVED into a DEFER case instead, thus moving the query onto the
+ next server in the list. For a cluster of N elements, all N servers must
+ be defined in redis_servers.
+
+GF/02 Catch and remove uninitialized value warning in exiqsumm
+ Check for existence of @ARGV before looking at $ARGV[0]
+
+JH/01 Replace the store_release() internal interface with store_newblock(),
+ which internalises the check required to safely use the old one, plus
+ the allocate and data copy operations duplicated in both (!) of the
+ extant use locations.
+
+JH/02 Disallow '/' characters in queue names specified for the "queue=" ACL
+ modifier. This matches the restriction on the commandline.
+
+JH/03 Fix pgsql lookup for multiple result-tuples with a single column.
+ Previously only the last row was returned.
+
+JH/04 Bug 2217: Tighten up the parsing of DKIM signature headers. Previously
+ we assumed that tags in the header were well-formed, and parsed the
+ element content after inspecting only the first char of the tag.
+ Assumptions at that stage could crash the receive process on malformed
+ input.
+
+JH/05 Bug 2215: Fix crash associated with dnsdb lookup done from DKIM ACL.
+ While running the DKIM ACL we operate on the Permanent memory pool so that
+ variables created with "set" persist to the DATA ACL. Also (at any time)
+ DNS lookups that fail create cache records using the Permanent pool. But
+ expansions release any allocations made on the current pool - so a dnsdb
+ lookup expansion done in the DKIM ACL releases the memory used for the
+ DNS negative-cache, and bad things result. Solution is to switch to the
+ Main pool for expansions.
+ While we're in that code, add checks on the DNS cache during store_reset,
+ active in the testsuite.
+ Problem spotted, and debugging aided, by Wolfgang Breyha.
+
+JH/06 Fix issue with continued-connections when the DNS shifts unreliably.
+ When none of the hosts presented to a transport match an already-open
+ connection, close it and proceed with the list. Previously we would
+ queue the message. Spotted by Lena with Yahoo, probably involving
+ round-robin DNS.
+
+JH/07 Bug 2214: Fix SMTP responses resulting from non-accept result of MIME ACL.
+ Previously a spurious "250 OK id=" response was appended to the proper
+ failure response.
+
+JH/08 The "support for" informational output now, which built with Content
+ Scanning support, has a line for the malware scanner interfaces compiled
+ in. Interface can be individually included or not at build time.
+
+JH/09 The "aveserver", "kavdaemon" and "mksd" interfaces are now not included
+ by the template makefile "src/EDITME". The "STREAM" support for an older
+ ClamAV interface method is removed.
+
+JH/10 Bug 2223: Fix mysql lookup returns for the no-data case (when the number of
+ rows affected is given instead).
+
+JH/11 The runtime Berkeley DB library version is now additionally output by
+ "exim -d -bV". Previously only the compile-time version was shown.
+
+JH/12 Bug 2230: Fix cutthrough routing for nonfirst messages in an initiating
+ SMTP connection. Previously, when one had more recipients than the
+ first, an abortive onward connection was made. Move to full support for
+ multiple onward connections in sequence, handling cutthrough connection
+ for all multi-message initiating connections.
+
+JH/13 Bug 2229: Fix cutthrough routing for nonstandard port numbers defined by
+ routers. Previously, a multi-recipient message would fail to match the
+ onward-connection opened for the first recipient, and cause its closure.
+
+JH/14 Bug 2174: A timeout on connect for a callout was also erroneously seen as
+ a timeout on read on a GnuTLS initiating connection, resulting in the
+ initiating connection being dropped. This mattered most when the callout
+ was marked defer_ok. Fix to keep the two timeout-detection methods
+ separate.
+
+JH/15 Relax results from ACL control request to enable cutthrough, in
+ unsupported situations, from error to silently (except under debug)
+ ignoring. This covers use with PRDR, frozen messages, queue-only and
+ fake-reject.
+
+HS/01 Fix Buffer overflow in base64d() (CVE-2018-6789)
+
+JH/16 Fix bug in DKIM verify: a buffer overflow could corrupt the malloc
+ metadata, resulting in a crash in free().
+
+PP/01 Fix broken Heimdal GSSAPI authenticator integration.
+ Broken in f2ed27cf5, missing an equals sign for specified-initialisers.
+ Broken also in d185889f4, with init system revamp.
+
+JH/17 Bug 2113: Fix conversation closedown with the Avast malware scanner.
+ Previously we abruptly closed the connection after reading a malware-
+ found indication; now we go on to read the "scan ok" response line,
+ and send a quit.
+
+JH/18 Bug 2239: Enforce non-usability of control=utf8_downconvert in the mail
+ ACL. Previously, a crash would result.
+
+JH/19 Speed up macro lookups during configuration file read, by skipping non-
+ macro text after a replacement (previously it was only once per line) and
+ by skipping builtin macros when searching for an uppercase lead character.
+
+JH/20 DANE support moved from Experimental to mainline. The Makefile control
+ for the build is renamed.
+
+JH/21 Fix memory leak during multi-message connections using STARTTLS. A buffer
+ was allocated for every new TLS startup, meaning one per message. Fix
+ by only allocating once (OpenSSL) or freeing on TLS-close (GnuTLS).
+
+JH/22 Bug 2236: When a DKIM verification result is overridden by ACL, DMARC
+ reported the original. Fix to report (as far as possible) the ACL
+ result replacing the original.
+
+JH/23 Fix memory leak during multi-message connections using STARTTLS under
+ OpenSSL. Certificate information is loaded for every new TLS startup,
+ and the resources needed to be freed.
+
+JH/24 Bug 2242: Fix exim_dbmbuild to permit directoryless filenames.
+
+JH/25 Fix utf8_downconvert propagation through a redirect router. Previously it
+ was not propagated.
+
+JH/26 Bug 2253: For logging delivery lines under PRDR, append the overall
+ DATA response info to the (existing) per-recipient response info for
+ the "C=" log element. It can have useful tracking info from the
+ destination system. Patch from Simon Arlott.
+
+JH/27 Bug 2251: Fix ldap lookups that return a single attribute having zero-
+ length value. Previously this would segfault.
+
+HS/02 Support Avast multiline protoocol, this allows passing flags to
+ newer versions of the scanner.
+
+JH/28 Ensure that variables possibly set during message acceptance are marked
+ dead before release of memory in the daemon loop. This stops complaints
+ about them when the debug_store option is enabled. Discovered specifically
+ for sender_rate_period, but applies to a whole set of variables.
+ Do the same for the queue-runner and queue-list loops, for variables set
+ from spool message files. Do the same for the SMTP per-message loop, for
+ certain variables indirectly set in ACL operations.
+
+JH/29 Bug 2250: Fix a longstanding bug in heavily-pipelined SMTP input (such
+ as a multi-recipient message from a mailinglist manager). The coding had
+ an arbitrary cutoff number of characters while checking for more input;
+ enforced by writing a NUL into the buffer. This corrupted long / fast
+ input. The problem was exposed more widely when more pipelineing of SMTP
+ responses was introduced, and one Exim system was feeding another.
+ The symptom is log complaints of SMTP syntax error (NUL chars) on the
+ receiving system, and refused recipients seen by the sending system
+ (propating to people being dropped from mailing lists).
+ Discovered and pinpointed by David Carter.
+
+JH/30 The (EXPERIMENTAL_DMARC) variable $dmarc_ar_header is withdrawn, being
+ replaced by the ${authresults } expansion.
+
+JH/31 Bug 2257: Fix pipe transport to not use a socket-only syscall.
+
+HS/03 Set a handler for SIGTERM and call exit(3) if running as PID 1. This
+ allows proper process termination in container environments.
+
+JH/32 Bug 2258: Fix spool_wireformat in combination with LMTP transport.
+ Previously the "final dot" had a newline after it; ensure it is CR,LF.
+
+JH/33 SPF: remove support for the "spf" ACL condition outcome values "err_temp"
+ and "err_perm", deprecated since 4.83 when the RFC-defined words
+ "temperror" and "permerror" were introduced.
+
+JH/34 Re-introduce enforcement of no cutthrough delivery on transports having
+ transport-filters or DKIM-signing. The restriction was lost in the
+ consolidation of verify-callout and delivery SMTP handling.
+ Extend the restriction to also cover ARC-signing.
+
+JH/35 Cutthrough: for a final-dot response timeout (and nonunderstood responses)
+ in defer=pass mode supply a 450 to the initiator. Previously the message
+ would be spooled.
+
+PP/02 DANE: add dane_require_tls_ciphers SMTP Transport option; if unset,
+ tls_require_ciphers is used as before.
+
+HS/03 Malware Avast: Better match the Avast multiline protocol. Add
+ "pass_unscanned". Only tmpfails from the scanner are written to
+ the paniclog, as they may require admin intervention (permission
+ denied, license issues). Other scanner errors (like decompression
+ bombs) do not cause a paniclog entry.
+
+JH/36 Fix reinitialisation of DKIM logging variable between messages.
+ Previously it was possible to log spurious information in receive log
+ lines.
+
+JH/37 Bug 2255: Revert the disable of the OpenSSL session caching. This
+ triggered odd behaviour from Outlook Express clients.
+
+PP/03 Add util/renew-opendmarc-tlds.sh script for safe renewal of public
+ suffix list.
+
+JH/38 DKIM: accept Ed25519 pubkeys in SubjectPublicKeyInfo-wrapped form,
+ since the IETF WG has not yet settled on that versus the original
+ "bare" representation.
+
+JH/39 Fix syslog logging for syslog_timestamp=no and log_selector +millisec.
+ Previously the millisecond value corrupted the output.
+ Fix also for syslog_pid=no and log_selector +pid, for which the pid
+ corrupted the output.
+
+
+Exim version 4.90
+-----------------
+
+JH/01 Rework error string handling in TLS interface so that the caller in
+ more cases is responsible for logging. This permits library-sourced
+ string to be attached to addresses during delivery, and collapses
+ pairs of long lines into single ones.
+
+PP/01 Allow PKG_CONFIG_PATH to be set in Local/Makefile and use it correctly
+ during configuration. Wildcards are allowed and expanded.
+
+JH/02 Rework error string handling in DKIM to pass more info back to callers.
+ This permits better logging.
+
+JH/03 Rework the transport continued-connection mechanism: when TLS is active,
+ do not close it down and have the child transport start it up again on
+ the passed-on TCP connection. Instead, proxy the child (and any
+ subsequent ones) for TLS via a unix-domain socket channel. Logging is
+ affected: the continued delivery log lines do not have any DNSSEC, TLS
+ Certificate or OCSP information. TLS cipher information is still logged.
+
+JH/04 Shorten the log line for daemon startup by collapsing adjacent sets of
+ identical IP addresses on different listening ports. Will also affect
+ "exiwhat" output.
+
+PP/02 Bug 2070: uClibc defines __GLIBC__ without providing glibc headers;
+ add noisy ifdef guards to special-case this sillyness.
+ Patch from Bernd Kuhls.
+
+JH/05 Tighten up the checking in isip4 (et al): dotted-quad components larger
+ than 255 are no longer allowed.
+
+JH/06 Default openssl_options to include +no_ticket, to reduce load on peers.
+ Disable the session-cache too, which might reduce our load. Since we
+ currrectly use a new context for every connection, both as server and
+ client, there is no benefit for these.
+ GnuTLS appears to not support tickets server-side by default (we don't
+ call gnutls_session_ticket_enable_server()) but client side is enabled
+ by default on recent versions (3.1.3 +) unless the PFS priority string
+ is used (3.2.4 +).
+
+PP/03 Add $SOURCE_DATE_EPOCH support for reproducible builds, per spec at
+ <https://reproducible-builds.org/specs/source-date-epoch/>.
+
+JH/07 Fix smtp transport use of limited max_rcpt under mua_wrapper. Previously
+ the check for any unsuccessful recipients did not notice the limit, and
+ erroneously found still-pending ones.
+
+JH/08 Pipeline CHUNKING command and data together, on kernels that support
+ MSG_MORE. Only in-clear (not on TLS connections).
+
+JH/09 Avoid using a temporary file during transport using dkim. Unless a
+ transport-filter is involved we can buffer the headers in memory for
+ creating the signature, and read the spool data file once for the
+ signature and again for transmission.
+
+JH/10 Enable use of sendfile in Linux builds as default. It was disabled in
+ 4.77 as the kernel support then wasn't solid, having issues in 64bit
+ mode. Now, it's been long enough. Add support for FreeBSD also.
+
+JH/11 Bug 2104: Fix continued use of a transport connection with TLS. In the
+ case where the routing stage had gathered several addresses to send to
+ a host before calling the transport for the first, we previously failed
+ to close down TLS in the old transport process before passing the TCP
+ connection to the new process. The new one sent a STARTTLS command
+ which naturally failed, giving a failed delivery and bloating the retry
+ database. Investigation and fix prototype from Wolfgang Breyha.
+
+JH/12 Fix check on SMTP command input synchronisation. Previously there were
+ false-negatives in the check that the sender had not preempted a response
+ or prompt from Exim (running as a server), due to that code's lack of
+ awareness of the SMTP input buffering.
+
+PP/04 Add commandline_checks_require_admin option.
+ Exim drops privileges sanely, various checks such as -be aren't a
+ security problem, as long as you trust local users with access to their
+ own account. When invoked by services which pass untrusted data to
+ Exim, this might be an issue. Set this option in main configuration
+ AND make fixes to the calling application, such as using `--` to stop
+ processing options.
+
+JH/13 Do pipelining under TLS. Previously, although safe, no advantage was
+ taken. Now take care to pack both (client) MAIL,RCPT,DATA, and (server)
+ responses to those, into a single TLS record each way (this usually means
+ a single packet). As a side issue, smtp_enforce_sync now works on TLS
+ connections.
+
+PP/05 OpenSSL/1.1: use DH_bits() for more accurate DH param sizes. This
+ affects you only if you're dancing at the edge of the param size limits.
+ If you are, and this message makes sense to you, then: raise the
+ configured limit or use OpenSSL 1.1. Nothing we can do for older
+ versions.
+
+JH/14 For the "sock" variant of the malware scanner interface, accept an empty
+ cmdline element to get the documented default one. Previously it was
+ inaccessible.
+
+JH/15 Fix a crash in the smtp transport caused when two hosts in succession
+ are unsuable for non-message-specific reasons - eg. connection timeout,
+ banner-time rejection.
+
+JH/16 Fix logging of delivery remote port, when specified by router, under
+ callout/hold.
+
+PP/06 Repair manualroute's ability to take options in any order, even if one
+ is the name of a transport.
+ Fixes bug 2140.
+
+HS/01 Cleanup, prevent repeated use of -p/-oMr (CVE-2017-1000369)
+
+JH/17 Change the list-building routines interface to use the expanding-string
+ triplet model, for better allocation and copying behaviour.
+
+JH/18 Prebuild the data-structure for "builtin" macros, for faster startup.
+ Previously it was constructed the first time a possibly-matching string
+ was met in the configuration file input during startup; now it is done
+ during compilation.
+
+JH/19 Bug 2141: Use the full-complex API for Berkeley DB rather than the legacy-
+ compatible one, to avoid the (poorly documented) possibility of a config
+ file in the working directory redirecting the DB files, possibly correpting
+ some existing file. CVE-2017-10140 assigned for BDB.
+
+JH/20 Bug 2147: Do not defer for a verify-with-callout-and-random which is not
+ cache-hot. Previously, although the result was properly cached, the
+ initial verify call returned a defer.
+
+JH/21 Bug 2151: Avoid using SIZE on the MAIL for a callout verify, on any but
+ the main verify for receipient in uncached-mode.
+
+JH/22 Retire historical build files to an "unsupported" subdir. These are
+ defined as "ones for which we have no current evidence of testing".
+
+JH/23 DKIM: enforce the DNS pubkey record "h" permitted-hashes optional field,
+ if present. Previously it was ignored.
+
+JH/24 Start using specified-initialisers in C structure init coding. This is
+ a C99 feature (it's 2017, so now considered safe).
+
+JH/25 Use one-bit bitfields for flags in the "addr" data structure. Previously
+ if was a fixed-sized field and bitmask ops via macros; it is now more
+ extensible.
+
+PP/07 GitHub PR 56: Apply MariaDB build fix.
+ Patch provided by Jaroslav Škarvada.
+
+PP/08 Bug 2161: Fix regression in sieve quoted-printable handling introduced
+ during Coverity cleanups [4.87 JH/47]
+ Diagnosis and fix provided by Michael Fischer v. Mollard.
+
+JH/26 Fix DKIM bug: when the pseudoheader generated for signing was exactly
+ the right size to place the terminating semicolon on its own folded
+ line, the header hash was calculated to an incorrect value thanks to
+ the (relaxed) space the fold became.
+
+HS/02 Fix Bug 2130: large writes from the transport subprocess were chunked
+ and confused the parent.
+
+JH/27 Fix SOCKS bug: an unitialized pointer was deref'd by the transport process
+ which could crash as a result. This could lead to undeliverable messages.
+
+JH/28 Logging: "next input sent too soon" now shows where input was truncated
+ for log purposes.
+
+JH/29 Fix queue_run_in_order to ignore the PID portion of the message ID. This
+ matters on fast-turnover and PID-randomising systems, which were getting
+ out-of-order delivery.
+
+JH/30 Fix a logging bug on aarch64: an unsafe routine was previously used for
+ a possibly-overlapping copy. The symptom was that "Remote host closed
+ connection in response to HELO" was logged instead of the actual 4xx
+ error for the HELO.
+
+JH/31 Fix CHUNKING code to properly flush the unwanted chunk after an error.
+ Previously only that bufferd was discarded, resulting in SYMTP command
+ desynchronisation.
+
+JH/32 DKIM: when a message has multiple signatures matching an identity given
+ in dkim_verify_signers, run the dkim acl once for each. Previously only
+ one run was done. Bug 2189.
+
+JH/33 Downgrade an unfound-list name (usually a typo in the config file) from
+ "panic the current process" to "deliberately defer". The panic log is
+ still written with the problem list name; the mail and reject logs now
+ get a temp-reject line for the message that was being handled, saying
+ something like "domains check lookup or other defer". The SMTP 451
+ message is still "Temporary local problem".
+
+JH/34 Bug 2199: Fix a use-after-free while reading smtp input for header lines.
+ A crafted sequence of BDAT commands could result in in-use memory beeing
+ freed. CVE-2017-16943.
+
+HS/03 Bug 2201: Fix checking for leading-dot on a line during headers reading
+ from SMTP input. Previously it was always done; now only done for DATA
+ and not BDAT commands. CVE-2017-16944.
+
+JH/35 Bug 2201: Flush received data in BDAT mode after detecting an error fatal
+ to the message (such as an overlong header line). Previously this was
+ not done and we did not exit BDAT mode. Followon from the previous item
+ though a different problem.
+
+
+Exim version 4.89
+-----------------
+
+JH/01 Bug 1922: Support IDNA2008. This has slightly different conversion rules
+ than -2003 did; needs libidn2 in addition to libidn.
+
+JH/02 The path option on a pipe transport is now expanded before use.
+
+PP/01 GitHub PR 50: Do not call ldap_start_tls_s on ldapi:// connections.
+ Patch provided by "Björn", documentation fix added too.
+
+JH/03 Bug 2003: fix Proxy Protocol v2 handling: the address size field was
+ missing a wire-to-host endian conversion.
+
+JH/04 Bug 2004: fix CHUNKING in non-PIPELINEING mode. Chunk data following
+ close after a BDAT command line could be taken as a following command,
+ giving a synch failure. Fix by only checking for synch immediately
+ before acknowledging the chunk.
+
+PP/02 GitHub PR 52: many spelling fixes, which include fixing parsing of
+ no_require_dnssec option and creation of _HAVE_TRANSPORT_APPEND_MAILDIR
+ macro. Patches provided by Josh Soref.
+
+JH/05 Have the EHLO response advertise VRFY, if there is a vrfy ACL defined.
+ Previously we did not; the RFC seems ambiguous and VRFY is not listed
+ by IANA as a service extension. However, John Klensin suggests that we
+ should.
+
+JH/06 Bug 2017: Fix DKIM verification in -bh test mode. The data feed into
+ the dkim code may be unix-mode line endings rather than smtp wire-format
+ CRLF, so prepend a CR to any bare LF.
+
+JH/07 Rationalise the coding for callout smtp conversations and transport ones.
+ As a side-benfit, callouts can now use PIPELINING hence fewer round-trips.
+
+JH/08 Bug 2016: Fix DKIM verification vs. CHUNKING. Any BDAT commands after
+ the first were themselves being wrongly included in the feed into dkim
+ processing; with most chunk sizes in use this resulted in an incorrect
+ body hash calculated value.
+
+JH/09 Bug 2014: permit inclusion of a DKIM-Signature header in a received
+ DKIM signature block, for verification. Although advised against by
+ standards it is specifically not ruled illegal.
+
+JH/10 Bug 2025: Fix reception of (quoted) local-parts with embedded spaces.
+
+JH/11 Bug 2029: Fix crash in DKIM verification when a message signature block is
+ missing a body hash (the bh= tag).
+
+JH/12 Bug 2018: Re-order Proxy Protocol startup versus TLS-on-connect startup.
+ It seems that HAProxy sends the Proxy Protocol information in clear and
+ only then does a TLS startup, so do the same.
+
+JH/13 Bug 2027: Avoid attempting to use TCP Fast Open for non-transport client
+ TCP connections (such as for Spamd) unless the daemon successfully set
+ Fast Open mode on its listening sockets. This fixes breakage seen on
+ too-old kernels or those not configured for Fast Open, at the cost of
+ requiring both directions being enabled for TFO, and TFO never being used
+ by non-daemon-related Exim processes.
+
+JH/14 Bug 2000: Reject messages recieved with CHUNKING but with malformed line
+ endings, at least on the first header line. Try to canonify any that get
+ past that check, despite the cost.
+
+JH/15 Angle-bracket nesting (an error inserted by broken sendmails) levels are
+ now limited to an arbitrary five deep, while parsing addresses with the
+ strip_excess_angle_brackets option enabled.
+
+PP/03 Bug 2018: For Proxy Protocol and TLS-on-connect, do not over-read and
+ instead leave the unprompted TLS handshake in socket buffer for the
+ TLS library to consume.
+
+PP/04 Bug 2018: Also handle Proxy Protocol v2 safely.
+
+PP/05 FreeBSD compat: handle that Ports no longer create /usr/bin/perl
+
+JH/16 Drop variables when they go out of scope. Memory management drops a whole
+ region in one operation, for speed, and this leaves assigned pointers
+ dangling. Add checks run only under the testsuite which checks all
+ variables at a store-reset and panics on a dangling pointer; add code
+ explicitly nulling out all the variables discovered. Fixes one known
+ bug: a transport crash, where a dangling pointer for $sending_ip_address
+ originally assigned in a verify callout, is re-used.
+
+PP/06 Drop '.' from @INC in various Perl scripts.
+
+PP/07 Switch FreeBSD iconv to always use the base-system libc functions.
+
+PP/08 Reduce a number of compilation warnings under clang; building with
+ CC=clang CFLAGS+=-Wno-dangling-else -Wno-logical-op-parentheses
+ should be warning-free.
+
+JH/17 Fix inbound CHUNKING when DKIM disabled at runtime.
+
+HS/01 Fix portability problems introduced by PP/08 for platforms where
+ realloc(NULL) is not equivalent to malloc() [SunOS et al].
+
+HS/02 Bug 1974: Fix missing line terminator on the last received BDAT
+ chunk. This allows us to accept broken chunked messages. We need a more
+ general solution here.
+
+PP/09 Wrote util/chunking_fixqueue_finalnewlines.pl to help recover
+ already-broken messages in the queue.
+
+JH/18 Bug 2061: Fix ${extract } corrupting an enclosing ${reduce } $value.
+
+JH/19 Fix reference counting bug in routing-generated-address tracking.
+
+
+Exim version 4.88
+-----------------
+
+JH/01 Use SIZE on MAIL FROM in a cutthrough connection, if the destination
+ supports it and a size is available (ie. the sending peer gave us one).
+
+JH/02 The obsolete acl condition "demime" is removed (finally, after ten
+ years of being deprecated). The replacements are the ACLs
+ acl_smtp_mime and acl_not_smtp_mime.
+
+JH/03 Upgrade security requirements imposed for hosts_try_dane: previously
+ a downgraded non-dane trust-anchor for the TLS connection (CA-style)
+ or even an in-clear connection were permitted. Now, if the host lookup
+ was dnssec and dane was requested then the host is only used if the
+ TLSA lookup succeeds and is dnssec. Further hosts (eg. lower priority
+ MXs) will be tried (for hosts_try_dane though not for hosts_require_dane)
+ if one fails this test.
+ This means that a poorly-configured remote DNS will make it incommunicado;
+ but it protects against a DNS-interception attack on it.
+
+JH/04 Bug 1810: make continued-use of an open smtp transport connection
+ non-noisy when a race steals the message being considered.
+
+JH/05 If main configuration option tls_certificate is unset, generate a
+ self-signed certificate for inbound TLS connections.
+
+JH/06 Bug 165: hide more cases of password exposure - this time in expansions
+ in rewrites and routers.
+
+JH/07 Retire gnutls_require_mac et.al. These were nonfunctional since 4.80
+ and logged a warning sing 4.83; now they are a configuration file error.
+
+JH/08 Bug 1836: Fix crash in VRFY handling when handed an unqualified name
+ (lacking @domain). Apply the same qualification processing as RCPT.
+
+JH/09 Bug 1804: Avoid writing msglog files when in -bh or -bhc mode.
+
+JH/10 Support ${sha256:} applied to a string (as well as the previous
+ certificate).
+
+JH/11 Cutthrough: avoid using the callout hints db on a verify callout when
+ a cutthrough deliver is pending, as we always want to make a connection.
+ This also avoids re-routing the message when later placing the cutthrough
+ connection after a verify cache hit.
+ Do not update it with the verify result either.
+
+JH/12 Cutthrough: disable when verify option success_on_redirect is used, and
+ when routing results in more than one destination address.
+
+JH/13 Cutthrough: expand transport dkim_domain option when testing for dkim
+ signing (which inhibits the cutthrough capability). Previously only
+ the presence of an option was tested; now an expansion evaluating as
+ empty is permissible (obviously it should depend only on data available
+ when the cutthrough connection is made).
+
+JH/14 Fix logging of errors under PIPELINING. Previously the log line giving
+ the relevant preceding SMTP command did not note the pipelining mode.
+
+JH/15 Fix counting of empty lines in $body_linecount and $message_linecount.
+ Previously they were not counted.
+
+JH/16 DANE: treat a TLSA lookup response having all non-TLSA RRs, the same
+ as one having no matching records. Previously we deferred the message
+ that needed the lookup.
+
+JH/17 Fakereject: previously logged as a normal message arrival "<="; now
+ distinguished as "(=".
+
+JH/18 Bug 1867: make the fail_defer_domains option on a dnslookup router work
+ for missing MX records. Previously it only worked for missing A records.
+
+JH/19 Bug 1850: support Radius libraries that return REJECT_RC.
+
+JH/20 Bug 1872: Ensure that acl_smtp_notquit is run when the connection drops
+ after the data-go-ahead and data-ack. Patch from Jason Betts.
+
+JH/21 Bug 1846: Send DMARC forensic reports for reject and quarantine results,
+ even for a "none" policy. Patch from Tony Meyer.
+
+JH/22 Fix continued use of a connection for further deliveries. If a port was
+ specified by a router, it must also match for the delivery to be
+ compatible.
+
+JH/23 Bug 1874: fix continued use of a connection for further deliveries.
+ When one of the recipients of a message was unsuitable for the connection
+ (has no matching addresses), we lost track of needing to mark it
+ deferred. As a result mail would be lost.
+
+JH/24 Bug 1832: Log EHLO response on getting conn-close response for HELO.
+
+JH/25 Decoding ACL controls is now done using a binary search; the source code
+ takes up less space and should be simpler to maintain. Merge the ACL
+ condition decode tables also, with similar effect.
+
+JH/26 Fix problem with one_time used on a redirect router which returned the
+ parent address unchanged. A retry would see the parent address marked as
+ delivered, so not attempt the (identical) child. As a result mail would
+ be lost.
+
+JH/27 Fix a possible security hole, wherein a process operating with the Exim
+ UID can gain a root shell. Credit to http://www.halfdog.net/ for
+ discovery and writeup. Ubuntu bug 1580454; no bug raised against Exim
+ itself :(
+
+JH/28 Enable {spool,log} filesystem space and inode checks as default.
+ Main config options check_{log,spool}_{inodes,space} are now
+ 100 inodes, 10MB unless set otherwise in the configuration.
+
+JH/29 Fix the connection_reject log selector to apply to the connect ACL.
+ Previously it only applied to the main-section connection policy
+ options.
+
+JH/30 Bug 1897: fix callouts connection fallback from TLS to cleartext.
+
+PP/01 Changed default Diffie-Hellman parameters to be Exim-specific, created
+ by me. Added RFC7919 DH primes as an alternative.
+
+PP/02 Unbreak build via pkg-config with new hash support when crypto headers
+ are not in the system include path.
+
+JH/31 Fix longstanding bug with aborted TLS server connection handling. Under
+ GnuTLS, when a session startup failed (eg because the client disconnected)
+ Exim did stdio operations after fclose. This was exposed by a recent
+ change which nulled out the file handle after the fclose.
+
+JH/32 Bug 1909: Fix OCSP proof verification for cases where the proof is
+ signed directly by the cert-signing cert, rather than an intermediate
+ OCSP-signing cert. This is the model used by LetsEncrypt.
+
+JH/33 Bug 1914: Ensure socket is nonblocking before draining after SMTP QUIT.
+
+HS/01 Fix leak in verify callout under GnuTLS, about 3MB per recipient on
+ an incoming connection.
+
+HS/02 Bug 1802: Do not half-close the connection after sending a request
+ to rspamd.
+
+HS/03 Use "auto" as the default EC curve parameter. For OpenSSL < 1.0.2
+ fallback to "prime256v1".
+
+JH/34 SECURITY: Use proper copy of DATA command in error message.
+ Could leak key material. Remotely exploitable. CVE-2016-9963.
+
+
+Exim version 4.87
+-----------------
+
+JH/01 Bug 1664: Disable OCSP for GnuTLS library versions at/before 3.3.16
+ and 3.4.4 - once the server is enabled to respond to an OCSP request
+ it does even when not requested, resulting in a stapling non-aware
+ client dropping the TLS connection.
+
+TF/01 Code cleanup: Overhaul the debug_selector and log_selector machinery to
+ support variable-length bit vectors. No functional change.
+
+TF/02 Improve the consistency of logging incoming and outgoing interfaces.
+ The I= interface field on outgoing lines is now after the H= remote
+ host field, same as incoming lines. There is a separate
+ outgoing_interface log selector which allows you to disable the
+ outgoing I= field.
+
+JH/02 Bug 728: Close logfiles after a daemon-process "exceptional" log write.
+ If not running log_selector +smtp_connection the mainlog would be held
+ open indefinitely after a "too many connections" event, including to a
+ deleted file after a log rotate. Leave the per net connection logging
+ leaving it open for efficiency as that will be quickly detected by the
+ check on the next write.
+
+HS/01 Bug 1671: Fix post transport crash.
+ Processing the wait-<transport> messages could crash the delivery
+ process if the message IDs didn't exist for some reason. When
+ using 'split_spool_directory=yes' the construction of the spool
+ file name failed already, exposing the same netto behaviour.
+
+JH/03 Bug 425: Capture substrings in $regex1, $regex2 etc from regex &
+ mime_regex ACL conditions.
+
+JH/04 Bug 1686: When compiled with EXPERIMENTAL_DSN_INFO: Add extra information
+ to DSN fail messages (bounces): remote IP, remote greeting, remote response
+ to HELO, local diagnostic string.
+
+JH/05 Downgrade message for a TLS-certificate-based authentication fail from
+ log line to debug. Even when configured with a tls authenticator many
+ client connections are expected to not authenticate in this way, so
+ an authenticate fail is not an error.
+
+HS/02 Add the Exim version string to the process info. This way exiwhat
+ gives some more detail about the running daemon.
+
+JH/06 Bug 1395: time-limit caching of DNS lookups, to the TTL value. This may
+ matter for fast-change records such as DNSBLs.
+
+JH/07 Bug 1678: Always record an interface option value, if set, as part of a
+ retry record, even if constant. There may be multiple transports with
+ different interface settings and the retry behaviour needs to be kept
+ distinct.
+
+JH/08 Bug 1586: exiqgrep now refuses to run if there are unexpected arguments.
+
+JH/09 Bug 1700: ignore space & tab embedded in base64 during decode.
+
+JH/10 Bug 840: fix log_defer_output option of pipe transport
+
+JH/11 Bug 830: use same host for all RCPTS of a message, even under
+ hosts_randomize. This matters a lot when combined with mua_wrapper.
+
+JH/12 Bug 1706: percent and underbar characters are no longer escaped by the
+ ${quote_pgsql:<string>} operator.
+
+JH/13 Bug 1708: avoid misaligned access in cached lookup.
+
+JH/14 Change header file name for freeradius-client. Relevant if compiling
+ with Radius support; from the Gentoo tree and checked under Fedora.
+
+JH/15 Bug 1712: Introduce $prdr_requested flag variable
+
+JH/16 Bug 1714: Permit an empty string as expansion result for transport
+ option transport_filter, meaning no filtering.
+
+JH/17 Bug 1713: Fix non-PDKIM_DEBUG build. Patch from Jasen Betts.
+
+JH/18 Bug 1709: When built with TLS support, the tls_advertise_hosts option now
+ defaults to "*" (all hosts). The variable is now available when not built
+ with TLS, default unset, mainly to enable keeping the testsuite sane.
+ If a server certificate is not supplied (via tls_certificate) an error is
+ logged, and clients will find TLS connections fail on startup. Presumably
+ they will retry in-clear.
+ Packagers of Exim are strongly encouraged to create a server certificate
+ at installation time.
+
+HS/03 Add -bP config_file as a synonym for -bP configure_file, for consistency
+ with the $config_file variable.
+
+JH/19 Two additional event types: msg:rcpt:defer and msg:rcpt:host:defer. Both
+ in transport context, after the attempt, and per-recipient. The latter type
+ is per host attempted. The event data is the error message, and the errno
+ information encodes the lookup type (A vs. MX) used for the (first) host,
+ and the trailing two digits of the smtp 4xx response.
+
+GF/01 Bug 1715: Fix for race condition in exicyclog, where exim could attempt
+ to write to mainlog (or rejectlog, paniclog) in the window between file
+ creation and permissions/ownership being changed. Particularly affects
+ installations where exicyclog is run as root, rather than exim user;
+ result is that the running daemon panics and dies.
+
+JH/20 Bug 1701: For MySQL lookups, support MySQL config file option group names.
+
+JH/21 Bug 1720: Add support for priority groups and weighted-random proxy
+ selection for the EXPERIMENTAL_SOCKS feature, via new per-proxy options
+ "pri" and "weight". Note that the previous implicit priority given by the
+ list order is no longer honoured.
+
+JH/22 Bugs 963, 1721: Fix some corner cases in message body canonicalization
+ for DKIM processing.
+
+JH/23 Move SOCKS5 support from Experimental to mainline, enabled for a build
+ by defining SUPPORT_SOCKS.
+
+JH/26 Move PROXY support from Experimental to mainline, enabled for a build
+ by defining SUPPORT_PROXY. Note that the proxy_required_hosts option
+ is renamed to hosts_proxy, and the proxy_{host,target}_{address,port}.
+ variables are renamed to proxy_{local,external}_{address,port}.
+
+JH/27 Move Internationalisation support from Experimental to mainline, enabled
+ for a build by defining SUPPORT_I18N
+
+JH/28 Bug 1745: Fix redis lookups to handle (quoted) spaces embedded in parts
+ of the query string, and make ${quote_redis:} do that quoting.
+
+JH/29 Move Events support from Experimental to mainline, enabled by default
+ and removable for a build by defining DISABLE_EVENT.
+
+JH/30 Updated DANE implementation code to current from Viktor Dukhovni.
+
+JH/31 Fix bug with hosts_connection_nolog and named-lists which were wrongly
+ cached by the daemon.
+
+JH/32 Move Redis support from Experimental to mainline, enabled for a build
+ by defining LOOKUP_REDIS. The libhiredis library is required.
+
+JH/33 Bug 1748: Permit ACL dnslists= condition in non-smtp ACLs if explicit
+ keys are given for lookup.
+
+JH/34 Bug 1192: replace the embedded copy of PolarSSL RSA routines in the DKIM
+ support, by using OpenSSL or GnuTLS library ones. This means DKIM is
+ only supported when built with TLS support. The PolarSSL SHA routines
+ are still used when the TLS library is too old for convenient support.
+
+JH/35 Require SINGLE_DH_USE by default in OpenSSL (main config option
+ openssl_options), for security. OpenSSL forces this from version 1.1.0
+ server-side so match that on older versions.
+
+JH/36 Bug 1778: longstanding bug in memory use by the ${run } expansion: A fresh
+ allocation for $value could be released as the expansion processing
+ concluded, but leaving the global pointer active for it.
+
+JH/37 Bug 1769: Permit a VRFY ACL to override the default 252 response,
+ and to use the domains and local_parts ACL conditions.
+
+JH/38 Fix cutthrough bug with body lines having a single dot. The dot was
+ incorrectly not doubled on cutthrough transmission, hence seen as a
+ body-termination at the receiving system - resulting in truncated mails.
+ Commonly the sender saw a TCP-level error, and retransmitted the message
+ via the normal store-and-forward channel. This could result in duplicates
+ received - but deduplicating mailstores were liable to retain only the
+ initial truncated version.
+
+JH/39 Bug 1781: Fix use of DKIM private-keys having trailing '=' in the base-64.
+
+JH/40 Fix crash in queryprogram router when compiled with EXPERIMENTAL_SRS.
+
+JH/41 Bug 1792: Fix selection of headers to sign for DKIM: bottom-up. While
+ we're in there, support oversigning also; bug 1309.
+
+JH/42 Bug 1796: Fix error logged on a malware scanner connection failure.
+
+HS/04 Add support for keep_environment and add_environment options.
+
+JH/43 Tidy coding issues detected by gcc --fsanitize=undefined. Some remain;
+ either intentional arithmetic overflow during PRNG, or testing config-
+ induced overflows.
+
+JH/44 Bug 1800: The combination of a -bhc commandline option and cutthrough
+ delivery resulted in actual delivery. Cancel cutthrough before DATA
+ stage.
+
+JH/45 Fix cutthrough, when connection not opened by verify and target hard-
+ rejects a recipient: pass the reject to the originator.
+
+JH/46 Multiple issues raised by Coverity. Some were obvious or plausible bugs.
+ Many were false-positives and ignorable, but it's worth fixing the
+ former class.
+
+JH/47 Fix build on HP-UX and older Solaris, which need (un)setenv now also
+ for the new environment-manipulation done at startup. Move the routines
+ from being local to tls.c to being global via the os.c file.
+
+JH/48 Bug 1807: Fix ${extract } for the numeric/3-string case. While preparsing
+ an extract embedded as result-arg for a map, the first arg for extract
+ is unavailable so we cannot tell if this is a numbered or keyed
+ extraction. Accept either.
+
+
+Exim version 4.86
+-----------------
+
+JH/01 Bug 1545: The smtp transport option "retry_include_ip_address" is now
+ expanded.
+
+JH/02 The smtp transport option "multi_domain" is now expanded.
+
+JH/03 The smtp transport now requests PRDR by default, if the server offers
+ it.
+
+JH/04 Certificate name checking on server certificates, when exim is a client,
+ is now done by default. The transport option tls_verify_cert_hostnames
+ can be used to disable this per-host. The build option
+ EXPERIMENTAL_CERTNAMES is withdrawn.
+
+JH/05 The value of the tls_verify_certificates smtp transport and main options
+ default to the word "system" to access the system default CA bundle.
+ For GnuTLS, only version 3.0.20 or later.
+
+JH/06 Verification of the server certificate for a TLS connection is now tried
+ (but not required) by default. The verification status is now logged by
+ default, for both outbound TLS and client-certificate supplying inbound
+ TLS connections
+
+JH/07 Changed the default rfc1413 lookup settings to disable calls. Few
+ sites use this now.
+
+JH/08 The EXPERIMENTAL_DSN compile option is no longer needed; all Delivery
+ Status Notification (bounce) messages are now MIME format per RFC 3464.
+ Support for RFC 3461 DSN options NOTIFY,ENVID,RET,ORCPT can be advertised
+ under the control of the dsn_advertise_hosts option, and routers may
+ have a dsn_lasthop option.
+
+JH/09 A timeout of 2 minutes is now applied to all malware scanner types by
+ default, modifiable by a malware= option. The list separator for
+ the options can now be changed in the usual way. Bug 68.
+
+JH/10 The smtp_receive_timeout main option is now expanded before use.
+
+JH/11 The incoming_interface log option now also enables logging of the
+ local interface on delivery outgoing connections.
+
+JH/12 The cutthrough-routing facility now supports multi-recipient mails,
+ if the interface and destination host and port all match.
+
+JH/13 Bug 344: The verify = reverse_host_lookup ACL condition now accepts a
+ /defer_ok option.
+
+JH/14 Bug 1573: The spam= ACL condition now additionally supports Rspamd.
+ Patch from Andrew Lewis.
+
+JH/15 Bug 670: The spamd_address main option (for the spam= ACL condition)
+ now supports optional time-restrictions, weighting, and priority
+ modifiers per server. Patch originally by <rommer@active.by>.
+
+JH/16 The spamd_address main option now supports a mixed list of local
+ and remote servers. Remote servers can be IPv6 addresses, and
+ specify a port-range.
+
+JH/17 Bug 68: The spamd_address main option now supports an optional
+ timeout value per server.
+
+JH/18 Bug 1581: Router and transport options headers_add/remove can
+ now have the list separator specified.
+
+JH/19 Bug 392: spamd_address, and clamd av_scanner, now support retry
+ option values.
+
+JH/20 Bug 1571: Ensure that $tls_in_peerdn is set, when verification fails
+ under OpenSSL.
+
+JH/21 Support for the A6 type of dns record is withdrawn.
+
+JH/22 Bug 608: The result of a QUIT or not-QUIT toplevel ACL now matters
+ rather than the verbs used.
+
+JH/23 Bug 1572: Increase limit on SMTP confirmation message copy size
+ from 255 to 1024 chars.
+
+JH/24 Verification callouts now attempt to use TLS by default.
+
+HS/01 DNSSEC options (dnssec_require_domains, dnssec_request_domains)
+ are generic router options now. The defaults didn't change.
+
+JH/25 Bug 466: Add RFC2322 support for MIME attachment filenames.
+ Original patch from Alexander Shikoff, worked over by JH.
+
+HS/02 Bug 1575: exigrep falls back to autodetection of compressed
+ files if ZCAT_COMMAND is not executable.
+
+JH/26 Bug 1539: Add timeout/retry options on dnsdb lookups.
+
+JH/27 Bug 286: Support SOA lookup in dnsdb lookups.
+
+JH/28 Bug 1588: Do not use the A lookup following an AAAA for setting the FQDN.
+ Normally benign, it bites when the pair was led to by a CNAME;
+ modern usage is to not canonicalize the domain to a CNAME target
+ (and we were inconsistent anyway for A-only vs AAAA+A).
+
+JH/29 Bug 1632: Removed the word "rejected" from line logged for ACL discards.
+
+JH/30 Check the forward DNS lookup for DNSSEC, in addition to the reverse,
+ when evaluating $sender_host_dnssec.
+
+JH/31 Check the HELO verification lookup for DNSSEC, adding new
+ $sender_helo_dnssec variable.
+
+JH/32 Bug 1397: Enable ECDHE on OpenSSL, just the NIST P-256 curve.
+
+JH/33 Bug 1346: Note MAIL cmd seen in -bS batch, to avoid smtp_no_mail log.
+
+JH/34 Bug 1648: Fix a memory leak seen with "mailq" and large queues.
+
+JH/35 Bug 1642: Fix support of $spam_ variables at delivery time. Was
+ documented as working, but never had. Support all but $spam_report.
+
+JH/36 Bug 1659: Guard checking of input smtp commands again pseudo-command
+ added for tls authenticator.
+
+HS/03 Add perl_taintmode main config option
+
+
+Exim version 4.85
+-----------------
+
+TL/01 When running the test suite, the README says that variables such as
+ no_msglog_check are global and can be placed anywhere in a specific
+ test's script, however it was observed that placement needed to be near
+ the beginning for it to behave that way. Changed the runtest perl
+ script to read through the entire script once to detect and set these
+ variables, reset to the beginning of the script, and then run through
+ the script parsing/test process like normal.
+
+TL/02 The BSD's have an arc4random API. One of the functions to induce
+ adding randomness was arc4random_stir(), but it has been removed in
+ OpenBSD 5.5. Detect this OpenBSD version and skip calling this
+ function when detected.
+
+JH/01 Expand the EXPERIMENTAL_TPDA feature. Several different events now
+ cause callback expansion.
+
+TL/03 Bugzilla 1518: Clarify "condition" processing in routers; that
+ syntax errors in an expansion can be treated as a string instead of
+ logging or causing an error, due to the internal use of bool_lax
+ instead of bool when processing it.
+
+JH/02 Add EXPERIMENTAL_DANE, allowing for using the DNS as trust-anchor for
+ server certificates when making smtp deliveries.
+
+JH/03 Support secondary-separator specifier for MX, SRV, TLSA lookups.
+
+JH/04 Add ${sort {list}{condition}{extractor}} expansion item.
+
+TL/04 Bugzilla 1216: Add -M (related messages) option to exigrep.
+
+TL/05 GitHub Issue 18: Adjust logic testing for true/false in redis lookups.
+ Merged patch from Sebastian Wiedenroth.
+
+JH/05 Fix results-pipe from transport process. Several recipients, combined
+ with certificate use, exposed issues where response data items split
+ over buffer boundaries were not parsed properly. This eventually
+ resulted in duplicates being sent. This issue only became common enough
+ to notice due to the introduction of connection certificate information,
+ the item size being so much larger. Found and fixed by Wolfgang Breyha.
+
+JH/06 Bug 1533: Fix truncation of items in headers_remove lists. A fixed
+ size buffer was used, resulting in syntax errors when an expansion
+ exceeded it.
+
+JH/07 Add support for directories of certificates when compiled with a GnuTLS
+ version 3.3.6 or later.
+
+JH/08 Rename the TPDA experimental facility to Event Actions. The #ifdef
+ is EXPERIMENTAL_EVENT, the main-configuration and transport options
+ both become "event_action", the variables become $event_name, $event_data
+ and $event_defer_errno. There is a new variable $verify_mode, usable in
+ routers, transports and related events. The tls:cert event is now also
+ raised for inbound connections, if the main configuration event_action
+ option is defined.
+
+TL/06 In test suite, disable OCSP for old versions of openssl which contained
+ early OCSP support, but no stapling (appears to be less than 1.0.0).
+
+JH/09 When compiled with OpenSSL and EXPERIMENTAL_CERTNAMES, the checks on
+ server certificate names available under the smtp transport option
+ "tls_verify_cert_hostname" now do not permit multi-component wildcard
+ matches.
+
+JH/10 Time-related extraction expansions from certificates now use the main
+ option "timezone" setting for output formatting, and are consistent
+ between OpenSSL and GnuTLS compilations. Bug 1541.
+
+JH/11 Fix a crash in mime ACL when meeting a zero-length, quoted or RFC2047-
+ encoded parameter in the incoming message. Bug 1558.
+
+JH/12 Bug 1527: Autogrow buffer used in reading spool files. Since they now
+ include certificate info, eximon was claiming there were spoolfile
+ syntax errors.
+
+JH/13 Bug 1521: Fix ldap lookup for single-attr request, multiple-attr return.
+
+JH/14 Log delivery-related information more consistently, using the sequence
+ "H=<name> [<ip>]" wherever possible.
+
+TL/07 Bug 1547: Omit RFCs from release. Draft and RFCs have licenses which
+ are problematic for Debian distribution, omit them from the release
+ tarball.
+
+JH/15 Updates and fixes to the EXPERIMENTAL_DSN feature.
+
+JH/16 Fix string representation of time values on 64bit time_t architectures.
+ Bug 1561.
+
+JH/17 Fix a null-indirection in certextract expansions when a nondefault
+ output list separator was used.
+
+
+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 introduced 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_DSN 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 both 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 modifier 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" from "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 upgrade 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 Workaround 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 compatibility 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 anomalous, 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 responses 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 particular, 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
+ whitespace 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 output 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 determined 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 messages, 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 assuming 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 outputting 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
+ $local_part_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.
+ However, 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 error. 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 canonicalized.
+
+ 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. However, 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 release 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 guaranteed 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_UNAVAILABLE (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 legitimately 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/DANE-draft-notes b/doc/DANE-draft-notes
new file mode 100644
index 0000000..21b3992
--- /dev/null
+++ b/doc/DANE-draft-notes
@@ -0,0 +1,11 @@
+
+draft 11
+
+3.1.2 - Para 4 (records with Sel Full(0) are discouraged)
+==> There's a matching type Full but not such a Selector type.
+ Should this be "Cert(0), or Matching Type Full(0)" ?
+ Suspect the latter.
+
+3.1.2 Needs a para added regarding certificate date verification,
+ to contrast with the requirement to NOT check for
+ DANE-EE defined in 3.1.1
diff --git a/doc/Exim3.upgrade b/doc/Exim3.upgrade
new file mode 100644
index 0000000..4ab94c4
--- /dev/null
+++ b/doc/Exim3.upgrade
@@ -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 details.
+
+
+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
index 0000000..86d4a4d
--- /dev/null
+++ b/doc/Exim4.upgrade
@@ -0,0 +1,1734 @@
+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 (see
+ "Security" below) for checking the files required by this option. 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 deferral 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_recipients,
+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{command 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. Exim dropped the use of seteuid() in
+most places. But recent (2020-10/2021-04) vulnerabilities forced us to
+re-introduce seteuid() for opening the database files (hint files) as secure as
+possible. For future (>= 4.95) versions we work on a solution that
+does not need the seteuid call.
+
+. 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
index 0000000..ab4e5aa
--- /dev/null
+++ b/doc/GnuTLS-FAQ.txt
@@ -0,0 +1,414 @@
+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? (And SHA-1?)
+(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? (And SHA-1?)
+--------------------------------------------
+
+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.
+
+The same has happened to SHA-1: there are real-world collision attacks against
+SHA-1, so SHA-1 is mostly defunct in certificates. GnuTLS no longer supports
+its use in TLS certificates.
+
+
+
+(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
index 0000000..2986b2c
--- /dev/null
+++ b/doc/NewStuff
@@ -0,0 +1,1476 @@
+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 Git before the documentation is updated. Once
+the documentation is updated, this file is reduced to a short list.
+
+Version 4.96
+------------
+
+ 1. A new ACL condition: seen. Records/tests a timestamp against a key.
+
+ 2. A variant of the "mask" expansion operator to give normalised IPv6.
+
+ 3. UTC output option for exim_dumpdb, exim_fixdb.
+
+ 4. An event for failing TLS connects to the daemon.
+
+ 5. The ACL "debug" control gains options "stop", "pretrigger" and "trigger".
+
+ 6. Query-style lookups are now checked for quoting, if the query string is
+ built using untrusted data ("tainted"). For now lack of quoting is merely
+ logged; a future release will upgrade this to an error.
+
+ 7. The expansion conditions match_<list-type> and inlist now set $value for
+ the expansion of the "true" result of the ${if}. With a static list, this
+ can be used for de-tainting.
+
+ 8. Recipient verify callouts now set $domain_data & $local_part_data, with
+ de-tainted values.
+
+Version 4.95
+------------
+
+ 1. The fast-ramp two phase queue run support, previously experimental, is
+ now supported by default.
+
+ 2. The native SRS support, previously experimental, is now supported. It is
+ not built unless specified in the Local/Makefile.
+
+ 3. TLS resumption support, previously experimental, is now supported and
+ included in default builds.
+
+ 4. Single-key LMDB lookups, previously experimental, are now supported.
+ The support is not built unless specified in the Local/Makefile.
+
+ 5. Option "message_linelength_limit" on the smtp transport to enforce (by
+ default) the RFC 998 character limit.
+
+ 6. An option to ignore the cache on a lookup.
+
+ 7. Quota checking during reception (i.e. at SMTP time) for appendfile-
+ transport-managed quotas.
+
+ 8. Sqlite lookups accept a "file=<path>" option to specify a per-operation
+ db file, replacing the previous prefix to the SQL string (which had
+ issues when the SQL used tainted values).
+
+ 9. Lsearch lookups accept a "ret=full" option, to return both the portion
+ of the line matching the key, and the remainder.
+
+10. A command-line option to have a daemon not create a notifier socket.
+
+11. Faster TLS startup. When various configuration options contain no
+ expandable elements, the information can be preloaded and cached rather
+ than the previous behaviour of always loading at startup time for every
+ connection. This helps particularly for the CA bundle.
+
+12. Proxy Protocol Timeout is configurable via "proxy_protocol_timeout"
+ main config option.
+
+13. Option "smtp_accept_max_per_connection" is now expanded.
+
+14. Log selector "queue_time_exclusive", enabled by default, to exclude the
+ time taken for reception from QT log elements.
+
+15. Main option "smtp_backlog_monitor", to set a level above which listen
+ socket backlogs are logged.
+
+16. Main option "hosts_require_helo", requiring HELO or EHLO before MAIL.
+
+17. A main config option "allow_insecure_tainted_data" allows to turn
+
+18. TLS ALPN handling. By default, refuse TLS connections that try to specify
+ a non-smtp (eg. http) use. Options for customising.
+
+19. Support for MacOS (darwin) has been dropped.
+
+
+Version 4.94
+------------
+
+ 1. EXPERIMENTAL_SRS_NATIVE optional build feature. See the experimental.spec
+ file.
+
+ 2. Channel-binding for authenticators is now supported under OpenSSL.
+ Previously it was GnuTLS-only.
+
+ 3. A msg:defer event.
+
+ 4. Client-side support in the gsasl authenticator. Tested against the
+ plaintext driver for PLAIN; only against itself for SCRAM-SHA-1 and
+ SCRAM-SHA-1-PLUS methods.
+
+ 5. Server-side support in the gsasl authenticator for encrypted passwords, as
+ an alternate for the existing plaintext.
+
+ 6. Variable $local_part_data now also set by router check_local_user option,
+ with an de-tainted version of $local_part.
+
+ 7. Named-list definitions can now be prefixed "hide" so that "-bP" commands do
+ not output the content. Previously this could only be done on options.
+
+ 8. As an experimental feature, the dovecot authentication driver supports inet
+ sockets. Previously it was unix-domain sockets only.
+
+ 9. The ACL control "queue_only" can also be spelled "queue", and now takes an
+ option "first_pass_route" to do the same as a "-odqs" on the command line.
+
+10. Items specified for the router and transport headers_remove option can use
+ a trailing asterisk to specify globbing.
+
+11. New $queue_size variable.
+
+12. New variables $local_part_{pre,suf}fix_v.
+
+13. New main option "sqlite_dbfile", for use in preference to prefixing the
+ lookup string. The older method fails when tainted variables are used
+ in the lookup, as the filename becomes tainted. The new method keeps the
+ filename separate.
+
+14. Options on the dsearch lookup, to return the full path and to filter
+ filetypes for matching.
+
+15. Options on pgsql and mysql lookups, to specify server separate from the
+ lookup string.
+
+16. An option on all single-key lookups, to return (on a hit) a de-tainted
+ version of the lookup key rather than the looked-up data.
+
+17. $domain_data and $local_part_data are now set by all list-match successes.
+ Previously only list items that performed lookups did so.
+ Also, matching list items that are tail-match or RE-match now set the
+ numeric variables $0 (etc) in the same way os other RE matches.
+
+18. Expansion item ${listquote {<char} {<item>}}.
+
+19. An option for the ${readsocket {}{}{}} expansion to make the result data
+ cacheable.
+
+20. dkim_verify_min_keysizes, a list of minimum acceptable public-key sizes.
+
+21. bounce_message_file and warn_message_file are now expanded before use.
+
+22. New main config option spf_smtp_comment_template to customise the
+ $spf_smtp_comment variable
+
+
+
+Version 4.93
+------------
+
+ 1. An "external" authenticator, per RFC 4422 Appendix A.
+
+ 2. A JSON lookup type, and JSON variants of the forall/any expansion conditions.
+
+ 3. Variables $tls_in_cipher_std, $tls_out_cipher_std giving the RFC names
+ for ciphersuites.
+
+ 4. Log_selectors "msg_id" (on by default) and "msg_id_created".
+
+ 5. A case_insensitive option for verify=not_blind.
+
+ 6. EXPERIMENTAL_TLS_RESUME optional build feature. See the experimental.spec
+ file.
+
+ 7. A main option exim_version to override the version Exim
+ reports in verious places ($exim_version, $version_number).
+
+ 8. Expansion operator ${sha2_N:} for N=256, 384, 512.
+
+ 9. Router variables, $r_... settable from router options and usable in routers
+ and transports.
+
+10. The spf lookup now supports IPv6.
+
+11. Main options for DKIM verify to filter hash and key types.
+
+12. With TLS1.3, support for full-chain OCSP stapling.
+
+13. Dual-certificate stacks on servers now support OCSP stapling, under OpenSSL.
+
+14: An smtp:ehlo transport event, for observability of the remote offered features.
+
+15: Support under OpenSSL for writing NSS-style key files for packet-capture
+ decode. The environment variable SSLKEYLOGFILE is used; if an absolute path
+ it must indicate a file under the spool directory; if relative the the spool
+ directory is prepended. Works on the server side only. Support under
+ GnuTLS was already there, being done purely by the library (server side
+ only, and exim must be run as root).
+
+16: Command-line option to move messages from one named queue to another.
+
+17. Variables $tls_in_ver, $tls_out_ver.
+
+
+Version 4.92
+--------------
+
+ 1. ${l_header:<name>} and ${l_h:<name>} expansion items, giving a colon-sep
+ list when there are multiple headers having a given name. This matters
+ when individual headers are wrapped onto multiple lines; with previous
+ facilities hard to parse.
+
+ 2. The ${readsocket } expansion item now takes a "tls" option, doing the
+ obvious thing.
+
+ 3. EXPERIMENTAL_REQUIRETLS and EXPERIMENTAL_PIPE_CONNECT optional build
+ features. See the experimental.spec file.
+
+ 4. If built with SUPPORT_I18N a "utf8_downconvert" option on the smtp transport.
+
+ 5. A "pipelining" log_selector.
+
+ 6. Builtin macros for supported log_selector and openssl_options values.
+
+ 7. JSON variants of the ${extract } expansion item.
+
+ 8. A "noutf8" debug option, for disabling the UTF-8 characters in debug output.
+
+ 9. TCP Fast Open support on MacOS.
+
+Version 4.91
+--------------
+
+ 1. Dual-certificate stacks on servers now support OCSP stapling, under GnuTLS
+ version 3.5.6 or later.
+
+ 2. DANE is now supported under GnuTLS version 3.0.0 or later. Both GnuTLS and
+ OpenSSL versions are moved to mainline support from Experimental.
+ New SMTP transport option "dane_require_tls_ciphers".
+
+ 3. Feature macros for the compiled-in set of malware scanner interfaces.
+
+ 4. SPF support is promoted from Experimental to mainline status. The template
+ src/EDITME makefile does not enable its inclusion.
+
+ 5. Logging control for DKIM verification. The existing DKIM log line is
+ controlled by a "dkim_verbose" selector which is _not_ enabled by default.
+ A new tag "DKIM=<domain>" is added to <= lines by default, controlled by
+ a "dkim" log_selector.
+
+ 6. Receive duration on <= lines, under a new log_selector "receive_time".
+
+ 7. Options "ipv4_only" and "ipv4_prefer" on the dnslookup router and on
+ routing rules in the manualroute router.
+
+ 8. Expansion item ${sha3:<string>} / ${sha3_<N>:<string>} now also supported
+ under OpenSSL version 1.1.1 or later.
+
+ 9. DKIM operations can now use the Ed25519 algorithm in addition to RSA, under
+ GnuTLS 3.6.0 or OpenSSL 1.1.1 or later.
+
+10. Builtin feature-macros _CRYPTO_HASH_SHA3 and _CRYPTO_SIGN_ED25519, library
+ version dependent.
+
+11. "exim -bP macro <name>" returns caller-usable status.
+
+12. Expansion item ${authresults {<machine>}} for creating an
+ Authentication-Results: header.
+
+13. EXPERIMENTAL_ARC. See the experimental.spec file.
+ See also new util/renew-opendmarc-tlds.sh script for use with DMARC/ARC.
+
+14: A dane:fail event, intended to facilitate reporting.
+
+15. "Lightweight" support for Redis Cluster. Requires redis_servers list to
+ contain all the servers in the cluster, all of which must be reachable from
+ the running exim instance. If the cluster has master/slave replication, the
+ list must contain all the master and slave servers.
+
+16. Add an option to the Avast scanner interface: "pass_unscanned". This
+ allows to treat unscanned files as clean. Files may be unscanned for
+ several reasons: decompression bombs, broken archives.
+
+
+Version 4.90
+------------
+
+ 1. PKG_CONFIG_PATH can now be set in Local/Makefile;
+ wildcards will be expanded, values are collapsed.
+
+ 2. The ${readsocket } expansion now takes an option to not shutdown the
+ connection after sending the query string. The default remains to do so.
+
+ 3. An smtp transport option "hosts_noproxy_tls" to control whether multiple
+ deliveries on a single TCP connection can maintain a TLS connection
+ open. By default disabled for all hosts, doing so saves the cost of
+ making new TLS sessions, at the cost of having to proxy the data via
+ another process. Logging is also affected.
+
+ 4. A malware connection type for the FPSCAND protocol.
+
+ 5. An option for recipient verify callouts to hold the connection open for
+ further recipients and for delivery.
+
+ 6. The reproducible build $SOURCE_DATE_EPOCH environment variable is now
+ supported.
+
+ 7. Optionally, an alternate format for spool data-files which matches the
+ wire format - meaning more efficient reception and transmission (at the
+ cost of difficulty with standard Unix tools). Only used for messages
+ received using the ESMTP CHUNKING option, and when a new main-section
+ option "spool_wireformat" (false by default) is set.
+
+ 8. New main configuration option "commandline_checks_require_admin" to
+ restrict who can use various introspection options.
+
+ 9. New option modifier "no_check" for quota and quota_filecount
+ appendfile transport.
+
+10. Variable $smtp_command_history returning a comma-sep list of recent
+ SMTP commands.
+
+11. Millisecond timetamps in logs, on log_selector "millisec". Also affects
+ log elements QT, DT and D, and timstamps in debug output.
+
+12. TCP Fast Open logging. As a server, logs when the SMTP banner was sent
+ while still in SYN_RECV state; as a client logs when the connection
+ is opened with a TFO cookie.
+
+13. DKIM support for multiple signing, by domain and/or key-selector.
+ DKIM support for multiple hashes, and for alternate-identity tags.
+ Builtin macro with default list of signed headers.
+ Better syntax for specifying oversigning.
+ The DKIM ACL can override verification status, and status is visible in
+ the data ACL.
+
+14. Exipick understands -C|--config for an alternative Exim
+ configuration file.
+
+15. TCP Fast Open used, with data-on-SYN, for client SMTP via SOCKS5 proxy,
+ for ${readsocket } expansions, and for ClamAV.
+
+16. The "-be" expansion test mode now supports macros. Macros are expanded
+ in test lines, and new macros can be defined.
+
+17. Support for server-side dual-certificate-stacks (eg. RSA + ECDSA).
+
+
+Version 4.89
+------------
+
+ 1. Allow relative config file names for ".include"
+
+ 2. A main-section config option "debug_store" to control the checks on
+ variable locations during store-reset. Normally false but can be enabled
+ when a memory corruption issue is suspected on a production system.
+
+
+Version 4.88
+------------
+
+ 1. The new perl_taintmode option allows to run the embedded perl
+ interpreter in taint mode.
+
+ 2. New log_selector: dnssec, adds a "DS" tag to acceptance and delivery lines.
+
+ 3. Speculative debugging, via a "kill" option to the "control=debug" ACL
+ modifier.
+
+ 4. New expansion item ${sha3:<string>} / ${sha3_<N>:<string>}.
+ N can be 224, 256 (default), 384, 512.
+ With GnuTLS 3.5.0 or later, only.
+
+ 5. Facility for named queues: A command-line argument can specify
+ the queue name for a queue operation, and an ACL modifier can set
+ the queue to be used for a message. A $queue_name variable gives
+ visibility.
+
+ 6. New expansion operators base32/base32d.
+
+ 7. The CHUNKING ESMTP extension from RFC 3030. May give some slight
+ performance increase and network load decrease. Main config option
+ chunking_advertise_hosts, and smtp transport option hosts_try_chunking
+ for control.
+
+ 8. LMDB lookup support, as Experimental. Patch supplied by Andrew Colin Kissa.
+
+ 9. Expansion operator escape8bit, like escape but not touching newline etc..
+
+10. Feature macros, generated from compile options. All start with "_HAVE_"
+ and go on with some roughly recognisable name. Driver macros, for
+ router, transport and authentication drivers; names starting with "_DRIVER_".
+ Option macros, for each configuration-file option; all start with "_OPT_".
+ Use the "-bP macros" command-line option to see what is present.
+
+11. Integer values for options can take a "G" multiplier.
+
+12. defer=pass option for the ACL control cutthrough_delivery, to reflect 4xx
+ returns from the target back to the initiator, rather than spooling the
+ message.
+
+13. New built-in constants available for tls_dhparam and default changed.
+
+14. If built with EXPERIMENTAL_QUEUEFILE, a queuefile transport, for writing
+ out copies of the message spool files for use by 3rd-party scanners.
+
+15. A new option on the smtp transport, hosts_try_fastopen. If the system
+ supports it (on Linux it must be enabled in the kernel by the sysadmin)
+ try to use RFC 7413 "TCP Fast Open". No data is sent on the SYN segment
+ but it permits a peer that also supports the facility to send its SMTP
+ banner immediately after the SYN,ACK segment rather then waiting for
+ another ACK - so saving up to one roundtrip time. Because it requires
+ previous communication with the peer (we save a cookie from it) this
+ will only become active on frequently-contacted destinations.
+
+16. A new syslog_pid option to suppress PID duplication in syslog lines.
+
+
+Version 4.87
+------------
+
+ 1. The ACL conditions regex and mime_regex now capture substrings
+ into numeric variables $regex1 to 9, like the "match" expansion condition.
+
+ 2. New $callout_address variable records the address used for a spam=,
+ malware= or verify= callout.
+
+ 3. Transports now take a "max_parallel" option, to limit concurrency.
+
+ 4. Expansion operators ${ipv6norm:<string>} and ${ipv6denorm:<string>}.
+ The latter expands to a 8-element colon-sep set of hex digits including
+ leading zeroes. A trailing ipv4-style dotted-decimal set is converted
+ to hex. Pure ipv4 addresses are converted to IPv4-mapped IPv6.
+ The former operator strips leading zeroes and collapses the longest
+ set of 0-groups to a double-colon.
+
+ 5. New "-bP config" support, to dump the effective configuration.
+
+ 6. New $dkim_key_length variable.
+
+ 7. New base64d and base64 expansion items (the existing str2b64 being a
+ synonym of the latter). Add support in base64 for certificates.
+
+ 8. New main configuration option "bounce_return_linesize_limit" to
+ avoid oversize bodies in bounces. The default value matches RFC
+ limits.
+
+ 9. New $initial_cwd expansion variable.
+
+
+Version 4.86
+------------
+
+ 1. Support for using the system standard CA bundle.
+
+ 2. New expansion items $config_file, $config_dir, containing the file
+ and directory name of the main configuration file. Also $exim_version.
+
+ 3. New "malware=" support for Avast.
+
+ 4. New "spam=" variant option for Rspamd.
+
+ 5. Assorted options on malware= and spam= scanners.
+
+ 6. A command-line option to write a comment into the logfile.
+
+ 7. If built with EXPERIMENTAL_SOCKS feature enabled, the smtp transport can
+ be configured to make connections via socks5 proxies.
+
+ 8. If built with EXPERIMENTAL_INTERNATIONAL, support is included for
+ the transmission of UTF-8 envelope addresses.
+
+ 9. If built with EXPERIMENTAL_INTERNATIONAL, an expansion item for a commonly
+ used encoding of Maildir folder names.
+
+10. A logging option for slow DNS lookups.
+
+11. New ${env {<variable>}} expansion.
+
+12. A non-SMTP authenticator using information from TLS client certificates.
+
+13. Main option "tls_eccurve" for selecting an Elliptic Curve for TLS.
+ Patch originally by Wolfgang Breyha.
+
+14. Main option "dns_trust_aa" for trusting your local nameserver at the
+ same level as DNSSEC.
+
+Version 4.85
+------------
+
+ 1. If built with EXPERIMENTAL_DANE feature enabled, Exim will follow the
+ DANE SMTP draft to assess a secure chain of trust of the certificate
+ used to establish the TLS connection based on a TLSA record in the
+ domain of the sender.
+
+ 2. The EXPERIMENTAL_TPDA feature has been renamed to EXPERIMENTAL_EVENT
+ and several new events have been created. The reason is because it has
+ been expanded beyond just firing events during the transport phase. Any
+ existing TPDA transport options will have to be rewritten to use a new
+ $event_name expansion variable in a condition. Refer to the
+ experimental-spec.txt for details and examples.
+
+ 3. The EXPERIMENTAL_CERTNAMES features is an enhancement to verify that
+ server certs used for TLS match the result of the MX lookup. It does
+ not use the same mechanism as DANE.
+
+
+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 spoolfile 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 negotiate
+ 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 received 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 negotiated 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 Received-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 message).
+
+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 command-line 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. The default for the dmarc_tld_file option is
+ "/etc/exim/opendmarc.tlds" and can be changed via EDITME.
+
+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 modifier "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, analogous 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
index 0000000..55b0f36
--- /dev/null
+++ b/doc/OptionLists.txt
@@ -0,0 +1,1094 @@
+LISTS OF EXIM OPTIONS
+---------------------
+
+This file contains an almost 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.95.
+
+
+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 experimental_prdr, 4.83 unless disable_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_insecure_tainted_data boolean true main 4.95 support will drop ~4.97ٍ
+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
+arc_sign string* unset smtp 4.91 with Experimental_ARC
+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 expanded from 4.94
+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_authz string* unset gsasl 4.94
+client_condition string* unset authenticators 4.68
+client_ignore_invalid_base64 boolean false plaintext 4.61
+client_name string* + cram_md5 3.10
+client_password string* unset gsasl 4.94
+client_secret string* unset cram_md5 3.10
+client_send string* unset plaintext 3.10
+ unset external (auth) 4.93
+client_username string* unset gsasl 4.94
+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
+commandline_checks_require_admin boolean false main 4.90
+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
+dane_require_tls_ciphers string* unset smtp 4.91
+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
+debug_store boolean false main 4.90
+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_timestamps integer* unset smtp 4.92
+dkim_verify_hashes string sha256:sha512:sha1 main 4.93
+dkim_verify_keytypes string ed25519:rsa main 4.93
+dkim_verify_min_keysizes string list "rsa=1024 ed25519=250" main 4.94
+dkim_verify_minimal boolean false main 4.93
+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, 4.93 mainline
+dmarc_history_file string unset main 4.82 if experimental_dmarc, 4.93 mainline
+dmarc_tld_file string unset main 4.82 if experimental_dmarc, 4.93 mainline
+dns_again_means_nonexist domain list unset main 1.89
+dns_check_names_pattern string + main 2.11
+dns_cname_loops integer 0 main 4.92 Set to 9 for older behaviour
+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_dane_ok integer -1 main 4.83
+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
+exim_version string ++ main 4.93
+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_name_extract string
+ "${if and {{match{.outlook.com\\$}{$host}} {match{$item}{\\N^250-([\\w.]+)\\s\\N}}} {$1}}"
+ smtp 4.96
+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_noproxy_tls host list "*" smtp 4.90
+hosts_override boolean false smtp 2.11
+hosts_pipe_connect host_list unset smtp 4.93 if experimental_pipe_connect
+hosts_randomize boolean false manualroute 4.00
+ false smtp 3.14
+hosts_require_auth host list unset smtp 4.00
+hosts_require_alpn host list unset main 4.95
+ smtp 4.95
+hosts_require_dane host list unset smtp 4.91 (4.85 experimental)
+hosts_require_helo host list "*" main 4.95
+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_dane host list unset smtp 4.91 (4.85 experimental)
+hosts_try_fastopen host list unset smtp 4.88
+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_linelength_limit integer 998 smtp 4.94
+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
+notifier_socket string "$spool_directory/exim_daemon_notify"
+ main 4.94
+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
+pipelining__connect_advertise_hosts host list "*" main 4.92 if experimental_pipe_connect
+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_fast_ramp boolean false main 4.95
+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 50000 main 1.60 default changed in 4.95 (was 0)
+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_param1 string* unset tls (auth) 4.86
+server_param2 string* unset tls (auth) 4.86
+server_param3 string* unset tls (auth) 4.86
+server_param2 string* unset tls (auth) 4.86 (tls-only) 4.93 (external)
+server_param3 string* unset tls (auth) 4.86 (tls-only) 4.93 (external)
+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
+set string* unset routers 4.93
+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_backlog_monitor integer 0 main 4.95
+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
+spf_guess string "v=spf1 a/24 mx/24 ptr ?all"
+ main 4.91 with SUPPORT_SPF
+spf_smtp_comment_template string* "Please see http://www.open-spf.org/Why"
+ main 4.94 with SUPPORT_SPF
+split_spool_directory boolean false main 1.70
+spool_directory string ++ main
+spool_wireformat boolean false main 4.90
+sqlite_dbfile string* unset main 4.94 with LOOKUP_SQLITE
+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_alpn string* unset main 4.95
+ smtp 4.95
+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_resumption_hosts host list* unset main 4.95
+ host list* unset smtp 4.95
+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
+utf8_downconvert integer unset smtp 4.92 if SUPPORT_I18N
+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 expanded from 4.94
+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** -SIGUSR1 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
+EXIM_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
index 0000000..a019ab8
--- /dev/null
+++ b/doc/README
@@ -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
index 0000000..d36998f
--- /dev/null
+++ b/doc/README.SIEVE
@@ -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 implementations 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/cve-2016-9663 b/doc/cve-2016-9663
new file mode 100644
index 0000000..ffff3db
--- /dev/null
+++ b/doc/cve-2016-9663
@@ -0,0 +1,95 @@
+CVE ID: CVE-2016-9963
+Date: 2016-12-15
+Credits: Bjoern Jacke <bjoern@j3e.de>
+Version(s): 4.69 -> 4.87
+Issue: If several conditions are met, Exim leaks private information
+ to a remote attacker.
+
+Conditions
+==========
+
+If *all* of the following conditions are met
+
+ Build options
+ -------------
+
+ * Exim is built with DKIM enabled (default for newer versions)
+ exim -bV | grep 'Support.*DKIM'
+
+ Runtime options
+ ---------------
+
+ * Exim uses DKIM signing (transport options dkim_private_key,
+ dkim_domain, and other)
+
+ * The dkim_private_key option names a file containing the key.
+
+ exim -bP transports | grep 'dkim_private_key = .'
+
+ * Exim uses PRDR (transport option hosts_try_prdr) (default
+ since 4.86)
+
+ exim -bP transports | grep 'hosts_try_prdr = .'
+
+ *OR*
+
+ Exim uses the LMTP protocol variant for SMTP transport.
+
+ exim -bP transports | grep 'protocol = lmtp'
+
+ Operation
+ ---------
+
+ * Exim transports a multi-recipient message
+
+ * The destination host supports PRDR
+ OR
+ the message transport uses LMTP
+
+ * One or more recipients are rejected after the DATA phase
+
+Impact
+======
+
+Exim leaks the private DKIM signing key to the log files. Additionally,
+if the build option EXPERIMENTAL_DSN_INFO=yes is used, the key material
+is included in the bounce message.
+
+Fix
+===
+
+Install a fixed Exim version:
+
+ 4.88
+ 4.87.1
+
+If you can't install one of the above versions, ask your package
+maintainer for a version containing the backported fix. On request and
+depending on our resources we will support you in backporting the fix.
+(Please note, that Exim project officially doesn't support versions
+prior the current stable version.)
+
+If you think that you MIGHT be affected, we HIGHLY recommend to create
+a new set of DKIM keys and fade out the previous DKIM key soon to make
+sure that a possibly leaked DKIM key can not be misused in the future.
+
+
+Workaround
+==========
+
+Disable PRDR in your outgoing transport(s): set hosts_try_prdr to an
+empty string.
+
+AND do not use the LMTP protocol variant of the SMTP driver.
+
+Indication
+==========
+
+You can check if you where affected already. The mainlog entries look like this:
+
+2016-12-17 09:44:33 10HmaX-0005vi-00 ** baduser@test.ex R=client T=send_to_server H=ip4.ip4.ip4.ip4 [ip4.ip4.ip4.ip4]: PRDR error after -----BEGIN RSA PRIVATE KEY-----\nMIICXQIBAAKBgQDXRFf+VhT+lCgFhhSkinZKcFNeRzjYdW8vT29Rbb3NadvTFwAd\n+cVLPFwZL8H5tUD/7JbUPqNTCPxmpgIL+V5T4tEZMorHatvvUM2qfcpQ45IfsZ+Y\ndhbIiAslHCpy4xNxIR3zylgqRUF4+Dtsaqy3a5LhwMiKCLrnzhXk1F1hxwIDAQAB\nAoGAZPokJKQQmRK6a0zn5f8lWemy0airG66KhzDF0Pafb/nWKgDCB02gpJgdw5rJ\nbO7/HI3IeqsfRdYTP7tjfmZtPiPo1mnF7D1rSRspZjOF2yXY/ky7t7c5xChRcSxf\n+69CknwjrfteY9Aj0j6o7N+2w2uvHO+AAq8BHDgXKmPo0SECQQDzQ/glyhNH9tlO\nx+3TTMwwyZUf2mYYosN3Q9NIl3Umz/3+13K5b6Ed6fZvS/XwU55Qf5IBUVj2Fujk\nRv2lbGPpAkEA4okpnzYz5nm1X5WjpJPQPyo8nGEU1A5QfoDbkAvWYvVoYrpWPOx5\nHFpOAHkvSk1Y1vhCUa+zHwiQRBC8OMp6LwJBAOAUK/AjQ792UpWO9DM++pe2F/dP\nZdwrkYG6qFSlrvQhgwXLz5GgkfjMGoRKpDDL1XixCfzMwfVtBPnBqsNGJIECQGYX\nSIGu7L7edMXJ60C9OKluwHf9LGTQuqf4LHsDSq+4Rz3PGhREwePsMqD1/EDxEKt4\noHKtyvyeYF28aQbzARMCQQCRtJlR6vlKhxYL8+xoPrCu3MijKgVruRUcNstXkDZK\nfKQax6vhiMq+0qIiEwLA1wavyLVKZ7Mfag+/4NTcDUVC\n-----END RSA PRIVATE KEY-----\n: 550 PRDR R=<baduser@test.ex> refusal
+
+Even if there is no evidence in the existing log files, that a DKIM key
+leakage happened this might have happened in the past, log files might
+have been deleted already but a key leak could have ended up via mail
+bounce in a user mail box
diff --git a/doc/cve-2019-13917 b/doc/cve-2019-13917
new file mode 100644
index 0000000..fd94da8
--- /dev/null
+++ b/doc/cve-2019-13917
@@ -0,0 +1,46 @@
+CVE ID: CVE-2019-13917
+OVE ID: OVE-20190718-0006
+Date: 2019-07-18
+Credits: Jeremy Harris
+Version(s): 4.85 up to and including 4.92
+Issue: A local or remote attacker can execute programs with root
+ privileges - if you've an unusual configuration. See below.
+
+Conditions to be vulnerable
+===========================
+
+If your configuration uses the ${sort } expansion for items that can be
+controlled by an attacker (e.g. $local_part, $domain). The default
+config, as shipped by the Exim developers, does not contain ${sort }.
+
+Details
+=======
+
+The vulnerability is exploitable either remotely or locally and could
+be used to execute other programs with root privilege. The ${sort }
+expansion re-evaluates its items.
+
+Mitigation
+==========
+
+Do not use ${sort } in your configuration.
+
+Fix
+===
+
+Download and build a fixed version:
+
+ Tarballs: http://ftp.exim.org/pub/exim/exim4/
+ Git: https://github.com/Exim/exim.git
+ - tag exim-4.92.1
+ - branch exim-4.92+fixes
+
+The tagged commit is the officially released version. The +fixes branch
+isn't officially maintained, but contains useful patches *and* the
+security fix.
+
+If you can't install the above versions, ask your package maintainer for
+a version containing the backported fix. On request and depending on our
+resources we will support you in backporting the fix. (Please note,
+that Exim project officially doesn't support versions prior the current
+stable version.)
diff --git a/doc/cve-2019-15846/cve.txt b/doc/cve-2019-15846/cve.txt
new file mode 100644
index 0000000..b52722b
--- /dev/null
+++ b/doc/cve-2019-15846/cve.txt
@@ -0,0 +1,45 @@
+CVE ID: CVE-2019-15846
+Date: 2019-09-02 (CVE assigned)
+Credits: Zerons <sironhide0null@gmail.com> for the initial report
+ Qualys https://www.qualys.com/ for the analysis
+Version(s): all versions up to and including 4.92.1
+Issue: A local or remote attacker can execute programs with root
+ privileges.
+
+Conditions to be vulnerable
+===========================
+
+If your Exim server accepts TLS connections, it is vulnerable. This does
+not depend on the TLS libray, so both, GnuTLS and OpenSSL are affected.
+
+Details
+=======
+
+The vulnerability is exploitable by sending a SNI ending in a
+backslash-null sequence during the initial TLS handshake. The exploit
+exists as a POC. For more details see the document qualys.mbx
+
+Mitigation
+==========
+
+Do not offer TLS. (This mitigation is not recommended.)
+
+Fix
+===
+
+Download and build a fixed version:
+
+ Tarballs: https://ftp.exim.org/pub/exim/exim4/
+ Git: https://github.com/Exim/exim.git
+ - tag exim-4.92.2
+ - branch exim-4.92.2+fixes
+
+The tagged commit is the officially released version. The +fixes branch
+isn't officially maintained, but contains the security fix *and* useful
+fixes.
+
+If you can't install the above versions, ask your package maintainer for
+a version containing the backported fix. On request and depending on our
+resources we will support you in backporting the fix. (Please note,
+the Exim project officially doesn't support versions prior the current
+stable version.)
diff --git a/doc/cve-2019-15846/mitre.mbx b/doc/cve-2019-15846/mitre.mbx
new file mode 100644
index 0000000..ddd6f9c
--- /dev/null
+++ b/doc/cve-2019-15846/mitre.mbx
@@ -0,0 +1,84 @@
+From cve-request@mitre.org Mon Sep 2 18:12:21 2019
+Return-Path: <cve-request@mitre.org>
+Authentication-Results: mx.net.schlittermann.de; iprev=pass
+ (smtpvbsrv1.mitre.org) smtp.remote-ip=198.49.146.234; spf=pass
+ smtp.mailfrom=mitre.org; dkim=pass header.d=mitre.org header.s=selector1
+ header.a=rsa-sha256; dmarc=pass header.from=mitre.org
+From: cve-request@mitre.org
+To: hs@schlittermann.de
+Cc: cve-request@mitre.org
+Subject: Re: [scr749683] one CVE
+Date: Mon, 2 Sep 2019 12:12:12 -0400 (EDT)
+MIME-Version: 1.0
+Content-Transfer-Encoding: 8bit
+Content-Type: text/plain; charset=utf-8
+Status: RO
+
+> [Suggested description]
+> The SMTP Delivery process in Exim 4.92.1 has a Buffer Overflow.
+> In the default runtime configuration, this is exploitable with crafted
+> Server Name Indication (SNI) data during a TLS negotiation. In other
+> configurations, it is exploitable with a crafted client TLS certificate.
+>
+> ------------------------------------------
+>
+> [Additional Information]
+> It's the first CVE I request, so if there is anything missing, please tell me
+>
+> ------------------------------------------
+>
+> [Vulnerability Type]
+> Buffer Overflow
+>
+> ------------------------------------------
+>
+> [Vendor of Product]
+> Exim Development Team
+>
+> ------------------------------------------
+>
+> [Affected Product Code Base]
+> Exim - 4.92.1
+>
+> ------------------------------------------
+>
+> [Affected Component]
+> SMTP Delivery process
+>
+> ------------------------------------------
+>
+> [Attack Type]
+> Remote
+>
+> ------------------------------------------
+>
+> [Impact Code execution]
+> true
+>
+> ------------------------------------------
+>
+> [Attack Vectors]
+> To exploit the vulnerability the attacker needs a crafted client TLS
+> certificate or a crafted SNI. While the first attack vector needs a
+> non-default runtime configuration, the latter one should work with the
+> default runtime config.
+>
+> ------------------------------------------
+>
+> [Discoverer]
+> zerons zerons <sironhide0null@gmail.com>
+>
+> ------------------------------------------
+>
+> [Reference]
+> http://exim.org/static/doc/security/CVE-2019-15846.txt
+
+Use CVE-2019-15846.
+
+
+--
+CVE Assignment Team
+M/S M300, 202 Burlington Road, Bedford, MA 01730 USA
+[ A PGP key is available for encrypted communications at
+ http://cve.mitre.org/cve/request_id.html ]
+
diff --git a/doc/cve-2019-15846/posting-0.txt b/doc/cve-2019-15846/posting-0.txt
new file mode 100644
index 0000000..90d754d
--- /dev/null
+++ b/doc/cve-2019-15846/posting-0.txt
@@ -0,0 +1,59 @@
+To: distros@vs.openwall.org, exim-maintainers@exim.org
+From: [ do not use a dmarc protected sender ]
+
+** EMBARGO *** This information is not public yet.
+
+CVE ID: CVE-2019-15846
+Credits: Zerons <sironhide0null@gmail.com>, Qualys
+Version(s): all versions up to and including 4.92.1
+Issue: The SMTP Delivery process in all versions up to and
+ including Exim 4.92.1 has a Buffer Overflow. In the default
+ runtime configuration, this is exploitable with crafted Server
+ Name Indication (SNI) data during a TLS negotiation. In other
+ configurations, it is exploitable with a crafted client TLS certificate.
+Details: doc/doc-txt/cve-2019-15846 in the downloaded source tree
+
+Contact: security@exim.org
+
+Proposed Timeline
+=================
+
+2019-09-03:
+ - This notice to distros@vs.openwall.org and exim-maintainers@exim.org
+ - Open limited access to our security Git repo. See below.
+
+2019-09-04:
+ - Heads-up notice to oss-security@lists.openwall.com,
+ exim-users@exim.org, and exim-announce@exim.org
+ about the upcoming security release
+
+2019-09-06 10:00 UTC:
+ - Coordinated relase date
+ - Publish the patches in our official and public Git repositories
+ and the packages on our FTP/HTTP(S) server.
+
+Downloads
+=========
+
+The downloads mentioned below are accessible only for a limited set of SSH
+keys. At CRD they will be mirrored to the public repositories.
+(Note: the repo names changed from the recently used ones.)
+
+For release tarballs (exim-4.92.2):
+
+ git clone --depth 1 ssh://git@git.exim.org/exim-packages-security
+
+The package files are signed with my GPG key.
+
+For the full Git repo:
+
+ git clone ssh://git@exim.org/exim-security
+ - tag exim-4.92.2
+ - branch exim-4.92.2+fixes
+
+The tagged commit is the officially maintained version. The tag is signed
+with my GPG key. The +fixes branch isn't officially maintained, but
+contains useful patches *and* the security fix. The relevant commit
+is signed with my GPG key.
+
+If you need help backporting the patch, please contact us directly.
diff --git a/doc/cve-2019-15846/posting-1.txt b/doc/cve-2019-15846/posting-1.txt
new file mode 100644
index 0000000..d22b85c
--- /dev/null
+++ b/doc/cve-2019-15846/posting-1.txt
@@ -0,0 +1,59 @@
+To: oss-security@lists.openwall.com, exim-users@exim.org,
+ exim-announce@exim.org
+From: [ do not use a dmarc protected sender ]
+
+*** Note: EMBARGO is still in effect ***
+*** Distros must not publish any detail yet ***
+
+Head up! Security release ahead!
+
+CVE ID: CVE-2019-15846
+Version(s): up to and including 4.92.1
+Issue: A local or remote attacker can execute programs with root
+ privileges.
+Details: Will be made public at CRD.
+
+Coordinated Release Date (CRD) for Exim 4.92.2: 2019-09-06 10:00 UTC
+
+Contact: security@exim.org
+
+Proposed Timeline
+=================
+
+2019-09-03:
+ - initial notification to distros@openwall.org and
+ exim-maintainers@exim.org
+
+2019-09-04: <-- NOW
+ - This Heads-up notice to oss-security@lists.openwall.com,
+ exim-users@exim.org, and exim-announce@exim.org
+
+2019-09-06 10:00 UTC:
+ - Coordinated relase date
+ - Publish the patches in our official and public Git repositories
+ and the packages on our FTP server.
+
+Downloads available starting at CRD
+====================================
+
+The downloads are not yet available. They will be made available
+at the above mentioned CRD.
+
+Release tarballs (exim-4.92.2):
+
+ https://ftp.exim.org/pub/exim/exim4/
+
+The package files are signed with my GPG key.
+
+The full Git repo:
+
+ https://git.exim.org/exim.git
+ https://github.com/Exim/exim [mirror of the above]
+ - tag exim-4.92.2
+ - branch exim-4.92.2+fixes
+
+The tagged commit is the officially released version. The tag is signed
+with my GPG key. The +fixes branch isn't officially maintained, but
+contains useful patches *and* the security fix. The relevant commit is
+signed with my GPG key. The old exim-4.92.1+fixes branch is being functionally
+replaced by the new exim-4.92.2+fixes branch.
diff --git a/doc/cve-2019-15846/posting-2.txt b/doc/cve-2019-15846/posting-2.txt
new file mode 100644
index 0000000..20037dd
--- /dev/null
+++ b/doc/cve-2019-15846/posting-2.txt
@@ -0,0 +1,44 @@
+To: exim-users@exim.org, exim-announce@exim.org, exim-maintainers@exim.org
+From: [ do not use a dmarc protected sender ]
+
+CVE ID: CVE-2019-15846
+Credits: Zerons <sironhide0null@gmail.com>, Qualys
+Version(s): all versions up to and including 4.92.1
+Issue: The SMTP Delivery process in all versions up to and
+ including Exim 4.92.1 has a Buffer Overflow. In the default
+ runtime configuration, this is exploitable with crafted Server
+ Name Indication (SNI) data during a TLS negotiation. In other
+ configurations, it is exploitable with a crafted client TLS certificate.
+Details: doc/doc-txt/cve-2019-15846 in the downloaded source tree
+
+Coordinated Release Date (CRD) for Exim 4.92.2:
+ 2019-09-06 10:00 UTC
+
+Contact: security@exim.org
+
+We released Exim 4.92.2. This is a security update based on 4.92.1.
+
+Downloads
+=========
+
+Starting at CRD the downloads will be available from the following
+sources:
+
+Release tarballs (exim-4.92.2):
+
+ https://ftp.exim.org/pub/exim/exim4/
+
+The package files are signed with my GPG key.
+
+The full Git repo:
+
+ https://git.exim.org/exim.git
+ https://github.com/Exim/exim [mirror of the above]
+ - tag exim-4.92.2
+ - branch exim-4.92.2+fixes
+
+The tagged commit is the officially released version. The tag is signed
+with my GPG key. The +fixes branch isn't officially maintained, but
+contains useful patches *and* the security fix. The relevant commit is
+signed with my GPG key. The old exim-4.92.1+fixes branch is being functionally
+replaced by the new exim-4.92.2+fixes branch.
diff --git a/doc/cve-2019-15846/qualys.mbx b/doc/cve-2019-15846/qualys.mbx
new file mode 100644
index 0000000..66c1e8e
--- /dev/null
+++ b/doc/cve-2019-15846/qualys.mbx
@@ -0,0 +1,175 @@
+From qsa@qualys.com Wed Aug 14 01:29:25 CEST 2019
+Date: Tue, 13 Aug 2019 23:29:25 +0000
+From: Qualys Security Advisory <qsa@qualys.com>
+To: Heiko Schlittermann <hs@schlittermann.de>
+Subject: Re: Help evaluating a Bug in Exim MTA
+Return-Path: <qsa@qualys.com>
+Authentication-Results: mx.net.schlittermann.de; iprev=pass
+ (mx0b-001ca501.pphosted.com) smtp.remote-ip=148.163.158.195; spf=pass
+ smtp.mailfrom=qualys.com; dkim=pass header.d=qualys.com header.s=qualyscom
+ header.a=rsa-sha256; dkim=pass header.d=qualys.onmicrosoft.com
+ header.s=selector2-qualys-onmicrosoft-com header.a=rsa-sha256; dmarc=none
+ header.from=qualys.com
+Authentication-Results: ppops.net; spf=pass smtp.mailfrom=qsa@qualys.com
+Status: O
+Content-Length: 3899
+Lines: 80
+
+Hi Heiko,
+
+On Mon, Aug 12, 2019 at 11:56:12PM +0200, Heiko Schlittermann wrote:
+> So I'd say, you do not need to rush, but I'd like to close it sooner or
+> later in either manner.
+
+OK, below is our preliminary analysis. First:
+
+- From an attacker's point of view, most calls to
+ string_interpret_escape() are uninteresting. For example, nextitem()
+ in src/filter.c checks for buffer overflows, and string_dequote()
+ seems to process trusted strings only (strings from configuration
+ files).
+
+- On the other hand, string_unprinting() is very interesting:
+
+ - It is used in tls_import_cert() (for peercert, for example); but
+ certificates are in PEM format (i.e., base64) and hence unlikely to
+ contain the problematic backslash-null-byte sequence.
+
+ - It is used for peerdn and sni in src/spool_in.c; but peerdn is used
+ only if client certificates are processed by Exim, and this is not
+ the default (and although some sites use client-certificate
+ authentication, this is not very common, and hence not very
+ interesting for an attacker).
+
+ - In any case, as long as Exim supports and accepts tls connections,
+ an attacker can send an sni, and hence reach the problematic
+ string_unprinting() and string_interpret_escape() functions.
+
+Next question: is it possible to send an sni that is written to the
+spool header file and that ends with the problematic backslash-null-byte
+sequence? The answer is yes, because of what we believe is another bug,
+in string_printing(): the sni is written to the spool header file via
+string_printing(tls_in.sni), which escapes characters with backslash,
+but does *not* escape the escaping character itself (backslash),
+although it definitely should.
+
+This bug is what makes it possible to reach and trigger the bug in
+string_unprinting() and string_interpret_escape(), with an sni that ends
+in an unescaped backslash (followed by the terminating null byte).
+
+Last question: is this exploitable? The answer is, almost certainly, yes
+(and, because spool_read_header() runs as root, this means remote root).
+
+The sni is read from the spool via string_unprinting(string_copy()), and
+both string_unprinting() and string_copy() use store_get(): as a result,
+the destination buffer is allocated right after the source buffer, and
+the characters that are read out-of-bounds after the end of the source
+buffer are the first characters of the destination buffer, which are
+fully under the attacker's control. This results in a heap overflow
+whose length and contents are both under the attacker's control (we
+verified this). This is almost certainly exploitable.
+
+Our advice is to start the security-release process as soon as possible.
+We know it is very painful, but we are really confident that this bug is
+exploitable; we will try to confirm this in the next few days. We also
+believe that an Exim server must support and accept tls connections to
+be remotely exploitable (via sni).
+
+During our analysis of this bug, we probably spotted three other bugs:
+
+- The unescaped backslash in string_printing() that we mentioned above.
+
+- A bug in spool_read_header(): before the for (;;) loop, p is set to
+ big_buffer + 2; and inside the loop, big_buffer may be re-allocated;
+ but p is never updated. This can lead to a use-after-free (we did not
+ assess the security impact of this bug, though).
+
+- A bug in spool_write_header(): the return value of tls_export_cert()
+ is not checked (for ourcert, but more importantly, for peercert). If
+ this function fails (maybe because big_buffer is not big enough), then
+ big_buffer may be uninitialized or unterminated, and garbage may be
+ written to the spool file (we did not assess the security impact of
+ this bug, either).
+
+We are at your disposal for questions, comments, and further
+discussions. Thank you very much for reaching out! With best regards,
+
+--
+the Qualys Security Advisory team
+From qsa@qualys.com Mon Aug 19 00:23:03 CEST 2019
+Date: Sun, 18 Aug 2019 22:23:03 +0000
+From: Qualys Security Advisory <qsa@qualys.com>
+To: Heiko Schlittermann <hs@schlittermann.de>
+Subject: Re: Help evaluating a Bug in Exim MTA
+Return-Path: <qsa@qualys.com>
+Authentication-Results: mx.net.schlittermann.de; iprev=pass
+ (mx0a-001ca501.pphosted.com) smtp.remote-ip=148.163.156.198; spf=pass
+ smtp.mailfrom=qualys.com; dkim=pass header.d=qualys.com header.s=qualyscom
+ header.a=rsa-sha256; dkim=pass header.d=qualys.onmicrosoft.com
+ header.s=selector2-qualys-onmicrosoft-com header.a=rsa-sha256; dmarc=none
+ header.from=qualys.com
+Authentication-Results: ppops.net; spf=pass smtp.mailfrom=qsa@qualys.com
+Status: RO
+Content-Length: 2484
+Lines: 59
+
+Hi Heiko,
+
+On Tue, Aug 13, 2019 at 11:29:25PM +0000, Qualys Security Advisory wrote:
+> we are really confident that this bug is exploitable
+
+We can confirm that this bug is indeed exploitable: we wrote a
+rudimentary exploit that remotely obtains root privileges (because
+deliver_message() runs as root).
+
+Some general notes on this exploit:
+
+- To the best of our knowledge, the string_interpret_escape() bug
+ (backslash-null) is remotely exploitable if and only if Exim supports
+ and accepts TLS connections (because the only attack vector that we
+ know of is the string_unprinting() of SNI).
+
+- Both OpenSSL and GnuTLS installations are exploitable.
+
+- Our exploit is Linux-specific (because our heap-overflow exploitation
+ is specific to glibc's malloc implementation), but works on both i386
+ and amd64.
+
+Some detailed notes on this exploit:
+
+- First, we connect to Exim with TLS and send an SNI that ends with
+ backslash-null (this SNI is written unmodified to the spool because of
+ the unescaped-backslash bug in string_printing2()).
+
+- Second, we exploit the backslash-null bug in string_interpret_escape()
+ (our SNI is read from the spool and unescaped by string_unprinting()),
+ and we transform this out-of-bounds read into an out-of-bounds write
+ (a heap overflow).
+
+- Next, we use this heap overflow to overwrite the header of a free
+ malloc chunk, and increase its size to make it overlap with other,
+ already-allocated malloc chunks.
+
+- Last, we allocate this enlarged malloc chunk, and use it to overwrite
+ large parts of the heap (the already-allocated malloc chunks) with
+ arbitrary data:
+
+ . we overwrite the "id" string: it is used to build the message-log
+ file name, and therefore allows us to write to "/etc/passwd" (by
+ overwriting "id" with "/../../../../../../../../etc/passwd");
+
+ . we overwrite the "sender_address" string: it is written to the
+ message-log file, and therefore allows us to add a new user to
+ "/etc/passwd".
+
+Other exploitation methods may exist. We will not publish our exploit:
+it is a quick and dirty proof of concept, and we will not have the time
+to clean it anytime soon. However, please feel free to quote us on the
+exploitability of this bug (we do have a working exploit), and please
+feel free to quote all or parts of this email in your announcements.
+
+We are at your disposal for questions, comments, and further
+discussions. Thank you very much! With best regards,
+
+--
+the Qualys Security Advisory team
diff --git a/doc/cve-2020-qualys b/doc/cve-2020-qualys
new file mode 100644
index 0000000..d571644
--- /dev/null
+++ b/doc/cve-2020-qualys
@@ -0,0 +1,2 @@
+For the vulnerabilites reported by Qualys in October 2020 please see the
+Exim Website: https://exim.org/static/doc/security/CVE-2020-qualys/
diff --git a/doc/dbm.discuss.txt b/doc/dbm.discuss.txt
new file mode 100644
index 0000000..50f0687
--- /dev/null
+++ b/doc/dbm.discuss.txt
@@ -0,0 +1,321 @@
+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. In 2022 it was 5.3.28 on Linux (the then-owner
+has developed it further but Exim does not support anything after 5.x).
+
+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
+performance 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 development 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
index 0000000..4b41796
--- /dev/null
+++ b/doc/exim.8
@@ -0,0 +1,1810 @@
+.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 runtime 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$message_exim_id\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.
+.sp
+Macro processing is done on lines before string\-expansion: new macros can be
+defined and macros will be expanded.
+Because macros in the config file are often used for secrets, those are only
+available to admin users.
+.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 or directory
+(depending on the used scanner interface),
+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 \fBconfig\fP is given as an argument, the config is
+output, as it was parsed, any include file resolved, any comment removed.
+.sp
+If \fBconfig_file\fP is given as an argument, the name of the runtime
+configuration file is output. (\fBconfigure_file\fP works too, for
+backward compatibility.)
+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 \fBenvironment\fP is given as an argument, the set of environment
+variables is output, line by line. Using the \fB\-n\fP flag suppresses the value of the
+variables.
+.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.
+For the "\-bP macro <name>" form, if no such macro is found
+the exit status will be nonzero.
+.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 in 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 in 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 in 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 in 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 runtime 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 runtime 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 filename,
+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
+in 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 filename 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 filename 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.
+Only macro names up to 22 letters long can be set.
+.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:
+ 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
+ noutf8
+modifier: avoid UTF\-8 line\-drawing
+ pid
+modifier: 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
+modifier: 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
+The noutf8 selector disables the use of
+UTF\-8 line\-drawing characters to group related information.
+When disabled. ascii\-art is used instead.
+Using the +all option does not set this modifier,
+.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.
+Solaris 2.4 (SunOS 5.4) Sendmail has a similar \fB\-i\fP processing option
+\fBhttps://docs.oracle.com/cd/E19457\-01/801\-6680\-1M/801\-6680\-1M.pdf\fP,
+p. 1M\-529), and therefore a \fB\-oi\fP command line option, which both are used
+by its \fImailx\fP command.
+.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> <\fIhost IP\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\-MCD\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
+remote host supports the ESMTP DSN extension.
+.TP 10
+\fB\-MCd\fP
+This option is not intended for use by external callers. It is used internally
+by Exim in conjunction with the \fB\-d\fP option
+to pass on an information string on the purpose of the process.
+.TP 10
+\fB\-MCG\fP <\fIqueue name\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 an
+alternate queue is used, named by the following argument.
+.TP 10
+\fB\-MCK\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 a
+remote host supports the ESMTP CHUNKING extension.
+.TP 10
+\fB\-MCL\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 advertised limits on numbers of mails, recipients or
+recipient domains.
+The limits are given by the following three arguments.
+.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\-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 connection
+t a remote server is via a SOCKS proxy, using addresses and ports given by
+the following four arguments.
+.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\-MCq\fP <\fIrecipient address\fP> <\fIsize\fP>
+This option is not intended for use by external callers. It is used internally
+by Exim to implement quota checking for local users.
+.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
+ESMTP 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\-MCr\fP <\fISNI\fP>
+\fB\-MCs\fP <\fISNI\fP>
+These options are not intended for use by external callers. It is used internally
+by Exim in conjunction with the \fB\-MCt\fP option, and passes on the fact that
+a TLS Server Name Indication was sent as part of the channel establishment.
+The argument gives the SNI string.
+The "r" variant indicates a DANE\-verified connection.
+.TP 10
+\fB\-MCt\fP <\fIIP address\fP> <\fIport\fP> <\fIcipher\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
+connection is being proxied by a parent process for handling TLS encryption.
+The arguments give the local address and port being proxied, and the TLS cipher.
+.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\-MG\fP <\fIqueue name\fP> <\fImessage id\fP> <\fImessage id\fP> ...
+This option requests that each listed message be moved from its current
+queue to the given named queue.
+The destination queue name argument is required, but can be an empty
+string to define the default queue.
+If the messages are not currently located in the default queue,
+a \fB\-qG<name>\fP option will be required to define the source queue.
+.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 in 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 a synonym for \fB\-om\fP that is accepted by Sendmail
+(\fBhttps://docs.oracle.com/cd/E19457\-01/801\-6680\-1M/801\-6680\-1M.pdf\fP
+p. 1M\-258), 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 makes the output more terse (suppresses
+option names, environment values and config pretty printing).
+.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 filename. 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 in 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 in 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 in 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. Repeated use of this option is not supported.
+.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\-oPX\fP
+This option is not intended for general use.
+The daemon uses it when terminating due to a SIGTEM, possibly in
+combination with \fB\-oP\fP <\fIpath\fP>.
+It causes the pid file to be removed.
+.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 filename.
+.TP 10
+\fB\-oY\fP
+This option controls the creation of an inter\-process communications endpoint
+by the Exim daemon.
+It is only relevant when the \fB\-bd\fP (start listening daemon) option is also
+given.
+Normally the daemon creates this socket, unless a \fB\-oX\fP and \fBno\fP \fB\-oP\fP
+option is also present.
+If this option is given then the socket will not be created. This could be
+required if the system is running multiple daemons.
+.sp
+The socket is currently used for
+.sp
+fast ramp\-up of queue runner processes
+.sp
+obtaining a current queue size
+.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).
+Repeated use of this option is not supported.
+.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
+If other commandline options do not specify an action,
+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
+Performance will be best if the \fBqueue_run_in_order\fP option is false.
+If that is so and the \fBqueue_fast_ramp\fP option is true then
+in the first phase of the run,
+once a threshold number of messages are routed for a given host,
+a delivery process is forked in parallel with the rest of the scan.
+.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 in 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 in the queue
+for later delivery.
+.TP 10
+\fB\-q[q][i][f[f]][l][G<name>[/<time>]]]\fP
+If the \fIG\fP flag and a name is present, the queue runner operates on the
+queue with the given name rather than the default queue.
+The name should not contain a \fI/\fP character.
+For a periodic queue run (see below)
+append to the name a slash and a time value.
+.sp
+If other commandline options specify an action, a \fI\-qG<name>\fP option
+will specify a queue to operate on.
+For example:
+.sp
+ exim \-bp \-qGquarantine
+ mailq \-qGquarantine
+ exim \-qGoffpeak \-Rf @special.domain.example
+.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.
+.TP 10
+\fB\-z\fP <\fIlog\-line\fP>
+This option writes its argument to Exim's logfile.
+Use is restricted to administrators; the intent is for operational notes.
+Quotes should be used to maintain a multi\-word item as a single argument,
+under most shells.
+.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
index 0000000..dbd57d6
--- /dev/null
+++ b/doc/experimental-spec.txt
@@ -0,0 +1,665 @@
+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) support
+--------------------------------------------------------------
+
+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 Brightmail 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.
+
+
+
+
+DCC Support
+--------------------------------------------------------------
+Distributed Checksum Clearinghouse; http://www.rhyolite.com/dcc/
+
+*) 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 actually uses.
+
+
+
+DSN extra information
+---------------------
+If compiled with EXPERIMENTAL_DSN_INFO extra information will be added
+to DSN fail messages ("bounces"), when available. The intent is to aid
+tracing of specific failing messages, when presented with a "bounce"
+complaint and needing to search logs.
+
+
+The remote MTA IP address, with port number if nonstandard.
+Example:
+ Remote-MTA: X-ip; [127.0.0.1]:587
+Rationale:
+ Several addresses may correspond to the (already available)
+ dns name for the remote MTA.
+
+The remote MTA connect-time greeting.
+Example:
+ X-Remote-MTA-smtp-greeting: X-str; 220 the.local.host.name ESMTP Exim x.yz Tue, 2 Mar 1999 09:44:33 +0000
+Rationale:
+ This string sometimes presents the remote MTA's idea of its
+ own name, and sometimes identifies the MTA software.
+
+The remote MTA response to HELO or EHLO.
+Example:
+ X-Remote-MTA-helo-response: X-str; 250-the.local.host.name Hello localhost [127.0.0.1]
+Limitations:
+ Only the first line of a multiline response is recorded.
+Rationale:
+ This string sometimes presents the remote MTA's view of
+ the peer IP connecting to it.
+
+The reporting MTA detailed diagnostic.
+Example:
+ X-Exim-Diagnostic: X-str; SMTP error from remote mail server after RCPT TO:<d3@myhost.test.ex>: 550 hard error
+Rationale:
+ This string sometimes give extra information over the
+ existing (already available) Diagnostic-Code field.
+
+
+Note that non-RFC-documented field names and data types are used.
+
+
+Queuefile transport
+-------------------
+Queuefile is a pseudo transport which does not perform final delivery.
+It simply copies the exim spool files out of the spool directory into
+an external directory retaining the exim spool format.
+
+The spool files can then be processed by external processes and then
+requeued into exim spool directories for final delivery.
+However, note carefully the warnings in the main documentation on
+qpool file formats.
+
+The motivation/inspiration for the transport is to allow external
+processes to access email queued by exim and have access to all the
+information which would not be available if the messages were delivered
+to the process in the standard email formats.
+
+The mailscanner package is one of the processes that can take advantage
+of this transport to filter email.
+
+The transport can be used in the same way as the other existing transports,
+i.e by configuring a router to route mail to a transport configured with
+the queuefile driver.
+
+The transport only takes one option:
+
+* directory - This is used to specify the directory messages should be
+copied to. Expanded.
+
+The generic transport options (body_only, current_directory, disable_logging,
+debug_print, delivery_date_add, envelope_to_add, event_action, group,
+headers_add, headers_only, headers_remove, headers_rewrite, home_directory,
+initgroups, max_parallel, message_size_limit, rcpt_include_affixes,
+retry_use_local_part, return_path, return_path_add, shadow_condition,
+shadow_transport, transport_filter, transport_filter_timeout, user) are
+ignored.
+
+Sample configuration:
+
+(Router)
+
+scan:
+ driver = accept
+ transport = scan
+
+(Transport)
+
+scan:
+ driver = queuefile
+ directory = /var/spool/baruwa-scanner/input
+
+
+In order to build exim with Queuefile transport support add or uncomment
+
+EXPERIMENTAL_QUEUEFILE=yes
+
+to your Local/Makefile. (Re-)build/install exim. exim -d should show
+Experimental_QUEUEFILE in the line "Support for:".
+
+
+ARC support
+-----------
+Specification: https://tools.ietf.org/html/draft-ietf-dmarc-arc-protocol-11
+Note that this is not an RFC yet, so may change.
+
+[RFC 8617 was published 2019/06. Draft 11 was 2018/01. A review of the
+changes has not yet been done]
+
+ARC is intended to support the utility of SPF and DKIM in the presence of
+intermediaries in the transmission path - forwarders and mailinglists -
+by establishing a cryptographically-signed chain in headers.
+
+Normally one would only bother doing ARC-signing when functioning as
+an intermediary. One might do verify for local destinations.
+
+ARC uses the notion of a "ADministrative Management Domain" (ADMD).
+Described in RFC 5598 (section 2.3), this is essentially a set of
+mail-handling systems that mail transits that are all under the control
+of one organisation. A label should be chosen to identify the ADMD.
+Messages should be ARC-verified on entry to the ADMD, and ARC-signed on exit
+from it.
+
+
+Building with ARC Support
+--
+Enable using EXPERIMENTAL_ARC=yes in your Local/Makefile.
+You must also have DKIM present (not disabled), and you very likely
+want to have SPF enabled.
+
+
+Verification
+--
+An ACL condition is provided to perform the "verifier actions" detailed
+in section 6 of the above specification. It may be called from the DATA ACL
+and succeeds if the result matches any of a given list.
+It also records the highest ARC instance number (the chain size)
+and verification result for later use in creating an Authentication-Results:
+standard header.
+
+ verify = arc/<acceptable_list> none:fail:pass
+
+ add_header = :at_start:${authresults {<admd-identifier>}}
+
+ Note that it would be wise to strip incoming messages of A-R headers
+ that claim to be from our own <admd-identifier>.
+
+There are four new variables:
+
+ $arc_state One of pass, fail, none
+ $arc_state_reason (if fail, why)
+ $arc_domains colon-sep list of ARC chain domains, in chain order.
+ problematic elements may have empty list elements
+ $arc_oldest_pass lowest passing instance number of chain
+
+Example:
+ logwrite = oldest-p-ams: <${reduce {$lh_ARC-Authentication-Results:} \
+ {} \
+ {${if = {$arc_oldest_pass} \
+ {${extract {i}{${extract {1}{;}{$item}}}}} \
+ {$item} {$value}}} \
+ }>
+
+Receive log lines for an ARC pass will be tagged "ARC".
+
+
+Signing
+--
+arc_sign = <admd-identifier> : <selector> : <privkey> [ : <options> ]
+An option on the smtp transport, which constructs and prepends to the message
+an ARC set of headers. The textually-first Authentication-Results: header
+is used as a basis (you must have added one on entry to the ADMD).
+Expanded as a whole; if unset, empty or forced-failure then no signing is done.
+If it is set, all of the first three elements must be non-empty.
+
+The fourth element is optional, and if present consists of a comma-separated list
+of options. The options implemented are
+
+ timestamps Add a t= tag to the generated AMS and AS headers, with the
+ current time.
+ expire[=<val>] Add an x= tag to the generated AMS header, with an expiry time.
+ If the value <val> is an plain number it is used unchanged.
+ If it starts with a '+' then the following number is added
+ to the current time, as an offset in seconds.
+ If a value is not given it defaults to a one month offset.
+
+[As of writing, gmail insist that a t= tag on the AS is mandatory]
+
+Caveats:
+ * There must be an Authentication-Results header, presumably added by an ACL
+ while receiving the message, for the same ADMD, for arc_sign to succeed.
+ This requires careful coordination between inbound and outbound logic.
+
+ Only one A-R header is taken account of. This is a limitation versus
+ the ARC spec (which says that all A-R headers from within the ADMD must
+ be used).
+
+ * If passing a message to another system, such as a mailing-list manager
+ (MLM), between receipt and sending, be wary of manipulations to headers made
+ by the MLM.
+ + For instance, Mailman with REMOVE_DKIM_HEADERS==3 might improve
+ deliverability in a pre-ARC world, but that option also renames the
+ Authentication-Results header, which breaks signing.
+
+ * Even if you use multiple DKIM keys for different domains, the ARC concept
+ should try to stick to one ADMD, so pick a primary domain and use that for
+ AR headers and outbound signing.
+
+Signing is not compatible with cutthrough delivery; any (before expansion)
+value set for the option will result in cutthrough delivery not being
+used via the transport in question.
+
+
+
+Dovecot authenticator via inet socket
+--------------------------------------------------------------
+If Dovecot is configured similar to :-
+
+service auth {
+...
+#SASL
+ inet_listener {
+ name = exim
+ port = 12345
+ }
+...
+}
+
+then an Exim authenticator can be configured :-
+
+ dovecot-plain:
+ driver = dovecot
+ public_name = PLAIN
+ server_socket = dovecot_server_name 12345
+ server_tls = true
+ server_set_id = $auth1
+
+If the server_socket does not start with a / it is taken as a hostname (or IP);
+and a whitespace-separated port number must be given.
+
+
+
+
+Logging protocol unusual states
+---------------------------------------------------------------
+An extra log_selector, "protocol_detail" has been added in the default build.
+The name may change in future, hence the Experimenal status.
+
+Currrently the only effect is to enable logging, under TLS,
+of a TCP RST received directly after a QUIT (in server mode).
+
+Outlook is consistently doing this; not waiting for the SMTP response
+to its QUIT, not properly closing the TLS session and not properly closing
+the TCP connection. Previously this resulted is an error from SSL_write
+being logged.
+
+
+
+Limits ESMTP extension
+---------------------------------------------------------------
+Per https://datatracker.ietf.org/doc/html/draft-freed-smtp-limits-01
+
+If compiled with EXPERIMENTAL_ESMTP_LIMITS=yes :-
+
+As a server, Exim will advertise, in the EHLO response, the limit for RCPT
+commands set by the recipients_max main-section config option (if it is set),
+and the limit for MAIL commands set by the smtp_accept_max_per_connection
+option.
+
+Note that as of writing, smtp_accept_max_per_connection is expanded but
+recipients_max is not.
+
+A new main-section option "limits_advertise_hosts" controls whether
+the limits are advertised; the default for the option is "*".
+
+As a client, Exim will:
+
+ - note an advertised MAILMAX; the lower of the value given and the
+ value from the transport connection_max_messages option is used.
+
+ - note an advertised RCPTMAX; the lower of the
+ value given and the value from the transport max_rcpt option is used.
+ Parallisation of transactions is not done if due to a RCPTMAX, unlike
+ max_rcpt.
+
+ - note an advertised RCPTDOMAINMAX, and behave as if the transport
+ multi_domains option was set to false. The value advertised is ignored.
+
+Values advertised are only noted for TLS connections and ones for which
+the server does not advertise TLS support.
+
+
+--------------------------------------------------------------
+End of file
+--------------------------------------------------------------
diff --git a/doc/filter.txt b/doc/filter.txt
new file mode 100644
index 0000000..010979b
--- /dev/null
+++ b/doc/filter.txt
@@ -0,0 +1,1720 @@
+Exim's interfaces to mail filtering
+
+Philip Hazel
+
+Copyright (c) 2021 The Exim Maintainers
+
+Revision 4.96 25 Jun 2022 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) The Exim Maintainers 2021. It corresponds to
+Exim version 4.96.
+
+
+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. A previously opened log is
+closed on a subsequent logfile command. 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 PCRE2 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/openssl.txt b/doc/openssl.txt
new file mode 100644
index 0000000..3efa833
--- /dev/null
+++ b/doc/openssl.txt
@@ -0,0 +1,165 @@
+OpenSSL
+=======
+
+The OpenSSL Project documents their supported releases at
+<https://www.openssl.org/policies/releasestrat.html>. The Exim
+Maintainers are unwilling to try to support Exim built with a
+version of a critical security library which is unmaintained.
+
+Thus as versions of OpenSSL become unsupported by OpenSSL, they become
+unsupported by Exim. Exim might build with older releases of OpenSSL,
+but that's risky behaviour.
+
+If your operating system vendor continues to ship an older version of
+OpenSSL and is diligently backporting security fixes, and they support
+Exim, then they will be backporting fixes to their packages of Exim too.
+If you wish to stick purely to packages of OpenSSL, then stick to
+packages of Exim too.
+
+If someone maintains "backports", that is worth exploring too.
+
+Note that a number of OSes use Exim with GnuTLS, not OpenSSL.
+
+Otherwise, assuming that your operating system has old OpenSSL, and you
+wish to use current Exim with OpenSSL, then you need to build and
+install your own, without interfering with the system libraries.
+Fortunately, this is easy.
+
+So this only applies if you build Exim yourself.
+
+
+Insecure versions and ciphers
+-----------------------------
+
+Email delivery to MX hosts is usually done with automatic fallback to
+plaintext if TLS could not be negotiated. There are good historical reasons
+for this. You can and should avoid it by using DNSSEC for signing your DNS
+and publishing TLSA records, to enable "DANE" security. This signals to
+senders that they should be able to verify your certificates, and that they
+should not fallback to cleartext.
+
+In the absence of DANE, trying to increase the security of TLS by removing
+support for older generations of ciphers and protocols will actually _lower_
+the security, because the clients fallback to plaintext and retry anyway. As
+a result, you should give serious thought to enabling older features which are
+no longer default in OpenSSL.
+
+The examples below explicitly enable ssl3 and weak ciphers.
+
+We don't like this, but reality doesn't care and is messy.
+
+
+Build
+-----
+
+Extract the current source of OpenSSL. Change into that directory.
+
+This assumes that `/opt/openssl` is not in use. If it is, pick
+something else. `/opt/exim/openssl` perhaps.
+
+If you pick a location shared amongst various local packages, such as
+`/usr/local` on Linux, then the new OpenSSL will be used by all of those
+packages. If that's what you want, great! If instead you want to
+ensure that only software you explicitly set to use the newer OpenSSL
+will try to use the new OpenSSL, then stick to something like
+`/opt/openssl`.
+
+ ./config --prefix=/opt/openssl --openssldir=/etc/ssl \
+ -L/opt/openssl/lib -Wl,-R/opt/openssl/lib \
+ enable-ssl-trace shared \
+ enable-ssl3 enable-ssl3-method enable-weak-ssl-ciphers
+ make
+ make install
+
+On some systems, the linker uses `-rpath` instead of `-R`; on such systems,
+replace the parameter starting `-Wl` with: `-Wl,-rpath,/opt/openssl/lib`.
+There are more variations on less common systems.
+
+You now have an installed OpenSSL under /opt/openssl which will not be
+used by any system programs.
+
+When you copy `src/EDITME` to `Local/Makefile` to make your build edits,
+choose the pkg-config approach in that file, but also tell Exim to add
+the relevant directory into the rpath stamped into the binary:
+
+ PKG_CONFIG_PATH=/opt/openssl/lib/pkgconfig
+
+ SUPPORT_TLS=yes
+ USE_OPENSSL_PC=openssl
+ LDFLAGS+=-ldl -Wl,-rpath,/opt/openssl/lib
+
+The -ldl is needed by OpenSSL 1.0.2+ on Linux and is not needed on most
+other platforms. The LDFLAGS is needed because `pkg-config` doesn't know
+how to emit information about RPATH-stamping, but we can still leverage
+`pkg-config` for everything else.
+
+Then build Exim:
+
+ make
+ sudo make install
+
+
+Confirming
+----------
+
+Run:
+
+ exim -d-all+expand --version
+
+and look for the `Library version: OpenSSL:` lines.
+
+To look at the libraries _probably_ found by the linker, use:
+
+ ldd $(which exim) # most platforms
+ otool -L $(which exim) # MacOS
+
+although that does not correctly handle restrictions imposed upon
+executables which are setuid.
+
+If the `chrpath` package is installed, then:
+
+ chrpath -l $(which exim)
+
+will show the DT_RPATH stamped into the binary.
+
+Your `binutils` package should come with `readelf`, so an alternative
+is to run:
+
+ readelf -d $(which exim) | grep RPATH
+
+It is important to use `RPATH` and not `RUNPATH`!
+
+The gory details about `RUNPATH` (skip unless interested):
+The OpenSSL library might be opened indirectly by some other library
+which Exim depends upon. If the executable does have `RUNPATH` then
+that will inhibit using either of `RPATH` or `RUNPATH` from the
+executable for finding the OpenSSL library when that other library tries
+to load it.
+In fact, if the intermediate library has a `RUNPATH` stamped into it,
+then this will block `RPATH` too, and will create problems with Exim.
+If you're in such a situation, and those libraries were supplied to you
+instead of built by you, then you're reaching the limits of sane
+repairability and it's time to prioritize rebuilding your mail-server
+hosts to be a current OS release which natively pulls in an
+upstream-supported OpenSSL, or stick to the OS releases of Exim.
+
+
+Very Advanced
+-------------
+
+You can not use $ORIGIN for portably packing OpenSSL in with Exim with
+normal Exim builds, because Exim is installed setuid which causes the
+runtime linker to ignore $ORIGIN in DT_RPATH.
+
+_If_ following the steps for a non-setuid Exim, _then_ you can use:
+
+ EXTRALIBS_EXIM=-ldl '-Wl,-rpath,$$ORIGIN/../lib'
+
+The doubled `$$` is needed for the make(1) layer and the quotes needed
+for the shell invoked by make(1) for calling the linker.
+
+Note that this is sufficiently far outside normal that the build-system
+doesn't support it by default; you'll want to drop a symlink to the lib
+directory into the Exim release top-level directory, so that lib exists
+as a sibling to the build-$platform directory.
+
diff --git a/doc/spec.txt b/doc/spec.txt
new file mode 100644
index 0000000..ddadcaf
--- /dev/null
+++ b/doc/spec.txt
@@ -0,0 +1,39528 @@
+Specification of the Exim Mail Transfer Agent
+
+Exim Maintainers
+
+Copyright (c) 2022 The Exim Maintainers
+
+Revision 4.96 25 Jun 2022 EM
+
+-------------------------------------------------------------------------------
+
+TABLE OF CONTENTS
+
+1. Introduction
+ 1.1. Exim documentation
+ 1.2. FTP site and websites
+ 1.3. Mailing lists
+ 1.4. Bug reports
+ 1.5. Where to find the Exim distribution
+ 1.6. Limitations
+ 1.7. Runtime configuration
+ 1.8. Calling interface
+ 1.9. 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. PCRE2 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. Running the daemon
+ 4.22. Upgrading Exim
+ 4.23. 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 runtime 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. Builtin macros
+ 6.10. Conditional skips in the configuration file
+ 6.11. Common option syntax
+ 6.12. Boolean options
+ 6.13. Integer values
+ 6.14. Octal integer values
+ 6.15. Fixed point numbers
+ 6.16. Time intervals
+ 6.17. String values
+ 6.18. Expanded strings
+ 6.19. User and group names
+ 6.20. List construction
+ 6.21. Changing list separators
+ 6.22. Empty items in lists
+ 6.23. Format of driver configurations
+7. The default configuration file
+ 7.1. Macros
+ 7.2. Main configuration settings
+ 7.3. ACL configuration
+ 7.4. Router configuration
+ 7.5. Transport configuration
+ 7.6. Default retry rule
+ 7.7. Rewriting configuration
+ 7.8. 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. Dnsdb lookup modifiers
+ 9.12. Pseudo dnsdb record types
+ 9.13. Multiple dnsdb lookups
+ 9.14. More about LDAP
+ 9.15. Format of LDAP queries
+ 9.16. LDAP quoting
+ 9.17. LDAP connections
+ 9.18. LDAP authentication and control information
+ 9.19. Format of data returned by LDAP
+ 9.20. More about NIS+
+ 9.21. SQL lookups
+ 9.22. More about MySQL, PostgreSQL, Oracle, InterBase, and Redis
+ 9.23. Specifying the server in the query
+ 9.24. Special MySQL features
+ 9.25. Special PostgreSQL features
+ 9.26. More about SQLite
+ 9.27. More about Redis
+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. Results of list checking
+ 10.6. Named lists
+ 10.7. Named lists compared with macros
+ 10.8. Named list caching
+ 10.9. Domain lists
+ 10.10. Host lists
+ 10.11. Special host list patterns
+ 10.12. Host list patterns that match by IP address
+ 10.13. Host list patterns for single-key lookups by host address
+ 10.14. Host list patterns that match by host name
+ 10.15. Behaviour when an IP address or name cannot be found
+ 10.16. Mixing wildcarded host names and addresses in host lists
+ 10.17. Temporary DNS errors when looking up host information
+ 10.18. Host list patterns for single-key lookups by host name
+ 10.19. Host list patterns for query-style lookups
+ 10.20. Address lists
+ 10.21. Case of letters in address lists
+ 10.22. 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 submissions (aka 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. Avoiding cleartext use
+ 34.2. Plaintext server options
+ 34.3. Using plaintext in a server
+ 34.4. The PLAIN authentication mechanism
+ 34.5. The LOGIN authentication mechanism
+ 34.6. Support for different kinds of authentication
+ 34.7. 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. The external authenticator
+ 41.1. External options
+ 41.2. Using external in a server
+ 41.3. Using external in a client
+42. The tls authenticator
+43. Encrypted SMTP connections using TLS/SSL
+ 43.1. Support for the "submissions" (aka "ssmtp" and "smtps") protocol
+ 43.2. OpenSSL vs GnuTLS
+ 43.3. GnuTLS parameter computation
+ 43.4. Requiring specific ciphers in OpenSSL
+ 43.5. Requiring specific ciphers or other parameters in GnuTLS
+ 43.6. Configuring an Exim server to use TLS
+ 43.7. Requesting and verifying client certificates
+ 43.8. Revoked certificates
+ 43.9. Caching of static server configuration items
+ 43.10. Configuring an Exim client to use TLS
+ 43.11. Caching of static client configuration items
+ 43.12. Use of TLS Server Name Indication
+ 43.13. Multiple messages on the same encrypted TCP/IP connection
+ 43.14. Certificates and all that
+ 43.15. Certificate chains
+ 43.16. Self-signed certificates
+ 43.17. TLS Resumption
+ 43.18. DANE
+44. Access control lists
+ 44.1. Testing ACLs
+ 44.2. Specifying when ACLs are used
+ 44.3. The non-SMTP ACLs
+ 44.4. The SMTP connect ACL
+ 44.5. The EHLO/HELO ACL
+ 44.6. The DATA ACLs
+ 44.7. The SMTP DKIM ACL
+ 44.8. The SMTP MIME ACL
+ 44.9. The SMTP PRDR ACL
+ 44.10. The QUIT ACL
+ 44.11. The not-QUIT ACL
+ 44.12. Finding an ACL to use
+ 44.13. ACL return codes
+ 44.14. Unset ACL options
+ 44.15. Data for message ACLs
+ 44.16. Data for non-message ACLs
+ 44.17. Format of an ACL
+ 44.18. ACL verbs
+ 44.19. ACL variables
+ 44.20. Condition and modifier processing
+ 44.21. ACL modifiers
+ 44.22. Use of the control modifier
+ 44.23. Summary of message fixup control
+ 44.24. Adding header lines in ACLs
+ 44.25. Removing header lines in ACLs
+ 44.26. ACL conditions
+ 44.27. Using DNS lists
+ 44.28. Specifying the IP address for a DNS list lookup
+ 44.29. DNS lists keyed on domain names
+ 44.30. Multiple explicit keys for a DNS list
+ 44.31. Data returned by DNS lists
+ 44.32. Variables set from DNS lists
+ 44.33. Additional matching conditions for DNS lists
+ 44.34. Negated DNS matching conditions
+ 44.35. Handling multiple DNS records from a DNS list
+ 44.36. Detailed information from merged DNS lists
+ 44.37. DNS lists and IPv6
+ 44.38. Previously seen user and hosts
+ 44.39. Rate limiting incoming messages
+ 44.40. Ratelimit options for what is being measured
+ 44.41. Ratelimit update modes
+ 44.42. Ratelimit options for handling fast clients
+ 44.43. Limiting the rate of different events
+ 44.44. Using rate limiting
+ 44.45. Address verification
+ 44.46. Callout verification
+ 44.47. Additional parameters for callouts
+ 44.48. Callout caching
+ 44.49. Quota caching
+ 44.50. Sender address verification reporting
+ 44.51. Redirection while verifying
+ 44.52. Client SMTP authorization (CSA)
+ 44.53. Bounce address tag validation
+ 44.54. Using an ACL to control relaying
+ 44.55. Checking a relay configuration
+45. Content scanning at ACL time
+ 45.1. Scanning for viruses
+ 45.2. Scanning with SpamAssassin and Rspamd
+ 45.3. Calling SpamAssassin from an Exim ACL
+ 45.4. Scanning MIME parts
+ 45.5. Scanning with regular expressions
+46. Adding a local scan function to Exim
+ 46.1. Building Exim to use a local scan function
+ 46.2. API for local_scan()
+ 46.3. Configuration options for local_scan()
+ 46.4. Available Exim variables
+ 46.5. Structure of header lines
+ 46.6. Structure of recipient items
+ 46.7. Available Exim functions
+ 46.8. More about Exim's memory handling
+47. System-wide message filtering
+ 47.1. Specifying a system filter
+ 47.2. Testing a system filter
+ 47.3. Contents of a system filter
+ 47.4. Additional variable for system filters
+ 47.5. Defer, freeze, and fail commands for system filters
+ 47.6. Adding and removing headers in a system filter
+ 47.7. Setting an errors address in a system filter
+ 47.8. Per-address filtering
+48. Message processing
+ 48.1. Submission mode for non-local messages
+ 48.2. Line endings
+ 48.3. Unqualified addresses
+ 48.4. The UUCP From line
+ 48.5. Resent- header lines
+ 48.6. The Auto-Submitted: header line
+ 48.7. The Bcc: header line
+ 48.8. The Date: header line
+ 48.9. The Delivery-date: header line
+ 48.10. The Envelope-to: header line
+ 48.11. The From: header line
+ 48.12. The Message-ID: header line
+ 48.13. The Received: header line
+ 48.14. The References: header line
+ 48.15. The Return-path: header line
+ 48.16. The Sender: header line
+ 48.17. Adding and removing header lines in routers and transports
+ 48.18. Constructed addresses
+ 48.19. Case of local parts
+ 48.20. Dots in local parts
+ 48.21. Rewriting addresses
+49. SMTP processing
+ 49.1. Outgoing SMTP and LMTP over TCP/IP
+ 49.2. Errors in outgoing SMTP
+ 49.3. Incoming SMTP messages over TCP/IP
+ 49.4. Unrecognized SMTP commands
+ 49.5. Syntax and protocol errors in SMTP commands
+ 49.6. Use of non-mail SMTP commands
+ 49.7. The VRFY and EXPN commands
+ 49.8. The ETRN command
+ 49.9. Incoming local SMTP
+ 49.10. Outgoing batched SMTP
+ 49.11. Incoming batched SMTP
+50. Customizing bounce and warning messages
+ 50.1. Customizing bounce messages
+ 50.2. Customizing warning messages
+51. Some common configuration settings
+ 51.1. Sending mail to a smart host
+ 51.2. Using Exim to handle mailing lists
+ 51.3. Syntax errors in mailing lists
+ 51.4. Re-expansion of mailing lists
+ 51.5. Closed mailing lists
+ 51.6. Variable Envelope Return Paths (VERP)
+ 51.7. Virtual domains
+ 51.8. Multiple user mailboxes
+ 51.9. Simplified vacation processing
+ 51.10. Taking copies of mail
+ 51.11. Intermittently connected hosts
+ 51.12. Exim on the upstream server host
+ 51.13. Exim on the intermittently connected client host
+52. Using Exim as a non-queueing client
+53. Log files
+ 53.1. Where the logs are written
+ 53.2. Logging to local files that are periodically "cycled"
+ 53.3. Datestamped log files
+ 53.4. Logging to syslog
+ 53.5. Log line flags
+ 53.6. Logging message reception
+ 53.7. Logging deliveries
+ 53.8. Discarded deliveries
+ 53.9. Deferred deliveries
+ 53.10. Delivery failures
+ 53.11. Fake deliveries
+ 53.12. Completion
+ 53.13. Summary of Fields in Log Lines
+ 53.14. Other log entries
+ 53.15. Reducing or increasing what is logged
+ 53.16. Message log
+54. Exim utilities
+ 54.1. Finding out what Exim processes are doing (exiwhat)
+ 54.2. Selective queue listing (exiqgrep)
+ 54.3. Summarizing the queue (exiqsumm)
+ 54.4. Extracting specific information from the log (exigrep)
+ 54.5. Selecting messages by various criteria (exipick)
+ 54.6. Cycling log files (exicyclog)
+ 54.7. Mail statistics (eximstats)
+ 54.8. Checking access policy (exim_checkaccess)
+ 54.9. Making DBM files (exim_dbmbuild)
+ 54.10. Finding individual retry times (exinext)
+ 54.11. Hints database maintenance
+ 54.12. exim_dumpdb
+ 54.13. exim_tidydb
+ 54.14. exim_fixdb
+ 54.15. Mailbox maintenance (exim_lock)
+55. The Exim monitor
+ 55.1. Running the monitor
+ 55.2. The stripcharts
+ 55.3. Main action buttons
+ 55.4. The log display
+ 55.5. The queue display
+ 55.6. The queue menu
+56. Security considerations
+ 56.1. Building a more "hardened" Exim
+ 56.2. Root privilege
+ 56.3. Running Exim without privilege
+ 56.4. Delivering to local files
+ 56.5. Running local commands
+ 56.6. Trust in configuration data
+ 56.7. IPv4 source routing
+ 56.8. The VRFY, EXPN, and ETRN commands in SMTP
+ 56.9. Privileged users
+ 56.10. Spool files
+ 56.11. Use of argv[0]
+ 56.12. Use of %f formatting
+ 56.13. Embedded Exim path
+ 56.14. Dynamic module directory
+ 56.15. Use of sprintf()
+ 56.16. Use of debug_printf() and log_write()
+ 56.17. Use of strcat() and strcpy()
+57. Format of spool files
+ 57.1. Format of the -H file
+ 57.2. Format of the -D file
+58. DKIM, SPF, SRS and DMARC
+ 58.1. DKIM (DomainKeys Identified Mail)
+ 58.2. Signing outgoing messages
+ 58.3. Verifying DKIM signatures in incoming mail
+ 58.4. SPF (Sender Policy Framework)
+ 58.5. SRS (Sender Rewriting Scheme)
+ 58.6. DMARC
+59. Proxies
+ 59.1. Inbound proxies
+ 59.2. Outbound proxies
+ 59.3. Logging
+60. Internationalisation
+ 60.1. MTA operations
+ 60.2. MDA operations
+61. Events
+62. 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 Exim, 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.96 of Exim.
+Substantive changes from the 4.95 edition are marked in some renditions of this
+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, this 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 (https://
+www.uit.co.uk/exim-book/).
+
+The 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 Exim 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 Exim (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
+openssl.txt installing a current OpenSSL release
+
+The main specification and the specification of the filtering language are also
+available in other formats (HTML, PostScript, PDF, and Texinfo). Section 1.5
+below tells you how to get hold of these.
+
+
+1.2 FTP site and websites
+-------------------------
+
+The primary site for Exim source distributions is the exim.org FTP site,
+available over HTTPS, HTTP and FTP. These services, and the exim.org website,
+are hosted at the University of Cambridge.
+
+As well as Exim distribution tar files, the Exim website contains a number of
+differently formatted versions of the documentation. A recent addition to the
+online information is the Exim wiki (https://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. The wiki site should always
+redirect to the correct place, which is currently provided by GitHub, and is
+open to editing by anyone with a GitHub account.
+
+An Exim Bugzilla exists at https://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. Please do not ask for configuration
+help in the bug-tracker.
+
+
+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:
+
+https://alioth-lists.debian.net/cgi-bin/mailman/listinfo/pkg-exim4-users
+
+Please ask Debian-specific questions on that list and not on the general Exim
+lists.
+
+
+1.4 Bug reports
+---------------
+
+Reports of obvious bugs can be emailed to bugs@exim.org or reported via the
+Bugzilla (https://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.5 Where to find the Exim distribution
+---------------------------------------
+
+The master distribution site for the Exim distribution is
+
+https://downloads.exim.org/
+
+The service is available over HTTPS, HTTP and FTP. We encourage people to
+migrate to HTTPS.
+
+The content served at https://downloads.exim.org/ is identical to the content
+served at https://ftp.exim.org/pub/exim and ftp://ftp.exim.org/pub/exim.
+
+If accessing via a hostname containing ftp, then the file references that
+follow are relative to the exim directories at these sites. If accessing via
+the hostname downloads then the subdirectories described here are top-level
+directories.
+
+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 top 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.xz
+exim-n.nn.tar.gz
+exim-n.nn.tar.bz2
+
+where n.nn is the highest such version number in the directory. The three files
+contain identical data; the only difference is the type of compression. The .xz
+file is usually the smallest, while the .gz file is the most portable to old
+systems.
+
+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 the Exim Maintainer's PGP
+keys, a version of which can be found in the release directory in the file
+Exim-Maintainers-Keyring.asc. All keys used will be available in public
+keyserver pools, such as pool.sks-keyservers.net.
+
+At the time of the last update, releases were being made by Jeremy Harris and
+signed with key 0xBCE58C8CE41F32DF. Other recent keys used for signing are
+those of Heiko Schlittermann, 0x26101B62F69376CE, and of Phil Pennock,
+0x4D1E900E14C1CC04.
+
+The signatures for the tar bundles are in:
+
+exim-n.nn.tar.xz.asc
+exim-n.nn.tar.gz.asc
+exim-n.nn.tar.bz2.asc
+
+For each released version, the log of changes is made 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 and .xz forms.
+
+
+1.6 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.7 Runtime configuration
+-------------------------
+
+Exim's runtime 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.8 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 in 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 in 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.9 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 the 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 in 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 PCRE2 library, copyright (c)
+ University of Cambridge. The source to PCRE2 is not longer shipped with
+ Exim, so you will need to use the version of PCRE2 shipped with your
+ system, or obtain and install the full version of the library from https://
+ github.com/PhilipHazel/pcre2/releases.
+
+ * 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 https://cr.yp.to/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 (https://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 license 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 from 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 44). 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 46). 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 47). 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 filenames, 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 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 addresses
+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 46) 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 57.
+
+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 all 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 53). 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 Exim 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. Runtime options specify which ones are
+actually used for delivering messages.
+
+Each driver that is specified in the runtime 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 specifically by the local host.
+Typically these are 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 case-sensitively. 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. A match verifies the variable $domain (which
+ carries tainted data) and assigns an untainted value to the $domain_data
+ variable. Such an untainted value is often needed in the transport. For
+ specifics of the matching operation and the resulting untainted value,
+ refer to section 10.9.
+
+ When an untainted value is wanted, use this option rather than the generic
+ condition option.
+
+ * If the local_parts option is set, the local part of the address must be in
+ the set of local parts that it defines. A match verifies the variable
+ $local_part (which carries tainted data) and assigns an untainted value to
+ the $local_part_data variable. Such an untainted value is often needed in
+ the transport. For specifics of the matching operation and the resulting
+ untainted value, refer to section 10.22.
+
+ When an untainted value is wanted, use this option rather than the generic
+ condition option.
+
+ 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, $local_part_prefix_v, $local_part_suffix and
+ $local_part_suffix_v 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 while using this option for address matching technically works,
+ it does not set any de-tainted values. Such values are often needed, either
+ for router-specific options or for transport options. Using the domains and
+ local_parts options is usually the most convenient way to obtain them.
+
+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 47
+ 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 in your queue forever. 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 50 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
+51.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 in 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.96) 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. A C99-capable compiler will be required for the build.
+
+
+4.3 PCRE2 library
+-----------------
+
+Exim no longer has an embedded regular-expression library as the vast majority
+of modern systems include PCRE2 as a system library, although you may need to
+install the PCRE2 package or the PCRE2 development package for your operating
+system. If your system has a normal PCRE2 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 PCRE2_LIBS and INCLUDE
+directives appropriately, or set PCRE2_CONFIG=yes to use the installed
+pcre-config command. If your operating system has no PCRE2 support then you
+will need to obtain and build the current PCRE2 from https://github.com/
+PhilipHazel/pcre2/releases. More information on PCRE2 is available at https://
+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
+ filename 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 when Exim last revamped support were numbered 5.x.
+ Maintenance of some of the earlier releases has ceased, and Exim no longer
+ supports versions before 3.x.
+
+ All versions of Berkeley DB could be obtained from http://www.sleepycat.com
+ /, which is now a redirect to their new owner's page with far newer
+ versions listed. It is probably wise to plan to move your storage
+ configurations away from Berkeley DB format, as today there are smaller and
+ simpler alternatives more suited to Exim's usage model.
+
+ 6. Yet another DBM library, called tdb, is available from https://
+ sourceforge.net/projects/tdb/files/. 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.
+
+You can set USE_NDBM if needed to override an operating system default.
+
+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
+DBMLIB = -lgdbm -lgdbm_compat
+
+The last of those was for a Linux having GDBM provide emulated NDBM facilities.
+
+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 runtime 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 filenames; Exim uses the first of them that exists.
+
+There are a few other parameters that can be specified either at build time or
+at runtime, 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 runtime, 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
+45.
+
+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 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 is set at build time). 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 https://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 is usually built to support encrypted SMTP connections, using the STARTTLS
+command as per RFC 2487. It can also support 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 you do not want TLS support you should set
+
+DISABLE_TLS=yes
+
+in Local/Makefile.
+
+If OpenSSL is installed, you should set
+
+USE_OPENSL=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:
+
+USE_OPENSSL=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:
+
+USE_OPENSSL=yes
+USE_OPENSSL_PC=openssl
+
+If GnuTLS is installed, you should set
+
+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:
+
+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:
+
+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 43.
+
+
+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". Exim used to have a
+compile option for including A6 record support but this has now been withdrawn.
+
+
+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.
+
+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
+runtime 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
+runtime.
+
+
+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 56 for
+details).
+
+Exim's runtime 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 runtime 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.96-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.5).
+
+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 runtime 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 runtime 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 Running the daemon
+-----------------------
+
+The most common command line for launching the Exim daemon looks like
+
+exim -bd -q5m
+
+This starts a daemon which
+
+ * listens for incoming smtp connections, launching handler processes for each
+ new one
+
+ * starts a queue-runner process every five minutes, to inspect queued
+ messages and run delivery attempts on any that have arrived at their retry
+ time
+
+Should a queue run take longer than the time between queue-runner starts, they
+will run in parallel. Numbers of jobs of the various types are subject to
+policy controls defined in the configuration.
+
+
+4.22 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.23 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 runtime 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 $message_exim_id)
+ 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.
+
+ Macro processing is done on lines before string-expansion: new macros can
+ be defined and macros will be expanded. Because macros in the config file
+ are often used for secrets, those are only available to admin users.
+
+-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 44.46) 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 54.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 44 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 or directory
+ (depending on the used scanner interface), 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 config is given as an argument, the config is output, as it was parsed,
+ any include file resolved, any comment removed.
+
+ If config_file is given as an argument, the name of the runtime
+ configuration file is output. (configure_file works too, for backward
+ compatibility.) 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 environment is given as an argument, the set of environment variables is
+ output, line by line. Using the -n flag suppresses the value of the
+ variables.
+
+ 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. For the "-bP macro <name>" form, if no such macro is found the
+ exit status will be nonzero.
+
+-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 in 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 in 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 in 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 in 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 44).
+ 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 49.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 44) 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 runtime 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 44). 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 runtime 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
+ filename, 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 in 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 filename 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 filename 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. Only macro names up to
+ 22 letters long can be set.
+
+-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 46)
+ lookup general lookup code and all lookups
+ memory memory handling
+ noutf8 modifier: avoid UTF-8 line-drawing
+ pid modifier: 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 modifier: 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.
+
+ The "noutf8" selector disables the use of UTF-8 line-drawing characters to
+ group related information. When disabled. ascii-art is used instead. Using
+ the "+all" option does not set this modifier,
+
+ 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 48.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. Solaris
+ 2.4 (SunOS 5.4) Sendmail has a similar -i processing option https://
+ docs.oracle.com/cd/E19457-01/801-6680-1M/801-6680-1M.pdf, p. 1M-529), and
+ therefore a -oi command line option, which both are used by its mailx
+ command.
+
+-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> <host IP> <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 49. 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.
+
+-MCD
+
+ 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 remote host supports the ESMTP DSN extension.
+
+-MCd
+
+ This option is not intended for use by external callers. It is used
+ internally by Exim in conjunction with the -d option to pass on an
+ information string on the purpose of the process.
+
+-MCG <queue name>
+
+ 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 an
+ alternate queue is used, named by the following argument.
+
+-MCK
+
+ 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 a
+ remote host supports the ESMTP CHUNKING extension.
+
+-MCL
+
+ 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 advertised limits on numbers of
+ mails, recipients or recipient domains. The limits are given by the
+ following three arguments.
+
+-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.
+
+-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 connection t a remote server is via a SOCKS proxy, using addresses and
+ ports given by the following four arguments.
+
+-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.
+
+-MCq <recipient address> <size>
+
+ This option is not intended for use by external callers. It is used
+ internally by Exim to implement quota checking for local users.
+
+-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 ESMTP 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.
+
+-MCr <SNI>, -MCs <SNI>
+
+ These options are not intended for use by external callers. It is used
+ internally by Exim in conjunction with the -MCt option, and passes on the
+ fact that a TLS Server Name Indication was sent as part of the channel
+ establishment. The argument gives the SNI string. The "r" variant indicates
+ a DANE-verified connection.
+
+-MCt <IP address> <port> <cipher>
+
+ 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 connection is being proxied by a parent process for handling
+ TLS encryption. The arguments give the local address and port being
+ proxied, and the TLS cipher.
+
+-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 56).
+ 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.
+
+-MG <queue name> <message id> <message id> ...
+
+ This option requests that each listed message be moved from its current
+ queue to the given named queue. The destination queue name argument is
+ required, but can be an empty string to define the default queue. If the
+ messages are not currently located in the default queue, a -qG<name> option
+ will be required to define the source queue.
+
+-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 in 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 a synonym for -om that is accepted by Sendmail (https://
+ docs.oracle.com/cd/E19457-01/801-6680-1M/801-6680-1M.pdf p. 1M-258), 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 makes
+ the output more terse (suppresses option names, environment values and
+ config pretty printing).
+
+-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 filename. 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 in the queue for later delivery, and the original reception
+ process exits. See chapter 52 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 in 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 in 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. Repeated use of
+ this option is not supported.
+
+-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.
+
+-oPX
+
+ This option is not intended for general use. The daemon uses it when
+ terminating due to a SIGTEM, possibly in combination with -oP <path>. It
+ causes the pid file to be removed.
+
+-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.16.
+
+-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.16.
+
+-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 filename.
+
+-oY
+
+ This option controls the creation of an inter-process communications
+ endpoint by the Exim daemon. It is only relevant when the -bd (start
+ listening daemon) option is also given. Normally the daemon creates this
+ socket, unless a -oX and no -oP option is also present. If this option is
+ given then the socket will not be created. This could be required if the
+ system is running multiple daemons.
+
+ The socket is currently used for
+
+ o fast ramp-up of queue runner processes
+
+ o obtaining a current queue size
+
+-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). Repeated
+ use of this option is not supported.
+
+-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).
+
+ If other commandline options do not specify an action, 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.
+
+ Performance will be best if the queue_run_in_order option is false. If that
+ is so and the queue_fast_ramp option is true then in the first phase of the
+ run, once a threshold number of messages are routed for a given host, a
+ delivery process is forked in parallel with the rest of the scan.
+
+ 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 in 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 in the
+ queue for later delivery.
+
+-q[q][i][f[f]][l][G<name>[/<time>]]]
+
+ If the G flag and a name is present, the queue runner operates on the queue
+ with the given name rather than the default queue. The name should not
+ contain a / character. For a periodic queue run (see below) append to the
+ name a slash and a time value.
+
+ If other commandline options specify an action, a -qG<name> option will
+ specify a queue to operate on. For example:
+
+ exim -bp -qGquarantine
+ mailq -qGquarantine
+ exim -qGoffpeak -Rf @special.domain.example
+
+-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.16). 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 44), 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 43 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.
+
+-z <log-line>
+
+ This option writes its argument to Exim's logfile. Use is restricted to
+ administrators; the intent is for operational notes. Quotes should be used
+ to maintain a multi-word item as a single argument, under most shells.
+
+
+
+===============================================================================
+6. THE EXIM RUNTIME CONFIGURATION FILE
+
+Exim uses a single runtime 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 filenames, in which case Exim uses the first existing
+file in the list.
+
+The runtime 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 runtime 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 runtime 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 filename, 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 in 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 filename must not contain the sequence "/../". There is no default setting
+for ALT_CONFIG_PREFIX; when it is unset, any filename 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 filename 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 filename
+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 at least one literal space, and
+the name of the part. The optional parts are:
+
+ * ACL: Access control lists for controlling incoming SMTP mail (see chapter
+ 44).
+
+ * 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 46.
+
+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 44, 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.11 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 runtime configuration file by using
+this syntax:
+
+.include <filename>
+.include_if_exists <filename>
+
+on a line by itself. Double quotes round the filename 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. The first form allows a
+relative name. It is resolved relative to the directory of the including file.
+For the second form an absolute filename 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.6.
+
+
+6.9 Builtin macros
+------------------
+
+Exim defines some macros depending on facilities available, which may differ
+due to build-time definitions and from one release to another. All of these
+macros start with an underscore. They can be used to conditionally include
+parts of a configuration (see below).
+
+The following classes of macros are defined:
+
+ _HAVE_* build-time defines
+ _DRIVER_ROUTER_* router drivers
+ _DRIVER_TRANSPORT_* transport drivers
+ _DRIVER_AUTHENTICATOR_* authenticator drivers
+ _LOG_* log_selector values
+ _OPT_MAIN_* main config options
+ _OPT_ROUTERS_* generic router options
+ _OPT_TRANSPORTS_* generic transport options
+ _OPT_AUTHENTICATORS_* generic authenticator options
+ _OPT_ROUTER_*_* private router options
+ _OPT_TRANSPORT_*_* private transport options
+ _OPT_AUTHENTICATOR_*_* private authenticator options
+
+Use an "exim -bP macros" command to get the list of macros.
+
+
+6.10 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 (or "A" or
+"AA"), 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.11 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.12 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.13 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; if by the letter
+G, 1024x1024x1024. 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.14 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.15 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.16 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.17 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.18 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.19 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.20 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.17 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 spaces around the first
+colon in the example above are necessary. If they were not there, the list
+would be interpreted as the two items 127.0.0.1:: and 1.
+
+
+6.21 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.22 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.23 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 Macros
+----------
+
+All macros should be defined before any options.
+
+One macro is specified, but commented out, in the default configuration:
+
+# ROUTER_SMARTHOST=MAIL.HOSTNAME.FOR.CENTRAL.SERVER.EXAMPLE
+
+If all off-site mail is expected to be delivered to a "smarthost", then set the
+hostname here and uncomment the macro. This will affect which router is used
+later on. If this is left commented out, then Exim will perform direct-to-MX
+deliveries using a dnslookup router.
+
+In addition to macros defined here, Exim includes a number of built-in macros
+to enable configuration to be guarded by a binary built with support for a
+given feature. See section 6.9 for more details.
+
+
+7.2 Main configuration settings
+-------------------------------
+
+The main (global) configuration option settings section must always come first
+in the file, after the macros. 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.6).
+
+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 45.
+
+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 43.
+
+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.8). Mail submission from mail clients (MUAs) should be
+separate from inbound mail to your domain (MX delivery) for various good
+reasons (eg, ability to impose much saner TLS protocol and ciphersuite
+requirements without unintended consequences). RFC 6409 (previously 4409)
+specifies use of port 587 for SMTP Submission, which uses STARTTLS, so this is
+the "submission" port. RFC 8314 specifies use of port 465 as the "submissions"
+protocol, which should be used in preference to 587. You should also consider
+deploying SRV records to help clients find these ports. Older names for
+"submissions" are "smtps" and "ssmtp".
+
+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 negotiated 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 log_selector option is used to increase the detail of logging over the
+default:
+
+log_selector = +smtp_protocol_error +smtp_syntax_error \
+ +tls_certificate_verified
+
+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 next 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 in 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.
+
+Exim queues it's messages in a spool directory. If you expect to have large
+queues, you may consider using this option. It splits the spool directory into
+subdirectories to avoid file system degradation from many files in a single
+directory, resulting in better performance. Manual manipulation of queued
+messages becomes more complex (though fortunately not often needed).
+
+# split_spool_directory = true
+
+In an ideal world everybody follows the standards. For non-ASCII messages RFC
+2047 is a standard, allowing a maximum line length of 76 characters. Exim
+adheres that standard and won't process messages which violate this standard.
+(Even ${rfc2047:...} expansions will fail.) 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.
+
+# check_rfc2047_length = false
+
+If you need to be strictly RFC compliant you may wish to disable the 8BITMIME
+advertisement. Use this, if you exchange mails with systems that are not 8-bit
+clean.
+
+# accept_8bitmime = false
+
+Libraries you use may depend on specific environment settings. This imposes a
+security risk (e.g. PATH). There are two lists: keep_environment for the
+variables to import as they are, and add_environment for variables we want to
+set to a fixed value. Note that TZ is handled separately, by the timezone
+runtime option and by the TIMEZONE_DEFAULT buildtime option.
+
+# keep_environment = ^LDAP
+# add_environment = PATH=/usr/bin::/bin
+
+
+7.3 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 domains = +local_domains
+ local_parts = ^[.] : ^.*[@%!/|]
+ message = Restricted characters in address
+
+deny domains = !+local_domains
+ local_parts = ^[./|] : ^.*[@%!] : ^.*/\\.\\./
+ message = Restricted characters in address
+
+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
+filename (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 44.45 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 48.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.8. 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 dnslists = black.list.example
+# message = rejected because $sender_host_address \
+# is in a black list at $dnslist_domain\n\
+# $dnslist_text
+#
+# 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.4 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.
+
+Which router is used next depends upon whether or not the ROUTER_SMARTHOST
+macro has been defined, per
+
+.ifdef ROUTER_SMARTHOST
+smarthost:
+#...
+.else
+dnslookup:
+#...
+.endif
+
+If ROUTER_SMARTHOST has been defined, either at the top of the file or on the
+command-line, then we route all non-local mail to that smarthost; otherwise,
+we'll perform DNS lookups for direct-to-MX lookup. Any mail which is to a local
+domain will skip these routers because of the domains option.
+
+smarthost:
+ driver = manualroute
+ domains = ! +local_domains
+ transport = smarthost_smtp
+ route_data = ROUTER_SMARTHOST
+ ignore_target_hosts = <; 0.0.0.0 ; 127.0.0.0/8 ; ::1
+ no_more
+
+This router only handles mail which is not to 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 manualroute because we are manually specifying
+how mail should be routed onwards, instead of using DNS MX. While the name of
+this router instance is arbitrary, the driver option must be one of the driver
+modules that is in the Exim binary.
+
+With no pre-conditions other than domains, all mail for non-local domains will
+be handled by this router, and the no_more setting will ensure that no other
+routers will be used for messages matching the pre-conditions. See 3.12 for
+more on how the pre-conditions apply. For messages which are handled by this
+router, we provide a hostname to deliver to in route_data and the macro
+supplies the value; the address is then queued for the smarthost_smtp
+transport.
+
+dnslookup:
+ driver = dnslookup
+ domains = ! +local_domains
+ transport = remote_smtp
+ ignore_target_hosts = 0.0.0.0 : 127.0.0.0/8
+ no_more
+
+The domains option behaves as per smarthost, above.
+
+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.5 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
+
+Two remote transports and four local transports are defined.
+
+remote_smtp:
+ driver = smtp
+ message_size_limit = ${if > {$max_received_linelength}{998} {1}{0}}
+.ifdef _HAVE_PRDR
+ hosts_try_prdr = *
+.endif
+
+This transport is used for delivering messages over SMTP connections. The list
+of remote hosts comes from the router. The message_size_limit usage is a hack
+to avoid sending on messages with over-long lines.
+
+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. The built-in macro _HAVE_PRDR guards the use of the
+hosts_try_prdr configuration option.
+
+The other remote transport is used when delivering to a specific smarthost with
+whom there must be some kind of existing relationship, instead of the usual
+federated system.
+
+smarthost_smtp:
+ driver = smtp
+ message_size_limit = ${if > {$max_received_linelength}{998} {1}{0}}
+ multi_domain
+ #
+.ifdef _HAVE_TLS
+ # Comment out any of these which you have to, then file a Support
+ # request with your smarthost provider to get things fixed:
+ hosts_require_tls = *
+ tls_verify_hosts = *
+ # As long as tls_verify_hosts is enabled, this this will have no effect,
+ # but if you have to comment it out then this will at least log whether
+ # you succeed or not:
+ tls_try_verify_hosts = *
+ #
+ # The SNI name should match the name which we'll expect to verify;
+ # many mail systems don't use SNI and this doesn't matter, but if it does,
+ # we need to send a name which the remote site will recognize.
+ # This _should_ be the name which the smarthost operators specified as
+ # the hostname for sending your mail to.
+ tls_sni = ROUTER_SMARTHOST
+ #
+.ifdef _HAVE_OPENSSL
+ tls_require_ciphers = HIGH:!aNULL:@STRENGTH
+.endif
+.ifdef _HAVE_GNUTLS
+ tls_require_ciphers = SECURE192:-VERS-SSL3.0:-VERS-TLS1.0:-VERS-TLS1.1
+.endif
+.endif
+.ifdef _HAVE_PRDR
+ hosts_try_prdr = *
+.endif
+
+After the same message_size_limit hack, we then specify that this Transport can
+handle messages to multiple domains in one run. The assumption here is that
+you're routing all non-local mail to the same place and that place is happy to
+take all messages from you as quickly as possible. All other options depend
+upon built-in macros; if Exim was built without TLS support then no other
+options are defined. If TLS is available, then we configure "stronger than
+default" TLS ciphersuites and versions using the tls_require_ciphers option,
+where the value to be used depends upon the library providing TLS. Beyond that,
+the options adopt the stance that you should have TLS support available from
+your smarthost on today's Internet, so we turn on requiring TLS for the mail to
+be delivered, and requiring that the certificate be valid, and match the
+expected hostname. The tls_sni option can be used by service providers to
+select an appropriate certificate to present to you and here we re-use the
+ROUTER_SMARTHOST macro, because that is unaffected by CNAMEs present in DNS.
+You want to specify the hostname which you'll expect to validate for, and that
+should not be subject to insecure tampering via DNS results.
+
+For the hosts_try_prdr option see the previous transport.
+
+All other options are defaulted.
+
+local_delivery:
+ driver = appendfile
+ file = /var/mail/$local_part_data
+ 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.
+
+We prefer to avoid using $local_part directly to define the mailbox filename,
+as it is provided by a potential bad actor. Instead we use $local_part_data,
+the result of looking up $local_part in the user database (done by using
+check_local_user in the the router).
+
+By default appendfile 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 on stdout or stderr 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.6 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. The time is
+measured from first failure, not from the time the message was received.
+
+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.7 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.8 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.2.
+
+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 PCRE2 regular expression library; this provides regular expression matching
+that is compatible with Perl 5. The syntax and semantics of regular expressions
+is discussed in online Perl manpages, 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 PCRE2 is included in the PCRE2 distribution, and no further
+description is included here. The PCRE2 functions are called from Exim using
+the default option settings (that is, with no PCRE2 options set), except that
+the PCRE2_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. The key for
+ the lookup is specified as part of the string expansion.
+
+ 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. The key for the lookup is implicit, given by the
+ context in which the list is expanded.
+
+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.
+
+The key for an expansion-style lookup must be given explicitly.
+
+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). The result of
+the expansion is not tainted.
+
+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.
+
+The key for a list-style lookup is implicit, from the lookup context, if the
+lookup is a single-key type (see below). For query-style lookup types the key
+must be given explicitly.
+
+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 lookup type may optionally be followed by a comma and a comma-separated
+list of options. Each option is a "name=value" pair. Whether an option is
+meaningful depends on the lookup type.
+
+All lookups support the option "cache=no_rd". If this is given then the cache
+that Exim manages for lookup results is not checked before doing the lookup.
+The result of the lookup is still written to the cache.
+
+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
+ file string may not be tainted.
+
+ All single-key lookups support the option "ret=key". If this is given and
+ the lookup (either underlying implementation or cached value) returns data,
+ the result is replaced with a non-tainted version of the lookup key.
+
+ * 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.
+
+ If tainted data is used in the query then it should be quuted by using the
+ ${quote_<lookup-type>:<string>} expansion operator appropriate for the
+ lookup.
+
+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 and tools for building the files can be found in several places:
+
+ https://cr.yp.to/cdb.html
+ https://www.corpit.ru/mjt/tinycdb.html
+ https://packages.debian.org/stable/utils/freecdb
+ https://github.com/philpennock/cdbtools (in Go)
+
+ 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 54.9).
+
+ * dsearch: The given file must be an absolute directory path; 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 then so does the lookup. The result is regarded as untainted.
+
+ Options for the lookup can be given by appending them after the word
+ "dsearch", separated by a comma. Options, if present, are a comma-separated
+ list having each element starting with a tag name and an equals.
+
+ Two options are supported, for the return value and for filtering match
+ candidates. The "ret" option requests an alternate result value of the
+ entire path for the entry. Example:
+
+ ${lookup {passwd} dsearch,ret=full {/etc}}
+
+ The default result is just the requested entry. The "filter" option
+ requests that only directory entries of a given type are matched. The match
+ value is one of "file", "dir" or "subdir" (the latter not matching "." or
+ ".."). Example:
+
+ ${lookup {passwd} dsearch,filter=file {/etc}}
+
+ The default matching is for any entry type, including directories and
+ symlinks.
+
+ An example of how this lookup can be used to support virtual domains is
+ given in section 51.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.13).
+
+ Warning 3: Do not use an IPv4-mapped IPv6 address for a key; use the IPv4,
+ in dotted-quad form. (Exim converts IPv4-mapped IPv6 addresses to this
+ notation before executing the lookup.)
+
+ One option is supported, "ret=full", to request the return of the entire
+ line rather than omitting the key portion. Note however that the key
+ portion will have been de-quoted.
+
+ * json: The given file is a text file with a JSON structure. An element of
+ the structure is extracted, defined by the search key. The key is a list of
+ subelement selectors (colon-separated by default but changeable in the
+ usual way) which are applied in turn to select smaller and smaller portions
+ of the JSON structure. If a selector is numeric, it must apply to a JSON
+ array; the (zero-based) nunbered array element is selected. Otherwise it
+ must apply to a JSON object; the named element is selected. The final
+ resulting element can be a simple JSON type or a JSON object or array; for
+ the latter two a string-representation of the JSON is returned. For
+ elements of type string, the returned value is de-quoted.
+
+ * lmdb: The given file is an LMDB database. LMDB is a memory-mapped key-value
+ store, with API modeled loosely on that of BerkeleyDB. See https://
+ symas.com/products/lightning-memory-mapped-database/ for the feature set
+ and operation modes.
+
+ Exim provides read-only access via the LMDB C library. The library can be
+ obtained from https://github.com/LMDB/lmdb or your operating system package
+ repository. To enable LMDB support in Exim set LOOKUP_LMDB=yes in Local/
+ Makefile.
+
+ You will need to separately create the LMDB database file, possibly using
+ the "mdb_load" utility.
+
+ * 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.17). 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.
+
+ * spf: If Exim is built with SPF support, manual lookups can be done (as
+ opposed to the standard ACL condition method). For details see section 58.4
+ .
+
+
+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.14.
+
+ * mysql: The format of the query is an SQL statement that is passed to a
+ MySQL database. See section 9.21.
+
+ * nisplus: This does a NIS+ lookup using a query that can specify the name of
+ the field to be returned. See section 9.20.
+
+ * oracle: The format of the query is an SQL statement that is passed to an
+ Oracle database. See section 9.21.
+
+ * 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.21.
+
+ * redis: The format of the query is either a simple get or simple set, passed
+ to a Redis database. See section 9.21.
+
+ * sqlite: The format of the query is an SQL statement that is passed to an
+ SQLite database. See section 9.26.
+
+ * 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.20).
+
+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.
+
+If an option "cache=no_rd" is used on the lookup then the cache is only written
+to, cached data is not used for the operation and a real lookup is done.
+
+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, SOA, SPF, SRV, TLSA
+and TXT, and, when Exim is compiled with IPv6 support, AAAA. If no type is
+given, TXT is assumed.
+
+For any record type, if multiple records are found, 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 lookup types that return multiple fields per record, an
+alternate field separator can be specified using a comma after the main
+separator character, followed immediately by the field separator.
+
+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. The
+field separator can be modified as above.
+
+For TXT records with multiple items of data, only the first item is returned,
+unless a field separator is specified. 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.
+
+For an SOA lookup, while no result is obtained the lookup is redone with
+successively more leading components dropped from the given domain. Only the
+primary-nameserver field is returned unless a field separator is specified.
+
+${lookup dnsdb{>:,; soa=a.b.example.com}}
+
+
+9.11 Dnsdb lookup modifiers
+---------------------------
+
+Modifiers for dnsdb lookups are given 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 "lax".
+
+See also the $lookup_dnssec_authenticated variable.
+
+Timeout for the dnsdb lookup can be controlled by a retrans modifier. The form
+is "retrans_VAL" where VAL is an Exim time specification (e.g. "5s"). The
+default value is set by the main configuration option dns_retrans.
+
+Retries for the dnsdb lookup can be controlled by a retry modifier. The form if
+"retry_VAL" where VAL is an integer. The default count is set by the main
+configuration option dns_retry.
+
+Dnsdb lookup results are cached within a single process (and its children). The
+cache entry lifetime is limited to the smallest time-to-live (TTL) value of the
+set of returned DNS records.
+
+
+9.12 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 44.52.
+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 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.13 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.
+
+
+9.14 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.15 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.16 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.17 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.18 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_default_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 (colon-separated).
+
+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.19 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. Any comma that is part of an
+attribute's value is doubled.
+
+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.
+Any commas in attribute values are doubled (permitting treatment of the values
+as a comma-separated list). 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, one of them with an embedded comma, whereas attr2 has
+only one value. Both attributes are derived from attr (they have SUP attr in
+their schema definitions).
+
+ldap:///o=base?attr1?sub?(uid=fred)
+value1.1,value1,,2
+
+ldap:///o=base?attr2?sub?(uid=fred)
+value two
+
+ldap:///o=base?attr?sub?(uid=fred)
+value1.1,value1,,2,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"
+
+You can make use of Exim's -be option to run expansion tests and thereby check
+the results of LDAP lookups. The extract operator in string expansions can be
+used to pick out individual fields from data that consists of key=value pairs.
+The listextract operator should be used to pick out individual values of
+attributes, even when only a single value is expected. The doubling of embedded
+commas allows you to use the returned data as a comma separated list (using the
+"<," syntax for changing the input list separator).
+
+
+9.20 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.21 SQL lookups
+----------------
+
+Exim can support lookups in InterBase, MySQL, Oracle, PostgreSQL, Redis, 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.22 More about MySQL, PostgreSQL, Oracle, InterBase, and Redis
+---------------------------------------------------------------
+
+If any MySQL, PostgreSQL, Oracle, InterBase or Redis lookups are used, the
+mysql_servers, pgsql_servers, oracle_servers, ibase_servers, or redis_servers
+option (as appropriate) must be set to a colon-separated list of server
+information. (For MySQL and PostgreSQL, the global option need not be set if
+all queries contain their own server information - see section 9.23.) For all
+but Redis 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.
+
+For Redis the global option need not be specified if all queries contain their
+own server information - see section 9.23. If specified, the option must be set
+to a colon-separated list of server information. Each item in the list is a
+slash-separated list of three items: host, database number, and password.
+
+ 1. The host is required and may be either an IPv4 address and optional port
+ number (separated by a colon, which needs doubling due to the higher-level
+ list), or a Unix socket pathname enclosed in parentheses
+
+ 2. The database number is optional; if present that number is selected in the
+ backend
+
+ 3. The password is optional; if present it is used to authenticate to the
+ backend
+
+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_redis expansion operator escapes whitespace and backslash characters
+with a backslash.
+
+
+9.23 Specifying the server in the query
+---------------------------------------
+
+For MySQL, PostgreSQL and Redis 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 appending a comma-separated option to the query type:
+
+,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 ...} }
+
+An older syntax places the servers specification before the query, semicolon
+separated:
+
+${lookup mysql{servers=master; UPDATE ...} }
+
+The new version avoids potential issues with tainted arguments in the query,
+for explicit expansion. Note: server specifications in list-style lookups are
+still problematic.
+
+
+9.24 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. An option group name for
+MySQL option files can be specified in square brackets; the default value is
+"exim". The full syntax of each item in mysql_servers is:
+
+<hostname>::<port>(<socket name>)[<option group>]/<database>/<user>/<password>
+
+Any of the four 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.25 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.26 More about SQLite
+----------------------
+
+SQLite is different to the other SQL lookups because a filename 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.
+
+There are two ways of specifying the file. The first is is by using the
+sqlite_dbfile main option. The second, which allows separate files for each
+query, is to use an option appended, comma-separated, to the "sqlite" lookup
+type word. The option is the word "file", then an equals, then the filename.
+The filename in this case cannot contain whitespace or open-brace charachters.
+
+A deprecated method is available, prefixing the query with the filename
+separated by white space. This means that the query cannot use any tainted
+values, as that taints the entire query including the filename - resulting in a
+refusal to open the file.
+
+In all the above cases the filename must be an absolute path.
+
+Here is a lookup expansion example:
+
+sqlite_dbfile = /some/thing/sqlitedb
+...
+${lookup sqlite {select name from aliases where id='userx';}}
+
+In a list, the syntax is similar. For example:
+
+domainlist relay_to_domains = sqlite;\
+ 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.
+
+
+9.27 More about Redis
+---------------------
+
+Redis is a non-SQL database. Commands are simple get and set. Examples:
+
+${lookup redis{set keyname ${quote_redis:objvalue plus}}}
+${lookup redis{get keyname}}
+
+As of release 4.91, "lightweight" support for Redis Cluster is available.
+Requires redis_servers list to contain all the servers in the cluster, all of
+which must be reachable from the running exim instance. If the cluster has
+master/slave replication, the list must contain all the master and slave
+servers.
+
+When the Redis Cluster returns a "MOVED" response to a query, Exim does not
+immediately follow the redirection but treats the response as a DEFER, moving
+on to the next server in the redis_servers list until the correct server is
+reached.
+
+
+
+===============================================================================
+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 44), 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.
+
+Note that other parts of Exim use a string list which does not support all the
+complexity available in domain, host, address and local part lists.
+
+
+10.1 Expansion of lists
+-----------------------
+
+Each list is expanded as a single string before it is used.
+
+Exception: the router headers_remove option, where list-item splitting is done
+before string-expansion.
+
+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.20 and
+6.22 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
+filename (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
+filenames 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 filename 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 filename 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 filename 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 Results of list checking
+-----------------------------
+
+The primary result of doing a list check is a truth value. In some contexts
+additional information is stored about the list element that matched:
+
+hosts
+
+ A hosts ACL condition will store a result in the $host_data variable.
+
+local_parts
+
+ A local_parts router option or local_parts ACL condition will store a
+ result in the $local_part_data variable.
+
+domains
+
+ A domains router option or domains ACL condition will store a result in the
+ $domain_data variable.
+
+senders
+
+ A senders router option or senders ACL condition will store a result in the
+ $sender_data variable.
+
+recipients
+
+ A recipients ACL condition will store a result in the $recipient_data
+ variable.
+
+The detail of the additional information depends on the type of match and is
+given below as the value information.
+
+
+10.6 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.
+
+Some named list definitions 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 definition with the word
+"hide". For example:
+
+hide domainlist filter_for_domains = ldap;PASS=secret ldap::/// ...
+
+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.7 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.8 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.9 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.
+
+ The value for a match will be the primary host name.
+
+ * 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;
+ see the allow_domain_literals main option.
+
+ The value for a match will be the string "@[]".
+
+ * 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 ? ...
+
+ The value for a match will be the list element string (starting "@mx_").
+
+ * 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.
+
+ The value for a match will be the list element string (starting with the
+ asterisk). Additionally, $0 will be set to the matched string and $1 to the
+ variable portion which the asterisk matched.
+
+ * 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).
+
+ The value for a match will be the list element string (starting with the
+ circumflex). Additionally, $0 will be set to the string matching the
+ regular expression, and $1 (onwards) to any submatches identified by
+ parentheses.
+
+ * 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 filename 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 value resulting from the lookup 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 value is preserved in the
+ $domain_data variable and can be referred to in other router options or
+ other statements in the same ACL. The value will be untainted.
+
+ Note: If the data result of the lookup (as opposed to the key) is empty,
+ then this empty value is stored in $domain_data. The option to return the
+ key for the lookup, as the value, may be what is wanted.
+
+ * 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 value resulting from the lookup 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 value is preserved
+ in the $domain_data variable and can be referred to in other options. The
+ value will be untainted.
+
+ * If the pattern starts with the name of a lookup type of either kind
+ (single-key or query-style) it may be followed by a comma and options, The
+ options are lookup-type specific and consist of a comma-separated list.
+ Each item starts with a tag and and equals "=" sign.
+
+ * If none of the above cases apply, a caseless textual comparison is made
+ between the pattern and the domain.
+
+ The value for a match will be the list element string. Note that this is
+ commonly untainted (depending on the way the list was created).
+ Specifically, explicit text in the configuration file in not tainted. This
+ is a useful way of obtaining an untainted equivalent to the domain, for
+ later operations.
+
+ However if the list (including one-element lists) is created by expanding a
+ variable containing tainted data, it is tainted and so will the match value
+ be.
+
+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.10 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.11 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.12 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.15 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.13 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 except IPv4-mapped IPv6, full, unabbreviated IPv6
+addresses are always used. The latter are converted to IPv4 addresses, in
+dotted-quad form.
+
+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.14 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.12 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.15 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.15 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.12), or it may need to look up a host name from an IP
+address (see section 10.14). 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.16 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
+ 44 for details of ACLs. Alternatively, you can use "+ignore_unknown", which
+ was discussed in depth in the first example in this section.
+
+
+10.17 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", analogous 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.18 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.13). 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.19 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.14 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.13.)
+
+
+10.20 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.21 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 filenames, 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.22 Local part lists
+----------------------
+
+These behave in the same way as domain and host lists, with the following
+changes:
+
+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.9 for details of the
+other available item types.
+
+
+
+===============================================================================
+11. STRING EXPANSIONS
+
+Many strings in Exim's runtime 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, and
+expansion of data deriving from the sender ("tainted data") is not permitted
+(including acessing a file using a tainted name).
+
+Common ways of obtaining untainted equivalents of variables with tainted values
+come down to using the tainted value as a lookup key in a trusted database.
+This database could be the filesystem structure, or the password file, or
+accessed via a DBMS. Specific methods are indexed under "de-tainting".
+
+
+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.17).
+
+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 filename. 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 44) 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.
+
+${authresults{<authserv-id>}}
+
+ This item returns a string suitable for insertion as an
+ Authentication-Results: header line. The given <authserv-id> is included in
+ the result; typically this will be a domain name identifying the system
+ performing the authentications. Methods that might be present in the result
+ include:
+
+ none
+ iprev
+ auth
+ spf
+ dkim
+
+ Example use (as an ACL modifier):
+
+ add_header = :at_start:${authresults {$primary_hostname}}
+
+ This is safe even if no authentication results are available.
+
+${certextract{<field>}{<certificate>}{<string2>}{<string3>}}
+
+ The <certificate> must be a variable of type certificate. The field name is
+ expanded and used to retrieve 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 containing 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 modifier 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 take an optional modifier of
+ "int" for which the result is the number of seconds since epoch. Otherwise
+ the result is a human-readable string in the timezone selected by the main
+ "timezone" option.
+
+ 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 element
+ 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, first
+ DLFUNC_IMPL should be defined, and second 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.
+
+${env{<key>}{<string1>}{<string2>}}
+
+ The key is first expanded separately, and leading and trailing white space
+ removed. This is then searched for as a name in the environment. If a
+ variable is found then its value is placed in $value and <string1> is
+ expanded, otherwise <string2> is expanded.
+
+ Instead of {<string2>} the word "fail" (not in curly brackets) can appear,
+ for example:
+
+ ${env{USER}{$value} fail }
+
+ This forces an expansion failure (see section 11.4); {<string1>} must be
+ present for "fail" to be recognized.
+
+ If {<string2>} is omitted an empty string is substituted on search failure.
+ If {<string1>} is omitted the search result is substituted on search
+ success.
+
+ The environment is adjusted by the keep_environment and add_environment
+ main section options.
+
+${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 be empty and 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.17. 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 json{<key>}{<string1>}{<string2>}{<string3>}}, ${extract jsons{<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 be empty and must not consist entirely of digits. The expanded
+ <string1> must be of the form:
+
+ { <"key1"> : <value1> , <"key2"> , <value2> ... }
+
+ The braces, commas and colons, and the quoting of the member name are
+ required; the spaces are optional. Matching of the key against the member
+ names is done case-sensitively. For the "json" variant, if a returned value
+ is a JSON string, it retains its leading and trailing quotes. For the
+ "jsons" variant, which is intended for use with JSON strings, the leading
+ and trailing quotes are removed from the returned value.
+
+ The results of matching are handled as above.
+
+${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).
+
+${extract json {<number>}}{<string1>}{<string2>}{<string3>}}, ${extract jsons{<
+number>}}{<string1>}{<string2>}{<string3>}}
+
+ The <number> argument must consist entirely of decimal digits, apart from
+ leading and trailing white space, which is ignored.
+
+ Field selection and result handling is as above; there is no choice of
+ field separator. For the "json" variant, if a returned value is a JSON
+ string, it retains its leading and trailing quotes. For the "jsons"
+ variant, which is intended for use with JSON strings, the leading and
+ trailing quotes are removed from the returned value.
+
+${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 (6.21). 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>:, $lheader_<header name>: or $lh_<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 the four pairs of expansions is in the way the data
+ in the header line is interpreted.
+
+ o 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.
+
+ o lheader gives a colon-separated list, one element per header when there
+ are multiple headers with a given name. Any embedded colon characters
+ within an element are doubled, so normal Exim list-processing
+ facilities can be used. The terminating newline of each element is
+ removed; in other respects the content is "raw".
+
+ o 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.
+
+ o 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 phase completes, because the header structure is not
+ set up until the message is received. They are visible in DKIM, PRDR and
+ DATA ACLs. 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 any of the above ACLs are 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, this white space 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.
+
+ When the headers are from an incoming message, the result of expanding any
+ of these variables is tainted.
+
+${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}}
+
+${imapfolder{<foldername>}}
+
+ This item converts a (possibly multilevel, or with non-ASCII characters)
+ folder specification to a Maildir name for filesystem use. For information
+ on internationalisation support see 60.2.
+
+${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> bytes or the whole of <
+ string2>, whichever is the shorter. Do not confuse length with strlen,
+ which gives the length of a string. All measurement is done in bytes and is
+ not UTF-8 aware.
+
+${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 (6.21).
+
+ 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: 42".
+
+ 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.
+
+${listquote{<separator>}{<string>}}
+
+ This item doubles any occurrence of the separator character in the given
+ string. An empty string is replaced with a single space. This converts the
+ string into a safe form for use as a list element, in a list using the
+ given separator.
+
+${lookup {<key>} <search type> {<file>} {<string1>} {<string2>}}, ${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 (6.21). 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 entire expansion is
+ forced to fail, in the same way as an explicit "fail" on a lookup item does
+ (see section 11.4). Whatever you return is evaluated in a scalar context,
+ thus the return value is a scalar. For example, if you return 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 44.53.
+
+${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 44.53.
+
+${readfile{<file name>}{<eol string>}}
+
+ The filename and end-of-line (eol) 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>}{<options>}{<eol string>}{<fail string>}}
+
+ This item inserts data from a Unix domain or TCP 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; no terminating NUL is ever sent) 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}}
+
+ The third argument is a list of options, of which the first element is the
+ timeout and must be present if any options are given. Further elements are
+ options of form name=value. Example:
+
+ ${readsocket{/socket/name}{request string}{3s:shutdown=no}}
+
+ The following option names are recognised:
+
+ o cache Defines if the result data can be cached for use by a later
+ identical request in the same process. Values are "yes" or "no" (the
+ default). If not, all cached results for this connection specification
+ will be invalidated.
+
+ o shutdown Defines whether or not a write-shutdown is done on the
+ connection after sending the request. Values are "yes" (the default) or
+ "no" (preferred, eg. by some webservers).
+
+ o tls Controls the use of TLS on the connection. Values are "yes" or "no"
+ (the default). If it is enabled, a shutdown as described above is never
+ done.
+
+ 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:
+
+ o Failure to create a socket file descriptor;
+
+ o Failure to connect the socket;
+
+ o Failure to write the request string;
+
+ o 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 (6.21). 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 in section 11.5 above.
+
+${run <options> {<command arg list>}{<string1>}{<string2>}}
+
+ This item runs an external command, as a subprocess.
+
+ One option is supported after the word run, comma-separated.
+
+ If the option preexpand is not used, the command string is split into
+ individual arguments by spaces and then each argument is expanded. 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. The command name may
+ not be tainted, but the remaining arguments can be.
+
+ Note: if tainted arguments are used, they are supplied by a potential
+ attacker; a careful assessment for security vulnerabilities should be done.
+
+ If the option preexpand is used,
+
+ the command and its arguments are first expanded as one string. The result
+ is split apart into individual arguments by spaces, and then the command is
+ run as above. 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.
+
+ Neither the command nor any argument may be tainted.
+
+ 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.
+
+ The regular expression is compiled in 8-bit mode, working against bytes
+ rather than any Unicode-aware character handling.
+
+${sort{<string>}{<comparator>}{<extractor>}}
+
+ After expansion, <string> is interpreted as a list, colon-separated by
+ default, but the separator can be changed in the usual way (6.21). The <
+ comparator> argument is interpreted as the operator of a two-argument
+ expansion condition. The numeric operators plus ge, gt, le, lt (and ~i
+ variants) are supported. The comparison should return true when applied to
+ two values if the first value should sort before the second value. The <
+ extractor> expansion is applied repeatedly to elements of the list, the
+ element being placed in $item, to give values for comparison.
+
+ The item result is a sorted list, with the original list separator, of the
+ list elements (in full) of the original.
+
+ Examples:
+
+ ${sort{3:2:1:4}{<}{$item}}
+
+ sorts a list of numbers, and
+
+ ${sort {${lookup dnsdb{>:,,mx=example.com}}} {<} {${listextract{1}{<,$item}}}}
+
+ will sort an MX lookup into priority order.
+
+${srs_encode {<secret>}{<return path>}{<original domain>}}
+
+ SRS encoding. See SECT 58.5 for details.
+
+${substr{<start>}{<len>}{<subject>}}
+
+ 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 <
+ start> and <len> do not change when they are expanded, you can use the
+ simpler operator notation that avoids some of the braces:
+
+ ${substr_<n>_<m>:<subject>}
+
+ 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 byte (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 byte (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 bytes
+ (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".
+
+ All measurement is done in bytes and is not UTF-8 aware.
+
+${tr{<subject>}{<characters>}{<replacements>}}
+
+ This item does single-character (in bytes) 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.
+
+ All character handling is done in bytes and is not UTF-8 aware.
+
+
+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.
+
+ The parsing correctly handles SMTPUTF8 Unicode in the string.
+
+${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". The string is expanded first, so
+ if the expanded string starts with >, it may change the output separator
+ unintentionally. This can be avoided by setting the output separator
+ explicitly:
+
+ ${addresses:>:$h_from:}
+
+ 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 separator. 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. The fourth example shows SMTPUTF8 handling.
+
+ # 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
+ # exim -be '${addresses:?????? <??????????@example.jp>}'
+ ??????????@example.jp
+
+${base32:<digits>}
+
+ The string must consist entirely of decimal digits. The number is converted
+ to base 32 and output as a (empty, for zero) string of characters. Only
+ lowercase letters are used.
+
+${base32d:<base-32 digits>}
+
+ The string must consist entirely of base-32 digits. The number is converted
+ to decimal and output as a string.
+
+${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 filenames), 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.
+
+${base64:<string>}
+
+ This operator converts a string into one that is base64 encoded.
+
+ If the string is a single variable of type certificate, returns the base64
+ encoding of the DER form of the certificate.
+
+${base64d:<string>}
+
+ This operator converts a base64-encoded string into the un-coded form.
+
+${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.
+
+${escape8bit:<string>}
+
+ If the string contains any characters with the most significant bit set,
+ they are converted to escape sequences starting with a backslash.
+ Backslashes and DEL characters are also converted.
+
+${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 condition = \
+ ${if and { \
+ {>{$rcpt_count}{10}} \
+ { \
+ < \
+ {$recipients_count} \
+ {${eval:$rcpt_count/2}} \
+ } \
+ }{yes}{no}}
+ message = Too many bad recipients
+
+ 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 various 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".
+
+${ipv6denorm:<string>}
+
+ This expands an IPv6 address to a full eight-element colon-separated set of
+ hex digits including leading zeroes. A trailing ipv4-style dotted-decimal
+ set is converted to hex. Pure IPv4 addresses are converted to IPv4-mapped
+ IPv6.
+
+${ipv6norm:<string>}
+
+ This converts an IPv6 address to canonical form. Leading zeroes of groups
+ are omitted, and the longest set of zero-valued groups is replaced with a
+ double colon. A trailing ipv4-style dotted-decimal set is converted to hex.
+ Pure IPv4 addresses are converted to IPv4-mapped IPv6.
+
+${lc:<string>}
+
+ This forces the letters in the string into lower-case, for example:
+
+ ${lc:$local_part}
+
+ Case is defined per the system C locale.
+
+${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. All measurement is done in bytes and is not
+ UTF-8 aware.
+
+${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. Note: Neither
+ string-expansion of lists referenced by named-list syntax elements, nor
+ expansion of lookup elements, is done by the listnamed operator.
+
+${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. The parsing correctly handles SMTPUTF8 Unicode in the string.
+
+${mask:<IP address>/<bit count>}, ${mask_n:<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
+
+ normal
+
+ 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
+
+ If the optional form mask_n is used, IPv6 address result are instead
+ returned in normailsed form, using colons and with zero-compression.
+
+ 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.
+
+ If the string is a single variable of type certificate, returns the MD5
+ hash fingerprint of the certificate.
+
+${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.
+
+ This quoting determination is not SMTPUTF8-aware, thus quoting non-ASCII
+ data will likely use the quoting form. Thus ${quote_local_part:??????} will
+ always become "??????".
+
+${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 addresses 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 gets its default at build time. 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.
+
+ If the string is a single variable of type certificate, returns the SHA-1
+ hash fingerprint of the certificate.
+
+${sha256:<string>}, ${sha2:<string>}, ${sha2_<n>:<string>}
+
+ The sha256 operator computes the SHA-256 hash value of the string and
+ returns it as a 64-digit hexadecimal number, in which any letters are in
+ upper case.
+
+ If the string is a single variable of type certificate, returns the SHA-256
+ hash fingerprint of the certificate.
+
+ The operator can also be spelled sha2 and does the same as sha256 (except
+ for certificates, which are not supported). Finally, if an underbar and a
+ number is appended it specifies the output length, selecting a member of
+ the SHA-2 family of hash functions. Values of 256, 384 and 512 are
+ accepted, with 256 being the default.
+
+${sha3:<string>}, ${sha3_<n>:<string>}
+
+ The sha3 operator computes the SHA3-256 hash value of the string and
+ returns it as a 64-digit hexadecimal number, in which any letters are in
+ upper case.
+
+ If a number is appended, separated by an underbar, it specifies the output
+ length. Values of 224, 256, 384 and 512 are accepted; with 256 being the
+ default.
+
+ The sha3 expansion item is only supported if Exim has been compiled with
+ GnuTLS 3.5.0 or later, or OpenSSL 1.1.1 or later. The macro
+ "_CRYPTO_HASH_SHA3" will be defined if it is 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>}
+
+ Now deprecated, a synonym for the base64 expansion operator.
+
+${strlen:<string>}
+
+ The item is replaced by the length of the expanded string, expressed as a
+ decimal number. Note: Do not confuse strlen with length. All measurement is
+ done in bytes and is not UTF-8 aware.
+
+${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. All
+ measurement is done in bytes and is not UTF-8 aware.
+
+${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. Case is defined per
+ the system C locale.
+
+${utf8clean:<string>}
+
+ This replaces any invalid utf-8 sequence in the string by the character "?
+ ". In versions of Exim before 4.92, this did not correctly do so for a
+ truncated final codepoint's encoding, and the character would be silently
+ dropped. If you must handle detection of this scenario across both sets of
+ Exim behavior, the complexity will depend upon the task. For instance, to
+ detect if the first character is multibyte and a 1-byte extraction can be
+ successfully used as a path component (as is common for dividing up
+ delivery folders), you might use:
+
+ condition = ${if inlist{${utf8clean:${length_1:$local_part}}}{:?}{yes}{no}}
+
+ (which will false-positive if the first character of the local part is a
+ literal question mark).
+
+${utf8_domain_to_alabel:<string>}, ${utf8_domain_from_alabel:<string>}, $
+{utf8_localpart_to_alabel:<string>}, ${utf8_localpart_from_alabel:<string>}
+
+ These convert EAI mail name components between UTF-8 and a-label forms. For
+ information on internationalisation support see 60.1.
+
+
+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 44) 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:
+
+ o {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.
+
+ o {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.
+
+ o {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.
+
+ o {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, where case is
+ defined per the system C locale.
+
+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.
+
+ Note: Testing a path using this condition is not a sufficient way of
+ de-tainting it. Consider using a dsearch lookup.
+
+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 (6.21). 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.
+
+ o 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.
+
+ o 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.
+
+forall_json{<a JSON array>}{<a condition>}, forany_json{<a JSON array>}{<a
+condition>}, forall_jsons{<a JSON array>}{<a condition>}, forany_jsons{<a JSON
+array>}{<a condition>}
+
+ As for the above, except that the first argument must, after expansion, be
+ a JSON array. The array separator is not changeable. For the "jsons"
+ variants the elements are expected to be JSON strings and have their quotes
+ removed before the evaluation of the condition.
+
+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. Case and collation order are defined per the system C
+ locale.
+
+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. Case and collation order are defined per the system C
+ locale.
+
+inbound_srs {<local part>}{<secret>}
+
+ SRS decode. See SECT 58.5 for details.
+
+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. For the case-independent inlisti condition, case is defined per
+ the system C locale.
+
+ 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}}}
+
+ The variable $value will be set for a successful match and can be used in
+ the success clause of an if expansion item using the condition. It will
+ have the same taint status as the list; expansions such as
+
+ ${if inlist {$h_mycode:} {0 : 1 : 42} {$value}}
+
+ can be used for de-tainting. Any previous $value is restored after the if.
+
+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 used to be just on the form of the address; actual
+ numerical values were not considered. Thus, for example, 999.999.999.999
+ passed the IPv4 check. This is no longer the case.
+
+ 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.14
+ 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. Case and collation order are defined per the system C
+ locale.
+
+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. Case and collation order are defined per the system C
+ locale.
+
+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. All character handling is done in bytes and is not
+ UTF-8 aware, but we might change this in a future Exim release.
+
+ 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:
+
+ o An IP address, optionally with a CIDR mask.
+
+ o A single asterisk, which matches any IP address.
+
+ o 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.
+
+ o The item @[] matches any of the local host's interface addresses.
+
+ o 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.12 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 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.
+
+ The variable $value will be set for a successful match and can be used in
+ the success clause of an if expansion item using the condition. It will
+ have the same taint status as the list; expansions such as
+
+ ${if match_local_part {$local_part} {alice : bill : charlotte : dave} {$value}}
+
+ can be used for de-tainting. Any previous $value is restored after the if.
+
+ 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 (https://mirrors.edge.kernel.org/pub/linux
+ /libs/pam/) are a facility that is available in 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. The listquote expansion item can be used for this. For
+ example, the configuration of a LOGIN authenticator might contain this
+ setting:
+
+ server_condition = ${if pam{$auth1:${listquote{:}{$auth2}}}}
+
+ 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.
+
+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.
+
+Variables marked as tainted are likely to carry data supplied by a potential
+attacker. Variables without such marking may also, depending on how their
+values are created. Such variables should not be further expanded, used as
+filenames or used as command-line arguments for external commands.
+
+$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. In
+ the expansion condition case 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.
+
+ If the subject string was tainted then any captured substring will also be.
+
+$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 of
+ the verb. 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. Note: The variable is cleared at the end of processing the ACL
+ verb.
+
+$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 - $auth4
+
+ These variables are used in SMTP authenticators (see chapters 34-42).
+ 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. This second case also sets up information used by the
+ $authresults expansion item.
+
+$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
+
+ Tainted
+
+ 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
+ cancellation of a authentication attempt, and 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 50).
+
+$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 50).
+
+$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.
+
+$callout_address
+
+ After a callout for verification, spamd or malware daemon service, the
+ address that was connected to.
+
+$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 Exim.
+
+$config_dir
+
+ The directory name of the main configuration file. That is, the content of
+ $config_file with the last component stripped. The value does not contain
+ the trailing slash. If $config_file does not contain a slash, $config_dir
+ is ".".
+
+$config_file
+
+ The name of the main configuration file Exim is using.
+
+$dkim_verify_status
+
+ Results of DKIM verification. For details see section 58.3.
+
+$dkim_cur_signer, $dkim_verify_reason, $dkim_domain, $dkim_identity,
+$dkim_selector, $dkim_algo, $dkim_canon_body, $dkim_canon_headers,
+$dkim_copiedheaders, $dkim_bodylength, $dkim_created, $dkim_expires,
+$dkim_headernames, $dkim_key_testing, $dkim_key_nosubdomains, $dkim_key_srvtype
+, $dkim_key_granularity, $dkim_key_notes, $dkim_key_length
+
+ These variables are only available within the DKIM ACL. For details see
+ section 58.3.
+
+$dkim_signers
+
+ When a message has been received this variable contains a colon-separated
+ list of signer domains and identities for the message. For details see
+ section 58.3.
+
+$dmarc_domain_policy, $dmarc_status, $dmarc_status_text, $dmarc_used_domains
+
+ Results of DMARC verification. For details see section 58.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 44.32 for more details.
+
+$domain
+
+ Tainted
+
+ 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:
+
+ o 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.
+
+ o 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.
+
+ o 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).
+
+ o When the smtp_etrn_command option is being expanded, $domain contains
+ the complete argument of the ETRN command (see section 49.8).
+
+ If the origin of the data is an incoming message, the result of expanding
+ this variable is tainted and may not be further expanded or used as a
+ filename. When an untainted version is needed, one should be obtained from
+ looking up the value in a local (therefore trusted) database. Often
+ $domain_data is usable in this role.
+
+$domain_data
+
+ When the domains condition on a router or an ACL matches a domain against a
+ list, the match value is copied to $domain_data. This is an enhancement
+ over previous versions of Exim, when it only applied to the data read by a
+ lookup. For details on match values see section 10.5 et. al.
+
+ If the router 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 set in an ACL is available during the rest of the ACL
+ statement.
+
+$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.
+
+$exim_version
+
+ This variable contains the version string of the Exim build. The first
+ character is a major version number, currently 4. Then after a dot, the
+ next group of digits is a minor version number. There may be other
+ characters following the minor version. This value may be overridden by the
+ exim_version main config option.
+
+$header_<name>
+
+ Tainted
+
+ 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. See the full
+ description in section 11.5 above.
+
+$headers_added
+
+ Within an ACL this variable contains the headers added so far by the ACL
+ modifier add_header (section 44.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, which is subject to the keep_environment and
+ add_environment main config options.
+
+$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".
+
+ o 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".
+
+ o 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.
+
+ Performing these checks sets up information used by the authresults
+ expansion item.
+
+$host_lookup_failed
+
+ See $host_lookup_deferred.
+
+$host_port
+
+ This variable is set to the remote host's TCP port whenever $host is set
+ for an outbound connection.
+
+$initial_cwd
+
+ This variable contains the full path name of the initial working directory
+ of the current Exim process. This may differ from the current working
+ directory, as Exim changes this to "/" during early startup, and to
+ $spool_directory later.
+
+$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, $interface_port
+
+ These are obsolete names for $received_ip_address and $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
+
+ Tainted
+
+ 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 the origin of the data is an incoming message, the result of expanding
+ this variable is tainted and may not be further expanded or used as a
+ filename.
+
+ Warning: the content of this variable is usually provided by a potential
+ attacker. Consider carefully the implications of using it unvalidated as a
+ name for file access. This presents issues for users' .forward and filter
+ files. For traditional full user accounts, use check_local_users and the
+ $local_part_data variable rather than this one. For virtual users, store a
+ suitable pathname component in the database which is used for account name
+ validation, and use that retrieved value rather than this variable. Often
+ $local_part_data is usable in this role. If needed, use a router
+ address_data or set option for the retrieved data.
+
+ 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 filename 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 condition on a router or ACL matches a local part list
+ the match value is copied to $local_part_data. This is an enhancement over
+ previous versions of Exim, when it only applied to the data read by a
+ lookup. For details on match values see section 10.5 et. al.
+
+ The check_local_user router option also sets this variable.
+
+ 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. If the specification did not include a wildcard then the
+ affix variable value is not tainted.
+
+ If the affix specification included a wildcard then the portion of the
+ affix matched by the wildcard is in $local_part_prefix_v or
+ $local_part_suffix_v as appropriate, and both the whole and varying values
+ are tainted.
+
+$local_scan_data
+
+ This variable contains the text returned by the local_scan() function when
+ a message is received. See chapter 46 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. Results that are labelled as authoritative answer that
+ match the dns_trust_aa configuration variable count also as authenticated
+ data.
+
+$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 45.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). It is not valid if the spool_wireformat option is used.
+
+$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
+
+ Tainted
+
+ 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
+
+ Tainted
+
+ 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.
+
+ If the spool file is wireformat (see the spool_wireformat main option) the
+ CRLF line-terminators are included in the count.
+
+$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
+
+ Tainted
+
+ 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
+
+ Tainted
+
+ 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. It 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 condition = \
+ ${if <{250}{${eval:$message_linecount - $body_linecount}}}
+ message = Too many lines in message header
+
+ In the MAIL and RCPT ACLs, the value is zero because at that stage the
+ message has not yet been received.
+
+ This variable is not valid if the spool_wireformat option is used.
+
+$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_anomaly_level, $mime_anomaly_text, $mime_boundary, $mime_charset,
+$mime_content_description, $mime_content_disposition, $mime_content_id,
+$mime_content_size, $mime_content_transfer_encoding, $mime_content_type,
+$mime_decoded_filename, $mime_filename, $mime_is_coverletter,
+$mime_is_multipart, $mime_is_rfc822, $mime_part_count
+
+ A number of variables whose names start with $mime are available when Exim
+ is compiled with the content-scanning extension. For details, see section
+ 45.4.
+
+$n0 - $n9
+
+ These variables are counters that can be incremented by means of the add
+ command in filter files.
+
+$original_domain
+
+ Tainted
+
+ 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
+
+ Tainted
+
+ 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
+
+ Tainted
+
+ This variable is similar to $original_domain (see above), except that it
+ refers to the immediately preceding parent address.
+
+$parent_local_part
+
+ Tainted
+
+ 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.
+
+ Note: This value permits data supplied by a potential attacker to be used
+ in the command for a pipe transport. Such configurations should be
+ carefully assessed for security vulnerbilities.
+
+$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.
+
+$proxy_external_address, $proxy_external_port, $proxy_local_address,
+$proxy_local_port, $proxy_session
+
+ These variables are only available when built with Proxy Protocol or SOCKS5
+ support. For details see chapter 59.1.
+
+$prdr_requested
+
+ This variable is set to "yes" if PRDR was requested by the client for the
+ current message, otherwise "no".
+
+$prvscheck_address, $prvscheck_keynum, $prvscheck_result
+
+ These variables are used in conjunction with the prvscheck expansion item,
+ which is described in sections 11.5 and 44.53.
+
+$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.
+
+$queue_name
+
+ The name of the spool queue in use; empty for the default queue.
+
+$queue_size
+
+ This variable contains the number of messages queued. It is evaluated on
+ demand, but no more often than once every minute. If there is no daemon
+ notifier socket open, the value will be an empty string.
+
+$r_...
+
+ Values can be placed in these variables by the set option of a router. They
+ can be given any name that starts with $r_. The values persist for the
+ address being handled through subsequent routers and the eventual
+ transport.
+
+$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
+
+ Tainted
+
+ 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, $received_port
+
+ As soon as an Exim server starts processing an incoming TCP/IP connection,
+ these variables are set to the address and port on the local IP interface.
+ (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 filename 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. For outbound connections see
+ $sending_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:
+
+ o "qualify": The address was unqualified (no domain), and the message was
+ neither local nor came from an exempted host.
+
+ o "route": Routing failed.
+
+ o "mail": Routing succeeded, and a callout was attempted; rejection
+ occurred at or before the MAIL command (that is, on initial connection,
+ HELO, or MAIL).
+
+ o "recipient": The RCPT command in a callout was rejected.
+
+ o "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
+
+ Tainted
+
+ 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 45.5).
+
+$regex1, $regex2, etc
+
+ When a regex or mime_regex ACL condition succeeds, these variables contain
+ the captured substrings identified by the regular expression.
+
+ If the subject string was tainted then so will any captured substring.
+
+$reply_address
+
+ Tainted
+
+ 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
+
+ Tainted
+
+ 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
+
+ Tainted
+
+ The domain portion of $sender_address.
+
+$sender_address_local_part
+
+ Tainted
+
+ 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_dnssec
+
+ This boolean variable is true if a successful HELO verification was done
+ using DNS information the resolver library stated was authenticated data.
+
+$sender_helo_name
+
+ Tainted
+
+ 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 using SMTP, this variable
+ contains that host's IP address. For locally non-SMTP 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 both the reverse and forward
+ DNS were 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
+
+ In addition, on Linux with glibc 2.31 or newer the resolver library will
+ default to stripping out a successful validation status. This will break a
+ previously working Exim installation. Provided that you do trust the
+ resolver (ie, is on localhost) you can tell glibc to pass through any
+ successful validation with a new option in /etc/resolv.conf:
+
+ options trust-ad
+
+ Exim does not perform DNSSEC validation itself, instead leaving that to a
+ validating resolver (e.g. unbound, or bind with suitable configuration).
+
+ If you have changed host_lookup_order so that "bydns" is not the first
+ mechanism in the list, then this variable will be false.
+
+ This requires that your system resolver library support EDNS0 (and that
+ DNSSEC flags exist in the system headers). If the resolver silently drops
+ all EDNS0 options, then this will have no effect. OpenBSD's asr resolver is
+ known to currently ignore EDNS0, documented in CAVEATS of asr_run(3).
+
+$sender_host_name
+
+ Tainted
+
+ 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:
+
+ o A string containing $sender_host_name is expanded.
+
+ o 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.)
+
+ o 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.14 and 10.18.
+
+ o 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.
+
+ o 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 44.39.
+
+$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
+
+ Tainted
+
+ 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
+
+ Tainted
+
+ 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_command_history
+
+ A comma-separated list (with no whitespace) of the most-recent SMTP
+ commands received, in time-order left to right. Only a limited number of
+ commands are remembered.
+
+$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_score, $spam_score_int, $spam_bar, $spam_report, $spam_action
+
+ A number of variables whose names start with $spam are available when Exim
+ is compiled with the content-scanning extension. For details, see section
+ 45.2.
+
+$spf_header_comment, $spf_received, $spf_result, $spf_result_guessed,
+$spf_smtp_comment
+
+ These variables are only available if Exim is built with SPF support. For
+ details see section 58.4.
+
+$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, sha1 or sha256 operator, or a def
+ condition.
+
+ Note: Under versions of OpenSSL preceding 1.1.1, when a list of more than
+ one file is used for tls_certificate, this variable is not reliable. The
+ macro "_TLS_BAD_MULTICERT_IN_OURCERT" will be defined for those versions.
+
+$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, sha1 or sha256 operator, or a def
+ condition. If certificate verification fails it may refer to a failing
+ chain element which is not the leaf.
+
+$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, sha1 or sha256 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, sha1 or sha256 operator, or a def condition. If
+ certificate verification fails it may refer to a failing chain element
+ which is not the leaf.
+
+$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_verified 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_in_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_in_cipher_std
+
+ As above, but returning the RFC standard name for the cipher suite.
+
+$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 43
+ for details of TLS support and chapter 30 for details of the smtp
+ transport.
+
+$tls_out_cipher_std
+
+ As above, but returning the RFC standard name for the cipher suite.
+
+$tls_out_dane
+
+ DANE active status. See section 43.18.
+
+$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. If
+ certificate verification fails it may refer to a failing chain element
+ which is not the leaf.
+
+ 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. If
+ certificate verification fails it may refer to a failing chain element
+ which is not the leaf.
+
+$tls_in_resumption, $tls_out_resumption
+
+ Observability for TLS session resumption. See 43.17 for details.
+
+$tls_in_sni
+
+ Tainted
+
+ 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 43.12, 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.
+
+$tls_out_tlsa_usage
+
+ Bitfield of TLSA record types found. See section 43.18.
+
+$tls_in_ver
+
+ When a message is received from a remote host over an encrypted SMTP
+ connection this variable is set to the protocol version, eg TLS1.2.
+
+$tls_out_ver
+
+ When a message is being delivered to a remote host over an encrypted SMTP
+ connection this variable is set to the protocol version.
+
+$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.
+
+$verify_mode
+
+ While a router or transport is being run in verify mode or for cutthrough
+ delivery, contains "S" for sender-verification or "R" for
+ recipient-verification. Otherwise, empty.
+
+$version_number
+
+ The version number of Exim. Same as $exim_version, may be overridden by the
+ exim_version main config option.
+
+$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 50.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 50.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.
+
+ * To provide more security executing Perl code via the embedded Perl
+ interpreter, the perl_taintmode option can be set. This enables the taint
+ mode of the Perl interpreter. You are encouraged to set this option to a
+ true value. To avoid breaking existing installations, it defaults to false.
+
+ Note: This is entirely separate from Exim's tainted-data tracking.
+
+
+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.21. 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 (6.21) 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 submissions (aka SSMTP or SMTPS) protocol
+--------------------------------------------------------------
+
+Exim supports the use of TLS-on-connect, used by mail clients in the
+"submissions" protocol, historically also known as SMTPS or SSMTP. For some
+years, IETF Standards Track documents only blessed the STARTTLS-based
+Submission service (port 587) while common practice was to support the same
+feature set on port 465, but using TLS-on-connect. If your installation needs
+to provide service to mail clients (Mail User Agents, MUAs) then you should
+provide service on both the 587 and the 465 TCP ports.
+
+If the tls_on_connect_ports option is set to a list of port numbers or service
+names, connections to those ports must first establish TLS, before proceeding
+to the application layer use of the SMTP protocol.
+
+The common use of this option is expected to be
+
+tls_on_connect_ports = 465
+
+per RFC 8314. 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 runtime 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.6.
+
+ * 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.11 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
+------------------
+
+add_environment environment variables
+bi_command to run for -bi command line option
+debug_store do extra internal checks
+disable_ipv6 do no IPv6 processing
+keep_environment environment variables
+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
+spool_wireformat use wire-format spool data files when possible
+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
+commandline_checks_require_admin require admin for various checks
+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
+------------
+
+event_action custom 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
+slow_lookup_log control logging of slow DNS lookups
+syslog_duplication controls duplicate log lines on syslog
+syslog_facility set syslog "facility" field
+syslog_pid pid in syslog lines
+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
+perl_taintmode enable taint mode in 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
+notifier_socket override compiled-in value
+pid_file_path override compiled-in value
+queue_run_max maximum simultaneous queue runners
+smtp_backlog_monitor level to log listen backlog
+
+
+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_notquit ACL for non-QUIT terminations
+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_cname_loops follow CNAMEs returned by resolver
+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
+hosts_proxy use proxy protocol for these hosts
+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
+proxy_protocol_timeout timeout for proxy protocol negotiation
+spamd_address set interface to SpamAssassin
+strict_acl_vars object to unset ACL variables
+spf_smtp_comment_template template for $spf_smtp_comment
+
+
+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
+hosts_require_alpn mandatory ALPN
+hosts_require_helo mandatory HELO/EHLO
+openssl_options adjust OpenSSL compatibility options
+tls_advertise_hosts advertise TLS to these hosts
+tls_alpn acceptable protocol names
+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_eccurve EC curve selection 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.
+
+dkim_verify_hashes DKIM hash methods accepted for signatures
+dkim_verify_keytypes DKIM key types accepted for signatures
+dkim_verify_min_keysizes DKIM key sizes accepted for signatures
+dkim_verify_signers DKIM domains for which DKIM ACL is run
+dmarc_forensic_sender DMARC sender for report messages
+dmarc_history_file DMARC results log
+dmarc_tld_file DMARC toplevel domains file
+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
+chunking_advertise_hosts advertise CHUNKING to these hosts
+dsn_advertise_hosts advertise DSN extensions 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
+pipelining_connect_advertise_hosts advertise pipelining to these hosts
+prdr_enable advertise PRDR to all hosts
+smtputf8_advertise_hosts advertise SMTPUTF8 to these 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_trust_aa DNS zones trusted as authentic
+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_fast_ramp parallel delivery with 2-phase queue run
+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_linesize_limit limit on returned message line length
+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:
+
+https://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 44 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 44 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 44 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 44 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 44 for further details.
+
++----------------------------------------------------------+
+|acl_smtp_data_prdr|Use: main|Type: string*|Default: accept|
++----------------------------------------------------------+
+
+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 44 for further details.
+
++----------------------------------------------------+
+|acl_smtp_dkim|Use: main|Type: string*|Default: unset|
++----------------------------------------------------+
+
+This option defines the ACL that is run for each DKIM signature (by default, or
+as specified in the dkim_verify_signers option) of a received message. See
+section 58.3 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 44 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 44 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 44 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 44 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 44 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 45.4 for details.
+
++-------------------------------------------------------+
+|acl_smtp_notquit|Use: main|Type: string*|Default: unset|
++-------------------------------------------------------+
+
+This option defines the ACL that is run when an SMTP session ends without a
+QUIT command being received. See chapter 44 for further 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 44 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 44 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 44 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 44 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 44 for further details.
+
++----------------------------------------------------------+
+|add_environment|Use: main|Type: string list|Default: empty|
++----------------------------------------------------------+
+
+This option adds individual environment variables that the currently linked
+libraries and programs in child processes may use. Each list element should be
+of the form "name=value".
+
+See 29.4 for the environment of pipe transports.
+
++--------------------------------------------------------+
+|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 misconfiguration. 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.
+
+If Exim is built with internationalization support and the SMTPUTF8 ESMTP
+option is in use (see chapter 60) this option can be left as default. Without
+that, if you want to look up such 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 45.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 50. The option is expanded to give the file path, which must be
+absolute and untainted. 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_linesize_limit|Use: main|Type: integer|Default: 998|
++-----------------------------------------------------------------+
+
+This option sets a limit in bytes on the line length of messages that are
+returned to senders due to delivery problems, when bounce_return_message is
+true. The default value corresponds to RFC limits. If the message being
+returned has lines longer than this value it is treated as if the
+bounce_return_size_limit (below) restriction was exceeded.
+
+The option also applies to bounces returned when an error is detected during
+reception of a message. In this case lines from the original are truncated.
+
+The option does not apply to messages generated by an autoreply transport.
+
++-----------------------------------------------------------+
+|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 44.46 for details of callout verification, and section
+44.48 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 44.46 for details of callout verification, and section
+44.48 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 44.46 for details of callout verification, and section
+44.48 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 44.46 for details of callout verification, and section
+44.48 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 44.47 for details of how this value is used.
+
++-----------------------------------------------------+
+|check_log_inodes|Use: main|Type: integer|Default: 100|
++-----------------------------------------------------+
+
+See check_spool_space below.
+
++----------------------------------------------------+
+|check_log_space|Use: main|Type: integer|Default: 10M|
++----------------------------------------------------+
+
+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: 100|
++-------------------------------------------------------+
+
+See check_spool_space below.
+
++------------------------------------------------------+
+|check_spool_space|Use: main|Type: integer|Default: 10M|
++------------------------------------------------------+
+
+The four check_... options allow for checking of disk resources before a
+message is accepted.
+
+When any of these options are nonzero, 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 = 100M
+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 (though specified in bytes). 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.
+
+There is a slight performance penalty for these checks. Versions of Exim
+preceding 4.88 had these disabled by default; high-rate installations confident
+they will never run out of resources may wish to deliberately disable them.
+
++--------------------------------------------------------------+
+|chunking_advertise_hosts|Use: main|Type: host list*|Default: *|
++--------------------------------------------------------------+
+
+The CHUNKING extension (RFC3030) will be advertised in the EHLO message to
+these hosts. Hosts may use the BDAT command as an alternate to DATA.
+
++-------------------------------------------------------------------------+
+|commandline_checks_require_admin|Use: main|Type: boolean|Default: "false"|
++-------------------------------------------------------------------------+
+
+This option restricts various basic checking features to require an
+administrative user. This affects most of the -b* options, such as -be.
+
++----------------------------------------------------+
+|debug_store|Use: main|Type: boolean|Default: "false"|
++----------------------------------------------------+
+
+This option, when true, enables extra checking in Exim's internal memory
+management. For use when a memory corruption issue is being investigated, it
+should normally be left as default.
+
++--------------------------------------------------------+
+|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 in 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 56.
+
++-----------------------------------------------------------------+
+|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.
+
++-----------------------------------------------------------------------+
+|dkim_verify_hashes|Use: main|Type: string list|Default: sha256 : sha512|
++-----------------------------------------------------------------------+
+
+This option gives a list of hash types which are acceptable in signatures, and
+an order of processing. Signatures with algorithms not in the list will be
+ignored.
+
+Acceptable values include:
+
+sha1
+sha256
+sha512
+
+Note that the acceptance of sha1 violates RFC 8301.
+
++-----------------------------------------------------------------------+
+|dkim_verify_keytypes|Use: main|Type: string list|Default: ed25519 : rsa|
++-----------------------------------------------------------------------+
+
+This option gives a list of key types which are acceptable in signatures, and
+an order of processing. Signatures with algorithms not in the list will be
+ignored.
+
++-----------------------------------------------------------------------------+
+|dkim_verify_min_keysizes| Use: | Type: string |Default: rsa=1024 ed25519=|
+| | main | list | 250|
++-----------------------------------------------------------------------------+
+
+This option gives a list of key sizes which are acceptable in signatures. The
+list is keyed by the algorithm type for the key; the values are in bits.
+Signatures with keys smaller than given by this option will fail verification.
+
+The default enforces the RFC 8301 minimum key size for RSA signatures.
+
++----------------------------------------------------------+
+|dkim_verify_minimal|Use: main|Type: boolean|Default: false|
++----------------------------------------------------------+
+
+If set to true, verification of signatures will terminate after the first
+success.
+
++-----------------------------------------------------------------------+
+|dkim_verify_signers|Use: main|Type: domain list*|Default: $dkim_signers|
++-----------------------------------------------------------------------+
+
+This option gives a list of DKIM domains for which the DKIM ACL is run. It is
+expanded after the message is received; by default it runs the ACL once for
+each signature in the message. See section 58.3.
+
++------------------------------------------------------------+
+|dmarc_forensic_sender|Use: main|Type: string*|Default: unset|
++------------------------------------------------------------+
+
++--------------------------------------------------------+
+|dmarc_history_file|Use: main|Type: string|Default: unset|
++--------------------------------------------------------+
+
++----------------------------------------------------+
+|dmarc_tld_file|Use: main|Type: string|Default: unset|
++----------------------------------------------------+
+
+These options control DMARC processing. See section 58.6 for details.
+
++--------------------------------------------------------------------+
+|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 44.52.
+
++---------------------------------------------------------+
+|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 44.52.
+
++--------------------------------------------------+
+|dns_cname_loops|Use: main|Type: integer|Default: 1|
++--------------------------------------------------+
+
+This option controls the following of CNAME chains, needed if the resolver does
+not do it internally. As of 2018 most should, and the default can be left. If
+you have an ancient one, a value of 10 is likely needed.
+
+The default value of one CNAME-follow is needed thanks to the observed return
+for an MX request, given no MX presence but a CNAME to an A, of the CNAME.
+
++-------------------------------------------------+
+|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.
+
+On Linux with glibc 2.31 or newer this is insufficient, the resolver library
+will default to stripping out a successful validation status. This will break a
+previously working Exim installation. Provided that you do trust the resolver
+(ie, is on localhost) you can tell glibc to pass through any successful
+validation with a new option in /etc/resolv.conf:
+
+options trust-ad
+
++-----------------------------------------------------------+
+|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. Note that all
+lookups, including those done for verification, are affected; this will result
+in verify failure for IPv6 connections or ones using names only valid for IPv6
+addresses.
+
++--------------------------------------------+
+|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. See
+also the slow_lookup_log option.
+
++--------------------------------------------+
+|dns_retry|Use: main|Type: integer|Default: 0|
++--------------------------------------------+
+
+See dns_retrans above.
+
++--------------------------------------------------------+
+|dns_trust_aa|Use: main|Type: domain list*|Default: unset|
++--------------------------------------------------------+
+
+If this option is set then lookup results marked with the AA bit (Authoritative
+Answer) are trusted the same way as if they were DNSSEC-verified. The authority
+section's name of the answer must match with this expanded domain list.
+
+Use this option only if you talk directly to a resolver that is authoritative
+for some zones and does not set the AD (Authentic Data) bit in the answer. Some
+DNS servers may have an configuration option to mark the answers from their own
+zones as verified (they set the AD bit). Others do not have this option. It is
+considered as poor practice using a resolver that is an authoritative server
+for some zones.
+
+Use this option only if you really have to (e.g. if you want to use DANE for
+remote delivery to a server that is listed in the DNS zones that your resolver
+is authoritative for).
+
+If the DNS answer packet has the AA bit set and contains resource record in the
+answer section, the name of the first NS record appearing in the authority
+section is compared against the list. If the answer packet is authoritative but
+the answer section is empty, the name of the first SOA record in the
+authoritative section is used instead.
+
++-------------------------------------------------+
+|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.
+
+OpenBSD's asr resolver routines are known to ignore the EDNS0 option; this
+means that DNSSEC will not work with Exim on that platform either, unless Exim
+is linked against an alternative DNS client library.
+
++----------------------------------------------+
+|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 48.2.
+
++-------------------------------------------------------------+
+|dsn_advertise_hosts|Use: main|Type: host list*|Default: unset|
++-------------------------------------------------------------+
+
+DSN extensions (RFC3461) will be advertised in the EHLO message to, and
+accepted from, these hosts. Hosts may use the NOTIFY and ORCPT options on RCPT
+TO commands, and RET and ENVID options on MAIL FROM commands. A NOTIFY=SUCCESS
+option requests success-DSN messages. A NOTIFY= option with no argument
+requests that no delay or failure DSNs are sent. Note: Supplying success-DSN
+messages has been criticised on privacy grounds; it can leak details of
+internal forwarding.
+
++---------------------------------------------------+
+|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 message'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.20). 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.
+
++---------------------------------------------------+
+|event_action|Use: main|Type: string*|Default: unset|
++---------------------------------------------------+
+
+This option declares a string to be expanded for Exim's events mechanism. For
+details see chapter 61.
+
++------------------------------------------------------------------+
+|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 56 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 56
+for a discussion of security issues.
+
++------------------------------------------------------------+
+|exim_version|Use: main|Type: string|Default: current version|
++------------------------------------------------------------+
+
+This option overrides the $version_number/$exim_version that Exim reports in
+various places. Use with care; this may fool stupid security scanners.
+
++-----------------------------------------------------------------+
+|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.
+
++---------------------------------------------------------------+
+|gnutls_allow_auto_pkcs11|Use: main|Type: boolean|Default: 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 https://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 in DNS 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.
+
+If DNS was used for successful verification, the variable $helo_verify_dnssec
+records the DNSSEC status of the lookups.
+
++-----------------------------------------------------------+
+|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 in 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 44
+.
+
++----------------------------------------------------------------+
+|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_require_alpn|Use: main|Type: host list*|Default: unset|
++------------------------------------------------------------+
+
+If the TLS library supports ALPN then a successful negotiation of ALPN will be
+required for any client matching the list, for TLS to be used. See also the
+tls_alpn option.
+
+Note: prevention of fallback to in-clear connection is not managed by this
+option, and should be done separately.
+
++--------------------------------------------------------+
+|hosts_require_helo|Use: main|Type: host list*|Default: *|
++--------------------------------------------------------+
+
+Exim will require an accepted HELO or EHLO command from a host matching this
+list, before accepting a MAIL command.
+
++-----------------------------------------------------+
+|hosts_proxy|Use: main|Type: host list*|Default: unset|
++-----------------------------------------------------+
+
+This option enables use of Proxy Protocol proxies for incoming connections. For
+details see section 59.1.
+
++----------------------------------------------------------------+
+|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.9), 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.22). 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 in 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_environment|Use: main|Type: string list|Default: unset|
++-----------------------------------------------------------+
+
+This option contains a string list of environment variables to keep. You have
+to trust these variables or you have to be sure that these variables do not
+impose any security risk. Keep in mind that during the startup phase Exim is
+running with an effective UID 0 in most installations. As the default value is
+an empty list, the default environment for using libraries, running embedded
+Perl code, or running external binaries is empty, and does not not even contain
+PATH or HOME.
+
+Actually the list is interpreted as a list of patterns (10.1), except that it
+is not expanded first.
+
+WARNING: Macro substitution is still done first, so having a macro FOO and
+having FOO_HOME in your keep_environment option may have unexpected results.
+You may work around this using a regular expression that does not match the
+macro name: ^[F]OO_HOME$.
+
+Current versions of Exim issue a warning during startup if you do not mention
+keep_environment in your runtime configuration file and if your current
+environment is not empty. Future versions may not issue that warning anymore.
+
+See the add_environment main config option for a way to set environment
+variables to a fixed value. The environment for pipe transports is handled
+separately, see section 29.4 for details.
+
++-----------------------------------------------+
+|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 43.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.15 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. This option is ignored for "ldapi" connections.
+
++---------------------------------------------------+
+|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 48.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 46). 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 48.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 runtime, or if
+the option is unset at runtime (i.e. "log_file_path = ") they are written in a
+sub-directory called log in Exim's spool directory. A path must start with a
+slash. To send to syslog, use the word "syslog". Chapter 53 contains further
+details about Exim's logging, and section 53.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 53.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.
+
+If nonzero the value will be advertised as a parameter to the ESMTP SIZE
+service extension keyword.
+
+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. E.g., 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 52 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.22). 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.
+
++-----------------------------------------------------------------------------+
+|notifier_socket| Use: | Type: | Default: $spool_directory/|
+| | main | string | exim_daemon_notify|
++-----------------------------------------------------------------------------+
+
+This option gives the name for a unix-domain socket on which the daemon listens
+for work and information-requests. Only installations running multiple daemons
+sharing a spool directory should need to modify the default.
+
+The option is expanded before use. If the platform supports Linux-style
+abstract socket names, the result is used with a nul byte prefixed. Otherwise,
+it should be a full path name and use a directory accessible to Exim.
+
+If this option is set as empty, or the command line -oY option is used, or the
+command line uses a -oX option and does not use -oP, then a notifier socket is
+not created.
+
++-----------------------------------------------------------------------------+
+| |Use: | Type: |Default: +no_sslv2 +no_sslv3 +single_dh_use|
+|openssl_options| main | string | +no_ticket +no_renegotiation|
+| | | list | |
++-----------------------------------------------------------------------------+
+
+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.
+
+The option affects Exim operating both as a server and as a client.
+
+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.
+
+Examples:
+
+# Make both old MS and old Eudora happy:
+openssl_options = -all +microsoft_big_sslv3_buffer \
+ +dont_insert_empty_fragments
+
+# Disable older protocol versions:
+openssl_options = +no_sslv2 +no_sslv3
+
+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.22). 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.
+
++-----------------------------------------------------+
+|perl_taintmode|Use: main|Type: boolean|Default: false|
++-----------------------------------------------------+
+
+This option enables the taint mode of the embedded Perl interpreter.
+
++--------------------------------------------------------+
+|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.22). 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
+44.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).
+
++------------------------------------------------------------------------+
+|pipelining_connect_advertise_hosts|Use: main|Type: host list*|Default: *|
++------------------------------------------------------------------------+
+
+If Exim is built without the DISABLE_PIPE_CONNECT build option this option
+controls which hosts the facility is advertised to and from which pipeline
+early-connection (before MAIL) SMTP commands are acceptable. When used, the
+pipelining saves on roundtrip times.
+
+See also the hosts_pipe_connect smtp transport option.
+
+The SMTP service extension keyword advertised is "PIPECONNECT"; it permits the
+client to pipeline TCP connection and hello command (inclear phase), or
+TLS-establishment and hello command (encrypted phase), on later connections to
+the same host.
+
++--------------------------------------------------+
+|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 received. See section 44.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 48.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 and
+commandline_checks_require_admin.
+
++-------------------------------------------------------+
+|proxy_protocol_timeout|Use: main|Type: time|Default: 3s|
++-------------------------------------------------------+
+
+This option sets the timeout for proxy protocol negotiation. For details see
+section 59.1.
+
++--------------------------------------------------------+
+|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_fast_ramp|Use: main|Type: boolean|Default: false|
++------------------------------------------------------+
+
+If set to true, two-phase queue runs, initiated using -qq on the command line,
+may start parallel delivery processes during their first phase. This will be
+done when a threshold number of messages have been routed for a single host.
+
++---------------------------------------------------------------+
+|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 and commandline_checks_require_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 in 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.
+
+To set limits for different named queues use an expansion depending on the
+$queue_name variable.
+
++--------------------------------------------------------------+
+|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 in 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 forever. 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_ver { ($tls_in_ver)}}\
+ ${if def:tls_in_cipher_std { tls $tls_in_cipher_std\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 references to the TLS version and cipher are 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: 50000|
++-----------------------------------------------------+
+
+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.
+
+See also the max_parallel generic transport option, and the serialize_hosts
+smtp transport option.
+
+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. The default value specifies just this host, being any local
+interface for the system.
+
++------------------------------------------------------+
+|rfc1413_query_timeout|Use: main|Type: time|Default: 0s|
++------------------------------------------------------+
+
+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.
+
++--------------------------------------------------+
+|slow_lookup_log|Use: main|Type: integer|Default: 0|
++--------------------------------------------------+
+
+This option controls logging of slow lookups. If the value is nonzero it is
+taken as a number of milliseconds and lookups taking longer than this are
+logged. Currently this applies only to DNS lookups.
+
++-----------------------------------------------------------+
+|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). The option is expanded after the HELO or EHLO is received and may depend
+on values available at that time. An empty or zero value after expansion
+removes the limit.
+
++---------------------------------------------------------------+
+|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
+in 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 in 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_backlog_monitor|Use: main|Type: integer|Default: 0|
++-------------------------------------------------------+
+
+If this option is set to greater than zero, and the backlog of available TCP
+connections on a socket listening for SMTP is larger than it, a line is logged
+giving the value and the socket address and port. The value is retrived jsut
+before an accept call. This facility is only available on Linux.
+
++------------------------------------------------------+
+|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 44.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
+44). 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
+
+If the option is not set, the argument for the ETRN command must be a #
+followed by an address string. In this case an exim -R <string> command is
+used; if the ETRN ACL has set up a named-queue then -MCG <queue> is appended.
+
+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
+49.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 44.39 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.
+
+If the first character of the option is a "$" the option is expanded before use
+and may depend on $sender_host_name, $sender_host_address and $sender_host_port
+.
+
+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
+
++--------------------------------------------------------------+
+|smtputf8_advertise_hosts|Use: main|Type: host list*|Default: *|
++--------------------------------------------------------------+
+
+When Exim is built with support for internationalised mail names, the
+availability thereof is advertised in response to EHLO only to those client
+hosts that match this option. See chapter 60 for details of Exim's support for
+internationalisation.
+
++-----------------------------------------------------------+
+|spamd_address|Use: main|Type: string|Default: 127.0.0.1 783|
++-----------------------------------------------------------+
+
+This option is available when Exim is compiled with the content-scanning
+extension. It specifies how Exim connects to SpamAssassin's spamd daemon. See
+section 45.2 for more details.
+
++--------------------------------------------------------------------+
+|spf_guess|Use: main|Type: string|Default: v=spf1 a/24 mx/24 ptr ?all|
++--------------------------------------------------------------------+
+
+This option is available when Exim is compiled with SPF support. See section
+58.4 for more details.
+
++-----------------------------------------------------------------------------+
+|spf_smtp_comment_template|Use: | Type: | Default: Please%_see%_http://|
+| | main | string* | www.open-spf.org/Why|
++-----------------------------------------------------------------------------+
+
+This option is available when Exim is compiled with SPF support. It allows the
+customisation of the SMTP comment that the SPF library generates. You are
+strongly encouraged to link to your own explanative site. The template must not
+contain spaces. If you need spaces in the output, use the proper placeholder.
+If libspf2 can not parse the template, it uses a built-in default broken link.
+The following placeholders (along with Exim variables (but see below)) are
+allowed in the template:
+
+ * %_: A space.
+
+ * %{L}: Envelope sender's local part.
+
+ * %{S}: Envelope sender.
+
+ * %{O}: Envelope sender's domain.
+
+ * %{D}: Current(?) domain.
+
+ * %{I}: SMTP client Ip.
+
+ * %{C}: SMTP client pretty IP.
+
+ * %{T}: Epoch time (UTC).
+
+ * %{P}: SMTP client domain name.
+
+ * %{V}: IP version.
+
+ * %{H}: EHLO/HELO domain.
+
+ * %{R}: Receiving domain.
+
+The capitalized placeholders do proper URL encoding, if you use them
+lowercased, no encoding takes place. This list was compiled from the libspf2
+sources.
+
+A note on using Exim variables: As currently the SPF library is initialized
+before the SMTP EHLO phase, the variables useful for expansion are quite
+limited.
+
++------------------------------------------------------------+
+|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 in 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.
+
++-------------------------------------------------------+
+|spool_wireformat|Use: main|Type: boolean|Default: false|
++-------------------------------------------------------+
+
+If this option is set, Exim may for some messages use an alternative format for
+data-files in the spool which matches the wire format. Doing this permits more
+efficient message reception and transmission. Currently it is only done for
+messages received using the ESMTP CHUNKING option.
+
+The following variables will not have useful values:
+
+$max_received_linelength
+$body_linecount
+$body_zerocount
+
+Users of the local_scan() API (see 46), and any external programs which are
+passed a reference to a message data file (except via the "regex", "malware" or
+"spam") ACL conditions) will need to be aware of the different formats
+potentially available.
+
+Using any of the ACL conditions noted will negate the reception benefit (as a
+Unix-mbox-format file is constructed for them). The transmission benefit is
+maintained.
+
++----------------------------------------------------+
+|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.26 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 44.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 53 for details of Exim's logging.
+
++------------------------------------------------+
+|syslog_pid|Use: main|Type: boolean|Default: true|
++------------------------------------------------+
+
+If syslog_pid is set false, the PID on Exim's log lines are omitted when these
+lines are sent to syslog. (Syslog normally prefixes the log lines with the PID
+of the logging process automatically.) You need to enable the "+pid" log
+selector item, if you want Exim to write it's PID into the logs.) See chapter
+53 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 53 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 53 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 47. A
+forced expansion failure results in no filter operation.
+
++------------------------------------------------------------------------+
+|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 in 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 in 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: *|
++---------------------------------------------------------+
+
+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
+43 for details of Exim's support for TLS. Note that the default value requires
+that a certificate be supplied using the tls_certificate option. If TLS support
+for incoming connections is not required the tls_advertise_hosts option should
+be set empty.
+
++-----------------------------------------------------------+
+|tls_alpn|Use: main|Type: string list*|Default: smtp : esmtp|
++-----------------------------------------------------------+
+
+If this option is set, the TLS library supports ALPN, and the client offers
+either more than ALPN name or a name which does not match the list, the TLS
+connection is declined.
+
++-----------------------------------------------------------+
+|tls_certificate|Use: main|Type: string list*|Default: unset|
++-----------------------------------------------------------+
+
+The value of this option is expanded, and must then be a list of absolute paths
+to files which contain the server's certificates (in PEM format). Commonly only
+one file is needed. The server's private key is also assumed to be in this file
+if tls_privatekey is unset. See chapter 43 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.
+
+Note: If you use filenames based on IP addresses, change the list separator in
+the usual way (6.21) to avoid confusion under IPv6.
+
+Note: Under versions of OpenSSL preceding 1.1.1, when a list of more than one
+file is used, the $tls_in_ourcert variable is unreliable. The macro
+"_TLS_BAD_MULTICERT_IN_OURCERT" will be defined for those versions.
+
+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 43.12 will
+be re-expanded.
+
+If this option is unset or empty a self-signed certificate will be used. Under
+Linux this is generated at daemon startup; on other platforms it will be
+generated fresh for every connection.
+
++----------------------------------------------+
+|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 CRLs in PEM format.
+
+Under OpenSSL the option can specify a directory with CRL files.
+
+Note: Under OpenSSL the option must, if given, supply a CRL for each signing
+element of the certificate chain (i.e. all but the leaf). For the file variant
+this can be multiple PEM blocks in the one file.
+
+See 43.12 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.
+
+Note: The Exim Maintainers strongly recommend using a filename with
+site-generated local DH parameters, which has been supported across all
+versions of Exim. The other specific constants available are a fallback so that
+even when "unconfigured", Exim can offer Perfect Forward Secrecy in older
+ciphersuites in TLS.
+
+If tls_dhparam 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 43.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 Exim-specific but lacks verifiable
+provenance.
+
+In older versions of Exim the default was 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, RFC 5114, RFC 7919, or from other
+sources. As names, Exim uses a standard specified name, else "ike" followed by
+the number used by IKE, or "default" which corresponds to
+"exim.dev.20160529.3".
+
+The available standard primes are: "ffdhe2048", "ffdhe3072", "ffdhe4096",
+"ffdhe6144", "ffdhe8192", "ike1", "ike2", "ike5", "ike14", "ike15", "ike16",
+"ike17", "ike18", "ike22", "ike23" and "ike24".
+
+The available additional primes are: "exim.dev.20160529.1",
+"exim.dev.20160529.2" and "exim.dev.20160529.3".
+
+Some of these will be too small to be accepted by clients. Some may be too
+large to be accepted by clients. The open cryptographic community has
+suspicions about the integrity of some of the later IKE values, which led into
+RFC7919 providing new fixed constants (the "ffdhe" identifiers).
+
+At this point, all of the "ike" values should be considered obsolete; they are
+still in Exim to avoid breaking unusual configurations, but are candidates for
+removal the next time we have backwards-incompatible changes.
+
+Two of them in particular ("ike1" and "ike22") are called out by RFC 8247 as
+MUST NOT use for IPSEC, and two more ("ike23" and "ike24") as SHOULD NOT.
+Because of this, Exim regards them as deprecated; if either of the first pair
+are used, warnings will be logged in the paniclog, and if any are used then
+warnings will be logged in the mainlog. All four will be removed in a future
+Exim release.
+
+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_eccurve|Use: main|Type: string*|Default: "auto"|
++---------------------------------------------------+
+
+This option selects a EC curve for use by Exim when used with OpenSSL. It has
+no effect when Exim is used with GnuTLS.
+
+After expansion it must contain a valid EC curve parameter, such as
+"prime256v1", "secp384r1", or "P-512". Consult your OpenSSL manual for valid
+selections.
+
+For OpenSSL versions before (and not including) 1.0.2, the string "auto"
+selects "prime256v1". For more recent OpenSSL versions "auto" tells the library
+to choose.
+
+If the option expands to an empty string, no EC curves will be enabled.
+
++----------------------------------------------------+
+|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.
+
+Usable for GnuTLS 3.4.4 or 3.3.17 or OpenSSL 1.1.0 (or later). The macro
+"_HAVE_TLS_OCSP" will be defined for those versions.
+
+For OpenSSL 1.1.0 or later, and for GnuTLS 3.5.6 or later the expanded value of
+this option can be a list of files, to match a list given for the
+tls_certificate option. The ordering of the two lists must match. The macro
+"_HAVE_TLS_OCSP_LIST" will be defined for those versions.
+
+The file(s) should be in DER format, except for GnuTLS 3.6.3 or later or for
+OpenSSL, when an optional filetype prefix can be used. The prefix must be one
+of "DER" or "PEM", followed by a single space. If one is used it sets the
+format for subsequent files in the list; the initial format is DER. If multiple
+proofs are wanted, for multiple chain elements (this only works under TLS1.3)
+they must be coded as a combined OCSP response.
+
+Although GnuTLS will accept PEM files with multiple separate PEM blobs (ie.
+separate OCSP responses), it sends them in the TLS Certificate record
+interleaved with the certificates of the chain; although a GnuTLS client is
+happy with that, an OpenSSL client is not.
+
++---------------------------------------------------------------+
+|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 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 list*|Default: unset|
++----------------------------------------------------------+
+
+The value of this option is expanded, and must then be a list of absolute paths
+to files which contains the server's private keys. 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 43 for further details.
+
+See 43.12 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 43.4
+and 43.5.
+
++--------------------------------------------------------------+
+|tls_resumption_hosts|Use: main|Type: host list*|Default: unset|
++--------------------------------------------------------------+
+
+This option controls which connections to offer the TLS resumption feature. See
+43.17 for details.
+
++--------------------------------------------------------------+
+|tls_try_verify_hosts|Use: main|Type: host list*|Default: unset|
++--------------------------------------------------------------+
+
+See tls_verify_hosts below.
+
++---------------------------------------------------------------+
+|tls_verify_certificates|Use: main|Type: string*|Default: system|
++---------------------------------------------------------------+
+
+The value of this option is expanded, and must then be either the word "system"
+or the absolute path to a file or directory containing permitted certificates
+for clients that match tls_verify_hosts or tls_try_verify_hosts.
+
+The "system" value for the option will use a system default location compiled
+into the SSL library. This is not available for GnuTLS versions preceding
+3.0.20, and will be taken as empty; an explicit location must be specified.
+
+The use of a directory for the option value is not available for GnuTLS
+versions preceding 3.3.6 and a single file must be used.
+
+With OpenSSL the certificates specified explicitly either by file or directory
+are added to those given by the system default location.
+
+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 the
+explicit directory version. (If your peer is Exim up to 4.85, using GnuTLS, you
+may need to send the CAs (thus using the file variant). Otherwise the peer
+doesn't send its certificate.)
+
+See 43.12 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 48.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 in the queue for a specified amount of time, as specified by delay_warning
+. Details of the file's contents are given in chapter 50. The option is
+expanded to give the file path, which must be absolute and untainted. 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 53 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.
+
+The name of a router is limited to be 64 ASCII characters long; prior to Exim
+4.95 names would be silently truncated at this length, but now it is enforced.
+
++------------------------------------------------------+
+|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).
+
+See also the set option below.
+
+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.21 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 44.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,
+$local_part_data is set to an untainted version of the local part and $home is
+set from the password data. The latter 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.
+
+Historical note: We have condition on ACLs and on Routers. Routers are far
+older, and use one set of semantics. ACLs are newer and when they were created,
+the ACL condition process was given far stricter parse semantics. The bool{}
+expansion condition uses the same rules as ACLs. The bool_lax{} expansion
+condition uses the same rules as Routers. More pointedly, the bool_lax{} was
+written to match the existing Router rules processing behavior.
+
+This is best illustrated in an example:
+
+# If used in an ACL condition will fail with a syntax error, but
+# in a router condition any extra characters are treated as a string
+
+$ exim -be '${if eq {${lc:GOOGLE.com}} {google.com}} {yes} {no}}'
+true {yes} {no}}
+
+$ exim -be '${if eq {${lc:WHOIS.com}} {google.com}} {yes} {no}}'
+ {yes} {no}}
+
+In each example above, the if statement actually ends after "{google.com}}".
+Since no true or false braces were defined, the default if behavior is to
+return a boolean true or a null answer (which evaluates to false). The rest of
+the line is then treated as a string. So the first example resulted in the
+boolean answer "true" with the string " {yes} {no}}" appended to it. The second
+example resulted in the null output (indicating false) with the string " {yes}
+{no}}" appended to it.
+
+In fact you can put excess forward braces in too. In the router condition,
+Exim's parser only looks for "{" symbols when they mean something, like after a
+"$" or when required as part of a conditional. But otherwise "{" and "}" are
+treated as ordinary string characters.
+
+Thus, in a Router, the above expansion strings will both always evaluate true,
+as the result of expansion is a non-empty string which doesn't match an
+explicit false value. This can be tricky to debug. By contrast, in an ACL
+either of those strings will always result in an expansion error because the
+result doesn't look sufficiently boolean.
+
++-----------------------------------------------------+
+|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.
+
++-----------------------------------------------------------------+
+|dnssec_request_domains|Use: routers|Type: domain list*|Default: *|
++-----------------------------------------------------------------+
+
+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, AAAA, A lookup
+sequence.
+
++---------------------------------------------------------------------+
+|dnssec_require_domains|Use: routers|Type: domain list*|Default: unset|
++---------------------------------------------------------------------+
+
+DNS lookups for domains matching dnssec_require_domains will be done with the
+DNSSEC request bit set. Any returns not having the Authenticated Data bit (AD
+bit) set will be ignored and logged as a host-lookup failure. This applies to
+all of the SRV, MX, AAAA, A lookup sequence.
+
++--------------------------------------------------------+
+|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 and in the transport. 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.
+
++-----------------------------------------------------+
+|dsn_lasthop|Use: routers|Type: boolean|Default: false|
++-----------------------------------------------------+
+
+If this option is set true, and extended DSN (RFC3461) processing is in effect,
+Exim will not pass on DSN requests to downstream DSN-aware hosts but will
+instead send a success DSN as if the next hop does not support DSN. Not
+effective on redirect routers.
+
++---------------------------------------------------+
+|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 51.2, or to implement VERP
+(Variable Envelope Return Paths) (see section 51.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 44). 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.21), 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 (by default,
+changeable in the usual way 6.21), 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 48.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 (by default,
+changeable in the usual way 6.21), that is associated with any addresses that
+are accepted by the router. However, the option has no effect when an address
+is just being verified. Each list item is separately expanded, at transport
+time. If an item ends in *, it will match any header with the given prefix. The
+way in which the text is used to remove header lines at transport time is
+described in section 48.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 handled 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.
+
+Warning 3: Because of the separate expansion of the list items, items that
+contain a list separator must have it doubled. To avoid this, change the list
+separator (6.21).
+
++----------------------------------------------------------------+
+|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 51.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.
+
+If wildcarding (above) was used then the part of the prefix matching the
+wildcard is available in $local_part_prefix_v.
+
+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.22 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_data
+
+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 or in the transport. 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 (
+6.21). 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 (e.g., .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_data:$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 filename (or the exclamation mark that precedes the filename
+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 any of
+check_local_user, local_parts, condition, local_part_prefix, local_part_suffix,
+senders or require_files 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.
+
+Failing to set this option when it is needed (because a remote router handles
+only some of the local-parts for a domain) can result in incorrect error
+messages being generated.
+
+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.
+
++-------------------------------------------------+
+|set|Use: routers|Type: string list|Default: unset|
++-------------------------------------------------+
+
+This option may be used multiple times on a router; because of this the list
+aspect is mostly irrelevant. The list separator is a semicolon but can be
+changed in the usual way.
+
+Each list-element given must be of the form "name = value" and the names used
+must start with the string "r_". Values containing a list-separator should have
+them doubled. When a router runs, the strings are evaluated in order, to create
+variables which are added to the set associated with the address. This is done
+immediately after all the preconditions, before the evaluation of the
+address_data option. The variable is set with the expansion of the value. The
+variables can be used by the router options (not including any preconditions)
+and by the transport. Later definitions of a given named variable will override
+former ones. Variable use is via the usual $r_... syntax.
+
+This is similar to the address_data option, except that many independent
+variables can be used, with choice of naming.
+
++--------------------------------------------------------------+
+|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. See also the $verify_mode variable.
+
++--------------------------------------------------------+
+|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. See also the $verify_mode variable.
+
+
+
+===============================================================================
+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 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
+misbehaving 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.
+
+The router will defer rather than decline if the domain is found in the
+fail_defer_domains router option.
+
+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.
+
++-------------------------------------------------------------------+
+|fail_defer_domains|Use: dnslookup|Type: domain list*|Default: unset|
++-------------------------------------------------------------------+
+
+DNS lookups for domains matching fail_defer_domains which find no matching
+record will cause the router to defer rather than the default behaviour of
+decline. This maybe be useful for queueing messages for a newly created domain
+while the DNS configuration is not ready. However, it will result in any
+message with mistyped domains also being queued.
+
++-------------------------------------------+
+|ipv4_only|Use: string*|Type: unset|Default:|
++-------------------------------------------+
+
+The string is expanded, and if the result is anything but 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), only A records are used.
+
++---------------------------------------------+
+|ipv4_prefer|Use: string*|Type: unset|Default:|
++---------------------------------------------+
+
+The string is expanded, and if the result is anything but 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), A records are sorted before AAAA records
+(inverting the default).
+
++-----------------------------------------------------------+
+|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.20. 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.9), 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. If the list is written with spaces,
+it must be protected with quotes. 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.21.
+
+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 can be either a host name or an IP address,
+optionally with an attached port number, or it can be a single "+" (see
+hosts_randomize). 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, space-separated. 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.
+
+ * ipv4_only: in direct DNS lookups, look up only A records.
+
+ * ipv4_prefer: in direct DNS lookups, sort A records before AAAA records.
+
+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.
+
+Compatibility: From Exim 4.85 until fixed for 4.90, there was an inadvertent
+constraint that a transport name as an option had to be the last option
+specified.
+
+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.
+
+If success DSNs have been requested redirection triggers one and the DSN
+options are not passed any further.
+
+
+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.
+
+Tainted data may not be used for a filename.
+
+Warning: It is unwise to use $local_part or $domain directly for redirection,
+as they are provided by a potential attacker. In the examples above,
+$local_part is used for looking up data held locally on the system, and not
+used directly (the second example derives $home via the passsword file or
+database, using $local_part).
+
+
+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 filename 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, filenames, 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 filename, but
+
+ /s=molari/o=babylon/@x400gate.way
+
+ is treated as an address. For a filename, 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 filename, 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
+
+ Tainted data may not be used for a filename.
+
+ * 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 specifying /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 for verify, 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 in 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 filename 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 54.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_data
+ 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 name of a transport is limited to be 64 ASCII characters long; prior to
+Exim 4.95 names would be silently truncated at this length, but now it is
+enforced.
+
+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.
+
+Note: If used on a transport handling multiple recipients (the smtp transport
+unless rcpt_max is 1, the appendfile, pipe or lmtp transport if batch_max is
+greater than 1) then information about Bcc recipients will be leaked. Doing so
+is generally not advised.
+
++---------------------------------------------------------+
+|event_action|Use: transports|Type: string*|Default: unset|
++---------------------------------------------------------+
+
+This option declares a string to be expanded for Exim's events mechanism. For
+details see chapter 61.
+
++-------------------------------------------------------+
+|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 (by default,
+changeable in the usual way 6.21), which are (separately) expanded and added to
+the header portion of a message as it is transported, as described in section
+48.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 text headers, colon-separated (by default,
+changeable in the usual way 6.21), to be removed from the message. However, the
+option has no effect when an address is just being verified. 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. If an item ends in
+*, it will match any header with the given prefix.
+
+Matching headers are omitted from the message as it is transported, as
+described in section 48.17. Header removal can also be specified by routers.
+
+Unlike most options, headers_remove can be specified multiple times for a
+transport; all listed headers are removed.
+
+Warning: Because of the separate expansion of the list items, items that
+contain a list separator must have it doubled. To avoid this, change the list
+separator (6.21).
+
++-----------------------------------------------------------+
+|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.
+
++----------------------------------------------------------+
+|max_parallel|Use: transports|Type: integer*|Default: unset|
++----------------------------------------------------------+
+
+If this option is set and expands to an integer greater than zero it limits the
+number of concurrent runs of the transport. The control does not apply to
+shadow transports.
+
+Exim implements this control by means of a hints database in which a record is
+incremented whenever a transport process is being created. The record is
+decremented and possibly removed when the process terminates. 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 use this option, 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 and smtp
+transport serialization.
+
++-----------------------------------------------------------+
+|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 51.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. If unset, or expanding to an empty
+string, no filtering is done.
+
+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 $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 49.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.
+
+If the create_file option is set to a path which matches (see the option
+definition below for details) a file or directory name for the delivery, that
+name becomes de-tainted.
+
+Tainted data may not be used for a file or directory name. This means that, for
+instance, $local_part cannot be used directly as a component of a path. It can
+however be used as the key for a lookup which returns a path (or component).
+
+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 filename. 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_data} \
+ {${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", or to an absolute path.
+
+In the second and third cases, a home directory must have been set for the
+transport, and the file or directory being created must reside within it. The
+"belowhome" checking additionally checks for attempts to use "../" to evade the
+testing. This option is not useful when an explicit filename is given for
+normal mailbox deliveries. It is intended for the case when filenames are
+generated from users' .forward files. These are usually handled by an
+appendfile transport called address_file. See also file_must_exist.
+
+In the fourth case, the value given for this option must be an absolute path
+for an existing directory. The value is used for checking instead of a home
+directory; checking is done in "belowhome" mode.
+
+If "belowhome" checking is used, the file or directory path becomes de-tainted.
+
++------------------------------------------------------+
+|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.
+
+The result of expansion must not be tainted, unless the create_file option
+specifies a path.
+
++---------------------------------------------------------------+
+|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.
+
+The result of expansion must not be tainted, unless the create_file option
+specifies a path.
+
+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_data
+file = /home/$local_part_data/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, optionally followed by a slash and
+further option modifiers. If Exim is running on a system with large file
+support (Linux and FreeBSD have this), mailboxes larger than 2G can be handled.
+
+The option modifier no_check can be used to force delivery even if the over
+quota condition is met. The quota gets updated as usual.
+
+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".
+
+The option modifier no_check can be used to force delivery even if the over
+quota condition is met. The quota gets updated as usual.
+
++--------------------------------------------------------------+
+|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 filename, 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 filename. 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
+filename (even though maildir_tag puts it there) because maildir MUAs sometimes
+add other information onto the ends of message filenames.
+
+Section 26.7 contains further information.
+
+This option should not be used when other message-handling software may
+duplicate messages by making hardlinks to the files. When that is done Exim
+will count the message size once for each filename, in contrast with the actual
+disk usage. When the option is not set, calculating total usage requires a
+system-call per file to get the size; the number of links is then available
+also as is used to adjust the effective size.
+
++-------------------------------------------------------------------+
+|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 49.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 filename.
+
+ 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 filename, <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 filename.
+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_data\
+ ${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
+49.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 filename, 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.
+
+Tainted data may not be used for the command name.
+
+
+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. Alternatively the
+max_parallel option could be used with a value of "1" to enforce serialization.
+
+
+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" (no quotes). 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. The environment for the pipe transport is not subject to the
+add_environment and keep_environment main config options.
+
+Note: Using enviroment variables loses track of tainted data. Writers of pipe
+transport commands should be wary of data supplied by potential attackers.
+
+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.
+
+No part of the resulting command may be tainted.
+
++--------------------------------------------------+
+|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 on stdout or stderr, 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 on stdout or stderr,
+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 on stdout or stderr,
+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: /bin:/usr/bin|
++---------------------------------------------------+
+
+This option is expanded and specifies the string that is set up in the PATH
+environment variable of the subprocess. 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 49.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_data
+ return_path_add
+ delivery_date_add
+ envelope_to_add
+ check_string = "From "
+ escape_string = ">From "
+ umask = 077
+ user = $local_part_data
+ 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 \
+ -- $local_part_data
+ user = cyrus
+ group = mail
+ return_output
+ log_output
+ message_prefix =
+ message_suffix =
+
+# router
+local_user_cyrus:
+ driver = accept
+ check_local_user
+ 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 49.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.
+
++---------------------------------------------------------------+
+|dane_require_tls_ciphers|Use: smtp|Type: string*|Default: unset|
++---------------------------------------------------------------+
+
+This option may be used to override tls_require_ciphers for connections where
+DANE has been determined to be in effect. If not set, then tls_require_ciphers
+will be used. Normal SMTP delivery is not able to make strong demands of TLS
+cipher configuration, because delivery will fall back to plaintext. Once DANE
+has been determined to be in effect, there is no plaintext fallback and making
+the TLS cipherlist configuration stronger will increase security, rather than
+counter-intuitively decreasing it. If the option expands to be empty or is
+forced to fail, then it will be treated as unset and tls_require_ciphers will
+be used instead.
+
++---------------------------------------------+
+|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.
+
++-------------------------------------------------+
+|dkim_canon|Use: smtp|Type: string*|Default: unset|
++-------------------------------------------------+
+
+DKIM signing option. For details see section 58.2.
+
++-------------------------------------------------------+
+|dkim_domain|Use: smtp|Type: string list*|Default: unset|
++-------------------------------------------------------+
+
+DKIM signing option. For details see section 58.2.
+
++-------------------------------------------------+
+|dkim_hash|Use: smtp|Type: string*|Default: sha256|
++-------------------------------------------------+
+
+DKIM signing option. For details see section 58.2.
+
++----------------------------------------------------+
+|dkim_identity|Use: smtp|Type: string*|Default: unset|
++----------------------------------------------------+
+
+DKIM signing option. For details see section 58.2.
+
++-------------------------------------------------------+
+|dkim_private_key|Use: smtp|Type: string*|Default: unset|
++-------------------------------------------------------+
+
+DKIM signing option. For details see section 58.2.
+
++----------------------------------------------------+
+|dkim_selector|Use: smtp|Type: string*|Default: unset|
++----------------------------------------------------+
+
+DKIM signing option. For details see section 58.2.
+
++--------------------------------------------------+
+|dkim_strict|Use: smtp|Type: string*|Default: unset|
++--------------------------------------------------+
+
+DKIM signing option. For details see section 58.2.
+
++----------------------------------------------------------+
+|dkim_sign_headers|Use: smtp|Type: string*|Default: per RFC|
++----------------------------------------------------------+
+
+DKIM signing option. For details see section 58.2.
+
++------------------------------------------------------+
+|dkim_timestamps|Use: smtp|Type: string*|Default: unset|
++------------------------------------------------------+
+
+DKIM signing option. For details see section 58.2.
+
++--------------------------------------------------------+
+|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: *|
++--------------------------------------------------------------+
+
+DNS lookups for domains matching dnssec_request_domains will be done with the
+DNSSEC request bit set. Setting this transport option is only useful if the
+transport overrides or sets the host names. See the dnssec_request_domains
+router option.
+
++------------------------------------------------------------------+
+|dnssec_require_domains|Use: smtp|Type: domain list*|Default: unset|
++------------------------------------------------------------------+
+
+DNS lookups for domains matching dnssec_require_domains will be done with the
+DNSSEC request bit set. Setting this transport option is only useful if the
+transport overrides or sets the host names. See the dnssec_require_domains
+router option.
+
++-------------------------------------------+
+|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.20
+. 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} \
+ {${listextract{1}{<\n $value}}} \
+ {$primary_hostname}}
+
+The use of helo_data applies both to sending messages and when doing callouts.
+
++-----------------------------------------------------------------+
+|host_name_extract|Use: smtp|Type: string list*|Default: see below|
++-----------------------------------------------------------------+
+
+Some mail-accepting sites (notably Microsoft) operate many servers behind a
+network load-balancer. When this is done, with separated TLS session caches,
+TLS session resuption becomes problematic. It will only succeed when the same
+server happens to be selected by the load-balancer, matching the session stored
+in the client's cache.
+
+Exim can pull out a server name, if there is one, from the response to the
+client's SMTP EHLO command. The default value of this option:
+
+ ${if and { {match {$host} {.outlook.com\$}} \
+ {match {$item} {\N^250-([\w.]+)\s\N}} \
+ } {$1}}
+
+suffices for one known case. During the expansion of this option the $item
+variable will have the server's EHLO response. The result of the option
+expansion is included in the key used to store and retrieve the TLS session,
+for session resumption.
+
+Operators of high-load sites may wish to evaluate their logs for indications of
+other destination sites operating load-balancers, and develop a suitable
+expression for this option. The smtp:ehlo event and the $tls_out_resumption
+variable will be useful for such work.
+
++-------------------------------------------------+
+|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.20. 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 ESMTP PIPELINING extension when delivering to any host
+that matches this list, even if the server host advertises PIPELINING support.
+
++------------------------------------------------------------+
+|hosts_pipe_connect|Use: smtp|Type: host list*|Default: unset|
++------------------------------------------------------------+
+
+If Exim is built with the SUPPORT_PIPE_CONNECT build option this option
+controls which to hosts the facility watched for and recorded, and used for
+subsequent connections.
+
+The retry hints database is used for the record, and records are subject to the
+retry_data_expire option. When used, the pipelining saves on roundtrip times.
+It also turns SMTP into a client-first protocol so combines well with TCP Fast
+Open.
+
+See also the pipelining_connect_advertise_hosts main option.
+
+Note:
+
+When the facility is used, if the transport interface option is unset the
+helo_data option
+
+will be expanded before the $sending_ip_address variable is filled in. A check
+is made for the use of that variable, without the presence of a "def:" test on
+it, but suitably complex coding can avoid the check and produce unexpected
+results. You have been warned.
+
++---------------------------------------------------------+
+|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 43 for details of TLS.
+
++----------------------------------------------------------------+
+|hosts_verify_avoid_tls|Use: smtp|Type: host list*|Default: unset|
++----------------------------------------------------------------+
+
+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.
+
++------------------------------------------------+
+|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 43.13 for an explanation of when
+this might be needed.
+
++-----------------------------------------------------------+
+|hosts_noproxy_tls|Use: smtp|Type: host list*|Default: unset|
++-----------------------------------------------------------+
+
+For any host that matches this list, a TLS session which has been started will
+not be passed to a new delivery process for sending another message on the same
+session.
+
+The traditional implementation closes down TLS and re-starts it in the new
+process, on the same open TCP connection, for each successive message sent. If
+permitted by this option a pipe to to the new process is set up instead, and
+the original process maintains the TLS connection and proxies the SMTP
+connection from and to the new process and any subsequents. The new process has
+no access to TLS information, so cannot include it in logging.
+
++-----------------------------------------------------+
+|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_alpn|Use: smtp|Type: host list*|Default: unset|
++------------------------------------------------------------+
+
+If the TLS library supports ALPN then a successful negotiation of ALPN will be
+required for any host matching the list, for TLS to be used. See also the
+tls_alpn option.
+
+Note: prevention of fallback to in-clear connection is not managed by this
+option; see hosts_require_tls.
+
++------------------------------------------------------------+
+|hosts_require_dane|Use: smtp|Type: host list*|Default: unset|
++------------------------------------------------------------+
+
+If built with DANE support, Exim will require that a DNSSEC-validated TLSA
+record is present for any host matching the list, and that a DANE-verified TLS
+connection is made. There will be no fallback to in-clear communication. See
+the dnssec_request_domains router and transport options. See section 43.18.
+
++------------------------------------------------------------+
+|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 43 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 and hosts_require_auth permits, Exim will try
+to transfer the message unauthenticated. See also chapter 33 for details of
+authentication.
+
++--------------------------------------------------------+
+|hosts_try_chunking|Use: smtp|Type: host list*|Default: *|
++--------------------------------------------------------+
+
+This option provides a list of servers to which, provided they announce
+CHUNKING support, Exim will attempt to use BDAT commands rather than DATA.
+Unless DKIM signing is being done, BDAT will not be used in conjunction with a
+transport filter.
+
++----------------------------------------------------+
+|hosts_try_dane|Use: smtp|Type: host list*|Default: *|
++----------------------------------------------------+
+
+If built with DANE support, Exim will look up a TLSA record for any host
+matching the list, If one is found and that lookup was DNSSEC-validated, then
+Exim requires that a DANE-verified TLS connection is made for that host; there
+will be no fallback to in-clear communication. See the dnssec_request_domains
+router and transport options. See section 43.18.
+
++--------------------------------------------------------+
+|hosts_try_fastopen|Use: smtp|Type: host list*|Default: *|
++--------------------------------------------------------+
+
+This option provides a list of servers to which, provided the facility is
+supported by this system, Exim will attempt to perform a TCP Fast Open. No data
+is sent on the SYN segment but, if the remote server also supports the
+facility, it can send its SMTP banner immediately after the SYN,ACK segment.
+This can save up to one round-trip time.
+
+The facility is only active for previously-contacted servers, as the initiator
+must present a cookie in the SYN segment.
+
+On (at least some) current Linux distributions the facility must be enabled in
+the kernel by the sysadmin before the support is usable. There is no option for
+control of the server side; if the system supports it it is always enabled.
+Note that lengthy operations in the connect ACL, such as DNSBL lookups, will
+still delay the emission of the SMTP banner.
+
++----------------------------------------------------+
+|hosts_try_prdr|Use: smtp|Type: host list*|Default: *|
++----------------------------------------------------+
+
+This option provides a list of servers to which, provided they announce PRDR
+support, Exim will attempt to negotiate PRDR for multi-recipient messages. The
+option can usually be left as default.
+
++-----------------------------------------------------+
+|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 (6.21). 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. A value setting of zero disables the limit.
+
++-------------------------------------------------------------+
+|message_linelength_limit|Use: smtp|Type: integer|Default: 998|
++-------------------------------------------------------------+
+
+This option sets the maximum line length, in bytes, that the transport will
+send. Any messages with lines exceeding the given value will fail and a
+failure-DSN ("bounce") message will if possible be returned to the sender. The
+default value is that defined by the SMTP standards.
+
+It is generally wise to also check in the data ACL so that messages received
+via SMTP can be refused without producing a bounce.
+
++---------------------------------------------------+
+|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.
+
+It is expanded per-address and can depend on any of $address_data, $domain_data
+, $local_part_data, $host, $host_address and $host_port.
+
+If the connection is DANE-enabled then this option is ignored; only messages
+having the domain used for the DANE TLSA lookup are sent on the connection.
+
++-----------------------------------------------+
+|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" and if protocol
+is set to "smtps" the default is "smtps". If the expansion fails, or if a port
+number cannot be found, delivery is deferred.
+
+Note that at least one Linux distribution has been seen failing to put "smtps"
+in its "/etc/services" file, resulting is such deferrals.
+
++---------------------------------------------+
+|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 value 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 used to strongly discourage use of this mode, but as of RFC
+8314 it is preferred over STARTTLS for message submission (as distinct from
+MTA-MTA communication).
+
++---------------------------------------------------------------+
+|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. Since it is expanded it can be made to depend
+on the host or domain.
+
++---------------------------------------------------------+
+|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.
+
+See also the max_parallel generic transport option.
+
++---------------------------------------------------+
+|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.
+
++--------------------------------------------------+
+|socks_proxy|Use: smtp|Type: string*|Default: unset|
++--------------------------------------------------+
+
+This option enables use of SOCKS proxies for connections made by the transport.
+For details see section 59.2.
+
++-----------------------------------------------+
+|tls_alpn|Use: smtp|Type: string*|Default: unset|
++-----------------------------------------------+
+
+If this option is set and the TLS library supports ALPN, the value given is
+used.
+
+As of writing no value has been standardised for email use. The authors suggest
+using "smtp".
+
++------------------------------------------------------+
+|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 43 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 43 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 43 for details of TLS; note that this option is used in
+different ways by OpenSSL and GnuTLS (see sections 43.4 and 43.5). For GnuTLS,
+the order of the ciphers is a preference order.
+
++--------------------------------------------------------------+
+|tls_resumption_hosts|Use: smtp|Type: host list*|Default: unset|
++--------------------------------------------------------------+
+
+This option controls which connections to use the TLS resumption feature. See
+43.17 for details.
+
++----------------------------------------------+
+|tls_sni|Use: smtp|Type: string*|Default: unset|
++----------------------------------------------+
+
+If this option is set and the connection is not DANE-validated 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 43.12 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*|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 matched. The
+$tls_out_certificate_verified variable is set when certificate verification
+succeeds.
+
++---------------------------------------------------------------+
+|tls_verify_cert_hostnames|Use: smtp|Type: host list*|Default: *|
++---------------------------------------------------------------+
+
+This option give a list of hosts for which, while verifying the server
+certificate, checks will be included on the host name (note that this will
+generally be the result of a DNS MX lookup) versus Subject and
+Subject-Alternate-Name fields. Wildcard names are permitted limited to being
+the initial component of a 3-or-more component FQDN.
+
+There is no equivalent checking on client certificates.
+
++---------------------------------------------------------------+
+|tls_verify_certificates|Use: smtp|Type: string*|Default: system|
++---------------------------------------------------------------+
+
+The value of this option must be either the word "system" or the absolute path
+to a file or directory containing permitted certificates for servers, for use
+when setting up an encrypted connection.
+
+The "system" value for the option will use a location compiled into the SSL
+library. This is not available for GnuTLS versions preceding 3.0.20; a value of
+"system" is taken as empty and an explicit location must be specified.
+
+The use of a directory for the option value is not available for GnuTLS
+versions preceding 3.3.6 and a single file must be used.
+
+With OpenSSL the certificates specified explicitly either by file or directory
+are added to those given by the system default location.
+
+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 43 for details of TLS.
+
+For back-compatibility, if neither tls_verify_hosts nor tls_try_verify_hosts
+are set (a single-colon empty list counts as being set) and certificate
+verification fails the TLS connection is closed.
+
++----------------------------------------------------------+
+|tls_verify_hosts|Use: smtp|Type: host list*|Default: unset|
++----------------------------------------------------------+
+
+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. Warning: Including a host in
+tls_verify_hosts does not require that connections use TLS. Fallback to
+in-clear communication will be done unless restricted by the hosts_require_tls
+option.
+
++-----------------------------------------------------+
+|utf8_downconvert|Use: smtp|Type: integer*|Default: -1|
++-----------------------------------------------------+
+
+If built with internationalization support, this option controls conversion of
+UTF-8 in message envelope addresses to a-label form. If, after expansion, the
+value is 1, 0, or -1 then this value overrides any value previously set for the
+message. Otherwise, any previously set value is used. To permit use of a
+previous value, set this option to an empty string. For details on the values
+see section 60.1.
+
+
+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 runtime
+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.20). 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 gets its
+ default at build time.
+
+ 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.6). 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 49.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.20). 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.
+
+lookup
+
+ A DNS lookup for a host failed. Note that a dnslookup router will need to
+ have matched its fail_defer_domains option for this retry type to be
+ usable. Also note that a manualroute router will probably need its
+ host_find_failed option set to defer.
+
+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 54). 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, as set by the
+retry_data_expire option, 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 in 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 49.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 runtime 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.
+
+The name of an authenticator is limited to be 64 ASCII characters long; prior
+to Exim 4.95 names would be silently truncated at this length, but now it is
+enforced.
+
+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_EXTERNAL=yes
+AUTH_GSASL=yes
+AUTH_HEIMDAL_GSSAPI=yes
+AUTH_PLAINTEXT=yes
+AUTH_SPA=yes
+AUTH_TLS=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 for negotiation of authentication done via non-SMTP means, as defined
+by RFC 4422 Appendix A. The fifth provides an interface to the GNU SASL
+authentication library, which provides mechanisms but typically not data
+sources. The sixth provides direct access to Heimdal GSSAPI, geared for
+Kerberos, but supporting setting a server keytab. The seventh 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
+eighth authenticator supports Microsoft's Secure Password Authentication
+mechanism. The last is an Exim authenticator but not an SMTP one; instead it
+can use information from a TLS negotiation.
+
+The authenticators are configured using the same syntax as other drivers (see
+section 6.23). 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 messages. 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.3 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. On a failing
+authentication the expansion result is instead saved in the
+$authenticated_fail_id variable. 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 *).
+
+ * If 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.
+
+Successful authentication sets up information used by the authresults expansion
+item.
+
+
+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 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.
+
+Note that the hostlist test for whether to do authentication can be confused if
+name-IP lookups change between the time the peer is decided upon and the time
+that the transport runs. For example, with a manualroute router given a host
+name, and with DNS "round-robin" used by that name: if the local resolver cache
+times out between the router and the transport running, the transport may get
+an IP for the name for its authentication check which does not match the
+connection peer IP. No authentication will then be done, despite the names
+being identical.
+
+For such cases use a separate transport which always authenticates.
+
+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 43) 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 Avoiding cleartext use
+---------------------------
+
+The following generic option settings will disable plaintext authenticators
+when TLS is not being used:
+
+ server_advertise_condition = ${if def:tls_in_cipher }
+ client_condition = ${if def:tls_out_cipher}
+
+Note: a plaintext SMTP AUTH done inside TLS is not vulnerable to casual
+snooping, but is still vulnerable to a Man In The Middle attack unless
+certificates (including their names) have been properly verified.
+
+
+34.2 Plaintext server 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 list*|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.3 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.4 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.5 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.6 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.7 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. Note that due to the
+ambiguity of parsing three consectutive circumflex characters there is no way
+to provide a password having a leading circumflex.
+
+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}{$value}fail}
+ 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 while at A
+L Digital Ltd.
+
+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 2 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 UNIX 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.
+
+The Dovecot configuration to match the above will look something like:
+
+conf.d/10-master.conf :-
+
+service auth {
+...
+#SASL
+ unix_listener auth-client {
+ mode = 0660
+ user = mail
+ }
+...
+}
+
+conf.d/10-auth.conf :-
+
+auth_mechanisms = plain login ntlm
+
+
+
+===============================================================================
+38. THE GSASL AUTHENTICATOR
+
+The gsasl authenticator provides 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.
+
+The library is expected to add support in an upcoming realease for the
+SCRAM-SHA-256 method. The macro _HAVE_AUTH_GSASL_SCRAM_SHA_256 will be defined
+when this happens.
+
+To see the list of mechanisms supported by the library run Exim with "auth"
+debug enabled and look for a line containing "GNU SASL supports". Note however
+that some may not have been tested from Exim.
+
++----------------------------------------------------+
+|client_authz|Use: gsasl|Type: string*|Default: unset|
++----------------------------------------------------+
+
+This option can be used to supply an authorization id which is different to the
+authentication_id provided by client_username option. If unset or (after
+expansion) empty it is not used, which is the common case.
+
++-------------------------------------------------------------+
+|client_channelbinding|Use: gsasl|Type: boolean|Default: false|
++-------------------------------------------------------------+
+
+See server_channelbinding below.
+
++-------------------------------------------------------+
+|client_password|Use: gsasl|Type: string*|Default: unset|
++-------------------------------------------------------+
+
+This option is exapanded before use, and should result in the password to be
+used, in clear.
+
++-------------------------------------------------------+
+|client_username|Use: gsasl|Type: string*|Default: unset|
++-------------------------------------------------------+
+
+This option is exapanded before use, and should result in the account name to
+be used.
+
++--------------------------------------------------------+
+|client_spassword|Use: gsasl|Type: string*|Default: unset|
++--------------------------------------------------------+
+
+This option is only supported for library versions 1.9.1 and greater. The macro
+_HAVE_AUTH_GSASL_SCRAM_S_KEY will be defined when this is so.
+
+If a SCRAM mechanism is being used and this option is set and correctly sized
+it is used in preference to client_password. The value after expansion should
+be a 40 (for SHA-1) or 64 (for SHA-256) character string with the
+PBKDF2-prepared password, hex-encoded.
+
+Note that this value will depend on the salt and iteration-count supplied by
+the server. The option is expanded before use. During the expansion $auth1 is
+set with the client username, $auth2 with the iteration count, and $auth3 with
+the salt.
+
+The intent of this option is to support clients that can cache thes salted
+password to save on recalculation costs. The cache lookup should return an
+unusable value (eg. an empty string) if the salt or iteration count has changed
+
+If the authentication succeeds then the above variables are set, plus the
+calculated salted password value value in $auth4, during the expansion of the
+client_set_id option. A side-effect of this expansion can be used to prime the
+cache.
+
++-------------------------------------------------------------+
+|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 should have meant 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 only usable by mechanisms which support "channel binding"; at time of
+writing, that's the SCRAM family. When using this feature the "-PLUS" variants
+of the method names need to be used.
+
+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 might
+have switched the default to be true.
+
+This option was deprecated in previous releases due to doubts over the "Triple
+Handshake" vulnerability. Exim takes suitable precausions (requiring Extended
+Master Secret if TLS Session Resumption was used) for safety.
+
++-----------------------------------------------------------+
+|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: 4096|
++--------------------------------------------------------+
+
+This option provides data for the SCRAM family of mechanisms. The $auth1,
+$auth2 and $auth3 variables are available when this option is expanded.
+
+The result of expansion should be a decimal number, and represents both a
+lower-bound on the security, and a compute cost factor imposed on the client
+(if it does not cache results, or the server changes either the iteration count
+or the salt). A minimum value of 4096 is required by the standards for all
+current SCRAM mechanism variants.
+
++---------------------------------------------------------+
+|server_scram_salt|Use: gsasl|Type: string*|Default: unset|
++---------------------------------------------------------+
+
+This option provides data for the SCRAM family of mechanisms. The $auth1,
+$auth2 and $auth3 variables are available when this option is expanded. The
+value should be a base64-encoded string, of random data typically 4-to-16 bytes
+long. If unset or empty after expansion the library will provides a value for
+the protocol conversation.
+
++--------------------------------------------------+
+|server_key|Use: gsasl|Type: string*|Default: unset|
++--------------------------------------------------+
+
++---------------------------------------------------+
+|server_skey|Use: gsasl|Type: string*|Default: unset|
++---------------------------------------------------+
+
+These options can be used for the SCRAM family of mechanisms to provide stored
+information related to a password, the storage of which is preferable to
+plaintext.
+
+server_key is the value defined in the SCRAM standards as ServerKey;
+server_skey is StoredKey.
+
+They are only available for version 1.9.0 (or later) of the gsasl library. When
+this is so, the macros _OPT_AUTHENTICATOR_GSASL_SERVER_KEY and
+_HAVE_AUTH_GSASL_SCRAM_S_KEY will be defined.
+
+The $authN variables are available when these options are expanded.
+
+If set, the results of expansion should for each should be a 28 (for SHA-1) or
+44 (for SHA-256) character string of base64-coded data, and will be used in
+preference to the server_password option. If unset or not of the right length,
+server_password will be used.
+
+The libgsasl library release includes a utility gsasl which can be used to
+generate these values.
+
++------------------------------------------------------+
+|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 identifier 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 (https://
+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. THE EXTERNAL AUTHENTICATOR
+
+The external authenticator provides support for authentication based on
+non-SMTP information. The specification is in RFC 4422 Appendix A (https://
+tools.ietf.org/html/rfc4422). It is only a transport and negotiation mechanism;
+the process of authentication is entirely controlled by the server
+configuration.
+
+The client presents an identity in-clear. It is probably wise for a server to
+only advertise, and for clients to only attempt, this authentication method on
+a secure (eg. under TLS) connection.
+
+One possible use, compatible with the K-9 Mail Android client (https://
+k9mail.github.io/), is for using X509 client certificates.
+
+It thus overlaps in function with the TLS authenticator (see 42) but is a full
+SMTP SASL authenticator rather than being implicit for TLS-connection carried
+client certificates only.
+
+The examples and discussion in this chapter assume that client-certificate
+authentication is being done.
+
+The client must present a certificate, for which it must have been requested
+via the tls_verify_hosts or tls_try_verify_hosts main options (see 43). For
+authentication to be effective the certificate should be verifiable against a
+trust-anchor certificate known to the server.
+
+
+41.1 External options
+---------------------
+
+The external authenticator has two server options:
+
++--------------------------------------------------------+
+|server_param2|Use: external|Type: string*|Default: unset|
++--------------------------------------------------------+
+
++--------------------------------------------------------+
+|server_param3|Use: external|Type: string*|Default: unset|
++--------------------------------------------------------+
+
+These options are expanded before the server_condition option and the result
+are placed in $auth2 and $auth3 resectively. If the expansion is forced to
+fail, authentication fails. Any other expansion failure causes a temporary
+error code to be returned.
+
+They can be used to clarify the coding of a complex server_condition.
+
+
+41.2 Using external in a server
+-------------------------------
+
+When running as a server, external 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. The decoded value is treated as an identity for
+authentication and placed in the expansion variable $auth1.
+
+For compatibility with previous releases of Exim, the value is also placed in
+the expansion variable $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 them for other things.
+
+Once an identity has 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.
+
+Example:
+
+ext_ccert_san_mail:
+ driver = external
+ public_name = EXTERNAL
+
+ server_advertise_condition = $tls_in_certificate_verified
+ server_param2 = ${certextract {subj_altname,mail,>:} \
+ {$tls_in_peercert}}
+ server_condition = ${if forany {$auth2} \
+ {eq {$item}{$auth1}}}
+ server_set_id = $auth1
+
+This accepts a client certificate that is verifiable against any of your
+configured trust-anchors (which usually means the full set of public CAs) and
+which has a mail-SAN matching the claimed identity sent by the client.
+
+Note: up to TLS1.2, the client cert is on the wire in-clear, including the SAN.
+The account name is therefore guessable by an opponent. TLS 1.3 protects both
+server and client certificates, and is not vulnerable in this way.
+
+
+41.3 Using external in a client
+-------------------------------
+
+The external authenticator has one client option:
+
++------------------------------------------------------+
+|client_send|Use: external|Type: string*|Default: unset|
++------------------------------------------------------+
+
+This option is expanded and sent with the AUTH command as the identity being
+asserted.
+
+Example:
+
+ext_ccert:
+ driver = external
+ public_name = EXTERNAL
+
+ client_condition = ${if !eq{$tls_out_cipher}{}}
+ client_send = myaccount@smarthost.example.net
+
+
+
+===============================================================================
+42. THE TLS AUTHENTICATOR
+
+The tls authenticator provides server support for authentication based on
+client certificates.
+
+It is not an SMTP authentication mechanism and is not advertised by the server
+as part of the SMTP EHLO response. It is an Exim authenticator in the sense
+that it affects the protocol element of the log line, can be tested for by the
+authenticated ACL condition, and can set the $authenticated_id variable.
+
+The client must present a verifiable certificate, for which it must have been
+requested via the tls_verify_hosts or tls_try_verify_hosts main options (see 43
+).
+
+If an authenticator of this type is configured it is run before any SMTP-level
+communication is done, and can authenticate the connection. If it does, SMTP
+authentication is not offered.
+
+A maximum of one authenticator of this type may be present.
+
+The tls authenticator has three server options:
+
++---------------------------------------------------+
+|server_param1|Use: tls|Type: string*|Default: unset|
++---------------------------------------------------+
+
+This option is expanded after the TLS negotiation and the result is placed in
+$auth1. If the expansion is forced to fail, authentication fails. Any other
+expansion failure causes a temporary error code to be returned.
+
++---------------------------------------------------+
+|server_param2|Use: tls|Type: string*|Default: unset|
++---------------------------------------------------+
+
++---------------------------------------------------+
+|server_param3|Use: tls|Type: string*|Default: unset|
++---------------------------------------------------+
+
+As above, for $auth2 and $auth3.
+
+server_param1 may also be spelled server_param.
+
+Example:
+
+tls:
+ driver = tls
+ server_param1 = ${certextract {subj_altname,mail,>:} \
+ {$tls_in_peercert}}
+ server_condition = ${if and { {eq{$tls_in_certificate_verified}{1}} \
+ {forany {$auth1} \
+ {!= {0} \
+ {${lookup ldap{ldap:///\
+ mailname=${quote_ldap_dn:${lc:$item}},\
+ ou=users,LDAP_DC?mailid} {$value}{0} \
+ } } } }}}
+ server_set_id = ${if = {1}{${listcount:$auth1}} {$auth1}{}}
+
+This accepts a client certificate that is verifiable against any of your
+configured trust-anchors (which usually means the full set of public CAs) and
+which has a SAN with a good account name.
+
+Note that, up to TLS1.2, the client cert is on the wire in-clear, including the
+SAN, The account name is therefore guessable by an opponent. TLS 1.3 protects
+both server and client certificates, and is not vulnerable in this way.
+Likewise, a traditional plaintext SMTP AUTH done inside TLS is not.
+
+Note that because authentication is traditionally an SMTP operation, the
+authenticated ACL condition cannot be used in a connect- or helo-ACL.
+
+
+
+===============================================================================
+43. 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.
+
+
+43.1 Support for the "submissions" (aka "ssmtp" and "smtps") protocol
+---------------------------------------------------------------------
+
+The history of port numbers for TLS in SMTP is a little messy and has been
+contentious. As of RFC 8314, the common practice of using the historically
+allocated port 465 for "email submission but with TLS immediately upon connect
+instead of using STARTTLS" is officially blessed by the IETF, and recommended
+by them in preference to STARTTLS.
+
+The name originally assigned to the port was "ssmtp" or "smtps", but as clarity
+emerged over the dual roles of SMTP, for MX delivery and Email Submission,
+nomenclature has shifted. The modern name is now "submissions".
+
+This approach was, for a while, officially abandoned when encrypted SMTP was
+standardized, but many clients kept using it, even as the TCP port number was
+reassigned for other use. Thus you may encounter guidance claiming that you
+shouldn't enable use of this port. In practice, a number of mail-clients have
+only ever supported submissions, not submission with STARTTLS upgrade. Ideally,
+offer both submission (587) and submissions (465) service.
+
+Exim supports TLS-on-connect 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 TLS-only behaviour for all ports.
+
+
+43.2 OpenSSL vs GnuTLS
+----------------------
+
+TLS is supported in Exim using either the OpenSSL or GnuTLS library. To build
+Exim to use OpenSSL you need to set
+
+USE_OPENSSL=yes
+
+in Local/Makefile.
+
+To build Exim to use GnuTLS, you need to set
+
+USE_GNUTLS=yes
+
+in Local/Makefile.
+
+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 cannot be the path of a directory for
+ GnuTLS versions before 3.3.6 (for later versions, or 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 43.4 and 43.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).
+
+ * With GnuTLS, if an explicit list is used for the tls_privatekey main option
+ main option, it must be ordered to match the tls_certificate list.
+
+ * 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.
+
+ * The output from "exim -bV" will show which (if any) support was included in
+ the build. Also, the macro "_HAVE_OPENSSL" or "_HAVE_GNUTLS" will be
+ defined.
+
+
+43.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.
+
+
+43.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 for TLS versions prior to 1.3. 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}}
+
+This example will prefer ECDSA-authenticated ciphers over RSA ones:
+
+tls_require_ciphers = ECDSA:RSA:!COMPLEMENTOFDEFAULT
+
+For TLS version 1.3 the control available is less fine-grained and Exim does
+not provide access to it at present. The value of the tls_require_ciphers
+option is ignored when TLS version 1.3 is negotiated.
+
+As of writing the library default cipher suite list for TLSv1.3 is
+
+TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256
+
+
+43.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 and
+controls both protocols and ciphers.
+
+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 https://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 https://www.gnutls.org/manual/gnutls.html#
+Listing-the-ciphersuites-in-a-priority-string on that site can be used to test
+a given string.
+
+For example:
+
+# Disable older versions of protocols
+tls_require_ciphers = NORMAL:%LATEST_RECORD_VERSION:-VERS-SSL3.0
+
+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}}
+
+
+43.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 *, which means that STARTTLS is
+always advertised. Set it to blank to never advertise; this is reasonable for
+systems that want to use TLS only as a client.
+
+If STARTTLS is to be used you need to set some other options in order to make
+TLS available.
+
+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, the tls_advertise_hosts option must be
+set to match some hosts. The default is * which matches all hosts.
+
+If this is all you do, TLS encryption will be enabled but not authentication -
+meaning that the peer has no assurance it is actually you he is talking to. You
+gain protection from a passive sniffer listening on the wire but not from
+someone able to intercept the communication.
+
+Further protection requires some further configuration at the server end.
+
+To make TLS 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 PEM format and readable by the Exim
+user, and must always be given as full path names. The key must not be
+password-protected. 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.
+
+For dual-stack (eg. RSA and ECDSA) configurations, these options can be
+colon-separated lists of file paths. Ciphers using given authentication
+algorithms require the presence of a suitable certificate to supply the
+public-key. The server selects among the certificates to present to the client
+depending on the selected cipher, hence the priority ordering for ciphers will
+affect which certificate is used.
+
+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 43.14.)
+
+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).
+
+
+43.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
+trust-anchors or certificates. These may be the system default set (depending
+on library version), an explicit file or, depending on library version, 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.
+
+There is no checking of names of the client against the certificate Subject
+Name or Subject Alternate Names.
+
+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.
+
+
+43.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 they 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.3.16 / 3.4.8 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.
+
+
+43.9 Caching of static server configuration items
+-------------------------------------------------
+
+If any of the main configuration options tls_certificate, tls_privatekey,
+tls_crl and tls_ocsp_file have values with no expandable elements, then the
+associated information is loaded at daemon startup. It is made available to
+child processes forked for handling received SMTP connections.
+
+This caching is currently only supported under Linux and FreeBSD.
+
+If caching is not possible, for example if an item has to be dependent on the
+peer host so contains a $sender_host_name expansion, the load of the associated
+information is done at the startup of the TLS connection.
+
+The cache is invalidated and reloaded after any changes to the directories
+containing files specified by these options.
+
+The information specified by the main option tls_verify_certificates is
+similarly cached so long as it specifies files explicitly or (under GnuTLS) is
+the string "system,cache". The latter case is not automatically invalidated; it
+is the operator's responsibility to arrange for a daemon restart any time the
+system certificate authority bundle is updated. A HUP signal is sufficient for
+this. The value "system" results in no caching under GnuTLS.
+
+The macro _HAVE_TLS_CA_CACHE will be defined if the suffix for "system" is
+acceptable in configurations for the Exim executavble.
+
+Caching of the system Certificate Authorities bundle can save siginificant time
+and processing on every TLS connection accepted by Exim.
+
+
+43.10 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.
+This is an optional thing for TLS connections, although either end may insist
+on it. If the server is Exim, it will request a certificate only if
+tls_verify_hosts or tls_try_verify_hosts matches the client.
+
+Note: Do not use a certificate which has the OCSP-must-staple extension, for
+client use (they are usable for server use). As the TLS protocol has no means
+for the client to staple before TLS 1.3 it will result in failed connections.
+
+If the tls_verify_certificates option is set on the smtp transport, it
+specifies a collection of expected server certificates. These may be the system
+default set (depending on library version), a file, or (depending on library
+version) a directory. 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 tls_verify_cert_hostnames option lists hosts for which additional name
+checks are made on the server certificate. The match against this list is, as
+per other Exim usage, the IP for the host. That is most closely associated with
+the name on the DNS A (or AAAA) record for the host. However, the name that
+needs to be in the certificate is the one at the head of any CNAME chain
+leading to the A record. The option defaults to always checking.
+
+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.
+
+
+43.11 Caching of static client configuration items
+--------------------------------------------------
+
+If any of the transport configuration options tls_certificate, tls_privatekey
+and tls_crl have values with no expandable elements, then the associated
+information is loaded per smtp transport at daemon startup, at the start of a
+queue run, or on a command-line specified message delivery. It is made
+available to child processes forked for handling making SMTP connections.
+
+This caching is currently only supported under Linux.
+
+If caching is not possible, the load of the associated information is done at
+the startup of the TLS connection.
+
+The cache is invalidated in the daemon and reloaded after any changes to the
+directories containing files specified by these options.
+
+The information specified by the main option tls_verify_certificates is
+similarly cached so long as it specifies files explicitly or (under GnuTLS) is
+the string "system,cache". The latter case is not automatically invaludated; it
+is the operator's responsibility to arrange for a daemon restart any time the
+system certificate authority bundle is updated. A HUP signal is sufficient for
+this. The value "system" results in no caching under GnuTLS.
+
+The macro _HAVE_TLS_CA_CACHE will be defined if the suffix for "system" is
+acceptable in configurations for the Exim executavble.
+
+Caching of the system Certificate Authorities bundle can save siginificant time
+and processing on every TLS connection initiated by Exim.
+
+
+43.12 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 analogous 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).
+
+If DANE validated the connection attempt then the value of the tls_sni option
+is forced to the name of the destination host, after any MX- or
+CNAME-following.
+
+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_ocsp_file
+
+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_in_sni is arbitrary
+unverified data provided prior to authentication. Further, the initial
+certificate is loaded before SNI has arrived, so an expansion for
+tls_certificate must have a default which is used when $tls_in_sni is empty.
+
+The Exim developers are proceeding cautiously and so far no other TLS options
+are re-expanded.
+
+When Exim is built against 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).
+
+There is a TLS feature related to SNI called Application Layer Protocol Name
+(ALPN). This is intended to declare, or select, what protocol layer will be
+using a TLS connection. The client for the connection proposes a set of
+protocol names, and the server responds with a selected one. It is not, as of
+2021, commonly used for SMTP connections. However, to guard against misirected
+or malicious use of web clients (which often do use ALPN) against MTA ports,
+Exim by default check that there is no incompatible ALPN specified by a client
+for a TLS connection. If there is, the connection is rejected.
+
+As a client Exim does not supply ALPN by default. The behaviour of both client
+and server can be configured using the options tls_alpn and hosts_require_alpn.
+There are no variables providing observability. Some feature-specific logging
+may appear on denied connections, but this depends on the behavious of the peer
+(not all peers can send a feature-specific TLS Alert).
+
+This feature is available when Exim is built with OpenSSL 1.1.0 or later or
+GnuTLS 3.2.0 or later; the macro _HAVE_TLS_ALPN will be defined when this is
+so.
+
+
+43.13 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, for sending using TLS Exim
+starts an additional proxy process for handling the encryption, piping the
+unencrypted data stream from and to the delivery processes.
+
+An older mode of operation can be enabled on a per-host basis by the
+hosts_noproxy_tls option on the smtp transport. If the host matches this list
+the proxy process described above is not used; instead Exim shuts down an
+existing TLS session being run by the delivery process 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.
+
+
+43.14 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 a large
+topic and an introductory guide is unsuitable for the Exim reference manual, so
+instead we provide pointers to existing documentation.
+
+The Apache web-server was for a long time the canonical guide, so their
+documentation is a good place to start; their SSL module's Introduction
+document is currently at
+
+https://httpd.apache.org/docs/current/ssl/ssl_intro.html
+
+and their FAQ is at
+
+https://httpd.apache.org/docs/current/ssl/ssl_faq.html
+
+Eric Rescorla's book, SSL and TLS, published by Addison-Wesley (ISBN
+0-201-61598-3) in 2001, contains both introductory and more in-depth
+descriptions. More recently Ivan Risti?'s book Bulletproof SSL and TLS,
+published by Feisty Duck (ISBN 978-1907117046) in 2013 is good. Ivan is the
+author of the popular TLS testing tools at https://www.ssllabs.com/.
+
+
+43.15 Certificate chains
+------------------------
+
+A 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.
+
+
+43.16 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 https://sourceforge.net/projects/
+ospkibook/.
+
+
+43.17 TLS Resumption
+--------------------
+
+TLS Session Resumption for TLS 1.2 and TLS 1.3 connections can be used (defined
+in RFC 5077 for 1.2). The support for this requires GnuTLS 3.6.3 or OpenSSL
+1.1.1 (or later).
+
+Session resumption (this is the "stateless" variant) involves the server
+sending a "session ticket" to the client on one connection, which can be stored
+by the client and used for a later session. The ticket contains sufficient
+state for the server to reconstruct the TLS session, avoiding some expensive
+crypto calculation and (on TLS1.2) one full packet roundtrip time.
+
+ * Operational cost/benefit:
+
+ The extra data being transmitted costs a minor amount, and the client has
+ extra costs in storing and retrieving the data.
+
+ In the Exim/Gnutls implementation the extra cost on an initial connection
+ which is TLS1.2 over a loopback path is about 6ms on 2017-laptop class
+ hardware. The saved cost on a subsequent connection is about 4ms; three or
+ more connections become a net win. On longer network paths, two or more
+ connections will have an average lower startup time thanks to the one saved
+ packet roundtrip. TLS1.3 will save the crypto cpu costs but not any packet
+ roundtrips.
+
+ Since a new hints DB is used on the TLS client, the hints DB maintenance
+ should be updated to additionally handle "tls".
+
+ * Security aspects:
+
+ The session ticket is encrypted, but is obviously an additional security
+ vulnarability surface. An attacker able to decrypt it would have access all
+ connections using the resumed session. The session ticket encryption key is
+ not committed to storage by the server and is rotated regularly (OpenSSL:
+ 1hr, and one previous key is used for overlap; GnuTLS 6hr but does not
+ specify any overlap). Tickets have limited lifetime (2hr, and new ones
+ issued after 1hr under OpenSSL. GnuTLS 2hr, appears to not do overlap).
+
+ There is a question-mark over the security of the Diffie-Helman parameters
+ used for session negotiation.
+
+ * Observability:
+
+ The log_selector "tls_resumption" appends an asterisk to the tls_cipher "X=
+ " element.
+
+ The variables $tls_in_resumption and $tls_out_resumption have bits 0-4
+ indicating respectively support built, client requested ticket, client
+ offered session, server issued ticket, resume used. A suitable decode list
+ is provided in the builtin macro _RESUME_DECODE for in listextract
+ expansions.
+
+ * Control:
+
+ The tls_resumption_hosts main option specifies a hostlist for which exim,
+ operating as a server, will offer resumption to clients. Current best
+ practice is to not offer the feature to MUA connection. Commonly this can
+ be done like this:
+
+ tls_resumption_hosts = ${if inlist {$received_port}{587:465} {:}{*}}
+
+ If the peer host matches the list after expansion then resumption is
+ offered and/or accepted.
+
+ The tls_resumption_hosts smtp transport option performs the equivalent
+ function for operation as a client. If the peer host matches the list after
+ expansion then resumption is attempted (if a stored session is available)
+ or the information stored (if supplied by the peer).
+
+ * Issues:
+
+ In a resumed session:
+
+ + The variables $tls_{in,out}_cipher will have values different to the
+ original (under GnuTLS).
+
+ + The variables $tls_{in,out}_ocsp will be "not requested" or "no
+ response", and the hosts_require_ocsp smtp trasnport option will fail.
+
+
+43.18 DANE
+----------
+
+DNS-based Authentication of Named Entities, as applied to SMTP over TLS,
+provides assurance to a client that it is actually talking to the server it
+wants to rather than some attacker operating a Man In The Middle (MITM)
+operation. The latter can terminate the TLS connection you make, and make
+another one to the server (so both you and the server still think you have an
+encrypted connection) and, if one of the "well known" set of Certificate
+Authorities has been suborned - something which *has* been seen already (2014),
+a verifiable certificate (if you're using normal root CAs, eg. the Mozilla set,
+as your trust anchors).
+
+What DANE does is replace the CAs with the DNS as the trust anchor. The
+assurance is limited to a) the possibility that the DNS has been suborned, b)
+mistakes made by the admins of the target server. The attack surface presented
+by (a) is thought to be smaller than that of the set of root CAs.
+
+It also allows the server to declare (implicitly) that connections to it should
+use TLS. An MITM could simply fail to pass on a server's STARTTLS.
+
+DANE scales better than having to maintain (and communicate via side-channel)
+copies of server certificates for every possible target server. It also scales
+(slightly) better than having to maintain on an SMTP client a copy of the
+standard CAs bundle. It also means not having to pay a CA for certificates.
+
+DANE requires a server operator to do three things: 1) run DNSSEC. This
+provides assurance to clients that DNS lookups they do for the server have not
+been tampered with. The domain MX record applying to this server, its A record,
+its TLSA record and any associated CNAME records must all be covered by DNSSEC.
+2) add TLSA DNS records. These say what the server certificate for a TLS
+connection should be. 3) offer a server certificate, or certificate chain, in
+TLS connections which is is anchored by one of the TLSA records.
+
+There are no changes to Exim specific to server-side operation of DANE. Support
+for client-side operation of DANE can be included at compile time by defining
+SUPPORT_DANE=yes in Local/Makefile. If it has been included, the macro
+"_HAVE_DANE" will be defined.
+
+A TLSA record consist of 4 fields, the "Certificate Usage", the "Selector", the
+"Matching type", and the "Certificate Association Data". For a detailed
+description of the TLSA record see RFC 7671.
+
+The TLSA record for the server may have "Certificate Usage" (1st) field of
+DANE-TA(2) or DANE-EE(3). These are the "Trust Anchor" and "End Entity"
+variants. The latter specifies the End Entity directly, i.e. the certificate
+involved is that of the server (and if only DANE-EE is used then it should be
+the sole one transmitted during the TLS handshake); this is appropriate for a
+single system, using a self-signed certificate. DANE-TA usage is effectively
+declaring a specific CA to be used; this might be a private CA or a public,
+well-known one. A private CA at simplest is just a self-signed certificate
+(with certain attributes) which is used to sign server certificates, but
+running one securely does require careful arrangement. With DANE-TA, as
+implemented in Exim and commonly in other MTAs, the server TLS handshake must
+transmit the entire certificate chain from CA to server-certificate. DANE-TA is
+commonly used for several services and/or servers, each having a TLSA
+query-domain CNAME record, all of which point to a single TLSA record. DANE-TA
+and DANE-EE can both be used together.
+
+Our recommendation is to use DANE with a certificate from a public CA, because
+this enables a variety of strategies for remote clients to verify your
+certificate. You can then publish information both via DANE and another
+technology, "MTA-STS", described below.
+
+When you use DANE-TA to publish trust anchor information, you ask entities
+outside your administrative control to trust the Certificate Authority for
+connections to you. If using a private CA then you should expect others to
+still apply the technical criteria they'd use for a public CA to your
+certificates. In particular, you should probably try to follow current best
+practices for CA operation around hash algorithms and key sizes. Do not expect
+other organizations to lower their security expectations just because a
+particular profile might be reasonable for your own internal use.
+
+When this text was last updated, this in practice means to avoid use of SHA-1
+and MD5; if using RSA to use key sizes of at least 2048 bits (and no larger
+than 4096, for interoperability); to use keyUsage fields correctly; to use
+random serial numbers. The list of requirements is subject to change as best
+practices evolve. If you're not already using a private CA, or it doesn't meet
+these requirements, then we encourage you to avoid all these issues and use a
+public CA such as Let's Encrypt instead.
+
+The TLSA record should have a "Selector" (2nd) field of SPKI(1) and a "Matching
+Type" (3rd) field of SHA2-512(2).
+
+For the "Certificate Authority Data" (4th) field, commands like
+
+ openssl x509 -pubkey -noout <certificate.pem \
+ | openssl rsa -outform der -pubin 2>/dev/null \
+ | openssl sha512 \
+ | awk '{print $2}'
+
+are workable to create a hash of the certificate's public key.
+
+An example TLSA record for DANE-EE(3), SPKI(1), and SHA-512 (2) looks like
+
+ _25._tcp.mail.example.com. TLSA 3 1 2 8BA8A336E...
+
+At the time of writing, https://www.huque.com/bin/gen_tlsa is useful for
+quickly generating TLSA records.
+
+For use with the DANE-TA model, server certificates must have a correct name
+(SubjectName or SubjectAltName).
+
+The Certificate issued by the CA published in the DANE-TA model should be
+issued using a strong hash algorithm. Exim, and importantly various other MTAs
+sending to you, will not re-enable hash algorithms which have been disabled by
+default in TLS libraries. This means no MD5 and no SHA-1. SHA2-256 is the
+minimum for reliable interoperability (and probably the maximum too, in 2018).
+
+The use of OCSP-stapling should be considered, allowing for fast revocation of
+certificates (which would otherwise be limited by the DNS TTL on the TLSA
+records). However, this is likely to only be usable with DANE-TA. NOTE: the
+default of requesting OCSP for all hosts is modified iff DANE is in use, to:
+
+ hosts_request_ocsp = ${if or { {= {0}{$tls_out_tlsa_usage}} \
+ {= {4}{$tls_out_tlsa_usage}} } \
+ {*}{}}
+
+The (new) variable $tls_out_tlsa_usage is a bitfield with numbered bits set for
+TLSA record usage codes. The zero above means DANE was not in use, the four
+means that only DANE-TA usage TLSA records were found. If the definition of
+hosts_request_ocsp includes the string "tls_out_tlsa_usage", they are
+re-expanded in time to control the OCSP request.
+
+This modification of hosts_request_ocsp is only done if it has the default
+value of "*". Admins who change it, and those who use hosts_require_ocsp,
+should consider the interaction with DANE in their OCSP settings.
+
+For client-side DANE there are three new smtp transport options, hosts_try_dane
+, hosts_require_dane and dane_require_tls_ciphers. The "require" variant will
+result in failure if the target host is not DNSSEC-secured. To get
+DNSSEC-secured hostname resolution, use the dnssec_request_domains router or
+transport option.
+
+DANE will only be usable if the target host has DNSSEC-secured MX, A and TLSA
+records.
+
+A TLSA lookup will be done if either of the above options match and the
+host-lookup succeeded using DNSSEC. If a TLSA lookup is done and succeeds, a
+DANE-verified TLS connection will be required for the host. If it does not, the
+host will not be used; there is no fallback to non-DANE or non-TLS.
+
+If DANE is requested and usable, then the TLS cipher list configuration prefers
+to use the option dane_require_tls_ciphers and falls back to
+tls_require_ciphers only if that is unset. This lets you configure "decent
+crypto" for DANE and "better than nothing crypto" as the default. Note though
+that while GnuTLS lets the string control which versions of TLS/SSL will be
+negotiated, OpenSSL does not and you're limited to ciphersuite constraints.
+
+If DANE is requested and useable (see above) the following transport options
+are ignored:
+
+ hosts_require_tls
+ tls_verify_hosts
+ tls_try_verify_hosts
+ tls_verify_certificates
+ tls_crl
+ tls_verify_cert_hostnames
+ tls_sni
+
+If DANE is not usable, whether requested or not, and CA-anchored verification
+evaluation is wanted, the above variables should be set appropriately.
+
+The router and transport option dnssec_request_domains must not be set to
+"never", and dnssec_require_domains is ignored.
+
+If verification was successful using DANE then the "CV" item in the delivery
+log line will show as "CV=dane".
+
+There is a new variable $tls_out_dane which will have "yes" if verification
+succeeded using DANE and "no" otherwise (only useful in combination with
+events; see 61), and a new variable $tls_out_tlsa_usage (detailed above).
+
+An event (see 61) of type "dane:fail" will be raised on failures to achieve
+DANE-verified connection, if one was either requested and offered, or required.
+This is intended to support TLS-reporting as defined in https://tools.ietf.org/
+html/draft-ietf-uta-smtp-tlsrpt-17. The $event_data will be one of the Result
+Types defined in Section 4.3 of that document.
+
+Under GnuTLS, DANE is only supported from version 3.0.0 onwards.
+
+DANE is specified in published RFCs and decouples certificate authority trust
+selection from a "race to the bottom" of "you must trust everything for mail to
+get through". There is an alternative technology called MTA-STS, which instead
+publishes MX trust anchor information on an HTTPS website. At the time this
+text was last updated, MTA-STS was still a draft, not yet an RFC. Exim has no
+support for MTA-STS as a client, but Exim mail server operators can choose to
+publish information describing their TLS configuration using MTA-STS to let
+those clients who do use that protocol derive trust information.
+
+The MTA-STS design requires a certificate from a public Certificate Authority
+which is recognized by clients sending to you. That selection of which CAs are
+trusted by others is outside your control.
+
+The most interoperable course of action is probably to use Let's Encrypt, with
+automated certificate renewal; to publish the anchor information in
+DNSSEC-secured DNS via TLSA records for DANE clients (such as Exim and Postfix)
+and to publish anchor information for MTA-STS as well. This is what is done for
+the exim.org domain itself (with caveats around occasionally broken MTA-STS
+because of incompatible specification changes prior to reaching RFC status).
+
+
+
+===============================================================================
+44. ACCESS CONTROL LISTS
+
+Access Control Lists (ACLs) are defined in a separate section of the runtime
+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.
+
+
+44.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.
+
+
+44.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_dkim ACL for each DKIM signer
+ 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.
+
+
+44.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 45.
+
+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.
+
+
+44.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.
+
+For tls-on-connect connections, the ACL is run after the TLS connection is
+accepted (however, host_reject_connection is tested before).
+
+
+44.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.
+
+Note also that a deny neither forces the client to go away nor means that mail
+will be refused on the connection. Consider checking for $sender_helo_name
+being defined in a MAIL or RCPT ACL to do that.
+
+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.
+
+
+44.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.
+
+If CHUNKING was advertised and a BDAT command sequence is received, the
+acl_smtp_predata ACL is not run. The acl_smtp_data is run after the last BDAT
+command and all of the data specified is received.
+
+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.
+
+
+44.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 section 58.1.
+
+
+44.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 45.
+
+This ACL is evaluated after acl_smtp_dkim but before acl_smtp_data.
+
+
+44.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 specified by acl_smtp_data_prdr happens after a message has been
+received, and is executed once for each recipient of the message with
+$local_part and $domain valid. The test may accept, defer or deny for
+individual 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
+variable $prdr_requested is "yes". 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 (e.g. 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.
+
+
+44.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, it may only accept or
+warn as its final result.
+
+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.
+
+
+44.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.
+
+
+44.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 an RFC 8314
+"submissions" 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 filename, 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.
+
+
+44.13 ACL return codes
+----------------------
+
+Except for the QUIT ACL, which does not affect the SMTP return code (see
+section 44.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.
+
+If the ACL for VRFY returns "accept", a recipient verify (without callout) is
+done on the address and the result determines the SMTP response.
+
+The local_scan() function is always run, even if there are no remaining
+recipients; it may create new recipients.
+
+
+44.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.
+
+
+44.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.
+
+
+44.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.)
+
+
+44.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.
+
+
+44.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 condition = ${if > {$rcpt_count}{20}}
+ message = I don't take more than 20 RCPTs
+
+ 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 44.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 44.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.
+
+
+44.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.
+
+
+44.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.
+
+
+44.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 44.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 44.22. The control modifier can be used in
+ several different ways. For example:
+
+ o It can be at the end of an accept statement:
+
+ accept ...some conditions
+ control = queue
+
+ In this case, the control is applied when this statement yields
+ "accept", in other words, when the conditions are all true.
+
+ o It can be in the middle of an accept statement:
+
+ accept ...some conditions...
+ control = queue
+ ...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.
+
+ o 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.
+
+ o 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.
+
+ While the text is being expanded, the $acl_verify_message variable contains
+ any message previously set. Afterwards, $acl_verify_message is cleared.
+
+ 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.
+
+queue = <text>
+
+ This modifier specifies the use of a named queue for spool files for the
+ message. It can only be used before the message is received (i.e. not in
+ the DATA ACL). This could be used, for example, for known high-volume burst
+ sources of traffic, or for quarantine of messages. Separate queue-runner
+ processes will be needed for named queues. If the text after expansion is
+ empty, the default queue is used.
+
+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 44.25.
+
+set <acl_name> = <value>
+
+ This modifier puts a value into one of the ACL variables (see section 44.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
+
+
+44.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/<options>
+
+ This option requests delivery be attempted while the item is being
+ received.
+
+ The option is usable in the RCPT ACL. If enabled for a message received via
+ smtp and routed to an smtp transport, and only one transport, interface,
+ destination host and port combination is used for all recipients of the
+ message, then the delivery connection is made while the receiving
+ connection is open and data is copied from one to the other.
+
+ An attempt to set this option for any recipient but the first for a mail
+ will be quietly ignored. If a recipient-verify callout (with use_sender)
+ connection is subsequently requested in the same ACL it is held open and
+ used for any subsequent recipients and the data, otherwise one is made
+ after the initial RCPT 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. The Received-By: header is generated
+ as soon as the body reception starts, rather than the traditional time
+ after the full message is received; this will affect the timestamp.
+
+ All the usual ACLs are called; if one results in the message being
+ rejected, all effort spent in delivery (including the costs on the ultimate
+ destination) will be wasted. Note that in the case of data-time ACLs this
+ includes the entire message body.
+
+ 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. It
+ is not supported for messages received with the SMTP PRDR or CHUNKING
+ options in use.
+
+ 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 the item is successfully delivered in cutthrough mode the
+ delivery log lines are tagged with ">>" rather than "=>" and appear before
+ the acceptance "<=" line.
+
+ If there is a temporary error the item is queued for later delivery in the
+ usual fashion. This behaviour can be adjusted by appending the option defer
+ =<value> to the control; the default value is "spool" and the alternate
+ value "pass" copies an SMTP defer response from the target back to the
+ initiator and does not queue the message. Note that this is independent of
+ any recipient verify conditions in the ACL.
+
+ 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 in the usual logs
+ directory, by default called debuglog.
+
+ Logging set up by the control will be maintained across spool residency.
+
+ Options are a slash-separated list. If an option takes an argument, the
+ option name and argument are separated by an equals character. Several
+ options are supported:
+
+ tag=<suffix> The filename can be adjusted with thise option.
+ The argument, which may access any variables already defined,
+ is appended to the default name.
+
+ opts=<debug options> The argument specififes what is to be logged,
+ using the same values as the -d command-line option.
+
+ stop Logging started with this control may be
+ stopped by using this option.
+
+ kill Logging started with this control may be
+ stopped by using this option.
+ Additionally the debug file will be removed,
+ providing one means for speculative debug tracing.
+
+ pretrigger=<size> This option specifies a memory buffuer to be used
+ for pre-trigger debug capture.
+ Debug lines are recorded in the buffer until
+ and if) a trigger occurs; at which time they are
+ dumped to the debug file. Newer lines displace the
+ oldest if the buffer is full. After a trigger,
+ immediate writes to file are done as normal.
+
+ trigger=<reason> This option selects cause for the pretrigger buffer
+ see above) to be copied to file. A reason of $*now*
+ take effect immediately; one of paniclog triggers
+ on a write to the panic log.
+
+ 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 = debug/kill
+ control = debug/opts=+all/pretrigger=1024/trigger=paniclog
+ control = debug/trigger=now
+
+control = dkim_disable_verify
+
+ This control turns off DKIM verification processing entirely. For details
+ on the operation and configuration of DKIM, see section 58.1.
+
+control = dmarc_disable_verify
+
+ This control turns off DMARC verification processing entirely. For details
+ on the operation and configuration of DMARC, see section 58.6.
+
+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:
+
+ o 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.
+
+ o 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/<options>*, 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. If used with no options set, no immediate delivery
+ process is started. In other words, it has the effect as the queue_only
+ global option or -odq command-line option.
+
+ If the first_pass_route option is given then the behaviour is like the
+ command-line -oqds option; a delivery process is started which stops short
+ of making any SMTP delivery. The benefit is that the hints database will be
+ updated for the message being waiting for a specific host, and a later
+ queue run will be able to send all such messages on a single connection.
+
+ The control only applies 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 48 describes the processing that Exim applies to messages. Section
+ 48.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:
+
+ o Any Sender: header line is left alone (in this respect, it is a dynamic
+ version of local_sender_retain).
+
+ o No Message-ID:, From:, or Date: header lines are added.
+
+ o 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.
+
+control = utf8_downconvert
+
+ This control enables conversion of UTF-8 in message envelope addresses to
+ a-label form. For details see section 60.1.
+
+
+44.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".
+
+
+44.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 a message 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 44.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.
+
+
+44.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 from the message if the modifier is used in DATA,
+MIME or DKIM ACLs for a message 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
+
+Header names for removal are accumulated during the MAIL, RCPT, and predata
+ACLs. Matching header lines are removed from the message before processing the
+DATA and MIME ACLs. If multiple header lines match, all are removed. There is
+no harm in attempting to remove the same header twice nor in 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 44.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.
+
+
+44.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 45.
+
+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. Note that spaces in complex expansions which are
+ used as arguments will act as argument separators.
+
+ 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 45.
+
+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 44.27-44.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 and only after a DATA command. It causes the
+ incoming message to be scanned for viruses. For details, see chapter 45.
+
+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 45.
+
+ratelimit = <parameters>
+
+ This condition can be used to limit the rate at which a user or host
+ submits messages. Details are given in section 44.39.
+
+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 45.
+
+seen = <parameters>
+
+ This condition can be used to test if a situation has been previously met,
+ for example for greylisting. Details are given in section 44.38.
+
+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 45.
+
+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 43).
+
+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 44.52.
+
+verify = header_names_ascii
+
+ This condition is relevant only in an ACL that is run after a message has
+ been received. This usually means 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 44.45 (callouts are described in section 44.46). You can combine
+ this condition with the senders condition to restrict it to bounce messages
+ only:
+
+ deny senders = :
+ !verify = header_sender
+ message = A valid sender header is required for bounces
+
+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:),
+ returning true if there are no problems. 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, and for detail of the
+ verification.
+
+ For SMTP input that does not come over TCP/IP (the -bs command line
+ option), this condition is always true.
+
+verify = not_blind/<options>
+
+ 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 is one possible option, "case_insensitive". If this is present then
+ local parts are checked case-insensitively.
+
+ 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 44.45. 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/<options>
+
+ 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.
+
+ There is one possible option, "defer_ok". If this is present and a DNS
+ operation returns a temporary error, the verify condition succeeds.
+
+ 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 44.45. 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.
+
+ Note that '/' is legal in local-parts; if the address may have such (eg. is
+ generated from the received message) they must be protected from the
+ options parsing by doubling:
+
+ verify = sender=${listquote{/}{${address:$h_sender:}}}
+
+
+44.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 dnslists = dialups.mail-abuse.org
+ message = X-Warn: sending host is on dialups list
+
+DNS list lookups are cached by Exim for the duration of the SMTP session (but
+limited by the DNS return TTL value), so a lookup based on the IP address is
+done at most once for any incoming connection (assuming long-enough TTL). Exim
+does not share information between multiple incoming connections (but your
+local name server cache should be active).
+
+There are a number of DNS lists to choose from, some commercial, some free, or
+free for small deployments. An overview can be found at https://
+en.wikipedia.org/wiki/Comparison_of_DNS_blacklists.
+
+
+44.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
+44.30 below.
+
+
+44.29 DNS lists keyed on domain names
+-------------------------------------
+
+There are some lists that are keyed on domain names rather than inverted IP
+addresses (see, e.g., 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 dnslists = dsn.rfc-ignorant.org/$sender_address_domain
+ message = Sender's domain is listed at $dnslist_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.
+
+
+44.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 44.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 dnslists = sbl.spamhaus.org/<|${lookup dnsdb {>|a=<|\
+ ${lookup dnsdb {>|mxh=\
+ $sender_address_domain} }} }
+ message = The mail servers for the domain \
+ $sender_address_domain \
+ are listed at $dnslist_domain ($dnslist_value); \
+ see $dnslist_text.
+
+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.spamhaus.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 44.32).
+
+
+44.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 44.33 below describes how you can distinguish between different values.
+Some DNS lists may return more than one address record; see section 44.35 for
+details of how they are checked.
+
+Values returned by a properly running DBSBL should be in the 127.0.0.0/8 range.
+If a DNSBL operator loses control of the domain, lookups on it may start
+returning other addresses. Because of this, Exim now ignores returned values
+outside the 127/8 region.
+
+
+44.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 44.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 44.36 for a way of obtaining more information.
+
+You can use the DNS list variables in message or log_message modifiers - even
+if 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
+
+
+44.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 44.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.
+
+
+44.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.
+
+Negation can also be used with a bitwise-and restriction. The dnslists
+condition with only be trus if a result is returned by the lookup which, anded
+with the restriction, is all zeroes. For example:
+
+deny dnslists = zen.spamhaus.org!&0.255.255.0
+
+
+44.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 "=&".
+
+
+44.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:
+
+deny dnslists = \
+ sbl.spamhaus.org,sbl-xbl.spamhaus.org=127.0.0.2 : \
+ dul.dnsbl.sorbs.net,dnsbl.sorbs.net=127.0.0.10
+ message = \
+ rejected because $sender_host_address is blacklisted \
+ at $dnslist_domain\n$dnslist_text
+
+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:
+
+deny 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.
+
+
+44.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
+
+
+44.38 Previously seen user and hosts
+------------------------------------
+
+The seen ACL condition can be used to test whether a situation has been
+previously met. It uses a hints database to record a timestamp against a key.
+host. The syntax of the condition is:
+
+seen = <optional flag><time interval> / <options>
+
+For example,
+
+defer seen = -5m / key=${sender_host_address}_$local_part@$domain
+
+in a RCPT ACL will implement simple greylisting.
+
+The parameters for the condition are a possible minus sign, then an interval,
+then, slash-separated, a list of options. The interval is taken as an offset
+before the current time, and used for the test. If the interval is preceded by
+a minus sign then the condition returns whether a record is found which is
+before the test time. Otherwise, the condition returns whether one is found
+which is since the test time.
+
+Options are read in order with later ones overriding earlier ones.
+
+The default key is $sender_host_address. An explicit key can be set using a key
+=value option.
+
+If a readonly option is given then no record create or update is done. If a
+write option is given then a record create or update is always done. An update
+is done if the test is for "since". If none of those hold and there was no
+existing record, a record is created.
+
+Creates and updates are marked with the current time.
+
+Finally, a "before" test which succeeds, and for which the record is old
+enough, will be refreshed with a timestamp of the test time. This can prevent
+tidying of the database from removing the entry. The interval for this is, by
+default, 10 days. An explicit interval can be set using a refresh=value option.
+
+Note that "seen" should be added to the list of hints databases for maintenance
+if this ACL condition is used.
+
+
+44.39 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.
+
+
+44.40 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,
+or acl_smtp_data 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 (accepted) 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 44.43 below.
+
+
+44.41 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.
+
+
+44.42 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, up to the given limit. This is
+appropriate if the countermeasure when the condition is true consists of
+refusing the message, and is generally the better choice if you have clients
+that retry automatically. If the action when true is anything more complex then
+this option is likely not what is wanted.
+
+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)
+
+
+44.43 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.
+
+
+44.44 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 ratelimit = 10 / 1s / $primary_hostname
+ message = Sorry, too busy. Try again later.
+
+# Restrict incoming rate from each host, with a default
+# set using a macro and special cases looked up in a table.
+defer ratelimit = ${lookup {$sender_host_address} \
+ cdb {DB/ratelimits.cdb} \
+ {$value} {RATELIMIT} }
+ message = Sender rate exceeds $sender_rate_limit \
+ messages per $sender_rate_period
+
+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).
+
+
+44.45 Address verification
+--------------------------
+
+Several of the verify conditions described in section 44.26 cause addresses to
+be verified. Section 44.50 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 44.50, 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 44.51.
+
+ * If the quota option is specified for recipient verify, successful routing
+ to an appendfile transport is followed by a call into the transport to
+ evaluate the quota status for the recipient. No actual delivery is done,
+ but verification will succeed if the quota is sufficient for the message
+ (if the sender gave a message size) or not already exceeded (otherwise).
+
+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. This variable is cleared at the end of processing the ACL
+verb.
+
+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.
+
+ * quota: The quota check for a local recipient did non pass.
+
+The main use of these variables is expected to be to distinguish between
+rejections of MAIL and rejections of RCPT in callouts.
+
+The above variables may also be set after a successful address verification to:
+
+ * random: A random local-part callout succeeded
+
+
+44.46 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 44.48.
+
+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.
+
+A recipient callout which gets a 2xx code will assign untainted values to the
+$domain_data and $local_part_data variables, corresponding to the domain and
+local parts of the recipient address.
+
+
+44.47 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.
+
+hold
+
+ This option applies to recipient callouts only. For example:
+
+ require verify = recipient/callout=use_sender,hold
+
+ It causes the connection to be held open and used for any further
+ recipients and for eventual delivery (should that be done quickly). Doing
+ this saves on TCP and SMTP startup costs, and TLS costs also when that is
+ used for the connections. The advantage is only gained if there are no
+ callout cache hits (which could be enforced by the no_cache option), if the
+ use_sender option is used, if neither the random nor the use_postmaster
+ option is used, and if no other callouts intervene.
+
+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.
+
+
+44.48 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.
+
+
+44.49 Quota caching
+-------------------
+
+Exim caches the results of quota verification in order to reduce the amount of
+resources used. The "callout" hints database is used.
+
+The default cache periods are five minutes for a positive (good) result and one
+hour for a negative result. To change the periods the quota option can be
+followed by an equals sign and a number of optional paramemters, separated by
+commas. For example:
+
+verify = recipient/quota=cachepos=1h,cacheneg=1d
+
+Possible parameters are:
+
+cachepos = <time interval>
+
+ Set the lifetime for a positive cache entry. A value of zero seconds is
+ legitimate.
+
+cacheneg = <time interval>
+
+ As above, for a negative entry.
+
+no_cache
+
+ Set both positive and negative lifetimes to zero.
+
+
+44.50 Sender address verification reporting
+-------------------------------------------
+
+See section 44.45 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
+
+
+44.51 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.
+
+
+44.52 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.
+
+
+44.53 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. The validity
+period on signed addresses is seven days.
+
+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 senders = :
+ recipients = +batv_senders
+ message = This address does not send an unsigned reverse path
+
+# Bounces: In case of prvs-signed address, check signature.
+deny senders = :
+ condition = ${prvscheck {$local_part@$domain}\
+ {PRVSCHECK_SQL}{1}}
+ !condition = $prvscheck_result
+ message = Invalid reverse path signature.
+
+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.
+
+
+44.54 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.
+
+
+44.55 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.
+
+
+
+===============================================================================
+45. 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 46) 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.
+
+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.
+
+
+45.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.
+
+Since message data needs to have arrived, the condition may be only called in
+ACL defined by acl_smtp_data, acl_smtp_data_prdr, acl_smtp_mime or
+acl_smtp_dkim
+
+A timeout of 2 minutes is applied to a scanner call (by default); if it expires
+then a defer action is taken.
+
+You can set the av_scanner option in the main part of the configuration 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.20) applies. The
+following scanner types are supported in this release, though individual ones
+can be included or not at build time:
+
+avast
+
+ This is the scanner daemon of Avast. It has been tested with Avast Core
+ Security (currently at version 2.2.0). You can get a trial version at
+ https://www.avast.com or for Linux at https://www.avast.com/
+ linux-server-antivirus. This scanner type takes one option, which can be
+ either a full path to a UNIX socket, or host and port specifiers separated
+ by white space. The host may be a name or an IP address; the port is either
+ a single number or a pair of numbers with a dash between. A list of options
+ may follow. These options are interpreted on the Exim's side of the malware
+ scanner, or are given on separate lines to the daemon as options before the
+ main scan command.
+
+ If "pass_unscanned" is set, any files the Avast scanner can't scan (e.g.
+ decompression bombs, or invalid archives) are considered clean. Use with
+ care.
+
+ For example:
+
+ av_scanner = avast:/var/run/avast/scan.sock:FLAGS -fullfiles:SENSITIVITY -pup
+ av_scanner = avast:/var/run/avast/scan.sock:pass_unscanned:FLAGS -fullfiles:SENSITIVITY -pup
+ av_scanner = avast:192.168.2.22 5036
+
+ If you omit the argument, the default path /var/run/avast/scan.sock is
+ used. If you use a remote host, you need to make Exim's spool directory
+ available to it, as the scanner is passed a file path, not file contents.
+ For information about available commands and their options you may use
+
+ $ socat UNIX:/var/run/avast/scan.sock STDIO:
+ FLAGS
+ SENSITIVITY
+ PACK
+
+ If the scanner returns a temporary failure (e.g. license issues, or
+ permission problems), the message is deferred and a paniclog entry is
+ written. The usual "defer_ok" option is available.
+
+aveserver
+
+ This is the scanner daemon of Kaspersky Version 5. You can get a trial
+ version at https://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 https://
+ 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 is no longer believed to be necessary.
+
+ The options are a list of server specifiers, which may be a UNIX socket
+ specification, a TCP socket specification, or a (global) option.
+
+ A socket specification consists of a space-separated list. For a Unix
+ socket the first element is a full path for the socket, for a TCP socket
+ the first element is the IP address and the second a port number, Any
+ further elements are per-server (non-global) options. These per-server
+ options are supported:
+
+ retry=<timespec> Retry on connect fail
+
+ The "retry" option specifies a time after which a single retry for a failed
+ connect is made. The default is to not retry.
+
+ If a Unix socket file is specified, only one server is supported.
+
+ 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 retry=10s
+ 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" option, then the ClamAV interface will pass a filename containing
+ the data to be scanned, which 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.
+
+ 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 (https://www.sald.ru/) interface takes one option,
+ either a full path to a UNIX socket, or host and port specifiers separated
+ by white space. The host may be a name or an IP address; the port is either
+ a single number or a pair of numbers with a dash between. For example:
+
+ 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.
+
+f-protd
+
+ The f-protd scanner is accessed via HTTP over TCP. One argument is taken,
+ being a space-separated hostname and port number (or port-range). For
+ example:
+
+ av_scanner = f-protd:localhost 10200-10204
+
+ If you omit the argument, the default values shown above are used.
+
+f-prot6d
+
+ The f-prot6d scanner is accessed using the FPSCAND protocol over TCP. One
+ argument is taken, being a space-separated hostname and port number. For
+ example:
+
+ av_scanner = f-prot6d:localhost 10200
+
+ If you omit the argument, the default values show above are used.
+
+fsecure
+
+ The F-Secure daemon scanner (https://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 was a daemon type scanner that is aimed mainly at Polish users, though
+ some documentation was available in English. The history can be shown at
+ https://en.wikipedia.org/wiki/Mks_vir and this appears to be a candidate
+ for removal from Exim, unless we are informed of other virus scanners which
+ use the same protocol to integrate. The only option for this scanner type
+ is the maximum number of processes used simultaneously to scan the
+ attachments, 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 address 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, and an RE
+ to extract malware_name from the returned data. For example:
+
+ av_scanner = sock:127.0.0.1 6001:%s:(SPAM|VIRUS):(.*)$
+
+ Note that surrounding whitespace is stripped from each option, meaning
+ there is no way to specify a trailing newline. The socket specifier and
+ both regular-expressions are required. Default for the commandline is %s\n
+ (note this does have a trailing newline); specify an empty element to get
+ this.
+
+sophie
+
+ Sophie is a daemon that uses Sophos' libsavi library to scan for viruses.
+ You can get Sophie at http://sophie.sourceforge.net/. 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
+and taken as a list, slash-separated by default. The first element 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. Note that "/" characters in the RE must be doubled due to the
+ list-processing, unless the separator is changed (in the usual way 6.21).
+
+You can append a "defer_ok" element to the malware argument list to accept
+messages even if there is a problem with the virus scanner. Otherwise, such a
+problem causes the ACL to defer.
+
+You can append a "tmo=<val>" element to the malware argument list to specify a
+non-default timeout. The default is two minutes. For example:
+
+malware = * / defer_ok / tmo=10s
+
+A timeout causes the ACL to defer.
+
+When a connection is made to the scanner the expansion variable
+$callout_address is set to record the actual address used.
+
+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.
+
+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 malware = *
+ message = This message contains malware ($malware_name)
+
+The next example accepts messages when there is a problem with the scanner:
+
+deny malware = */defer_ok
+ message = This message contains malware ($malware_name)
+
+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 set acl_m0 = sophie
+ malware = *
+ message = This message contains malware ($malware_name)
+
+deny set acl_m0 = aveserver
+ malware = *
+ message = This message contains malware ($malware_name)
+
+
+45.2 Scanning with SpamAssassin and Rspamd
+------------------------------------------
+
+The spam ACL condition calls SpamAssassin's spamd daemon to get a spam score
+and a report for the message. Support is also provided for Rspamd.
+
+For more information about installation and configuration of SpamAssassin or
+Rspamd refer to their respective websites at https://spamassassin.apache.org/
+and https://www.rspamd.com/
+
+SpamAssassin can be installed with 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.
+
+By default, SpamAssassin listens on 127.0.0.1, TCP port 783 and if you intend
+to use an instance running on the local host you do not need to set
+spamd_address. If you intend to use another host or port for SpamAssassin, you
+must set the spamd_address option in the global part of the Exim configuration
+as follows (example):
+
+spamd_address = 192.168.99.45 783
+
+The SpamAssassin protocol relies on a TCP half-close from the client. If your
+SpamAssassin client side is running a Linux system with an iptables firewall,
+consider setting net.netfilter.nf_conntrack_tcp_timeout_close_wait to at least
+the timeout, Exim uses when waiting for a response from the SpamAssassin server
+(currently defaulting to 120s). With a lower value the Linux connection
+tracking may consider your half-closed connection as dead too soon.
+
+To use Rspamd (which by default listens on all local addresses on TCP port
+11333) you should add variant=rspamd after the address/port pair, for example:
+
+spamd_address = 127.0.0.1 11333 variant=rspamd
+
+As of version 2.60, SpamAssassin also supports communication over UNIX sockets.
+If you want to us these, supply spamd_address with an absolute filename instead
+of an 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 (the separator can be changed in the usual way 6.21):
+
+spamd_address = 192.168.2.10 783 : \
+ 192.168.2.11 783 : \
+ 192.168.2.12 783
+
+Up to 32 spamd servers are supported. 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.
+
+Unix and TCP socket specifications may be mixed in any order. Each element of
+the list is a list itself, space-separated by default and changeable in the
+usual way (6.21); take care to not double the separator.
+
+For TCP socket specifications a host name or IP (v4 or v6, but subject to
+list-separator quoting rules) address can be used, and the port can be one or a
+dash-separated pair. In the latter case, the range is tried in strict order.
+
+Elements after the first for Unix sockets, or second for TCP socket, are
+options. The supported options are:
+
+pri=<priority> Selection priority
+weight=<value> Selection bias
+time=<start>-<end> Use only between these times of day
+retry=<timespec> Retry on connect fail
+tmo=<timespec> Connection time limit
+variant=rspamd Use Rspamd rather than SpamAssassin protocol
+
+The "pri" option specifies a priority for the server within the list, higher
+values being tried first. The default priority is 1.
+
+The "weight" option specifies a selection bias. Within a priority set servers
+are queried in a random fashion, weighted by this value. The default value for
+selection bias is 1.
+
+Time specifications for the "time" option are <hour>.<minute>.<second> in the
+local time zone; each element being one or more digits. Either the seconds or
+both minutes and seconds, plus the leading "." characters, may be omitted and
+will be taken as zero.
+
+Timeout specifications for the "retry" and "tmo" options are the usual Exim
+time interval standard, e.g. "20s" or "1m".
+
+The "tmo" option specifies an overall timeout for communication. The default
+value is two minutes.
+
+The "retry" option specifies a time after which a single retry for a failed
+connect is made. The default is to not retry.
+
+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.
+
+When a connection is made to the server the expansion variable $callout_address
+is set to record the actual address used.
+
+
+45.3 Calling SpamAssassin from an Exim ACL
+------------------------------------------
+
+Here is a simple example of the use of the spam condition in a DATA ACL:
+
+deny spam = joe
+ message = This message was classified as SPAM
+
+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". Rspamd does
+not use this setting. 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-time ACL in order to be able to
+read the contents of the message, the variables $local_part and $domain are not
+set. Careful enforcement of single-recipient messages (e.g. by responding with
+defer in the recipient ACL for all recipients after the first), or the use of
+PRDR, are needed to use this feature.
+
+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 condition = ${if < {$message_size}{10K}}
+ spam = nobody
+ message = This message was classified as SPAM
+
+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.
+Except for $spam_report, these variables are saved with the received message so
+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. The maximum length of the spam bar is
+ 50 characters.
+
+$spam_report
+
+ A multiline text table, containing the full SpamAssassin report for the
+ message. Useful for inclusion in headers or reject messages. This variable
+ is only usable in a DATA-time ACL. Beware that SpamAssassin may return
+ non-ASCII characters, especially when running in country-specific locales,
+ which are not legal unencoded in headers.
+
+$spam_action
+
+ For SpamAssassin either 'reject' or 'no action' depending on the spam score
+ versus threshold. For Rspamd, the recommended action.
+
+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 spam = joe/defer_ok
+ message = This message was classified as SPAM
+
+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 spam = nobody:true
+ condition = ${if >{$spam_score_int}{120}{1}{0}}
+ message = This message scored $spam_score spam points.
+
+
+45.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 45.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
+ filename 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 filename.
+
+ 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 45.5.
+
+The following list describes all expansion variables that are available in the
+MIME ACL:
+
+$mime_anomaly_level, $mime_anomaly_text
+
+ If there are problems decoding, these variables contain information on the
+ detected issue.
+
+$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 filename 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
+ or RFC2231 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-HTML coverletter mail will also be
+ allowed:
+
+ deny !condition = $mime_is_rfc822
+ condition = $mime_is_coverletter
+ condition = ${if eq{$mime_content_type}{text/html}{1}{0}}
+ message = HTML mail is not accepted here
+
+$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.
+
+
+45.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 regex = [Mm]ortgage : URGENT BUSINESS PROPOSAL
+ message = contains blacklisted regex ($regex_match_string)
+
+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. The expansion variables $regex1 $regex2 etc are set to any
+substrings captured by the regular expression.
+
+Warning: With large messages, these conditions can be fairly CPU-intensive.
+
+
+
+===============================================================================
+46. 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 45) 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 44), 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.
+
+
+46.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 both HAVE_LOCAL_SCAN and
+LOCAL_SCAN_SOURCE in your Local/Makefile. A recommended place to put it is in
+the Local directory, so you might set
+
+HAVE_LOCAL_SCAN=yes
+LOCAL_SCAN_SOURCE=Local/local_scan.c
+
+for example. The function must be called local_scan(); the source file(s) for
+it should first #define LOCAL_SCAN and then #include "local_scan.h". 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 runtime configuration file to set options for
+your local_scan() function, you must also set
+
+LOCAL_SCAN_HAS_OPTIONS=yes
+
+in Local/Makefile (see section 46.3 below).
+
+
+46.2 API for local_scan()
+-------------------------
+
+You must include this line near the start of your code:
+
+#define LOCAL_SCAN
+#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 makes available 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 53.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.
+
+
+46.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.
+
+
+46.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. It is not
+ valid if the spool_wireformat option is used.
+
+int body_zerocount
+
+ This variable contains the number of binary zero bytes in the message's
+ body. It is not valid if the spool_wireformat option is used.
+
+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:
+
+ o 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.
+
+ o 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.
+
+const 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 46.8 for details.
+
+
+46.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 57 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.
+
+
+46.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.
+
+
+46.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:
+
+ o >= 0
+
+ The process terminated by a normal exit and the value is the process
+ ending status.
+
+ o < 0 and > -256
+
+ The process was terminated by a signal and the value is the negation of
+ the signal number.
+
+ o -256
+
+ The process timed out.
+
+ o -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
+ 46.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 *,BOOL, ...)
+
+ The arguments of this function are almost 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.
+
+ The second argument is used to request that the data be buffered (when
+ TRUE) or flushed (along with any previously buffered, when FALSE). This is
+ advisory only, but likely to save on system-calls and packets sent when a
+ sequence of calls to the function are made.
+
+ The argument was added in Exim version 4.90 - changing the API/ABI. Nobody
+ noticed until 4.93 was imminent, at which point the ABI version number was
+ incremented.
+
+ 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 guarantee a flush of 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,BOOL)
+
+ This function accesses Exim's internal store (memory) manager. It gets a
+ new chunk of memory whose size is given by the first argument. The second
+ argument should be given as TRUE if the memory will be used for data
+ possibly coming from an attacker (eg. the message content), FALSE if it is
+ locally-sourced. 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,BOOL)
+
+ 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.
+
+
+46.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.
+
+
+
+===============================================================================
+47. 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 47.8
+below.
+
+
+47.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.
+
+
+47.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.
+
+
+47.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.
+
+
+47.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.
+
+
+47.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.
+
+
+47.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 48.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"
+
+
+47.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.
+
+
+47.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_data and $domain_data 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_data
+ 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.
+
+
+
+===============================================================================
+48. 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.
+
+
+48.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 44.21
+and 44.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 48.11
+and 48.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.
+
+
+48.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.
+
+
+48.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,
+
+
+48.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.
+
+
+48.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.
+
+
+48.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
+
+
+48.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.
+
+
+48.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.
+
+
+48.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.
+
+
+48.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.
+
+
+48.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 48.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 48.18.
+
+
+48.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.
+
+
+48.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.
+
+
+48.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.
+
+
+48.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.
+
+
+48.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.
+
+
+48.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 47.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 44.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. Note that colons in complex expansions which are used
+to form all or part of a headers_remove list will act as list separators.
+
+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.
+
+
+48.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.
+
+
+48.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.
+
+
+48.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.
+
+
+48.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.
+
+
+
+===============================================================================
+49. 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.
+
+
+49.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 extension 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 43 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.
+
+
+49.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:
+
+ o Connection refused or timed out,
+
+ o Any error response code on connection,
+
+ o Any error response code to EHLO or HELO,
+
+ o Loss of connection at any time, except after ".",
+
+ o I/O errors at any time,
+
+ o 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:
+
+ o Any error response code to MAIL, DATA, or the "." that terminates the
+ data,
+
+ o Timeout after MAIL,
+
+ o 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:
+
+ o Any error response to RCPT,
+
+ o 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.
+
+
+49.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 44 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.
+
+
+49.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.
+
+
+49.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).
+
+
+49.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.
+
+
+49.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.
+
+When no ACL is defined for VRFY, or if it rejects without setting an explicit
+response code, the command is accepted (with a 252 SMTP response code) in order
+to support awkward clients that do a VRFY before every RCPT. When VRFY is
+accepted, it runs exactly the same code as when Exim is called with the -bv
+option, and returns 250/451/550 SMTP response codes.
+
+If no ACL for EXPN is defined, the command is rejected. 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.
+
+
+49.8 The ETRN command
+---------------------
+
+RFC 1985 describes an ESMTP 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.
+
+
+49.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.
+
+
+49.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).
+
+
+49.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.
+
+
+
+===============================================================================
+50. CUSTOMIZING BOUNCE AND WARNING MESSAGES
+
+When a message fails to be delivered, or remains in 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,
+
+
+50.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, fifth and sixth items will be ignored and may be empty. The
+ fields exist for back-compatibility
+
+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.
+****
+
+
+50.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 in 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.
+
+
+
+===============================================================================
+51. 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.
+
+
+51.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 52).
+
+
+51.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 = ${lookup {$local_part} dsearch,ret=full {/usr/lists}}
+ forbid_pipe
+ forbid_file
+ errors_to = ${quote_local_part:$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 filename 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.
+
+
+51.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.
+
+
+51.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.
+
+
+51.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
+ local_parts = ${lookup {$local_part} dsearch,filter=file {/usr/lists}}
+ file = /usr/lists/${local_part_data}-request
+ no_more
+
+lists_post:
+ driver = redirect
+ domains = lists.example
+ local_parts = ${lookup {$local_part} dsearch,filter=file,ret=full {/usr/lists}}
+ senders = ${if exists {$local_part_data} {lsearch;$local_part_data}{*}}
+ file = ${lookup {$local_part} dsearch,ret=full {/usr/lists}}
+ forbid_pipe
+ forbid_file
+ errors_to = ${quote_local_part:$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.
+
+
+51.6 Variable Envelope Return Paths (VERP)
+------------------------------------------
+
+Variable Envelope Return Paths - see https://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\$}\
+ {${quote_local_part:$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\$}}
+ {${quote_local_part:$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).
+
+
+51.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_data}}
+ 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. The dsearch lookup used results in an untainted
+version of $domain being placed into the $domain_data variable.
+
+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 filenames
+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_data/$local_part_data
+ 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.
+
+
+51.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_data/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
+ local_part_suffix = -*
+ local_part_suffix_optional
+ file = ${lookup {.forward$local_part_suffix} dsearch,ret=full {$home} {$value}fail}
+ 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.
+
+
+51.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.
+
+
+51.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.
+
+
+51.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.
+
+
+51.12 Exim on the upstream server host
+--------------------------------------
+
+It is tempting to arrange for incoming mail for the intermittently connected
+host to remain in 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 49.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.
+
+
+51.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.
+
+
+
+===============================================================================
+52. 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 56.3 for a general discussion about the advantages and
+disadvantages of running without root privilege.
+
+
+
+===============================================================================
+53. 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 54.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 53.15
+). When this is set, the process id is output, in square brackets, immediately
+after the time and date.
+
+
+53.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 runtime 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 runtime, 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". If no such item exists, log files are
+written in the log subdirectory of the spool directory. This is equivalent to
+the configuration file setting:
+
+log_file_path = $spool_directory/log/%slog
+
+If you do not specify anything at build time or runtime, or if you unset the
+option at runtime (i.e. "log_file_path = "), that is where the logs are
+written.
+
+A log file path may also contain "%D" or "%M" if datestamped log filenames are
+in use - see section 53.3 below.
+
+Here are some examples of possible Makefile 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.
+
+
+53.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 54.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.
+
+
+53.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
+
+
+53.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.
+
+
+53.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
+ (= message fakereject
+ => normal message delivery
+ -> additional address in same delivery
+ >> cutthrough message delivery
+ *> delivery suppressed by -N
+ ** delivery failed; address bounced
+ == delivery deferred; temporary problem
+
+
+53.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 53.15 below.
+
+
+53.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 multiple 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, as a second colon-separated list item. Optionally (see the
+smtp_mailauth log_selector) there may be a third list item.
+
+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 remote IP address (and port
+if enabled) in the log lines for the second and subsequent messages. When two
+or more messages are delivered down a single TLS connection, the DNS and some
+TLS-related information logged for the first message delivered will not be
+present in the log lines for the second and subsequent messages. TLS cipher
+information is still available.
+
+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 53.15 below.
+
+
+53.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
+
+
+53.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.
+
+
+53.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
+"**".
+
+
+53.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 "*>".
+
+
+53.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.
+
+
+53.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"
+DKIM domain verified in incoming message
+DN distinguished name from peer certificate
+DS DNSSEC secured lookups
+DT on =>, == and ** lines: time taken for, or to attempt, a delivery
+F sender address (on delivery lines)
+H host name and IP address
+I local interface used
+id message id (from header) for incoming message
+K CHUNKING extension used
+L on <= and => lines: PIPELINING extension used
+M8S 8BITMIME status for incoming message
+P on <= lines: protocol used
+ on => and ** lines: return path
+PRDR PRDR extension used
+PRX on <= and => lines: proxy address
+Q alternate queue name
+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
+RT on <= lines: time taken for reception
+S size of message in bytes
+SNI server name indication from TLS client hello
+ST shadow transport name
+T on <= lines: message subject (topic)
+TFO connection took advantage of TCP Fast Open
+ on => ** and == lines: transport name
+U local user or RFC 1413 identity
+X TLS cipher suite
+
+
+53.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.
+
+ * DKIM: d= Verbose results of a DKIM verification attempt, if enabled for
+ logging and the message has a DKIM signature header.
+
+
+53.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 attempt delivery
+ "delivery_size" add "S="nnn to => lines
+ "dkim" * DKIM verified domain on <= lines
+ "dkim_verbose" separate full DKIM verification result
+ line, per signature
+ "dnslist_defer" * defers of DNS list (aka RBL) lookups
+ "dnssec" DNSSEC secured lookups
+ "etrn" * ETRN commands
+ "host_lookup_failed" * as it says
+ "ident_timeout" timeout for ident connection
+ "incoming_interface" local interface on <= and => lines
+ "incoming_port" remote port on <= lines
+ "lost_incoming_connection" * as it says (includes timeouts)
+ "millisec" millisecond timestamps and RT,QT,DT,D times
+ "msg_id" * on <= lines, Message-ID: header value
+ "msg_id_created" on <= lines, Message-ID: header value when
+ one had to be added
+ "outgoing_interface" local interface on => lines
+ "outgoing_port" add remote port to => lines
+ "queue_run" * start and end queue runs
+ "queue_time" time on queue for one recipient
+ "queue_time_exclusive" exclude recieve time from QT times
+ "queue_time_overall" time on queue for whole message
+ "pid" Exim process id
+ "pipelining" PIPELINING use, on <= and => lines
+ "proxy" proxy address on <= and => lines
+ "receive_time" time taken to receive message
+ "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" incoming 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_resumption" append * to cipher field
+ "tls_sni" TLS SNI on <= lines
+ "unknown_in_list" DNS lookup failed in list match
+ "all" all of the above
+
+See also the slow_lookup_log main configuration option, section 14.4
+
+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".
+ If millisecond logging is enabled, short times will be shown with greater
+ precision, eg. "DT=0.304s".
+
+ * delivery_size: For each delivery, the size of message delivered is added to
+ the "=>" line, tagged with S=.
+
+ * dkim: For message acceptance log lines, when an DKIM signature in the
+ header verifies successfully a tag of DKIM is added, with one of the
+ verified domains.
+
+ * dkim_verbose: A log entry is written for each attempted DKIM verification.
+
+ * dnslist_defer: A log entry is written if an attempt to look up a host in a
+ DNS black list suffers a temporary error.
+
+ * dnssec: For message acceptance and (attempted) delivery log lines, when dns
+ lookups gave secure results a tag of DS is added. For acceptance this
+ covers the reverse and forward lookups for host name verification. It does
+ not cover helo-name verification. For delivery this covers the SRV, MX, A
+ and/or AAAA lookups.
+
+ * 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", to
+ rejection lines, and (despite the name) to outgoing "=>", "->", "==" and
+ "**" lines. The latter can be disabled by turning off the
+ outgoing_interface option.
+
+ * proxy: The internal (closest to the system running Exim) IP address of the
+ proxy, tagged by PRX=, on the "<=" line for a message accepted on a proxied
+ connection or the "=>" line for a message delivered on a proxied
+ connection. See 59.1 for more information.
+
+ * 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.
+
+ * millisec: Timestamps have a period and three decimal places of finer
+ granularity appended to the seconds value.
+
+ * msg_id: The value of the Message-ID: header.
+
+ * msg_id_created: The value of the Message-ID: header, when one had to be
+ created. This will be either because the message is a bounce, or was
+ submitted locally (submission mode) without one. The field identifier will
+ have an asterix appended: "id*=".
+
+ * outgoing_interface: If incoming_interface is turned on, then the interface
+ on which a message was sent is added to delivery lines as an I= tag
+ followed by IP address in square brackets. You can disable this by turning
+ off the outgoing_interface option.
+
+ * outgoing_port: The remote port number is added to delivery log lines (those
+ containing => tags) following the IP address. The local port is also added
+ if incoming_interface and outgoing_interface are both enabled. This option
+ is not included in the default setting, because for most ordinary
+ configurations, the remote port number is always 25 (the SMTP port), and
+ the local port is a random ephemeral port.
+
+ * pid: The current process id is added to every log line, in square brackets,
+ immediately after the time and date.
+
+ * pipelining: A field is added to delivery and accept log lines when the
+ ESMTP PIPELINING extension was used. The field is a single "L".
+
+ On accept lines, where PIPELINING was offered but not used by the client,
+ the field has a minus appended.
+
+ If Exim is built without the DISABLE_PIPE_CONNECT build option accept "L"
+ fields have a period appended if the feature was offered but not used, or
+ an asterisk appended if used. Delivery "L" fields have an asterisk appended
+ if used.
+
+ * 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". If millisecond logging is enabled, short times will be shown
+ with greater precision, eg. "QT=1.578s".
+
+ * 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".
+
+ * receive_time: For each message, the amount of real time it has taken to
+ perform the reception is logged as RT=<time>, for example, "RT=1s". If
+ millisecond logging is enabled, short times will be shown with greater
+ precision, eg. "RT=0.204s".
+
+ * 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 46.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 another process is already delivering it or because it
+ is frozen. The message that is written is either "spool file is locked" or
+ "message is frozen".
+
+ * 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 incoming 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_accept_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 using a CA trust anchor, "CV=dane" if using a DNS trust anchor,
+ 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_resumption: When a message is sent or received over an encrypted
+ connection and the TLS session resumed one used on a previous TCP
+ connection, an asterisk is appended to the X= cipher field in the log line.
+
+ * 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.
+
+
+53.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.
+
+
+
+===============================================================================
+54. 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:
+
+ 54.1 exiwhat list what Exim processes are doing
+ 54.2 exiqgrep grep the queue
+ 54.3 exiqsumm summarize the queue
+ 54.4 exigrep search the main log
+ 54.5 exipick select messages on various criteria
+ 54.6 exicyclog cycle (rotate) log files
+ 54.7 eximstats extract statistics from the log
+ 54.8 exim_checkaccess check address acceptance from given IP
+ 54.9 exim_dbmbuild build a DBM file
+ 54.10 exinext extract retry information
+ 54.12 exim_dumpdb dump a hints database
+ 54.13 exim_tidydb clean up a hints database
+ 54.14 exim_fixdb patch a hints database
+ 54.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 https:
+//duncanthrax.net/exilog/ for details.
+
+
+54.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.
+
+
+54.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
+
+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-insensitive 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.
+
+-G <queuename>
+
+ Match only messages in the given queue. Without this, the default queue is
+ searched.
+
+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.
+
+The following options give alternates for configuration:
+
+-C <config file>
+
+ is used to specify an alternate exim.conf which might contain alternate
+ exim configuration the queue management might be using.
+
+-E <path>
+
+ can be used to specify a path for the exim binary, overriding the built-in
+ one.
+
+There is one more option, -h, which outputs a list of options.
+
+At least one selection option, or either the -c or -h option, must be given.
+
+
+54.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 in 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).
+
+
+54.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] [-M] [-v] <pattern> [<log file>] ...
+
+If no log filenames 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 in 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.
+
+The -M options means "related messages". exigrep will show messages that are
+generated as a result/response to a message that exigrep matched normally.
+
+Example of -M: user_a sends a message to user_b, which generates a bounce back
+to user_b. If exigrep is used to search for "user_a", only the first message
+will be displayed. But if exigrep is used to search for "user_b", the first and
+the second (bounce) message will be displayed. Using -M with exigrep when
+searching for "user_a" will show both messages since the bounce is "related" to
+or a "result" of the first message that was found by the search term.
+
+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. If the ZCAT_COMMAND is not
+executable, exigrep tries to use autodetection of some well known compression
+extensions.
+
+
+54.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, run exipick with the --help option.
+
+
+54.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 53.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 filenames get "shuffled down" by one. If the
+main log filename 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.
+
+
+54.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.
+
+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 in 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
+
+
+54.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.
+
+
+54.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 filenames
+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 filename.
+
+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.
+
+
+54.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 49.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.
+
+
+54.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
+
+ * tls: TLS session resumption data
+
+ * 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)
+
+ * Limiting the concurrency of specific transports (when max_parallel is set
+ in a transport)
+
+
+54.12 exim_dumpdb
+-----------------
+
+The entire contents of a database are written to the standard output by the
+exim_dumpdb program,
+
+taking as arguments the spool and database names. An option -z may be given to
+request times in UTC; otherwise times are in the local timezone. An option -k
+may be given to dump only the record keys.
+
+For example, to dump the retry database:
+
+exim_dumpdb /var/spool/exim retry
+
+For the retry database 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.
+
+
+54.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.
+
+
+54.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. 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.
+
+Both displayed and input times are in the local timezone by default. If an
+option -z is used on the command line, displayed times are in UTC.
+
+
+54.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 forever; 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.
+
+
+
+===============================================================================
+55. 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.
+
+
+55.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 in 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.
+
+
+55.2 The stripcharts
+--------------------
+
+The first stripchart is always a count of messages in 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.
+
+
+55.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.
+
+
+55.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.
+
+
+55.5 The queue display
+----------------------
+
+The bottom section of the monitor window contains a list of all messages that
+are in 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 in 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 in 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.
+
+
+55.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 57
+ 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 runtime.
+
+ * 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.
+
+
+
+===============================================================================
+56. 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.
+
+
+56.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 filenames used with the -C option. When it is set, these
+ filenames 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.
+
+
+56.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 runtime 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.
+
+
+56.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 52),
+deliver_drop_privilege is forced to be true.
+
+
+56.4 Delivering to local files
+------------------------------
+
+Full details of the checks applied by appendfile before it writes to a file are
+given in chapter 26.
+
+
+56.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 analogous 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.
+
+
+56.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 PCRE2. 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.
+
+
+56.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.
+
+
+56.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.
+
+
+56.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.
+
+By default, regular users are trusted to perform basic testing and
+introspection commands, as themselves. This setting can be tightened by setting
+the commandline_checks_require_admin option. This affects most of the checking
+options, such as -be and anything else -b*.
+
+
+56.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.
+
+
+56.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.
+
+
+56.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.
+
+
+56.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.
+
+
+56.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.
+
+
+56.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.
+
+
+56.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.
+
+
+56.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.
+
+
+
+===============================================================================
+57. 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.
+
+The file formats may be changed, or new formats added, at any release. Spool
+files are not intended as an interface to other programs and should not be used
+as such.
+
+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 and can cause
+ incomplete transmission of messages or undeliverable messages.
+
+ * 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.
+
+Files whose names end with -K or .eml may also be seen in the spool. These are
+temporaries used for DKIM or malware processing, when that is used. They should
+be tidied up by normal operations; any old ones are probably relics of crashes
+and can be removed.
+
+
+57.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 contain variables,
+can appear in any order, and are omitted when not relevant.
+
+If there is a second hyphen after the first, the corresponding data is tainted.
+If there is a value in parentheses, the data is quoted for a lookup.
+
+The following word specifies a variable, and the remainder of the item depends
+on the variable.
+
+-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 present
+ unless -spool_file_wireformat is.
+
+-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.
+
+-spool_file_wireformat
+
+ The -D file for this message is in wire-format (for ESMTP CHUNKING) rather
+ than Unix-format. The line-ending is CRLF rather than newline. There is
+ still, however, no leading-dot-stuffing.
+
+-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.
+
+
+57.2 Format of the -D file
+--------------------------
+
+The data file is traditionally in Unix-standard format: lines are ended with an
+ASCII newline character. However, when the spool_wireformat main option is used
+some -D files can have an alternate format. This is flagged by a
+-spool_file_wireformat line in the corresponding -H file. The -D file lines
+(not including the first name-component line) are suitable for direct copying
+to the wire when transmitting using the ESMTP CHUNKING option, meaning lower
+processing overhead. Lines are terminated with an ASCII CRLF pair. There is no
+dot-stuffing (and no dot-termination).
+
+
+
+===============================================================================
+58. DKIM, SPF, SRS AND DMARC
+
+
+58.1 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 6376.
+
+As DKIM relies on the message being unchanged in transit, messages handled by a
+mailing-list (which traditionally adds to the message) will not match any
+original DKIM signature.
+
+DKIM support is compiled into Exim by default if TLS support is present. It can
+be disabled by setting DISABLE_DKIM=yes in Local/Makefile.
+
+Exim's DKIM implementation allows for
+
+ 1. Signing 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. Verifying 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 (in the <= line) purposes.
+
+Additional log detail can be enabled using the dkim_verbose log_selector. When
+set, 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).
+
+
+58.2 Signing outgoing messages
+------------------------------
+
+For signing to be usable you must have published a DKIM record in DNS. Note
+that RFC 8301 (which does not cover EC keys) says:
+
+rsa-sha1 MUST NOT be used for signing or verifying.
+
+Signers MUST use RSA keys of at least 1024 bits for all keys.
+Signers SHOULD use RSA keys of at least 2048 bits.
+
+Note also that the key content (the 'p=' field) in the DNS record is different
+between RSA and EC keys; for the former it is the base64 of the ASN.1 for the
+RSA public key (equivalent to the private-key .pem with the header/trailer
+stripped) but for EC keys it is the base64 of the pure key; no ASN.1 wrapping.
+
+Signing is enabled by setting private options on the SMTP transport. These
+options take (expandable) strings as arguments.
+
++-------------------------------------------------------+
+|dkim_domain|Use: smtp|Type: string list*|Default: unset|
++-------------------------------------------------------+
+
+The domain(s) you want to sign with. After expansion, this can be a list. Each
+element in turn, lowercased, is put into the $dkim_domain expansion variable
+while expanding the remaining signing options. If it is empty after expansion,
+DKIM signing is not done, and no error will result even if dkim_strict is set.
+
++---------------------------------------------------------+
+|dkim_selector|Use: smtp|Type: string list*|Default: unset|
++---------------------------------------------------------+
+
+This sets the key selector string. After expansion, which can use $dkim_domain,
+this can be a list. Each element in turn is put in the expansion variable
+$dkim_selector which may be used in the dkim_private_key option along with
+$dkim_domain. If the option is empty after expansion, DKIM signing is not done
+for this domain, and no error will result even if dkim_strict is set.
+
+To do, for example, dual-signing with RSA and EC keys this could be be used:
+
+dkim_selector = ec_sel : rsa_sel
+dkim_private_key = KEYS_DIR/$dkim_selector
+
++-------------------------------------------------------+
+|dkim_private_key|Use: smtp|Type: string*|Default: unset|
++-------------------------------------------------------+
+
+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 (.pem file), including line
+ breaks
+
+ * with GnuTLS 3.6.0 or OpenSSL 1.1.1 or later, be a valid Ed25519 private key
+ (same format as above)
+
+ * 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.
+
+To generate keys under OpenSSL:
+
+openssl genrsa -out dkim_rsa.private 2048
+openssl rsa -in dkim_rsa.private -out /dev/stdout -pubout -outform PEM
+
+The result file from the first command should be retained, and this option set
+to use it. Take the base-64 lines from the output of the second command,
+concatenated, for the DNS TXT record. See section 3.6 of RFC6376 for the record
+specification.
+
+Under GnuTLS:
+
+certtool --generate-privkey --rsa --bits=2048 --password='' -8 --outfile=dkim_rsa.private
+certtool --load-privkey=dkim_rsa.private --pubkey-info
+
+Note that RFC 8301 says:
+
+Signers MUST use RSA keys of at least 1024 bits for all keys.
+Signers SHOULD use RSA keys of at least 2048 bits.
+
+EC keys for DKIM are defined by RFC 8463. They are considerably smaller than
+RSA keys for equivalent protection. As they are a recent development, users
+should consider dual-signing (by setting a list of selectors, and an expansion
+for this option) for some transition period. The "_CRYPTO_SIGN_ED25519" macro
+will be defined if support is present for EC keys.
+
+OpenSSL 1.1.1 and GnuTLS 3.6.0 can create Ed25519 private keys:
+
+openssl genpkey -algorithm ed25519 -out dkim_ed25519.private
+certtool --generate-privkey --key-type=ed25519 --outfile=dkim_ed25519.private
+
+To produce the required public key value for a DNS record:
+
+openssl pkey -outform DER -pubout -in dkim_ed25519.private | tail -c +13 | base64
+certtool --load_privkey=dkim_ed25519.private --pubkey_info --outder | tail -c +13 | base64
+
+Exim also supports an alternate format of Ed25519 keys in DNS which was a
+candidate during development of the standard, but not adopted. A future release
+will probably drop that support.
+
++-------------------------------------------------+
+|dkim_hash|Use: smtp|Type: string*|Default: sha256|
++-------------------------------------------------+
+
+Can be set to any one of the supported hash methods, which are:
+
+ * "sha1" - should not be used, is old and insecure
+
+ * "sha256" - the default
+
+ * "sha512" - possibly more secure but less well supported
+
+Note that RFC 8301 says:
+
+rsa-sha1 MUST NOT be used for signing or verifying.
+
++----------------------------------------------------+
+|dkim_identity|Use: smtp|Type: string*|Default: unset|
++----------------------------------------------------+
+
+If set after expansion, the value is used to set an "i=" tag in the signing
+header. The DKIM standards restrict the permissible syntax of this optional tag
+to a mail address, with possibly-empty local part, an @, and a domain identical
+to or subdomain of the "d=" tag value. Note that Exim does not check the value.
+
++-------------------------------------------------+
+|dkim_canon|Use: smtp|Type: string*|Default: unset|
++-------------------------------------------------+
+
+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 signing with the same canonicalization method for both headers and
+body.
+
++--------------------------------------------------+
+|dkim_strict|Use: smtp|Type: string*|Default: unset|
++--------------------------------------------------+
+
+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: see below|
++------------------------------------------------------------+
+
+If set, this option must expand to a colon-separated list of header names.
+Headers with these names, or the absence or such a header, will be included in
+the message signature. When unspecified, the header names listed in RFC4871
+will be used, whether or not each header is present in the message. The default
+list is available for the expansion in the macro "_DKIM_SIGN_HEADERS" and an
+oversigning variant is in "_DKIM_OVERSIGN_HEADERS".
+
+If a name is repeated, multiple headers by that name (or the absence thereof)
+will be signed. The textually later headers in the headers part of the message
+are signed first, if there are multiples.
+
+A name can be prefixed with either an "=" or a "+" character. If an "=" prefix
+is used, all headers that are present with this name will be signed. If a "+"
+prefix if used, all headers that are present with this name will be signed, and
+one signature added for a missing header with the name will be appended.
+
++-------------------------------------------------------+
+|dkim_timestamps|Use: smtp|Type: integer*|Default: unset|
++-------------------------------------------------------+
+
+This option controls the inclusion of timestamp information in the signature.
+If not set, no such information will be included. Otherwise, must be an
+unsigned number giving an offset in seconds from the current time for the
+expiry tag (eg. 1209600 for two weeks); both creation (t=) and expiry (x=) tags
+will be included.
+
+RFC 6376 lists these tags as RECOMMENDED.
+
+
+58.3 Verifying DKIM signatures in incoming mail
+-----------------------------------------------
+
+Verification of DKIM signatures in SMTP incoming email is done for all messages
+for which an ACL control dkim_disable_verify has not been set. Individual
+classes of signature algorithm can be ignored by changing the main options
+dkim_verify_hashes or dkim_verify_keytypes. The dkim_verify_minimal option can
+be set to cease verification processing for a message once the first passing
+signature is found.
+
+Performing verification sets up information used by the authresults expansion
+item.
+
+For most purposes the default option settings suffice and the remainder of this
+section can be ignored.
+
+The results of verification are made available to the acl_smtp_dkim ACL, which
+can examine and modify them. A missing ACL definition defaults to accept. By
+default, the ACL is called once for each syntactically(!) correct signature in
+the incoming message. If any ACL call does not accept, the message is not
+accepted. If a cutthrough delivery was in progress for the message, that is
+summarily dropped (having wasted the transmission effort).
+
+To evaluate the verification result 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 main option dkim_verify_signers, and an
+expansion variable $dkim_signers exist.
+
+The main 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.
+
+Note that if the option is set using untrustworthy data (such as the From:
+header) care should be taken to force lowercase for domains and for the domain
+part if identities. The default setting can be regarded as trustworthy in this
+respect.
+
+If multiple signatures match a domain (or identity), the ACL is called once for
+each matching signature.
+
+Inside the DKIM ACL, 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
+
+ Within the DKIM ACL, a string describing the general status of the
+ signature. One of
+
+ o none: There is no signature in the message for the current domain or
+ identity (as reflected by $dkim_cur_signer).
+
+ o invalid: The signature could not be verified due to a processing error.
+ More detail is available in $dkim_verify_reason.
+
+ o fail: Verification of the signature failed. More detail is available in
+ $dkim_verify_reason.
+
+ o pass: The signature passed verification. It is valid.
+
+ This variable can be overwritten using an ACL 'set' modifier. This might,
+ for instance, be done to enforce a policy restriction on hash-method or
+ key-size:
+
+ warn condition = ${if eq {$dkim_verify_status}{pass}}
+ condition = ${if eq {${length_3:$dkim_algo}}{rsa}}
+ condition = ${if or {{eq {$dkim_algo}{rsa-sha1}} \
+ {< {$dkim_key_length}{1024}}}}
+ logwrite = NOTE: forcing DKIM verify fail (was pass)
+ set dkim_verify_status = fail
+ set dkim_verify_reason = hash too weak or key too short
+
+ So long as a DKIM ACL is defined (it need do no more than accept), after
+ all the DKIM ACL runs have completed, the value becomes a colon-separated
+ list of the values after each run. This is maintained for the mime, prdr
+ and data ACLs.
+
+$dkim_verify_reason
+
+ A string giving a little bit more detail when $dkim_verify_status is either
+ "fail" or "invalid". One of
+
+ o pubkey_unavailable (when $dkim_verify_status="invalid"): The public key
+ for the domain could not be retrieved. This may be a temporary problem.
+
+ o pubkey_syntax (when $dkim_verify_status="invalid"): The public key
+ record for the domain is syntactically invalid.
+
+ o 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.
+
+ o 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.
+
+ This variable can be overwritten, with any value, using an ACL 'set'
+ modifier.
+
+$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'. If running under
+ GnuTLS 3.6.0 or OpenSSL 1.1.1 or later, may also be 'ed25519-sha256'. The
+ "_CRYPTO_SIGN_ED25519" macro will be defined if support is present for EC
+ keys.
+
+ Note that RFC 8301 says:
+
+ rsa-sha1 MUST NOT be used for signing or verifying.
+
+ DKIM signatures identified as having been signed with historic
+ algorithms (currently, rsa-sha1) have permanently failed evaluation
+
+ To enforce this you must either have a DKIM ACL which checks this variable
+ and overwrites the $dkim_verify_status variable as discussed above, or have
+ set the main option dkim_verify_hashes to exclude processing of such
+ signatures.
+
+$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). Note that RFC6376
+ requires that verification fail if the From: header is not included in the
+ signature. Exim does not enforce this; sites wishing strict enforcement
+ should code the check explicitly.
+
+$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. Note: The presence
+ of the signature tag specifying a signing body length is one possible route
+ to spoofing of valid DKIM signatures. A paranoid implementation might wish
+ to regard signature where this variable shows less than the "no limit"
+ return as being invalid.
+
+$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. Note that Exim does not check
+ 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=).
+
+$dkim_key_length
+
+ Number of bits in the key. Valid only once the key is loaded, which is at
+ the time the header signature is verified, which is after the body hash is.
+
+ Note that RFC 8301 says:
+
+ Verifiers MUST NOT consider signatures using RSA keys of
+ less than 1024 bits as valid signatures.
+
+ This is enforced by the default setting for the dkim_verify_min_keysizes
+ option.
+
+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 gmail signature
+ warn sender_domains = gmail.com
+ dkim_signers = gmail.com
+ dkim_status = none
+ log_message = GMail sender without gmail.com DKIM signature
+
+ Note that the above does not check for a total lack of DKIM signing; for
+ that check for empty $h_DKIM-Signature: in the data ACL.
+
+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 sender_domains = paypal.com:paypal.de
+ dkim_signers = paypal.com:paypal.de
+ dkim_status = none:invalid:fail
+ message = Mail from Paypal with invalid/missing signature
+
+ 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.
+
+
+58.4 SPF (Sender Policy Framework)
+----------------------------------
+
+SPF is a mechanism whereby a domain may assert which IP addresses may transmit
+messages with its domain in the envelope from, documented by RFC 7208. For more
+information on SPF see http://www.open-spf.org, a static copy of the http://
+openspf.org.
+
+Messages sent by a system not authorised will fail checking of such assertions.
+This includes retransmissions done by traditional forwarders.
+
+SPF verification support is built into Exim if SUPPORT_SPF=yes is set in Local/
+Makefile. The support uses the libspf2 library https://www.libspf2.org/. There
+is no Exim involvement in the transmission of messages; publishing certain DNS
+records is all that is required.
+
+For verification, an ACL condition and an expansion lookup are provided.
+Performing verification sets up information used by the authresults expansion
+item.
+
+The ACL condition "spf" can be used at or after the MAIL ACL. It takes as an
+argument a list of strings giving the outcome of the SPF check, and will
+succeed for any matching outcome. Valid strings are:
+
+pass
+
+ The SPF check passed, the sending host is positively verified by SPF.
+
+fail
+
+ The SPF check failed, the sending host is NOT allowed to send mail for the
+ domain in the envelope-from address.
+
+softfail
+
+ The SPF check failed, but the queried domain can't absolutely confirm that
+ this is a forgery.
+
+none
+
+ The queried domain does not publish SPF records.
+
+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".
+
+permerror
+
+ This indicates a syntax error in the SPF record of the queried domain. You
+ may deny messages when this occurs.
+
+temperror
+
+ This indicates a temporary error during all processing, including Exim's
+ SPF processing. You may defer messages when this occurs.
+
+invalid
+
+ There was an error during processing of the SPF lookup
+
+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.
+
+Example:
+
+deny spf = fail
+ 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.open-spf.org/Why;\
+ identity=${if def:sender_address_domain \
+ {$sender_address}{$sender_helo_name}};\
+ ip=$sender_host_address
+
+Note: The above mentioned URL may not be as helpful as expected. You are
+encouraged to replace the link with a link to a site with more explanations.
+
+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 (name and content) 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, i.e. with
+
+ add_header = :at_start:$spf_received
+
+ See section 44.24 for further details.
+
+ 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, currently one of
+ pass, fail, softfail, none, neutral, permerror, temperror, or "(invalid)".
+
+$spf_result_guessed
+
+ This boolean is true only if a best-guess operation was used and required
+ in order to obtain a result.
+
+$spf_smtp_comment
+
+ This contains a string that can be used in a SMTP response to the calling
+ party. Useful for "fail". The string is generated by the SPF library from
+ the template configured in the main config option spf_smtp_comment_template
+ .
+
+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.open-spf.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 spf_guess = fail
+ message = $sender_host_address doesn't look trustworthy to me
+
+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 the main configuration spf_guess
+option. For example, the following:
+
+spf_guess = v=spf1 a/16 mx/16 ptr ?all
+
+would relax host matching rules to a broader network range.
+
+A lookup expansion is also available. It takes an email address as the key and
+an IP address (v4 or v6) as the database:
+
+ ${lookup {username@domain} spf {ip.ip.ip.ip}}
+
+The lookup will return the same result strings as can appear in $spf_result
+(pass,fail,softfail,neutral,none,err_perm,err_temp).
+
+
+58.5 SRS (Sender Rewriting Scheme)
+----------------------------------
+
+SRS can be used to modify sender addresses when forwarding so that SPF
+verification does not object to them. It operates by encoding the original
+envelope sender in a new sender local part and using a domain run by the
+forwarding site as the new domain for the sender. Any DSN message should be
+returned to this new sender at the forwarding site, which can extract the
+original sender from the coded local part and forward the DSN to the
+originator.
+
+This is a way of avoiding the breakage that SPF does to forwarding. The
+constructed local-part will be longer than the original, leading to possible
+problems with very long addresses. The changing of the sender address also
+hinders the tracing of mail problems.
+
+Exim can be built to include native SRS support. To do this SUPPORT_SRS=yes
+must be defined in Local/Makefile. If this has been done, the macros _HAVE_SRS
+and _HAVE_NATIVE_SRS will be defined. The support is limited to SRS0-encoding;
+SRS1 is not supported.
+
+To encode an address use this expansion item:
+
+${srs_encode {<secret>}{<return path>}{<original domain>}}
+
+ The first argument should be a secret known and used by all systems
+ handling the recipient domain for the original message. There is no need to
+ periodically change this key; a timestamp is also encoded. The second
+ argument should be given as the envelope sender address before this
+ encoding operation.
+
+ If this value is empty the the expansion result will be empty.
+
+ The third argument should be the recipient domain of the message when it
+ arrived at this system.
+
+To decode an address use this expansion condition:
+
+inbound_srs {<local part>}{<secret>}
+
+ The first argument should be the recipient local prt as is was received.
+ The second argument is the site secret.
+
+ If the messages is not for an SRS-encoded recipient the condition will
+ return false. If it is, the condition will return true and the variable
+ $srs_recipient will be set to the decoded (original) value.
+
+Example usage:
+
+ #macro
+ SRS_SECRET = <pick something unique for your site for this. Use on all MXs.>
+
+ #routers
+
+ outbound:
+ driver = dnslookup
+ # if outbound, and forwarding has been done, use an alternate transport
+ domains = ! +my_domains
+ transport = ${if eq {$local_part@$domain} \
+ {$original_local_part@$original_domain} \
+ {remote_smtp} {remote_forwarded_smtp}}
+
+ inbound_srs:
+ driver = redirect
+ senders = :
+ domains = +my_domains
+ # detect inbound bounces which are SRS'd, and decode them
+ condition = ${if inbound_srs {$local_part} {SRS_SECRET}}
+ data = $srs_recipient
+
+ inbound_srs_failure:
+ driver = redirect
+ senders = :
+ domains = +my_domains
+ # detect inbound bounces which look SRS'd but are invalid
+ condition = ${if inbound_srs {$local_part} {}}
+ allow_fail
+ data = :fail: Invalid SRS recipient address
+
+ #... further routers here
+
+ # transport; should look like the non-forward outbound
+ # one, plus the max_rcpt and return_path options
+ remote_forwarded_smtp:
+ driver = smtp
+ # modify the envelope from, for mails that we forward
+ max_rcpt = 1
+ return_path = ${srs_encode {SRS_SECRET} {$return_path} {$original_domain}}
+
+
+58.6 DMARC
+----------
+
+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/.
+
+If Exim is built with DMARC support, the libopendmarc library is used.
+
+For building Exim yourself, obtain the library from http://sourceforge.net/
+projects/opendmarc/ to obtain a copy, or find it in your favorite package
+repository. You will need to attend to the local/Makefile feature SUPPORT_DMARC
+and the associated LDFLAGS addition. This description assumes that headers will
+be in /usr/local/include, and that the libraries are in /usr/local/lib.
+
+There are three main-configuration options:
+
+The dmarc_tld_file option 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 https://
+publicsuffix.org/list/public_suffix_list.dat. See also the util/
+renew-opendmarc-tlds.sh script. The default for the option is unset. If not
+set, DMARC processing is disabled.
+
+The dmarc_history_file option, if set 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. The default is unset.
+
+The dmarc_forensic_sender option defines an alternate 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. If set,
+this is expanded and used for the From: header line; the address is extracted
+from it and used for the envelope from. If not set (the default), the From:
+header is expanded from the dsn_from option, and <> is used for the envelope
+from.
+
+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 an ACL control modifier:
+
+ 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 is also advised that you configure a
+dmarc_forensic_sender because the default sender address construction might be
+inadequate.
+
+ control = dmarc_enable_forensic
+
+(AGAIN: You can choose not to send these forensic reports by simply not putting
+the dmarc_enable_forensic control line at any point in your exim config. If you
+don't tell exim to send them, it will not send them.)
+
+There are no options to either control. Both must appear before the DATA acl.
+
+DMARC checks cam be run on incoming SMTP messages 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:
+
+ accept The DMARC check passed and the library recommends accepting the
+ email
+ reject The DMARC check failed and the library recommends rejecting the
+ email
+ quarantine The DMARC check failed and the library recommends keeping it for
+ further inspection
+ none The DMARC check passed and the library recommends no specific
+ action, neutral
+ norecord No policy section in the DMARC record for this RFC5322.From
+ field
+ nofrom Unable to determine the domain of the sender
+ temperror Library error or dns error
+ 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.
+
+Performing the check sets up information used by the authresults expansion
+item.
+
+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:
+
+$dmarc_status
+
+ 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.
+
+$dmarc_status_text
+
+ Slightly longer, human readable status.
+
+$dmarc_used_domain
+
+ The domain which DMARC used to look up the DMARC policy record.
+
+$dmarc_domain_policy
+
+ 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.
+
+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:
+
+ * Configure the global option dmarc_history_file
+
+ * 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:
+
+ * Configure the global option dmarc_forensic_sender
+
+ * Configure, somewhere before the DATA ACL, the control option to enable
+ sending DMARC forensic reports
+
+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
+
+ 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 $dmarc_used_domain failed sender's DMARC policy, REJECT
+
+ warn add_header = :at_start:${authresults {$primary_hostname}}
+
+
+
+===============================================================================
+59. PROXIES
+
+A proxy is an intermediate system through which communication is passed.
+Proxies may provide a security, availability or load-distribution function.
+
+
+59.1 Inbound proxies
+--------------------
+
+Exim has support for receiving inbound SMTP connections via a proxy that uses
+"Proxy Protocol" to speak to it. To include this support, include
+"SUPPORT_PROXY=yes" in Local/Makefile.
+
+It was built on the HAProxy specification, found at https://www.haproxy.org/
+download/1.8/doc/proxy-protocol.txt.
+
+The purpose of this facility is so that an application load balancer, such as
+HAProxy, can sit in front of several Exim servers to distribute load. Exim uses
+the local protocol communication with the proxy to obtain the remote SMTP
+system IP address and port information. 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).
+
+Use of a proxy is enabled by setting the hosts_proxy main configuration option
+to a hostlist; connections from these hosts will use Proxy Protocol. Exim
+supports both version 1 and version 2 of the Proxy Protocol and automatically
+determines which version is in use.
+
+The Proxy Protocol header is the first data received on a TCP connection and is
+inserted before any TLS-on-connect handshake from the client; Exim negotiates
+TLS between Exim-as-server and the remote client, not between Exim and the
+proxy server. The Proxy Protocol header must be received within
+proxy_protocol_timeout, which defaults to 3s.
+
+The following expansion variables are usable ("internal" and "external" here
+refer to the interfaces of the proxy):
+
+ $proxy_external_address IP of host being proxied or IP of remote interface
+ of proxy
+ $proxy_external_port Port of host being proxied or Port on remote
+ interface of proxy
+ $proxy_local_address IP of proxy server inbound or IP of local interface
+ of proxy
+ $proxy_local_port Port of proxy server inbound or Port on local
+ interface of proxy
+ $proxy_session boolean: SMTP connection via proxy
+
+If $proxy_session is set but $proxy_external_address is empty there was a
+protocol error. The variables $sender_host_address and $sender_host_port will
+have values for the actual client system, not the proxy.
+
+Since the real connections are all coming from the 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 option set so high, you lose the
+ability to protect your server from many connections from one IP. In order to
+prevent your server from overload, you need to add a per connection ratelimit
+to your connect ACL. A possible solution is:
+
+ # 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 ratelimit = LIMIT / 5s / per_conn / strict
+ message = Too many connections from this IP right now
+
+
+59.2 Outbound proxies
+---------------------
+
+Exim has support for sending outbound SMTP via a proxy using a protocol called
+SOCKS5 (defined by RFC1928). The support can be optionally included by defining
+SUPPORT_SOCKS=yes in Local/Makefile.
+
+Use of a proxy is enabled by setting the socks_proxy option on an smtp
+transport. The option value is expanded and should then be a list
+(colon-separated by default) of proxy specifiers. Each proxy specifier is a
+list (space-separated by default) where the initial element is an IP address
+and any subsequent elements are options.
+
+Options are a string <name>=<value>. The list of options is in the following
+table:
+
+ auth authentication method
+ name authentication username
+ pass authentication password
+ port tcp port
+ tmo connection timeout
+ pri priority
+ weight selection bias
+
+More details on each of these options follows:
+
+ * auth: Either "none" (default) or "name". Using "name" selects username/
+ password authentication per RFC 1929 for access to the proxy. Default is
+ "none".
+
+ * name: sets the username for the "name" authentication method. Default is
+ empty.
+
+ * pass: sets the password for the "name" authentication method. Default is
+ empty.
+
+ * port: the TCP port number to use for the connection to the proxy. Default
+ is 1080.
+
+ * tmo: sets a connection timeout in seconds for this proxy. Default is 5.
+
+ * pri: specifies a priority for the proxy within the list, higher values
+ being tried first. The default priority is 1.
+
+ * weight: specifies a selection bias. Within a priority set servers are
+ queried in a random fashion, weighted by this value. The default value for
+ selection bias is 1.
+
+Proxies from the list are tried according to their priority and weight settings
+until one responds. The timeout for the overall connection applies to the set
+of proxied attempts.
+
+
+59.3 Logging
+------------
+
+To log the (local) IP of a proxy in the incoming or delivery logline, add
+"+proxy" to the log_selector option. This will add a component tagged with "PRX
+=" to the line.
+
+
+
+===============================================================================
+60. INTERNATIONALISATION
+
+Exim has support for Internationalised mail names. To include this it must be
+built with SUPPORT_I18N and the libidn library. Standards supported are RFCs
+2060, 5890, 6530 and 6533.
+
+If Exim is built with SUPPORT_I18N_2008 (in addition to SUPPORT_I18N, not
+instead of it) then IDNA2008 is supported; this adds an extra library
+requirement, upon libidn2.
+
+
+60.1 MTA operations
+-------------------
+
+The main configuration option smtputf8_advertise_hosts specifies a host list.
+If this matches the sending host and accept_8bitmime is true (the default) then
+the ESMTP option SMTPUTF8 will be advertised.
+
+If the sender specifies the SMTPUTF8 option on a MAIL command international
+handling for the message is enabled and the expansion variable
+$message_smtputf8 will have value TRUE.
+
+The option allow_utf8_domains is set to true for this message. All DNS lookups
+are converted to a-label form whatever the setting of allow_utf8_domains when
+Exim is built with SUPPORT_I18N.
+
+Both localparts and domain are maintained as the original UTF-8 form
+internally; any comparison or regular-expression use will require appropriate
+care. Filenames created, eg. by the appendfile transport, will have UTF-8
+names.
+
+HELO names sent by the smtp transport will have any UTF-8 components expanded
+to a-label form, and any certificate name checks will be done using the a-label
+form of the name.
+
+Log lines and Received-by: header lines will acquire a "utf8" prefix on the
+protocol element, eg. utf8esmtp.
+
+The following expansion operators can be used:
+
+${utf8_domain_to_alabel:str}
+${utf8_domain_from_alabel:str}
+${utf8_localpart_to_alabel:str}
+${utf8_localpart_from_alabel:str}
+
+The RCPT ACL may use the following modifier:
+
+control = utf8_downconvert
+control = utf8_downconvert/<value>
+
+This sets a flag requiring that envelope addresses are converted to a-label
+form before smtp delivery. This is usually for use in a Message Submission
+Agent context, but could be used for any message.
+
+If a value is appended it may be:
+
+ "1" mandatory downconversion
+ "0" no downconversion
+ "-1" if SMTPUTF8 not supported by destination host
+
+If no value is given, 1 is used.
+
+If mua_wrapper is set, the utf8_downconvert control is initially set to -1.
+
+The smtp transport has an option utf8_downconvert. If set it must expand to one
+of the three values described above, or an empty string. If non-empty it
+overrides value previously set (due to mua_wrapper or by an ACL control).
+
+There is no explicit support for VRFY and EXPN. Configurations supporting these
+should inspect $smtp_command_argument for an SMTPUTF8 argument.
+
+There is no support for LMTP on Unix sockets. Using the "lmtp" protocol option
+on an smtp transport, for LMTP over TCP, should work as expected.
+
+There is no support for DSN unitext handling, and no provision for converting
+logging from or to UTF-8.
+
+
+60.2 MDA operations
+-------------------
+
+To aid in constructing names suitable for IMAP folders the following expansion
+operator can be used:
+
+${imapfolder {<string>} {<sep>} {<specials>}}
+
+The string is converted from the charset specified by the "headers charset"
+command (in a filter file) or headers_charset main configuration option
+(otherwise), to the modified UTF-7 encoding specified by RFC 2060, with the
+following exception: All occurrences of <sep> (which has to be a single
+character) are replaced with periods ("."), and all periods and slashes that
+are not <sep> and are not in the <specials> string are BASE64 encoded.
+
+The third argument can be omitted, defaulting to an empty string. The second
+argument can be omitted, defaulting to "/".
+
+This is the encoding used by Courier for Maildir names on disk, and followed by
+many other IMAP servers.
+
+Examples:
+
+${imapfolder {Foo/Bar}} yields Foo.Bar
+${imapfolder {Foo/Bar}{.}{/}} yields Foo&AC8-Bar
+${imapfolder {R?ksm?rg?s}} yields R&AOQ-ksm&APY-rg&AOU-s
+
+Note that the source charset setting is vital, and also that characters must be
+representable in UTF-16.
+
+
+
+===============================================================================
+61. EVENTS
+
+The events mechanism in Exim can be used to intercept processing at a number of
+points. It was originally invented to give a way to do customised logging
+actions (for example, to a database) but can also be used to modify some
+processing actions.
+
+Most installations will never need to use Events. The support can be left out
+of a build by defining DISABLE_EVENT=yes in Local/Makefile.
+
+There are two major classes of events: main and transport. The main
+configuration option event_action controls reception events; a transport option
+event_action controls delivery events.
+
+Both options are a string which is expanded when the event fires. An example
+might look like:
+
+event_action = ${if eq {msg:delivery}{$event_name} \
+{${lookup pgsql {SELECT * FROM record_Delivery( \
+ '${quote_pgsql:$sender_address_domain}',\
+ '${quote_pgsql:${lc:$sender_address_local_part}}', \
+ '${quote_pgsql:$domain}', \
+ '${quote_pgsql:${lc:$local_part}}', \
+ '${quote_pgsql:$host_address}', \
+ '${quote_pgsql:${lc:$host}}', \
+ '${quote_pgsql:$message_exim_id}')}} \
+} {}}
+
+Events have names which correspond to the point in process at which they fire.
+The name is placed in the variable $event_name and the event action expansion
+must check this, as it will be called for every possible event type.
+
+The current list of events is:
+
++-----------------------------------------------------------------------------+
+| dane:fail |after |transport|per connection |
+|-----------------------+------+---------+------------------------------------|
+| msg:complete |after | main |per message |
+|-----------------------+------+---------+------------------------------------|
+| msg:defer |after |transport|per message per delivery try |
+|-----------------------+------+---------+------------------------------------|
+| msg:delivery |after |transport|per recipient |
+|-----------------------+------+---------+------------------------------------|
+| msg:rcpt:host:defer|after |transport|per recipient per host |
+|-----------------------+------+---------+------------------------------------|
+| msg:rcpt:defer |after |transport|per recipient |
+|-----------------------+------+---------+------------------------------------|
+| msg:host:defer |after |transport|per host per delivery try; host |
+| | | |errors |
+|-----------------------+------+---------+------------------------------------|
+| msg:fail:delivery |after |transport|per recipient |
+|-----------------------+------+---------+------------------------------------|
+| msg:fail:internal |after | main |per recipient |
+|-----------------------+------+---------+------------------------------------|
+| tcp:connect |before|transport|per connection |
+|-----------------------+------+---------+------------------------------------|
+| tcp:close |after |transport|per connection |
+|-----------------------+------+---------+------------------------------------|
+| tls:cert |before| both |per certificate in verification |
+| | | |chain |
+|-----------------------+------+---------+------------------------------------|
+| tls:fail:connect |after | main |per connection |
+|-----------------------+------+---------+------------------------------------|
+| smtp:connect |after |transport|per connection |
+|-----------------------+------+---------+------------------------------------|
+| smtp:ehlo |after |transport|per connection |
++-----------------------------------------------------------------------------+
+
+New event types may be added in future.
+
+The event name is a colon-separated list, defining the type of event in a tree
+of possibilities. It may be used as a list or just matched on as a whole. There
+will be no spaces in the name.
+
+The second column in the table above describes whether the event fires before
+or after the action is associates with. Those which fire before can be used to
+affect that action (more on this below).
+
+The third column in the table above says what section of the configuration
+should define the event action.
+
+An additional variable, $event_data, is filled with information varying with
+the event type:
+
++-------------------------------------------------+
+| dane:fail |failure reason |
+|-----------------------+-------------------------|
+| msg:defer |error string |
+|-----------------------+-------------------------|
+| msg:delivery |smtp confirmation message|
+|-----------------------+-------------------------|
+| msg:fail:internal |failure reason |
+|-----------------------+-------------------------|
+| msg:fail:delivery |smtp error message |
+|-----------------------+-------------------------|
+| msg:host:defer |error string |
+|-----------------------+-------------------------|
+| msg:rcpt:host:defer|error string |
+|-----------------------+-------------------------|
+| msg:rcpt:defer |error string |
+|-----------------------+-------------------------|
+| tls:cert |verification chain depth |
+|-----------------------+-------------------------|
+| tls:fail:connect |error string |
+|-----------------------+-------------------------|
+| smtp:connect |smtp banner |
+|-----------------------+-------------------------|
+| smtp:ehlo |smtp ehlo response |
++-------------------------------------------------+
+
+The :defer events populate one extra variable: $event_defer_errno.
+
+For complex operations an ACL expansion can be used in event_action, however
+due to the multiple contexts that Exim operates in during the course of its
+processing:
+
+ * variables set in transport events will not be visible outside that
+ transport call
+
+ * acl_m variables in a server context are lost on a new connection, and after
+ smtp helo/ehlo/mail/starttls/rset commands
+
+Using an ACL expansion with the logwrite modifier can be a useful way of
+writing to the main log.
+
+The expansion of the event_action option should normally return an empty
+string. Should it return anything else the following will be forced:
+
++------------------------------------+
+| tcp:connect |do not connect |
+|----------------+-------------------|
+| tls:cert |refuse verification|
+|----------------+-------------------|
+| smtp:connect|close connection |
++------------------------------------+
+
+All other message types ignore the result string, and no other use is made of
+it.
+
+For a tcp:connect event, if the connection is being made to a proxy then the
+address and port variables will be that of the proxy and not the target system.
+
+For tls:cert events, if GnuTLS is in use this will trigger only per chain
+element received on the connection. For OpenSSL it will trigger for every chain
+element including those loaded locally.
+
+
+
+===============================================================================
+62. 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. Edit OS/Makefile-Base adding a .o file for the predefined-macros, to the
+ definition of OBJ_MACRO. Add a set of line to do the compile also.
+
+ 8. Create newdriver.h and newdriver.c in the appropriate sub-directory of src.
+
+ 9. 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
index 0000000..a70b7cb
--- /dev/null
+++ b/exim_monitor/EDITME
@@ -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
index 0000000..3b94c22
--- /dev/null
+++ b/exim_monitor/em_StripChart.c
@@ -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 receive 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
index 0000000..ff5d1a8
--- /dev/null
+++ b/exim_monitor/em_TextPop.c
@@ -0,0 +1,767 @@
+/***********************************************************
+Copyright (c) The Exim Maintainers 2022
+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 beginning 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 successful 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: PopdownSearch
+ * 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 argument 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) {
+ 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) {
+ 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 search structure.
+ * Returns: TRUE if successful.
+ */
+
+/* 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) ((long)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 arguments.
+ *
+ * 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
index 0000000..3d452c6
--- /dev/null
+++ b/exim_monitor/em_globals.c
@@ -0,0 +1,239 @@
+/*************************************************
+* Exim Monitor *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2021 */
+/* 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;
+
+pcre2_code *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;
+pcre2_code **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";
+unsigned dkim_collect_input = 0;
+BOOL dkim_disable_verify = FALSE;
+#endif
+
+BOOL dont_deliver = FALSE;
+
+int dsn_ret = 0;
+uschar *dsn_envid = NULL;
+
+struct global_flags f = {
+ .sender_local = FALSE,
+};
+
+#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;
+
+uschar *queue_name = US"";
+
+int received_count = 0;
+uschar *received_protocol = NULL;
+struct timeval received_time = { 0, 0 };
+struct timeval received_time_complete = { 0, 0 };
+int recipients_count = 0;
+recipient_item *recipients_list = NULL;
+int recipients_list_max = 0;
+BOOL 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_auth_pubname = NULL;
+uschar *sender_host_authenticated = NULL;
+uschar *sender_host_name = NULL;
+int sender_host_port = 0;
+uschar *sender_ident = NULL;
+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 = {
+ .active = { .sock = -1 }
+ /* remainder zero/null/false */
+};
+
+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
index 0000000..ab37806
--- /dev/null
+++ b/exim_monitor/em_hdr.h
@@ -0,0 +1,329 @@
+/*************************************************
+* Exim Monitor *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/* 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 */
+
+#define PCRE2_CODE_UNIT_WIDTH 8
+#include <pcre2.h>
+
+/* Includes from the main source of Exim. One of these days I should tidy up
+this interface so that this kind of kludge isn't needed. */
+
+#ifndef NS_MAXMSG
+# define NS_MAXMSG 65535
+#endif
+typedef void * hctx;
+
+#include "local_scan.h"
+#include "macros.h"
+#include "structs.h"
+#include "blob.h"
+#include "globals.h"
+#include "hintsdb.h"
+#include "hintsdb_structs.h"
+#include "functions.h"
+#include "osfunctions.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 pcre2_code *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 pcre2_code **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
index 0000000..e0bc3b0
--- /dev/null
+++ b/exim_monitor/em_init.c
@@ -0,0 +1,238 @@
+/*************************************************
+* Exim monitor *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* Copyright (c) The Exim Maintainers 2020 - 2021 */
+/* 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)
+ {
+ size_t offset;
+ int err;
+
+ if (!(stripchart_regex[indx] =
+ pcre2_compile((PCRE2_SPTR)buffer,
+ PCRE2_ZERO_TERMINATED, PCRE_COPT,
+ &err, &offset, NULL)))
+ {
+ uschar errbuf[128];
+ pcre2_get_error_message(err, errbuf, sizeof(errbuf));
+ printf("regular expression error: %s at offset %ld "
+ "while compiling %s\n", errbuf, (long)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 = (pcre2_code **)store_malloc(stripchart_number * sizeof(pcre2_code *));
+ stripchart_title = (uschar **)store_malloc(stripchart_number * sizeof(uschar *));
+ }
+ }
+}
+
+
+/*************************************************
+* Initialize *
+*************************************************/
+
+void init(int argc, uschar **argv)
+{
+int x;
+size_t erroroffset;
+uschar *s;
+
+argc = argc; /* These are currently unused. */
+argv = argv;
+
+/* Deal with simple values in the environment. */
+
+if ((s = US getenv("ACTION_OUTPUT")))
+ {
+ if (Ustrcmp(s, "no") == 0) action_output = FALSE;
+ if (Ustrcmp(s, "yes") == 0) action_output = TRUE;
+ }
+
+if ((s = US getenv("ACTION_QUEUE_UPDATE")))
+ {
+ 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 && (x = Uatoi(s)) != 0) body_max = x;
+
+if ((s = US getenv("EXIM_PATH")))
+ exim_path = string_copy(s);
+
+if ((s = US getenv("EXIMON_EXIM_CONFIG")))
+ alternate_config = string_copy(s);
+
+if ((s = US getenv("LOG_BUFFER")))
+ {
+ 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 && (x = Uatoi(s)) != 0) log_depth = x;
+
+if ((s = US getenv("LOG_FILE_NAME")))
+ log_file = string_copy(s);
+
+if ((s = US getenv("LOG_FONT")))
+ log_font = string_copy(s);
+
+s = US getenv("LOG_WIDTH");
+if (s && (x = Uatoi(s)) != 0) log_width = x;
+
+if ((s = US getenv("MENU_EVENT")))
+ menu_event = string_copy(s);
+
+s = US getenv("MIN_HEIGHT");
+if (s && (x = Uatoi(s)) > 0) min_height = x;
+
+s = US getenv("MIN_WIDTH");
+if (s && (x = Uatoi(s)) > 0) min_width = x;
+
+if ((s = US getenv("QUALIFY_DOMAIN")))
+ qualify_domain = string_copy(s);
+else
+ qualify_domain = US""; /* Don't want NULL */
+
+s = US getenv("QUEUE_DEPTH");
+if (s && (x = Uatoi(s)) != 0) queue_depth = x;
+
+if ((s = US getenv("QUEUE_FONT")))
+ queue_font = string_copy(s);
+
+s = US getenv("QUEUE_INTERVAL");
+if (s && (x = Uatoi(s)) != 0) queue_update = x;
+
+s = US getenv("QUEUE_MAX_ADDRESSES");
+if (s && (x = Uatoi(s)) != 0) queue_max_addresses = x;
+
+s = US getenv("QUEUE_WIDTH");
+if (s && (x = Uatoi(s)) != 0) queue_width = x;
+
+if ((s = US getenv("SPOOL_DIRECTORY")))
+ spool_directory = string_copy(s);
+
+s = US getenv("START_SMALL");
+if (s && Ustrcmp(s, "yes") == 0) start_small = 1;
+
+s = US getenv("TEXT_DEPTH");
+if (s && (x = Uatoi(s)) != 0) text_depth = x;
+
+if ((s = US getenv("WINDOW_TITLE")))
+ 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 && *s)
+ {
+ 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);
+ }
+
+if ((s = US getenv("LOG_STRIPCHARTS")))
+ decode_stripchart_config(s);
+
+s = US getenv("STRIPCHART_INTERVAL");
+if (s && (x = Uatoi(s)) != 0) stripchart_update = x;
+
+s = US getenv("QUEUE_STRIPCHART_NAME");
+queue_stripchart_name = s ? string_copy(s) : US"queue";
+
+/* Compile the regex for matching yyyy-mm-dd at the start of a string. */
+
+yyyymmdd_regex = pcre2_compile((PCRE2_SPTR)"^\\d{4}-\\d\\d-\\d\\d\\s",
+ PCRE2_ZERO_TERMINATED, PCRE_COPT, &x, &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
index 0000000..8d85c13
--- /dev/null
+++ b/exim_monitor/em_log.c
@@ -0,0 +1,411 @@
+/*************************************************
+* Exim Monitor *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainters 2021 - 2022 */
+/* 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)
+ {
+ if (fseek(LOG, log_position, SEEK_SET))
+ {
+ perror("logfile fseek");
+ exit(1);
+ }
+
+ while (Ufgets(buffer, log_buffer_len, LOG) != NULL)
+ {
+ uschar *id;
+ uschar *p = buffer;
+ rmark reset_point;
+ int length = Ustrlen(buffer);
+ pcre2_match_data * md = pcre2_match_data_create(1, NULL);
+
+ /* 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_mark();
+
+ /* 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 (int i = stripchart_varstart; i < stripchart_number; i++)
+ if (pcre2_match(stripchart_regex[i], (PCRE2_SPTR)buffer, length,
+ 0, PCRE_EOPT, md, NULL) >= 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 (pcre2_match(yyyymmdd_regex, (PCRE2_SPTR) buffer, length, 0, PCRE_EOPT,
+ md, NULL) >= 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);
+ }
+ pcre2_match_data_free(md);
+
+ /* Deal with frozen and unfrozen messages */
+
+ if (strstric(buffer, US"frozen", FALSE) != NULL)
+ {
+ queue_item *qq = find_queue(id, queue_noop, 0);
+ if (qq)
+ qq->frozen = strstric(buffer, US"unfrozen", FALSE) == NULL;
+ }
+
+ /* 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)
+ {
+ 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 string to
+ be expanded. The trailing NULL arg is to quieten preprocessors that need at
+ least one arg for a variadic set in a macro. */
+ string_format(log_file_wanted, sizeof(log_file_wanted), CS log_file, NULL);
+ 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;
+ if (fstat(fileno(LOG), &statdata))
+ {
+ fprintf(stderr, "fstat %s: %s\n", log_file_open, strerror(errno));
+ exit(1);
+ }
+ 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
index 0000000..5714b99
--- /dev/null
+++ b/exim_monitor/em_main.c
@@ -0,0 +1,953 @@
+/*************************************************
+* Exim Monitor *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/* 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);
+}
+
+
+
+
+/*************************************************
+* 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 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;
+}
+
+
+
+
+/*************************************************
+* 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)
+{
+scan_spool_input(TRUE);
+queue_display();
+tick_queue_accumulator = 0;
+}
+
+void hideAction(Widget w, XtPointer client_data, XtPointer 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;
+
+while (sk)
+ {
+ 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)
+{
+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);
+
+/* 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();
+
+/* 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. */
+
+store_init();
+big_buffer = store_get(big_buffer_size, GET_UNTAINTED);
+
+/* 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 = US 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, NULL);
+ 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);
+ if (fstat(fileno(LOG), &statdata))
+ {
+ perror("log file fstat");
+ fclose(LOG);
+ LOG=NULL;
+ }
+ else
+ 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
index 0000000..881f374
--- /dev/null
+++ b/exim_monitor/em_menu.c
@@ -0,0 +1,930 @@
+/*************************************************
+* Exim Monitor *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2021 */
+/* 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)
+{
+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)
+{
+Widget text = text_create(US client_data, text_depth);
+uschar * fname = NULL;
+FILE * f = NULL;
+
+/* End up with the split version, so message looks right when non-exist */
+
+for (int i = 0; i < (spool_is_split ? 2:1); i++)
+ {
+ message_subdir[0] = i != 0 ? (US client_data)[5] : 0;
+ fname = spool_fname(US"msglog", message_subdir, US client_data, US"");
+ if ((f = fopen(CS fname, "r")))
+ break;
+ }
+
+if (!f)
+ text_showf(text, "%s: %s\n", fname, strerror(errno));
+else
+ {
+ uschar buffer[256];
+ while (Ufgets(buffer, sizeof(buffer), f) != NULL) text_show(text, buffer);
+ fclose(f);
+ }
+}
+
+
+
+/*************************************************
+* Display the message body *
+*************************************************/
+
+static void
+bodyAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+Widget text = text_create(US client_data, text_depth);
+FILE *f = NULL;
+
+for (int i = 0; i < (spool_is_split? 2:1); i++)
+ {
+ uschar * fname;
+ message_subdir[0] = i != 0 ? (US client_data)[5] : 0;
+ fname = spool_fname(US"input", message_subdir, US client_data, US"-D");
+ if ((f = fopen(CS fname, "r")))
+ break;
+ }
+
+if (!f)
+ text_showf(text, "Failed to open file: %s\n", strerror(errno));
+else
+ {
+ uschar buffer[256];
+ int count = 0;
+
+ while (Ufgets(buffer, sizeof(buffer), 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;
+ }
+
+if ( fcntl(pipe_fd[0], F_SETFL, O_NONBLOCK)
+ || fcntl(pipe_fd[1], F_SETFL, O_NONBLOCK))
+ {
+ perror("set nonblocking on pipe");
+ exit(1);
+ }
+
+/* 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)
+ {
+ if (q->sender) 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(errno)); 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)
+{
+ActOnMessage(US client_data, US"-v -M", US"");
+}
+
+/*************************************************
+* Cause a message to be Frozen *
+*************************************************/
+
+static void
+freezeAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+ActOnMessage(US client_data, US"-Mf", US"");
+}
+
+/*************************************************
+* Cause a message to be thawed *
+*************************************************/
+
+static void
+thawAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+ActOnMessage(US 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);
+
+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)
+{
+Ustrncpy(actioned_message, client_data, 24);
+actioned_message[23] = '\0';
+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)
+{
+Ustrncpy(actioned_message, client_data, 24);
+actioned_message[23] = '\0';
+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)
+{
+ActOnMessage(US 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;
+
+Ustrncpy(actioned_message, client_data, 24);
+actioned_message[23] = '\0';
+q = find_queue(actioned_message, queue_noop, 0);
+sender = !q ? 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)
+{
+ActOnMessage(US client_data, US"-v -Mg", US"");
+}
+
+/*************************************************
+* Cause a message to be cancelled *
+*************************************************/
+
+static void
+removeAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+ActOnMessage(US client_data, US"-Mrm", US"");
+}
+
+/*************************************************
+* Display a message's headers *
+*************************************************/
+
+static void
+headersAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+uschar buffer[256];
+Widget text = text_create(US client_data, text_depth);
+rmark reset_point;
+
+/* 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_mark();
+
+sprintf(CS buffer, "%s-H", US 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=%lu\n", buffer,
+ (unsigned long)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)
+ text_showf(text, "%s sender: <%s>\n", f.sender_local ? "Local" : "Remote",
+ sender_address);
+
+if (recipients_list)
+ {
+ text_show(text, US"Recipients:\n");
+ for (int i = 0; i < recipients_count; i++)
+ text_showf(text, " %s %s\n",
+ tree_search(tree_nonrecipients, recipients_list[i].address)
+ ? "*" : " ",
+ recipients_list[i].address);
+ text_show(text, US"\n");
+ }
+
+for (header_line * next, * h = header_list; h; 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)
+{
+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. */
+
+for (pipe_item * p = pipe_chain; p; p = p->next)
+ for (Widget pp = p->widget; pp; pp = XtParent(pp))
+ if (pp == (Widget)client_data) { p->widget = NULL; return; }
+}
+
+
+
+/*************************************************
+* 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\
+ ");
+
+/* 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
index 0000000..9badd24
--- /dev/null
+++ b/exim_monitor/em_queue.c
@@ -0,0 +1,828 @@
+/*************************************************
+* Exim Monitor *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* 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), GET_UNTAINTED);
+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;
+rmark 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;
+Ustrncpy(q->name, name, sizeof(q->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_mark();
+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.tv_sec;
+ 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 = " OFF_T_FMT " ***",
+ statbuf.st_size);
+ else msg = US"*** Format error in spool file ***";
+ }
+ else msg = US"*** Cannot read spool file ***";
+
+ if (rc == spool_read_hdrerror)
+ {
+ (void)find_dest(q, msg, dest_add, FALSE);
+ }
+ else
+ {
+ f.deliver_freeze = FALSE;
+ sender_address = msg;
+ recipients_count = 0;
+ }
+ }
+
+/* Now set up the remaining data. */
+
+q->frozen = f.deliver_freeze;
+
+if (f.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;
+
+snprintf(CS buffer, sizeof(buffer), "%s/input/%s/%s/%s-D",
+ spool_directory, queue_name, 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;
+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 */
+ DIR *dd;
+ struct dirent *ent;
+
+ if (subdirchar != 0)
+ {
+ input_dir[subptr] = '/';
+ input_dir[subptr+1] = subdirchar;
+ }
+
+ if (!(dd = exim_opendir(input_dir))) continue;
+
+ while ((ent = readdir(dd)))
+ {
+ 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!*/
+
+for (p = queue_index[0]; p; )
+ if (!p->seen)
+ {
+ queue_item * next = p->next;
+ if (p->prev)
+ p->prev->next = next;
+ else
+ queue_index[0] = next;
+ if (next)
+ next->prev = p->prev;
+ else
+ {
+ 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;
+ }
+ 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;
+rmark reset_point;
+struct stat statdata;
+uschar buffer[1024];
+
+message_subdir[0] = p->dir_char;
+
+snprintf(CS buffer, sizeof(buffer), "%s/input/%s/%s/%s-J",
+ spool_directory, queue_name, message_subdir, p->name);
+
+if (!(jread = fopen(CS buffer, "r")))
+ {
+ snprintf(CS buffer, sizeof(buffer), "%s/input/%s/%s/%s-H",
+ spool_directory, queue_name, 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_mark();
+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)
+ for (i = 0; i < recipients_count; i++)
+ {
+ uschar * pp;
+ uschar * r = recipients_list[i].address;
+ tree_node * node;
+
+ if (!(node = tree_search(tree_nonrecipients, r)))
+ node = tree_search(tree_nonrecipients, string_copylc(r));
+
+ if ((pp = strstric(r+1, qualify_domain, FALSE)) && *(--pp) == '@')
+ *pp = 0;
+ if (!node)
+ (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
index 0000000..03864d2
--- /dev/null
+++ b/exim_monitor/em_strip.c
@@ -0,0 +1,266 @@
+/*************************************************
+* 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 previous 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 = (long)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, US"%");
+ 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)(long)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
index 0000000..3a36829
--- /dev/null
+++ b/exim_monitor/em_text.c
@@ -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
index 0000000..b627a6e
--- /dev/null
+++ b/exim_monitor/em_version.c
@@ -0,0 +1,65 @@
+/*************************************************
+* Exim Monitor *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 - 2021 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#define EM_VERSION_C
+
+/* Needed by macros.h */
+/* 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 "mytypes.h"
+#include "store.h"
+#include "macros.h"
+#include <string.h>
+#include <stdlib.h>
+
+#include "version.h"
+
+extern uschar *version_string;
+extern uschar *version_date;
+
+void
+version_init(void)
+{
+int i = 0;
+uschar today[20];
+
+version_string = US"2.06";
+
+#ifdef EXIM_BUILD_DATE_OVERRIDE
+/* Reproducible build support; build tooling should have given us something looking like
+ * "25-Feb-2017 20:15:40" in EXIM_BUILD_DATE_OVERRIDE based on $SOURCE_DATE_EPOCH in environ
+ * per <https://reproducible-builds.org/specs/source-date-epoch/>
+ */
+version_date = US malloc(32);
+version_date[0] = 0;
+Ustrncat(version_date, EXIM_BUILD_DATE_OVERRIDE, 31);
+
+#else
+Ustrcpy(today, US __DATE__);
+if (today[4] == ' ') i = 1;
+today[3] = today[6] = '-';
+
+version_date = US 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, US" ");
+Ustrcat(version_date, US __TIME__);
+#endif
+}
+
+/* End of em_version.c */
diff --git a/exim_monitor/em_xs.c b/exim_monitor/em_xs.c
new file mode 100644
index 0000000..ee91f7c
--- /dev/null
+++ b/exim_monitor/em_xs.c
@@ -0,0 +1,45 @@
+/*************************************************
+* Exim Monitor *
+*************************************************/
+
+/* Copyright (c) University of Cambridge, 1995 - 2016 */
+/* 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)? store_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);
+ }
+va_end(ap);
+XtSetValues(w, aa, num_args);
+if (num_args > 15) store_free(aa);
+}
+
+/* End of em_xs.c */
diff --git a/scripts/Configure b/scripts/Configure
new file mode 100755
index 0000000..6c340ee
--- /dev/null
+++ b/scripts/Configure
@@ -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
index 0000000..ed77b6a
--- /dev/null
+++ b/scripts/Configure-Makefile
@@ -0,0 +1,360 @@
+#! /bin/sh
+LC_ALL=C
+export LC_ALL
+
+# 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.
+#
+# Copyright (c) The Exim Maintainers 1995 - 2021
+
+
+# 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
+mftepcp=$mf-tepcp
+mftepcp2=$mf-tepcp2
+
+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 $mftepcp $mftepcp2 $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 \
+ | sed 's/^TMPDIR=/EXIM_&/' \
+ >> $mft || exit 1
+
+# handle PKG_CONFIG_PATH because we need it in our env, and we want to handle
+# wildcards; note that this logic means all setting _appends_ values, never
+# replacing; if that's a problem, we can revisit.
+sed -n "s/^[$st]*PKG_CONFIG_PATH[$st]*[+]*=[$st]*//p" $mft | \
+ sed "s/[$st]*\$//" >> $mftepcp
+if test -s ./$mftepcp
+then
+ # expand any wildcards and strip spaces, to make it a real PATH-like variable
+ ( IFS=":${IFS-$st}"; for P in `cat ./$mftepcp`; do echo "$P"; done ) | xargs | sed "s/[$st]/:/g" >./$mftepcp2
+ sed "s/^/PKG_CONFIG_PATH='/" < ./$mftepcp2 | sed "s/\$/'/" > ./$mftepcp
+ . ./$mftepcp
+ export PKG_CONFIG_PATH
+ egrep -v "^[$st]*PKG_CONFIG_PATH[$st]*=" ./$mft > ./$mftt
+ rm -f ./$mft
+ (
+ echo "# Collapsed PKG_CONFIG_PATH in build-prep:"
+ sed "s/'//g" ./$mftepcp
+ echo "# End of collapsed PKG_CONFIG_PATH"
+ echo ""
+ cat ./$mftt
+ ) > ./$mft
+ rm -f ./$mftt
+fi
+rm -f ./$mftepcp ./$mftepcp2
+
+# 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|PCRE2?_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=''
+ need_core=''
+ if [ ".$DISABLE_TLS" = .yes ]; then
+ # no TLS, not referencing
+ true
+ elif [ ".$var" = ".USE_GNUTLS_PC" ] && [ ".$USE_GNUTLS" != "." ]; then
+ need_this=t
+ need_core="gnutls-special"
+ elif [ ".$var" = ".USE_OPENSSL_PC" ] && [ ".$USE_GNUTLS" = "." ]; then
+ need_this=t
+ need_core=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"
+ # With hash.h pulling crypto into the core, we need to also handle that
+ if [ ".$need_this" = ".t" ]; then
+ echo "CFLAGS += $tls_include"
+ echo "LDFLAGS += $tls_libs"
+ elif [ ".$need_this" = ".gnutls-special" ]; then
+ if pkg-config --atleast-version=2.10 gnutls ; then
+ echo "CFLAGS += $tls_include"
+ echo "LDFLAGS += $tls_libs"
+ else
+ echo "CFLAGS += `libgcrypt-config --cflags`"
+ echo "LDFLAGS += `libgcrypt-config --libs`"
+ fi
+ fi
+ 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)
+ echo >&2 "pcre is no longer supported; migrate to pcre2"
+ exit 1
+
+# 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
+ ;;
+
+ PCRE2_CONFIG)
+ case $PCRE2_CONFIG in
+ yes|YES|y|Y)
+ cflags=`pcre2-config --cflags`
+ if [ $? -ne 0 ]; then
+ echo >&2 "*** Missing pcre2-config for regular expression support"
+ exit 1
+ fi
+ libs=`pcre2-config --libs8`
+ 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
index 0000000..75d366f
--- /dev/null
+++ b/scripts/Configure-config.h
@@ -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
index 0000000..0aca3b3
--- /dev/null
+++ b/scripts/Configure-eximon
@@ -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
index 0000000..f00a562
--- /dev/null
+++ b/scripts/Configure-os.c
@@ -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
index 0000000..ae1ecf9
--- /dev/null
+++ b/scripts/Configure-os.h
@@ -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 accommodate 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
index 0000000..9cdb931
--- /dev/null
+++ b/scripts/MakeLinks
@@ -0,0 +1,133 @@
+#!/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.
+#
+# Copyright (c) The Exim Maintainers 1995 - 2022
+
+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
+# Makefile is generated
+for f in README cdb.c dbmdb.c dnsdb.c dsearch.c ibase.c json.c ldap.h ldap.c \
+ lmdb.c lsearch.c mysql.c nis.c nisplus.c oracle.c passwd.c \
+ pgsql.c readsock.c redis.c spf.c sqlite.c testdb.c whoson.c \
+ lf_functions.h lf_check_file.c lf_quote.c lf_sqlperform.c
+do
+ ln -s ../../src/lookups/$f $f
+done
+
+cd ..
+
+# Likewise for the code for the routers
+mkdir routers
+cd routers
+for f in README Makefile accept.h accept.c dnslookup.h dnslookup.c \
+ ipliteral.h ipliteral.c iplookup.h iplookup.c manualroute.h \
+ manualroute.c queryprogram.h queryprogram.c redirect.h redirect.c \
+ rf_functions.h rf_change_domain.c rf_expand_data.c rf_get_errors_address.c \
+ rf_get_munge_headers.c rf_get_transport.c rf_get_ugid.c rf_queue_add.c \
+ rf_lookup_hostlist.c rf_self_action.c rf_set_ugid.c
+do
+ ln -s ../../src/routers/$f $f
+done
+
+cd ..
+
+# Likewise for the code for the transports
+mkdir transports
+cd transports
+for f in README Makefile appendfile.h appendfile.c autoreply.h \
+ autoreply.c lmtp.h lmtp.c pipe.h pipe.c queuefile.c queuefile.h \
+ smtp.h smtp.c smtp_socks.c tf_maildir.c tf_maildir.h
+do
+ ln -s ../../src/transports/$f $f
+done
+
+cd ..
+
+# Likewise for the code for the authentication drivers
+mkdir auths
+cd auths
+for f in README Makefile call_pam.c call_pwcheck.c \
+ call_radius.c check_serv_cond.c cyrus_sasl.c cyrus_sasl.h gsasl_exim.c \
+ gsasl_exim.h get_data.c get_no64_data.c heimdal_gssapi.c heimdal_gssapi.h \
+ xtextencode.c xtextdecode.c cram_md5.c cram_md5.h plaintext.c plaintext.h \
+ pwcheck.c pwcheck.h auth-spa.c auth-spa.h dovecot.c dovecot.h sha1.c spa.c \
+ spa.h tls.c tls.h external.c external.h
+do
+ ln -s ../../src/auths/$f $f
+done
+cd ..
+
+# Likewise for the code for the PDKIM library
+mkdir pdkim
+cd pdkim
+for f in README Makefile crypt_ver.h pdkim.c \
+ pdkim.h hash.c hash.h signing.c signing.h blob.h
+do
+ ln -s ../../src/pdkim/$f $f
+done
+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.
+
+for f in blob.h dbfunctions.h exim.h functions.h globals.h \
+ hash.h hintsdb.h hintsdb_structs.h local_scan.h \
+ macros.h mytypes.h osfunctions.h store.h structs.h lookupapi.h sha_ver.h \
+ \
+ acl.c buildconfig.c base64.c child.c crypt16.c daemon.c dbfn.c debug.c \
+ deliver.c directory.c dns.c dnsbl.c drtables.c dummies.c enq.c exim.c \
+ exim_dbmbuild.c exim_dbutil.c exim_lock.c expand.c filter.c filtertest.c \
+ globals.c hash.c header.c host.c ip.c log.c lss.c match.c md5.c moan.c \
+ parse.c perl.c priv.c queue.c rda.c readconf.c receive.c retry.c rewrite.c \
+ rfc2047.c route.c search.c setenv.c environment.c \
+ sieve.c smtp_in.c smtp_out.c spool_in.c spool_out.c std-crypto.c store.c \
+ string.c tls.c tlscert-gnu.c tlscert-openssl.c tls-cipher-stdname.c \
+ tls-gnu.c tls-openssl.c \
+ tod.c transport.c tree.c verify.c version.c \
+ dkim.c dkim.h dkim_transport.c dmarc.c dmarc.h \
+ valgrind.h memcheck.h \
+ macro_predef.c macro_predef.h
+do
+ ln -s ../src/$f $f
+done
+
+# WITH_CONTENT_SCAN
+for f in spam.c spam.h spool_mbox.c regex.c mime.c mime.h malware.c
+do
+ ln -s ../src/$f $f
+done
+
+# EXPERIMENTAL_*
+for f in arc.c bmi_spam.c bmi_spam.h dcc.c dcc.h dane.c dane-openssl.c \
+ danessl.h imap_utf7.c spf.c spf.h srs.c srs.h utf8.c
+do
+ ln -s ../src/$f $f
+done
+
+
+# End of MakeLinks
diff --git a/scripts/arch-type b/scripts/arch-type
new file mode 100755
index 0000000..f5fe8eb
--- /dev/null
+++ b/scripts/arch-type
@@ -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
index 0000000..1e88050
--- /dev/null
+++ b/scripts/exim_install
@@ -0,0 +1,472 @@
+#! /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
+ exim="./exim -bV -C /dev/null"
+ version=exim-`$exim 2>/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 ***"
+ $exim
+ 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
index 0000000..ed8b0cb
--- /dev/null
+++ b/scripts/lookups-Makefile
@@ -0,0 +1,194 @@
+#! /bin/sh
+
+# Copyright (c) The Exim Maintainers 1995 - 2021
+
+# 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 JSON LMDB LSEARCH MYSQL NIS NISPLUS ORACLE \
+ PASSWD PGSQL REDIS 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"
+
+# readsock is always wanted as it implements the ${readsock } expansion
+OBJ="${OBJ} readsock.o"
+
+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
index 0000000..24c09e8
--- /dev/null
+++ b/scripts/newer
@@ -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
index 0000000..a188c4a
--- /dev/null
+++ b/scripts/os-type
@@ -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
index 0000000..b932224
--- /dev/null
+++ b/scripts/reversion
@@ -0,0 +1,134 @@
+#!/bin/sh
+# Copyright (c) The Exim Maintainers 1995 - 2021
+
+set -e
+LC_ALL=C
+export LC_ALL
+
+# 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.
+
+# Override, used for automated testing w/o access to the
+# .git directory (w.g. inside a git worktree)
+if [ -n "$EXIM_RELEASE_VERSION" ]; then
+ :
+elif [ -f ./version.sh ]; then
+ . ./version.sh
+elif [ -f ../src/version.sh ]; then
+ . ../src/version.sh
+elif [ -d ../../.git ] || [ -f ../../.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.
+ if [ "$2" ]
+ then
+ description=$(git describe "$2")
+ else
+ description=$(git describe --dirty=-XX --match 'exim-4*')
+ fi
+ set $(echo "$description" | sed '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 [ -z "$EXIM_RELEASE_VERSION" ]; then
+ echo "Cannot determine the release number" >&2
+ echo "You may want to override it with EXIM_RELEASE_VERSION" >&2
+ exit 1
+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.
+
+if test -z "$EXIM_RELEASE_VERSION"; then
+ echo "$0: Your copy of Exim lacks any version information." >&2
+ exit 1
+fi
+
+EXIM_COMPILE_NUMBER=$(expr "${EXIM_COMPILE_NUMBER:-0}" + 1)
+
+echo "$EXIM_COMPILE_NUMBER" >cnumber.h
+
+# Reproducible builds, accept a build timestamp override from environ per
+# <https://reproducible-builds.org/specs/source-date-epoch/>.
+# We require a fairly modern date(1) command here, which is not portable
+# to some of the systems Exim is built on. That's okay, because the scenarios
+# are:
+# 1) Local postmaster building, not using $SOURCE_DATE_EPOCH, doesn't matter
+# 2) Packaging folks who don't care about reproducible builds
+# 3) Packaging folks who care but are using systems where date Just Works
+# 3) Packaging folks who care and can put a modern date(1) in $PATH
+# 4) Packaging folks who care and can supply us with a clean patch to support
+# their requirements
+# 5) Packaging folks who care but won't do any work to support their strange
+# old systems and want us to do the work for them. We don't care either,
+# they're SOL and have to live without reproducible builds.
+#
+exim_build_date_override=''
+if [ ".${SOURCE_DATE_EPOCH:-}" != "." ]; then
+ fmt='+%d-%b-%Y %H:%M:%S'
+ # Non-reproducible, we use __DATE__ and __TIME__ in C, which respect timezone
+ # (think localtime, not gmtime); for reproduction between systems, UTC makes
+ # more sense and the examples available use UTC without explicitly mandating
+ # it. I think that we can switch behavior and use UTC for reproducible
+ # builds without it causing any problems: nothing really cares about timezone.
+ # GNU date: "date -d @TS"
+ # BSD date: "date -r TS"
+ exim_build_date_override="$(date -u -d "@${SOURCE_DATE_EPOCH}" "$fmt" 2>/dev/null || date -u -r "${SOURCE_DATE_EPOCH}" "$fmt" 2>/dev/null)"
+fi
+
+( echo '# automatically generated file - see ../scripts/reversion'
+ echo EXIM_RELEASE_VERSION='"'"$EXIM_RELEASE_VERSION"'"'
+ test -n "$EXIM_VARIANT_VERSION" && \
+ echo EXIM_VARIANT_VERSION='"'"$EXIM_VARIANT_VERSION"'"'
+ echo EXIM_COMPILE_NUMBER='"'"$EXIM_COMPILE_NUMBER"'"'
+ if [ ".${exim_build_date_override:-}" != "." ]; then
+ echo EXIM_BUILD_DATE_OVERRIDE='"'"${exim_build_date_override}"'"'
+ fi
+) >version.sh
+
+if [ ! -f version.h ]
+then
+( echo '/* automatically generated file - see ../scripts/reversion */'
+ echo '#define EXIM_RELEASE_VERSION "'"$EXIM_RELEASE_VERSION"'"'
+ test -n "$EXIM_VARIANT_VERSION" && \
+ echo '#define EXIM_VARIANT_VERSION "'"$EXIM_VARIANT_VERSION"'"'
+ echo '#ifdef EXIM_VARIANT_VERSION'
+ echo '#define EXIM_VERSION_STR EXIM_RELEASE_VERSION "-" EXIM_VARIANT_VERSION'
+ echo '#else'
+ echo '#define EXIM_VERSION_STR EXIM_RELEASE_VERSION'
+ echo '#endif'
+ if [ ".${exim_build_date_override:-}" != "." ]; then
+ echo '#define EXIM_BUILD_DATE_OVERRIDE "'"${exim_build_date_override}"'"'
+ fi
+) >version.h
+fi
+
+#test -t 1 && echo ">>> version $EXIM_RELEASE_VERSION $EXIM_VARIANT_VERSION #$EXIM_COMPILE_NUMBER"
diff --git a/scripts/source_checks b/scripts/source_checks
new file mode 100644
index 0000000..918a6f8
--- /dev/null
+++ b/scripts/source_checks
@@ -0,0 +1,52 @@
+#!/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
+ std-crypto.c dh_constants
+ 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
+ acl.c conditions
+ acl.c controls_list
+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
+END
+
diff --git a/src/EDITME b/src/EDITME
new file mode 100644
index 0000000..53022e5
--- /dev/null
+++ b/src/EDITME
@@ -0,0 +1,1497 @@
+##################################################
+# 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 OS/Makefile-<osname>. You can further override these settings by
+# creating files Local/Makefile-<osname>, and Local/Makefile-<build>.
+# The suffix "<osname>" stands for the name of your operating system - look
+# at the names in the OS directory to see which names are recognized,
+# and "<build>" is the content of the environment variable "build".
+
+# 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
+
+
+
+###############################################################################
+# TLS #
+###############################################################################
+# Exim is built by default to support the SMTP STARTTLS command, which implements
+# Transport Layer Security using SSL (Secure Sockets Layer). This requires you
+# must install the OpenSSL library package or the GnuTLS library. Exim contains
+# no cryptographic code of its own.
+
+# If you are running Exim as a (TLS) server, just building it with TLS support
+# is all you need to do, as tls_advertise_hosts is set to '*' by
+# default. But you are advised to create a suiteable certificate, and tell
+# Exim about it by means of the tls_certificate and tls_privatekey run
+# time options, otherwise Exim will create a self signed certificate on
+# the fly. If you are running Exim only as a (TLS) 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.
+# Enabling the USE_*_PC options should be sufficient. If not using
+# pkg-config, then you have to specify the libraries, and you might
+# need to specify the locations too.
+
+# Uncomment the following lines if you want
+# to build Exim without any TLS support (either OpenSSL or GnuTLS):
+# DISABLE_TLS=yes
+# Unless you do this, you must define one of USE_OPENSSL or USE_GNUTLS
+# below.
+
+# If you are building with TLS, the library configuration must be done:
+
+# Uncomment this if you are using OpenSSL
+# USE_OPENSSL=yes
+# Uncomment one of these settings if you are using OpenSSL; pkg-config vs not
+# and an optional location.
+# USE_OPENSSL_PC=openssl
+# TLS_LIBS=-lssl -lcrypto
+# TLS_LIBS=-L/usr/local/openssl/lib -lssl -lcrypto
+
+# Uncomment this if you are using GnuTLS
+# USE_GNUTLS=yes
+# Uncomment one of these settings if you are using GnuTLS; pkg-config vs not
+# and an optional location. If you disable SUPPORT_DANE below, you
+# can remove the gnutls-dane references here. Earlier versions of GnuTLS
+# required libtasn1 and libgrypt also; add if needed.
+# USE_GNUTLS_PC=gnutls gnutls-dane
+# TLS_LIBS=-lgnutls -lgnutls-dane
+# TLS_LIBS=-L/usr/local/gnu/lib -lgnutls -ltasn1 -lgcrypt -lgnutls-dane
+
+# If using GnuTLS older than 2.10 and using pkg-config then note that Exim's
+# build process will require libgcrypt-config to exist in your $PATH. A
+# version that old is likely to become unsupported by Exim in 2017.
+
+# 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 -lgnutls-dane
+# If not using DANE under GnuTLS we can lose one library
+# 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.
+
+
+# Uncomment the following line to remove support for TLS Resumption
+# DISABLE_TLS_RESUME=yes
+
+
+###############################################################################
+# 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.
+
+# If you need to override how pkg-config finds configuration files for
+# installed software, then you can set that here; wildcards will be expanded.
+
+# PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig : /opt/*/lib/pkgconfig
+
+
+#------------------------------------------------------------------------------
+# 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!
+#
+# For Redis 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
+# (often += -I/usr/local/include) and LDFLAGS (-lhiredis) lines.
+
+# 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_JSON=yes
+# LOOKUP_LDAP=yes
+# LOOKUP_LMDB=yes
+
+# LOOKUP_MYSQL=yes
+# LOOKUP_MYSQL_PC=mariadb
+# LOOKUP_NIS=yes
+# LOOKUP_NISPLUS=yes
+# LOOKUP_ORACLE=yes
+# LOOKUP_PASSWD=yes
+# LOOKUP_PGSQL=yes
+# LOOKUP_REDIS=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
+
+
+# Some platforms may need this for LOOKUP_NIS:
+# LIBS += -lnsl
+
+#------------------------------------------------------------------------------
+# 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 PCRE2 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 PCRE2.
+# In either case you must specify the library link info here. If the
+# PCRE2 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.
+
+PCRE2_CONFIG=yes
+# PCRE_LIBS=-lpcre2
+
+
+#------------------------------------------------------------------------------
+# Comment out the following line to remove DANE support
+# Note: Enabling this unconditionally overrides DISABLE_DNSSEC
+# forces you to have SUPPORT_TLS enabled (the default). For DANE under
+# GnuTLS we need an additional library. See TLS_LIBS or USE_GNUTLS_PC
+# below.
+SUPPORT_DANE=yes
+
+#------------------------------------------------------------------------------
+# 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, -lhiredis
+# is for Redis, -ljansson for JSON.
+#
+# 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_INCLUDE +=-I /usr/local/include
+# LOOKUP_LIBS=-L/usr/local/lib -lldap -llber -lmysqlclient -lpq -lgds -lsqlite3 -llmdb
+
+#------------------------------------------------------------------------------
+# If you included LOOKUP_LMDB above you will need the library. Depending
+# on where installed you may also need an include directory
+#
+# LOOKUP_INCLUDE += -I/usr/local/include
+# LOOKUP_LIBS += -llmdb
+
+
+#------------------------------------------------------------------------------
+# 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". De-comment this setting to enable 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 have content scanning you may wish to only include some of the scanner
+# interfaces. Uncomment any of these lines to remove that code.
+
+# DISABLE_MAL_FFROTD=yes
+# DISABLE_MAL_FFROT6D=yes
+# DISABLE_MAL_DRWEB=yes
+# DISABLE_MAL_FSECURE=yes
+# DISABLE_MAL_SOPHIE=yes
+# DISABLE_MAL_CLAM=yes
+# DISABLE_MAL_AVAST=yes
+# DISABLE_MAL_SOCK=yes
+# DISABLE_MAL_CMDLINE=yes
+
+# These scanners are claimed to be no longer existent.
+
+DISABLE_MAL_AVE=yes
+DISABLE_MAL_KAV=yes
+DISABLE_MAL_MKS=yes
+
+
+#------------------------------------------------------------------------------
+# If built with TLS, 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"
+# Note: Enabling SUPPORT_DANE unconditionally overrides this setting.
+
+# DISABLE_DNSSEC=yes
+
+# To disable support for Events set DISABLE_EVENT to "yes"
+# DISABLE_EVENT=yes
+
+
+# Uncomment this line to remove support for early pipelining, per
+# https://datatracker.ietf.org/doc/draft-harris-early-pipe/
+# DISABLE_PIPE_CONNECT=yes
+
+
+# Uncomment the following to remove the fast-ramp two-phase-queue-run support
+# DISABLE_QUEUE_RAMP=yes
+
+# Uncomment the following lines to add SRS (Sender Rewriting Scheme) support
+# using only native facilities.
+# SUPPORT_SRS=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.
+# Doing so will also explicitly turn on the WITH_CONTENT_SCAN option.
+
+# EXPERIMENTAL_DCC=yes
+
+# Uncomment the following line to add DMARC checking capability, implemented
+# using libopendmarc libraries. You must have SPF and DKIM support enabled also.
+# Library version libopendmarc-1.4.1-1.fc33.x86_64 (on Fedora 33) is known broken;
+# 1.3.2-3 works. I seems that the OpenDMARC project broke their API.
+# SUPPORT_DMARC=yes
+# CFLAGS += -I/usr/local/include
+# LDFLAGS += -lopendmarc
+# Uncomment the following if you need to change the default. You can
+# override it at runtime (main config option dmarc_tld_file)
+# DMARC_TLD_FILE=/etc/exim/opendmarc.tlds
+
+# Uncomment the following line to add ARC (Authenticated Received Chain)
+# support. You must have SPF and DKIM support enabled also.
+# EXPERIMENTAL_ARC=yes
+
+# 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 to include extra information in fail DSN message (bounces)
+# EXPERIMENTAL_DSN_INFO=yes
+
+# Uncomment the following line to add queuefile transport support
+# EXPERIMENTAL_QUEUEFILE=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 defines 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_EXTERNAL=yes
+# AUTH_GSASL=yes
+# AUTH_GSASL_PC=libgsasl
+# AUTH_HEIMDAL_GSSAPI=yes
+# AUTH_HEIMDAL_GSSAPI_PC=heimdal-gssapi
+# AUTH_HEIMDAL_GSSAPI_PC=heimdal-gssapi heimdal-krb5
+# AUTH_PLAINTEXT=yes
+# AUTH_SPA=yes
+# AUTH_TLS=yes
+
+# Heimdal through 1.5 required pkg-config 'heimdal-gssapi'; Heimdal 7.1
+# requires multiple pkg-config files to work with Exim, so the second example
+# above is needed.
+
+#------------------------------------------------------------------------------
+# 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
+
+# If using AUTH_GSASL with SCRAM methods, you should also be defining
+# SUPPORT_I18N to get standards-conformant support of utf8 normalization.
+
+
+#------------------------------------------------------------------------------
+# 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.
+#
+# nb: FreeBSD as of 4.89 defines LIBICONV_PLUG to pick up the system iconv
+# more reliably. If you explicitly want the libiconv Port then as well
+# as adding -liconv you'll want to unset LIBICONV_PLUG. If you actually need
+# this, let us know, but for now the Exim Maintainers are assuming that this
+# is uncommon and so you'll need to edit OS/os.h-FreeBSD yourself to remove
+# the define.
+
+
+#------------------------------------------------------------------------------
+# 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 ***
+
+
+#------------------------------------------------------------------------------
+# 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 unpacks 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.
+
+# Leave it empty to enforce autodetection at runtime:
+# ZCAT_COMMAND=
+#
+# Omit the path if you want to use your system's PATH:
+# ZCAT_COMMAND=zcat
+#
+# Or specify the full pathname:
+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.
+
+
+#------------------------------------------------------------------------------
+# Proxying.
+#
+# If you may want to use outbound (client-side) proxying, using Socks5,
+# uncomment the line below.
+
+# SUPPORT_SOCKS=yes
+
+# If you may want to use inbound (server-side) proxying, using Proxy Protocol,
+# uncomment the line below.
+
+# SUPPORT_PROXY=yes
+
+
+#------------------------------------------------------------------------------
+# Internationalisation.
+#
+# Uncomment the following to include Internationalisation features. This is the
+# SMTPUTF8 ESMTP extension, and associated facilities for handling UTF8 domain
+# and localparts, per RFC 3490 (IDNA2003).
+# You need to have the IDN library installed.
+# If you want IDNA2008 mappings per RFCs 5890, 6530 and 6533, you additionally
+# need libidn2 and SUPPORT_I18N_2008.
+
+# SUPPORT_I18N=yes
+# LDFLAGS += -lidn
+# SUPPORT_I18N_2008=yes
+# LDFLAGS += -lidn -lidn2
+
+
+#------------------------------------------------------------------------------
+# 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.
+
+# SUPPORT_SPF=yes
+# CFLAGS += -I/usr/local/include
+# LDFLAGS += -lspf2
+
+
+#------------------------------------------------------------------------------
+# 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 intended 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.
+
+# If libreadline is not in the normal library paths, then because Exim is
+# setuid you'll need to ensure that the correct directory is stamped into
+# the binary so that dlopen will find it.
+# Eg, on macOS/Darwin with a third-party install of libreadline, perhaps:
+
+# EXTRALIBS_EXIM+=-Wl,-rpath,/usr/local/opt/readline/lib
+
+
+#------------------------------------------------------------------------------
+# 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. Older installations call this macro
+# just TMPDIR, but this has side effects at build time. At runtime
+# TMPDIR is checked as before.
+
+EXIM_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 example, /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 parameters: 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
+
+#------------------------------------------------------------------------------
+# For development, add this to include code to time various stages and report.
+# CFLAGS += -DMEASURE_TIMING
+
+# For a very slightly smaller build, for constrained systems, uncomment this.
+# The feature involved is purely for debugging.
+
+# DISABLE_CLIENT_CMD_LOG=yes
+
+# End of EDITME for Exim 4.
diff --git a/src/acl.c b/src/acl.c
new file mode 100644
index 0000000..fb78a7b
--- /dev/null
+++ b/src/acl.c
@@ -0,0 +1,4896 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Code for handling Access Control Lists (ACLs) */
+
+#include "exim.h"
+
+#ifndef MACRO_PREDEF
+
+/* Default callout timeout */
+
+#define CALLOUT_TIMEOUT_DEFAULT 30
+
+/* Default quota cache TTLs */
+
+#define QUOTA_POS_DEFAULT (5*60)
+#define QUOTA_NEG_DEFAULT (60*60)
+
+
+/* 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[] = {
+ [ACL_ACCEPT] = US"accept",
+ [ACL_DEFER] = US"defer",
+ [ACL_DENY] = US"deny",
+ [ACL_DISCARD] = US"discard",
+ [ACL_DROP] = US"drop",
+ [ACL_REQUIRE] = US"require",
+ [ACL_WARN] = 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[] = {
+ [ACL_ACCEPT] = BIT(OK) | BIT(FAIL) | BIT(FAIL_DROP),
+ [ACL_DEFER] = BIT(OK),
+ [ACL_DENY] = BIT(OK),
+ [ACL_DISCARD] = BIT(OK) | BIT(FAIL) | BIT(FAIL_DROP),
+ [ACL_DROP] = BIT(OK),
+ [ACL_REQUIRE] = BIT(FAIL) | BIT(FAIL_DROP),
+ [ACL_WARN] = BIT(OK)
+ };
+
+#endif
+
+/* ACL condition and modifier codes - keep in step with the table that
+follows.
+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,
+#ifndef DISABLE_DKIM
+ ACLC_DKIM_SIGNER,
+ ACLC_DKIM_STATUS,
+#endif
+#ifdef SUPPORT_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_QUEUE,
+ ACLC_RATELIMIT,
+ ACLC_RECIPIENTS,
+#ifdef WITH_CONTENT_SCAN
+ ACLC_REGEX,
+#endif
+ ACLC_REMOVE_HEADER,
+ ACLC_SEEN,
+ ACLC_SENDER_DOMAINS,
+ ACLC_SENDERS,
+ ACLC_SET,
+#ifdef WITH_CONTENT_SCAN
+ ACLC_SPAM,
+#endif
+#ifdef SUPPORT_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", "queue" and "set" are
+modifiers that look like conditions but always return TRUE. They are used for
+their side effects. Do not invent new modifier names that result in one name
+being the prefix of another; the binary-search in the list will go wrong. */
+
+typedef struct condition_def {
+ uschar *name;
+
+/* Flag to indicate the condition/modifier has a string expansion done
+at the outer level. In the other cases, expansion already occurs in the
+checking functions. */
+ BOOL expand_at_top:1;
+
+ BOOL is_modifier:1;
+
+/* 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. */
+ unsigned forbids;
+
+} condition_def;
+
+static condition_def conditions[] = {
+ [ACLC_ACL] = { US"acl", FALSE, FALSE, 0 },
+
+ [ACLC_ADD_HEADER] = { US"add_header", TRUE, TRUE,
+ (unsigned int)
+ ~(ACL_BIT_MAIL | ACL_BIT_RCPT |
+ ACL_BIT_PREDATA | ACL_BIT_DATA |
+#ifndef DISABLE_PRDR
+ ACL_BIT_PRDR |
+#endif
+ ACL_BIT_MIME | ACL_BIT_NOTSMTP |
+ ACL_BIT_DKIM |
+ ACL_BIT_NOTSMTP_START),
+ },
+
+ [ACLC_AUTHENTICATED] = { US"authenticated", FALSE, FALSE,
+ ACL_BIT_NOTSMTP | ACL_BIT_NOTSMTP_START |
+ ACL_BIT_CONNECT | ACL_BIT_HELO,
+ },
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+ [ACLC_BMI_OPTIN] = { US"bmi_optin", TRUE, TRUE,
+ ACL_BIT_AUTH |
+ ACL_BIT_CONNECT | ACL_BIT_HELO |
+ ACL_BIT_DATA | ACL_BIT_MIME |
+# ifndef DISABLE_PRDR
+ ACL_BIT_PRDR |
+# endif
+ ACL_BIT_ETRN | ACL_BIT_EXPN |
+ ACL_BIT_MAILAUTH |
+ ACL_BIT_MAIL | ACL_BIT_STARTTLS |
+ ACL_BIT_VRFY | ACL_BIT_PREDATA |
+ ACL_BIT_NOTSMTP_START,
+ },
+#endif
+ [ACLC_CONDITION] = { US"condition", TRUE, FALSE, 0 },
+ [ACLC_CONTINUE] = { US"continue", TRUE, TRUE, 0 },
+
+ /* Certain types of control are always allowed, so we let it through
+ always and check in the control processing itself. */
+ [ACLC_CONTROL] = { US"control", TRUE, TRUE, 0 },
+
+#ifdef EXPERIMENTAL_DCC
+ [ACLC_DCC] = { US"dcc", TRUE, FALSE,
+ (unsigned int)
+ ~(ACL_BIT_DATA |
+# ifndef DISABLE_PRDR
+ ACL_BIT_PRDR |
+# endif
+ ACL_BIT_NOTSMTP),
+ },
+#endif
+#ifdef WITH_CONTENT_SCAN
+ [ACLC_DECODE] = { US"decode", TRUE, FALSE, (unsigned int) ~ACL_BIT_MIME },
+
+#endif
+ [ACLC_DELAY] = { US"delay", TRUE, TRUE, ACL_BIT_NOTQUIT },
+#ifndef DISABLE_DKIM
+ [ACLC_DKIM_SIGNER] = { US"dkim_signers", TRUE, FALSE, (unsigned int) ~ACL_BIT_DKIM },
+ [ACLC_DKIM_STATUS] = { US"dkim_status", TRUE, FALSE, (unsigned int) ~ACL_BIT_DKIM },
+#endif
+#ifdef SUPPORT_DMARC
+ [ACLC_DMARC_STATUS] = { US"dmarc_status", TRUE, FALSE, (unsigned int) ~ACL_BIT_DATA },
+#endif
+
+ /* Explicit key lookups can be made in non-smtp ACLs so pass
+ always and check in the verify processing itself. */
+ [ACLC_DNSLISTS] = { US"dnslists", TRUE, FALSE, 0 },
+
+ [ACLC_DOMAINS] = { US"domains", FALSE, FALSE,
+ (unsigned int)
+ ~(ACL_BIT_RCPT | ACL_BIT_VRFY
+#ifndef DISABLE_PRDR
+ |ACL_BIT_PRDR
+#endif
+ ),
+ },
+ [ACLC_ENCRYPTED] = { US"encrypted", FALSE, FALSE,
+ ACL_BIT_NOTSMTP | ACL_BIT_NOTSMTP_START |
+ ACL_BIT_HELO,
+ },
+
+ [ACLC_ENDPASS] = { US"endpass", TRUE, TRUE, 0 },
+
+ [ACLC_HOSTS] = { US"hosts", FALSE, FALSE,
+ ACL_BIT_NOTSMTP | ACL_BIT_NOTSMTP_START,
+ },
+ [ACLC_LOCAL_PARTS] = { US"local_parts", FALSE, FALSE,
+ (unsigned int)
+ ~(ACL_BIT_RCPT | ACL_BIT_VRFY
+#ifndef DISABLE_PRDR
+ | ACL_BIT_PRDR
+#endif
+ ),
+ },
+
+ [ACLC_LOG_MESSAGE] = { US"log_message", TRUE, TRUE, 0 },
+ [ACLC_LOG_REJECT_TARGET] = { US"log_reject_target", TRUE, TRUE, 0 },
+ [ACLC_LOGWRITE] = { US"logwrite", TRUE, TRUE, 0 },
+
+#ifdef WITH_CONTENT_SCAN
+ [ACLC_MALWARE] = { US"malware", TRUE, FALSE,
+ (unsigned int)
+ ~(ACL_BIT_DATA |
+# ifndef DISABLE_PRDR
+ ACL_BIT_PRDR |
+# endif
+ ACL_BIT_NOTSMTP),
+ },
+#endif
+
+ [ACLC_MESSAGE] = { US"message", TRUE, TRUE, 0 },
+#ifdef WITH_CONTENT_SCAN
+ [ACLC_MIME_REGEX] = { US"mime_regex", TRUE, FALSE, (unsigned int) ~ACL_BIT_MIME },
+#endif
+
+ [ACLC_QUEUE] = { US"queue", TRUE, TRUE,
+ ACL_BIT_NOTSMTP |
+#ifndef DISABLE_PRDR
+ ACL_BIT_PRDR |
+#endif
+ ACL_BIT_DATA,
+ },
+
+ [ACLC_RATELIMIT] = { US"ratelimit", TRUE, FALSE, 0 },
+ [ACLC_RECIPIENTS] = { US"recipients", FALSE, FALSE, (unsigned int) ~ACL_BIT_RCPT },
+
+#ifdef WITH_CONTENT_SCAN
+ [ACLC_REGEX] = { US"regex", TRUE, FALSE,
+ (unsigned int)
+ ~(ACL_BIT_DATA |
+# ifndef DISABLE_PRDR
+ ACL_BIT_PRDR |
+# endif
+ ACL_BIT_NOTSMTP |
+ ACL_BIT_MIME),
+ },
+
+#endif
+ [ACLC_REMOVE_HEADER] = { US"remove_header", TRUE, TRUE,
+ (unsigned int)
+ ~(ACL_BIT_MAIL|ACL_BIT_RCPT |
+ ACL_BIT_PREDATA | ACL_BIT_DATA |
+#ifndef DISABLE_PRDR
+ ACL_BIT_PRDR |
+#endif
+ ACL_BIT_MIME | ACL_BIT_NOTSMTP |
+ ACL_BIT_NOTSMTP_START),
+ },
+ [ACLC_SEEN] = { US"seen", TRUE, FALSE, 0 },
+ [ACLC_SENDER_DOMAINS] = { US"sender_domains", FALSE, FALSE,
+ ACL_BIT_AUTH | ACL_BIT_CONNECT |
+ ACL_BIT_HELO |
+ ACL_BIT_MAILAUTH | ACL_BIT_QUIT |
+ ACL_BIT_ETRN | ACL_BIT_EXPN |
+ ACL_BIT_STARTTLS | ACL_BIT_VRFY,
+ },
+ [ACLC_SENDERS] = { US"senders", FALSE, FALSE,
+ ACL_BIT_AUTH | ACL_BIT_CONNECT |
+ ACL_BIT_HELO |
+ ACL_BIT_MAILAUTH | ACL_BIT_QUIT |
+ ACL_BIT_ETRN | ACL_BIT_EXPN |
+ ACL_BIT_STARTTLS | ACL_BIT_VRFY,
+ },
+
+ [ACLC_SET] = { US"set", TRUE, TRUE, 0 },
+
+#ifdef WITH_CONTENT_SCAN
+ [ACLC_SPAM] = { US"spam", TRUE, FALSE,
+ (unsigned int) ~(ACL_BIT_DATA |
+# ifndef DISABLE_PRDR
+ ACL_BIT_PRDR |
+# endif
+ ACL_BIT_NOTSMTP),
+ },
+#endif
+#ifdef SUPPORT_SPF
+ [ACLC_SPF] = { US"spf", TRUE, FALSE,
+ ACL_BIT_AUTH | ACL_BIT_CONNECT |
+ ACL_BIT_HELO | ACL_BIT_MAILAUTH |
+ ACL_BIT_ETRN | ACL_BIT_EXPN |
+ ACL_BIT_STARTTLS | ACL_BIT_VRFY |
+ ACL_BIT_NOTSMTP | ACL_BIT_NOTSMTP_START,
+ },
+ [ACLC_SPF_GUESS] = { US"spf_guess", TRUE, FALSE,
+ ACL_BIT_AUTH | ACL_BIT_CONNECT |
+ ACL_BIT_HELO | ACL_BIT_MAILAUTH |
+ ACL_BIT_ETRN | ACL_BIT_EXPN |
+ ACL_BIT_STARTTLS | ACL_BIT_VRFY |
+ ACL_BIT_NOTSMTP | ACL_BIT_NOTSMTP_START,
+ },
+#endif
+ [ACLC_UDPSEND] = { US"udpsend", TRUE, TRUE, 0 },
+
+ /* Certain types of verify are always allowed, so we let it through
+ always and check in the verify function itself */
+ [ACLC_VERIFY] = { US"verify", TRUE, FALSE, 0 },
+};
+
+
+#ifdef MACRO_PREDEF
+# include "macro_predef.h"
+void
+features_acl(void)
+{
+for (condition_def * c = conditions; c < conditions + nelem(conditions); c++)
+ {
+ uschar buf[64], * p, * s;
+ int n = sprintf(CS buf, "_ACL_%s_", c->is_modifier ? "MOD" : "COND");
+ for (p = buf + n, s = c->name; *s; s++) *p++ = toupper(*s);
+ *p = '\0';
+ builtin_macro_create(buf);
+ }
+}
+#endif
+
+
+#ifndef MACRO_PREDEF
+
+/* Return values from decode_control(); used as index so keep in step
+with the controls_list table that follows! */
+
+enum {
+ CONTROL_AUTH_UNADVERTISED,
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+ CONTROL_BMI_RUN,
+#endif
+ CONTROL_CASEFUL_LOCAL_PART,
+ CONTROL_CASELOWER_LOCAL_PART,
+ CONTROL_CUTTHROUGH_DELIVERY,
+ CONTROL_DEBUG,
+#ifndef DISABLE_DKIM
+ CONTROL_DKIM_VERIFY,
+#endif
+#ifdef SUPPORT_DMARC
+ CONTROL_DMARC_VERIFY,
+ CONTROL_DMARC_FORENSIC,
+#endif
+ CONTROL_DSCP,
+ CONTROL_ENFORCE_SYNC,
+ CONTROL_ERROR, /* pseudo-value for decode errors */
+ CONTROL_FAKEDEFER,
+ CONTROL_FAKEREJECT,
+ CONTROL_FREEZE,
+
+ CONTROL_NO_CALLOUT_FLUSH,
+ CONTROL_NO_DELAY_FLUSH,
+ CONTROL_NO_ENFORCE_SYNC,
+#ifdef WITH_CONTENT_SCAN
+ CONTROL_NO_MBOX_UNSPOOL,
+#endif
+ CONTROL_NO_MULTILINE,
+ CONTROL_NO_PIPELINING,
+
+ CONTROL_QUEUE,
+ CONTROL_SUBMISSION,
+ CONTROL_SUPPRESS_LOCAL_FIXUPS,
+#ifdef SUPPORT_I18N
+ CONTROL_UTF8_DOWNCONVERT,
+#endif
+};
+
+
+
+/* Structure listing various control arguments, with their characteristics.
+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. */
+
+typedef struct control_def {
+ uschar *name;
+ BOOL has_option; /* Has /option(s) following */
+ unsigned forbids; /* bitmap of dis-allowed times */
+} control_def;
+
+static control_def controls_list[] = {
+ /* name has_option forbids */
+[CONTROL_AUTH_UNADVERTISED] =
+ { US"allow_auth_unadvertised", FALSE,
+ (unsigned)
+ ~(ACL_BIT_CONNECT | ACL_BIT_HELO)
+ },
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+[CONTROL_BMI_RUN] =
+ { US"bmi_run", FALSE, 0 },
+#endif
+[CONTROL_CASEFUL_LOCAL_PART] =
+ { US"caseful_local_part", FALSE, (unsigned) ~ACL_BIT_RCPT },
+[CONTROL_CASELOWER_LOCAL_PART] =
+ { US"caselower_local_part", FALSE, (unsigned) ~ACL_BIT_RCPT },
+[CONTROL_CUTTHROUGH_DELIVERY] =
+ { US"cutthrough_delivery", TRUE, 0 },
+[CONTROL_DEBUG] =
+ { US"debug", TRUE, 0 },
+
+#ifndef DISABLE_DKIM
+[CONTROL_DKIM_VERIFY] =
+ { US"dkim_disable_verify", FALSE,
+ ACL_BIT_DATA | ACL_BIT_NOTSMTP |
+# ifndef DISABLE_PRDR
+ ACL_BIT_PRDR |
+# endif
+ ACL_BIT_NOTSMTP_START
+ },
+#endif
+
+#ifdef SUPPORT_DMARC
+[CONTROL_DMARC_VERIFY] =
+ { US"dmarc_disable_verify", FALSE,
+ ACL_BIT_DATA | ACL_BIT_NOTSMTP | ACL_BIT_NOTSMTP_START
+ },
+[CONTROL_DMARC_FORENSIC] =
+ { US"dmarc_enable_forensic", FALSE,
+ ACL_BIT_DATA | ACL_BIT_NOTSMTP | ACL_BIT_NOTSMTP_START
+ },
+#endif
+
+[CONTROL_DSCP] =
+ { US"dscp", TRUE,
+ ACL_BIT_NOTSMTP | ACL_BIT_NOTSMTP_START | ACL_BIT_NOTQUIT
+ },
+[CONTROL_ENFORCE_SYNC] =
+ { US"enforce_sync", FALSE,
+ ACL_BIT_NOTSMTP | ACL_BIT_NOTSMTP_START
+ },
+
+ /* Pseudo-value for decode errors */
+[CONTROL_ERROR] =
+ { US"error", FALSE, 0 },
+
+[CONTROL_FAKEDEFER] =
+ { US"fakedefer", TRUE,
+ (unsigned)
+ ~(ACL_BIT_MAIL | ACL_BIT_RCPT |
+ ACL_BIT_PREDATA | ACL_BIT_DATA |
+#ifndef DISABLE_PRDR
+ ACL_BIT_PRDR |
+#endif
+ ACL_BIT_MIME)
+ },
+[CONTROL_FAKEREJECT] =
+ { US"fakereject", TRUE,
+ (unsigned)
+ ~(ACL_BIT_MAIL | ACL_BIT_RCPT |
+ ACL_BIT_PREDATA | ACL_BIT_DATA |
+#ifndef DISABLE_PRDR
+ ACL_BIT_PRDR |
+#endif
+ ACL_BIT_MIME)
+ },
+[CONTROL_FREEZE] =
+ { US"freeze", TRUE,
+ (unsigned)
+ ~(ACL_BIT_MAIL | ACL_BIT_RCPT |
+ ACL_BIT_PREDATA | ACL_BIT_DATA |
+ // ACL_BIT_PRDR| /* Not allow one user to freeze for all */
+ ACL_BIT_NOTSMTP | ACL_BIT_MIME)
+ },
+
+[CONTROL_NO_CALLOUT_FLUSH] =
+ { US"no_callout_flush", FALSE,
+ ACL_BIT_NOTSMTP | ACL_BIT_NOTSMTP_START
+ },
+[CONTROL_NO_DELAY_FLUSH] =
+ { US"no_delay_flush", FALSE,
+ ACL_BIT_NOTSMTP | ACL_BIT_NOTSMTP_START
+ },
+
+[CONTROL_NO_ENFORCE_SYNC] =
+ { US"no_enforce_sync", FALSE,
+ ACL_BIT_NOTSMTP | ACL_BIT_NOTSMTP_START
+ },
+#ifdef WITH_CONTENT_SCAN
+[CONTROL_NO_MBOX_UNSPOOL] =
+ { US"no_mbox_unspool", FALSE,
+ (unsigned)
+ ~(ACL_BIT_MAIL | ACL_BIT_RCPT |
+ ACL_BIT_PREDATA | ACL_BIT_DATA |
+ // ACL_BIT_PRDR| /* Not allow one user to freeze for all */
+ ACL_BIT_MIME)
+ },
+#endif
+[CONTROL_NO_MULTILINE] =
+ { US"no_multiline_responses", FALSE,
+ ACL_BIT_NOTSMTP | ACL_BIT_NOTSMTP_START
+ },
+[CONTROL_NO_PIPELINING] =
+ { US"no_pipelining", FALSE,
+ ACL_BIT_NOTSMTP | ACL_BIT_NOTSMTP_START
+ },
+
+[CONTROL_QUEUE] =
+ { US"queue", TRUE,
+ (unsigned)
+ ~(ACL_BIT_MAIL | ACL_BIT_RCPT |
+ ACL_BIT_PREDATA | ACL_BIT_DATA |
+ // ACL_BIT_PRDR| /* Not allow one user to freeze for all */
+ ACL_BIT_NOTSMTP | ACL_BIT_MIME)
+ },
+
+[CONTROL_SUBMISSION] =
+ { US"submission", TRUE,
+ (unsigned)
+ ~(ACL_BIT_MAIL | ACL_BIT_RCPT | ACL_BIT_PREDATA)
+ },
+[CONTROL_SUPPRESS_LOCAL_FIXUPS] =
+ { US"suppress_local_fixups", FALSE,
+ (unsigned)
+ ~(ACL_BIT_MAIL | ACL_BIT_RCPT | ACL_BIT_PREDATA |
+ ACL_BIT_NOTSMTP_START)
+ },
+#ifdef SUPPORT_I18N
+[CONTROL_UTF8_DOWNCONVERT] =
+ { US"utf8_downconvert", TRUE, (unsigned) ~(ACL_BIT_RCPT | ACL_BIT_VRFY)
+ }
+#endif
+};
+
+/* 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[] = {
+ [CSA_UNKNOWN] = OK,
+ [CSA_OK] = OK,
+ [CSA_DEFER_SRV] = OK,
+ [CSA_DEFER_ADDR] = OK,
+ [CSA_FAIL_EXPLICIT] = FAIL,
+ [CSA_FAIL_DOMAIN] = FAIL,
+ [CSA_FAIL_NOADDR] = FAIL,
+ [CSA_FAIL_MISMATCH] = FAIL
+};
+
+static uschar *csa_status_string[] = {
+ [CSA_UNKNOWN] = US"unknown",
+ [CSA_OK] = US"ok",
+ [CSA_DEFER_SRV] = US"defer",
+ [CSA_DEFER_ADDR] = US"defer",
+ [CSA_FAIL_EXPLICIT] = US"fail",
+ [CSA_FAIL_DOMAIN] = US"fail",
+ [CSA_FAIL_NOADDR] = US"fail",
+ [CSA_FAIL_MISMATCH] = US"fail"
+};
+
+static uschar *csa_reason_string[] = {
+ [CSA_UNKNOWN] = US"unknown",
+ [CSA_OK] = US"ok",
+ [CSA_DEFER_SRV] = US"deferred (SRV lookup failed)",
+ [CSA_DEFER_ADDR] = US"deferred (target address lookup failed)",
+ [CSA_FAIL_EXPLICIT] = US"failed (explicit authorization required)",
+ [CSA_FAIL_DOMAIN] = US"failed (host name not authorized)",
+ [CSA_FAIL_NOADDR] = US"failed (no authorized addresses)",
+ [CSA_FAIL_MISMATCH] = 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[] = {
+ [RATE_PER_WHAT] = US"?",
+ [RATE_PER_CLASH] = US"!",
+ [RATE_PER_ADDR] = US"per_addr",
+ [RATE_PER_BYTE] = US"per_byte",
+ [RATE_PER_CMD] = US"per_cmd",
+ [RATE_PER_CONN] = US"per_conn",
+ [RATE_PER_MAIL] = US"per_mail",
+ [RATE_PER_RCPT] = US"per_rcpt",
+ [RATE_PER_ALLRCPTS] = US"per_rcpt"
+};
+
+/* Enable recursion between acl_check_internal() and acl_check_condition() */
+
+static int acl_check_wargs(int, address_item *, const uschar *, uschar **,
+ uschar **);
+
+static acl_block * acl_current = NULL;
+
+
+/*************************************************
+* Find control in list *
+*************************************************/
+
+/* The lists are always in order, so binary chop can be used.
+
+Arguments:
+ name the control name to search for
+ ol the first entry in the control list
+ last one more than the offset of the last entry in the control list
+
+Returns: index of a control entry, or -1 if not found
+*/
+
+static int
+find_control(const uschar * name, control_def * ol, int last)
+{
+for (int first = 0; last > first; )
+ {
+ int middle = (first + last)/2;
+ uschar * s = ol[middle].name;
+ int c = Ustrncmp(name, s, Ustrlen(s));
+ if (c == 0) return middle;
+ else if (c > 0) first = middle + 1;
+ else last = middle;
+ }
+return -1;
+}
+
+
+
+/*************************************************
+* Pick out condition from list *
+*************************************************/
+
+/* Use a binary chop method
+
+Arguments:
+ name name to find
+ list list of conditions
+ end size of list
+
+Returns: offset in list, or -1 if not found
+*/
+
+static int
+acl_checkcondition(uschar * name, condition_def * list, int end)
+{
+for (int start = 0; start < end; )
+ {
+ int mid = (start + end)/2;
+ int c = Ustrcmp(name, list[mid].name);
+ if (c == 0) return mid;
+ if (c < 0) end = mid;
+ else start = mid + 1;
+ }
+return -1;
+}
+
+
+/*************************************************
+* 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)
+{
+for (int start = 0; 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)()))
+ {
+ int v, c;
+ BOOL negated = FALSE;
+ uschar *saveline = s;
+ uschar name[EXIM_DRIVERNAME_MAX];
+
+ /* Conditions (but not verbs) are allowed to be negated by an initial
+ exclamation mark. */
+
+ if (Uskip_whitespace(&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. */
+
+ if ((v = acl_checkname(name, verbs, nelem(verbs))) < 0)
+ {
+ if (!this)
+ {
+ *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), GET_UNTAINTED);
+ *lastp = this;
+ lastp = &(this->next);
+ this->next = NULL;
+ this->condition = NULL;
+ this->verb = v;
+ this->srcline = config_lineno; /* for debug output */
+ this->srcfile = config_filename; /**/
+ 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. */
+
+ if ((c = acl_checkcondition(name, conditions, nelem(conditions))) < 0)
+ {
+ *error = string_sprintf("unknown ACL condition/modifier in \"%s\"",
+ saveline);
+ return NULL;
+ }
+
+ /* The modifiers may not be negated */
+
+ if (negated && conditions[c].is_modifier)
+ {
+ *error = string_sprintf("ACL error: negation is not allowed with "
+ "\"%s\"", conditions[c].name);
+ 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].name, verbs[this->verb]);
+ return NULL;
+ }
+
+ cond = store_get(sizeof(acl_condition_block), GET_UNTAINTED);
+ 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)
+#ifndef DISABLE_DKIM
+ if ( Ustrncmp(s, "dkim_verify_status", 18) == 0
+ || Ustrncmp(s, "dkim_verify_reason", 18) == 0)
+ {
+ uschar * endptr = s+18;
+
+ if (isalnum(*endptr))
+ {
+ *error = string_sprintf("invalid variable name after \"set\" in ACL "
+ "modifier \"set %s\" "
+ "(only \"dkim_verify_status\" or \"dkim_verify_reason\" permitted)",
+ s);
+ return NULL;
+ }
+ cond->u.varname = string_copyn(s, 18);
+ s = endptr;
+ Uskip_whitespace(&s);
+ }
+ else
+#endif
+ {
+ 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 && *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;
+ Uskip_whitespace(&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,
+ conditions[c].is_modifier ? US"modifier" : US"condition");
+ return NULL;
+ }
+ Uskip_whitespace(&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(const uschar *hstring)
+{
+const 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') /* no newline */
+ q = string_sprintf("%s\n", hstring);
+else if (hstring[hlen-1] == '\n') /* double newline */
+ {
+ uschar * s = string_copy(hstring);
+ while(s[--hlen] == '\n')
+ s[hlen+1] = '\0';
+ q = s;
+ }
+else
+ q = hstring;
+
+/* Loop for multiple header lines, taking care about continuations */
+
+for (p = q; *p; p = q)
+ {
+ const uschar *s;
+ uschar * hdr;
+ int newtype = htype_add_bot;
+ header_line **hptr = &acl_added_headers;
+
+ /* Find next header line within the string */
+
+ for (;;)
+ {
+ q = Ustrchr(q, '\n'); /* we know there was a newline */
+ 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;
+
+ hdr = string_sprintf("%s%.*s", *s == ':' ? "" : "X-ACL-Warn: ", (int) (q - p), p);
+ hlen = Ustrlen(hdr);
+
+ /* See if this line has already been added */
+
+ while (*hptr)
+ {
+ if (Ustrncmp((*hptr)->text, hdr, hlen) == 0) break;
+ hptr = &(*hptr)->next;
+ }
+
+ /* Add if not previously present */
+
+ if (!*hptr)
+ {
+ /* The header_line struct itself is not tainted, though it points to
+ possibly tainted data. */
+ header_line * h = store_get(sizeof(header_line), GET_UNTAINTED);
+ h->text = hdr;
+ h->next = NULL;
+ h->type = newtype;
+ h->slen = hlen;
+ *hptr = h;
+ hptr = &h->next;
+ }
+ }
+}
+
+
+
+/*************************************************
+* List the added header lines *
+*************************************************/
+uschar *
+fn_hdrs_added(void)
+{
+gstring * g = NULL;
+
+for (header_line * h = acl_added_headers; h; h = h->next)
+ {
+ int i = h->slen;
+ if (h->text[i-1] == '\n') i--;
+ g = string_append_listele_n(g, '\n', h->text, i);
+ }
+
+return g ? g->s : NULL;
+}
+
+
+/*************************************************
+* 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(const uschar *hnames)
+{
+if (*hnames)
+ acl_removed_headers = acl_removed_headers
+ ? string_sprintf("%s : %s", acl_removed_headers, hnames)
+ : string_copy(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; logged = logged->next)
+ if (Ustrcmp(logged->text, text) == 0) break;
+
+ if (!logged)
+ {
+ int length = Ustrlen(text) + 1;
+ log_write(0, LOG_MAIN, "%s", text);
+ logged = store_malloc(sizeof(string_item) + length);
+ logged->text = US 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) 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;
+
+/* Previous success */
+
+if (sender_host_name) 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_indent("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 specifying 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)
+{
+int rc = CSA_FAIL_NOADDR;
+
+for (dns_record * rr = dns_next_rr(dnsa, dnss, reset);
+ rr;
+ 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
+ #endif
+ ) continue;
+
+ if (strcmpic(target, rr->name) != 0) continue;
+
+ rc = CSA_FAIL_MISMATCH;
+
+ /* 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 (dns_address * da = dns_address_from_rr(dnsa, rr); da; da = da->next)
+ {
+ /* If the client IP address matches the target IP address, it's good! */
+
+ DEBUG(D_acl) debug_printf_indent("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. */
+
+return rc;
+}
+
+
+
+/*************************************************
+* 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(const uschar *domain)
+{
+tree_node *t;
+const uschar *found;
+int priority, weight, port;
+dns_answer * dnsa;
+dns_scan dnss;
+dns_record *rr;
+int rc, type, yield;
+#define TARGET_SIZE 256
+uschar * target = store_get(TARGET_SIZE, GET_TAINTED);
+
+/* 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) domain = sender_host_address;
+if (!sender_host_address) 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] == '[')
+ {
+ const 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;
+ domain = dns_build_reverse(domain);
+ }
+
+/* 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. */
+
+if ((t = tree_search(csa_cache, domain)))
+ return t->data.val;
+
+t = store_get_perm(sizeof(tree_node) + Ustrlen(domain), domain);
+Ustrcpy(t->name, domain);
+(void)tree_insertnode(&csa_cache, t);
+
+/* Now we are ready to do the actual DNS lookup(s). */
+
+found = domain;
+dnsa = store_get_dns_answer();
+switch (dns_special_lookup(dnsa, domain, T_CSA, &found))
+ {
+ /* If something bad happened (most commonly DNS_AGAIN), defer. */
+
+ default:
+ yield = CSA_DEFER_SRV;
+ goto out;
+
+ /* If we found nothing, the client's authorization is unknown. */
+
+ case DNS_NOMATCH:
+ case DNS_NODATA:
+ yield = CSA_UNKNOWN;
+ goto out;
+
+ /* 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;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)) if (rr->type == T_SRV)
+ {
+ const uschar * p = rr->data;
+
+ /* Extract the numerical SRV fields (p is incremented) */
+
+ GETSHORT(priority, p);
+ GETSHORT(weight, p);
+ GETSHORT(port, p);
+
+ DEBUG(D_acl)
+ debug_printf_indent("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 (Ustrcmp(found, domain) != 0)
+ {
+ yield = port & 1 ? CSA_FAIL_EXPLICIT : CSA_UNKNOWN;
+ goto out;
+ }
+
+ /* 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)
+ {
+ yield = CSA_FAIL_DOMAIN;
+ goto out;
+ }
+
+ 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, TARGET_SIZE);
+
+ DEBUG(D_acl) debug_printf_indent("CSA target is %s\n", target);
+
+ break;
+ }
+
+/* If we didn't break the loop then no appropriate records were found. */
+
+if (!rr)
+ {
+ yield = CSA_UNKNOWN;
+ goto out;
+ }
+
+/* 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)
+ {
+ yield = CSA_FAIL_NOADDR;
+ goto out;
+ }
+
+/* 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)
+ {
+ yield = rc;
+ goto out;
+ }
+
+/* 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;
+
+
+lookup_dnssec_authenticated = NULL;
+switch (dns_lookup(dnsa, target, type, NULL))
+ {
+ /* If something bad happened (most commonly DNS_AGAIN), defer. */
+
+ default:
+ yield = CSA_DEFER_ADDR;
+ break;
+
+ /* 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)
+ {
+ yield = rc;
+ break;
+ }
+ /* 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:
+ yield = CSA_FAIL_NOADDR;
+ break;
+ }
+
+out:
+
+store_free_dns_answer(dnsa);
+return t->data.val = yield;
+}
+
+
+
+/*************************************************
+* 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, VERIFY_ARC
+ };
+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[] = {
+ /* name value where no-opt opt-sep */
+ { US"reverse_host_lookup", VERIFY_REV_HOST_LKUP, (unsigned)~0, FALSE, 0 },
+ { US"certificate", VERIFY_CERT, (unsigned)~0, TRUE, 0 },
+ { US"helo", VERIFY_HELO, (unsigned)~0, TRUE, 0 },
+ { US"csa", VERIFY_CSA, (unsigned)~0, FALSE, 0 },
+ { US"header_syntax", VERIFY_HDR_SYNTAX, ACL_BITS_HAVEDATA, TRUE, 0 },
+ { US"not_blind", VERIFY_NOT_BLIND, ACL_BITS_HAVEDATA, FALSE, 0 },
+ { US"header_sender", VERIFY_HDR_SNDR, ACL_BITS_HAVEDATA, FALSE, 0 },
+ { US"sender", VERIFY_SNDR, ACL_BIT_MAIL | ACL_BIT_RCPT
+ | ACL_BIT_PREDATA | ACL_BIT_DATA | ACL_BIT_NOTSMTP,
+ FALSE, 6 },
+ { US"recipient", VERIFY_RCPT, ACL_BIT_RCPT, FALSE, 0 },
+ { US"header_names_ascii", VERIFY_HDR_NAMES_ASCII, ACL_BITS_HAVEDATA, TRUE, 0 },
+#ifdef EXPERIMENTAL_ARC
+ { US"arc", VERIFY_ARC, ACL_BIT_DATA, FALSE , 0 },
+#endif
+ };
+
+
+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_HOLD, CALLOUT_TIME /* TIME must be last */
+ };
+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[] = {
+ /* name value flag has-opt has-time */
+ { 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 },
+ { US"hold", CALLOUT_HOLD, vopt_callout_hold, FALSE, FALSE },
+ { NULL, CALLOUT_TIME, 0, FALSE, TRUE }
+ };
+
+
+
+static int
+v_period(const uschar * s, const uschar * arg, uschar ** log_msgptr)
+{
+int period;
+if ((period = readconf_readtime(s, 0, FALSE)) < 0)
+ {
+ *log_msgptr = string_sprintf("bad time value in ACL condition "
+ "\"verify %s\"", arg);
+ }
+return period;
+}
+
+
+
+/* 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, const 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;
+BOOL quota = FALSE;
+int quota_pos_cache = QUOTA_POS_DEFAULT, quota_neg_cache = QUOTA_NEG_DEFAULT;
+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, '/');
+const uschar *list = arg;
+uschar *ss = string_nextinlist(&list, &sep, NULL, 0);
+verify_type_t * vp;
+
+if (!ss) goto BAD_VERIFY;
+
+/* Handle name/address consistency verification in a separate function. */
+
+for (vp = verify_type_list;
+ CS vp < CS 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 (CS vp >= CS verify_type_list + sizeof(verify_type_list))
+ goto BAD_VERIFY;
+
+if (vp->no_options && slash)
+ {
+ *log_msgptr = string_sprintf("unexpected '/' found in \"%s\" "
+ "(this verify item has no options)", arg);
+ return ERROR;
+ }
+if (!(vp->where_allowed & BIT(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) return OK;
+ if ((rc = acl_verify_reverse(user_msgptr, log_msgptr)) == DEFER)
+ while ((ss = string_nextinlist(&list, &sep, NULL, 0)))
+ if (strcmpic(ss, US"defer_ok") == 0)
+ return OK;
+ return rc;
+
+ 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 (!f.helo_verified && !f.helo_verify_failed) smtp_verify_helo();
+ return f.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_indent("CSA result %s\n", csa_status);
+ return csa_return_code[rc];
+
+#ifdef EXPERIMENTAL_ARC
+ case VERIFY_ARC:
+ { /* Do Authenticated Received Chain checks in a separate function. */
+ const uschar * condlist = CUS string_nextinlist(&list, &sep, NULL, 0);
+ int csep = 0;
+ uschar * cond;
+
+ if (!(arc_state = acl_verify_arc())) return DEFER;
+ DEBUG(D_acl) debug_printf_indent("ARC verify result %s %s%s%s\n", arc_state,
+ arc_state_reason ? "(":"", arc_state_reason, arc_state_reason ? ")":"");
+
+ if (!condlist) condlist = US"none:pass";
+ while ((cond = string_nextinlist(&condlist, &csep, NULL, 0)))
+ if (Ustrcmp(arc_state, cond) == 0) return OK;
+ return FAIL;
+ }
+#endif
+
+ case VERIFY_HDR_SYNTAX:
+ /* Check that all relevant header lines have the correct 5322-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 && *log_msgptr)
+ if (smtp_return_error_details)
+ *user_msgptr = string_sprintf("Rejected after DATA: %s", *log_msgptr);
+ else
+ acl_verify_message = *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)
+ *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:. */
+ {
+ BOOL case_sensitive = TRUE;
+
+ while ((ss = string_nextinlist(&list, &sep, NULL, 0)))
+ if (strcmpic(ss, US"case_insensitive") == 0)
+ case_sensitive = FALSE;
+ else
+ {
+ *log_msgptr = string_sprintf("unknown option \"%s\" in ACL "
+ "condition \"verify %s\"", ss, arg);
+ return ERROR;
+ }
+
+ if ((rc = verify_check_notblind(case_sensitive)) != OK)
+ {
+ *log_msgptr = US"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)
+ 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, NULL, 0)))
+ {
+ 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;
+ if (*(ss += 7))
+ {
+ while (isspace(*ss)) ss++;
+ if (*ss++ == '=')
+ {
+ const uschar * sublist = ss;
+ int optsep = ',';
+
+ while (isspace(*sublist)) sublist++;
+ for (uschar * opt; opt = string_nextinlist(&sublist, &optsep, NULL, 0); )
+ {
+ 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 = v_period(opt, arg, log_msgptr)) < 0)
+ 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;
+ }
+ }
+ }
+
+ /* The quota option has sub-options, comma-separated */
+
+ else if (strncmpic(ss, US"quota", 5) == 0)
+ {
+ quota = TRUE;
+ if (*(ss += 5))
+ {
+ while (isspace(*ss)) ss++;
+ if (*ss++ == '=')
+ {
+ const uschar * sublist = ss;
+ int optsep = ',';
+ int period;
+
+ while (isspace(*sublist)) sublist++;
+ for (uschar * opt; opt = string_nextinlist(&sublist, &optsep, NULL, 0); )
+ if (Ustrncmp(opt, "cachepos=", 9) == 0)
+ if ((period = v_period(opt += 9, arg, log_msgptr)) < 0)
+ return ERROR;
+ else
+ quota_pos_cache = period;
+ else if (Ustrncmp(opt, "cacheneg=", 9) == 0)
+ if ((period = v_period(opt += 9, arg, log_msgptr)) < 0)
+ return ERROR;
+ else
+ quota_neg_cache = period;
+ else if (Ustrcmp(opt, "no_cache") == 0)
+ quota_pos_cache = quota_neg_cache = 0;
+ }
+ }
+ }
+
+ /* 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 quota verification */
+if (quota)
+ {
+ if (vp->value != VERIFY_RCPT)
+ {
+ *log_msgptr = US"can only verify quota of recipient";
+ return ERROR;
+ }
+
+ if ((rc = verify_quota_call(addr->address,
+ quota_pos_cache, quota_neg_cache, log_msgptr)) != OK)
+ {
+ *basic_errno = errno;
+ if (smtp_return_error_details)
+ {
+ if (!*user_msgptr && *log_msgptr)
+ *user_msgptr = string_sprintf("Rejected after %s: %s",
+ smtp_names[smtp_connection_had[SMTP_HBUFF_PREV(smtp_ch_index)]],
+ *log_msgptr);
+ if (rc == DEFER) f.acl_temp_details = TRUE;
+ }
+ }
+
+ return rc;
+ }
+
+/* Handle sender-in-header verification. Default the user message to the log
+message if giving out verification details. */
+
+if (verify_header_sender)
+ {
+ int verrno;
+
+ if ((rc = verify_check_header_address(user_msgptr, log_msgptr, callout,
+ callout_overall, callout_connect, se_mailfrom, pm_mailfrom, verify_options,
+ &verrno)) != OK)
+ {
+ *basic_errno = verrno;
+ if (smtp_return_error_details)
+ {
+ if (!*user_msgptr && *log_msgptr)
+ *user_msgptr = string_sprintf("Rejected after DATA: %s", *log_msgptr);
+ if (rc == DEFER) f.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)
+ {
+ if ((verify_options & (vopt_callout_recipsender|vopt_callout_recippmaster)))
+ {
+ *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 /* 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_indent("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);
+#ifdef SUPPORT_I18N
+ if ((sender_vaddr->prop.utf8_msg = message_smtputf8))
+ {
+ sender_vaddr->prop.utf8_downcvt = message_utf8_downconvert == 1;
+ sender_vaddr->prop.utf8_downcvt_maybe = message_utf8_downconvert == -1;
+ }
+#endif
+ 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_indent("----------- end verify ------------\n");
+
+ if (rc != OK)
+ *basic_errno = sender_vaddr->basic_errno;
+ else
+ DEBUG(D_acl)
+ if (Ustrcmp(sender_vaddr->address, verify_sender_address) != 0)
+ debug_printf_indent("sender %s verified ok as %s\n",
+ verify_sender_address, sender_vaddr->address);
+ else
+ debug_printf_indent("sender %s verified ok\n",
+ verify_sender_address);
+ }
+ 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->prop.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_indent("----------- end verify ------------\n");
+
+ *basic_errno = addr2.basic_errno;
+ *log_msgptr = addr2.message;
+ *user_msgptr = addr2.user_message ? addr2.user_message : addr2.message;
+
+ /* Allow details for temporary error if the address is so flagged. */
+ if (testflag((&addr2), af_pass_message)) f.acl_temp_details = TRUE;
+
+ /* Make $address_data visible */
+ deliver_address_data = addr2.prop.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_indent("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)
+ {
+ 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)
+ {
+ 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.
+To handle the case "queue_only" we accept an _ in the
+initial / option-switch position.
+
+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(const uschar *arg, const uschar **pptr, int where, uschar **log_msgptr)
+{
+int idx, len;
+control_def * d;
+uschar c;
+
+if ( (idx = find_control(arg, controls_list, nelem(controls_list))) < 0
+ || ( (c = arg[len = Ustrlen((d = controls_list+idx)->name)]) != 0
+ && (!d->has_option || c != '/' && c != '_')
+ ) )
+ {
+ *log_msgptr = string_sprintf("syntax error in \"control=%s\"", arg);
+ return CONTROL_ERROR;
+ }
+
+*pptr = arg + len;
+return idx;
+}
+
+
+
+
+/*************************************************
+* Return a ratelimit error *
+*************************************************/
+
+/* Called from acl_ratelimit() below
+
+Arguments:
+ log_msgptr for error messages
+ format format string
+ ... supplementary arguments
+
+Returns: ERROR
+*/
+
+static int
+ratelimit_error(uschar **log_msgptr, const char *format, ...)
+{
+va_list ap;
+gstring * g =
+ string_cat(NULL, US"error in arguments to \"ratelimit\" condition: ");
+
+va_start(ap, format);
+g = string_vformat(g, SVFMT_EXTEND|SVFMT_REBUFFER, format, ap);
+va_end(ap);
+
+gstring_release_unused(g);
+*log_msgptr = string_from_gstring(g);
+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(const 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. */
+
+if (!(sender_rate_limit = string_nextinlist(&arg, &sep, NULL, 0)))
+ return ratelimit_error(log_msgptr, "sender rate limit not set");
+
+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. */
+
+period = !(sender_rate_period = string_nextinlist(&arg, &sep, NULL, 0))
+ ? -1.0 : 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, NULL, 0)))
+ {
+ 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 command, 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)
+ 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, or cannot be used 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)
+ key = !sender_host_address ? 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_indent("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;
+ }
+
+if ((t = tree_search(*anchor, key)))
+ {
+ 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_indent("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. */
+
+if (!(dbm = dbfn_open(US"ratelimit", O_RDWR, &dbblock, TRUE, TRUE)))
+ {
+ store_pool = old_pool;
+ sender_rate = NULL;
+ HDEBUG(D_acl) debug_printf_indent("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)
+ {
+ /* Locate the basic ratelimit block inside the DB data. */
+ HDEBUG(D_acl) debug_printf_indent("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 && tv.tv_sec > dbdb->bloom_epoch + period)
+ {
+ HDEBUG(D_acl) debug_printf_indent("ratelimit discarding old Bloom filter\n");
+ dbdb = NULL;
+ }
+
+ /* Sanity check. */
+
+ if(unique && dbdb_size < sizeof(*dbdb))
+ {
+ HDEBUG(D_acl) debug_printf_indent("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)
+ {
+ if (!unique)
+ {
+ /* No Bloom filter. This basic ratelimit block is initialized below. */
+ HDEBUG(D_acl) debug_printf_indent("ratelimit creating new rate data block\n");
+ dbdb_size = sizeof(*dbd);
+ dbdb = store_get(dbdb_size, GET_UNTAINTED);
+ }
+ else
+ {
+ int extra;
+ HDEBUG(D_acl) debug_printf_indent("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, GET_UNTAINTED);
+ 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)
+ {
+ 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 && !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_indent("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_indent("ratelimit event found in Bloom filter\n");
+ count = 0.0;
+ }
+ else
+ HDEBUG(D_acl) debug_printf_indent("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)
+ {
+ HDEBUG(D_acl) debug_printf_indent("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-initialize 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_indent("ratelimit db updated\n");
+ }
+else
+ {
+ HDEBUG(D_acl) debug_printf_indent("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. Take the taint status
+from the key for consistency even though it's unlikely we'll ever expand this. */
+
+t = store_get(sizeof(tree_node) + Ustrlen(key), 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_indent("ratelimit computed rate %s\n", sender_rate);
+
+return rc;
+}
+
+
+
+/*************************************************
+* Handle a check for previously-seen *
+*************************************************/
+
+/*
+ACL clauses like: seen = -5m / key=$foo / readonly
+
+Return is true for condition-true - but the semantics
+depend heavily on the actual use-case.
+
+Negative times test for seen-before, positive for seen-more-recently-than
+(the given interval before current time).
+
+All are subject to history not having been cleaned from the DB.
+
+Default for seen-before is to create if not present, and to
+update if older than 10d (with the seen-test time).
+Default for seen-since is to always create or update.
+
+Options:
+ key=value. Default key is $sender_host_address
+ readonly
+ write
+ refresh=<interval>: update an existing DB entry older than given
+ amount. Default refresh lacking this option is 10d.
+ The update sets the record timestamp to the seen-test time.
+
+XXX do we need separate nocreate, noupdate controls?
+
+Arguments:
+ arg the option string for seen=
+ where ACL_WHERE_xxxx indicating which ACL this is
+ log_msgptr for error messages
+
+Returns: OK - Condition is true
+ FAIL - Condition is false
+ DEFER - Problem opening history database
+ ERROR - Syntax error in options
+*/
+
+static int
+acl_seen(const uschar * arg, int where, uschar ** log_msgptr)
+{
+enum { SEEN_DEFAULT, SEEN_READONLY, SEEN_WRITE };
+
+const uschar * list = arg;
+int slash = '/', interval, mode = SEEN_DEFAULT, yield = FAIL;
+BOOL before;
+int refresh = 10 * 24 * 60 * 60; /* 10 days */
+const uschar * ele, * key = sender_host_address;
+open_db dbblock, * dbm;
+dbdata_seen * dbd;
+time_t now;
+
+/* Parse the first element, the time-relation. */
+
+if (!(ele = string_nextinlist(&list, &slash, NULL, 0)))
+ goto badparse;
+if ((before = *ele == '-'))
+ ele++;
+if ((interval = readconf_readtime(ele, 0, FALSE)) < 0)
+ goto badparse;
+
+/* Remaining elements are options */
+
+while ((ele = string_nextinlist(&list, &slash, NULL, 0)))
+ if (Ustrncmp(ele, "key=", 4) == 0)
+ key = ele + 4;
+ else if (Ustrcmp(ele, "readonly") == 0)
+ mode = SEEN_READONLY;
+ else if (Ustrcmp(ele, "write") == 0)
+ mode = SEEN_WRITE;
+ else if (Ustrncmp(ele, "refresh=", 8) == 0)
+ {
+ if ((refresh = readconf_readtime(ele + 8, 0, FALSE)) < 0)
+ goto badparse;
+ }
+ else
+ goto badopt;
+
+if (!(dbm = dbfn_open(US"seen", O_RDWR, &dbblock, TRUE, TRUE)))
+ {
+ HDEBUG(D_acl) debug_printf_indent("database for 'seen' not available\n");
+ *log_msgptr = US"database for 'seen' not available";
+ return DEFER;
+ }
+
+dbd = dbfn_read_with_length(dbm, key, NULL);
+now = time(NULL);
+if (dbd) /* an existing record */
+ {
+ time_t diff = now - dbd->time_stamp; /* time since the record was written */
+
+ if (before ? diff >= interval : diff < interval)
+ yield = OK;
+
+ if (mode == SEEN_READONLY)
+ { HDEBUG(D_acl) debug_printf_indent("seen db not written (readonly)\n"); }
+ else if (mode == SEEN_WRITE || !before)
+ {
+ dbd->time_stamp = now;
+ dbfn_write(dbm, key, dbd, sizeof(*dbd));
+ HDEBUG(D_acl) debug_printf_indent("seen db written (update)\n");
+ }
+ else if (diff >= refresh)
+ {
+ dbd->time_stamp = now - interval;
+ dbfn_write(dbm, key, dbd, sizeof(*dbd));
+ HDEBUG(D_acl) debug_printf_indent("seen db written (refresh)\n");
+ }
+ }
+else
+ { /* No record found, yield always FAIL */
+ if (mode != SEEN_READONLY)
+ {
+ dbdata_seen d = {.time_stamp = now};
+ dbfn_write(dbm, key, &d, sizeof(*dbd));
+ HDEBUG(D_acl) debug_printf_indent("seen db written (create)\n");
+ }
+ else
+ HDEBUG(D_acl) debug_printf_indent("seen db not written (readonly)\n");
+ }
+
+dbfn_close(dbm);
+return yield;
+
+
+badparse:
+ *log_msgptr = string_sprintf("failed to parse '%s'", arg);
+ return ERROR;
+badopt:
+ *log_msgptr = string_sprintf("unrecognised option '%s' in '%s'", ele, arg);
+ return ERROR;
+}
+
+
+
+/*************************************************
+* 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(const 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)
+ {
+ *log_msgptr = US"missing destination host in \"udpsend\" modifier";
+ return ERROR;
+ }
+if (!portstr)
+ {
+ *log_msgptr = US"missing destination port in \"udpsend\" modifier";
+ return ERROR;
+ }
+if (!arg)
+ {
+ *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), GET_UNTAINTED);
+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_indent("udpsend [%s]:%d %s\n", h->address, portnum, arg);
+
+/*XXX this could better use sendto */
+r = s = ip_connectedsocket(SOCK_DGRAM, h->address, portnum, portnum,
+ 1, NULL, &errstr, NULL);
+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_indent("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;
+int rc = OK;
+#ifdef WITH_CONTENT_SCAN
+int sep = -'/';
+#endif
+
+for (; cb; cb = cb->next)
+ {
+ const 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_indent(" message: %s\n", cb->arg);
+ user_message = cb->arg;
+ continue;
+ }
+
+ if (cb->type == ACLC_LOG_MESSAGE)
+ {
+ HDEBUG(D_acl) debug_printf_indent("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 (!conditions[cb->type].expand_at_top)
+ arg = cb->arg;
+ else if (!(arg = expand_string(cb->arg)))
+ {
+ if (f.expand_string_forcedfail) continue;
+ *log_msgptr = string_sprintf("failed to expand ACL string \"%s\": %s",
+ cb->arg, expand_string_message);
+ return f.search_find_defer ? DEFER : ERROR;
+ }
+
+ /* Show condition, and expanded condition if it's different */
+
+ HDEBUG(D_acl)
+ {
+ int lhswidth = 0;
+ debug_printf_indent("check %s%s %n",
+ (!conditions[cb->type].is_modifier && cb->u.negated)? "!":"",
+ conditions[cb->type].name, &lhswidth);
+
+ if (cb->type == ACLC_SET)
+ {
+#ifndef DISABLE_DKIM
+ if ( Ustrcmp(cb->u.varname, "dkim_verify_status") == 0
+ || Ustrcmp(cb->u.varname, "dkim_verify_reason") == 0)
+ {
+ debug_printf("%s ", cb->u.varname);
+ lhswidth += 19;
+ }
+ else
+#endif
+ {
+ 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 ((conditions[cb->type].forbids & (1 << where)) != 0)
+ {
+ *log_msgptr = string_sprintf("cannot %s %s condition in %s ACL",
+ conditions[cb->type].is_modifier ? "use" : "test",
+ conditions[cb->type].name, 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, 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 ? match_isinlist(sender_host_authenticated,
+ &arg, 0, NULL, NULL, MCL_STRING, TRUE, NULL) : FAIL;
+ 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:
+ {
+ const uschar *p = NULL;
+ control_type = decode_control(arg, &p, where, log_msgptr);
+
+ /* Check if this control makes sense at this time */
+
+ if (controls_list[control_type].forbids & (1 << where))
+ {
+ *log_msgptr = string_sprintf("cannot use \"control=%s\" in %s ACL",
+ controls_list[control_type].name, acl_wherenames[where]);
+ return ERROR;
+ }
+
+ switch(control_type)
+ {
+ case CONTROL_AUTH_UNADVERTISED:
+ f.allow_auth_unadvertised = TRUE;
+ break;
+
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+ case CONTROL_BMI_RUN:
+ bmi_run = 1;
+ break;
+#endif
+
+#ifndef DISABLE_DKIM
+ case CONTROL_DKIM_VERIFY:
+ f.dkim_disable_verify = TRUE;
+# ifdef SUPPORT_DMARC
+ /* Since DKIM was blocked, skip DMARC too */
+ f.dmarc_disable_verify = TRUE;
+ f.dmarc_enable_forensic = FALSE;
+# endif
+ break;
+#endif
+
+#ifdef SUPPORT_DMARC
+ case CONTROL_DMARC_VERIFY:
+ f.dmarc_disable_verify = TRUE;
+ break;
+
+ case CONTROL_DMARC_FORENSIC:
+ f.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);
+ if ((af = ip_get_address_family(fd)) < 0)
+ {
+ HDEBUG(D_acl)
+ debug_printf_indent("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_indent("failed to set input DSCP[%s]: %s\n",
+ p+1, strerror(errno));
+ }
+ else
+ {
+ HDEBUG(D_acl) debug_printf_indent("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:
+ f.no_mbox_unspool = TRUE;
+ break;
+#endif
+
+ case CONTROL_NO_MULTILINE:
+ f.no_multiline_responses = TRUE;
+ break;
+
+ case CONTROL_NO_PIPELINING:
+ f.pipelining_enable = FALSE;
+ break;
+
+ case CONTROL_NO_DELAY_FLUSH:
+ f.disable_delay_flush = TRUE;
+ break;
+
+ case CONTROL_NO_CALLOUT_FLUSH:
+ f.disable_callout_flush = TRUE;
+ break;
+
+ case CONTROL_FAKEREJECT:
+ cancel_cutthrough_connection(TRUE, US"fakereject");
+ case CONTROL_FAKEDEFER:
+ fake_response = (control_type == CONTROL_FAKEDEFER) ? DEFER : FAIL;
+ if (*p == '/')
+ {
+ const uschar *pp = p + 1;
+ while (*pp) pp++;
+ /* The entire control= line was expanded at top so no need to expand
+ the part after the / */
+ fake_response_text = 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:
+ f.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)
+ {
+ *log_msgptr = string_sprintf("syntax error in \"control=%s\"", arg);
+ return ERROR;
+ }
+ cancel_cutthrough_connection(TRUE, US"item frozen");
+ break;
+
+ case CONTROL_QUEUE:
+ f.queue_only_policy = TRUE;
+ if (Ustrcmp(p, "_only") == 0)
+ p += 5;
+ else while (*p == '/')
+ if (Ustrncmp(p, "/only", 5) == 0)
+ { p += 5; f.queue_smtp = FALSE; }
+ else if (Ustrncmp(p, "/first_pass_route", 17) == 0)
+ { p += 17; f.queue_smtp = TRUE; }
+ else
+ break;
+ cancel_cutthrough_connection(TRUE, US"queueing forced");
+ break;
+
+ case CONTROL_SUBMISSION:
+ originator_name = US"";
+ f.submission_mode = TRUE;
+ while (*p == '/')
+ {
+ if (Ustrncmp(p, "/sender_retain", 14) == 0)
+ {
+ p += 14;
+ f.active_local_sender_retain = TRUE;
+ f.active_local_from_check = FALSE;
+ }
+ else if (Ustrncmp(p, "/domain=", 8) == 0)
+ {
+ const uschar *pp = p + 8;
+ while (*pp && *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)
+ {
+ const uschar *pp = p + 6;
+ while (*pp) pp++;
+ submission_name = parse_fix_phrase(p+6, pp-p-6);
+ p = pp;
+ }
+ else break;
+ }
+ if (*p)
+ {
+ *log_msgptr = string_sprintf("syntax error in \"control=%s\"", arg);
+ return ERROR;
+ }
+ break;
+
+ case CONTROL_DEBUG:
+ {
+ uschar * debug_tag = NULL, * debug_opts = NULL;
+ BOOL kill = FALSE, stop = FALSE;
+
+ while (*p == '/')
+ {
+ const uschar * pp = p+1;
+ if (Ustrncmp(pp, "tag=", 4) == 0)
+ {
+ for (pp += 4; *pp && *pp != '/';) pp++;
+ debug_tag = string_copyn(p+5, pp-p-5);
+ }
+ else if (Ustrncmp(pp, "opts=", 5) == 0)
+ {
+ for (pp += 5; *pp && *pp != '/';) pp++;
+ debug_opts = string_copyn(p+6, pp-p-6);
+ }
+ else if (Ustrncmp(pp, "kill", 4) == 0)
+ {
+ pp += 4;
+ kill = TRUE;
+ }
+ else if (Ustrncmp(pp, "stop", 4) == 0)
+ {
+ pp += 4;
+ stop = TRUE;
+ }
+ else if (Ustrncmp(pp, "pretrigger=", 11) == 0)
+ debug_pretrigger_setup(pp+11);
+ else if (Ustrncmp(pp, "trigger=", 8) == 0)
+ {
+ if (Ustrncmp(pp += 8, "now", 3) == 0)
+ {
+ pp += 3;
+ debug_trigger_fire();
+ }
+ else if (Ustrncmp(pp, "paniclog", 8) == 0)
+ {
+ pp += 8;
+ dtrigger_selector |= BIT(DTi_panictrigger);
+ }
+ }
+ while (*pp && *pp != '/') pp++;
+ p = pp;
+ }
+
+ if (kill)
+ debug_logging_stop(TRUE);
+ else if (stop)
+ debug_logging_stop(FALSE);
+ else if (debug_tag || debug_opts)
+ debug_logging_activate(debug_tag, debug_opts);
+ break;
+ }
+
+ case CONTROL_SUPPRESS_LOCAL_FIXUPS:
+ f.suppress_local_fixups = TRUE;
+ break;
+
+ case CONTROL_CUTTHROUGH_DELIVERY:
+ {
+ uschar * ignored = NULL;
+#ifndef DISABLE_PRDR
+ if (prdr_requested)
+#else
+ if (0)
+#endif
+ /* Too hard to think about for now. We might in future cutthrough
+ the case where both sides handle prdr and this-node prdr acl
+ is "accept" */
+ ignored = US"PRDR active";
+ else if (f.deliver_freeze)
+ ignored = US"frozen";
+ else if (f.queue_only_policy)
+ ignored = US"queue-only";
+ else if (fake_response == FAIL)
+ ignored = US"fakereject";
+ else if (rcpt_count != 1)
+ ignored = US"nonfirst rcpt";
+ else if (cutthrough.delivery)
+ ignored = US"repeated";
+ else if (cutthrough.callout_hold_only)
+ {
+ DEBUG(D_acl)
+ debug_printf_indent(" cutthrough request upgrades callout hold\n");
+ cutthrough.callout_hold_only = FALSE;
+ cutthrough.delivery = TRUE; /* control accepted */
+ }
+ else
+ {
+ cutthrough.delivery = TRUE; /* control accepted */
+ while (*p == '/')
+ {
+ const uschar * pp = p+1;
+ if (Ustrncmp(pp, "defer=", 6) == 0)
+ {
+ pp += 6;
+ if (Ustrncmp(pp, "pass", 4) == 0) cutthrough.defer_pass = TRUE;
+ /* else if (Ustrncmp(pp, "spool") == 0) ; default */
+ }
+ else
+ while (*pp && *pp != '/') pp++;
+ p = pp;
+ }
+ }
+
+ DEBUG(D_acl) if (ignored)
+ debug_printf(" cutthrough request ignored on %s item\n", ignored);
+ }
+ break;
+
+#ifdef SUPPORT_I18N
+ case CONTROL_UTF8_DOWNCONVERT:
+ if (*p == '/')
+ {
+ if (p[1] == '1')
+ {
+ message_utf8_downconvert = 1;
+ addr->prop.utf8_downcvt = TRUE;
+ addr->prop.utf8_downcvt_maybe = FALSE;
+ p += 2;
+ break;
+ }
+ if (p[1] == '0')
+ {
+ message_utf8_downconvert = 0;
+ addr->prop.utf8_downcvt = FALSE;
+ addr->prop.utf8_downcvt_maybe = FALSE;
+ p += 2;
+ break;
+ }
+ if (p[1] == '-' && p[2] == '1')
+ {
+ message_utf8_downconvert = -1;
+ addr->prop.utf8_downcvt = FALSE;
+ addr->prop.utf8_downcvt_maybe = TRUE;
+ p += 3;
+ break;
+ }
+ *log_msgptr = US"bad option value for control=utf8_downconvert";
+ }
+ else
+ {
+ message_utf8_downconvert = 1;
+ addr->prop.utf8_downcvt = TRUE;
+ addr->prop.utf8_downcvt_maybe = FALSE;
+ break;
+ }
+ return ERROR;
+#endif
+
+ }
+ break;
+ }
+
+ #ifdef EXPERIMENTAL_DCC
+ case ACLC_DCC:
+ {
+ /* Separate the regular expression and any optional parameters. */
+ const uschar * list = arg;
+ uschar *ss = string_nextinlist(&list, &sep, NULL, 0);
+ /* Run the dcc backend. */
+ rc = dcc_process(&ss);
+ /* Modify return code based upon the existence of options. */
+ while ((ss = string_nextinlist(&list, &sep, NULL, 0)))
+ if (strcmpic(ss, US"defer_ok") == 0 && rc == DEFER)
+ rc = FAIL; /* FAIL so that the message is passed to the next ACL */
+ }
+ 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_indent("delay modifier requests %d-second delay\n",
+ delay);
+ if (host_checking)
+ {
+ HDEBUG(D_acl)
+ debug_printf_indent("delay skipped in -bh checking mode\n");
+ }
+
+ /* NOTE 1: 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 && !f.disable_delay_flush)
+ mac_smtp_fflush();
+
+#if !defined(NO_POLL_H) && defined (POLLRDHUP)
+ {
+ struct pollfd p;
+ nfds_t n = 0;
+ if (smtp_out)
+ {
+ p.fd = fileno(smtp_out);
+ p.events = POLLRDHUP;
+ n = 1;
+ }
+ if (poll(&p, n, delay*1000) > 0)
+ HDEBUG(D_acl) debug_printf_indent("delay cancelled by peer close\n");
+ }
+#else
+ /* Lacking POLLRDHUP 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. */
+
+ while (delay > 0) delay = sleep(delay);
+#endif
+ }
+ }
+ }
+ break;
+
+#ifndef DISABLE_DKIM
+ case ACLC_DKIM_SIGNER:
+ if (dkim_cur_signer)
+ 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_verify_status,
+ &arg, 0, NULL, NULL, MCL_STRING, TRUE, NULL);
+ break;
+#endif
+
+#ifdef SUPPORT_DMARC
+ case ACLC_DMARC_STATUS:
+ if (!f.dmarc_has_been_checked)
+ dmarc_process();
+ f.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(where, &arg, log_msgptr);
+ break;
+
+ case ACLC_DOMAINS:
+ rc = match_isinlist(addr->domain, &arg, 0, &domainlist_anchor,
+ addr->domain_cache, MCL_DOMAIN, TRUE, CUSS &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) cipher = tls_in.cipher; else
+ {
+ endcipher = Ustrchr(++cipher, ':');
+ if (endcipher) *endcipher = 0;
+ }
+ rc = match_isinlist(cipher, &arg, 0, NULL, NULL, MCL_STRING, TRUE, NULL);
+ if (endcipher) *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 ? sender_host_address : US"", CUSS &host_data);
+ if (rc == DEFER) *log_msgptr = search_error_message;
+ if (host_data) host_data = string_copy_perm(host_data, TRUE);
+ break;
+
+ case ACLC_LOCAL_PARTS:
+ rc = match_isinlist(addr->cc_local_part, &arg, 0,
+ &localpartlist_anchor, addr->localpart_cache, MCL_LOCALPART, TRUE,
+ CUSS &deliver_localpart_data);
+ break;
+
+ case ACLC_LOG_REJECT_TARGET:
+ {
+ int logbits = 0;
+ int sep = 0;
+ const uschar *s = arg;
+ uschar * ss;
+ while ((ss = string_nextinlist(&s, &sep, NULL, 0)))
+ {
+ 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;
+ const 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: /* Run the malware backend. */
+ {
+ /* Separate the regular expression and any optional parameters. */
+ const uschar * list = arg;
+ uschar * ss = string_nextinlist(&list, &sep, NULL, 0);
+ uschar * opt;
+ BOOL defer_ok = FALSE;
+ int timeout = 0;
+
+ while ((opt = string_nextinlist(&list, &sep, NULL, 0)))
+ if (strcmpic(opt, US"defer_ok") == 0)
+ defer_ok = TRUE;
+ else if ( strncmpic(opt, US"tmo=", 4) == 0
+ && (timeout = readconf_readtime(opt+4, '\0', FALSE)) < 0
+ )
+ {
+ *log_msgptr = string_sprintf("bad timeout value in '%s'", opt);
+ return ERROR;
+ }
+
+ rc = malware(ss, timeout);
+ if (rc == DEFER && defer_ok)
+ rc = FAIL; /* FAIL so that the message is passed to the next ACL */
+ }
+ break;
+
+ case ACLC_MIME_REGEX:
+ rc = mime_regex(&arg);
+ break;
+ #endif
+
+ case ACLC_QUEUE:
+ if (is_tainted(arg))
+ {
+ *log_msgptr = string_sprintf("Tainted name '%s' for queue not permitted",
+ arg);
+ return ERROR;
+ }
+ if (Ustrchr(arg, '/'))
+ {
+ *log_msgptr = string_sprintf(
+ "Directory separator not permitted in queue name: '%s'", arg);
+ return ERROR;
+ }
+ queue_name = string_copy_perm(arg, FALSE);
+ break;
+
+ case ACLC_RATELIMIT:
+ rc = acl_ratelimit(arg, where, log_msgptr);
+ break;
+
+ case ACLC_RECIPIENTS:
+ rc = match_address_list(CUS addr->address, TRUE, TRUE, &arg, NULL, -1, 0,
+ CUSS &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_SEEN:
+ rc = acl_seen(arg, where, log_msgptr);
+ break;
+
+ case ACLC_SENDER_DOMAINS:
+ {
+ uschar *sdomain;
+ sdomain = Ustrrchr(sender_address, '@');
+ sdomain = sdomain ? sdomain + 1 : US"";
+ rc = match_isinlist(sdomain, &arg, 0, &domainlist_anchor,
+ sender_domain_cache, MCL_DOMAIN, TRUE, NULL);
+ }
+ break;
+
+ case ACLC_SENDERS:
+ rc = match_address_list(CUS sender_address, TRUE, TRUE, &arg,
+ sender_address_cache, -1, 0, CUSS &sender_data);
+ break;
+
+ /* Connection variables must persist forever; message variables not */
+
+ case ACLC_SET:
+ {
+ int old_pool = store_pool;
+ if ( cb->u.varname[0] != 'm'
+#ifndef DISABLE_EVENT
+ || event_name /* An event is being delivered */
+#endif
+ )
+ store_pool = POOL_PERM;
+#ifndef DISABLE_DKIM /* Overwriteable dkim result variables */
+ if (Ustrcmp(cb->u.varname, "dkim_verify_status") == 0)
+ dkim_verify_status = string_copy(arg);
+ else if (Ustrcmp(cb->u.varname, "dkim_verify_reason") == 0)
+ dkim_verify_reason = string_copy(arg);
+ else
+#endif
+ acl_var_create(cb->u.varname)->data.ptr = string_copy(arg);
+ store_pool = old_pool;
+ }
+ break;
+
+#ifdef WITH_CONTENT_SCAN
+ case ACLC_SPAM:
+ {
+ /* Separate the regular expression and any optional parameters. */
+ const uschar * list = arg;
+ uschar *ss = string_nextinlist(&list, &sep, NULL, 0);
+
+ rc = spam(CUSS &ss);
+ /* Modify return code based upon the existence of options. */
+ while ((ss = string_nextinlist(&list, &sep, NULL, 0)))
+ if (strcmpic(ss, US"defer_ok") == 0 && rc == DEFER)
+ rc = FAIL; /* FAIL so that the message is passed to the next ACL */
+ }
+ break;
+#endif
+
+#ifdef SUPPORT_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);
+ if (*user_msgptr)
+ 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 (!conditions[cb->type].is_modifier && 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 ((BIT(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)
+ {
+ acl_verify_message = old_user_msgptr;
+ expmessage = expand_string(user_message);
+ if (!expmessage)
+ {
+ if (!f.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)
+ {
+ acl_verify_message = old_log_msgptr;
+ expmessage = expand_string(log_message);
+ if (!expmessage)
+ {
+ if (!f.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) *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(;;)
+ {
+ Uskip_whitespace(&acl_text); /* Leading spaces/empty lines */
+ if (!*acl_text) return NULL; /* No more data */
+ yield = acl_text; /* Potential data line */
+
+ while (*acl_text && *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) 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 */
+}
+
+
+
+
+
+/************************************************/
+/* For error messages, a string describing the config location
+associated with current processing. NULL if not in an ACL. */
+
+uschar *
+acl_current_verb(void)
+{
+if (acl_current) return string_sprintf(" (ACL %s, %s %d)",
+ verbs[acl_current->verb], acl_current->srcfile, acl_current->srcline);
+return NULL;
+}
+
+/*************************************************
+* 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
+ 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,
+ 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 (acl_level > 20)
+ {
+ *log_msgptr = US"ACL nested too deep: possible loop";
+ return ERROR;
+ }
+
+if (!s)
+ {
+ HDEBUG(D_acl) debug_printf_indent("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 (acl_level == 0)
+ {
+ if (!(ss = expand_string(s)))
+ {
+ if (f.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;
+
+if (is_tainted(acl_text) && !f.running_in_test_harness)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "attempt to use tainted ACL text \"%s\"", acl_text);
+ /* Avoid leaking info to an attacker */
+ *log_msgptr = US"internal configuration error";
+ return ERROR;
+ }
+
+/* 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)
+ {
+ if (!(acl = (acl_block *)(t->data.ptr)))
+ {
+ HDEBUG(D_acl) debug_printf_indent("ACL \"%s\" is empty: implicit DENY\n", ss);
+ return FAIL;
+ }
+ acl_name = string_sprintf("ACL \"%s\"", ss);
+ HDEBUG(D_acl) debug_printf_indent("using ACL \"%s\"\n", ss);
+ }
+
+ else if (*ss == '/')
+ {
+ struct stat statbuf;
+ if ((fd = Uopen(ss, O_RDONLY, 0)) < 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;
+ }
+
+ /* If the string being used as a filename is tainted, so is the file content */
+ acl_text = store_get(statbuf.st_size + 1, ss);
+ 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_indent("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)
+ {
+ 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 && *log_msgptr) return ERROR;
+ if (fd >= 0)
+ {
+ tree_node * t = store_get_perm(sizeof(tree_node) + Ustrlen(ss), 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_current = acl))
+ {
+ int cond;
+ int basic_errno = 0;
+ BOOL endpass_seen = FALSE;
+ BOOL acl_quit_check = acl_level == 0
+ && (where == ACL_WHERE_QUIT || where == ACL_WHERE_NOTQUIT);
+
+ *log_msgptr = *user_msgptr = NULL;
+ f.acl_temp_details = FALSE;
+
+ HDEBUG(D_acl) debug_printf_indent("processing \"%s\" (%s %d)\n",
+ verbs[acl->verb], acl->srcfile, acl->srcline);
+
+ /* 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, acl_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_indent("%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) f.acl_temp_details = TRUE;
+ }
+ else
+ f.acl_temp_details = TRUE;
+ if (acl->verb != ACL_WARN) return DEFER;
+ break;
+
+ default: /* Paranoia */
+ case ERROR:
+ HDEBUG(D_acl) debug_printf_indent("%s: condition test error in %s\n",
+ verbs[acl->verb], acl_name);
+ return ERROR;
+
+ case OK:
+ HDEBUG(D_acl) debug_printf_indent("%s: condition test succeeded in %s\n",
+ verbs[acl->verb], acl_name);
+ break;
+
+ case FAIL:
+ HDEBUG(D_acl) debug_printf_indent("%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_indent("%s: condition test yielded \"discard\" in %s\n",
+ verbs[acl->verb], acl_name);
+ break;
+
+ case FAIL_DROP:
+ HDEBUG(D_acl) debug_printf_indent("%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)
+ {
+ HDEBUG(D_acl) debug_printf_indent("end of %s: ACCEPT\n", acl_name);
+ return cond;
+ }
+ if (endpass_seen)
+ {
+ HDEBUG(D_acl) debug_printf_indent("accept: endpass encountered - denying access\n");
+ return cond;
+ }
+ break;
+
+ case ACL_DEFER:
+ if (cond == OK)
+ {
+ HDEBUG(D_acl) debug_printf_indent("end of %s: DEFER\n", acl_name);
+ if (acl_quit_check) goto badquit;
+ f.acl_temp_details = TRUE;
+ return DEFER;
+ }
+ break;
+
+ case ACL_DENY:
+ if (cond == OK)
+ {
+ HDEBUG(D_acl) debug_printf_indent("end of %s: DENY\n", acl_name);
+ if (acl_quit_check) goto badquit;
+ return FAIL;
+ }
+ break;
+
+ case ACL_DISCARD:
+ if (cond == OK || cond == DISCARD)
+ {
+ HDEBUG(D_acl) debug_printf_indent("end of %s: DISCARD\n", acl_name);
+ if (acl_quit_check) goto badquit;
+ return DISCARD;
+ }
+ if (endpass_seen)
+ {
+ HDEBUG(D_acl)
+ debug_printf_indent("discard: endpass encountered - denying access\n");
+ return cond;
+ }
+ break;
+
+ case ACL_DROP:
+ if (cond == OK)
+ {
+ HDEBUG(D_acl) debug_printf_indent("end of %s: DROP\n", acl_name);
+ if (acl_quit_check) goto badquit;
+ return FAIL_DROP;
+ }
+ break;
+
+ case ACL_REQUIRE:
+ if (cond != OK)
+ {
+ HDEBUG(D_acl) debug_printf_indent("end of %s: not OK\n", acl_name);
+ if (acl_quit_check) goto badquit;
+ return cond;
+ }
+ break;
+
+ case ACL_WARN:
+ if (cond == OK)
+ acl_warn(where, *user_msgptr, *log_msgptr);
+ else if (cond == DEFER && LOGGING(acl_warn_skipped))
+ log_write(0, LOG_MAIN, "%s Warning: ACL \"warn\" statement skipped: "
+ "condition test deferred%s%s", host_and_ident(TRUE),
+ *log_msgptr ? US": " : US"",
+ *log_msgptr ? *log_msgptr : US"");
+ *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_indent("end of %s: implicit DENY\n", acl_name);
+return FAIL;
+
+badquit:
+ *log_msgptr = string_sprintf("QUIT or not-QUIT toplevel ACL may not fail "
+ "('%s' verb used incorrectly)", verbs[acl->verb]);
+ return ERROR;
+}
+
+
+
+
+/* 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, const uschar *s,
+ 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;
+ }
+
+acl_level++;
+ret = acl_check_internal(where, addr, name, user_msgptr, log_msgptr);
+acl_level--;
+
+acl_narg = sav_narg;
+for (i = 0; i < 9; i++) acl_arg[i] = sav_arg[i];
+return ret;
+
+bad:
+if (f.expand_string_forcedfail) return ERROR;
+*log_msgptr = string_sprintf("failed to expand ACL string \"%s\": %s",
+ tmp, expand_string_message);
+return f.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;
+int rc;
+
+*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;
+ }
+
+acl_level++;
+rc = acl_check_internal(where, addr, s, user_msgptr, log_msgptr);
+acl_level--;
+return rc;
+}
+
+
+
+/* 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_VRFY || where==ACL_WHERE_PRDR)
+#else
+if (where==ACL_WHERE_RCPT || where==ACL_WHERE_VRFY)
+#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;
+ }
+#ifdef SUPPORT_I18N
+ if ((addr->prop.utf8_msg = message_smtputf8))
+ {
+ addr->prop.utf8_downcvt = message_utf8_downconvert == 1;
+ addr->prop.utf8_downcvt_maybe = message_utf8_downconvert == -1;
+ }
+#endif
+ deliver_domain = addr->domain;
+ deliver_localpart = addr->local_part;
+ }
+
+acl_where = where;
+acl_level = 0;
+rc = acl_check_internal(where, addr, s, user_msgptr, log_msgptr);
+acl_level = 0;
+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,
+and will pass the fail to the originator.
+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 (f.host_checking_callout) /* -bhc mode */
+ cancel_cutthrough_connection(TRUE, US"host-checking mode");
+
+ else if ( rc == OK
+ && cutthrough.delivery
+ && rcpt_count > cutthrough.nrcpt
+ )
+ {
+ if ((rc = open_cutthrough_connection(addr)) == DEFER)
+ if (cutthrough.defer_pass)
+ {
+ uschar * s = addr->message;
+ /* Horrid kludge to recover target's SMTP message */
+ while (*s) s++;
+ do --s; while (!isdigit(*s));
+ if (*--s && isdigit(*s) && *--s && isdigit(*s)) *user_msgptr = s;
+ f.acl_temp_details = TRUE;
+ }
+ else
+ {
+ HDEBUG(D_acl) debug_printf_indent("cutthrough defer; will spool\n");
+ rc = OK;
+ }
+ }
+ else HDEBUG(D_acl) if (cutthrough.delivery)
+ if (rcpt_count <= cutthrough.nrcpt)
+ debug_printf_indent("ignore cutthrough request; nonfirst message\n");
+ else if (rc != OK)
+ debug_printf_indent("ignore cutthrough request; ACL did not accept\n");
+ break;
+
+ case ACL_WHERE_PREDATA:
+ if (rc == OK)
+ cutthrough_predata();
+ else
+ cancel_cutthrough_connection(TRUE, US"predata acl not ok");
+ break;
+
+ case ACL_WHERE_QUIT:
+ case ACL_WHERE_NOTQUIT:
+ /* Drop cutthrough conns, and drop heldopen verify conns if
+ the previous was not DATA */
+ {
+ uschar prev =
+ smtp_connection_had[SMTP_HBUFF_PREV(SMTP_HBUFF_PREV(smtp_ch_index))];
+ BOOL dropverify = !(prev == SCH_DATA || prev == SCH_BDAT);
+
+ cancel_cutthrough_connection(dropverify, US"quit or conndrop");
+ break;
+ }
+
+ default:
+ break;
+ }
+
+deliver_domain = deliver_localpart = deliver_address_data =
+ deliver_domain_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 = name[0] == 'c' ? &acl_var_c : &acl_var_m;
+if (!(node = tree_search(*root, name)))
+ {
+ node = store_get(sizeof(tree_node) + Ustrlen(name), 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;
+putc('-', f);
+if (is_tainted(value))
+ {
+ int q = quoter_for_address(value);
+ putc('-', f);
+ if (is_real_quoter(q)) fprintf(f, "(%s)", lookup_list[q]->name);
+ }
+fprintf(f, "acl%c %s %d\n%s\n", name[0], name+1, Ustrlen(value), value);
+}
+
+#endif /* !MACRO_PREDEF */
+/* vi: aw ai sw=2
+*/
+/* End of acl.c */
diff --git a/src/aliases.default b/src/aliases.default
new file mode 100644
index 0000000..725d172
--- /dev/null
+++ b/src/aliases.default
@@ -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 website
+
+####
diff --git a/src/arc.c b/src/arc.c
new file mode 100644
index 0000000..86688f6
--- /dev/null
+++ b/src/arc.c
@@ -0,0 +1,1903 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+/* Experimental ARC support for Exim
+ Copyright (c) Jeremy Harris 2018 - 2020
+ Copyright (c) The Exim Maintainers 2021 - 2022
+ License: GPL
+*/
+
+#include "exim.h"
+#if defined EXPERIMENTAL_ARC
+# if defined DISABLE_DKIM
+# error DKIM must also be enabled for ARC
+# else
+
+# include "functions.h"
+# include "pdkim/pdkim.h"
+# include "pdkim/signing.h"
+
+extern pdkim_ctx * dkim_verify_ctx;
+extern pdkim_ctx dkim_sign_ctx;
+
+#define ARC_SIGN_OPT_TSTAMP BIT(0)
+#define ARC_SIGN_OPT_EXPIRE BIT(1)
+
+#define ARC_SIGN_DEFAULT_EXPIRE_DELTA (60 * 60 * 24 * 30) /* one month */
+
+/******************************************************************************/
+
+typedef struct hdr_rlist {
+ struct hdr_rlist * prev;
+ BOOL used;
+ header_line * h;
+} hdr_rlist;
+
+typedef struct arc_line {
+ header_line * complete; /* including the header name; nul-term */
+ uschar * relaxed;
+
+ /* identified tag contents */
+ /*XXX t= for AS? */
+ blob i;
+ blob cv;
+ blob a;
+ blob b;
+ blob bh;
+ blob d;
+ blob h;
+ blob s;
+ blob c;
+ blob l;
+
+ /* tag content sub-portions */
+ blob a_algo;
+ blob a_hash;
+
+ blob c_head;
+ blob c_body;
+
+ /* modified copy of b= field in line */
+ blob rawsig_no_b_val;
+} arc_line;
+
+typedef struct arc_set {
+ struct arc_set * next;
+ struct arc_set * prev;
+
+ unsigned instance;
+ arc_line * hdr_aar;
+ arc_line * hdr_ams;
+ arc_line * hdr_as;
+
+ const uschar * ams_verify_done;
+ BOOL ams_verify_passed;
+} arc_set;
+
+typedef struct arc_ctx {
+ arc_set * arcset_chain;
+ arc_set * arcset_chain_last;
+} arc_ctx;
+
+#define ARC_HDR_AAR US"ARC-Authentication-Results:"
+#define ARC_HDRLEN_AAR 27
+#define ARC_HDR_AMS US"ARC-Message-Signature:"
+#define ARC_HDRLEN_AMS 22
+#define ARC_HDR_AS US"ARC-Seal:"
+#define ARC_HDRLEN_AS 9
+#define HDR_AR US"Authentication-Results:"
+#define HDRLEN_AR 23
+
+static time_t now;
+static time_t expire;
+static hdr_rlist * headers_rlist;
+static arc_ctx arc_sign_ctx = { NULL };
+static arc_ctx arc_verify_ctx = { NULL };
+
+
+/******************************************************************************/
+
+
+/* Get the instance number from the header.
+Return 0 on error */
+static unsigned
+arc_instance_from_hdr(const arc_line * al)
+{
+const uschar * s = al->i.data;
+if (!s || !al->i.len) return 0;
+return (unsigned) atoi(CCS s);
+}
+
+
+static uschar *
+skip_fws(uschar * s)
+{
+uschar c = *s;
+while (c && (c == ' ' || c == '\t' || c == '\n' || c == '\r')) c = *++s;
+return s;
+}
+
+
+/* Locate instance struct on chain, inserting a new one if
+needed. The chain is in increasing-instance-number order
+by the "next" link, and we have a "prev" link also.
+*/
+
+static arc_set *
+arc_find_set(arc_ctx * ctx, unsigned i)
+{
+arc_set ** pas, * as, * next, * prev;
+
+for (pas = &ctx->arcset_chain, prev = NULL, next = ctx->arcset_chain;
+ as = *pas; pas = &as->next)
+ {
+ if (as->instance > i) break;
+ if (as->instance == i)
+ {
+ DEBUG(D_acl) debug_printf("ARC: existing instance %u\n", i);
+ return as;
+ }
+ next = as->next;
+ prev = as;
+ }
+
+DEBUG(D_acl) debug_printf("ARC: new instance %u\n", i);
+*pas = as = store_get(sizeof(arc_set), GET_UNTAINTED);
+memset(as, 0, sizeof(arc_set));
+as->next = next;
+as->prev = prev;
+as->instance = i;
+if (next)
+ next->prev = as;
+else
+ ctx->arcset_chain_last = as;
+return as;
+}
+
+
+
+/* Insert a tag content into the line structure.
+Note this is a reference to existing data, not a copy.
+Check for already-seen tag.
+The string-pointer is on the '=' for entry. Update it past the
+content (to the ;) on return;
+*/
+
+static uschar *
+arc_insert_tagvalue(arc_line * al, unsigned loff, uschar ** ss)
+{
+uschar * s = *ss;
+uschar c = *++s;
+blob * b = (blob *)(US al + loff);
+size_t len = 0;
+
+/* [FWS] tag-value [FWS] */
+
+if (b->data) return US"fail";
+s = skip_fws(s); /* FWS */
+
+b->data = s;
+while ((c = *s) && c != ';') { len++; s++; }
+*ss = s;
+while (len && ((c = s[-1]) == ' ' || c == '\t' || c == '\n' || c == '\r'))
+ { s--; len--; } /* FWS */
+b->len = len;
+return NULL;
+}
+
+
+/* Inspect a header line, noting known tag fields.
+Check for duplicates. */
+
+static uschar *
+arc_parse_line(arc_line * al, header_line * h, unsigned off, BOOL instance_only)
+{
+uschar * s = h->text + off;
+uschar * r = NULL; /* compiler-quietening */
+uschar c;
+
+al->complete = h;
+
+if (!instance_only)
+ {
+ al->rawsig_no_b_val.data = store_get(h->slen + 1, GET_TAINTED);
+ memcpy(al->rawsig_no_b_val.data, h->text, off); /* copy the header name blind */
+ r = al->rawsig_no_b_val.data + off;
+ al->rawsig_no_b_val.len = off;
+ }
+
+/* tag-list = tag-spec *( ";" tag-spec ) [ ";" ] */
+
+while ((c = *s))
+ {
+ char tagchar;
+ uschar * t;
+ unsigned i = 0;
+ uschar * fieldstart = s;
+ uschar * bstart = NULL, * bend;
+
+ /* tag-spec = [FWS] tag-name [FWS] "=" [FWS] tag-value [FWS] */
+
+ s = skip_fws(s); /* FWS */
+ if (!*s) break;
+/* debug_printf("%s: consider '%s'\n", __FUNCTION__, s); */
+ tagchar = *s++;
+ s = skip_fws(s); /* FWS */
+ if (!*s) break;
+
+ if (!instance_only || tagchar == 'i') switch (tagchar)
+ {
+ case 'a': /* a= AMS algorithm */
+ {
+ if (*s != '=') return US"no 'a' value";
+ if (arc_insert_tagvalue(al, offsetof(arc_line, a), &s)) return US"a tag dup";
+
+ /* substructure: algo-hash (eg. rsa-sha256) */
+
+ t = al->a_algo.data = al->a.data;
+ while (*t != '-')
+ if (!*t++ || ++i > al->a.len) return US"no '-' in 'a' value";
+ al->a_algo.len = i;
+ if (*t++ != '-') return US"no '-' in 'a' value";
+ al->a_hash.data = t;
+ al->a_hash.len = al->a.len - i - 1;
+ }
+ break;
+ case 'b':
+ {
+ gstring * g = NULL;
+
+ switch (*s)
+ {
+ case '=': /* b= AMS signature */
+ if (al->b.data) return US"already b data";
+ bstart = s+1;
+
+ /* The signature can have FWS inserted in the content;
+ make a stripped copy */
+
+ while ((c = *++s) && c != ';')
+ if (c != ' ' && c != '\t' && c != '\n' && c != '\r')
+ g = string_catn(g, s, 1);
+ if (!g) return US"no b= value";
+ al->b.data = string_from_gstring(g);
+ al->b.len = g->ptr;
+ gstring_release_unused(g);
+ bend = s;
+ break;
+ case 'h': /* bh= AMS body hash */
+ s = skip_fws(++s); /* FWS */
+ if (*s != '=') return US"no bh value";
+ if (al->bh.data) return US"already bh data";
+
+ /* The bodyhash can have FWS inserted in the content;
+ make a stripped copy */
+
+ while ((c = *++s) && c != ';')
+ if (c != ' ' && c != '\t' && c != '\n' && c != '\r')
+ g = string_catn(g, s, 1);
+ if (!g) return US"no bh= value";
+ al->bh.data = string_from_gstring(g);
+ al->bh.len = g->ptr;
+ gstring_release_unused(g);
+ break;
+ default:
+ return US"b? tag";
+ }
+ }
+ break;
+ case 'c':
+ switch (*s)
+ {
+ case '=': /* c= AMS canonicalisation */
+ if (arc_insert_tagvalue(al, offsetof(arc_line, c), &s)) return US"c tag dup";
+
+ /* substructure: head/body (eg. relaxed/simple)) */
+
+ t = al->c_head.data = al->c.data;
+ while (isalpha(*t))
+ if (!*t++ || ++i > al->a.len) break;
+ al->c_head.len = i;
+ if (*t++ == '/') /* /body is optional */
+ {
+ al->c_body.data = t;
+ al->c_body.len = al->c.len - i - 1;
+ }
+ else
+ {
+ al->c_body.data = US"simple";
+ al->c_body.len = 6;
+ }
+ break;
+ case 'v': /* cv= AS validity */
+ if (*++s != '=') return US"cv tag val";
+ if (arc_insert_tagvalue(al, offsetof(arc_line, cv), &s)) return US"cv tag dup";
+ break;
+ default:
+ return US"c? tag";
+ }
+ break;
+ case 'd': /* d= AMS domain */
+ if (*s != '=') return US"d tag val";
+ if (arc_insert_tagvalue(al, offsetof(arc_line, d), &s)) return US"d tag dup";
+ break;
+ case 'h': /* h= AMS headers */
+ if (*s != '=') return US"h tag val";
+ if (arc_insert_tagvalue(al, offsetof(arc_line, h), &s)) return US"h tag dup";
+ break;
+ case 'i': /* i= ARC set instance */
+ if (*s != '=') return US"i tag val";
+ if (arc_insert_tagvalue(al, offsetof(arc_line, i), &s)) return US"i tag dup";
+ if (instance_only) goto done;
+ break;
+ case 'l': /* l= bodylength */
+ if (*s != '=') return US"l tag val";
+ if (arc_insert_tagvalue(al, offsetof(arc_line, l), &s)) return US"l tag dup";
+ break;
+ case 's': /* s= AMS selector */
+ if (*s != '=') return US"s tag val";
+ if (arc_insert_tagvalue(al, offsetof(arc_line, s), &s)) return US"s tag dup";
+ break;
+ }
+
+ while ((c = *s) && c != ';') s++;
+ if (c) s++; /* ; after tag-spec */
+
+ /* for all but the b= tag, copy the field including FWS. For the b=,
+ drop the tag content. */
+
+ if (!instance_only)
+ if (bstart)
+ {
+ size_t n = bstart - fieldstart;
+ memcpy(r, fieldstart, n); /* FWS "b=" */
+ r += n;
+ al->rawsig_no_b_val.len += n;
+ n = s - bend;
+ memcpy(r, bend, n); /* FWS ";" */
+ r += n;
+ al->rawsig_no_b_val.len += n;
+ }
+ else
+ {
+ size_t n = s - fieldstart;
+ memcpy(r, fieldstart, n);
+ r += n;
+ al->rawsig_no_b_val.len += n;
+ }
+ }
+
+if (!instance_only)
+ *r = '\0';
+
+done:
+/* debug_printf("%s: finshed\n", __FUNCTION__); */
+return NULL;
+}
+
+
+/* Insert one header line in the correct set of the chain,
+adding instances as needed and checking for duplicate lines.
+*/
+
+static uschar *
+arc_insert_hdr(arc_ctx * ctx, header_line * h, unsigned off, unsigned hoff,
+ BOOL instance_only, arc_line ** alp_ret)
+{
+unsigned i;
+arc_set * as;
+arc_line * al = store_get(sizeof(arc_line), GET_UNTAINTED), ** alp;
+uschar * e;
+
+memset(al, 0, sizeof(arc_line));
+
+if ((e = arc_parse_line(al, h, off, instance_only)))
+ {
+ DEBUG(D_acl) if (e) debug_printf("ARC: %s\n", e);
+ return US"line parse";
+ }
+if (!(i = arc_instance_from_hdr(al))) return US"instance find";
+if (i > 50) return US"overlarge instance number";
+if (!(as = arc_find_set(ctx, i))) return US"set find";
+if (*(alp = (arc_line **)(US as + hoff))) return US"dup hdr";
+
+*alp = al;
+if (alp_ret) *alp_ret = al;
+return NULL;
+}
+
+
+
+
+static const uschar *
+arc_try_header(arc_ctx * ctx, header_line * h, BOOL instance_only)
+{
+const uschar * e;
+
+/*debug_printf("consider hdr '%s'\n", h->text);*/
+if (strncmpic(ARC_HDR_AAR, h->text, ARC_HDRLEN_AAR) == 0)
+ {
+ DEBUG(D_acl)
+ {
+ int len = h->slen;
+ uschar * s;
+ for (s = h->text + h->slen; s[-1] == '\r' || s[-1] == '\n'; )
+ s--, len--;
+ debug_printf("ARC: found AAR: %.*s\n", len, h->text);
+ }
+ if ((e = arc_insert_hdr(ctx, h, ARC_HDRLEN_AAR, offsetof(arc_set, hdr_aar),
+ TRUE, NULL)))
+ {
+ DEBUG(D_acl) debug_printf("inserting AAR: %s\n", e);
+ return US"inserting AAR";
+ }
+ }
+else if (strncmpic(ARC_HDR_AMS, h->text, ARC_HDRLEN_AMS) == 0)
+ {
+ arc_line * ams;
+
+ DEBUG(D_acl)
+ {
+ int len = h->slen;
+ uschar * s;
+ for (s = h->text + h->slen; s[-1] == '\r' || s[-1] == '\n'; )
+ s--, len--;
+ debug_printf("ARC: found AMS: %.*s\n", len, h->text);
+ }
+ if ((e = arc_insert_hdr(ctx, h, ARC_HDRLEN_AMS, offsetof(arc_set, hdr_ams),
+ instance_only, &ams)))
+ {
+ DEBUG(D_acl) debug_printf("inserting AMS: %s\n", e);
+ return US"inserting AMS";
+ }
+
+ /* defaults */
+ if (!ams->c.data)
+ {
+ ams->c_head.data = US"simple"; ams->c_head.len = 6;
+ ams->c_body = ams->c_head;
+ }
+ }
+else if (strncmpic(ARC_HDR_AS, h->text, ARC_HDRLEN_AS) == 0)
+ {
+ DEBUG(D_acl)
+ {
+ int len = h->slen;
+ uschar * s;
+ for (s = h->text + h->slen; s[-1] == '\r' || s[-1] == '\n'; )
+ s--, len--;
+ debug_printf("ARC: found AS: %.*s\n", len, h->text);
+ }
+ if ((e = arc_insert_hdr(ctx, h, ARC_HDRLEN_AS, offsetof(arc_set, hdr_as),
+ instance_only, NULL)))
+ {
+ DEBUG(D_acl) debug_printf("inserting AS: %s\n", e);
+ return US"inserting AS";
+ }
+ }
+return NULL;
+}
+
+
+
+/* Gather the chain of arc sets from the headers.
+Check for duplicates while that is done. Also build the
+reverse-order headers list;
+
+Return: ARC state if determined, eg. by lack of any ARC chain.
+*/
+
+static const uschar *
+arc_vfy_collect_hdrs(arc_ctx * ctx)
+{
+header_line * h;
+hdr_rlist * r = NULL, * rprev = NULL;
+const uschar * e;
+
+DEBUG(D_acl) debug_printf("ARC: collecting arc sets\n");
+for (h = header_list; h; h = h->next)
+ {
+ r = store_get(sizeof(hdr_rlist), GET_UNTAINTED);
+ r->prev = rprev;
+ r->used = FALSE;
+ r->h = h;
+ rprev = r;
+
+ if ((e = arc_try_header(ctx, h, FALSE)))
+ {
+ arc_state_reason = string_sprintf("collecting headers: %s", e);
+ return US"fail";
+ }
+ }
+headers_rlist = r;
+
+if (!ctx->arcset_chain) return US"none";
+return NULL;
+}
+
+
+static BOOL
+arc_cv_match(arc_line * al, const uschar * s)
+{
+return Ustrncmp(s, al->cv.data, al->cv.len) == 0;
+}
+
+/******************************************************************************/
+
+/* Return the hash of headers from the message that the AMS claims it
+signed.
+*/
+
+static void
+arc_get_verify_hhash(arc_ctx * ctx, arc_line * ams, blob * hhash)
+{
+const uschar * headernames = string_copyn(ams->h.data, ams->h.len);
+const uschar * hn;
+int sep = ':';
+hdr_rlist * r;
+BOOL relaxed = Ustrncmp(US"relaxed", ams->c_head.data, ams->c_head.len) == 0;
+int hashtype = pdkim_hashname_to_hashtype(
+ ams->a_hash.data, ams->a_hash.len);
+hctx hhash_ctx;
+const uschar * s;
+int len;
+
+if ( hashtype == -1
+ || !exim_sha_init(&hhash_ctx, pdkim_hashes[hashtype].exim_hashmethod))
+ {
+ DEBUG(D_acl)
+ debug_printf("ARC: hash setup error, possibly nonhandled hashtype\n");
+ return;
+ }
+
+/* For each headername in the list from the AMS (walking in order)
+walk the message headers in reverse order, adding to the hash any
+found for the first time. For that last point, maintain used-marks
+on the list of message headers. */
+
+DEBUG(D_acl) debug_printf("ARC: AMS header data for verification:\n");
+
+for (r = headers_rlist; r; r = r->prev)
+ r->used = FALSE;
+while ((hn = string_nextinlist(&headernames, &sep, NULL, 0)))
+ for (r = headers_rlist; r; r = r->prev)
+ if ( !r->used
+ && strncasecmp(CCS (s = r->h->text), CCS hn, Ustrlen(hn)) == 0
+ )
+ {
+ if (relaxed) s = pdkim_relax_header_n(s, r->h->slen, TRUE);
+
+ len = Ustrlen(s);
+ DEBUG(D_acl) pdkim_quoteprint(s, len);
+ exim_sha_update_string(&hhash_ctx, s);
+ r->used = TRUE;
+ break;
+ }
+
+/* Finally add in the signature header (with the b= tag stripped); no CRLF */
+
+s = ams->rawsig_no_b_val.data, len = ams->rawsig_no_b_val.len;
+if (relaxed)
+ len = Ustrlen(s = pdkim_relax_header_n(s, len, FALSE));
+DEBUG(D_acl) pdkim_quoteprint(s, len);
+exim_sha_update(&hhash_ctx, s, len);
+
+exim_sha_finish(&hhash_ctx, hhash);
+DEBUG(D_acl)
+ { debug_printf("ARC: header hash: "); pdkim_hexprint(hhash->data, hhash->len); }
+return;
+}
+
+
+
+
+static pdkim_pubkey *
+arc_line_to_pubkey(arc_line * al)
+{
+uschar * dns_txt;
+pdkim_pubkey * p;
+
+if (!(dns_txt = dkim_exim_query_dns_txt(string_sprintf("%.*s._domainkey.%.*s",
+ (int)al->s.len, al->s.data, (int)al->d.len, al->d.data))))
+ {
+ DEBUG(D_acl) debug_printf("pubkey dns lookup fail\n");
+ return NULL;
+ }
+
+if ( !(p = pdkim_parse_pubkey_record(dns_txt))
+ || (Ustrcmp(p->srvtype, "*") != 0 && Ustrcmp(p->srvtype, "email") != 0)
+ )
+ {
+ DEBUG(D_acl) debug_printf("pubkey dns lookup format error\n");
+ return NULL;
+ }
+
+/* If the pubkey limits use to specified hashes, reject unusable
+signatures. XXX should we have looked for multiple dns records? */
+
+if (p->hashes)
+ {
+ const uschar * list = p->hashes, * ele;
+ int sep = ':';
+
+ while ((ele = string_nextinlist(&list, &sep, NULL, 0)))
+ if (Ustrncmp(ele, al->a_hash.data, al->a_hash.len) == 0) break;
+ if (!ele)
+ {
+ DEBUG(D_acl) debug_printf("pubkey h=%s vs sig a=%.*s\n",
+ p->hashes, (int)al->a.len, al->a.data);
+ return NULL;
+ }
+ }
+return p;
+}
+
+
+
+
+static pdkim_bodyhash *
+arc_ams_setup_vfy_bodyhash(arc_line * ams)
+{
+int canon_head = -1, canon_body = -1;
+long bodylen;
+
+if (!ams->c.data) ams->c.data = US"simple"; /* RFC 6376 (DKIM) default */
+pdkim_cstring_to_canons(ams->c.data, ams->c.len, &canon_head, &canon_body);
+bodylen = ams->l.data
+ ? strtol(CS string_copyn(ams->l.data, ams->l.len), NULL, 10) : -1;
+
+return pdkim_set_bodyhash(dkim_verify_ctx,
+ pdkim_hashname_to_hashtype(ams->a_hash.data, ams->a_hash.len),
+ canon_body,
+ bodylen);
+}
+
+
+
+/* Verify an AMS. This is a DKIM-sig header, but with an ARC i= tag
+and without a DKIM v= tag.
+*/
+
+static const uschar *
+arc_ams_verify(arc_ctx * ctx, arc_set * as)
+{
+arc_line * ams = as->hdr_ams;
+pdkim_bodyhash * b;
+pdkim_pubkey * p;
+blob sighash;
+blob hhash;
+ev_ctx vctx;
+int hashtype;
+const uschar * errstr;
+
+as->ams_verify_done = US"in-progress";
+
+/* Check the AMS has all the required tags:
+ "a=" algorithm
+ "b=" signature
+ "bh=" body hash
+ "d=" domain (for key lookup)
+ "h=" headers (included in signature)
+ "s=" key-selector (for key lookup)
+*/
+if ( !ams->a.data || !ams->b.data || !ams->bh.data || !ams->d.data
+ || !ams->h.data || !ams->s.data)
+ {
+ as->ams_verify_done = arc_state_reason = US"required tag missing";
+ return US"fail";
+ }
+
+
+/* The bodyhash should have been created earlier, and the dkim code should
+have managed calculating it during message input. Find the reference to it. */
+
+if (!(b = arc_ams_setup_vfy_bodyhash(ams)))
+ {
+ as->ams_verify_done = arc_state_reason = US"internal hash setup error";
+ return US"fail";
+ }
+
+DEBUG(D_acl)
+ {
+ debug_printf("ARC i=%d AMS Body bytes hashed: %lu\n"
+ " Body %.*s computed: ",
+ as->instance, b->signed_body_bytes,
+ (int)ams->a_hash.len, ams->a_hash.data);
+ pdkim_hexprint(CUS b->bh.data, b->bh.len);
+ }
+
+/* We know the bh-tag blob is of a nul-term string, so safe as a string */
+
+if ( !ams->bh.data
+ || (pdkim_decode_base64(ams->bh.data, &sighash), sighash.len != b->bh.len)
+ || memcmp(sighash.data, b->bh.data, b->bh.len) != 0
+ )
+ {
+ DEBUG(D_acl)
+ {
+ debug_printf("ARC i=%d AMS Body hash from headers: ", as->instance);
+ pdkim_hexprint(sighash.data, sighash.len);
+ debug_printf("ARC i=%d AMS Body hash did NOT match\n", as->instance);
+ }
+ return as->ams_verify_done = arc_state_reason = US"AMS body hash miscompare";
+ }
+
+DEBUG(D_acl) debug_printf("ARC i=%d AMS Body hash compared OK\n", as->instance);
+
+/* Get the public key from DNS */
+
+if (!(p = arc_line_to_pubkey(ams)))
+ return as->ams_verify_done = arc_state_reason = US"pubkey problem";
+
+/* We know the b-tag blob is of a nul-term string, so safe as a string */
+pdkim_decode_base64(ams->b.data, &sighash);
+
+arc_get_verify_hhash(ctx, ams, &hhash);
+
+/* Setup the interface to the signing library */
+
+if ((errstr = exim_dkim_verify_init(&p->key, KEYFMT_DER, &vctx, NULL)))
+ {
+ DEBUG(D_acl) debug_printf("ARC verify init: %s\n", errstr);
+ as->ams_verify_done = arc_state_reason = US"internal sigverify init error";
+ return US"fail";
+ }
+
+hashtype = pdkim_hashname_to_hashtype(ams->a_hash.data, ams->a_hash.len);
+if (hashtype == -1)
+ {
+ DEBUG(D_acl) debug_printf("ARC i=%d AMS verify bad a_hash\n", as->instance);
+ return as->ams_verify_done = arc_state_reason = US"AMS sig nonverify";
+ }
+
+if ((errstr = exim_dkim_verify(&vctx,
+ pdkim_hashes[hashtype].exim_hashmethod, &hhash, &sighash)))
+ {
+ DEBUG(D_acl) debug_printf("ARC i=%d AMS verify %s\n", as->instance, errstr);
+ return as->ams_verify_done = arc_state_reason = US"AMS sig nonverify";
+ }
+
+DEBUG(D_acl) debug_printf("ARC i=%d AMS verify pass\n", as->instance);
+as->ams_verify_passed = TRUE;
+return NULL;
+}
+
+
+
+/* Check the sets are instance-continuous and that all
+members are present. Check that no arc_seals are "fail".
+Set the highest instance number global.
+Verify the latest AMS.
+*/
+static uschar *
+arc_headers_check(arc_ctx * ctx)
+{
+arc_set * as;
+int inst;
+BOOL ams_fail_found = FALSE;
+
+if (!(as = ctx->arcset_chain_last))
+ return US"none";
+
+for(inst = as->instance; as; as = as->prev, inst--)
+ {
+ if (as->instance != inst)
+ arc_state_reason = string_sprintf("i=%d (sequence; expected %d)",
+ as->instance, inst);
+ else if (!as->hdr_aar || !as->hdr_ams || !as->hdr_as)
+ arc_state_reason = string_sprintf("i=%d (missing header)", as->instance);
+ else if (arc_cv_match(as->hdr_as, US"fail"))
+ arc_state_reason = string_sprintf("i=%d (cv)", as->instance);
+ else
+ goto good;
+
+ DEBUG(D_acl) debug_printf("ARC chain fail at %s\n", arc_state_reason);
+ return US"fail";
+
+ good:
+ /* Evaluate the oldest-pass AMS validation while we're here.
+ It does not affect the AS chain validation but is reported as
+ auxilary info. */
+
+ if (!ams_fail_found)
+ if (arc_ams_verify(ctx, as))
+ ams_fail_found = TRUE;
+ else
+ arc_oldest_pass = inst;
+ arc_state_reason = NULL;
+ }
+if (inst != 0)
+ {
+ arc_state_reason = string_sprintf("(sequence; expected i=%d)", inst);
+ DEBUG(D_acl) debug_printf("ARC chain fail %s\n", arc_state_reason);
+ return US"fail";
+ }
+
+arc_received = ctx->arcset_chain_last;
+arc_received_instance = arc_received->instance;
+
+/* We can skip the latest-AMS validation, if we already did it. */
+
+as = ctx->arcset_chain_last;
+if (!as->ams_verify_passed)
+ {
+ if (as->ams_verify_done)
+ {
+ arc_state_reason = as->ams_verify_done;
+ return US"fail";
+ }
+ if (!!arc_ams_verify(ctx, as))
+ return US"fail";
+ }
+return NULL;
+}
+
+
+/******************************************************************************/
+static const uschar *
+arc_seal_verify(arc_ctx * ctx, arc_set * as)
+{
+arc_line * hdr_as = as->hdr_as;
+arc_set * as2;
+int hashtype;
+hctx hhash_ctx;
+blob hhash_computed;
+blob sighash;
+ev_ctx vctx;
+pdkim_pubkey * p;
+const uschar * errstr;
+
+DEBUG(D_acl) debug_printf("ARC: AS vfy i=%d\n", as->instance);
+/*
+ 1. If the value of the "cv" tag on that seal is "fail", the
+ chain state is "fail" and the algorithm stops here. (This
+ step SHOULD be skipped if the earlier step (2.1) was
+ performed) [it was]
+
+ 2. In Boolean nomenclature: if ((i == 1 && cv != "none") or (cv
+ == "none" && i != 1)) then the chain state is "fail" and the
+ algorithm stops here (note that the ordering of the logic is
+ structured for short-circuit evaluation).
+*/
+
+if ( as->instance == 1 && !arc_cv_match(hdr_as, US"none")
+ || arc_cv_match(hdr_as, US"none") && as->instance != 1
+ )
+ {
+ arc_state_reason = US"seal cv state";
+ return US"fail";
+ }
+
+/*
+ 3. Initialize a hash function corresponding to the "a" tag of
+ the ARC-Seal.
+*/
+
+hashtype = pdkim_hashname_to_hashtype(hdr_as->a_hash.data, hdr_as->a_hash.len);
+
+if ( hashtype == -1
+ || !exim_sha_init(&hhash_ctx, pdkim_hashes[hashtype].exim_hashmethod))
+ {
+ DEBUG(D_acl)
+ debug_printf("ARC: hash setup error, possibly nonhandled hashtype\n");
+ arc_state_reason = US"seal hash setup error";
+ return US"fail";
+ }
+
+/*
+ 4. Compute the canonicalized form of the ARC header fields, in
+ the order described in Section 5.4.2, using the "relaxed"
+ header canonicalization defined in Section 3.4.2 of
+ [RFC6376]. Pass the canonicalized result to the hash
+ function.
+
+Headers are CRLF-separated, but the last one is not crlf-terminated.
+*/
+
+DEBUG(D_acl) debug_printf("ARC: AS header data for verification:\n");
+for (as2 = ctx->arcset_chain;
+ as2 && as2->instance <= as->instance;
+ as2 = as2->next)
+ {
+ arc_line * al;
+ uschar * s;
+ int len;
+
+ al = as2->hdr_aar;
+ if (!(s = al->relaxed))
+ al->relaxed = s = pdkim_relax_header_n(al->complete->text,
+ al->complete->slen, TRUE);
+ len = Ustrlen(s);
+ DEBUG(D_acl) pdkim_quoteprint(s, len);
+ exim_sha_update(&hhash_ctx, s, len);
+
+ al = as2->hdr_ams;
+ if (!(s = al->relaxed))
+ al->relaxed = s = pdkim_relax_header_n(al->complete->text,
+ al->complete->slen, TRUE);
+ len = Ustrlen(s);
+ DEBUG(D_acl) pdkim_quoteprint(s, len);
+ exim_sha_update(&hhash_ctx, s, len);
+
+ al = as2->hdr_as;
+ if (as2->instance == as->instance)
+ s = pdkim_relax_header_n(al->rawsig_no_b_val.data,
+ al->rawsig_no_b_val.len, FALSE);
+ else if (!(s = al->relaxed))
+ al->relaxed = s = pdkim_relax_header_n(al->complete->text,
+ al->complete->slen, TRUE);
+ len = Ustrlen(s);
+ DEBUG(D_acl) pdkim_quoteprint(s, len);
+ exim_sha_update(&hhash_ctx, s, len);
+ }
+
+/*
+ 5. Retrieve the final digest from the hash function.
+*/
+
+exim_sha_finish(&hhash_ctx, &hhash_computed);
+DEBUG(D_acl)
+ {
+ debug_printf("ARC i=%d AS Header %.*s computed: ",
+ as->instance, (int)hdr_as->a_hash.len, hdr_as->a_hash.data);
+ pdkim_hexprint(hhash_computed.data, hhash_computed.len);
+ }
+
+
+/*
+ 6. Retrieve the public key identified by the "s" and "d" tags in
+ the ARC-Seal, as described in Section 4.1.6.
+*/
+
+if (!(p = arc_line_to_pubkey(hdr_as)))
+ return US"pubkey problem";
+
+/*
+ 7. Determine whether the signature portion ("b" tag) of the ARC-
+ Seal and the digest computed above are valid according to the
+ public key. (See also Section Section 8.4 for failure case
+ handling)
+
+ 8. If the signature is not valid, the chain state is "fail" and
+ the algorithm stops here.
+*/
+
+/* We know the b-tag blob is of a nul-term string, so safe as a string */
+pdkim_decode_base64(hdr_as->b.data, &sighash);
+
+if ((errstr = exim_dkim_verify_init(&p->key, KEYFMT_DER, &vctx, NULL)))
+ {
+ DEBUG(D_acl) debug_printf("ARC verify init: %s\n", errstr);
+ return US"fail";
+ }
+
+if ((errstr = exim_dkim_verify(&vctx,
+ pdkim_hashes[hashtype].exim_hashmethod,
+ &hhash_computed, &sighash)))
+ {
+ DEBUG(D_acl)
+ debug_printf("ARC i=%d AS headers verify: %s\n", as->instance, errstr);
+ arc_state_reason = US"seal sigverify error";
+ return US"fail";
+ }
+
+DEBUG(D_acl) debug_printf("ARC: AS vfy i=%d pass\n", as->instance);
+return NULL;
+}
+
+
+static const uschar *
+arc_verify_seals(arc_ctx * ctx)
+{
+arc_set * as = ctx->arcset_chain_last;
+
+if (!as)
+ return US"none";
+
+for ( ; as; as = as->prev) if (arc_seal_verify(ctx, as)) return US"fail";
+
+DEBUG(D_acl) debug_printf("ARC: AS vfy overall pass\n");
+return NULL;
+}
+/******************************************************************************/
+
+/* Do ARC verification. Called from DATA ACL, on a verify = arc
+condition. No arguments; we are checking globals.
+
+Return: The ARC state, or NULL on error.
+*/
+
+const uschar *
+acl_verify_arc(void)
+{
+const uschar * res;
+
+memset(&arc_verify_ctx, 0, sizeof(arc_verify_ctx));
+
+if (!dkim_verify_ctx)
+ {
+ DEBUG(D_acl) debug_printf("ARC: no DKIM verify context\n");
+ return NULL;
+ }
+
+/* AS evaluation, per
+https://tools.ietf.org/html/draft-ietf-dmarc-arc-protocol-10#section-6
+*/
+/* 1. Collect all ARC sets currently on the message. If there were
+ none, the ARC state is "none" and the algorithm stops here.
+*/
+
+if ((res = arc_vfy_collect_hdrs(&arc_verify_ctx)))
+ goto out;
+
+/* 2. If the form of any ARC set is invalid (e.g., does not contain
+ exactly one of each of the three ARC-specific header fields),
+ then the chain state is "fail" and the algorithm stops here.
+
+ 1. To avoid the overhead of unnecessary computation and delay
+ from crypto and DNS operations, the cv value for all ARC-
+ Seal(s) MAY be checked at this point. If any of the values
+ are "fail", then the overall state of the chain is "fail" and
+ the algorithm stops here.
+
+ 3. Conduct verification of the ARC-Message-Signature header field
+ bearing the highest instance number. If this verification fails,
+ then the chain state is "fail" and the algorithm stops here.
+*/
+
+if ((res = arc_headers_check(&arc_verify_ctx)))
+ goto out;
+
+/* 4. For each ARC-Seal from the "N"th instance to the first, apply the
+ following logic:
+
+ 1. If the value of the "cv" tag on that seal is "fail", the
+ chain state is "fail" and the algorithm stops here. (This
+ step SHOULD be skipped if the earlier step (2.1) was
+ performed)
+
+ 2. In Boolean nomenclature: if ((i == 1 && cv != "none") or (cv
+ == "none" && i != 1)) then the chain state is "fail" and the
+ algorithm stops here (note that the ordering of the logic is
+ structured for short-circuit evaluation).
+
+ 3. Initialize a hash function corresponding to the "a" tag of
+ the ARC-Seal.
+
+ 4. Compute the canonicalized form of the ARC header fields, in
+ the order described in Section 5.4.2, using the "relaxed"
+ header canonicalization defined in Section 3.4.2 of
+ [RFC6376]. Pass the canonicalized result to the hash
+ function.
+
+ 5. Retrieve the final digest from the hash function.
+
+ 6. Retrieve the public key identified by the "s" and "d" tags in
+ the ARC-Seal, as described in Section 4.1.6.
+
+ 7. Determine whether the signature portion ("b" tag) of the ARC-
+ Seal and the digest computed above are valid according to the
+ public key. (See also Section Section 8.4 for failure case
+ handling)
+
+ 8. If the signature is not valid, the chain state is "fail" and
+ the algorithm stops here.
+
+ 5. If all seals pass validation, then the chain state is "pass", and
+ the algorithm is complete.
+*/
+
+if ((res = arc_verify_seals(&arc_verify_ctx)))
+ goto out;
+
+res = US"pass";
+
+out:
+ return res;
+}
+
+/******************************************************************************/
+
+/* Prepend the header to the rlist */
+
+static hdr_rlist *
+arc_rlist_entry(hdr_rlist * list, const uschar * s, int len)
+{
+hdr_rlist * r = store_get(sizeof(hdr_rlist) + sizeof(header_line), GET_UNTAINTED);
+header_line * h = r->h = (header_line *)(r+1);
+
+r->prev = list;
+r->used = FALSE;
+h->next = NULL;
+h->type = 0;
+h->slen = len;
+h->text = US s;
+
+return r;
+}
+
+
+/* Walk the given headers strings identifying each header, and construct
+a reverse-order list.
+*/
+
+static hdr_rlist *
+arc_sign_scan_headers(arc_ctx * ctx, gstring * sigheaders)
+{
+const uschar * s;
+hdr_rlist * rheaders = NULL;
+
+s = sigheaders ? sigheaders->s : NULL;
+if (s) while (*s)
+ {
+ const uschar * s2 = s;
+
+ /* This works for either NL or CRLF lines; also nul-termination */
+ while (*++s2)
+ if (*s2 == '\n' && s2[1] != '\t' && s2[1] != ' ') break;
+ s2++; /* move past end of line */
+
+ rheaders = arc_rlist_entry(rheaders, s, s2 - s);
+ s = s2;
+ }
+return rheaders;
+}
+
+
+
+/* Return the A-R content, without identity, with line-ending and
+NUL termination. */
+
+static BOOL
+arc_sign_find_ar(header_line * headers, const uschar * identity, blob * ret)
+{
+header_line * h;
+int ilen = Ustrlen(identity);
+
+ret->data = NULL;
+for(h = headers; h; h = h->next)
+ {
+ uschar * s = h->text, c;
+ int len = h->slen;
+
+ if (Ustrncmp(s, HDR_AR, HDRLEN_AR) != 0) continue;
+ s += HDRLEN_AR, len -= HDRLEN_AR; /* header name */
+ while ( len > 0
+ && (c = *s) && (c == ' ' || c == '\t' || c == '\r' || c == '\n'))
+ s++, len--; /* FWS */
+ if (Ustrncmp(s, identity, ilen) != 0) continue;
+ s += ilen; len -= ilen; /* identity */
+ if (len <= 0) continue;
+ if ((c = *s) && c == ';') s++, len--; /* identity terminator */
+ while ( len > 0
+ && (c = *s) && (c == ' ' || c == '\t' || c == '\r' || c == '\n'))
+ s++, len--; /* FWS */
+ if (len <= 0) continue;
+ ret->data = s;
+ ret->len = len;
+ return TRUE;
+ }
+return FALSE;
+}
+
+
+
+/* Append a constructed AAR including CRLF. Add it to the arc_ctx too. */
+
+static gstring *
+arc_sign_append_aar(gstring * g, arc_ctx * ctx,
+ const uschar * identity, int instance, blob * ar)
+{
+int aar_off = gstring_length(g);
+arc_set * as =
+ store_get(sizeof(arc_set) + sizeof(arc_line) + sizeof(header_line), GET_UNTAINTED);
+arc_line * al = (arc_line *)(as+1);
+header_line * h = (header_line *)(al+1);
+
+g = string_catn(g, ARC_HDR_AAR, ARC_HDRLEN_AAR);
+g = string_fmt_append(g, " i=%d; %s;\r\n\t", instance, identity);
+g = string_catn(g, US ar->data, ar->len);
+
+h->slen = g->ptr - aar_off;
+h->text = g->s + aar_off;
+al->complete = h;
+as->next = NULL;
+as->prev = ctx->arcset_chain_last;
+as->instance = instance;
+as->hdr_aar = al;
+if (instance == 1)
+ ctx->arcset_chain = as;
+else
+ ctx->arcset_chain_last->next = as;
+ctx->arcset_chain_last = as;
+
+DEBUG(D_transport) debug_printf("ARC: AAR '%.*s'\n", h->slen - 2, h->text);
+return g;
+}
+
+
+
+static BOOL
+arc_sig_from_pseudoheader(gstring * hdata, int hashtype, const uschar * privkey,
+ blob * sig, const uschar * why)
+{
+hashmethod hm = /*sig->keytype == KEYTYPE_ED25519*/ FALSE
+ ? HASH_SHA2_512 : pdkim_hashes[hashtype].exim_hashmethod;
+blob hhash;
+es_ctx sctx;
+const uschar * errstr;
+
+DEBUG(D_transport)
+ {
+ hctx hhash_ctx;
+ debug_printf("ARC: %s header data for signing:\n", why);
+ pdkim_quoteprint(hdata->s, hdata->ptr);
+
+ (void) exim_sha_init(&hhash_ctx, pdkim_hashes[hashtype].exim_hashmethod);
+ exim_sha_update(&hhash_ctx, hdata->s, hdata->ptr);
+ exim_sha_finish(&hhash_ctx, &hhash);
+ debug_printf("ARC: header hash: "); pdkim_hexprint(hhash.data, hhash.len);
+ }
+
+if (FALSE /*need hash for Ed25519 or GCrypt signing*/ )
+ {
+ hctx hhash_ctx;
+ (void) exim_sha_init(&hhash_ctx, pdkim_hashes[hashtype].exim_hashmethod);
+ exim_sha_update(&hhash_ctx, hdata->s, hdata->ptr);
+ exim_sha_finish(&hhash_ctx, &hhash);
+ }
+else
+ {
+ hhash.data = hdata->s;
+ hhash.len = hdata->ptr;
+ }
+
+if ( (errstr = exim_dkim_signing_init(privkey, &sctx))
+ || (errstr = exim_dkim_sign(&sctx, hm, &hhash, sig)))
+ {
+ log_write(0, LOG_MAIN, "ARC: %s signing: %s\n", why, errstr);
+ DEBUG(D_transport)
+ debug_printf("private key, or private-key file content, was: '%s'\n",
+ privkey);
+ return FALSE;
+ }
+return TRUE;
+}
+
+
+
+static gstring *
+arc_sign_append_sig(gstring * g, blob * sig)
+{
+/*debug_printf("%s: raw sig ", __FUNCTION__); pdkim_hexprint(sig->data, sig->len);*/
+sig->data = pdkim_encode_base64(sig);
+sig->len = Ustrlen(sig->data);
+for (;;)
+ {
+ int len = MIN(sig->len, 74);
+ g = string_catn(g, sig->data, len);
+ if ((sig->len -= len) == 0) break;
+ sig->data += len;
+ g = string_catn(g, US"\r\n\t ", 5);
+ }
+g = string_catn(g, US";\r\n", 3);
+gstring_release_unused(g);
+string_from_gstring(g);
+return g;
+}
+
+
+/* Append a constructed AMS including CRLF. Add it to the arc_ctx too. */
+
+static gstring *
+arc_sign_append_ams(gstring * g, arc_ctx * ctx, int instance,
+ const uschar * identity, const uschar * selector, blob * bodyhash,
+ hdr_rlist * rheaders, const uschar * privkey, unsigned options)
+{
+uschar * s;
+gstring * hdata = NULL;
+int col;
+int hashtype = pdkim_hashname_to_hashtype(US"sha256", 6); /*XXX hardwired */
+blob sig;
+int ams_off;
+arc_line * al = store_get(sizeof(header_line) + sizeof(arc_line), GET_UNTAINTED);
+header_line * h = (header_line *)(al+1);
+
+/* debug_printf("%s\n", __FUNCTION__); */
+
+/* Construct the to-be-signed AMS pseudo-header: everything but the sig. */
+
+ams_off = g->ptr;
+g = string_fmt_append(g, "%s i=%d; a=rsa-sha256; c=relaxed; d=%s; s=%s",
+ ARC_HDR_AMS, instance, identity, selector); /*XXX hardwired a= */
+if (options & ARC_SIGN_OPT_TSTAMP)
+ g = string_fmt_append(g, "; t=%lu", (u_long)now);
+if (options & ARC_SIGN_OPT_EXPIRE)
+ g = string_fmt_append(g, "; x=%lu", (u_long)expire);
+g = string_fmt_append(g, ";\r\n\tbh=%s;\r\n\th=",
+ pdkim_encode_base64(bodyhash));
+
+for(col = 3; rheaders; rheaders = rheaders->prev)
+ {
+ const uschar * hnames = US"DKIM-Signature:" PDKIM_DEFAULT_SIGN_HEADERS;
+ uschar * name, * htext = rheaders->h->text;
+ int sep = ':';
+
+ /* Spot headers of interest */
+
+ while ((name = string_nextinlist(&hnames, &sep, NULL, 0)))
+ {
+ int len = Ustrlen(name);
+ if (strncasecmp(CCS htext, CCS name, len) == 0)
+ {
+ /* If too long, fold line in h= field */
+
+ if (col + len > 78) g = string_catn(g, US"\r\n\t ", 5), col = 3;
+
+ /* Add name to h= list */
+
+ g = string_catn(g, name, len);
+ g = string_catn(g, US":", 1);
+ col += len + 1;
+
+ /* Accumulate header for hashing/signing */
+
+ hdata = string_cat(hdata,
+ pdkim_relax_header_n(htext, rheaders->h->slen, TRUE)); /*XXX hardwired */
+ break;
+ }
+ }
+ }
+
+/* Lose the last colon from the h= list */
+
+if (g->s[g->ptr - 1] == ':') g->ptr--;
+
+g = string_catn(g, US";\r\n\tb=;", 7);
+
+/* Include the pseudo-header in the accumulation */
+
+s = pdkim_relax_header_n(g->s + ams_off, g->ptr - ams_off, FALSE);
+hdata = string_cat(hdata, s);
+
+/* Calculate the signature from the accumulation */
+/*XXX does that need further relaxation? there are spaces embedded in the b= strings! */
+
+if (!arc_sig_from_pseudoheader(hdata, hashtype, privkey, &sig, US"AMS"))
+ return NULL;
+
+/* Lose the trailing semicolon from the psuedo-header, and append the signature
+(folded over lines) and termination to complete it. */
+
+g->ptr--;
+g = arc_sign_append_sig(g, &sig);
+
+h->slen = g->ptr - ams_off;
+h->text = g->s + ams_off;
+al->complete = h;
+ctx->arcset_chain_last->hdr_ams = al;
+
+DEBUG(D_transport) debug_printf("ARC: AMS '%.*s'\n", h->slen - 2, h->text);
+return g;
+}
+
+
+
+/* Look for an arc= result in an A-R header blob. We know that its data
+happens to be a NUL-term string. */
+
+static uschar *
+arc_ar_cv_status(blob * ar)
+{
+const uschar * resinfo = ar->data;
+int sep = ';';
+uschar * methodspec, * s;
+
+while ((methodspec = string_nextinlist(&resinfo, &sep, NULL, 0)))
+ if (Ustrncmp(methodspec, US"arc=", 4) == 0)
+ {
+ uschar c;
+ for (s = methodspec += 4;
+ (c = *s) && c != ';' && c != ' ' && c != '\r' && c != '\n'; ) s++;
+ return string_copyn(methodspec, s - methodspec);
+ }
+return US"none";
+}
+
+
+
+/* Build the AS header and prepend it */
+
+static gstring *
+arc_sign_prepend_as(gstring * arcset_interim, arc_ctx * ctx,
+ int instance, const uschar * identity, const uschar * selector, blob * ar,
+ const uschar * privkey, unsigned options)
+{
+gstring * arcset;
+uschar * status = arc_ar_cv_status(ar);
+arc_line * al = store_get(sizeof(header_line) + sizeof(arc_line), GET_UNTAINTED);
+header_line * h = (header_line *)(al+1);
+uschar * badline_str;
+
+gstring * hdata = NULL;
+int hashtype = pdkim_hashname_to_hashtype(US"sha256", 6); /*XXX hardwired */
+blob sig;
+
+/*
+- Generate AS
+ - no body coverage
+ - no h= tag; implicit coverage
+ - arc status from A-R
+ - if fail:
+ - coverage is just the new ARC set
+ including self (but with an empty b= in self)
+ - if non-fail:
+ - all ARC set headers, set-number order, aar then ams then as,
+ including self (but with an empty b= in self)
+*/
+DEBUG(D_transport) debug_printf("ARC: building AS for status '%s'\n", status);
+
+/* Construct the AS except for the signature */
+
+arcset = string_append(NULL, 9,
+ ARC_HDR_AS,
+ US" i=", string_sprintf("%d", instance),
+ US"; cv=", status,
+ US"; a=rsa-sha256; d=", identity, /*XXX hardwired */
+ US"; s=", selector); /*XXX same as AMS */
+if (options & ARC_SIGN_OPT_TSTAMP)
+ arcset = string_append(arcset, 2,
+ US"; t=", string_sprintf("%lu", (u_long)now));
+arcset = string_cat(arcset,
+ US";\r\n\t b=;");
+
+h->slen = arcset->ptr;
+h->text = arcset->s;
+al->complete = h;
+ctx->arcset_chain_last->hdr_as = al;
+
+/* For any but "fail" chain-verify status, walk the entire chain in order by
+instance. For fail, only the new arc-set. Accumulate the elements walked. */
+
+for (arc_set * as = Ustrcmp(status, US"fail") == 0
+ ? ctx->arcset_chain_last : ctx->arcset_chain;
+ as; as = as->next)
+ {
+ arc_line * l;
+ /* Accumulate AAR then AMS then AS. Relaxed canonicalisation
+ is required per standard. */
+
+ badline_str = US"aar";
+ if (!(l = as->hdr_aar)) goto badline;
+ h = l->complete;
+ hdata = string_cat(hdata, pdkim_relax_header_n(h->text, h->slen, TRUE));
+ badline_str = US"ams";
+ if (!(l = as->hdr_ams)) goto badline;
+ h = l->complete;
+ hdata = string_cat(hdata, pdkim_relax_header_n(h->text, h->slen, TRUE));
+ badline_str = US"as";
+ if (!(l = as->hdr_as)) goto badline;
+ h = l->complete;
+ hdata = string_cat(hdata, pdkim_relax_header_n(h->text, h->slen, !!as->next));
+ }
+
+/* Calculate the signature from the accumulation */
+
+if (!arc_sig_from_pseudoheader(hdata, hashtype, privkey, &sig, US"AS"))
+ return NULL;
+
+/* Lose the trailing semicolon */
+arcset->ptr--;
+arcset = arc_sign_append_sig(arcset, &sig);
+DEBUG(D_transport) debug_printf("ARC: AS '%.*s'\n", arcset->ptr - 2, arcset->s);
+
+/* Finally, append the AMS and AAR to the new AS */
+
+return string_catn(arcset, arcset_interim->s, arcset_interim->ptr);
+
+badline:
+ DEBUG(D_transport)
+ debug_printf("ARC: while building AS, missing %s in chain\n", badline_str);
+ return NULL;
+}
+
+
+/**************************************/
+
+/* Return pointer to pdkim_bodyhash for given hash method, creating new
+method if needed.
+*/
+
+void *
+arc_ams_setup_sign_bodyhash(void)
+{
+int canon_head, canon_body;
+
+DEBUG(D_transport) debug_printf("ARC: requesting bodyhash\n");
+pdkim_cstring_to_canons(US"relaxed", 7, &canon_head, &canon_body); /*XXX hardwired */
+return pdkim_set_bodyhash(&dkim_sign_ctx,
+ pdkim_hashname_to_hashtype(US"sha256", 6), /*XXX hardwired */
+ canon_body,
+ -1);
+}
+
+
+
+void
+arc_sign_init(void)
+{
+memset(&arc_sign_ctx, 0, sizeof(arc_sign_ctx));
+headers_rlist = NULL;
+}
+
+
+
+/* A "normal" header line, identified by DKIM processing. These arrive before
+the call to arc_sign(), which carries any newly-created DKIM headers - and
+those go textually before the normal ones in the message.
+
+We have to take the feed from DKIM as, in the transport-filter case, the
+headers are not in memory at the time of the call to arc_sign().
+
+Take a copy of the header and construct a reverse-order list.
+Also parse ARC-chain headers and build the chain struct, retaining pointers
+into the copies.
+*/
+
+static const uschar *
+arc_header_sign_feed(gstring * g)
+{
+uschar * s = string_copyn(g->s, g->ptr);
+headers_rlist = arc_rlist_entry(headers_rlist, s, g->ptr);
+return arc_try_header(&arc_sign_ctx, headers_rlist->h, TRUE);
+}
+
+
+
+/* Per RFCs 6376, 7489 the only allowed chars in either an ADMD id
+or a selector are ALPHA/DIGGIT/'-'/'.'
+
+Check, to help catch misconfigurations such as a missing selector
+element in the arc_sign list.
+*/
+
+static BOOL
+arc_valid_id(const uschar * s)
+{
+for (uschar c; c = *s++; )
+ if (!isalnum(c) && c != '-' && c != '.') return FALSE;
+return TRUE;
+}
+
+
+
+/* ARC signing. Called from the smtp transport, if the arc_sign option is set.
+The dkim_exim_sign() function has already been called, so will have hashed the
+message body for us so long as we requested a hash previously.
+
+Arguments:
+ signspec Three-element colon-sep list: identity, selector, privkey.
+ Optional fourth element: comma-sep list of options.
+ Already expanded
+ sigheaders Any signature headers already generated, eg. by DKIM, or NULL
+ errstr Error string
+
+Return value
+ Set of headers to prepend to the message, including the supplied sigheaders
+ but not the plainheaders.
+*/
+
+gstring *
+arc_sign(const uschar * signspec, gstring * sigheaders, uschar ** errstr)
+{
+const uschar * identity, * selector, * privkey, * opts, * s;
+unsigned options = 0;
+int sep = 0;
+header_line * headers;
+hdr_rlist * rheaders;
+blob ar;
+int instance;
+gstring * g = NULL;
+pdkim_bodyhash * b;
+
+expire = now = 0;
+
+/* Parse the signing specification */
+
+if (!(identity = string_nextinlist(&signspec, &sep, NULL, 0)) || !*identity)
+ { s = US"identity"; goto bad_arg_ret; }
+if (!(selector = string_nextinlist(&signspec, &sep, NULL, 0)) || !*selector)
+ { s = US"selector"; goto bad_arg_ret; }
+if (!(privkey = string_nextinlist(&signspec, &sep, NULL, 0)) || !*privkey)
+ { s = US"privkey"; goto bad_arg_ret; }
+if (!arc_valid_id(identity))
+ { s = US"identity"; goto bad_arg_ret; }
+if (!arc_valid_id(selector))
+ { s = US"selector"; goto bad_arg_ret; }
+if (*privkey == '/' && !(privkey = expand_file_big_buffer(privkey)))
+ goto ret_sigheaders;
+
+if ((opts = string_nextinlist(&signspec, &sep, NULL, 0)))
+ {
+ int osep = ',';
+ while ((s = string_nextinlist(&opts, &osep, NULL, 0)))
+ if (Ustrcmp(s, "timestamps") == 0)
+ {
+ options |= ARC_SIGN_OPT_TSTAMP;
+ if (!now) now = time(NULL);
+ }
+ else if (Ustrncmp(s, "expire", 6) == 0)
+ {
+ options |= ARC_SIGN_OPT_EXPIRE;
+ if (*(s += 6) == '=')
+ if (*++s == '+')
+ {
+ if (!(expire = (time_t)atoi(CS ++s)))
+ expire = ARC_SIGN_DEFAULT_EXPIRE_DELTA;
+ if (!now) now = time(NULL);
+ expire += now;
+ }
+ else
+ expire = (time_t)atol(CS s);
+ else
+ {
+ if (!now) now = time(NULL);
+ expire = now + ARC_SIGN_DEFAULT_EXPIRE_DELTA;
+ }
+ }
+ }
+
+DEBUG(D_transport) debug_printf("ARC: sign for %s\n", identity);
+
+/* Make an rlist of any new DKIM headers, then add the "normals" rlist to it.
+Then scan the list for an A-R header. */
+
+string_from_gstring(sigheaders);
+if ((rheaders = arc_sign_scan_headers(&arc_sign_ctx, sigheaders)))
+ {
+ hdr_rlist ** rp;
+ for (rp = &headers_rlist; *rp; ) rp = &(*rp)->prev;
+ *rp = rheaders;
+ }
+
+/* Finally, build a normal-order headers list */
+/*XXX only needed for hunt-the-AR? */
+/*XXX also, we really should be accepting any number of ADMD-matching ARs */
+ {
+ header_line * hnext = NULL;
+ for (rheaders = headers_rlist; rheaders;
+ hnext = rheaders->h, rheaders = rheaders->prev)
+ rheaders->h->next = hnext;
+ headers = hnext;
+ }
+
+if (!(arc_sign_find_ar(headers, identity, &ar)))
+ {
+ log_write(0, LOG_MAIN, "ARC: no Authentication-Results header for signing");
+ goto ret_sigheaders;
+ }
+
+/* We previously built the data-struct for the existing ARC chain, if any, using a headers
+feed from the DKIM module. Use that to give the instance number for the ARC set we are
+about to build. */
+
+DEBUG(D_transport)
+ if (arc_sign_ctx.arcset_chain_last)
+ debug_printf("ARC: existing chain highest instance: %d\n",
+ arc_sign_ctx.arcset_chain_last->instance);
+ else
+ debug_printf("ARC: no existing chain\n");
+
+instance = arc_sign_ctx.arcset_chain_last ? arc_sign_ctx.arcset_chain_last->instance + 1 : 1;
+
+/*
+- Generate AAR
+ - copy the A-R; prepend i= & identity
+*/
+
+g = arc_sign_append_aar(g, &arc_sign_ctx, identity, instance, &ar);
+
+/*
+- Generate AMS
+ - Looks fairly like a DKIM sig
+ - Cover all DKIM sig headers as well as the usuals
+ - ? oversigning?
+ - Covers the data
+ - we must have requested a suitable bodyhash previously
+*/
+
+b = arc_ams_setup_sign_bodyhash();
+g = arc_sign_append_ams(g, &arc_sign_ctx, instance, identity, selector,
+ &b->bh, headers_rlist, privkey, options);
+
+/*
+- Generate AS
+ - no body coverage
+ - no h= tag; implicit coverage
+ - arc status from A-R
+ - if fail:
+ - coverage is just the new ARC set
+ including self (but with an empty b= in self)
+ - if non-fail:
+ - all ARC set headers, set-number order, aar then ams then as,
+ including self (but with an empty b= in self)
+*/
+
+if (g)
+ g = arc_sign_prepend_as(g, &arc_sign_ctx, instance, identity, selector, &ar,
+ privkey, options);
+
+/* Finally, append the dkim headers and return the lot. */
+
+if (sigheaders) g = string_catn(g, sigheaders->s, sigheaders->ptr);
+
+out:
+ if (!g) return string_get(1);
+ (void) string_from_gstring(g);
+ gstring_release_unused(g);
+ return g;
+
+
+bad_arg_ret:
+ log_write(0, LOG_MAIN, "ARC: bad signing-specification (%s)", s);
+ret_sigheaders:
+ g = sigheaders;
+ goto out;
+}
+
+
+/******************************************************************************/
+
+/* Check to see if the line is an AMS and if so, set up to validate it.
+Called from the DKIM input processing. This must be done now as the message
+body data is hashed during input.
+
+We call the DKIM code to request a body-hash; it has the facility already
+and the hash parameters might be common with other requests.
+*/
+
+static const uschar *
+arc_header_vfy_feed(gstring * g)
+{
+header_line h;
+arc_line al;
+pdkim_bodyhash * b;
+uschar * errstr;
+
+if (!dkim_verify_ctx) return US"no dkim context";
+
+if (strncmpic(ARC_HDR_AMS, g->s, ARC_HDRLEN_AMS) != 0) return US"not AMS";
+
+DEBUG(D_receive) debug_printf("ARC: spotted AMS header\n");
+/* Parse the AMS header */
+
+h.next = NULL;
+h.slen = g->size;
+h.text = g->s;
+memset(&al, 0, sizeof(arc_line));
+if ((errstr = arc_parse_line(&al, &h, ARC_HDRLEN_AMS, FALSE)))
+ {
+ DEBUG(D_acl) if (errstr) debug_printf("ARC: %s\n", errstr);
+ goto badline;
+ }
+
+if (!al.a_hash.data)
+ {
+ DEBUG(D_acl) debug_printf("ARC: no a_hash from '%.*s'\n", h.slen, h.text);
+ goto badline;
+ }
+
+/* defaults */
+if (!al.c.data)
+ {
+ al.c_body.data = US"simple"; al.c_body.len = 6;
+ al.c_head = al.c_body;
+ }
+
+/* Ask the dkim code to calc a bodyhash with those specs */
+
+if (!(b = arc_ams_setup_vfy_bodyhash(&al)))
+ return US"dkim hash setup fail";
+
+/* Discard the reference; search again at verify time, knowing that one
+should have been created here. */
+
+return NULL;
+
+badline:
+ return US"line parsing error";
+}
+
+
+
+/* A header line has been identified by DKIM processing.
+
+Arguments:
+ g Header line
+ is_vfy TRUE for verify mode or FALSE for signing mode
+
+Return:
+ NULL for success, or an error string (probably unused)
+*/
+
+const uschar *
+arc_header_feed(gstring * g, BOOL is_vfy)
+{
+return is_vfy ? arc_header_vfy_feed(g) : arc_header_sign_feed(g);
+}
+
+
+
+/******************************************************************************/
+
+/* Construct the list of domains from the ARC chain after validation */
+
+uschar *
+fn_arc_domains(void)
+{
+arc_set * as;
+unsigned inst;
+gstring * g = NULL;
+
+for (as = arc_verify_ctx.arcset_chain, inst = 1; as; as = as->next, inst++)
+ {
+ arc_line * hdr_as = as->hdr_as;
+ if (hdr_as)
+ {
+ blob * d = &hdr_as->d;
+
+ for (; inst < as->instance; inst++)
+ g = string_catn(g, US":", 1);
+
+ g = d->data && d->len
+ ? string_append_listele_n(g, ':', d->data, d->len)
+ : string_catn(g, US":", 1);
+ }
+ else
+ g = string_catn(g, US":", 1);
+ }
+return g ? g->s : US"";
+}
+
+
+/* Construct an Authentication-Results header portion, for the ARC module */
+
+gstring *
+authres_arc(gstring * g)
+{
+if (arc_state)
+ {
+ arc_line * highest_ams;
+ int start = 0; /* Compiler quietening */
+ DEBUG(D_acl) start = g->ptr;
+
+ g = string_append(g, 2, US";\n\tarc=", arc_state);
+ if (arc_received_instance > 0)
+ {
+ g = string_fmt_append(g, " (i=%d)", arc_received_instance);
+ if (arc_state_reason)
+ g = string_append(g, 3, US"(", arc_state_reason, US")");
+ g = string_catn(g, US" header.s=", 10);
+ highest_ams = arc_received->hdr_ams;
+ g = string_catn(g, highest_ams->s.data, highest_ams->s.len);
+
+ g = string_fmt_append(g, " arc.oldest-pass=%d", arc_oldest_pass);
+
+ if (sender_host_address)
+ g = string_append(g, 2, US" smtp.remote-ip=", sender_host_address);
+ }
+ else if (arc_state_reason)
+ g = string_append(g, 3, US" (", arc_state_reason, US")");
+ DEBUG(D_acl) debug_printf("ARC: authres '%.*s'\n",
+ g->ptr - start - 3, g->s + start + 3);
+ }
+else
+ DEBUG(D_acl) debug_printf("ARC: no authres\n");
+return g;
+}
+
+
+# endif /* DISABLE_DKIM */
+#endif /* EXPERIMENTAL_ARC */
+/* vi: aw ai sw=2
+ */
diff --git a/src/auths/Makefile b/src/auths/Makefile
new file mode 100644
index 0000000..e85b22a
--- /dev/null
+++ b/src/auths/Makefile
@@ -0,0 +1,45 @@
+# 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 call_pam.o call_pwcheck.o \
+ call_radius.o check_serv_cond.o cram_md5.o cyrus_sasl.o dovecot.o \
+ external.o get_data.o get_no64_data.o gsasl_exim.o heimdal_gssapi.o \
+ plaintext.o pwcheck.o \
+ spa.o tls.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
+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
+pwcheck.o: $(HDRS) pwcheck.c pwcheck.h
+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
+external.o: $(HDRS) external.c external.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
+tls.o: $(HDRS) tls.c tls.h
+
+# End
diff --git a/src/auths/README b/src/auths/README
new file mode 100644
index 0000000..66bdcdc
--- /dev/null
+++ b/src/auths/README
@@ -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 SASL 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. The function may not touch big_buffer.
+
+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_context 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
index 0000000..8d886b6
--- /dev/null
+++ b/src/auths/auth-spa.c
@@ -0,0 +1,1524 @@
+/*************************************************
+* 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).
+ *
+ * Copyright (c) The Exim Maintainers 2021
+
+ * 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, US &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 (CS &challenge, sizeof(challenge),
+ CCS (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, US &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_X(a,b) ;
+
+extern int DEBUGLEVEL;
+
+#include "../exim.h"
+#include "auth-spa.h"
+#include <assert.h>
+
+
+#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) ((US (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 *)(CS (buf) + (pos)))
+# define IVAL(buf,pos) (*(uint32x *)(CS (buf) + (pos)))
+# define SVALS(buf,pos) (*(int16x *)(CS (buf) + (pos)))
+# define IVALS(buf,pos) (*(int32x *)(CS (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) \
+{ for (int l = 0; l < (len); l++) (val)[l] = macro((buf), (pos) + (size)*l); }
+
+# define SSMBMACRO(macro,buf,pos,val,len,size) \
+{ for (int 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_X(5,("%s%04x %s: ", \
+ tab_depth(depth), base,string)); \
+ if (charmode) print_asc(5, US (outbuf), (len)); else \
+ for (int idx = 0; idx < len; idx++) { DEBUG_X(5,("%02x ", (outbuf)[idx])); } \
+ DEBUG_X(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_X(5,("%s%04x %s: ", \
+ tab_depth(depth), base,string)); \
+ if (charmode) print_asc(5, US (outbuf), 2*(len)); else \
+ for (int idx = 0; idx < len; idx++) { DEBUG_X(5,("%04x ", (outbuf)[idx])); } \
+ DEBUG_X(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_X(5,("%s%04x %s: ", \
+ tab_depth(depth), base,string)); \
+ if (charmode) print_asc(5, US (outbuf), 4*(len)); else \
+ for (int idx = 0; idx < len; idx++) { DEBUG_X(5,("%08x ", (outbuf)[idx])); } \
+ DEBUG_X(5,("\n")); }
+
+# define DBG_RW_CVAL(string,depth,base,read,inbuf,outbuf) \
+ { RW_CVAL(read,inbuf,outbuf,0) \
+ DEBUG_X(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_X(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_X(5,("%s%04x %s: %08x\n", \
+ tab_depth(depth), base, string, outbuf)); }
+
+#endif /* _BYTEORDER_H */
+
+void E_P16 (uschar *p14, uschar *p16);
+void E_P24 (uschar *p21, uschar *c8, uschar *p24);
+void D_P16 (uschar *p14, uschar *in, uschar *out);
+void SMBOWFencrypt (uschar passwd[16], uschar * c8, uschar p24[24]);
+
+void mdfour (uschar *out, uschar *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 (char) -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 (uschar *out, const uschar *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)
+ {
+ uschar 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;
+uschar 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;
+}
+
+
+static uschar 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 uschar 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 uschar 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 uschar 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 uschar 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 uschar 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 uschar sc[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
+
+static uschar 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, uschar * p, int n)
+{
+for (int i = 0; i < n; i++)
+ out[i] = in[p[i] - 1];
+}
+
+static void
+lshift (char *d, int count, int n)
+{
+char out[64];
+for (int i = 0; i < n; i++)
+ out[i] = d[(i + count) % n];
+for (int 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)
+{
+for (int 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 (uschar *str, uschar *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 (uschar *out, uschar *in, uschar *key, int forw)
+{
+int i;
+char outb[64];
+char inb[64];
+char keyb[64];
+uschar 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 (uschar *p14, uschar *p16)
+{
+uschar 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 (uschar *p21, uschar *c8, uschar *p24)
+{
+smbhash (p24, c8, p21, 1);
+smbhash (p24 + 8, c8, p21 + 7, 1);
+smbhash (p24 + 16, c8, p21 + 14, 1);
+}
+
+void
+D_P16 (uschar *p14, uschar *in, uschar *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_X (0, ("ERROR: NULL dest in safe_strcpy\n"));
+ return NULL;
+ }
+
+if (!src)
+ {
+ *dest = 0;
+ return dest;
+ }
+
+len = strlen (src);
+
+if (len > maxlength)
+ {
+ DEBUG_X (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 ((uschar)(*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 (uschar * passwd, uschar * c8, uschar * p24)
+{
+uschar p14[15], p21[21];
+
+memset (p21, '\0', 21);
+memset (p14, '\0', 14);
+StrnCpy (CS p14, CS passwd, 14);
+
+strupper (CS p14);
+E_P16 (p14, p21);
+
+SMBOWFencrypt (p21, c8, p24);
+
+#ifdef DEBUG_PASSWORD
+DEBUG_X (100, ("spa_smb_encrypt: lm#, challenge, response\n"));
+dump_data (100, CS p21, 16);
+dump_data (100, CS c8, 8);
+dump_data (100, CS 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, uschar * 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 (uschar * passwd, uschar * p16)
+{
+int len;
+int16x wpwd[129];
+
+/* Password cannot be longer than 128 characters */
+len = strlen (CS 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, US wpwd, len);
+}
+
+/* Does both the NT and LM owfs of a user's password */
+void
+nt_lm_owf_gen (char *pwd, uschar nt_p16[16], uschar 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 (US passwd, nt_p16);
+
+#ifdef DEBUG_PASSWORD
+DEBUG_X (100, ("nt_lm_owf_gen: pwd, nt#\n"));
+dump_data (120, passwd, strlen (passwd));
+dump_data (100, CS 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 (US passwd, US p16);
+
+#ifdef DEBUG_PASSWORD
+DEBUG_X (100, ("nt_lm_owf_gen: pwd, lm#\n"));
+dump_data (120, passwd, strlen (passwd));
+dump_data (100, CS 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 (uschar passwd[16], uschar * c8, uschar p24[24])
+{
+uschar 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 (uschar passwd[8], uschar * ntlmchalresp, uschar p24[24])
+{
+uschar p21[21];
+
+memset (p21, '\0', 21);
+memcpy (p21, passwd, 8);
+memset (p21 + 8, 0xbd, 8);
+
+E_P24 (p21, ntlmchalresp, p24);
+#ifdef DEBUG_PASSWORD
+DEBUG_X (100, ("NTLMSSPOWFencrypt: p21, c8, p24\n"));
+dump_data (100, CS p21, 21);
+dump_data (100, CS ntlmchalresp, 8);
+dump_data (100, CS p24, 24);
+#endif
+}
+
+
+/* Does the NT MD4 hash then des encryption. */
+
+void
+spa_smb_nt_encrypt (uschar * passwd, uschar * c8, uschar * p24)
+{
+uschar p21[21];
+
+memset (p21, '\0', 21);
+
+E_md4hash (passwd, p21);
+SMBOWFencrypt (p21, c8, p24);
+
+#ifdef DEBUG_PASSWORD
+DEBUG_X (100, ("spa_smb_nt_encrypt: nt#, challenge, response\n"));
+dump_data (100, CS p21, 16);
+dump_data (100, CS c8, 8);
+dump_data (100, CS 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, uschar *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 (uschar *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 (uschar *out, uschar *in, int n)
+{
+uschar 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 && (count) != 0) /* we hate -Wint-in-bool-contex */ \
+ { \
+ 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, (US p), len); \
+}
+
+#define spa_unicode_add_string(ptr, header, string) \
+{ \
+char *p = string; \
+uschar *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(((CS structPtr) + IVAL(&structPtr->header.offset,0)), SVAL(&structPtr->header.len,0))
+
+#ifdef notdef
+
+#define DumpBuffer(fp, structPtr, header) \
+dumpRaw(fp,(US structPtr)+IVAL(&structPtr->header.offset,0),SVAL(&structPtr->header.len,0))
+
+
+static void
+dumpRaw (FILE * fp, uschar *buf, size_t len)
+{
+int i;
+
+for (i = 0; i < len; ++i)
+ fprintf (fp, "%02x ", buf[i]);
+
+fprintf (fp, "\n");
+}
+
+#endif
+
+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 uschar *
+strToUnicode (char *p)
+{
+static uschar 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 uschar *
+toString (char *p, size_t len)
+{
+static uschar buf[1024];
+
+assert (len + 1 < sizeof buf);
+
+memcpy (buf, p, len);
+buf[len] = 0;
+return buf;
+}
+
+#ifdef notdef
+
+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));
+}
+#endif
+
+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);
+
+/* 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] = (uschar)(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 (US password, challenge->challengeData, lmRespData);
+spa_smb_nt_encrypt (US 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)?
+ CCS GetUnicodeString(challenge, uDomain) :
+ CCS GetString(challenge, uDomain));
+
+spa_smb_encrypt (US password, challenge->challengeData, lmRespData);
+spa_smb_nt_encrypt (US 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
index 0000000..cfe1b08
--- /dev/null
+++ b/src/auths/auth-spa.h
@@ -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/call_pam.c b/src/auths/call_pam.c
new file mode 100644
index 0000000..80f80f1
--- /dev/null
+++ b/src/auths/call_pam.c
@@ -0,0 +1,204 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 - 2021 */
+/* 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 const 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 sep = 0;
+struct pam_response *reply;
+
+/* It seems that PAM frees reply[] */
+
+if ( pam_arg_ended
+ || !(reply = malloc(sizeof(struct pam_response) * num_msg)))
+ return PAM_CONV_ERR;
+
+for (int i = 0; i < num_msg; i++)
+ {
+ uschar *arg;
+ switch (msg[i]->msg_style)
+ {
+ case PAM_PROMPT_ECHO_ON:
+ case PAM_PROMPT_ECHO_OFF:
+ if (!(arg = string_nextinlist(&pam_args, &sep, NULL, 0)))
+ {
+ arg = US"";
+ pam_arg_ended = TRUE;
+ }
+ reply[i].resp = strdup(CCS arg); /* Use libc malloc, PAM frees resp directly*/
+ 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(const 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, NULL, 0);
+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 = US 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
index 0000000..0adde44
--- /dev/null
+++ b/src/auths/call_pwcheck.c
@@ -0,0 +1,121 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* Copyright (c) The Exim Maintainers 2020 */
+/* 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, CCSS &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(const uschar *username, const uschar *password,
+ const uschar *service, const 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
index 0000000..e7f9f52
--- /dev/null
+++ b/src/auths/call_radius.c
@@ -0,0 +1,223 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2016 */
+/* 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
+
+# ifdef RADIUS_LIB_RADIUSCLIENTNEW
+# define ENV FREERADIUSCLIENT_ENV /* Avoid clash with Berkeley DB */
+# include <freeradius-client.h>
+# else
+# include <radiusclient.h>
+# endif
+#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(const uschar *s, uschar **errptr)
+{
+uschar *user;
+const 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
+
+
+if (!(user = string_nextinlist(&radius_args, &sep, NULL, 0))) 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 = US"RADIUS: can't read dictionary";
+
+else if (!rc_avpair_add(&send, PW_USER_NAME, user, 0))
+ *errptr = US"RADIUS: add user name failed";
+
+else if (!rc_avpair_add(&send, PW_USER_PASSWORD, CS radius_args, 0))
+ *errptr = US"RADIUS: add password failed");
+
+else if (!rc_avpair_add(&send, PW_SERVICE_TYPE, &service, 0))
+ *errptr = US"RADIUS: add service type failed";
+
+#else /* RADIUS_LIB_RADIUSCLIENT unset => RADIUS_LIB_RADIUSCLIENT2 */
+
+if (!(h = rc_read_config(RADIUS_CONFIG_FILE)))
+ *errptr = string_sprintf("RADIUS: can't open %s", RADIUS_CONFIG_FILE);
+
+else if (rc_read_dictionary(h, rc_conf_str(h, "dictionary")) != 0)
+ *errptr = US"RADIUS: can't read dictionary";
+
+else if (!rc_avpair_add(h, &send, PW_USER_NAME, user, Ustrlen(user), 0))
+ *errptr = US"RADIUS: add user name failed";
+
+else if (!rc_avpair_add(h, &send, PW_USER_PASSWORD, CS radius_args,
+ Ustrlen(radius_args), 0))
+ *errptr = US"RADIUS: add password failed";
+
+else if (!rc_avpair_add(h, &send, PW_SERVICE_TYPE, &service, 0, 0))
+ *errptr = US"RADIUS: add service type failed";
+
+#endif /* RADIUS_LIB_RADIUSCLIENT */
+
+if (*errptr)
+ {
+ 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 REJECT_RC:
+ case ERROR_RC:
+ return FAIL;
+
+ case TIMEOUT_RC:
+ *errptr = US"RADIUS: timed out";
+ return ERROR;
+
+ case BADRESP_RC:
+ default:
+ *errptr = string_sprintf("RADIUS: unexpected response (%d)", result);
+ return ERROR;
+ }
+
+#else /* RADIUS_LIB_RADLIB is set */
+
+/* Authenticate using the libradius library */
+
+if (!(h = rad_auth_open()))
+ {
+ *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
+ switch (result = rad_send_request(h))
+ {
+ 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) 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
index 0000000..457a715
--- /dev/null
+++ b/src/auths/check_serv_cond.c
@@ -0,0 +1,124 @@
+/*************************************************
+* 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)
+ {
+ debug_printf("%s authenticator %s:\n", ablock->name, label);
+ for (int i = 0; i < AUTH_VARS; i++) if (auth_vars[i])
+ debug_printf(" $auth%d = %s\n", i + 1, auth_vars[i]);
+ for (int 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) return unset;
+cond = expand_string(condition);
+
+HDEBUG(D_auth)
+ if (!cond)
+ 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)
+ {
+ if (f.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
index 0000000..2c0616c
--- /dev/null
+++ b/src/auths/cram_md5.c
@@ -0,0 +1,360 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 */
+/* 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,
+ OPT_OFF(auth_cram_md5_options_block, client_name) },
+ { "client_secret", opt_stringptr,
+ OPT_OFF(auth_cram_md5_options_block, client_secret) },
+ { "server_secret", opt_stringptr,
+ OPT_OFF(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 condition authentication method. */
+
+auth_cram_md5_options_block auth_cram_md5_option_defaults = {
+ NULL, /* server_secret */
+ NULL, /* client_secret */
+ NULL /* client_name */
+};
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+void auth_cram_md5_init(auth_instance *ablock) {}
+int auth_cram_md5_server(auth_instance *ablock, uschar *data) {return 0;}
+int auth_cram_md5_client(auth_instance *ablock, void *sx, int timeout,
+ uschar *buffer, int buffsize) {return 0;}
+
+#else /*!MACRO_PREDEF*/
+
+
+/*************************************************
+* 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 /*!MACRO_PREDEF*/
+#endif /* STAND_ALONE */
+
+
+
+#ifndef MACRO_PREDEF
+/*************************************************
+* Perform 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 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, US secret, len, md5secret);
+ secret = US 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 (int 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, US 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 (f.running_in_test_harness)
+ challenge = US"<1896.697170952@postoffice.reston.mci.net>";
+
+/* No data should have been sent with the AUTH command */
+
+if (*data) return UNEXPECTED;
+
+/* Send the challenge, read the return */
+
+if ((rc = auth_get_data(&data, challenge, Ustrlen(challenge))) != OK) return rc;
+if ((len = 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 && !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 (f.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, US" 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 */
+ void * sx, /* smtp connextion */
+ 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 appropriate. */
+
+if (!secret || !name)
+ {
+ if (f.expand_string_forcedfail)
+ {
+ *buffer = 0; /* No message */
+ return CANCELLED;
+ }
+ string_format(buffer, buffsize, "expansion of \"%s\" failed in "
+ "%s authenticator: %s",
+ !secret ? 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(sx, SCMD_FLUSH, "AUTH %s\r\n", ablock->public_name) < 0)
+ return FAIL_SEND;
+if (!smtp_read_response(sx, buffer, buffsize, '3', timeout))
+ return FAIL;
+
+if (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);
+for (p = big_buffer; *p; ) p++;
+*p++ = ' ';
+
+for (i = 0; i < 16; i++)
+ p += sprintf(CS p, "%02x", digest[i]);
+
+/* Send the response, in base 64, and check the result. The response is
+in big_buffer, but 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(sx, SCMD_FLUSH, "%s\r\n", b64encode(CUS big_buffer,
+ p - big_buffer)) < 0) return FAIL_SEND;
+
+return smtp_read_response(sx, US 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
+
+#endif /*!MACRO_PREDEF*/
+/* End of cram_md5.c */
diff --git a/src/auths/cram_md5.h b/src/auths/cram_md5.h
new file mode 100644
index 0000000..95644db
--- /dev/null
+++ b/src/auths/cram_md5.h
@@ -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. */
+
+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 *, void *, 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
index 0000000..c8e2da5
--- /dev/null
+++ b/src/auths/cyrus_sasl.c
@@ -0,0 +1,513 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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,
+ OPT_OFF(auth_cyrus_sasl_options_block, server_hostname) },
+ { "server_mech", opt_stringptr,
+ OPT_OFF(auth_cyrus_sasl_options_block, server_mech) },
+ { "server_realm", opt_stringptr,
+ OPT_OFF(auth_cyrus_sasl_options_block, server_realm) },
+ { "server_service", opt_stringptr,
+ OPT_OFF(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 */
+};
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+void auth_cyrus_sasl_init(auth_instance *ablock) {}
+int auth_cyrus_sasl_server(auth_instance *ablock, uschar *data) {return 0;}
+int auth_cyrus_sasl_client(auth_instance *ablock, void * sx,
+ int timeout, uschar *buffer, int buffsize) {return 0;}
+gstring * auth_cyrus_sasl_version_report(gstring * g) {return NULL;}
+
+#else /*!MACRO_PREDEF*/
+
+
+
+
+/*************************************************
+* 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) *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);
+const uschar *list, *listptr, *buffer;
+int rc, i;
+unsigned int len;
+rmark rs_point;
+uschar *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) ob->server_mech = string_copy(ablock->public_name);
+
+if (!(expanded_hostname = expand_string(ob->server_hostname)))
+ 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
+ && !(realm_expanded = CS expand_string(ob->server_realm)))
+ 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;
+
+if ((rc = sasl_server_init(cbs, "exim")) != SASL_OK)
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
+ "couldn't initialise Cyrus SASL library.", ablock->name);
+
+if ((rc = sasl_server_new(CS ob->server_service, CS expanded_hostname,
+ realm_expanded, NULL, NULL, NULL, 0, &conn)) != SASL_OK)
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
+ "couldn't initialise Cyrus SASL server connection.", ablock->name);
+
+if ((rc = sasl_listmech(conn, NULL, "", ":", "", CCSS &list, &len, &i)) != 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_mark();
+
+/* 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 = NULL;
+int rc, 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);
+if (hname && ob->server_realm)
+ realm_expanded = CS expand_string(ob->server_realm);
+if (!hname || !realm_expanded && ob->server_realm)
+ {
+ auth_defer_msg = expand_string_message;
+ return DEFER;
+ }
+
+if (inlen)
+ {
+ if ((clen = b64decode(input, &clear)) < 0)
+ return BAD64;
+ input = clear;
+ inlen = clen;
+ }
+
+if ((rc = sasl_server_init(cbs, "exim")) != 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)
+ {
+ if ((rc = sasl_setprop(conn, SASL_SSF_EXTERNAL, (sasl_ssf_t *) &tls_in.bits)) != 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);
+
+ /*XXX Set channel-binding here with sasl_channel_binding_t / SASL_CHANNEL_BINDING
+ Unclear what the "name" element does though, ditto the "critical" flag. */
+ }
+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 (int i = 0; i < 2; ++i)
+ {
+ int propnum;
+ const uschar * label;
+ uschar * address_port;
+ const char *s_err;
+
+ if (i)
+ {
+ propnum = SASL_IPREMOTEPORT;
+ label = CUS"peer";
+ address_port = string_sprintf("%s;%d",
+ sender_host_address, sender_host_port);
+ }
+ else
+ {
+ propnum = SASL_IPLOCALPORT;
+ label = CUS"local";
+ address_port = string_sprintf("%s;%d", interface_address, interface_port);
+ }
+
+ if ((rc = sasl_setprop(conn, propnum, address_port)) != SASL_OK)
+ {
+ HDEBUG(D_auth)
+ {
+ s_err = sasl_errdetail(conn);
+ 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);
+ }
+
+for (rc = SASL_CONTINUE; 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,
+ CCSS &output, &outlen);
+ }
+ else
+ {
+ /* auth_get_data() takes a length-specfied block of binary
+ which can include zeroes; no terminating NUL is needed */
+
+ if ((rc = auth_get_data(&input, output, 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)
+ {
+ if ((clen = b64decode(input, &clear)) < 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, CCSS &output, &outlen);
+ }
+
+ if (rc == SASL_BADPROT)
+ {
+ sasl_dispose(&conn);
+ sasl_done();
+ return UNEXPECTED;
+ }
+ if (rc == SASL_CONTINUE)
+ continue;
+
+ /* Get the username and copy it into $auth1 and $1. The former is now the
+ preferred variable; the latter is the original variable. */
+
+ if ((sasl_getprop(conn, SASL_USERNAME, (const void **)&out2)) != 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): "
+ "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(out2);
+ expand_nmax = 1;
+
+ switch (rc)
+ {
+ case SASL_FAIL: case SASL_BUFOVER: case SASL_BADMAC: case SASL_BADAUTH:
+ case SASL_NOAUTHZ: case SASL_ENCRYPT: case SASL_EXPIRED:
+ case SASL_DISABLED: case 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): "
+ "Cyrus SASL permanent failure: %s", ablock->name, ob->server_mech,
+ sasl_errstring(rc, NULL, NULL));
+ sasl_dispose(&conn);
+ sasl_done();
+ return FAIL;
+
+ case 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;
+
+ case SASL_OK:
+ HDEBUG(D_auth)
+ debug_printf("Cyrus SASL %s authentication succeeded for %s\n",
+ ob->server_mech, auth_vars[0]);
+
+ if ((rc = sasl_getprop(conn, SASL_SSF, (const void **)(&negotiated_ssf_ptr)))!= 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): "
+ "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): "
+ "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);
+
+ default:
+ /* 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;
+ }
+ }
+/* NOTREACHED */
+return 0; /* Stop compiler complaints */
+}
+
+/*************************************************
+* Diagnostic API *
+*************************************************/
+
+gstring *
+auth_cyrus_sasl_version_report(gstring * g)
+{
+const char * implementation, * version;
+sasl_version_info(&implementation, &version, NULL, NULL, NULL, NULL);
+g = string_fmt_append(g,
+ "Library version: Cyrus SASL: Compile: %d.%d.%d\n"
+ " Runtime: %s [%s]\n",
+ SASL_VERSION_MAJOR, SASL_VERSION_MINOR, SASL_VERSION_STEP,
+ version, implementation);
+return g;
+}
+
+/*************************************************
+* Client entry point *
+*************************************************/
+
+/* For interface, see auths/README */
+
+int
+auth_cyrus_sasl_client(
+ auth_instance *ablock, /* authenticator block */
+ void * sx, /* connexction */
+ 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 /*!MACRO_PREDEF*/
+#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
index 0000000..6cf8834
--- /dev/null
+++ b/src/auths/cyrus_sasl.h
@@ -0,0 +1,36 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* 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 *, void *, int, uschar *, int);
+extern gstring * auth_cyrus_sasl_version_report(gstring *);
+
+/* End of cyrus_sasl.h */
diff --git a/src/auths/dovecot.c b/src/auths/dovecot.c
new file mode 100644
index 0000000..5d77133
--- /dev/null
+++ b/src/auths/dovecot.c
@@ -0,0 +1,530 @@
+/*
+ * Copyright (c) The Exim Maintainers 2006 - 2022
+ * Copyright (c) 2004 Andrey Panin <pazke@donpac.ru>
+ *
+ * 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, OPT_OFF(auth_dovecot_options_block, server_socket) },
+/*{ "server_tls", opt_bool, OPT_OFF(auth_dovecot_options_block, server_tls) },*/
+};
+
+/* 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 = nelem(auth_dovecot_options);
+
+/* Default private options block for the authentication method. */
+
+auth_dovecot_options_block auth_dovecot_option_defaults = {
+ .server_socket = NULL,
+/* .server_tls = FALSE,*/
+};
+
+
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+void auth_dovecot_init(auth_instance *ablock) {}
+int auth_dovecot_server(auth_instance *ablock, uschar *data) {return 0;}
+int auth_dovecot_client(auth_instance *ablock, void * sx,
+ int timeout, uschar *buffer, int buffsize) {return 0;}
+
+#else /*!MACRO_PREDEF*/
+
+
+/* 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) ablock->public_name = ablock->name;
+if (ob->server_socket) 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;
+ str[-1] = '\0';
+ }
+
+/* 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, client_conn_ctx * cctx)
+{
+int p = 0;
+int count = 0;
+
+for (;;)
+ {
+ if (socket_buffer_left == 0)
+ {
+ if ((socket_buffer_left =
+#ifndef DISABLE_TLS
+ cctx->tls_ctx ? tls_read(cctx->tls_ctx, sbuffer, sizeof(sbuffer)) :
+#endif
+ read(cctx->sock, sbuffer, sizeof(sbuffer))) <= 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;
+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, ret = DEFER;
+host_item host;
+client_conn_ctx cctx = {.sock = -1, .tls_ctx = NULL};
+BOOL found = FALSE, have_mech_line = FALSE;
+
+HDEBUG(D_auth) debug_printf("dovecot authentication\n");
+
+if (!data)
+ {
+ ret = FAIL;
+ goto out;
+ }
+
+/*XXX timeout? */
+cctx.sock = ip_streamsocket(ob->server_socket, &auth_defer_msg, 5, &host);
+if (cctx.sock < 0)
+ goto out;
+
+#ifdef notdef
+# ifndef DISABLE_TLS
+if (ob->server_tls)
+ {
+ union sockaddr_46 interface_sock;
+ EXIM_SOCKLEN_T size = sizeof(interface_sock);
+ smtp_connect_args conn_args = { .host = &host };
+ tls_support tls_dummy = { .sni = NULL };
+ uschar * errstr;
+
+ if (getsockname(cctx->sock, (struct sockaddr *) &interface_sock, &size) == 0)
+ conn_args.sending_ip_address = host_ntoa(-1, &interface_sock, NULL, NULL);
+ else
+ {
+ *errmsg = string_sprintf("getsockname failed: %s", strerror(errno));
+ goto bad;
+ }
+
+ if (!tls_client_start(&cctx, &conn_args, NULL, &tls_dummy, &errstr))
+ {
+ auth_defer_msg = string_sprintf("TLS connect failed: %s", errstr);
+ goto out;
+ }
+ }
+# endif
+#endif
+
+auth_defer_msg = US"authentication socket protocol error";
+
+socket_buffer_left = 0; /* Global, used to read more than a line but return by line */
+for (;;)
+ {
+debug_printf("%s %d\n", __FUNCTION__, __LINE__);
+ if (!dc_gets(buffer, sizeof(buffer), &cctx))
+ OUT("authentication socket read error or premature eof");
+debug_printf("%s %d\n", __FUNCTION__, __LINE__);
+ 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, nelem(args));
+
+ HDEBUG(D_auth) debug_strcut(args, nargs, nelem(args));
+
+ /* 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);
+ have_mech_line = TRUE;
+ if (strcmpic(US args[1], ablock->public_name) == 0)
+ found = TRUE;
+ }
+ else if (Ustrcmp(args[0], US"SPID") == 0)
+ {
+ /* Unfortunately the auth protocol handshake wasn't designed well
+ to differentiate between auth-client/userdb/master. auth-userdb
+ and auth-master send VERSION + SPID lines only and nothing
+ afterwards, while auth-client sends VERSION + MECH + SPID +
+ CUID + more. The simplest way that we can determine if we've
+ connected to the correct socket is to see if MECH line exists or
+ not (alternatively we'd have to have a small timeout after SPID
+ to see if CUID is sent or not). */
+
+ if (!have_mech_line)
+ OUT("authentication socket type mismatch"
+ " (connected to auth-master instead of auth-client)");
+ }
+ else if (Ustrcmp(args[0], US"DONE") == 0)
+ {
+ CHECK_COMMAND("DONE", 0, 0);
+ break;
+ }
+ }
+
+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)
+ 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
+ && 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 ? CS 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);
+
+if ((
+#ifndef DISABLE_TLS
+ cctx.tls_ctx ? tls_write(cctx.tls_ctx, auth_command, Ustrlen(auth_command), FALSE) :
+#endif
+ write(cctx.sock, 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'\n", auth_command);
+
+while (1)
+ {
+ uschar *temp;
+ uschar *auth_id_pre = NULL;
+
+ if (!dc_gets(buffer, sizeof(buffer), &cctx))
+ {
+ 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, nelem(args));
+ HDEBUG(D_auth) debug_strcut(args, nargs, nelem(args));
+
+ if (Uatoi(args[1]) != crequid)
+ OUT("authentication socket connection id mismatch");
+
+ switch (toupper(*args[0]))
+ {
+ case 'C':
+ CHECK_COMMAND("CONT", 1, 2);
+
+ if ((tmp = auth_get_no64_data(&data, US args[2])) != 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 ((
+#ifndef DISABLE_TLS
+ cctx.tls_ctx ? tls_write(cctx.tls_ctx, temp, Ustrlen(temp), FALSE) :
+#endif
+ write(cctx.sock, temp, Ustrlen(temp))) < 0)
+ OUT("authentication socket write error");
+ break;
+
+ case 'F':
+ CHECK_COMMAND("FAIL", 1, -1);
+
+ for (int i = 2; i < nargs && !auth_id_pre; 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 (int i = 2; i < nargs && !auth_id_pre; 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)
+ OUT("authentication socket protocol error, username missing");
+
+ auth_defer_msg = NULL;
+ ret = OK;
+ /* fallthrough */
+
+ default:
+ goto out;
+ }
+ }
+
+out:
+/* close the socket used by dovecot */
+#ifndef DISABLE_TLS
+if (cctx.tls_ctx)
+ tls_close(cctx.tls_ctx, TRUE);
+#endif
+if (cctx.sock >= 0)
+ close(cctx.sock);
+
+/* Expand server_condition as an authorization check */
+return ret == OK ? auth_check_serv_cond(ablock) : ret;
+}
+
+
+#endif /*!MACRO_PREDEF*/
diff --git a/src/auths/dovecot.h b/src/auths/dovecot.h
new file mode 100644
index 0000000..bfe1f07
--- /dev/null
+++ b/src/auths/dovecot.h
@@ -0,0 +1,30 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* Copyright (c) The Exim Maintainters 2020 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Private structure for the private options. */
+
+typedef struct {
+ uschar * server_socket;
+ BOOL server_tls;
+} 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/external.c b/src/auths/external.c
new file mode 100644
index 0000000..7e7fca8
--- /dev/null
+++ b/src/auths/external.c
@@ -0,0 +1,155 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Jeremy Harris 2019-2020 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This file provides an Exim authenticator driver for
+a server to verify a client SSL certificate, using the EXTERNAL
+method defined in RFC 4422 Appendix A.
+*/
+
+
+#include "../exim.h"
+#include "external.h"
+
+/* Options specific to the external authentication mechanism. */
+
+optionlist auth_external_options[] = {
+ { "client_send", opt_stringptr, OPT_OFF(auth_external_options_block, client_send) },
+ { "server_param2", opt_stringptr, OPT_OFF(auth_external_options_block, server_param2) },
+ { "server_param3", opt_stringptr, OPT_OFF(auth_external_options_block, server_param3) },
+};
+
+/* 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_external_options_count = nelem(auth_external_options);
+
+/* Default private options block for the authentication method. */
+
+auth_external_options_block auth_external_option_defaults = {
+ .server_param2 = NULL,
+ .server_param3 = NULL,
+
+ .client_send = NULL,
+};
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+void auth_external_init(auth_instance *ablock) {}
+int auth_external_server(auth_instance *ablock, uschar *data) {return 0;}
+int auth_external_client(auth_instance *ablock, void * sx,
+ int timeout, uschar *buffer, int buffsize) {return 0;}
+
+#else /*!MACRO_PREDEF*/
+
+
+
+
+/*************************************************
+* 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_external_init(auth_instance *ablock)
+{
+auth_external_options_block * ob = (auth_external_options_block *)ablock->options_block;
+if (!ablock->public_name) ablock->public_name = ablock->name;
+if (ablock->server_condition) ablock->server = TRUE;
+if (ob->client_send) ablock->client = TRUE;
+}
+
+
+
+/*************************************************
+* Server entry point *
+*************************************************/
+
+/* For interface, see auths/README */
+
+int
+auth_external_server(auth_instance * ablock, uschar * data)
+{
+auth_external_options_block * ob = (auth_external_options_block *)ablock->options_block;
+int rc;
+
+/* 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)
+ if ((rc = auth_read_input(data)) != OK)
+ return rc;
+
+/* 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. */
+
+if (expand_nmax == 0) /* skip if rxd data */
+ if ((rc = auth_prompt(CUS"")) != OK)
+ return rc;
+
+if (ob->server_param2)
+ {
+ uschar * s = expand_string(ob->server_param2);
+ auth_vars[expand_nmax] = s;
+ expand_nstring[++expand_nmax] = s;
+ expand_nlength[expand_nmax] = Ustrlen(s);
+ if (ob->server_param3)
+ {
+ s = expand_string(ob->server_param3);
+ auth_vars[expand_nmax] = s;
+ expand_nstring[++expand_nmax] = s;
+ expand_nlength[expand_nmax] = Ustrlen(s);
+ }
+ }
+
+return auth_check_serv_cond(ablock);
+}
+
+
+
+/*************************************************
+* Client entry point *
+*************************************************/
+
+/* For interface, see auths/README */
+
+int
+auth_external_client(
+ auth_instance *ablock, /* authenticator block */
+ void * sx, /* smtp connextion */
+ int timeout, /* command timeout */
+ uschar *buffer, /* buffer for reading response */
+ int buffsize) /* size of buffer */
+{
+auth_external_options_block *ob =
+ (auth_external_options_block *)(ablock->options_block);
+const uschar * text = ob->client_send;
+int rc;
+
+/* We output an AUTH command with one expanded argument, the client_send option */
+
+if ((rc = auth_client_item(sx, ablock, &text, AUTH_ITEM_FIRST | AUTH_ITEM_LAST,
+ timeout, buffer, buffsize)) != OK)
+ return rc == DEFER ? FAIL : rc;
+
+if (text) auth_vars[0] = string_copy(text);
+return OK;
+}
+
+
+
+#endif /*!MACRO_PREDEF*/
+/* End of external.c */
diff --git a/src/auths/external.h b/src/auths/external.h
new file mode 100644
index 0000000..7d43650
--- /dev/null
+++ b/src/auths/external.h
@@ -0,0 +1,32 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Jeremy Harris 2019 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Private structure for the private options. */
+
+typedef struct {
+ uschar * server_param2;
+ uschar * server_param3;
+
+ uschar * client_send;
+} auth_external_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist auth_external_options[];
+extern int auth_external_options_count;
+
+/* Block containing default values. */
+
+extern auth_external_options_block auth_external_option_defaults;
+
+/* The entry points for the mechanism */
+
+extern void auth_external_init(auth_instance *);
+extern int auth_external_server(auth_instance *, uschar *);
+extern int auth_external_client(auth_instance *, void *, int, uschar *, int);
+
+/* End of external.h */
diff --git a/src/auths/get_data.c b/src/auths/get_data.c
new file mode 100644
index 0000000..e0d79db
--- /dev/null
+++ b/src/auths/get_data.c
@@ -0,0 +1,259 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 - 2021 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+
+
+/****************************************************************
+* Decode and split the argument of an AUTH command *
+****************************************************************/
+
+/* 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. */
+
+int
+auth_read_input(const uschar * data)
+{
+if (Ustrcmp(data, "=") == 0)
+ {
+ auth_vars[0] = expand_nstring[++expand_nmax] = US"";
+ expand_nlength[expand_nmax] = 0;
+ }
+else
+ {
+ uschar * clear, * end;
+ int len;
+
+ if ((len = b64decode(data, &clear)) < 0) return BAD64;
+ DEBUG(D_auth) debug_printf("auth input decode:");
+ for (end = clear + len; clear < end && expand_nmax < EXPAND_MAXN; )
+ {
+ DEBUG(D_auth) debug_printf(" '%s'", clear);
+ 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];
+ }
+ DEBUG(D_auth) debug_printf("\n");
+ }
+return OK;
+}
+
+
+
+
+/*************************************************
+* Issue a challenge and get a response *
+*************************************************/
+
+/* This function is used by authentication drivers to b64-encode and
+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 data (unencoded, may be binary)
+ challen the length of the challenge data, in bytes
+
+Returns: OK on success
+ BAD64 if response too large for buffer
+ CANCELLED if response is "*"
+*/
+
+int
+auth_get_data(uschar ** aptr, const uschar * challenge, int challen)
+{
+int c;
+int p = 0;
+smtp_printf("334 %s\r\n", FALSE, b64encode(challenge, challen));
+while ((c = receive_getc(GETC_BUFFER_UNLIMITED)) != '\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;
+}
+
+
+
+int
+auth_prompt(const uschar * challenge)
+{
+int rc, len;
+uschar * resp, * clear, * end;
+
+if ((rc = auth_get_data(&resp, challenge, Ustrlen(challenge))) != OK)
+ return rc;
+if ((len = b64decode(resp, &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);
+return OK;
+}
+
+
+/***********************************************
+* Send an AUTH-negotiation item *
+************************************************/
+
+/* Expand and send one client auth item and read the response.
+Include the AUTH command and method if tagged as "first". Use the given buffer
+for receiving the b6-encoded reply; decode it it return it in the string arg.
+
+Return:
+ 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
+ DEFER more items expected
+*/
+
+int
+auth_client_item(void * sx, auth_instance * ablock, const uschar ** inout,
+ unsigned flags, int timeout, uschar * buffer, int buffsize)
+{
+int len, clear_len;
+uschar * ss, * clear;
+
+ss = US expand_cstring(*inout);
+if (ss == *inout) ss = string_copy(ss);
+
+/* 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)
+ {
+ if (!(flags & AUTH_ITEM_FIRST))
+ {
+ if (smtp_write_command(sx, SCMD_FLUSH, "*\r\n") >= 0)
+ (void) smtp_read_response(sx, US buffer, buffsize, '2', timeout);
+ }
+ if (f.expand_string_forcedfail)
+ {
+ *buffer = 0; /* No message */
+ return CANCELLED;
+ }
+ string_format(buffer, buffsize, "expansion of \"%s\" failed in %s "
+ "authenticator: %s", *inout, 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.
+
+The parsing ambiguity of ^^^ is taken as ^^ -> ^ ; ^ -> NUL - and there is
+no way to get a leading ^ after a NUL. We would need to intro new syntax to
+support that (probably preferring to take a more-standard exim list as a source
+and concat the elements with intervening NULs. Either a magic marker on the
+source string for client_send, or a new option). */
+
+for (int i = 0; i < len; i++)
+ if (ss[i] == '^')
+ if (ss[i+1] != '^')
+ ss[i] = 0;
+ else
+ if (--len > i+1) memmove(ss + i + 1, ss + i + 2, len - i);
+
+/* The first string is attached to the AUTH command; others are sent
+unembellished. */
+
+if (flags & AUTH_ITEM_FIRST)
+ {
+ if (smtp_write_command(sx, SCMD_FLUSH, "AUTH %s%s%s\r\n",
+ ablock->public_name, len == 0 ? "" : " ", b64encode(CUS ss, len)) < 0)
+ return FAIL_SEND;
+ }
+else
+ if (smtp_write_command(sx, SCMD_FLUSH, "%s\r\n", b64encode(CUS 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(sx, buffer, buffsize, '2', timeout))
+ {
+ *inout = NULL;
+ 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 (flags & AUTH_ITEM_LAST)
+ {
+ if (smtp_write_command(sx, SCMD_FLUSH, "*\r\n") >= 0)
+ (void)smtp_read_response(sx, 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 = 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 (!(flags & AUTH_ITEM_IGN64))
+ {
+ if (smtp_write_command(sx, SCMD_FLUSH, "*\r\n") >= 0)
+ (void)smtp_read_response(sx, US buffer, buffsize, '2', timeout);
+ string_format(buffer, buffsize, "Invalid base64 string in server "
+ "response \"%s\"", save_bad);
+ return CANCELLED;
+ }
+ DEBUG(D_auth) debug_printf("bad b64 decode for '%s';"
+ " ignoring due to client_ignore_invalid_base64\n", save_bad);
+ clear = string_copy(US"");
+ clear_len = 0;
+ }
+
+*inout = clear;
+return DEFER;
+}
+
+
+/* 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
index 0000000..a019756
--- /dev/null
+++ b/src/auths/get_no64_data.c
@@ -0,0 +1,47 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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", FALSE, challenge);
+while ((c = receive_getc(GETC_BUFFER_UNLIMITED)) != '\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
index 0000000..bae5f08
--- /dev/null
+++ b/src/auths/gsasl_exim.c
@@ -0,0 +1,1024 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2019 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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"
+
+
+#if GSASL_VERSION_MINOR >= 10
+# define EXIM_GSASL_HAVE_SCRAM_SHA_256
+# define EXIM_GSASL_SCRAM_S_KEY
+
+#elif GSASL_VERSION_MINOR == 9
+# define EXIM_GSASL_HAVE_SCRAM_SHA_256
+
+# if GSASL_VERSION_PATCH >= 1
+# define EXIM_GSASL_SCRAM_S_KEY
+# endif
+# if GSASL_VERSION_PATCH < 2
+# define CHANNELBIND_HACK
+# endif
+
+#else
+# define CHANNELBIND_HACK
+#endif
+
+/* Convenience for testing strings */
+
+#define STREQIC(Foo, Bar) (strcmpic((Foo), (Bar)) == 0)
+
+
+/* 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. */
+#define LOFF(field) OPT_OFF(auth_gsasl_options_block, field)
+
+optionlist auth_gsasl_options[] = {
+ { "client_authz", opt_stringptr, LOFF(client_authz) },
+ { "client_channelbinding", opt_bool, LOFF(client_channelbinding) },
+ { "client_password", opt_stringptr, LOFF(client_password) },
+ { "client_spassword", opt_stringptr, LOFF(client_spassword) },
+ { "client_username", opt_stringptr, LOFF(client_username) },
+
+ { "server_channelbinding", opt_bool, LOFF(server_channelbinding) },
+ { "server_hostname", opt_stringptr, LOFF(server_hostname) },
+#ifdef EXIM_GSASL_SCRAM_S_KEY
+ { "server_key", opt_stringptr, LOFF(server_key) },
+#endif
+ { "server_mech", opt_stringptr, LOFF(server_mech) },
+ { "server_password", opt_stringptr, LOFF(server_password) },
+ { "server_realm", opt_stringptr, LOFF(server_realm) },
+ { "server_scram_iter", opt_stringptr, LOFF(server_scram_iter) },
+ { "server_scram_salt", opt_stringptr, LOFF(server_scram_salt) },
+#ifdef EXIM_GSASL_SCRAM_S_KEY
+ { "server_skey", opt_stringptr, LOFF(server_s_key) },
+#endif
+ { "server_service", opt_stringptr, LOFF(server_service) }
+};
+
+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 = {
+ .server_service = US"smtp",
+ .server_hostname = US"$primary_hostname",
+ .server_scram_iter = US"4096",
+ /* all others zero/null */
+};
+
+
+#ifdef MACRO_PREDEF
+# include "../macro_predef.h"
+
+/* Dummy values */
+void auth_gsasl_init(auth_instance *ablock) {}
+int auth_gsasl_server(auth_instance *ablock, uschar *data) {return 0;}
+int auth_gsasl_client(auth_instance *ablock, void * sx,
+ int timeout, uschar *buffer, int buffsize) {return 0;}
+gstring * auth_gsasl_version_report(gstring * g) {return NULL;}
+
+void
+auth_gsasl_macros(void)
+{
+# ifdef EXIM_GSASL_HAVE_SCRAM_SHA_256
+ builtin_macro_create(US"_HAVE_AUTH_GSASL_SCRAM_SHA_256");
+# endif
+# ifdef EXIM_GSASL_SCRAM_S_KEY
+ builtin_macro_create(US"_HAVE_AUTH_GSASL_SCRAM_S_KEY");
+# endif
+}
+
+#else /*!MACRO_PREDEF*/
+
+
+
+/* "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)
+{
+static char * once = NULL;
+int rc;
+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)
+ 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)
+ {
+ if ((rc = gsasl_init(&gsasl_ctx)) != 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. */
+
+HDEBUG(D_auth) if (!once)
+ {
+ if ((rc = gsasl_server_mechlist(gsasl_ctx, &once)) != 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));
+
+ debug_printf("GNU SASL supports: %s\n", once);
+ }
+
+if (!gsasl_client_support_p(gsasl_ctx, CCS ob->server_mech))
+ 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)
+ ablock->server = TRUE;
+else if( ob->server_mech
+ && !STREQIC(ob->server_mech, US"EXTERNAL")
+ && !STREQIC(ob->server_mech, US"ANONYMOUS")
+ && !STREQIC(ob->server_mech, US"PLAIN")
+ && !STREQIC(ob->server_mech, US"LOGIN")
+ )
+ {
+ /* 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.
+ */
+
+ ablock->server = FALSE;
+ HDEBUG(D_auth) debug_printf("%s authenticator: "
+ "Need server_condition for %s mechanism\n",
+ 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
+ && STREQIC(ob->server_mech, US"DIGEST-MD5"))
+ {
+ ablock->server = FALSE;
+ HDEBUG(D_auth) debug_printf("%s authenticator: "
+ "Need server_realm for %s mechanism\n",
+ ablock->name, ob->server_mech);
+ }
+
+ablock->client = ob->client_username && ob->client_password;
+}
+
+
+/* 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);
+
+if (!cb_state)
+ {
+ HDEBUG(D_auth) debug_printf("gsasl callback (%d) not from our server/client processing\n", prop);
+#ifdef CHANNELBIND_HACK
+ if (prop == GSASL_CB_TLS_UNIQUE)
+ {
+ uschar * s;
+ if ((s = gsasl_callback_hook_get(ctx)))
+ {
+ HDEBUG(D_auth) debug_printf("GSASL_CB_TLS_UNIQUE from ctx hook\n");
+ gsasl_property_set(sctx, GSASL_CB_TLS_UNIQUE, CS s);
+ }
+ else
+ {
+ HDEBUG(D_auth) debug_printf("GSASL_CB_TLS_UNIQUE! dummy for now\n");
+ gsasl_property_set(sctx, GSASL_CB_TLS_UNIQUE, "");
+ }
+ return GSASL_OK;
+ }
+#endif
+ return GSASL_NO_CALLBACK;
+ }
+
+HDEBUG(D_auth)
+ debug_printf("GNU SASL Callback entered, prop=%d (loop prop=%d)\n",
+ prop, callback_loop);
+
+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;
+}
+
+
+/*************************************************
+* Debug service function *
+*************************************************/
+static const uschar *
+gsasl_prop_code_to_name(Gsasl_property prop)
+{
+switch (prop)
+ {
+ case GSASL_AUTHID: return US"AUTHID";
+ case GSASL_AUTHZID: return US"AUTHZID";
+ case GSASL_PASSWORD: return US"PASSWORD";
+ case GSASL_ANONYMOUS_TOKEN: return US"ANONYMOUS_TOKEN";
+ case GSASL_SERVICE: return US"SERVICE";
+ case GSASL_HOSTNAME: return US"HOSTNAME";
+ case GSASL_GSSAPI_DISPLAY_NAME: return US"GSSAPI_DISPLAY_NAME";
+ case GSASL_PASSCODE: return US"PASSCODE";
+ case GSASL_SUGGESTED_PIN: return US"SUGGESTED_PIN";
+ case GSASL_PIN: return US"PIN";
+ case GSASL_REALM: return US"REALM";
+ case GSASL_DIGEST_MD5_HASHED_PASSWORD: return US"DIGEST_MD5_HASHED_PASSWORD";
+ case GSASL_QOPS: return US"QOPS";
+ case GSASL_QOP: return US"QOP";
+ case GSASL_SCRAM_ITER: return US"SCRAM_ITER";
+ case GSASL_SCRAM_SALT: return US"SCRAM_SALT";
+ case GSASL_SCRAM_SALTED_PASSWORD: return US"SCRAM_SALTED_PASSWORD";
+#ifdef EXIM_GSASL_SCRAM_S_KEY
+ case GSASL_SCRAM_STOREDKEY: return US"SCRAM_STOREDKEY";
+ case GSASL_SCRAM_SERVERKEY: return US"SCRAM_SERVERKEY";
+#endif
+ case GSASL_CB_TLS_UNIQUE: return US"CB_TLS_UNIQUE";
+ case GSASL_SAML20_IDP_IDENTIFIER: return US"SAML20_IDP_IDENTIFIER";
+ case GSASL_SAML20_REDIRECT_URL: return US"SAML20_REDIRECT_URL";
+ case GSASL_OPENID20_REDIRECT_URL: return US"OPENID20_REDIRECT_URL";
+ case GSASL_OPENID20_OUTCOME_DATA: return US"OPENID20_OUTCOME_DATA";
+ case GSASL_SAML20_AUTHENTICATE_IN_BROWSER: return US"SAML20_AUTHENTICATE_IN_BROWSER";
+ case GSASL_OPENID20_AUTHENTICATE_IN_BROWSER: return US"OPENID20_AUTHENTICATE_IN_BROWSER";
+ case GSASL_VALIDATE_SIMPLE: return US"VALIDATE_SIMPLE";
+ case GSASL_VALIDATE_EXTERNAL: return US"VALIDATE_EXTERNAL";
+ case GSASL_VALIDATE_ANONYMOUS: return US"VALIDATE_ANONYMOUS";
+ case GSASL_VALIDATE_GSSAPI: return US"VALIDATE_GSSAPI";
+ case GSASL_VALIDATE_SECURID: return US"VALIDATE_SECURID";
+ case GSASL_VALIDATE_SAML20: return US"VALIDATE_SAML20";
+ case GSASL_VALIDATE_OPENID20: return US"VALIDATE_OPENID20";
+ }
+return CUS string_sprintf("(unknown prop: %d)", (int)prop);
+}
+
+/*************************************************
+* 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);
+
+#ifndef DISABLE_TLS
+if (tls_in.channelbinding && ob->server_channelbinding)
+ {
+# ifndef DISABLE_TLS_RESUME
+ if (!tls_in.ext_master_secret && tls_in.resumption == RESUME_USED)
+ { /* per RFC 7677 section 4 */
+ HDEBUG(D_auth) debug_printf(
+ "channel binding not usable on resumed TLS without extended-master-secret");
+ return FAIL;
+ }
+# endif
+# ifdef CHANNELBIND_HACK
+/* This is a gross hack to get around the library before 1.9.2
+a) requiring that c-b was already set, at the _start() call, and
+b) caching a b64'd version of the binding then which it never updates. */
+
+ gsasl_callback_hook_set(gsasl_ctx, tls_in.channelbinding);
+# endif
+ }
+#endif
+
+if ((rc = gsasl_server_start(gsasl_ctx, CCS ob->server_mech, &sctx)) != 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 */
+
+cb_state.ablock = ablock;
+cb_state.currently = CURRENTLY_SERVER;
+gsasl_session_hook_set(sctx, &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");
+
+#ifndef DISABLE_TLS
+if (tls_in.channelbinding)
+ {
+ /* 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 session 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*
+
+ Earlier library versions need this set early, during the _start() call,
+ so we had to misuse gsasl_callback_hook_set/get() as a data transfer
+ mech for the callback done at that time to get the bind-data. More recently
+ the callback is done (if needed) during the first gsasl_stop(). We know
+ the bind-data here so can set it (and should not get a callback).
+ */
+ if (ob->server_channelbinding)
+ {
+ HDEBUG(D_auth) debug_printf("Auth %s: Enabling channel-binding\n",
+ ablock->name);
+# ifndef CHANNELBIND_HACK
+ gsasl_property_set(sctx, GSASL_CB_TLS_UNIQUE, CCS tls_in.channelbinding);
+# endif
+ }
+ 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 {
+ switch (rc = gsasl_step64(sctx, received, &to_send))
+ {
+ 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;
+ }
+
+ /*XXX having our caller send the final smtp "235" is unfortunate; wastes a roundtrip */
+ if ((rc == GSASL_NEEDS_MORE) || (to_send && *to_send))
+ exim_error = auth_get_no64_data(USS &received, US 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;
+
+HDEBUG(D_auth)
+ {
+ const uschar * s;
+ if ((s = CUS gsasl_property_fast(sctx, GSASL_SCRAM_ITER)))
+ debug_printf(" - itercnt: '%s'\n", s);
+ if ((s = CUS gsasl_property_fast(sctx, GSASL_SCRAM_SALT)))
+ debug_printf(" - salt: '%s'\n", s);
+#ifdef EXIM_GSASL_SCRAM_S_KEY
+ if ((s = CUS gsasl_property_fast(sctx, GSASL_SCRAM_SERVERKEY)))
+ debug_printf(" - ServerKey: '%s'\n", s);
+ if ((s = CUS gsasl_property_fast(sctx, GSASL_SCRAM_STOREDKEY)))
+ debug_printf(" - StoredKey: '%s'\n", s);
+#endif
+ }
+
+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 = auth_check_some_cond(ablock, label, condition_string, FAIL);
+switch (exim_rc)
+ {
+ case OK: return GSASL_OK;
+ case DEFER: sasl_error_should_defer = TRUE;
+ return GSASL_AUTHENTICATION_ERROR;
+ case FAIL: return GSASL_AUTHENTICATION_ERROR;
+ default: 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;
+}
+
+
+/* Set the "next" $auth[n] and increment expand_nmax */
+
+static void
+set_exim_authvar_from_prop(Gsasl_session * sctx, Gsasl_property prop)
+{
+uschar * propval = US gsasl_property_fast(sctx, prop);
+int i = expand_nmax, j = i + 1;
+propval = propval ? string_copy(propval) : US"";
+HDEBUG(D_auth) debug_printf("auth[%d] <= %s'%s'\n",
+ j, gsasl_prop_code_to_name(prop), propval);
+expand_nstring[j] = propval;
+expand_nlength[j] = Ustrlen(propval);
+if (i < AUTH_VARS) auth_vars[i] = propval;
+expand_nmax = j;
+}
+
+static void
+set_exim_authvars_from_a_az_r_props(Gsasl_session * sctx)
+{
+if (expand_nmax > 0 ) return;
+
+/* 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. */
+
+set_exim_authvar_from_prop(sctx, GSASL_AUTHID);
+set_exim_authvar_from_prop(sctx, GSASL_AUTHZID);
+set_exim_authvar_from_prop(sctx, GSASL_REALM);
+}
+
+
+static int
+prop_from_option(Gsasl_session * sctx, Gsasl_property prop,
+ const uschar * option)
+{
+HDEBUG(D_auth) debug_printf(" %s\n", gsasl_prop_code_to_name(prop));
+if (option)
+ {
+ set_exim_authvars_from_a_az_r_props(sctx);
+ option = expand_cstring(option);
+ HDEBUG(D_auth) debug_printf(" '%s'\n", option);
+ if (*option)
+ gsasl_property_set(sctx, prop, CCS option);
+ return GSASL_OK;
+ }
+HDEBUG(D_auth) debug_printf(" option not set\n");
+return GSASL_NO_CALLBACK;
+}
+
+static int
+server_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop,
+ auth_instance *ablock)
+{
+char * tmps;
+uschar * s;
+int cbrc = GSASL_NO_CALLBACK;
+auth_gsasl_options_block * ob =
+ (auth_gsasl_options_block *)(ablock->options_block);
+
+HDEBUG(D_auth) debug_printf("GNU SASL callback %s for %s/%s as server\n",
+ gsasl_prop_code_to_name(prop), ablock->name, ablock->public_name);
+
+for (int 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 */
+ set_exim_authvar_from_prop(sctx, GSASL_AUTHID);
+ set_exim_authvar_from_prop(sctx, GSASL_AUTHZID);
+ set_exim_authvar_from_prop(sctx, GSASL_PASSWORD);
+
+ cbrc = condition_check(ablock, US"server_condition", ablock->server_condition);
+ checked_server_condition = TRUE;
+ break;
+
+ case GSASL_VALIDATE_EXTERNAL:
+ if (!ablock->server_condition)
+ {
+ HDEBUG(D_auth) debug_printf("No server_condition supplied, to validate EXTERNAL\n");
+ cbrc = GSASL_AUTHENTICATION_ERROR;
+ break;
+ }
+ set_exim_authvar_from_prop(sctx, GSASL_AUTHZID);
+
+ cbrc = condition_check(ablock,
+ US"server_condition (EXTERNAL)", ablock->server_condition);
+ checked_server_condition = TRUE;
+ break;
+
+ case GSASL_VALIDATE_ANONYMOUS:
+ if (!ablock->server_condition)
+ {
+ HDEBUG(D_auth) debug_printf("No server_condition supplied, to validate ANONYMOUS\n");
+ cbrc = GSASL_AUTHENTICATION_ERROR;
+ break;
+ }
+ set_exim_authvar_from_prop(sctx, GSASL_ANONYMOUS_TOKEN);
+
+ 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. */
+
+ set_exim_authvar_from_prop(sctx, GSASL_GSSAPI_DISPLAY_NAME);
+ set_exim_authvar_from_prop(sctx, GSASL_AUTHZID);
+
+ /* 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_SCRAM_ITER:
+ cbrc = prop_from_option(sctx, prop, ob->server_scram_iter);
+ break;
+
+ case GSASL_SCRAM_SALT:
+ cbrc = prop_from_option(sctx, prop, ob->server_scram_salt);
+ break;
+
+#ifdef EXIM_GSASL_SCRAM_S_KEY
+ case GSASL_SCRAM_STOREDKEY:
+ cbrc = prop_from_option(sctx, prop, ob->server_s_key);
+ break;
+
+ case GSASL_SCRAM_SERVERKEY:
+ cbrc = prop_from_option(sctx, prop, ob->server_key);
+ break;
+#endif
+
+ case GSASL_PASSWORD:
+ /* SCRAM-*: GSASL_AUTHID, GSASL_AUTHZID and GSASL_REALM
+ DIGEST-MD5: GSASL_AUTHID, GSASL_AUTHZID and GSASL_REALM
+ CRAM-MD5: GSASL_AUTHID
+ PLAIN: GSASL_AUTHID and GSASL_AUTHZID
+ LOGIN: GSASL_AUTHID
+ */
+ set_exim_authvars_from_a_az_r_props(sctx);
+
+ if (!(s = ob->server_password))
+ {
+ HDEBUG(D_auth) debug_printf("option not set\n");
+ break;
+ }
+ if (!(tmps = CS expand_string(s)))
+ {
+ sasl_error_should_defer = !f.expand_string_forcedfail;
+ 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;
+ }
+ HDEBUG(D_auth) debug_printf(" set\n");
+ 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. */
+
+ if (US tmps != s) 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;
+}
+
+
+/******************************************************************************/
+
+#define PROP_OPTIONAL BIT(0)
+
+static BOOL
+set_client_prop(Gsasl_session * sctx, Gsasl_property prop, uschar * val,
+ unsigned flags, uschar * buffer, int buffsize)
+{
+uschar * s;
+
+if (!val) return !!(flags & PROP_OPTIONAL);
+if (!(s = expand_string(val)) || !(flags & PROP_OPTIONAL) && !*s)
+ {
+ string_format(buffer, buffsize, "%s", expand_string_message);
+ return FALSE;
+ }
+if (*s)
+ {
+ HDEBUG(D_auth) debug_printf("%s: set %s = '%s'\n", __FUNCTION__,
+ gsasl_prop_code_to_name(prop), s);
+ gsasl_property_set(sctx, prop, CS s);
+ }
+
+return TRUE;
+}
+
+/*************************************************
+* Client entry point *
+*************************************************/
+
+/* For interface, see auths/README */
+
+int
+auth_gsasl_client(
+ auth_instance *ablock, /* authenticator block */
+ void * sx, /* connection */
+ int timeout, /* command timeout */
+ uschar *buffer, /* buffer for reading response */
+ int buffsize) /* size of buffer */
+{
+auth_gsasl_options_block *ob =
+ (auth_gsasl_options_block *)(ablock->options_block);
+Gsasl_session * sctx = NULL;
+struct callback_exim_state cb_state;
+uschar * s;
+BOOL initial = TRUE;
+int rc, yield = FAIL;
+
+HDEBUG(D_auth)
+ debug_printf("GNU SASL: initialising session for %s, mechanism %s\n",
+ ablock->name, ob->server_mech);
+
+*buffer = 0;
+
+#ifndef DISABLE_TLS
+if (tls_out.channelbinding && ob->client_channelbinding)
+ {
+# ifndef DISABLE_TLS_RESUME
+ if (!tls_out.ext_master_secret && tls_out.resumption == RESUME_USED)
+ { /* Per RFC 7677 section 4. See also RFC 7627, "Triple Handshake"
+ vulnerability, and https://www.mitls.org/pages/attacks/3SHAKE */
+ string_format(buffer, buffsize, "%s",
+ "channel binding not usable on resumed TLS without extended-master-secret");
+ return FAIL;
+ }
+# endif
+# ifdef CHANNELBIND_HACK
+ /* This is a gross hack to get around the library before 1.9.2
+ a) requiring that c-b was already set, at the _start() call, and
+ b) caching a b64'd version of the binding then which it never updates. */
+
+ gsasl_callback_hook_set(gsasl_ctx, tls_out.channelbinding);
+# endif
+ }
+#endif
+
+if ((rc = gsasl_client_start(gsasl_ctx, CCS ob->server_mech, &sctx)) != GSASL_OK)
+ {
+ string_format(buffer, buffsize, "GNU SASL: session start failure: %s (%s)",
+ gsasl_strerror_name(rc), gsasl_strerror(rc));
+ HDEBUG(D_auth) debug_printf("%s\n", buffer);
+ return ERROR;
+ }
+
+cb_state.ablock = ablock;
+cb_state.currently = CURRENTLY_CLIENT;
+gsasl_session_hook_set(sctx, &cb_state);
+
+/* Set properties */
+
+if ( !set_client_prop(sctx, GSASL_PASSWORD, ob->client_password,
+ 0, buffer, buffsize)
+ || !set_client_prop(sctx, GSASL_AUTHID, ob->client_username,
+ 0, buffer, buffsize)
+ || !set_client_prop(sctx, GSASL_AUTHZID, ob->client_authz,
+ PROP_OPTIONAL, buffer, buffsize)
+ )
+ return ERROR;
+
+#ifndef DISABLE_TLS
+if (tls_out.channelbinding)
+ if (ob->client_channelbinding)
+ {
+ HDEBUG(D_auth) debug_printf("Auth %s: Enabling channel-binding\n",
+ ablock->name);
+# ifndef CHANNELBIND_HACK
+ gsasl_property_set(sctx, GSASL_CB_TLS_UNIQUE, CCS tls_out.channelbinding);
+# endif
+ }
+ else
+ HDEBUG(D_auth)
+ debug_printf("Auth %s: Not enabling channel-binding (data available)\n",
+ ablock->name);
+#endif
+
+/* Run the SASL conversation with the server */
+
+for(s = NULL; ;)
+ {
+ uschar * outstr;
+ BOOL fail = TRUE;
+
+ rc = gsasl_step64(sctx, CS s, CSS &outstr);
+
+ if (rc == GSASL_NEEDS_MORE || rc == GSASL_OK)
+ {
+ fail = initial
+ ? smtp_write_command(sx, SCMD_FLUSH,
+ outstr ? "AUTH %s %s\r\n" : "AUTH %s\r\n",
+ ablock->public_name, outstr) <= 0
+ : outstr
+ ? smtp_write_command(sx, SCMD_FLUSH, "%s\r\n", outstr) <= 0
+ : FALSE;
+ free(outstr);
+ if (fail)
+ {
+ yield = FAIL_SEND;
+ goto done;
+ }
+ initial = FALSE;
+ }
+
+ if (rc != GSASL_NEEDS_MORE)
+ {
+ if (rc != GSASL_OK)
+ {
+ string_format(buffer, buffsize, "gsasl: %s", gsasl_strerror(rc));
+ break;
+ }
+
+ /* expecting a final 2xx from the server, accepting the AUTH */
+
+ if (smtp_read_response(sx, buffer, buffsize, '2', timeout))
+ yield = OK;
+ break; /* from SASL sequence loop */
+ }
+
+ /* 2xx or 3xx response is acceptable. If 2xx, no further input */
+
+ if (!smtp_read_response(sx, buffer, buffsize, '3', timeout))
+ if (errno == 0 && buffer[0] == '2')
+ buffer[4] = '\0';
+ else
+ {
+ yield = FAIL;
+ goto done;
+ }
+ s = buffer + 4;
+ }
+
+done:
+if (yield == OK)
+ {
+ expand_nmax = 0;
+ set_exim_authvar_from_prop(sctx, GSASL_AUTHID);
+ set_exim_authvar_from_prop(sctx, GSASL_SCRAM_ITER);
+ set_exim_authvar_from_prop(sctx, GSASL_SCRAM_SALT);
+ set_exim_authvar_from_prop(sctx, GSASL_SCRAM_SALTED_PASSWORD);
+ }
+
+gsasl_finish(sctx);
+return yield;
+}
+
+static int
+client_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock)
+{
+HDEBUG(D_auth) debug_printf("GNU SASL callback %s for %s/%s as client\n",
+ gsasl_prop_code_to_name(prop), ablock->name, ablock->public_name);
+switch (prop)
+ {
+ case GSASL_CB_TLS_UNIQUE: /*XXX should never get called for this */
+ HDEBUG(D_auth)
+ debug_printf(" filling in\n");
+ gsasl_property_set(sctx, GSASL_CB_TLS_UNIQUE, CCS tls_out.channelbinding);
+ return GSASL_OK;
+ case GSASL_SCRAM_SALTED_PASSWORD:
+ {
+ uschar * client_spassword =
+ ((auth_gsasl_options_block *) ablock->options_block)->client_spassword;
+ uschar dummy[4];
+ HDEBUG(D_auth) if (!client_spassword)
+ debug_printf(" client_spassword option unset\n");
+ if (client_spassword)
+ {
+ expand_nmax = 0;
+ set_exim_authvar_from_prop(sctx, GSASL_AUTHID);
+ set_exim_authvar_from_prop(sctx, GSASL_SCRAM_ITER);
+ set_exim_authvar_from_prop(sctx, GSASL_SCRAM_SALT);
+ set_client_prop(sctx, GSASL_SCRAM_SALTED_PASSWORD, client_spassword,
+ 0, dummy, sizeof(dummy));
+ for (int i = 0; i < AUTH_VARS; i++) auth_vars[i] = NULL;
+ expand_nmax = 0;
+ }
+ break;
+ }
+ default:
+ HDEBUG(D_auth)
+ debug_printf(" not providing one\n");
+ break;
+ }
+return GSASL_NO_CALLBACK;
+}
+
+/*************************************************
+* Diagnostic API *
+*************************************************/
+
+gstring *
+auth_gsasl_version_report(gstring * g)
+{
+return string_fmt_append(g, "Library version: GNU SASL: Compile: %s\n"
+ " Runtime: %s\n",
+ GSASL_VERSION, gsasl_check_version(NULL));
+}
+
+
+
+/* Dummy */
+void auth_gsasl_macros(void) {}
+
+#endif /*!MACRO_PREDEF*/
+#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
index 0000000..19c9036
--- /dev/null
+++ b/src/auths/gsasl_exim.h
@@ -0,0 +1,53 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2019 - 2022 */
+/* 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_key;
+ uschar *server_s_key;
+ uschar *server_scram_iter;
+ uschar *server_scram_salt;
+
+ uschar *client_username;
+ uschar *client_password;
+ uschar *client_authz;
+ uschar *client_spassword;
+
+ BOOL server_channelbinding;
+ BOOL client_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 *, void *,
+ int, uschar *, int);
+extern gstring * auth_gsasl_version_report(gstring *);
+extern void auth_gsasl_macros(void);
+
+/* End of gsasl_exim.h */
diff --git a/src/auths/heimdal_gssapi.c b/src/auths/heimdal_gssapi.c
new file mode 100644
index 0000000..3817632
--- /dev/null
+++ b/src/auths/heimdal_gssapi.c
@@ -0,0 +1,618 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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,
+ OPT_OFF(auth_heimdal_gssapi_options_block, server_hostname) },
+ { "server_keytab", opt_stringptr,
+ OPT_OFF(auth_heimdal_gssapi_options_block, server_keytab) },
+ { "server_service", opt_stringptr,
+ OPT_OFF(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 */
+};
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+void auth_heimdal_gssapi_init(auth_instance *ablock) {}
+int auth_heimdal_gssapi_server(auth_instance *ablock, uschar *data) {return 0;}
+int auth_heimdal_gssapi_client(auth_instance *ablock, void * sx,
+ int timeout, uschar *buffer, int buffsize) {return 0;}
+gstring * auth_heimdal_gssapi_version_report(gstring * g) {}
+
+#else /*!MACRO_PREDEF*/
+
+
+
+/* "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(rmark, 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;
+ }
+
+if ((krc = krb5_init_context(&context)))
+ {
+ 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);
+ if ((krc = krb5_kt_resolve(context, k_keytab_typed_name, &keytab)))
+ {
+ 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");
+ if ((krc = krb5_kt_default(context, &keytab)))
+ {
+ 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 */
+ if ((krc = krb5_kt_start_seq_get(context, keytab, &cursor)))
+ exim_heimdal_error_debug("krb5_kt_start_seq_get", context, krc);
+ else
+ {
+ while (!(krc = krb5_kt_next_entry(context, keytab, &entry, &cursor)))
+ {
+ 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);
+ }
+ if ((krc = krb5_kt_end_seq_get(context, keytab, &cursor)))
+ exim_heimdal_error_debug("krb5_kt_end_seq_get", context, krc);
+ }
+ }
+
+if ((krc = krb5_kt_close(context, keytab)))
+ HDEBUG(D_auth) exim_heimdal_error_debug("krb5_kt_close", context, krc);
+
+krb5_free_context(context);
+
+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;
+uschar *tmp1, *tmp2, *from_client;
+auth_heimdal_gssapi_options_block *ob =
+ (auth_heimdal_gssapi_options_block *)(ablock->options_block);
+BOOL handled_empty_ir;
+rmark store_reset_point;
+uschar *keytab;
+uschar sasl_config[4];
+uschar requested_qop;
+
+store_reset_point = store_mark();
+
+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)
+ {
+ if (handled_empty_ir)
+ {
+ HDEBUG(D_auth) debug_printf("gssapi: repeated empty input, grr.\n");
+ error_out = BAD64;
+ goto ERROR_OUT;
+ }
+
+ HDEBUG(D_auth) debug_printf("gssapi: missing initial response, nudging.\n");
+ if ((error_out = auth_get_data(&from_client, US"", 0)) != 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 = 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 = 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))
+ {
+ HDEBUG(D_auth)
+ debug_printf("gssapi: client requested security layers (%x)\n",
+ (unsigned int) requested_qop);
+ error_out = FAIL;
+ goto ERROR_OUT;
+ }
+
+ for (int 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(rmark store_reset_point,
+ OM_uint32 major, OM_uint32 minor,
+ const char *format, ...)
+{
+va_list ap;
+OM_uint32 maj_stat, min_stat;
+OM_uint32 msgcontext = 0;
+gss_buffer_desc status_string;
+gstring * g;
+
+HDEBUG(D_auth)
+ {
+ va_start(ap, format);
+ g = string_vformat(NULL, SVFMT_EXTEND|SVFMT_REBUFFER, format, ap);
+ 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)
+ auth_defer_msg = string_copy(US status_string.value);
+
+ HDEBUG(D_auth) debug_printf("heimdal %s: %.*s\n",
+ string_from_gstring(g), (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 */
+ void * sx, /* connection */
+ 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 *
+*************************************************/
+
+gstring *
+auth_heimdal_gssapi_version_report(gstring * g)
+{
+/* No build-time constants available unless we link against libraries at
+build-time and export the result as a string into a header ourselves. */
+
+return string_fmt_append(g, "Library version: Heimdal: Runtime: %s\n"
+ " Build Info: %s\n",
+ heimdal_version, heimdal_long_version));
+}
+
+#endif /*!MACRO_PREDEF*/
+#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
index 0000000..49775af
--- /dev/null
+++ b/src/auths/heimdal_gssapi.h
@@ -0,0 +1,39 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* 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 *, void *, int, uschar *, int);
+extern void auth_heimdal_gssapi_version_report(BOOL);
+
+/* End of heimdal_gssapi.h */
diff --git a/src/auths/plaintext.c b/src/auths/plaintext.c
new file mode 100644
index 0000000..58d1783
--- /dev/null
+++ b/src/auths/plaintext.c
@@ -0,0 +1,179 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 - 2021 */
+/* 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,
+ OPT_OFF(auth_plaintext_options_block, client_ignore_invalid_base64) },
+ { "client_send", opt_stringptr,
+ OPT_OFF(auth_plaintext_options_block, client_send) },
+ { "server_prompts", opt_stringptr,
+ OPT_OFF(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 */
+};
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+void auth_plaintext_init(auth_instance *ablock) {}
+int auth_plaintext_server(auth_instance *ablock, uschar *data) {return 0;}
+int auth_plaintext_client(auth_instance *ablock, void * sx, int timeout,
+ uschar *buffer, int buffsize) {return 0;}
+
+#else /*!MACRO_PREDEF*/
+
+
+
+/*************************************************
+* 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);
+const uschar * prompts = ob->server_prompts;
+uschar * s;
+int number = 1;
+int rc;
+int sep = 0;
+
+/* Expand a non-empty list of prompt strings */
+
+if (prompts)
+ if (!(prompts = expand_cstring(prompts)))
+ {
+ 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)
+ if ((rc = auth_read_input(data)) != OK)
+ return rc;
+
+/* 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, NULL, 0))
+ && expand_nmax < EXPAND_MAXN)
+ if (number++ > expand_nmax)
+ if ((rc = auth_prompt(CUS s)) != OK)
+ return rc;
+
+/* 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 */
+ void * sx, /* smtp connextion */
+ 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);
+const uschar * text = ob->client_send;
+const uschar * s;
+int sep = 0;
+int auth_var_idx = 0, rc;
+int flags = AUTH_ITEM_FIRST;
+
+if (ob->client_ignore_invalid_base64)
+ flags |= AUTH_ITEM_IGN64;
+
+/* 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, NULL, 0)))
+ {
+ if (!text)
+ flags |= AUTH_ITEM_LAST;
+
+ if ((rc = auth_client_item(sx, ablock, &s, flags, timeout, buffer, buffsize))
+ != DEFER)
+ return rc;
+
+ flags &= ~AUTH_ITEM_FIRST;
+
+ if (auth_var_idx < AUTH_VARS)
+ auth_vars[auth_var_idx++] = string_copy(s);
+ }
+
+/* Control should never actually get here. */
+
+return FAIL;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of plaintext.c */
diff --git a/src/auths/plaintext.h b/src/auths/plaintext.h
new file mode 100644
index 0000000..4c6d011
--- /dev/null
+++ b/src/auths/plaintext.h
@@ -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. */
+
+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 *, void *, int, uschar *, int);
+
+/* End of plaintext.h */
diff --git a/src/auths/pwcheck.c b/src/auths/pwcheck.c
new file mode 100644
index 0000000..7dd529f
--- /dev/null
+++ b/src/auths/pwcheck.c
@@ -0,0 +1,449 @@
+/* SASL server API implementation
+ * Rob Siemborski
+ * Tim Martin
+ * $Id: checkpw.c,v 1.49 2002/03/07 19:14:04 ken3 Exp $
+ */
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/*
+ * 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)
+{
+*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(CS &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 = CS userid;
+ iov[0].iov_len = strlen(userid)+1;
+ iov[1].iov_base = CS 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)
+{
+*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(CS &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 {
+ /* Assume the file is trusted, so no tainting */
+ *retval = store_get(count + 1, GET_UNTAINTED);
+ 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 = CS 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 = CS 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
index 0000000..1287ea2
--- /dev/null
+++ b/src/auths/pwcheck.h
@@ -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/spa.c b/src/auths/spa.c
new file mode 100644
index 0000000..ff90d33
--- /dev/null
+++ b/src/auths/spa.c
@@ -0,0 +1,376 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 */
+/* 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,
+ OPT_OFF(auth_spa_options_block, spa_domain) },
+ { "client_password", opt_stringptr,
+ OPT_OFF(auth_spa_options_block, spa_password) },
+ { "client_username", opt_stringptr,
+ OPT_OFF(auth_spa_options_block, spa_username) },
+ { "server_password", opt_stringptr,
+ OPT_OFF(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 condition 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) */
+};
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+void auth_spa_init(auth_instance *ablock) {}
+int auth_spa_server(auth_instance *ablock, uschar *data) {return 0;}
+int auth_spa_client(auth_instance *ablock, void * sx, int timeout,
+ uschar *buffer, int buffsize) {return 0;}
+
+#else /*!MACRO_PREDEF*/
+
+
+
+
+/*************************************************
+* 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) ((US (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, *s;
+unsigned off;
+
+/* send a 334, MS Exchange style, and grab the client's request,
+unless we already have it via an initial response. */
+
+if (!*data && auth_get_no64_data(&data, US"NTLM supported") != OK)
+ return FAIL;
+
+if (spa_base64_to_bits(CS &request, sizeof(request), CCS 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, US &challenge, spa_request_length(&challenge));
+
+if (auth_get_no64_data(&data, msgbuf) != OK)
+ return FAIL;
+
+/* dump client response */
+if (spa_base64_to_bits(CS &response, sizeof(response), CCS 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;
+ int len = SVAL(&responseptr->uUser.len,0)/2;
+
+ if ( (off = IVAL(&responseptr->uUser.offset,0)) >= sizeof(SPAAuthResponse)
+ || len >= sizeof(responseptr->buffer)/2
+ || (p = (CS responseptr) + off) + len*2 >= CS (responseptr+1)
+ )
+ {
+ DEBUG(D_auth)
+ debug_printf("auth_spa_server(): bad uUser spec in response\n");
+ return FAIL;
+ }
+
+ 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 */
+
+if (!(clearpass = expand_string(ob->spa_serverpassword)))
+ if (f.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) */
+
+off = IVAL(&responseptr->ntResponse.offset,0);
+if (off >= sizeof(SPAAuthResponse) - 24)
+ {
+ DEBUG(D_auth)
+ debug_printf("auth_spa_server(): bad ntRespData spec in response\n");
+ return FAIL;
+ }
+s = (US responseptr) + off;
+
+if (memcmp(ntRespData, s, 24) == 0)
+ return auth_check_serv_cond(ablock); /* success. we have a winner. */
+
+ /* 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 */
+ void * sx, /* connection */
+ 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 */
+
+if (!(username = CS expand_string(ob->spa_username)))
+ {
+ if (f.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;
+ }
+
+if (!(password = CS expand_string(ob->spa_password)))
+ {
+ if (f.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)
+ if (!(domain = CS expand_string(ob->spa_domain)))
+ {
+ if (f.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(sx, SCMD_FLUSH, "AUTH %s\r\n", ablock->public_name) < 0)
+ return FAIL_SEND;
+
+/* wait for the 3XX OK message */
+if (!smtp_read_response(sx, US 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, US &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(sx, SCMD_FLUSH, "%s\r\n", msgbuf) < 0)
+ return FAIL_SEND;
+
+/* wait for the auth challenge */
+if (!smtp_read_response(sx, US 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(CS (&challenge), sizeof(challenge), CCS (buffer + 4));
+
+spa_build_auth_response(&challenge, &response, CS username, CS password);
+spa_bits_to_base64(US msgbuf, US &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(sx, SCMD_FLUSH, "%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(sx, 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;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of spa.c */
diff --git a/src/auths/spa.h b/src/auths/spa.h
new file mode 100644
index 0000000..ca93469
--- /dev/null
+++ b/src/auths/spa.h
@@ -0,0 +1,38 @@
+/*************************************************
+* 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 *, void *, int, uschar *, int);
+
+/* End of spa.h */
diff --git a/src/auths/tls.c b/src/auths/tls.c
new file mode 100644
index 0000000..325e7b4
--- /dev/null
+++ b/src/auths/tls.c
@@ -0,0 +1,94 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Jeremy Harris 1995 - 2020 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This file provides an Exim authenticator driver for
+a server to verify a client SSL certificate
+*/
+
+
+#include "../exim.h"
+#include "tls.h"
+
+/* Options specific to the tls authentication mechanism. */
+
+optionlist auth_tls_options[] = {
+ { "server_param", opt_stringptr,
+ OPT_OFF(auth_tls_options_block, server_param1) },
+ { "server_param1", opt_stringptr,
+ OPT_OFF(auth_tls_options_block, server_param1) },
+ { "server_param2", opt_stringptr,
+ OPT_OFF(auth_tls_options_block, server_param2) },
+ { "server_param3", opt_stringptr,
+ OPT_OFF(auth_tls_options_block, server_param3) },
+};
+
+/* 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_tls_options_count = nelem(auth_tls_options);
+
+/* Default private options block for the authentication method. */
+
+auth_tls_options_block auth_tls_option_defaults = {
+ NULL, /* server_param1 */
+ NULL, /* server_param2 */
+ NULL, /* server_param3 */
+};
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+void auth_tls_init(auth_instance *ablock) {}
+int auth_tls_server(auth_instance *ablock, uschar *data) {return 0;}
+int auth_tls_client(auth_instance *ablock, void * sx,
+ int timeout, uschar *buffer, int buffsize) {return 0;}
+
+#else /*!MACRO_PREDEF*/
+
+
+
+
+/*************************************************
+* 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_tls_init(auth_instance *ablock)
+{
+ablock->public_name = ablock->name; /* needed for core code */
+}
+
+
+
+/*************************************************
+* Server entry point *
+*************************************************/
+
+/* For interface, see auths/README */
+
+int
+auth_tls_server(auth_instance *ablock, uschar *data)
+{
+auth_tls_options_block * ob = (auth_tls_options_block *)ablock->options_block;
+
+if (ob->server_param1)
+ auth_vars[expand_nmax++] = expand_string(ob->server_param1);
+if (ob->server_param2)
+ auth_vars[expand_nmax++] = expand_string(ob->server_param2);
+if (ob->server_param3)
+ auth_vars[expand_nmax++] = expand_string(ob->server_param3);
+return auth_check_serv_cond(ablock);
+}
+
+
+#endif /*!MACRO_PREDEF*/
+/* End of tls.c */
diff --git a/src/auths/tls.h b/src/auths/tls.h
new file mode 100644
index 0000000..7aa95b6
--- /dev/null
+++ b/src/auths/tls.h
@@ -0,0 +1,30 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Jeremy Harris 2015 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Private structure for the private options. */
+
+typedef struct {
+ uschar * server_param1;
+ uschar * server_param2;
+ uschar * server_param3;
+} auth_tls_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist auth_tls_options[];
+extern int auth_tls_options_count;
+
+/* Block containing default values. */
+
+extern auth_tls_options_block auth_tls_option_defaults;
+
+/* The entry points for the mechanism */
+
+extern void auth_tls_init(auth_instance *);
+extern int auth_tls_server(auth_instance *, uschar *);
+
+/* End of tls.h */
diff --git a/src/auths/xtextdecode.c b/src/auths/xtextdecode.c
new file mode 100644
index 0000000..746dfbd
--- /dev/null
+++ b/src/auths/xtextdecode.c
@@ -0,0 +1,58 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* 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, code);
+*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
index 0000000..fc571c7
--- /dev/null
+++ b/src/auths/xtextencode.c
@@ -0,0 +1,58 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 = US 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, clear);
+
+p = US clear;
+c = len;
+while (c-- > 0)
+ if ((x = *p++) < 33 || x > 127 || x == '+' || x == '=')
+ pp += sprintf(CS pp, "+%.02x", x); /* There's always room */
+ else
+ *pp++ = x;
+
+*pp = 0;
+return code;
+}
+
+/* End of xtextencode.c */
diff --git a/src/base64.c b/src/base64.c
new file mode 100644
index 0000000..fa06a7a
--- /dev/null
+++ b/src/base64.c
@@ -0,0 +1,297 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004, 2015 */
+/* License: GPL */
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "exim.h"
+#ifdef WITH_CONTENT_SCAN /* file-IO specific decode function */
+# include "mime.h"
+
+/* 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
+};
+
+/* decode base64 MIME part */
+ssize_t
+mime_decode_base64(FILE * in, FILE * out, uschar * boundary)
+{
+uschar ibuf[MIME_MAX_LINE_LENGTH], obuf[MIME_MAX_LINE_LENGTH];
+uschar *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 (uschar * ipos = ibuf ; *ipos != '\r' && *ipos != '\n' && *ipos; ++ipos)
+ if (*ipos == '=') /* skip padding */
+ ++bytestate;
+
+ else if (mime_b64[*ipos] == 128) /* skip bad characters */
+ mime_set_anomaly(MIME_ANOMALY_BROKEN_BASE64);
+
+ /* simple state-machine */
+ else switch((bytestate++) & 3)
+ {
+ case 0:
+ *opos = mime_b64[*ipos] << 2; break;
+ case 1:
+ *opos++ |= mime_b64[*ipos] >> 4;
+ *opos = mime_b64[*ipos] << 4; break;
+ case 2:
+ *opos++ |= mime_b64[*ipos] >> 2;
+ *opos = mime_b64[*ipos] << 6; break;
+ case 3:
+ *opos++ |= mime_b64[*ipos]; break;
+ }
+
+ /* 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;
+}
+
+#endif /*WITH_CONTENT_SCAN*/
+
+/*************************************************
+ *************************************************
+ *************************************************
+ *************************************************
+ *************************************************
+ *************************************************
+ *************************************************
+ *************************************************
+ *************************************************
+ *************************************************
+ *************************************************
+ *************************************************
+ *************************************************
+ *************************************************
+ *************************************************
+ *************************************************/
+
+
+/*************************************************
+* 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
+ allocated store, and zero-terminated
+
+Returns: the number of bytes in the result,
+ or -1 if the input was malformed
+
+Whitespace in the input is ignored.
+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
+b64decode(const uschar *code, uschar **ptr)
+{
+
+int x, y;
+uschar *result;
+
+ {
+ int l = Ustrlen(code);
+ *ptr = result = store_get(1 + l/4 * 3 + l%4, code);
+ }
+
+/* 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 (isspace(x)) continue;
+ /* debug_printf("b64d: '%c'\n", x); */
+
+ if (x > 127 || (x = dec64table[x]) == 255) return -1;
+
+ while (isspace(y = *code++)) ;
+ /* debug_printf("b64d: '%c'\n", y); */
+ if (y > 127 || (y = dec64table[y]) == 255)
+ return -1;
+
+ *result++ = (x << 2) | (y >> 4);
+ /* debug_printf("b64d: -> %02x\n", result[-1]); */
+
+ while (isspace(x = *code++)) ;
+ /* debug_printf("b64d: '%c'\n", x); */
+ if (x == '=') /* endmarker, but there should be another */
+ {
+ while (isspace(x = *code++)) ;
+ /* debug_printf("b64d: '%c'\n", x); */
+ if (x != '=') return -1;
+ while (isspace(y = *code++)) ;
+ if (y != 0) return -1;
+ /* debug_printf("b64d: DONE\n"); */
+ break;
+ }
+ else
+ {
+ if (x > 127 || (x = dec64table[x]) == 255) return -1;
+ *result++ = (y << 4) | (x >> 2);
+ /* debug_printf("b64d: -> %02x\n", result[-1]); */
+
+ while (isspace(y = *code++)) ;
+ /* debug_printf("b64d: '%c'\n", y); */
+ if (y == '=')
+ {
+ while (isspace(y = *code++)) ;
+ if (y != 0) return -1;
+ /* debug_printf("b64d: DONE\n"); */
+ break;
+ }
+ else
+ {
+ if (y > 127 || (y = dec64table[y]) == 255) return -1;
+ *result++ = (x << 6) | y;
+ /* debug_printf("b64d: -> %02x\n", result[-1]); */
+ }
+ }
+ }
+
+*result = 0;
+return result - *ptr;
+}
+
+
+/*************************************************
+* 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
+ proto_mem taint indicator
+
+Returns: a pointer to the zero-terminated base 64 string, which
+ is in working store
+*/
+
+static uschar *enc64table =
+ US"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+uschar *
+b64encode_taint(const uschar * clear, int len, const void * proto_mem)
+{
+uschar * code = store_get(4*((len+2)/3) + 1, proto_mem);
+uschar * p = code;
+
+while (len-- >0)
+ {
+ 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;
+}
+
+uschar *
+b64encode(const uschar * clear, int len)
+{
+return b64encode_taint(clear, len, clear);
+}
+
+
+/* End of base64.c */
+/* vi: sw ai sw=2
+*/
diff --git a/src/blob.h b/src/blob.h
new file mode 100644
index 0000000..a3f1e24
--- /dev/null
+++ b/src/blob.h
@@ -0,0 +1,15 @@
+/*
+ * Blob - a general pointer/size item for a memory chunk
+ *
+ * Copyright (C) 2016 Exim maintainers
+ */
+
+#ifndef BLOB_H /* entire file */
+#define BLOB_H
+
+typedef struct {
+ uschar * data;
+ size_t len;
+} blob;
+
+#endif
diff --git a/src/bmi_spam.c b/src/bmi_spam.c
new file mode 100644
index 0000000..af4bc46
--- /dev/null
+++ b/src/bmi_spam.c
@@ -0,0 +1,476 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Code for calling Brightmail AntiSpam.
+ Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004
+ License: GPL */
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+
+#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, CS 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(CS 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, CS host_address);
+ bmiFreeMessage(message);
+ bmiFreeSystem(system);
+ return NULL;
+ };
+
+ /* Send envelope sender address */
+ err = bmiProcessFROM(CS 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, CS 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, CS r->address, CS r->bmi_optin);
+ if (optin != NULL)
+ bmiOptinFree(optin);
+ optin = NULL;
+ };
+ };
+
+ err = bmiAccumulateTO(CS 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, CS 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(CCS 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(CCS 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. Since we are processing message data, assume that
+ the verdict is tainted. XXX this should use a growable-string */
+
+ verdicts = store_get(1, GET_TAINTED);
+ *verdicts = '\0';
+
+ for ( err = bmiAccessFirstVerdict(message, &verdict);
+ verdict;
+ 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, GET_TAINTED);
+ 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, GET_TAINTED);
+
+ /* loop through verdicts */
+ verdict_ptr = bmi_verdicts;
+ while ((verdict_str = CCS 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 = US 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 US 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 */
+ /* option_list doesn't seem to be expanded so cannot be tainted. If it ever is we
+ will trap here */
+ rule_ptr = option_list;
+ while ((rule_num = string_nextinlist(&rule_ptr, &sep,
+ rule_buffer, sizeof(rule_buffer)))) {
+ 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
index 0000000..a9af778
--- /dev/null
+++ b/src/bmi_spam.h
@@ -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
index 0000000..f9a8feb
--- /dev/null
+++ b/src/buildconfig.c
@@ -0,0 +1,984 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 <sys/time.h>
+#include <poll.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", "USE_NDBM" };
+
+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;
+ino_t test_ino_t;
+#if ! (__STDC_VERSION__ >= 199901L)
+size_t test_size_t = 0;
+ssize_t test_ssize_t = 0;
+unsigned long test_ulong_t = 0L;
+unsigned int test_uint_t = 0;
+#endif
+long test_long_t = 0;
+long long test_longlong_t = 0;
+int test_int_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");
+else
+ fprintf(new, "# define OFF_T_FMT \"%%ld\"\n");
+fprintf(new, "#endif\n\n");
+
+fprintf(new, "#ifndef LONGLONG_T\n");
+if (sizeof(test_longlong_t) > sizeof(test_long_t))
+ fprintf(new, "# define LONGLONG_T long long int\n");
+else
+ 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");
+
+fprintf(new, "#ifndef INO_T_FMT\n");
+if (sizeof(test_ino_t) > sizeof(test_long_t))
+ fprintf(new, "# define INO_T_FMT \"%%llu\"\n");
+else
+ fprintf(new, "# define INO_T_FMT \"%%lu\"\n");
+fprintf(new, "#endif\n\n");
+
+fprintf(new, "#ifndef PID_T_FMT\n");
+fprintf(new, "# define PID_T_FMT \"%%lu\"\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 if (sizeof(test_size_t) > sizeof (test_uint_t))
+ fprintf(new, "#define SIZE_T_FMT \"%%lu\"\n");
+else
+ fprintf(new, "#define SIZE_T_FMT \"%%u\"\n");
+
+if (sizeof(test_ssize_t) > sizeof(test_long_t))
+ fprintf(new, "#define SSIZE_T_FMT \"%%lld\"\n");
+else if (sizeof(test_ssize_t) > sizeof(test_int_t))
+ fprintf(new, "#define SSIZE_T_FMT \"%%ld\"\n");
+else
+ fprintf(new, "#define SSIZE_T_FMT \"%%d\"\n");
+#endif
+
+/* Now search the makefile for certain settings */
+
+if (!(base = fopen("Makefile", "rb")))
+ {
+ 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");
+ free(vector);
+ }
+ continue;
+ }
+
+ /* WITH_CONTENT_SCAN is another special case: it must be set if it or
+ EXPERIMENTAL_DCC is set. */
+
+ if (strcmp(name, "WITH_CONTENT_SCAN") == 0)
+ {
+ char *wcs = getenv("WITH_CONTENT_SCAN");
+ char *dcc = getenv("EXPERIMENTAL_DCC");
+ fprintf(new, wcs || dcc
+ ? "#define WITH_CONTENT_SCAN yes\n"
+ : "/* WITH_CONTENT_SCAN not set */\n");
+ continue;
+ }
+
+ /* DISABLE_DKIM is special; must be forced if DISABLE_TLS */
+ if (strcmp(name, "DISABLE_DKIM") == 0)
+ {
+ char *d_dkim = getenv("DISABLE_DKIM");
+ char *notls = getenv("DISABLE_TLS");
+
+ if (d_dkim)
+ fprintf(new, "#define DISABLE_DKIM yes\n");
+ else if (notls)
+ fprintf(new, "#define DISABLE_DKIM yes /* forced by lack of TLS */\n");
+ else
+ fprintf(new, "/* DISABLE_DKIM 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
+ {
+ strncpy(buffer, ss, sizeof(buffer));
+ buffer[sizeof(buffer)-1] = 0;
+ }
+ 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");
+
+/* Check poll() for timer functionality.
+Some OS' have released with it broken. */
+
+ {
+ struct timeval before, after;
+ size_t us;
+
+ gettimeofday(&before, NULL);
+ (void) poll(NULL, 0, 500);
+ gettimeofday(&after, NULL);
+
+ us = (after.tv_sec - before.tv_sec) * 1000000 +
+ (after.tv_usec - before.tv_usec);
+
+ if (us < 400000)
+ fprintf(new, "#define NO_POLL_H\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
index 0000000..1f38b58
--- /dev/null
+++ b/src/child.c
@@ -0,0 +1,556 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* 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
+*/
+
+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 ? *pcount : 0;
+uschar **argv;
+
+argv = store_get((extra + acount + MAX_CLMACROS + 24) * sizeof(char *), GET_UNTAINTED);
+
+/* In all case, the list starts out with the path, any macros, and a changed
+config file. */
+
+argv[n++] = exim_path; /* assume untainted */
+if (clmacro_count > 0)
+ {
+ memcpy(argv + n, clmacros, clmacro_count * sizeof(uschar *));
+ n += clmacro_count;
+ }
+if (f.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 (debug_fd > 2)
+ {
+ int flags = fcntl(debug_fd, F_GETFD);
+ if (flags != -1) (void)fcntl(debug_fd, F_SETFD, flags & ~FD_CLOEXEC);
+ close(2);
+ dup2(debug_fd, 2);
+ close(debug_fd);
+ }
+ }
+ }
+ if (debug_pretrigger_buf)
+ { argv[n++] = US"-dp"; argv[n++] = string_sprintf("0x%x", debug_pretrigger_bsize); }
+ if (dtrigger_selector != 0)
+ argv[n++] = string_sprintf("-dt=0x%x", dtrigger_selector);
+ DEBUG(D_any)
+ {
+ argv[n++] = US"-MCd";
+ argv[n++] = US process_purpose;
+ }
+ if (!f.testsuite_delays) argv[n++] = US"-odd";
+ if (f.dont_deliver) argv[n++] = US"-N";
+ if (f.queue_smtp) argv[n++] = US"-odqs";
+ if (f.synchronous_delivery) argv[n++] = US"-odi";
+ if (connection_max_messages >= 0)
+ argv[n++] = string_sprintf("-oB%d", connection_max_messages);
+ if (*queue_name)
+ { argv[n++] = US"-MCG"; argv[n++] = queue_name; }
+ }
+
+/* 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) *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(CUSS 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
+ purpose of the child process, for debug
+Returns: pid of the created process or -1 if anything has gone wrong
+*/
+
+pid_t
+child_open_exim_function(int * fdptr, const uschar * purpose)
+{
+return child_open_exim2_function(fdptr, US"<>", bounce_sender_authentication,
+ purpose);
+}
+
+
+/* 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
+ purpose of the child process, for debug
+
+Returns: pid of the created process or -1 if anything has gone wrong
+*/
+
+pid_t
+child_open_exim2_function(int * fdptr, uschar * sender,
+ uschar * sender_authentication, const uschar * purpose)
+{
+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 = exim_fork(purpose);
+
+/* 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 appropriate 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 (f.running_in_test_harness && !queue_only)
+ {
+ if (sender_authentication)
+ 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)
+ 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. */
+ }
+ }
+
+testharness_pause_ms(100); /* let child work even longer, for exec */
+
+/* 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
+ purpose for debug: reason for running the task
+
+Returns: the pid of the created process or -1 if anything has gone wrong
+*/
+
+pid_t
+child_open_uid(const uschar **argv, const uschar **envp, int newumask,
+ uid_t *newuid, gid_t *newgid, int *infdptr, int *outfdptr, uschar *wd,
+ BOOL make_leader, const uschar * purpose)
+{
+int save_errno;
+int inpfd[2], outpfd[2];
+pid_t pid;
+
+if (is_tainted(argv[0]))
+ {
+ log_write(0, LOG_MAIN | LOG_PANIC, "Attempt to exec tainted path: '%s'", argv[0]);
+ errno = EPERM;
+ return (pid_t)(-1);
+ }
+
+/* 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 = exim_fork(purpose);
+
+/* 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 && 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 && 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 && 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) execve(CS argv[0], (char *const *)argv, (char *const *)envp);
+ else execv(CS argv[0], (char *const *)argv);
+
+ /* 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
+ purpose for debug: reason for running the task
+
+Returns: the pid of the created process or -1 if anything has gone wrong
+*/
+
+pid_t
+child_open_function(uschar **argv, uschar **envp, int newumask, int *infdptr,
+ int *outfdptr, BOOL make_leader, const uschar * purpose)
+{
+return child_open_uid(CUSS argv, CUSS envp, newumask, NULL, NULL,
+ infdptr, outfdptr, NULL, make_leader, purpose);
+}
+
+
+
+
+/*************************************************
+* 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;
+ yield = lowbyte == 0 ? (status >> 8) & 255 : -lowbyte;
+ break;
+ }
+ if (rc < 0)
+ {
+ /* This "shouldn't happen" test does happen on MacOS: for some reason
+ I do not understand we seems to get an alarm signal despite not having
+ an active alarm set. There seems to be only one, so just go round again. */
+
+ if (errno == EINTR && sigalrm_seen && timeout <= 0) continue;
+
+ yield = (errno == EINTR && sigalrm_seen) ? -256 : -257;
+ break;
+ }
+ }
+
+if (timeout > 0) ALARM_CLR(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
index 0000000..d00491f
--- /dev/null
+++ b/src/cnumber.h
@@ -0,0 +1 @@
+1
diff --git a/src/config.h.defaults b/src/config.h.defaults
new file mode 100644
index 0000000..25ab755
--- /dev/null
+++ b/src/config.h.defaults
@@ -0,0 +1,235 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2018 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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.
+
+Do not put spaces between # and the 'define'.
+*/
+
+#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_EXTERNAL
+#define AUTH_GSASL
+#define AUTH_HEIMDAL_GSSAPI
+#define AUTH_PLAINTEXT
+#define AUTH_SPA
+#define AUTH_TLS
+
+#define AUTH_VARS 4
+
+#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_CLIENT_CMD_LOG
+#define DISABLE_D_OPTION
+#define DISABLE_DNSSEC
+#define DISABLE_DKIM
+#define DISABLE_EVENT
+#define DISABLE_OCSP
+#define DISABLE_PIPE_CONNECT
+#define DISABLE_PRDR
+#define DISABLE_QUEUE_RAMP
+#define DISABLE_TLS
+#define DISABLE_TLS_RESUME
+
+#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_LOCAL_SCAN
+#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_JSON
+#define LOOKUP_LDAP
+#define LOOKUP_LMDB
+#define LOOKUP_LSEARCH
+#define LOOKUP_MYSQL
+#define LOOKUP_NIS
+#define LOOKUP_NISPLUS
+#define LOOKUP_ORACLE
+#define LOOKUP_PASSWD
+#define LOOKUP_PGSQL
+#define LOOKUP_REDIS
+#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 REGEX_VARS 9
+
+#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_CRYPTEQ
+#define SUPPORT_DANE
+#define SUPPORT_DMARC
+#define DMARC_TLD_FILE "/etc/exim/opendmarc.tlds"
+#define SUPPORT_I18N
+#define SUPPORT_I18N_2008
+#define SUPPORT_MAILDIR
+#define SUPPORT_MAILSTORE
+#define SUPPORT_MBX
+#define SUPPORT_MOVE_FROZEN_MESSAGES
+#define SUPPORT_PAM
+#define SUPPORT_PROXY
+#define SUPPORT_SOCKS
+#define SUPPORT_SPF
+#define SUPPORT_SRS
+#define SUPPORT_TRANSLATE_IP_ADDRESS
+
+#define SYSLOG_LOG_PID
+#define SYSLOG_LONG_LINES
+
+#define TCP_WRAPPERS_DAEMON_NAME "exim"
+#define TIMEZONE_DEFAULT
+#define EXIM_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_NDBM
+#define USE_OPENSSL
+#define USE_READLINE
+#define USE_TCP_WRAPPERS
+#define USE_TDB
+
+#define WHITELIST_D_MACROS
+
+#define WITH_CONTENT_SCAN
+#define DISABLE_MAL_FFROTD
+#define DISABLE_MAL_FFROT6D
+#define DISABLE_MAL_DRWEB
+#define DISABLE_MAL_AVE
+#define DISABLE_MAL_FSECURE
+#define DISABLE_MAL_KAV
+#define DISABLE_MAL_SOPHIE
+#define DISABLE_MAL_CLAM
+#define DISABLE_MAL_MKS
+#define DISABLE_MAL_AVAST
+#define DISABLE_MAL_SOCK
+#define DISABLE_MAL_CMDLINE
+
+/* EXPERIMENTAL features */
+#define EXPERIMENTAL_ARC
+#define EXPERIMENTAL_BRIGHTMAIL
+#define EXPERIMENTAL_DCC
+#define EXPERIMENTAL_DSN_INFO
+#define EXPERIMENTAL_ESMTP_LIMITS
+#define EXPERIMENTAL_QUEUEFILE
+
+
+/* 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_ARITH_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
index 0000000..3761daf
--- /dev/null
+++ b/src/configure.default
@@ -0,0 +1,1015 @@
+######################################################################
+# 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 website.
+
+
+# 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 ###########
+
+
+
+######################################################################
+# MACROS #
+######################################################################
+#
+
+# If you want to use a smarthost instead of sending directly to recipient
+# domains, uncomment this macro definition and set a real hostname.
+# An appropriately privileged user can then redirect email on the command-line
+# in emergencies, via -D.
+#
+# ROUTER_SMARTHOST=MAIL.HOSTNAME.FOR.CENTRAL.SERVER.EXAMPLE
+
+######################################################################
+# 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
+.ifdef _HAVE_PRDR
+acl_smtp_data_prdr = acl_check_prdr
+.endif
+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 change the
+# following option so that Exim disallows certain clients from makeing encrypted
+# connections. The default is to allow all.
+# 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 TLS is allowed here.
+
+# This is equivalent to the default.
+
+# 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
+
+# For OpenSSL, prefer EC- over RSA-authenticated ciphers
+.ifdef _HAVE_OPENSSL
+tls_require_ciphers = ECDSA:RSA:!COMPLEMENTOFDEFAULT
+.endif
+
+# Don't offer resumption to (most) MUAs, who we don't want to reuse
+# tickets. Once the TLS extension for vended ticket numbers comes
+# though, re-examine since resumption on a single-use ticket is still a benefit.
+.ifdef _HAVE_TLS_RESUME
+tls_resumption_hosts = ${if inlist {$received_port}{587:465} {:}{*}}
+.endif
+
+# 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 setting below causes Exim to try to initialize the system resolver
+# library with DNSSEC support. It has no effect if your library lacks
+# DNSSEC support.
+
+dns_dnssec_ok = 1
+
+
+# The settings below 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. and to
+# disabled for release 4.86)
+#
+#rfc1413_hosts = *
+#rfc1413_query_timeout = 5s
+
+
+# Enable an efficiency feature. We advertise the feature; clients
+# may request to use it. For multi-recipient mails we then can
+# reject or accept per-user after the message is received.
+# This supports recipient-dependent content filtering; without it
+# you have to temp-reject any recipients after the first that have
+# incompatible filtering, and do the filtering in the data ACL.
+# Even with this enabled, you must support the old style for peers
+# not flagging support for PRDR (visible via $prdr_requested).
+#
+.ifdef _HAVE_PRDR
+prdr_enable = true
+.endif
+
+
+# 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).
+
+
+# Unless you run a high-volume site you probably want more logging
+# detail than the default. Adjust to suit.
+
+log_selector = +smtp_protocol_error +smtp_syntax_error \
+ +tls_certificate_verified
+
+
+# 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 is 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
+
+
+# Exim does not make use of environment variables itself. However,
+# libraries that Exim uses (e.g. LDAP) depend on specific environment settings.
+# There are two lists: keep_environment for the variables we trust, and
+# add_environment for variables we want to set to a specific value.
+# Note that TZ is handled separately by the timezone runtime option
+# and TIMEZONE_DEFAULT buildtime option.
+
+# keep_environment = ^LDAP
+# add_environment = PATH=/usr/bin::/bin
+
+
+
+######################################################################
+# 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
+
+ # Reject all RCPT commands after too many bad recipients
+ # This is partly a defense against spam abuse and partly attacker abuse.
+ # Real senders should manage, by the time they get to 10 RCPT directives,
+ # to have had at least half of them be real addresses.
+ #
+ # This is a lightweight check and can protect you against repeated
+ # invocations of more heavy-weight checks which would come after it.
+
+ deny condition = ${if and {\
+ {>{$rcpt_count}{10}}\
+ {<{$recipients_count}{${eval:$rcpt_count/2}}} }}
+ message = Rejected for too many bad recipients
+ logwrite = REJECT [$sender_host_address]: bad recipient count high [${eval:$rcpt_count-$recipients_count}]
+
+ # 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 dnslists = black.list.example
+ # message = rejected because $sender_host_address is in a black list at $dnslist_domain\n$dnslist_text
+ #
+ # 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
+ #############################################################################
+
+ #############################################################################
+ # If doing per-user content filtering then recipients with filters different
+ # to the first recipient must be deferred unless the sender talks PRDR.
+ #
+ # defer !condition = $prdr_requested
+ # condition = ${if > {0}{$recipients_count}}
+ # condition = ${if !eq {$acl_m_content_filter} \
+ # {${lookup PER_RCPT_CONTENT_FILTER}}}
+ # warn !condition = $prdr_requested
+ # condition = ${if > {0}{$recipients_count}}
+ # set acl_m_content_filter = ${lookup PER_RCPT_CONTENT_FILTER}
+ #############################################################################
+
+ # At this point, the address has passed all the checks that have been
+ # configured, so we accept it unconditionally.
+
+ accept
+
+
+# This ACL is used once per recipient, for multi-recipient messages, if
+# we advertised PRDR. It can be used to perform receipient-dependent
+# header- and body- based filtering and rejections.
+# We set a variable to record that PRDR was active used, so that checking
+# in the data ACL can be skipped.
+
+.ifdef _HAVE_PRDR
+acl_check_prdr:
+ warn set acl_m_did_prdr = y
+
+ #############################################################################
+ # do lookup on filtering, with $local_part@$domain, deny on filter match
+ #
+ # deny set acl_m_content_filter = ${lookup PER_RCPT_CONTENT_FILTER}
+ # condition = ...
+ #############################################################################
+
+ accept
+.endif
+
+# 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 an overlong line. Per the standards
+ # we should never receive one such via SMTP.
+ #
+ deny condition = ${if > {$max_received_linelength}{998}}
+ message = maximum allowed line length is 998 octets, \
+ got $max_received_linelength
+
+ # Deny if the headers contain badly-formed addresses.
+ #
+ deny !verify = header_syntax
+ message = header syntax
+ log_message = header syntax ($acl_verify_message)
+
+ # 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
+
+ #############################################################################
+ # No more tests if PRDR was actively used.
+ # accept condition = ${if def:acl_m_did_prdr}
+ #
+ # To get here, all message recipients must have identical per-user
+ # content filtering (enforced by RCPT ACL). Do lookup for filter
+ # and deny on match.
+ #
+ # deny set acl_m_content_filter = ${lookup PER_RCPT_CONTENT_FILTER}
+ # condition = ...
+ #############################################################################
+
+
+ # 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 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. The hostname in route_data comes from the
+# macro defined at the top of the file. If not defined, then we'll use the
+# dnslookup router below instead.
+# Beware that the hostname is specified again in the Transport.
+
+.ifdef ROUTER_SMARTHOST
+
+smarthost:
+ driver = manualroute
+ domains = ! +local_domains
+ transport = smarthost_smtp
+ route_data = ROUTER_SMARTHOST
+ ignore_target_hosts = <; 0.0.0.0 ; 127.0.0.0/8 ; ::1
+ no_more
+
+.else
+
+# 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 closes the ROUTER_SMARTHOST ifdef around the choice of routing for
+# off-site mail.
+.endif
+
+
+# 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
+.ifdef _HAVE_TLS_RESUME
+ tls_resumption_hosts = *
+.endif
+
+
+# This transport is used for delivering messages to a smarthost, if the
+# smarthost router is enabled. This starts from the same basis as
+# "remote_smtp" but then turns on various security options, because
+# we assume that if you're told "use smarthost.example.org as the smarthost"
+# then there will be TLS available, with a verifiable certificate for that
+# hostname, using decent TLS.
+
+smarthost_smtp:
+ driver = smtp
+ multi_domain
+ #
+.ifdef _HAVE_TLS
+ # Comment out any of these which you have to, then file a Support
+ # request with your smarthost provider to get things fixed:
+ hosts_require_tls = *
+ tls_verify_hosts = *
+ # As long as tls_verify_hosts is enabled, this this will have no effect,
+ # but if you have to comment it out then this will at least log whether
+ # you succeed or not:
+ tls_try_verify_hosts = *
+ #
+ # The SNI name should match the name which we'll expect to verify;
+ # many mail systems don't use SNI and this doesn't matter, but if it does,
+ # we need to send a name which the remote site will recognize.
+ # This _should_ be the name which the smarthost operators specified as
+ # the hostname for sending your mail to.
+ tls_sni = ROUTER_SMARTHOST
+ #
+.ifdef _HAVE_OPENSSL
+ tls_require_ciphers = HIGH:!aNULL:@STRENGTH
+.endif
+.ifdef _HAVE_GNUTLS
+ tls_require_ciphers = SECURE192:-VERS-SSL3.0:-VERS-TLS1.0:-VERS-TLS1.1
+.endif
+.ifdef _HAVE_TLS_RESUME
+ tls_resumption_hosts = *
+.endif
+.endif
+
+
+# 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_data
+ 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
index 0000000..d0b94d1
--- /dev/null
+++ b/src/convert4r3.src
@@ -0,0 +1,1382 @@
+#! PERL_COMMAND
+
+# 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.
+
+use warnings;
+BEGIN { pop @INC if $INC[-1] eq '.' };
+
+use Getopt::Long;
+use File::Basename;
+
+GetOptions(
+ 'version' => sub {
+ print basename($0) . ": $0\n",
+ "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION\n",
+ "perl(runtime): $^V\n";
+ exit 0;
+ },
+);
+
+##################################################
+# 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
index 0000000..47987fc
--- /dev/null
+++ b/src/convert4r4.src
@@ -0,0 +1,2527 @@
+#! PERL_COMMAND
+
+# 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.
+
+use warnings;
+BEGIN { pop @INC if $INC[-1] eq '.' };
+
+use Getopt::Long;
+use File::Basename;
+
+GetOptions(
+ 'version' => sub {
+ print basename($0) . ": $0\n",
+ "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION\n",
+ "perl(runtime): $^V\n";
+ exit 0;
+ },
+);
+
+# 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
index 0000000..56353c3
--- /dev/null
+++ b/src/crypt16.c
@@ -0,0 +1,77 @@
+/*
+ * 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]; /* Not threadsafe; like crypt() */
+static char s2[3];
+char *p;
+
+/* Clear the string of any previous data */
+memset (res, 0, sizeof (res));
+
+/* crypt the first part */
+if (!(p = crypt (key, salt))) return NULL;
+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
index 0000000..8e8a515
--- /dev/null
+++ b/src/daemon.c
@@ -0,0 +1,2654 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 assignments except as initializers in declarations). */
+
+static smtp_slot empty_smtp_slot = { .pid = 0, .host_address = NULL };
+
+
+
+/*************************************************
+* Local static variables *
+*************************************************/
+
+static SIGNAL_BOOL sigchld_seen;
+static SIGNAL_BOOL sighup_seen;
+static SIGNAL_BOOL sigterm_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)
+{
+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)
+{
+os_non_restarting_signal(SIGCHLD, SIG_DFL);
+sigchld_seen = TRUE;
+}
+
+
+/* SIGTERM handler. Try to get the daemon pid file removed
+before exiting. */
+
+static void
+main_sigterm_handler(int sig)
+{
+sigterm_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) smtp_printf("421 %s\r\n", FALSE, smtp_msg);
+}
+
+
+
+
+/*************************************************
+*************************************************/
+
+#ifndef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+static void
+unlink_notifier_socket(void)
+{
+uschar * s = expand_string(notifier_socket);
+DEBUG(D_any) debug_printf("unlinking notifier socket %s\n", s);
+Uunlink(s);
+}
+#endif
+
+
+static void
+close_daemon_sockets(int daemon_notifier_fd,
+ struct pollfd * fd_polls, int listen_socket_count)
+{
+if (daemon_notifier_fd >= 0)
+ {
+ (void) close(daemon_notifier_fd);
+ daemon_notifier_fd = -1;
+#ifndef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+ unlink_notifier_socket();
+#endif
+ }
+
+for (int i = 0; i < listen_socket_count; i++) (void) close(fd_polls[i].fd);
+}
+
+
+/*************************************************
+* 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:
+ fd_polls 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(struct pollfd *fd_polls, 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 save_log_selector = *log_selector;
+gstring * whofrom;
+
+rmark reset_point = store_mark();
+
+/* 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. */
+
+if (!(smtp_out = fdopen(accept_socket, "wb")))
+ {
+ never_error(US"daemon: fdopen() for smtp_out failed", US"", errno);
+ goto ERROR_RETURN;
+ }
+
+if ((dup_accept_socket = dup(accept_socket)) < 0)
+ {
+ never_error(US"daemon: couldn't dup socket descriptor",
+ US"Connection setup failed", errno);
+ goto ERROR_RETURN;
+ }
+
+if (!(smtp_in = fdopen(dup_accept_socket, "rb")))
+ {
+ 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", FALSE);
+ 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(NULL, 3, "[", sender_host_address, "]");
+
+if (LOGGING(incoming_port))
+ whofrom = string_fmt_append(whofrom, ":%d", sender_host_port);
+
+if (LOGGING(incoming_interface))
+ whofrom = string_fmt_append(whofrom, " I=[%s]:%d",
+ interface_address, interface_port);
+
+(void) string_from_gstring(whofrom); /* 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", FALSE);
+ log_write(L_connection_reject,
+ LOG_MAIN, "Connection from %s refused: too many connections",
+ whofrom->s);
+ 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 && 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", FALSE);
+ log_write(L_connection_reject,
+ LOG_MAIN, "Connection from %s refused: load average = %.2f",
+ whofrom->s, (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)
+ {
+ uschar *expanded = expand_string(smtp_accept_max_per_host);
+ if (!expanded)
+ {
+ if (!f.expand_string_forcedfail)
+ log_write(0, LOG_MAIN|LOG_PANIC, "expansion of smtp_accept_max_per_host "
+ "failed for %s: %s", whofrom->s, 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)
+ log_write(0, LOG_MAIN|LOG_PANIC, "expansion of smtp_accept_max_per_host "
+ "for %s contains non-digit: %s", whofrom->s, 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 host_accept_count = 0;
+ int other_host_count = 0; /* keep a count of non matches to optimise */
+
+ for (int i = 0; i < smtp_accept_max; ++i)
+ if (smtp_slots[i].host_address)
+ {
+ 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", FALSE);
+ log_write(L_connection_reject,
+ LOG_MAIN, "Connection from %s refused: too many connections "
+ "from that IP address", whofrom->s);
+ search_tidyup();
+ 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 (LOGGING(smtp_connection))
+ {
+ uschar *list = hosts_connection_nolog;
+ memset(sender_host_cache, 0, sizeof(sender_host_cache));
+ if (list && verify_check_host(&list) == OK)
+ save_log_selector &= ~L_smtp_connection;
+ else
+ log_write(L_smtp_connection, LOG_MAIN, "SMTP connection from %s "
+ "(TCP/IP connection count = %d)", whofrom->s, 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 = exim_fork(US"daemon-accept");
+
+/* Handle the child process */
+
+if (pid == 0)
+ {
+ 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 */
+
+ /* If the listen backlog was over the monitoring level, log it. */
+
+ if (smtp_listen_backlog > smtp_backlog_monitor)
+ log_write(0, LOG_MAIN, "listen backlog %d I=[%s]:%d",
+ smtp_listen_backlog, interface_address, interface_port);
+
+ /* May have been modified for the subprocess */
+
+ *log_selector = save_log_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)
+ {
+ uschar * nah = expand_string(raw_active_hostname);
+ if (!nah)
+ {
+ if (!f.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", FALSE);
+ mac_smtp_fflush();
+ search_tidyup();
+ exim_underbar_exit(EXIT_FAILURE);
+ }
+ }
+ else if (*nah) 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. */
+
+ close_daemon_sockets(daemon_notifier_fd, fd_polls, listen_socket_count);
+
+ /* 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
+ signal(SIGTERM, SIG_DFL);
+ signal(SIGINT, SIG_DFL);
+
+ /* 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 (f.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 (f.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();
+ exim_underbar_exit(EXIT_SUCCESS);
+ }
+
+ for (;;)
+ {
+ int rc;
+ message_id[0] = 0; /* Clear out any previous message_id */
+ reset_point = store_mark(); /* 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 */
+ {
+ cancel_cutthrough_connection(TRUE, US"receive dropped");
+ mac_smtp_fflush();
+ smtp_log_no_mail(); /* Log no mail if configured */
+ exim_underbar_exit(EXIT_SUCCESS);
+ }
+ if (message_id[0] == 0) continue; /* No message was accepted */
+ }
+ else /* bad smtp_setup_msg() */
+ {
+ if (smtp_out)
+ {
+ int fd = fileno(smtp_in);
+ uschar buf[128];
+
+ mac_smtp_fflush();
+ /* drain socket, for clean TCP FINs */
+ if (fcntl(fd, F_SETFL, O_NONBLOCK) == 0)
+ for(int i = 16; read(fd, buf, sizeof(buf)) > 0 && i > 0; ) i--;
+ }
+ cancel_cutthrough_connection(TRUE, US"message setup dropped");
+ search_tidyup();
+ smtp_log_no_mail(); /* Log no mail if configured */
+
+ /*XXX should we pause briefly, hoping that the client will be the
+ active TCP closer hence get the TCP_WAIT endpoint? */
+ DEBUG(D_receive) debug_printf("SMTP>>(close on process exit)\n");
+ exim_underbar_exit(rc ? EXIT_FAILURE : EXIT_SUCCESS);
+ }
+
+ /* Show the recipients when debugging */
+
+ DEBUG(D_receive)
+ {
+ if (sender_address)
+ debug_printf("Sender: %s\n", sender_address);
+ if (recipients_list)
+ {
+ debug_printf("Recipients:\n");
+ for (int 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 */
+
+ {
+ int r = receive_messagecount;
+ BOOL q = f.queue_only_policy;
+ smtp_reset(reset_point);
+ reset_point = NULL;
+ f.queue_only_policy = q;
+ receive_messagecount = r;
+ }
+
+ /* 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. */
+
+ if ( !(local_queue_only = session_local_queue_only)
+ && queue_only_load >= 0
+ && (local_queue_only = (load_average = OS_GETLOADAVG()) > queue_only_load)
+ )
+ {
+ 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 ( (!f.queue_only_policy || f.queue_smtp)
+ && !f.deliver_freeze)
+ {
+ pid_t dpid;
+
+ /* We used to flush smtp_out before forking so that buffered data was not
+ duplicated, but now we want to pipeline the responses for data and quit.
+ Instead, hard-close the fd underlying smtp_out right after fork to discard
+ the data buffer. */
+
+ if ((dpid = exim_fork(US"daemon-accept-delivery")) == 0)
+ {
+ (void)fclose(smtp_in);
+ (void)close(fileno(smtp_out));
+ (void)fclose(smtp_out);
+ smtp_in = smtp_out = NULL;
+
+ /* Don't ever molest the parent's SSL connection, but do clean up
+ the data structures if necessary. */
+
+#ifndef DISABLE_TLS
+ tls_close(NULL, TLS_NO_SHUTDOWN);
+#endif
+
+ /* Reset SIGHUP and SIGCHLD in the child in both cases. */
+
+ signal(SIGHUP, SIG_DFL);
+ signal(SIGCHLD, SIG_DFL);
+ signal(SIGTERM, SIG_DFL);
+ signal(SIGINT, SIG_DFL);
+
+ if (geteuid() != root_uid && !deliver_drop_privilege)
+ {
+ signal(SIGALRM, SIG_DFL);
+ delivery_re_exec(CEE_EXEC_PANIC);
+ /* 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();
+ exim_underbar_exit(EXIT_SUCCESS);
+ }
+
+ if (dpid > 0)
+ {
+ release_cutthrough_connection(US"passed for delivery");
+ DEBUG(D_any) debug_printf("forked delivery process %d\n", (int)dpid);
+ }
+ else
+ {
+ cancel_cutthrough_connection(TRUE, US"delivery fork failed");
+ 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
+ {
+ for (int i = 0; i < smtp_accept_max; ++i)
+ if (smtp_slots[i].pid <= 0)
+ {
+ smtp_slots[i].pid = pid;
+ /* Connection closes come asyncronously, so we cannot stack this store */
+ if (smtp_accept_max_per_host)
+ 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)
+ {
+ 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)
+ {
+ 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. */
+
+log_close_all();
+interface_address = sender_host_name = sender_host_address = NULL;
+store_reset(reset_point);
+}
+
+
+
+
+/*************************************************
+* 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)
+ {
+ 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)
+ {
+ int i;
+ for (i = 0; i < smtp_accept_max; i++)
+ if (smtp_slots[i].pid == pid)
+ {
+ if (smtp_slots[i].host_address)
+ 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)
+ {
+ int max = atoi(CS expand_string(queue_run_max));
+ for (int i = 0; i < 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;
+ }
+ }
+ }
+}
+
+
+static void
+set_pid_file_path(void)
+{
+if (override_pid_file_path)
+ pid_file_path = override_pid_file_path;
+
+if (!*pid_file_path)
+ pid_file_path = string_sprintf("%s/exim-daemon.pid", spool_directory);
+
+if (pid_file_path[0] != '/')
+ log_write(0, LOG_PANIC_DIE, "pid file path %s must be absolute\n", pid_file_path);
+}
+
+
+enum pid_op { PID_WRITE, PID_CHECK, PID_DELETE };
+
+/* Do various pid file operations as safe as possible. Ideally we'd just
+drop the privileges for creation of the pid file and not care at all about removal of
+the file. FIXME.
+Returns: true on success, false + errno==EACCES otherwise
+*/
+
+static BOOL
+operate_on_pid_file(const enum pid_op operation, const pid_t pid)
+{
+char pid_line[sizeof(int) * 3 + 2];
+const int pid_len = snprintf(pid_line, sizeof(pid_line), "%d\n", (int)pid);
+BOOL lines_match = FALSE;
+uschar * path, * base, * dir;
+
+const int dir_flags = O_RDONLY | O_NONBLOCK;
+const int base_flags = O_NOFOLLOW | O_NONBLOCK;
+const mode_t base_mode = 0644;
+struct stat sb;
+int cwd_fd = -1, dir_fd = -1, base_fd = -1;
+BOOL success = FALSE;
+errno = EACCES;
+
+set_pid_file_path();
+if (!f.running_in_test_harness && real_uid != root_uid && real_uid != exim_uid) goto cleanup;
+if (pid_len < 2 || pid_len >= (int)sizeof(pid_line)) goto cleanup;
+
+path = string_copy(pid_file_path);
+if ((base = Ustrrchr(path, '/')) == NULL) /* should not happen, but who knows */
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "pid file path \"%s\" does not contain a '/'", pid_file_path);
+
+dir = base != path ? path : US"/";
+*base++ = '\0';
+
+if (!dir || !*dir || *dir != '/') goto cleanup;
+if (!base || !*base || Ustrchr(base, '/') != NULL) goto cleanup;
+
+cwd_fd = open(".", dir_flags);
+if (cwd_fd < 0 || fstat(cwd_fd, &sb) != 0 || !S_ISDIR(sb.st_mode)) goto cleanup;
+dir_fd = open(CS dir, dir_flags);
+if (dir_fd < 0 || fstat(dir_fd, &sb) != 0 || !S_ISDIR(sb.st_mode)) goto cleanup;
+
+/* emulate openat */
+if (fchdir(dir_fd) != 0) goto cleanup;
+base_fd = open(CS base, O_RDONLY | base_flags);
+if (fchdir(cwd_fd) != 0)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "can't return to previous working dir: %s", strerror(errno));
+
+if (base_fd >= 0)
+ {
+ char line[sizeof(pid_line)];
+ ssize_t len = -1;
+
+ if (fstat(base_fd, &sb) != 0 || !S_ISREG(sb.st_mode)) goto cleanup;
+ if ((sb.st_mode & 07777) != base_mode || sb.st_nlink != 1) goto cleanup;
+ if (sb.st_size < 2 || sb.st_size >= (off_t)sizeof(line)) goto cleanup;
+
+ len = read(base_fd, line, sizeof(line));
+ if (len != (ssize_t)sb.st_size) goto cleanup;
+ line[len] = '\0';
+
+ if (strspn(line, "0123456789") != (size_t)len-1) goto cleanup;
+ if (line[len-1] != '\n') goto cleanup;
+ lines_match = len == pid_len && strcmp(line, pid_line) == 0;
+ }
+
+if (operation == PID_WRITE)
+ {
+ if (!lines_match)
+ {
+ if (base_fd >= 0)
+ {
+ int error = -1;
+ /* emulate unlinkat */
+ if (fchdir(dir_fd) != 0) goto cleanup;
+ error = unlink(CS base);
+ if (fchdir(cwd_fd) != 0)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "can't return to previous working dir: %s", strerror(errno));
+ if (error) goto cleanup;
+ (void)close(base_fd);
+ base_fd = -1;
+ }
+ /* emulate openat */
+ if (fchdir(dir_fd) != 0) goto cleanup;
+ base_fd = open(CS base, O_WRONLY | O_CREAT | O_EXCL | base_flags, base_mode);
+ if (fchdir(cwd_fd) != 0)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "can't return to previous working dir: %s", strerror(errno));
+ if (base_fd < 0) goto cleanup;
+ if (fchmod(base_fd, base_mode) != 0) goto cleanup;
+ if (write(base_fd, pid_line, pid_len) != pid_len) goto cleanup;
+ DEBUG(D_any) debug_printf("pid written to %s\n", pid_file_path);
+ }
+ }
+else
+ {
+ if (!lines_match) goto cleanup;
+ if (operation == PID_DELETE)
+ {
+ int error = -1;
+ /* emulate unlinkat */
+ if (fchdir(dir_fd) != 0) goto cleanup;
+ error = unlink(CS base);
+ if (fchdir(cwd_fd) != 0)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "can't return to previous working dir: %s", strerror(errno));
+ if (error) goto cleanup;
+ }
+ }
+
+success = TRUE;
+errno = 0;
+
+cleanup:
+if (cwd_fd >= 0) (void)close(cwd_fd);
+if (dir_fd >= 0) (void)close(dir_fd);
+if (base_fd >= 0) (void)close(base_fd);
+return success;
+}
+
+
+/* Remove the daemon's pidfile. Note: runs with root privilege,
+as a direct child of the daemon. Does not return. */
+
+void
+delete_pid_file(void)
+{
+const BOOL success = operate_on_pid_file(PID_DELETE, getppid());
+
+DEBUG(D_any)
+ debug_printf("delete pid file %s %s: %s\n", pid_file_path,
+ success ? "success" : "failure", strerror(errno));
+
+exim_exit(EXIT_SUCCESS);
+}
+
+
+/* Called by the daemon; exec a child to get the pid file deleted
+since we may require privs for the containing directory */
+
+static void
+daemon_die(void)
+{
+int pid;
+
+DEBUG(D_any) debug_printf("SIGTERM/SIGINT seen\n");
+#if !defined(DISABLE_TLS) && (defined(EXIM_HAVE_INOTIFY) || defined(EXIM_HAVE_KEVENT))
+tls_watch_invalidate();
+#endif
+
+if (daemon_notifier_fd >= 0)
+ {
+ close(daemon_notifier_fd);
+ daemon_notifier_fd = -1;
+#ifndef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+ unlink_notifier_socket();
+#endif
+ }
+
+if (f.running_in_test_harness || write_pid)
+ {
+ if ((pid = exim_fork(US"daemon-del-pidfile")) == 0)
+ {
+ if (override_pid_file_path)
+ (void)child_exec_exim(CEE_EXEC_PANIC, FALSE, NULL, FALSE, 3,
+ "-oP", override_pid_file_path, "-oPX");
+ else
+ (void)child_exec_exim(CEE_EXEC_PANIC, FALSE, NULL, FALSE, 1, "-oPX");
+
+ /* Control never returns here. */
+ }
+ if (pid > 0)
+ child_close(pid, 1);
+ }
+exim_exit(EXIT_SUCCESS);
+}
+
+
+/*************************************************
+* Listener socket for local work prompts *
+*************************************************/
+
+static void
+daemon_notifier_socket(void)
+{
+int fd;
+const uschar * where;
+struct sockaddr_un sa_un = {.sun_family = AF_UNIX};
+int len;
+
+if (!notifier_socket || !*notifier_socket)
+ {
+ DEBUG(D_any) debug_printf("-oY used so not creating notifier socket\n");
+ return;
+ }
+if (override_local_interfaces && !override_pid_file_path)
+ {
+ DEBUG(D_any)
+ debug_printf("-oX used without -oP so not creating notifier socket\n");
+ return;
+ }
+
+DEBUG(D_any) debug_printf("creating notifier socket\n");
+
+#ifdef SOCK_CLOEXEC
+if ((fd = socket(PF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
+ { where = US"socket"; goto bad; }
+#else
+if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0)
+ { where = US"socket"; goto bad; }
+(void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+#endif
+
+#ifdef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+sa_un.sun_path[0] = 0; /* Abstract local socket addr - Linux-specific? */
+len = offsetof(struct sockaddr_un, sun_path) + 1
+ + snprintf(sa_un.sun_path+1, sizeof(sa_un.sun_path)-1, "%s",
+ expand_string(notifier_socket));
+DEBUG(D_any) debug_printf(" @%s\n", sa_un.sun_path+1);
+#else /* filesystem-visible and persistent; will neeed removal */
+len = offsetof(struct sockaddr_un, sun_path)
+ + snprintf(sa_un.sun_path, sizeof(sa_un.sun_path), "%s",
+ expand_string(notifier_socket));
+DEBUG(D_any) debug_printf(" %s\n", sa_un.sun_path);
+#endif
+
+if (bind(fd, (const struct sockaddr *)&sa_un, len) < 0)
+ { where = US"bind"; goto bad; }
+
+#ifdef SO_PASSCRED /* Linux */
+if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0)
+ { where = US"SO_PASSCRED"; goto bad2; }
+#elif defined(LOCAL_CREDS) /* FreeBSD-ish */
+if (setsockopt(fd, 0, LOCAL_CREDS, &on, sizeof(on)) < 0)
+ { where = US"LOCAL_CREDS"; goto bad2; }
+#endif
+
+/* debug_printf("%s: fd %d\n", __FUNCTION__, fd); */
+daemon_notifier_fd = fd;
+return;
+
+bad2:
+#ifndef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+ Uunlink(sa_un.sun_path);
+#endif
+bad:
+ log_write(0, LOG_MAIN|LOG_PANIC, "%s %s: %s",
+ __FUNCTION__, where, strerror(errno));
+ close(fd);
+ return;
+}
+
+
+static uschar queuerun_msgid[MESSAGE_ID_LENGTH+1];
+
+/* Return TRUE if a sigalrm should be emulated */
+static BOOL
+daemon_notification(void)
+{
+uschar buf[256], cbuf[256];
+struct sockaddr_un sa_un;
+struct iovec iov = {.iov_base = buf, .iov_len = sizeof(buf)-1};
+struct msghdr msg = { .msg_name = &sa_un,
+ .msg_namelen = sizeof(sa_un),
+ .msg_iov = &iov,
+ .msg_iovlen = 1,
+ .msg_control = cbuf,
+ .msg_controllen = sizeof(cbuf)
+ };
+ssize_t sz;
+
+buf[sizeof(buf)-1] = 0;
+if ((sz = recvmsg(daemon_notifier_fd, &msg, 0)) <= 0) return FALSE;
+if (sz >= sizeof(buf)) return FALSE;
+
+#ifdef notdef
+debug_printf("addrlen %d\n", msg.msg_namelen);
+#endif
+DEBUG(D_queue_run) debug_printf("%s from addr '%s%.*s'\n", __FUNCTION__,
+ *sa_un.sun_path ? "" : "@",
+ (int)msg.msg_namelen - (*sa_un.sun_path ? 0 : 1),
+ sa_un.sun_path + (*sa_un.sun_path ? 0 : 1));
+
+/* Refuse to handle the item unless the peer has good credentials */
+#ifdef SCM_CREDENTIALS
+# define EXIM_SCM_CR_TYPE SCM_CREDENTIALS
+#elif defined(LOCAL_CREDS) && defined(SCM_CREDS)
+# define EXIM_SCM_CR_TYPE SCM_CREDS
+#else
+ /* The OS has no way to get the creds of the caller (for a unix/datagram socket.
+ Punt; don't try to check. */
+#endif
+
+#ifdef EXIM_SCM_CR_TYPE
+for (struct cmsghdr * cp = CMSG_FIRSTHDR(&msg);
+ cp;
+ cp = CMSG_NXTHDR(&msg, cp))
+ if (cp->cmsg_level == SOL_SOCKET && cp->cmsg_type == EXIM_SCM_CR_TYPE)
+ {
+# ifdef SCM_CREDENTIALS /* Linux */
+ struct ucred * cr = (struct ucred *) CMSG_DATA(cp);
+ if (cr->uid && cr->uid != exim_uid)
+ {
+ DEBUG(D_queue_run) debug_printf("%s: sender creds pid %d uid %d gid %d\n",
+ __FUNCTION__, (int)cr->pid, (int)cr->uid, (int)cr->gid);
+ return FALSE;
+ }
+# elif defined(LOCAL_CREDS) /* BSD-ish */
+ struct sockcred * cr = (struct sockcred *) CMSG_DATA(cp);
+ if (cr->sc_uid && cr->sc_uid != exim_uid)
+ {
+ DEBUG(D_queue_run) debug_printf("%s: sender creds pid ??? uid %d gid %d\n",
+ __FUNCTION__, (int)cr->sc_uid, (int)cr->sc_gid);
+ return FALSE;
+ }
+# endif
+ break;
+ }
+#endif
+
+buf[sz] = 0;
+switch (buf[0])
+ {
+#ifndef DISABLE_QUEUE_RAMP
+ case NOTIFY_MSG_QRUN:
+ /* this should be a message_id */
+ DEBUG(D_queue_run)
+ debug_printf("%s: qrunner trigger: %s\n", __FUNCTION__, buf+1);
+ memcpy(queuerun_msgid, buf+1, MESSAGE_ID_LENGTH+1);
+ return TRUE;
+#endif
+
+ case NOTIFY_QUEUE_SIZE_REQ:
+ {
+ uschar buf[16];
+ int len = snprintf(CS buf, sizeof(buf), "%u", queue_count_cached());
+
+ DEBUG(D_queue_run)
+ debug_printf("%s: queue size request: %s\n", __FUNCTION__, buf);
+
+ if (sendto(daemon_notifier_fd, buf, len, 0,
+ (const struct sockaddr *)&sa_un, msg.msg_namelen) < 0)
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s: sendto: %s\n", __FUNCTION__, strerror(errno));
+ return FALSE;
+ }
+ }
+return FALSE;
+}
+
+
+
+
+/*************************************************
+* 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;
+struct pollfd * fd_polls, * tls_watch_poll = NULL, * dnotify_poll = NULL;
+int listen_socket_count = 0, poll_fd_count;
+ip_address_item * addresses = NULL;
+time_t last_connection_time = (time_t)0;
+int local_queue_run_max = atoi(CS expand_string(queue_run_max));
+
+process_purpose = US"daemon";
+
+/* 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;
+
+/* Allocate enough pollstructs for inetd mode plus the ancillary sockets;
+also used when there are no listen sockets. */
+
+fd_polls = store_get(sizeof(struct pollfd) * 3, GET_UNTAINTED);
+
+if (f.inetd_wait_mode)
+ {
+ listen_socket_count = 1;
+ (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));
+
+ fd_polls[0].fd = 3;
+ fd_polls[0].events = POLLIN;
+ (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)
+ if (setsockopt(3, IPPROTO_TCP, TCP_NODELAY, US &on, sizeof(on)))
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to set socket NODELAY: %s",
+ strerror(errno));
+ }
+
+
+if (f.inetd_wait_mode || f.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 (f.daemon_listen && !f.inetd_wait_mode)
+ {
+ int *default_smtp_port;
+ int sep;
+ int pct = 0;
+ uschar *s;
+ const 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)
+ {
+ gstring * new_smtp_port = NULL;
+ gstring * new_local_interfaces = NULL;
+
+ if (!override_pid_file_path) write_pid = FALSE;
+
+ list = override_local_interfaces;
+ sep = 0;
+ while ((s = string_nextinlist(&list, &sep, NULL, 0)))
+ {
+ uschar joinstr[4];
+ gstring ** gp = Ustrpbrk(s, ".:") ? &new_local_interfaces : &new_smtp_port;
+
+ if (!*gp)
+ {
+ joinstr[0] = sep;
+ joinstr[1] = ' ';
+ *gp = string_catn(*gp, US"<", 1);
+ }
+
+ *gp = string_catn(*gp, joinstr, 2);
+ *gp = string_cat (*gp, s);
+ }
+
+ if (new_smtp_port)
+ {
+ daemon_smtp_port = string_from_gstring(new_smtp_port);
+ DEBUG(D_any) debug_printf("daemon_smtp_port overridden by -oX:\n %s\n",
+ daemon_smtp_port);
+ }
+
+ if (new_local_interfaces)
+ {
+ local_interfaces = string_from_gstring(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 explicit 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, NULL, 0)))
+ pct++;
+ default_smtp_port = store_get((pct+1) * sizeof(int), GET_UNTAINTED);
+ list = daemon_smtp_port;
+ sep = 0;
+ for (pct = 0;
+ (s = string_nextinlist(&list, &sep, NULL, 0));
+ 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;
+ /* the list isn't expanded so cannot be tainted. If it ever is we will trap here */
+ while ((s = string_nextinlist(&list, &sep, big_buffer, big_buffer_size)))
+ if (!isdigit(*s))
+ {
+ gstring * g = NULL;
+
+ 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));
+ }
+ g = string_append_listele(g, ':', s);
+ }
+ if (g)
+ tls_in.on_connect_ports = g->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; ipa = ipa->next)
+ {
+ 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 (int i = 1; default_smtp_port[i] > 0; i++)
+ {
+ ip_address_item * new = store_get(sizeof(ip_address_item), GET_UNTAINTED);
+
+ 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; pipa = &ipa->next, ipa = ipa->next)
+ {
+ ip_address_item *ipa2;
+
+ /* Handle an IPv4 wildcard */
+
+ if (ipa->address[0] == 0)
+ for (ipa2 = ipa; ipa2->next; 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; 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.
+ Two extra elements for the ancillary sockets */
+
+ for (ipa = addresses; ipa; ipa = ipa->next)
+ listen_socket_count++;
+ fd_polls = store_get(sizeof(struct pollfd) * (listen_socket_count + 2),
+ GET_UNTAINTED);
+ for (struct pollfd * p = fd_polls; p < fd_polls + listen_socket_count + 2;
+ p++)
+ { p->fd = -1; p->events = POLLIN; }
+
+ } /* daemon_listen but not inetd_wait_mode */
+
+if (f.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)
+ {
+ smtp_slots = store_get(smtp_accept_max * sizeof(smtp_slot), GET_UNTAINTED);
+ for (int 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 (f.background_daemon || f.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 (f.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 = exim_fork(US"daemon");
+ 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. */
+
+daemon_notifier_socket();
+
+if (f.daemon_listen && !f.inetd_wait_mode)
+ {
+ int sk;
+ 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 fd, af;
+
+ if (Ustrchr(ipa->address, ':') != NULL)
+ {
+ af = AF_INET6;
+ wildcard = ipa->address[1] == 0;
+ }
+ else
+ {
+ af = AF_INET;
+ wildcard = ipa->address[0] == 0;
+ }
+
+ if ((fd_polls[sk].fd = fd = ip_socket(SOCK_STREAM, af)) < 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(fd, IPPROTO_IPV6, IPV6_V6ONLY, &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(fd, SOL_SOCKET, SO_REUSEADDR, &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(fd, IPPROTO_TCP, TCP_NODELAY, &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. */
+
+#ifdef TCP_FASTOPEN
+ f.tcp_fastopen_ok = TRUE;
+#endif
+ for(;;)
+ {
+ uschar *msg, *addr;
+ if (ip_bind(fd, 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(fd);
+ 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, add to the set of sockets for select
+ and continue to the next address. */
+
+#if defined(TCP_FASTOPEN) && !defined(__APPLE__)
+ if ( f.tcp_fastopen_ok
+ && setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN,
+ &smtp_connect_backlog, sizeof(smtp_connect_backlog)))
+ {
+ DEBUG(D_any) debug_printf("setsockopt FASTOPEN: %s\n", strerror(errno));
+ f.tcp_fastopen_ok = FALSE;
+ }
+#endif
+ if (listen(fd, smtp_connect_backlog) >= 0)
+ {
+#if defined(TCP_FASTOPEN) && defined(__APPLE__)
+ if ( f.tcp_fastopen_ok
+ && setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &on, sizeof(on)))
+ {
+ DEBUG(D_any) debug_printf("setsockopt FASTOPEN: %s\n", strerror(errno));
+ f.tcp_fastopen_ok = FALSE;
+ }
+#endif
+ fd_polls[sk].fd = fd;
+ 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(fd);
+
+ /* 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)
+ 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 (f.running_in_test_harness || write_pid)
+ {
+ const enum pid_op operation = (f.running_in_test_harness
+ || real_uid == root_uid
+ || (real_uid == exim_uid && !override_pid_file_path)) ? PID_WRITE : PID_CHECK;
+ if (!operate_on_pid_file(operation, getpid()))
+ DEBUG(D_any) debug_printf("%s pid file %s: %s\n", (operation == PID_WRITE) ? "write" : "check", pid_file_path, strerror(errno));
+ }
+
+/* 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))
+ ? string_copy_perm(US pw->pw_name, FALSE) : 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 && local_queue_run_max > 0)
+ {
+ queue_pid_slots = store_get(local_queue_run_max * sizeof(pid_t), GET_UNTAINTED);
+ for (int i = 0; i < local_queue_run_max; i++) queue_pid_slots[i] = 0;
+ }
+
+/* Set up the handler for termination of child processes, and the one
+telling us to die. */
+
+sigchld_seen = FALSE;
+os_non_restarting_signal(SIGCHLD, main_sigchld_handler);
+
+sigterm_seen = FALSE;
+os_non_restarting_signal(SIGTERM, main_sigterm_handler);
+os_non_restarting_signal(SIGINT, main_sigterm_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 (f.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(%s): pre-listening socket", version_string);
+
+ /* set up the timeout logic */
+ sigalrm_seen = TRUE;
+ }
+
+else if (f.daemon_listen)
+ {
+ int smtp_ports = 0;
+ int smtps_ports = 0;
+ ip_address_item * ipa;
+ uschar * p;
+ uschar * qinfo = queue_interval > 0
+ ? string_sprintf("-q%s%s",
+ f.queue_2stage ? "q" : "", 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 (int j = 0, i; j < 2; j++)
+ {
+ for (i = 0, ipa = addresses; i < 10 && ipa; i++, ipa = ipa->next)
+ {
+ /* First time round, look for SMTP ports; second time round, look for
+ SMTPS ports. Build IP+port strings. */
+
+ if (host_is_tls_on_connect_port(ipa->port) == (j > 0))
+ {
+ if (j == 0)
+ smtp_ports++;
+ else
+ smtps_ports++;
+
+ /* Now the information about the port (and sometimes interface) */
+
+ if (ipa->address[0] == ':' && ipa->address[1] == 0)
+ { /* v6 wildcard */
+ if (ipa->next && ipa->next->address[0] == 0 &&
+ ipa->next->port == ipa->port)
+ {
+ ipa->log = string_sprintf(" port %d (IPv6 and IPv4)", ipa->port);
+ (ipa = ipa->next)->log = NULL;
+ }
+ else if (ipa->v6_include_v4)
+ ipa->log = string_sprintf(" port %d (IPv6 with IPv4)", ipa->port);
+ else
+ ipa->log = string_sprintf(" port %d (IPv6)", ipa->port);
+ }
+ else if (ipa->address[0] == 0) /* v4 wildcard */
+ ipa->log = string_sprintf(" port %d (IPv4)", ipa->port);
+ else /* check for previously-seen IP */
+ {
+ ip_address_item * i2;
+ for (i2 = addresses; i2 != ipa; i2 = i2->next)
+ if ( host_is_tls_on_connect_port(i2->port) == (j > 0)
+ && Ustrcmp(ipa->address, i2->address) == 0
+ )
+ { /* found; append port to list */
+ for (p = i2->log; *p; ) p++; /* end of existing string */
+ if (*--p == '}') *p = '\0'; /* drop EOL */
+ while (isdigit(*--p)) ; /* char before port */
+
+ i2->log = *p == ':' /* no list yet? */
+ ? string_sprintf("%.*s{%s,%d}",
+ (int)(p - i2->log + 1), i2->log, p+1, ipa->port)
+ : string_sprintf("%s,%d}", i2->log, ipa->port);
+ ipa->log = NULL;
+ break;
+ }
+ if (i2 == ipa) /* first-time IP */
+ ipa->log = string_sprintf(" [%s]:%d", ipa->address, ipa->port);
+ }
+ }
+ }
+ }
+
+ p = big_buffer;
+ for (int j = 0, i; j < 2; j++)
+ {
+ /* First time round, look for SMTP ports; second time round, look for
+ SMTPS ports. For the first one of each, insert leading text. */
+
+ if (j == 0)
+ {
+ if (smtp_ports > 0)
+ p += sprintf(CS p, "SMTP on");
+ }
+ else
+ if (smtps_ports > 0)
+ p += sprintf(CS p, "%sSMTPS on",
+ smtp_ports == 0 ? "" : " and for ");
+
+ /* Now the information about the port (and sometimes interface) */
+
+ for (i = 0, ipa = addresses; i < 10 && ipa; i++, ipa = ipa->next)
+ if (host_is_tls_on_connect_port(ipa->port) == (j > 0))
+ if (ipa->log)
+ p += sprintf(CS p, "%s", ipa->log);
+
+ if (ipa)
+ p += sprintf(CS p, " ...");
+ }
+
+ 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): %s, listening for %s",
+ version_string, qinfo, big_buffer);
+ }
+
+else
+ {
+ uschar * s = *queue_name
+ ? string_sprintf("-qG%s/%s", queue_name, readconf_printtime(queue_interval))
+ : string_sprintf("-q%s", readconf_printtime(queue_interval));
+ log_write(0, LOG_MAIN,
+ "exim %s daemon started: pid=%d, %s, not listening for SMTP",
+ version_string, getpid(), s);
+ set_process_info("daemon(%s): %s, not listening", version_string, s);
+ }
+
+/* Do any work it might be useful to amortize over our children
+(eg: compile regex) */
+
+dns_pattern_init();
+smtp_deliver_init(); /* Used for callouts */
+
+#ifndef DISABLE_DKIM
+ {
+# ifdef MEASURE_TIMING
+ struct timeval t0;
+ gettimeofday(&t0, NULL);
+# endif
+ dkim_exim_init();
+# ifdef MEASURE_TIMING
+ report_time_since(&t0, US"dkim_exim_init (delta)");
+# endif
+ }
+#endif
+
+#ifdef WITH_CONTENT_SCAN
+malware_init();
+#endif
+#ifdef SUPPORT_SPF
+spf_init();
+#endif
+#ifndef DISABLE_TLS
+tls_daemon_init();
+#endif
+
+/* Add ancillary sockets to the set for select */
+
+poll_fd_count = listen_socket_count;
+#ifndef DISABLE_TLS
+if (tls_watch_fd >= 0)
+ {
+ tls_watch_poll = &fd_polls[poll_fd_count++];
+ tls_watch_poll->fd = tls_watch_fd;
+ tls_watch_poll->events = POLLIN;
+ }
+#endif
+if (daemon_notifier_fd >= 0)
+ {
+ dnotify_poll = &fd_polls[poll_fd_count++];
+ dnotify_poll->fd = daemon_notifier_fd;
+ dnotify_poll->events = POLLIN;
+ }
+
+/* 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;
+
+#ifdef MEASURE_TIMING
+report_time_since(&timestamp_startup, US"daemon loop start"); /* testcase 0022 */
+#endif
+
+/* Enter the never-ending loop... */
+
+for (;;)
+ {
+ pid_t pid;
+
+ if (sigterm_seen)
+ daemon_die(); /* Does not return */
+
+ /* 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("%s received\n",
+#ifndef DISABLE_QUEUE_RAMP
+ *queuerun_msgid ? "qrun notification" :
+#endif
+ "SIGALRM");
+
+ /* 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
+ && (local_queue_run_max <= 0 || queue_run_count < local_queue_run_max))
+ {
+ if ((pid = exim_fork(US"queue-runner")) == 0)
+ {
+ /* 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 (f.debug_daemon) debug_selector = 0;
+
+ /* Close any open listening sockets in the child */
+
+ close_daemon_sockets(daemon_notifier_fd,
+ fd_polls, listen_socket_count);
+
+ /* Reset SIGHUP and SIGCHLD in the child in both cases. */
+
+ signal(SIGHUP, SIG_DFL);
+ signal(SIGCHLD, SIG_DFL);
+ signal(SIGTERM, SIG_DFL);
+ signal(SIGINT, 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[7];
+ int extracount = 1;
+
+ signal(SIGALRM, SIG_DFL);
+ *p++ = '-';
+ *p++ = 'q';
+ if ( f.queue_2stage
+#ifndef DISABLE_QUEUE_RAMP
+ && !*queuerun_msgid
+#endif
+ ) *p++ = 'q';
+ if (f.queue_run_first_delivery) *p++ = 'i';
+ if (f.queue_run_force) *p++ = 'f';
+ if (f.deliver_force_thaw) *p++ = 'f';
+ if (f.queue_run_local) *p++ = 'l';
+ *p = 0;
+ extra[0] = *queue_name
+ ? string_sprintf("%sG%s", opt, queue_name) : opt;
+
+#ifndef DISABLE_QUEUE_RAMP
+ if (*queuerun_msgid)
+ {
+ log_write(0, LOG_MAIN, "notify triggered queue run");
+ extra[extracount++] = queuerun_msgid; /* Trigger only the */
+ extra[extracount++] = queuerun_msgid; /* one message */
+ }
+#endif
+
+ /* If -R or -S were on the original command line, ensure they get
+ passed on. */
+
+ if (deliver_selectstring)
+ {
+ extra[extracount++] = f.deliver_selectstring_regex ? US"-Rr" : US"-R";
+ extra[extracount++] = deliver_selectstring;
+ }
+
+ if (deliver_selectstring_sender)
+ {
+ extra[extracount++] = f.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, FALSE, extracount,
+ extra[0], extra[1], extra[2], extra[3], extra[4], extra[5], extra[6]);
+
+ /* Control never returns here. */
+ }
+
+ /* No need to re-exec; SIGALRM remains set to the default handler */
+
+#ifndef DISABLE_QUEUE_RAMP
+ if (*queuerun_msgid)
+ {
+ log_write(0, LOG_MAIN, "notify triggered queue run");
+ f.queue_2stage = FALSE;
+ queue_run(queuerun_msgid, queuerun_msgid, FALSE);
+ }
+ else
+#endif
+ queue_run(NULL, NULL, FALSE);
+ exim_underbar_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
+ {
+ for (int i = 0; i < local_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;
+#ifndef DISABLE_QUEUE_RAMP
+ if (*queuerun_msgid)
+ *queuerun_msgid = 0;
+ else
+#endif
+ 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 (f.daemon_listen)
+ {
+ int lcount;
+ BOOL select_failed = FALSE;
+
+ 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 = poll(fd_polls, poll_fd_count, -1);
+
+ 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. */
+
+ {
+ int select_errno = errno;
+ handle_ending_processes();
+
+#ifndef DISABLE_TLS
+ {
+ int old_tfd;
+ /* Create or rotate any required keys; handle (delayed) filewatch event */
+
+ if ((old_tfd = tls_daemon_tick()) >= 0)
+ for (struct pollfd * p = &fd_polls[listen_socket_count];
+ p < fd_polls + poll_fd_count; p++)
+ if (p->fd == old_tfd) { p->fd = tls_watch_fd ; break; }
+ }
+#endif
+ 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 HAVE_IPV6
+ struct sockaddr_in6 accepted;
+#else
+ struct sockaddr_in accepted;
+#endif
+
+ if (!select_failed)
+ {
+#if !defined(DISABLE_TLS) && (defined(EXIM_HAVE_INOTIFY) || defined(EXIM_HAVE_KEVENT))
+ if (tls_watch_poll && tls_watch_poll->revents & POLLIN)
+ {
+ tls_watch_poll->revents = 0;
+ tls_watch_trigger_time = time(NULL); /* Set up delayed event */
+ tls_watch_discard_event(tls_watch_fd);
+ break; /* to top of daemon loop */
+ }
+#endif
+ if (dnotify_poll && dnotify_poll->revents & POLLIN)
+ {
+ dnotify_poll->revents = 0;
+ sigalrm_seen = daemon_notification();
+ break; /* to top of daemon loop */
+ }
+ for (struct pollfd * p = fd_polls; p < fd_polls + listen_socket_count;
+ p++)
+ if (p->revents & POLLIN)
+ {
+ EXIM_SOCKLEN_T alen = sizeof(accepted);
+#ifdef TCP_INFO
+ struct tcp_info ti;
+ socklen_t tlen = sizeof(ti);
+
+ /* If monitoring the backlog is wanted, grab for later logging */
+
+ smtp_listen_backlog = 0;
+ if ( smtp_backlog_monitor > 0
+ && getsockopt(p->fd, IPPROTO_TCP, TCP_INFO, &ti, &tlen) == 0)
+ {
+# ifdef EXIM_HAVE_TCPI_UNACKED
+ DEBUG(D_interface) debug_printf("listen fd %d queue max %u curr %u\n",
+ p->fd, ti.tcpi_sacked, ti.tcpi_unacked);
+ smtp_listen_backlog = ti.tcpi_unacked;
+# elif defined(__FreeBSD__) /* This does not work. Investigate kernel sourcecode. */
+ DEBUG(D_interface) debug_printf("listen fd %d queue max %u curr %u\n",
+ p->fd, ti.__tcpi_sacked, ti.__tcpi_unacked);
+ smtp_listen_backlog = ti.__tcpi_unacked;
+# endif
+ }
+#endif
+ p->revents = 0;
+ accept_socket = accept(p->fd, (struct sockaddr *)&accepted, &alen);
+ 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)
+ {
+#ifdef TCP_QUICKACK /* Avoid pure-ACKs while in tls protocol pingpong phase */
+ /* Unfortunately we cannot be certain to do this before a TLS-on-connect
+ Client Hello arrives and is acked. We do it as early as possible. */
+ (void) setsockopt(accept_socket, IPPROTO_TCP, TCP_QUICKACK, US &off, sizeof(off));
+#endif
+ if (inetd_wait_timeout)
+ last_connection_time = time(NULL);
+ handle_smtp_call(fd_polls, 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 pollfd p;
+ poll(&p, 0, queue_interval * 1000);
+ 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)
+ {
+ log_write(0, LOG_MAIN, "pid %d: SIGHUP received: re-exec daemon",
+ getpid());
+ close_daemon_sockets(daemon_notifier_fd, fd_polls, listen_socket_count);
+ ALARM_CLR(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/dane-openssl.c b/src/dane-openssl.c
new file mode 100644
index 0000000..6ed3529
--- /dev/null
+++ b/src/dane-openssl.c
@@ -0,0 +1,1719 @@
+/*
+ * Author: Viktor Dukhovni
+ * License: THIS CODE IS IN THE PUBLIC DOMAIN.
+ *
+ * Copyright (c) The Exim Maintainers 2014 - 2019
+ */
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+
+#include <openssl/opensslv.h>
+#include <openssl/err.h>
+#include <openssl/crypto.h>
+#include <openssl/safestack.h>
+#include <openssl/objects.h>
+#include <openssl/x509.h>
+#include <openssl/x509v3.h>
+#include <openssl/evp.h>
+#include <openssl/bn.h>
+
+#if OPENSSL_VERSION_NUMBER < 0x1000000fL
+# error "OpenSSL 1.0.0 or higher required"
+#endif
+
+#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
+# define X509_up_ref(x) CRYPTO_add(&((x)->references), 1, CRYPTO_LOCK_X509)
+#endif
+
+/* LibreSSL 2.9.0 and later - 2.9.0 has removed a number of macros ... */
+#ifdef LIBRESSL_VERSION_NUMBER
+# if LIBRESSL_VERSION_NUMBER >= 0x2090000fL
+# define EXIM_HAVE_ASN1_MACROS
+# endif
+#endif
+/* OpenSSL */
+#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
+# define EXIM_HAVE_ASN1_MACROS
+# define EXIM_OPAQUE_X509
+/* Older OpenSSL and all LibreSSL */
+#else
+# define X509_STORE_CTX_get_verify(ctx) (ctx)->verify
+# define X509_STORE_CTX_get_verify_cb(ctx) (ctx)->verify_cb
+# define X509_STORE_CTX_get0_cert(ctx) (ctx)->cert
+# define X509_STORE_CTX_get0_chain(ctx) (ctx)->chain
+# define X509_STORE_CTX_get0_untrusted(ctx) (ctx)->untrusted
+
+# define X509_STORE_CTX_set_verify(ctx, verify_chain) (ctx)->verify = (verify_chain)
+# define X509_STORE_CTX_set0_verified_chain(ctx, sk) (ctx)->chain = (sk)
+# define X509_STORE_CTX_set_error_depth(ctx, val) (ctx)->error_depth = (val)
+# define X509_STORE_CTX_set_current_cert(ctx, cert) (ctx)->current_cert = (cert)
+
+# define ASN1_STRING_get0_data ASN1_STRING_data
+# define X509_getm_notBefore X509_get_notBefore
+# define X509_getm_notAfter X509_get_notAfter
+
+# define CRYPTO_ONCE_STATIC_INIT 0
+# define CRYPTO_THREAD_run_once run_once
+typedef int CRYPTO_ONCE;
+#endif
+
+
+#include "danessl.h"
+
+#define DANESSL_F_ADD_SKID 100
+#define DANESSL_F_ADD_TLSA 101
+#define DANESSL_F_CHECK_END_ENTITY 102
+#define DANESSL_F_CTX_INIT 103
+#define DANESSL_F_GROW_CHAIN 104
+#define DANESSL_F_INIT 105
+#define DANESSL_F_LIBRARY_INIT 106
+#define DANESSL_F_LIST_ALLOC 107
+#define DANESSL_F_MATCH 108
+#define DANESSL_F_PUSH_EXT 109
+#define DANESSL_F_SET_TRUST_ANCHOR 110
+#define DANESSL_F_VERIFY_CERT 111
+#define DANESSL_F_WRAP_CERT 112
+#define DANESSL_F_DANESSL_VERIFY_CHAIN 113
+
+#define DANESSL_R_BAD_CERT 100
+#define DANESSL_R_BAD_CERT_PKEY 101
+#define DANESSL_R_BAD_DATA_LENGTH 102
+#define DANESSL_R_BAD_DIGEST 103
+#define DANESSL_R_BAD_NULL_DATA 104
+#define DANESSL_R_BAD_PKEY 105
+#define DANESSL_R_BAD_SELECTOR 106
+#define DANESSL_R_BAD_USAGE 107
+#define DANESSL_R_INIT 108
+#define DANESSL_R_LIBRARY_INIT 109
+#define DANESSL_R_NOSIGN_KEY 110
+#define DANESSL_R_SCTX_INIT 111
+#define DANESSL_R_SUPPORT 112
+
+#ifndef OPENSSL_NO_ERR
+#define DANESSL_F_PLACEHOLDER 0 /* FIRST! Value TBD */
+static ERR_STRING_DATA dane_str_functs[] = {
+ /* error string */
+ {DANESSL_F_PLACEHOLDER, "DANE library"}, /* FIRST!!! */
+ {DANESSL_F_ADD_SKID, "add_skid"},
+ {DANESSL_F_ADD_TLSA, "DANESSL_add_tlsa"},
+ {DANESSL_F_CHECK_END_ENTITY, "check_end_entity"},
+ {DANESSL_F_CTX_INIT, "DANESSL_CTX_init"},
+ {DANESSL_F_GROW_CHAIN, "grow_chain"},
+ {DANESSL_F_INIT, "DANESSL_init"},
+ {DANESSL_F_LIBRARY_INIT, "DANESSL_library_init"},
+ {DANESSL_F_LIST_ALLOC, "list_alloc"},
+ {DANESSL_F_MATCH, "match"},
+ {DANESSL_F_PUSH_EXT, "push_ext"},
+ {DANESSL_F_SET_TRUST_ANCHOR, "set_trust_anchor"},
+ {DANESSL_F_VERIFY_CERT, "verify_cert"},
+ {DANESSL_F_WRAP_CERT, "wrap_cert"},
+ {0, NULL}
+};
+static ERR_STRING_DATA dane_str_reasons[] = {
+ /* error string */
+ {DANESSL_R_BAD_CERT, "Bad TLSA record certificate"},
+ {DANESSL_R_BAD_CERT_PKEY, "Bad TLSA record certificate public key"},
+ {DANESSL_R_BAD_DATA_LENGTH, "Bad TLSA record digest length"},
+ {DANESSL_R_BAD_DIGEST, "Bad TLSA record digest"},
+ {DANESSL_R_BAD_NULL_DATA, "Bad TLSA record null data"},
+ {DANESSL_R_BAD_PKEY, "Bad TLSA record public key"},
+ {DANESSL_R_BAD_SELECTOR, "Bad TLSA record selector"},
+ {DANESSL_R_BAD_USAGE, "Bad TLSA record usage"},
+ {DANESSL_R_INIT, "DANESSL_init() required"},
+ {DANESSL_R_LIBRARY_INIT, "DANESSL_library_init() required"},
+ {DANESSL_R_NOSIGN_KEY, "Certificate usage 2 requires EC support"},
+ {DANESSL_R_SCTX_INIT, "DANESSL_CTX_init() required"},
+ {DANESSL_R_SUPPORT, "DANE library features not supported"},
+ {0, NULL}
+};
+#endif
+
+#define DANEerr(f, r) ERR_PUT_error(err_lib_dane, (f), (r), __FUNCTION__, __LINE__)
+
+static int err_lib_dane = -1;
+static int dane_idx = -1;
+
+#ifdef X509_V_FLAG_PARTIAL_CHAIN /* OpenSSL >= 1.0.2 */
+static int wrap_to_root = 0;
+#else
+static int wrap_to_root = 1;
+#endif
+
+static void (*cert_free)(void *) = (void (*)(void *)) X509_free;
+static void (*pkey_free)(void *) = (void (*)(void *)) EVP_PKEY_free;
+
+typedef struct dane_list
+{
+ struct dane_list *next;
+ void *value;
+} *dane_list;
+
+#define LINSERT(h, e) do { (e)->next = (h); (h) = (e); } while (0)
+
+typedef struct dane_host_list
+{
+ struct dane_host_list *next;
+ char *value;
+} *dane_host_list;
+
+typedef struct dane_data
+{
+ size_t datalen;
+ unsigned char data[0];
+} *dane_data;
+
+typedef struct dane_data_list
+{
+ struct dane_data_list *next;
+ dane_data value;
+} *dane_data_list;
+
+typedef struct dane_mtype
+{
+ int mdlen;
+ const EVP_MD *md;
+ dane_data_list data;
+} *dane_mtype;
+
+typedef struct dane_mtype_list
+{
+ struct dane_mtype_list *next;
+ dane_mtype value;
+} *dane_mtype_list;
+
+typedef struct dane_selector
+{
+ uint8_t selector;
+ dane_mtype_list mtype;
+} *dane_selector;
+
+typedef struct dane_selector_list
+{
+ struct dane_selector_list *next;
+ dane_selector value;
+} *dane_selector_list;
+
+typedef struct dane_pkey_list
+{
+ struct dane_pkey_list *next;
+ EVP_PKEY *value;
+} *dane_pkey_list;
+
+typedef struct dane_cert_list
+{
+ struct dane_cert_list *next;
+ X509 *value;
+} *dane_cert_list;
+
+typedef struct ssl_dane
+{
+ int (*verify)(X509_STORE_CTX *);
+ STACK_OF(X509) *roots;
+ STACK_OF(X509) *chain;
+ X509 *match; /* Matched cert */
+ const char *thost; /* TLSA base domain */
+ char *mhost; /* Matched peer name */
+ dane_pkey_list pkeys;
+ dane_cert_list certs;
+ dane_host_list hosts;
+ dane_selector_list selectors[DANESSL_USAGE_LAST + 1];
+ int depth;
+ int mdpth; /* Depth of matched cert */
+ int multi; /* Multi-label wildcards? */
+ int count; /* Number of TLSA records */
+} ssl_dane;
+
+#ifndef X509_V_ERR_HOSTNAME_MISMATCH
+# define X509_V_ERR_HOSTNAME_MISMATCH X509_V_ERR_APPLICATION_VERIFICATION
+#endif
+
+
+
+static int
+match(dane_selector_list slist, X509 *cert, int depth)
+{
+int matched;
+
+/*
+ * Note, set_trust_anchor() needs to know whether the match was for a
+ * pkey digest or a certificate digest. We return MATCHED_PKEY or
+ * MATCHED_CERT accordingly.
+ */
+#define MATCHED_CERT (DANESSL_SELECTOR_CERT + 1)
+#define MATCHED_PKEY (DANESSL_SELECTOR_SPKI + 1)
+
+/*
+ * Loop over each selector, mtype, and associated data element looking
+ * for a match.
+ */
+for (matched = 0; !matched && slist; slist = slist->next)
+ {
+ unsigned char mdbuf[EVP_MAX_MD_SIZE];
+ unsigned char *buf = NULL;
+ unsigned char *buf2;
+ unsigned int len = 0;
+
+ /*
+ * Extract ASN.1 DER form of certificate or public key.
+ */
+ switch(slist->value->selector)
+ {
+ case DANESSL_SELECTOR_CERT:
+ len = i2d_X509(cert, NULL);
+ buf2 = buf = US OPENSSL_malloc(len);
+ if(buf) i2d_X509(cert, &buf2);
+ break;
+ case DANESSL_SELECTOR_SPKI:
+ len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), NULL);
+ buf2 = buf = US OPENSSL_malloc(len);
+ if(buf) i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &buf2);
+ break;
+ }
+
+ if (!buf)
+ {
+ DANEerr(DANESSL_F_MATCH, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+ OPENSSL_assert(buf2 - buf == len);
+
+ /*
+ * Loop over each mtype and data element
+ */
+ for (dane_mtype_list m = slist->value->mtype; !matched && m; m = m->next)
+ {
+ unsigned char *cmpbuf = buf;
+ unsigned int cmplen = len;
+
+ /*
+ * If it is a digest, compute the corresponding digest of the
+ * DER data for comparison, otherwise, use the full object.
+ */
+ if (m->value->md)
+ {
+ cmpbuf = mdbuf;
+ if (!EVP_Digest(buf, len, cmpbuf, &cmplen, m->value->md, 0))
+ matched = -1;
+ }
+ for (dane_data_list d = m->value->data; !matched && d; d = d->next)
+ if ( cmplen == d->value->datalen
+ && memcmp(cmpbuf, d->value->data, cmplen) == 0)
+ matched = slist->value->selector + 1;
+ }
+
+ OPENSSL_free(buf);
+ }
+
+return matched;
+}
+
+static int
+push_ext(X509 *cert, X509_EXTENSION *ext)
+{
+if (ext)
+ {
+ if (X509_add_ext(cert, ext, -1))
+ return 1;
+ X509_EXTENSION_free(ext);
+ }
+DANEerr(DANESSL_F_PUSH_EXT, ERR_R_MALLOC_FAILURE);
+return 0;
+}
+
+static int
+add_ext(X509 *issuer, X509 *subject, int ext_nid, char *ext_val)
+{
+X509V3_CTX v3ctx;
+
+X509V3_set_ctx(&v3ctx, issuer, subject, 0, 0, 0);
+return push_ext(subject, X509V3_EXT_conf_nid(0, &v3ctx, ext_nid, ext_val));
+}
+
+static int
+set_serial(X509 *cert, AUTHORITY_KEYID *akid, X509 *subject)
+{
+int ret = 0;
+BIGNUM *bn;
+
+if (akid && akid->serial)
+ return (X509_set_serialNumber(cert, akid->serial));
+
+/*
+ * Add one to subject's serial to avoid collisions between TA serial and
+ * serial of signing root.
+ */
+if ( (bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(subject), 0)) != 0
+ && BN_add_word(bn, 1)
+ && BN_to_ASN1_INTEGER(bn, X509_get_serialNumber(cert)))
+ ret = 1;
+
+if (bn)
+ BN_free(bn);
+return ret;
+}
+
+static int
+add_akid(X509 *cert, AUTHORITY_KEYID *akid)
+{
+int nid = NID_authority_key_identifier;
+ASN1_OCTET_STRING *id;
+unsigned char c = 0;
+int ret = 0;
+
+/*
+ * 0 will never be our subject keyid from a SHA-1 hash, but it could be
+ * our subject keyid if forced from child's akid. If so, set our
+ * authority keyid to 1. This way we are never self-signed, and thus
+ * exempt from any potential (off by default for now in OpenSSL)
+ * self-signature checks!
+ */
+id = akid && akid->keyid ? akid->keyid : 0;
+if (id && ASN1_STRING_length(id) == 1 && *ASN1_STRING_get0_data(id) == c)
+ c = 1;
+
+if ( (akid = AUTHORITY_KEYID_new()) != 0
+ && (akid->keyid = ASN1_OCTET_STRING_new()) != 0
+#ifdef EXIM_HAVE_ASN1_MACROS
+ && ASN1_OCTET_STRING_set(akid->keyid, (void *) &c, 1)
+#else
+ && M_ASN1_OCTET_STRING_set(akid->keyid, (void *) &c, 1)
+#endif
+ && X509_add1_ext_i2d(cert, nid, akid, 0, X509V3_ADD_APPEND))
+ ret = 1;
+if (akid)
+ AUTHORITY_KEYID_free(akid);
+return ret;
+}
+
+static int
+add_skid(X509 *cert, AUTHORITY_KEYID *akid)
+{
+int nid = NID_subject_key_identifier;
+
+if (!akid || !akid->keyid)
+ return add_ext(0, cert, nid, "hash");
+return X509_add1_ext_i2d(cert, nid, akid->keyid, 0, X509V3_ADD_APPEND) > 0;
+}
+
+static X509_NAME *
+akid_issuer_name(AUTHORITY_KEYID *akid)
+{
+if (akid && akid->issuer)
+ {
+ GENERAL_NAMES *gens = akid->issuer;
+
+ for (int i = 0; i < sk_GENERAL_NAME_num(gens); ++i)
+ {
+ GENERAL_NAME *gn = sk_GENERAL_NAME_value(gens, i);
+
+ if (gn->type == GEN_DIRNAME)
+ return (gn->d.dirn);
+ }
+ }
+return 0;
+}
+
+static int
+set_issuer_name(X509 *cert, AUTHORITY_KEYID *akid, X509_NAME *subj)
+{
+X509_NAME *name = akid_issuer_name(akid);
+
+/*
+ * If subject's akid specifies an authority key identifier issuer name, we
+ * must use that.
+ */
+return X509_set_issuer_name(cert,
+ name ? name : subj);
+}
+
+static int
+grow_chain(ssl_dane *dane, int trusted, X509 *cert)
+{
+STACK_OF(X509) **xs = trusted ? &dane->roots : &dane->chain;
+static ASN1_OBJECT *serverAuth = 0;
+
+#define UNTRUSTED 0
+#define TRUSTED 1
+
+if ( trusted && !serverAuth
+ && !(serverAuth = OBJ_nid2obj(NID_server_auth)))
+ {
+ DANEerr(DANESSL_F_GROW_CHAIN, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+if (!*xs && !(*xs = sk_X509_new_null()))
+ {
+ DANEerr(DANESSL_F_GROW_CHAIN, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+
+if (cert)
+ {
+ if (trusted && !X509_add1_trust_object(cert, serverAuth))
+ return 0;
+#ifdef EXIM_OPAQUE_X509
+ X509_up_ref(cert);
+#else
+ CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
+#endif
+ if (!sk_X509_push(*xs, cert))
+ {
+ X509_free(cert);
+ DANEerr(DANESSL_F_GROW_CHAIN, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+ }
+return 1;
+}
+
+static int
+wrap_issuer(ssl_dane *dane, EVP_PKEY *key, X509 *subject, int depth, int top)
+{
+int ret = 1;
+X509 *cert = 0;
+AUTHORITY_KEYID *akid;
+X509_NAME *name = X509_get_issuer_name(subject);
+EVP_PKEY *newkey = key ? key : X509_get_pubkey(subject);
+
+#define WRAP_MID 0 /* Ensure intermediate. */
+#define WRAP_TOP 1 /* Ensure self-signed. */
+
+if (!name || !newkey || !(cert = X509_new()))
+ return 0;
+
+/*
+ * Record the depth of the trust-anchor certificate.
+ */
+if (dane->depth < 0)
+ dane->depth = depth + 1;
+
+/*
+ * XXX: Uncaught error condition:
+ *
+ * The return value is NULL both when the extension is missing, and when
+ * OpenSSL rans out of memory while parsing the extension.
+ */
+ERR_clear_error();
+akid = X509_get_ext_d2i(subject, NID_authority_key_identifier, 0, 0);
+/* XXX: Should we peek at the error stack here??? */
+
+/*
+ * If top is true generate a self-issued root CA, otherwise an
+ * intermediate CA and possibly its self-signed issuer.
+ *
+ * CA cert valid for +/- 30 days
+ */
+if ( !X509_set_version(cert, 2)
+ || !set_serial(cert, akid, subject)
+ || !set_issuer_name(cert, akid, name)
+ || !X509_gmtime_adj(X509_getm_notBefore(cert), -30 * 86400L)
+ || !X509_gmtime_adj(X509_getm_notAfter(cert), 30 * 86400L)
+ || !X509_set_subject_name(cert, name)
+ || !X509_set_pubkey(cert, newkey)
+ || !add_ext(0, cert, NID_basic_constraints, "CA:TRUE")
+ || (!top && !add_akid(cert, akid))
+ || !add_skid(cert, akid)
+ || ( !top && wrap_to_root
+ && !wrap_issuer(dane, newkey, cert, depth, WRAP_TOP)))
+ ret = 0;
+
+if (akid)
+ AUTHORITY_KEYID_free(akid);
+if (!key)
+ EVP_PKEY_free(newkey);
+if (ret)
+ ret = grow_chain(dane, !top && wrap_to_root ? UNTRUSTED : TRUSTED, cert);
+if (cert)
+ X509_free(cert);
+return ret;
+}
+
+static int
+wrap_cert(ssl_dane *dane, X509 *tacert, int depth)
+{
+if (dane->depth < 0)
+ dane->depth = depth + 1;
+
+/*
+ * If the TA certificate is self-issued, or need not be, use it directly.
+ * Otherwise, synthesize requisite ancestors.
+ */
+if ( !wrap_to_root
+ || X509_check_issued(tacert, tacert) == X509_V_OK)
+ return grow_chain(dane, TRUSTED, tacert);
+
+if (wrap_issuer(dane, 0, tacert, depth, WRAP_MID))
+ return grow_chain(dane, UNTRUSTED, tacert);
+return 0;
+}
+
+static int
+ta_signed(ssl_dane *dane, X509 *cert, int depth)
+{
+EVP_PKEY *pk;
+int done = 0;
+
+/*
+ * First check whether issued and signed by a TA cert, this is cheaper
+ * than the bare-public key checks below, since we can determine whether
+ * the candidate TA certificate issued the certificate to be checked
+ * first (name comparisons), before we bother with signature checks
+ * (public key operations).
+ */
+for (dane_cert_list x = dane->certs; !done && x; x = x->next)
+ {
+ if (X509_check_issued(x->value, cert) == X509_V_OK)
+ {
+ if (!(pk = X509_get_pubkey(x->value)))
+ {
+ /*
+ * The cert originally contained a valid pkey, which does
+ * not just vanish, so this is most likely a memory error.
+ */
+ done = -1;
+ break;
+ }
+ /* Check signature, since some other TA may work if not this. */
+ if (X509_verify(cert, pk) > 0)
+ done = wrap_cert(dane, x->value, depth) ? 1 : -1;
+ EVP_PKEY_free(pk);
+ }
+ }
+
+/*
+ * With bare TA public keys, we can't check whether the trust chain is
+ * issued by the key, but we can determine whether it is signed by the
+ * key, so we go with that.
+ *
+ * Ideally, the corresponding certificate was presented in the chain, and we
+ * matched it by its public key digest one level up. This code is here
+ * to handle adverse conditions imposed by sloppy administrators of
+ * receiving systems with poorly constructed chains.
+ *
+ * We'd like to optimize out keys that should not match when the cert's
+ * authority key id does not match the key id of this key computed via
+ * the RFC keyid algorithm (SHA-1 digest of public key bit-string sans
+ * ASN1 tag and length thus also excluding the unused bits field that is
+ * logically part of the length). However, some CAs have a non-standard
+ * authority keyid, so we lose. Too bad.
+ *
+ * This may push errors onto the stack when the certificate signature is
+ * not of the right type or length, throw these away,
+ */
+for (dane_pkey_list k = dane->pkeys; !done && k; k = k->next)
+ if (X509_verify(cert, k->value) > 0)
+ done = wrap_issuer(dane, k->value, cert, depth, WRAP_MID) ? 1 : -1;
+ else
+ ERR_clear_error();
+
+return done;
+}
+
+static int
+set_trust_anchor(X509_STORE_CTX *ctx, ssl_dane *dane, X509 *cert)
+{
+int matched = 0;
+int depth = 0;
+EVP_PKEY *takey;
+X509 *ca;
+STACK_OF(X509) *in = X509_STORE_CTX_get0_untrusted(ctx);
+
+if (!grow_chain(dane, UNTRUSTED, 0))
+ return -1;
+
+/*
+ * Accept a degenerate case: depth 0 self-signed trust-anchor.
+ */
+if (X509_check_issued(cert, cert) == X509_V_OK)
+ {
+ dane->depth = 0;
+ matched = match(dane->selectors[DANESSL_USAGE_DANE_TA], cert, 0);
+ if (matched > 0 && !grow_chain(dane, TRUSTED, cert))
+ matched = -1;
+ return matched;
+ }
+
+/* Make a shallow copy of the input untrusted chain. */
+if (!(in = sk_X509_dup(in)))
+ {
+ DANEerr(DANESSL_F_SET_TRUST_ANCHOR, ERR_R_MALLOC_FAILURE);
+ return -1;
+ }
+
+/*
+ * At each iteration we consume the issuer of the current cert. This
+ * reduces the length of the "in" chain by one. If no issuer is found,
+ * we are done. We also stop when a certificate matches a TA in the
+ * peer's TLSA RRset.
+ *
+ * Caller ensures that the initial certificate is not self-signed.
+ */
+for (int n = sk_X509_num(in); n > 0; --n, ++depth)
+ {
+ int i;
+ for (i = 0; i < n; ++i)
+ if (X509_check_issued(sk_X509_value(in, i), cert) == X509_V_OK)
+ break;
+
+ /*
+ * Final untrusted element with no issuer in the peer's chain, it may
+ * however be signed by a pkey or cert obtained via a TLSA RR.
+ */
+ if (i == n)
+ break;
+
+ /* Peer's chain contains an issuer ca. */
+ ca = sk_X509_delete(in, i);
+
+ /* If not a trust anchor, record untrusted ca and continue. */
+ if ((matched = match(dane->selectors[DANESSL_USAGE_DANE_TA], ca,
+ depth + 1)) == 0)
+ {
+ if (grow_chain(dane, UNTRUSTED, ca))
+ {
+ if (X509_check_issued(ca, ca) != X509_V_OK)
+ {
+ /* Restart with issuer as subject */
+ cert = ca;
+ continue;
+ }
+ /* Final self-signed element, skip ta_signed() check. */
+ cert = 0;
+ }
+ else
+ matched = -1;
+ }
+ else if(matched == MATCHED_CERT)
+ {
+ if(!wrap_cert(dane, ca, depth))
+ matched = -1;
+ }
+ else if(matched == MATCHED_PKEY)
+ {
+ if ( !(takey = X509_get_pubkey(ca))
+ || !wrap_issuer(dane, takey, cert, depth, WRAP_MID))
+ {
+ if (takey)
+ EVP_PKEY_free(takey);
+ else
+ DANEerr(DANESSL_F_SET_TRUST_ANCHOR, ERR_R_MALLOC_FAILURE);
+ matched = -1;
+ }
+ }
+ break;
+ }
+
+/* Shallow free the duplicated input untrusted chain. */
+sk_X509_free(in);
+
+/*
+ * When the loop exits, if "cert" is set, it is not self-signed and has
+ * no issuer in the chain, we check for a possible signature via a DNS
+ * obtained TA cert or public key.
+ */
+if (matched == 0 && cert)
+ matched = ta_signed(dane, cert, depth);
+
+return matched;
+}
+
+static int
+check_end_entity(X509_STORE_CTX *ctx, ssl_dane *dane, X509 *cert)
+{
+int matched;
+
+matched = match(dane->selectors[DANESSL_USAGE_DANE_EE], cert, 0);
+if (matched > 0)
+ {
+ dane->mdpth = 0;
+ dane->match = cert;
+ X509_up_ref(cert);
+ if(!X509_STORE_CTX_get0_chain(ctx))
+ {
+ STACK_OF(X509) * sk = sk_X509_new_null();
+ if (sk && sk_X509_push(sk, cert))
+ {
+ X509_up_ref(cert);
+ X509_STORE_CTX_set0_verified_chain(ctx, sk);
+ }
+ else
+ {
+ if (sk) sk_X509_free(sk);
+ DANEerr(DANESSL_F_CHECK_END_ENTITY, ERR_R_MALLOC_FAILURE);
+ return -1;
+ }
+ }
+ }
+return matched;
+}
+
+static int
+match_name(const char *certid, ssl_dane *dane)
+{
+int multi = dane->multi;
+
+for (dane_host_list hosts = dane->hosts; hosts; hosts = hosts->next)
+ {
+ int match_subdomain = 0;
+ const char *domain = hosts->value;
+ const char *parent;
+ int idlen;
+ int domlen;
+
+ if (*domain == '.' && domain[1] != '\0')
+ {
+ ++domain;
+ match_subdomain = 1;
+ }
+
+ /*
+ * Sub-domain match: certid is any sub-domain of hostname.
+ */
+ if(match_subdomain)
+ {
+ if ( (idlen = strlen(certid)) > (domlen = strlen(domain)) + 1
+ && certid[idlen - domlen - 1] == '.'
+ && !strcasecmp(certid + (idlen - domlen), domain))
+ return 1;
+ else
+ continue;
+ }
+
+ /*
+ * Exact match and initial "*" match. The initial "*" in a certid
+ * matches one (if multi is false) or more hostname components under
+ * the condition that the certid contains multiple hostname components.
+ */
+ if ( !strcasecmp(certid, domain)
+ || ( certid[0] == '*' && certid[1] == '.' && certid[2] != 0
+ && (parent = strchr(domain, '.')) != 0
+ && (idlen = strlen(certid + 1)) <= (domlen = strlen(parent))
+ && strcasecmp(multi ? parent + domlen - idlen : parent, certid+1) == 0))
+ return 1;
+ }
+return 0;
+}
+
+static const char *
+check_name(const char *name, int len)
+{
+const char *cp = name + len;
+
+while (len > 0 && !*--cp)
+ --len; /* Ignore trailing NULs */
+if (len <= 0)
+ return 0;
+for (cp = name; *cp; cp++)
+ {
+ char c = *cp;
+ if (!((c >= 'a' && c <= 'z') ||
+ (c >= '0' && c <= '9') ||
+ (c >= 'A' && c <= 'Z') ||
+ (c == '.' || c == '-') ||
+ (c == '*')))
+ return 0; /* Only LDH, '.' and '*' */
+ }
+if (cp - name != len) /* Guard against internal NULs */
+ return 0;
+return name;
+}
+
+static const char *
+parse_dns_name(const GENERAL_NAME *gn)
+{
+if (gn->type != GEN_DNS)
+ return 0;
+if (ASN1_STRING_type(gn->d.ia5) != V_ASN1_IA5STRING)
+ return 0;
+return check_name(CCS ASN1_STRING_get0_data(gn->d.ia5),
+ ASN1_STRING_length(gn->d.ia5));
+}
+
+static char *
+parse_subject_name(X509 *cert)
+{
+X509_NAME *name = X509_get_subject_name(cert);
+X509_NAME_ENTRY *entry;
+ASN1_STRING *entry_str;
+unsigned char *namebuf;
+int nid = NID_commonName;
+int len;
+int i;
+
+if (!name || (i = X509_NAME_get_index_by_NID(name, nid, -1)) < 0)
+ return 0;
+if (!(entry = X509_NAME_get_entry(name, i)))
+ return 0;
+if (!(entry_str = X509_NAME_ENTRY_get_data(entry)))
+ return 0;
+
+if ((len = ASN1_STRING_to_UTF8(&namebuf, entry_str)) < 0)
+ return 0;
+if (len <= 0 || check_name(CS namebuf, len) == 0)
+ {
+ OPENSSL_free(namebuf);
+ return 0;
+ }
+return CS namebuf;
+}
+
+static int
+name_check(ssl_dane *dane, X509 *cert)
+{
+int matched = 0;
+BOOL got_altname = FALSE;
+GENERAL_NAMES *gens;
+
+gens = X509_get_ext_d2i(cert, NID_subject_alt_name, 0, 0);
+if (gens)
+ {
+ int n = sk_GENERAL_NAME_num(gens);
+
+ for (int i = 0; i < n; ++i)
+ {
+ const GENERAL_NAME *gn = sk_GENERAL_NAME_value(gens, i);
+ const char *certid;
+
+ if (gn->type != GEN_DNS)
+ continue;
+ got_altname = TRUE;
+ certid = parse_dns_name(gn);
+ if (certid && *certid)
+ {
+ if ((matched = match_name(certid, dane)) == 0)
+ continue;
+ if (!(dane->mhost = OPENSSL_strdup(certid)))
+ matched = -1;
+ DEBUG(D_tls) debug_printf("Dane name_check: matched SAN %s\n", certid);
+ break;
+ }
+ }
+ GENERAL_NAMES_free(gens);
+ }
+
+/*
+ * XXX: Should the subjectName be skipped when *any* altnames are present,
+ * or only when DNS altnames are present?
+ */
+if (!got_altname)
+ {
+ char *certid = parse_subject_name(cert);
+ if (certid != 0 && *certid && (matched = match_name(certid, dane)) != 0)
+ {
+ DEBUG(D_tls) debug_printf("Dane name_check: matched SN %s\n", certid);
+ dane->mhost = OPENSSL_strdup(certid);
+ }
+ if (certid)
+ OPENSSL_free(certid);
+ }
+return matched;
+}
+
+static int
+verify_chain(X509_STORE_CTX *ctx)
+{
+int (*cb)(int, X509_STORE_CTX *) = X509_STORE_CTX_get_verify_cb(ctx);
+X509 *cert = X509_STORE_CTX_get0_cert(ctx);
+STACK_OF(X509) * chain = X509_STORE_CTX_get0_chain(ctx);
+int chain_length = sk_X509_num(chain);
+int ssl_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
+SSL *ssl = X509_STORE_CTX_get_ex_data(ctx, ssl_idx);
+ssl_dane *dane = SSL_get_ex_data(ssl, dane_idx);
+dane_selector_list issuer_rrs = dane->selectors[DANESSL_USAGE_PKIX_TA];
+dane_selector_list leaf_rrs = dane->selectors[DANESSL_USAGE_PKIX_EE];
+int matched = 0;
+
+DEBUG(D_tls) debug_printf("Dane verify_chain\n");
+
+/* Restore OpenSSL's internal_verify() as the signature check function */
+X509_STORE_CTX_set_verify(ctx, dane->verify);
+
+if ((matched = name_check(dane, cert)) < 0)
+ {
+ X509_STORE_CTX_set_error(ctx, X509_V_ERR_OUT_OF_MEM);
+ return 0;
+ }
+
+if (!matched)
+ {
+ X509_STORE_CTX_set_error_depth(ctx, 0);
+ X509_STORE_CTX_set_current_cert(ctx, cert);
+ X509_STORE_CTX_set_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH);
+ if (!cb(0, ctx))
+ return 0;
+ }
+matched = 0;
+
+/*
+ * Satisfy at least one usage 0 or 1 constraint, unless we've already
+ * matched a usage 2 trust anchor.
+ *
+ * XXX: internal_verify() doesn't callback with top certs that are not
+ * self-issued. This is fixed in OpenSSL 1.1.0.
+ */
+if (dane->roots && sk_X509_num(dane->roots))
+ {
+ X509 *top = sk_X509_value(chain, dane->depth);
+
+ dane->mdpth = dane->depth;
+ dane->match = top;
+ X509_up_ref(top);
+
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+ if (X509_check_issued(top, top) != X509_V_OK)
+ {
+ X509_STORE_CTX_set_error_depth(ctx, dane->depth);
+ X509_STORE_CTX_set_current_cert(ctx, top);
+ if (!cb(1, ctx))
+ return 0;
+ }
+#endif
+ /* Pop synthetic trust-anchor ancestors off the chain! */
+ while (--chain_length > dane->depth)
+ X509_free(sk_X509_pop(chain));
+ }
+else
+ {
+ int n = 0;
+ X509 *xn = cert;
+
+ /*
+ * Check for an EE match, then a CA match at depths > 0, and
+ * finally, if the EE cert is self-issued, for a depth 0 CA match.
+ */
+ if (leaf_rrs)
+ matched = match(leaf_rrs, xn, 0);
+ if (matched) DEBUG(D_tls) debug_printf("Dane verify_chain: matched EE\n");
+
+ if (!matched && issuer_rrs)
+ for (n = chain_length-1; !matched && n >= 0; --n)
+ {
+ xn = sk_X509_value(chain, n);
+ if (n > 0 || X509_check_issued(xn, xn) == X509_V_OK)
+ matched = match(issuer_rrs, xn, n);
+ }
+ if (matched) DEBUG(D_tls) debug_printf("Dane verify_chain: matched %s\n",
+ n>0 ? "CA" : "selfisssued EE");
+
+ if (!matched)
+ {
+ X509_STORE_CTX_set_error_depth(ctx, 0);
+ X509_STORE_CTX_set_current_cert(ctx, cert);
+ X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_UNTRUSTED);
+ if (!cb(0, ctx))
+ return 0;
+ }
+ else
+ {
+ dane->mdpth = n;
+ dane->match = xn;
+ X509_up_ref(xn);
+ }
+ }
+
+/* Tail recurse into OpenSSL's internal_verify */
+return dane->verify(ctx);
+}
+
+static void
+dane_reset(ssl_dane *dane)
+{
+dane->depth = -1;
+if (dane->mhost)
+ {
+ OPENSSL_free(dane->mhost);
+ dane->mhost = 0;
+ }
+if (dane->roots)
+ {
+ sk_X509_pop_free(dane->roots, X509_free);
+ dane->roots = 0;
+ }
+if (dane->chain)
+ {
+ sk_X509_pop_free(dane->chain, X509_free);
+ dane->chain = 0;
+ }
+if (dane->match)
+ {
+ X509_free(dane->match);
+ dane->match = 0;
+ }
+dane->mdpth = -1;
+}
+
+static int
+verify_cert(X509_STORE_CTX *ctx, void *unused_ctx)
+{
+static int ssl_idx = -1;
+SSL *ssl;
+ssl_dane *dane;
+int (*cb)(int, X509_STORE_CTX *) = X509_STORE_CTX_get_verify_cb(ctx);
+X509 *cert = X509_STORE_CTX_get0_cert(ctx);
+int matched;
+
+DEBUG(D_tls) debug_printf("Dane verify_cert\n");
+
+if (ssl_idx < 0)
+ ssl_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
+if (dane_idx < 0)
+ {
+ DANEerr(DANESSL_F_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
+ return -1;
+ }
+
+ssl = X509_STORE_CTX_get_ex_data(ctx, ssl_idx);
+if (!(dane = SSL_get_ex_data(ssl, dane_idx)) || !cert)
+ return X509_verify_cert(ctx);
+
+/* Reset for verification of a new chain, perhaps a renegotiation. */
+dane_reset(dane);
+
+if (dane->selectors[DANESSL_USAGE_DANE_EE])
+ {
+ if ((matched = check_end_entity(ctx, dane, cert)) > 0)
+ {
+ X509_STORE_CTX_set_error_depth(ctx, 0);
+ X509_STORE_CTX_set_current_cert(ctx, cert);
+ return cb(1, ctx);
+ }
+ if (matched < 0)
+ {
+ X509_STORE_CTX_set_error(ctx, X509_V_ERR_OUT_OF_MEM);
+ return -1;
+ }
+ }
+
+if (dane->selectors[DANESSL_USAGE_DANE_TA])
+ {
+ if ((matched = set_trust_anchor(ctx, dane, cert)) < 0)
+ {
+ X509_STORE_CTX_set_error(ctx, X509_V_ERR_OUT_OF_MEM);
+ return -1;
+ }
+ if (matched)
+ {
+ /*
+ * Check that setting the untrusted chain updates the expected
+ * structure member at the expected offset.
+ */
+ X509_STORE_CTX_trusted_stack(ctx, dane->roots);
+ X509_STORE_CTX_set_chain(ctx, dane->chain);
+ OPENSSL_assert(dane->chain == X509_STORE_CTX_get0_untrusted(ctx));
+ }
+ }
+
+/*
+ * Name checks and usage 0/1 constraint enforcement are delayed until
+ * X509_verify_cert() builds the full chain and calls our verify_chain()
+ * wrapper.
+ */
+dane->verify = X509_STORE_CTX_get_verify(ctx);
+X509_STORE_CTX_set_verify(ctx, verify_chain);
+
+if (X509_verify_cert(ctx))
+ return 1;
+
+/*
+ * If the chain is invalid, clear any matching cert or hostname, to
+ * protect callers that might erroneously rely on these alone without
+ * checking the validation status.
+ */
+if (dane->match)
+ {
+ X509_free(dane->match);
+ dane->match = 0;
+ }
+if (dane->mhost)
+ {
+ OPENSSL_free(dane->mhost);
+ dane->mhost = 0;
+ }
+ return 0;
+}
+
+static dane_list
+list_alloc(size_t vsize)
+{
+void *value = (void *) OPENSSL_malloc(vsize);
+dane_list l;
+
+if (!value)
+ {
+ DANEerr(DANESSL_F_LIST_ALLOC, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+if (!(l = (dane_list) OPENSSL_malloc(sizeof(*l))))
+ {
+ OPENSSL_free(value);
+ DANEerr(DANESSL_F_LIST_ALLOC, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+l->next = 0;
+l->value = value;
+return l;
+}
+
+static void
+list_free(void *list, void (*f)(void *))
+{
+dane_list next;
+
+for (dane_list head = (dane_list) list; head; head = next)
+ {
+ next = head->next;
+ if (f && head->value)
+ f(head->value);
+ OPENSSL_free(head);
+ }
+}
+
+static void
+ossl_free(void * p)
+{
+OPENSSL_free(p);
+}
+
+static void
+dane_mtype_free(void *p)
+{
+list_free(((dane_mtype) p)->data, ossl_free);
+OPENSSL_free(p);
+}
+
+static void
+dane_selector_free(void *p)
+{
+list_free(((dane_selector) p)->mtype, dane_mtype_free);
+OPENSSL_free(p);
+}
+
+
+
+/*
+
+Tidy up once the connection is finished with.
+
+Arguments
+ ssl The ssl connection handle
+
+=> Before calling SSL_free()
+tls_close() and tls_getc() [the error path] are the obvious places.
+Could we do it earlier - right after verification? In tls_client_start()
+right after SSL_connect() returns, in that case.
+
+*/
+
+void
+DANESSL_cleanup(SSL *ssl)
+{
+ssl_dane *dane;
+
+DEBUG(D_tls) debug_printf("Dane lib-cleanup\n");
+
+if (dane_idx < 0 || !(dane = SSL_get_ex_data(ssl, dane_idx)))
+ return;
+(void) SSL_set_ex_data(ssl, dane_idx, 0);
+
+dane_reset(dane);
+if (dane->hosts)
+ list_free(dane->hosts, ossl_free);
+for (int u = 0; u <= DANESSL_USAGE_LAST; ++u)
+ if (dane->selectors[u])
+ list_free(dane->selectors[u], dane_selector_free);
+if (dane->pkeys)
+ list_free(dane->pkeys, pkey_free);
+if (dane->certs)
+ list_free(dane->certs, cert_free);
+OPENSSL_free(dane);
+}
+
+static dane_host_list
+host_list_init(const char **src)
+{
+dane_host_list head = NULL;
+
+while (*src)
+ {
+ dane_host_list elem = (dane_host_list) OPENSSL_malloc(sizeof(*elem));
+ if (elem == 0)
+ {
+ list_free(head, ossl_free);
+ return 0;
+ }
+ elem->value = OPENSSL_strdup(*src++);
+ LINSERT(head, elem);
+ }
+return head;
+}
+
+
+int
+DANESSL_get_match_cert(SSL *ssl, X509 **match, const char **mhost, int *depth)
+{
+ssl_dane *dane;
+
+if (dane_idx < 0 || (dane = SSL_get_ex_data(ssl, dane_idx)) == 0)
+ {
+ DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_INIT);
+ return -1;
+ }
+
+if (dane->match)
+ {
+ if (match)
+ *match = dane->match;
+ if (mhost)
+ *mhost = dane->mhost;
+ if (depth)
+ *depth = dane->mdpth;
+ }
+
+ return (dane->match != 0);
+}
+
+
+#ifdef never_called
+int
+DANESSL_verify_chain(SSL *ssl, STACK_OF(X509) *chain)
+{
+int ret;
+X509 *cert;
+X509_STORE_CTX * store_ctx;
+SSL_CTX *ssl_ctx = SSL_get_SSL_CTX(ssl);
+X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx);
+int store_ctx_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
+
+cert = sk_X509_value(chain, 0);
+if (!(store_ctx = X509_STORE_CTX_new()))
+ {
+ DANEerr(DANESSL_F_DANESSL_VERIFY_CHAIN, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+if (!X509_STORE_CTX_init(store_ctx, store, cert, chain))
+ {
+ X509_STORE_CTX_free(store_ctx);
+ return 0;
+ }
+X509_STORE_CTX_set_ex_data(store_ctx, store_ctx_idx, ssl);
+
+X509_STORE_CTX_set_default(store_ctx,
+ SSL_is_server(ssl) ? "ssl_client" : "ssl_server");
+X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(store_ctx),
+ SSL_get0_param(ssl));
+
+if (SSL_get_verify_callback(ssl))
+ X509_STORE_CTX_set_verify_cb(store_ctx, SSL_get_verify_callback(ssl));
+
+ret = verify_cert(store_ctx, NULL);
+
+SSL_set_verify_result(ssl, X509_STORE_CTX_get_error(store_ctx));
+X509_STORE_CTX_cleanup(store_ctx);
+
+return (ret);
+}
+#endif
+
+
+
+
+/*
+
+Call this for each TLSA record found for the target, after the
+DANE setup has been done on the ssl connection handle.
+
+Arguments:
+ ssl Connection handle
+ usage TLSA record field
+ selector TLSA record field
+ mdname ??? message digest name?
+ data ??? TLSA record megalump?
+ dlen length of data
+
+Return
+ -1 on error
+ 0 action not taken
+ 1 record accepted
+*/
+
+int
+DANESSL_add_tlsa(SSL *ssl, uint8_t usage, uint8_t selector, const char *mdname,
+ unsigned const char *data, size_t dlen)
+{
+ssl_dane *dane;
+dane_selector_list s = 0;
+dane_mtype_list m = 0;
+dane_data_list d = 0;
+dane_cert_list xlist = 0;
+dane_pkey_list klist = 0;
+const EVP_MD *md = 0;
+
+DEBUG(D_tls) debug_printf("Dane add-tlsa: usage %u sel %u mdname \"%s\"\n",
+ usage, selector, mdname);
+
+if(dane_idx < 0 || !(dane = SSL_get_ex_data(ssl, dane_idx)))
+ {
+ DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_INIT);
+ return -1;
+ }
+
+if (usage > DANESSL_USAGE_LAST)
+ {
+ DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_USAGE);
+ return 0;
+ }
+if (selector > DANESSL_SELECTOR_LAST)
+ {
+ DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_SELECTOR);
+ return 0;
+ }
+
+/* Support built-in standard one-digit mtypes */
+if (mdname && *mdname && mdname[1] == '\0')
+ switch (*mdname - '0')
+ {
+ case DANESSL_MATCHING_FULL: mdname = 0; break;
+ case DANESSL_MATCHING_2256: mdname = "sha256"; break;
+ case DANESSL_MATCHING_2512: mdname = "sha512"; break;
+ }
+if (mdname && *mdname && !(md = EVP_get_digestbyname(mdname)))
+ {
+ DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_DIGEST);
+ return 0;
+ }
+if (mdname && *mdname && dlen != EVP_MD_size(md))
+ {
+ DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_DATA_LENGTH);
+ return 0;
+ }
+if (!data)
+ {
+ DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_NULL_DATA);
+ return 0;
+ }
+
+/*
+ * Full Certificate or Public Key when NULL or empty digest name
+ */
+if (!mdname || !*mdname)
+ {
+ X509 *x = 0;
+ EVP_PKEY *k = 0;
+ const unsigned char *p = data;
+
+#define xklistinit(lvar, ltype, var, freeFunc) do { \
+ (lvar) = (ltype) OPENSSL_malloc(sizeof(*(lvar))); \
+ if ((lvar) == 0) { \
+ DANEerr(DANESSL_F_ADD_TLSA, ERR_R_MALLOC_FAILURE); \
+ freeFunc((var)); \
+ return 0; \
+ } \
+ (lvar)->next = 0; \
+ lvar->value = var; \
+ } while (0)
+#define xkfreeret(ret) do { \
+ if (xlist) list_free(xlist, cert_free); \
+ if (klist) list_free(klist, pkey_free); \
+ return (ret); \
+ } while (0)
+
+ switch (selector)
+ {
+ case DANESSL_SELECTOR_CERT:
+ if (!d2i_X509(&x, &p, dlen) || dlen != p - data)
+ {
+ if (x)
+ X509_free(x);
+ DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_CERT);
+ return 0;
+ }
+ k = X509_get_pubkey(x);
+ EVP_PKEY_free(k);
+ if (k == 0)
+ {
+ X509_free(x);
+ DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_CERT_PKEY);
+ return 0;
+ }
+ if (usage == DANESSL_USAGE_DANE_TA)
+ xklistinit(xlist, dane_cert_list, x, X509_free);
+ break;
+
+ case DANESSL_SELECTOR_SPKI:
+ if (!d2i_PUBKEY(&k, &p, dlen) || dlen != p - data)
+ {
+ if (k)
+ EVP_PKEY_free(k);
+ DANEerr(DANESSL_F_ADD_TLSA, DANESSL_R_BAD_PKEY);
+ return 0;
+ }
+ if (usage == DANESSL_USAGE_DANE_TA)
+ xklistinit(klist, dane_pkey_list, k, EVP_PKEY_free);
+ break;
+ }
+ }
+
+/* Find insertion point and don't add duplicate elements. */
+for (s = dane->selectors[usage]; s; s = s->next)
+ if (s->value->selector == selector)
+ {
+ for (m = s->value->mtype; m; m = m->next)
+ if (m->value->md == md)
+ {
+ for (d = m->value->data; d; d = d->next)
+ if ( d->value->datalen == dlen
+ && memcmp(d->value->data, data, dlen) == 0)
+ xkfreeret(1);
+ break;
+ }
+ break;
+ }
+
+if ((d = (dane_data_list) list_alloc(sizeof(*d->value) + dlen)) == 0)
+ xkfreeret(0);
+d->value->datalen = dlen;
+memcpy(d->value->data, data, dlen);
+if (!m)
+ {
+ if ((m = (dane_mtype_list) list_alloc(sizeof(*m->value))) == 0)
+ {
+ list_free(d, ossl_free);
+ xkfreeret(0);
+ }
+ m->value->data = 0;
+ if ((m->value->md = md) != 0)
+ m->value->mdlen = dlen;
+ if (!s)
+ {
+ if ((s = (dane_selector_list) list_alloc(sizeof(*s->value))) == 0)
+ {
+ list_free(m, dane_mtype_free);
+ xkfreeret(0);
+ }
+ s->value->mtype = 0;
+ s->value->selector = selector;
+ LINSERT(dane->selectors[usage], s);
+ }
+ LINSERT(s->value->mtype, m);
+ }
+LINSERT(m->value->data, d);
+
+if (xlist)
+ LINSERT(dane->certs, xlist);
+else if (klist)
+ LINSERT(dane->pkeys, klist);
+++dane->count;
+return 1;
+}
+
+
+
+
+/*
+Call this once we have an ssl connection handle but before
+making the TLS connection.
+
+=> In tls_client_start() after the call to SSL_new()
+and before the call to SSL_connect(). Exactly where
+probably does not matter.
+We probably want to keep our existing SNI handling;
+call this with NULL.
+
+Arguments:
+ ssl Connection handle
+ sni_domain Optional peer server name
+ hostnames list of names to chack against peer cert
+
+Return
+ -1 on fatal error
+ 0 nonfatal error
+ 1 success
+*/
+
+int
+DANESSL_init(SSL *ssl, const char *sni_domain, const char **hostnames)
+{
+ssl_dane *dane;
+
+DEBUG(D_tls) debug_printf("Dane ssl_init\n");
+if (dane_idx < 0)
+ {
+ DANEerr(DANESSL_F_INIT, DANESSL_R_LIBRARY_INIT);
+ return -1;
+ }
+
+if (sni_domain && !SSL_set_tlsext_host_name(ssl, sni_domain))
+ return 0;
+
+if ((dane = (ssl_dane *) OPENSSL_malloc(sizeof(ssl_dane))) == 0)
+ {
+ DANEerr(DANESSL_F_INIT, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+if (!SSL_set_ex_data(ssl, dane_idx, dane))
+ {
+ DANEerr(DANESSL_F_INIT, ERR_R_MALLOC_FAILURE);
+ OPENSSL_free(dane);
+ return 0;
+ }
+
+dane->verify = 0;
+dane->hosts = 0;
+dane->thost = 0;
+dane->pkeys = 0;
+dane->certs = 0;
+dane->chain = 0;
+dane->match = 0;
+dane->roots = 0;
+dane->depth = -1;
+dane->mhost = 0; /* Future SSL control interface */
+dane->mdpth = 0; /* Future SSL control interface */
+dane->multi = 0; /* Future SSL control interface */
+dane->count = 0;
+dane->hosts = 0;
+
+for (int i = 0; i <= DANESSL_USAGE_LAST; ++i)
+ dane->selectors[i] = 0;
+
+if (hostnames && (dane->hosts = host_list_init(hostnames)) == 0)
+ {
+ DANEerr(DANESSL_F_INIT, ERR_R_MALLOC_FAILURE);
+ DANESSL_cleanup(ssl);
+ return 0;
+ }
+
+return 1;
+}
+
+
+/*
+
+Call this once we have a context to work with, but
+before DANESSL_init()
+
+=> in tls_client_start(), after tls_init() call gives us the ctx,
+if we decide we want to (policy) and can (TLSA records available)
+replacing (? what about fallback) everything from testing tls_verify_hosts
+down to just before calling SSL_new() for the conn handle.
+
+Arguments
+ ctx SSL context
+
+Return
+ -1 Error
+ 1 Success
+*/
+
+int
+DANESSL_CTX_init(SSL_CTX *ctx)
+{
+DEBUG(D_tls) debug_printf("Dane ctx-init\n");
+if (dane_idx >= 0)
+ {
+ SSL_CTX_set_cert_verify_callback(ctx, verify_cert, 0);
+ return 1;
+ }
+DANEerr(DANESSL_F_CTX_INIT, DANESSL_R_LIBRARY_INIT);
+return -1;
+}
+
+static void
+dane_init(void)
+{
+/*
+ * Store library id in zeroth function slot, used to locate the library
+ * name. This must be done before we load the error strings.
+ */
+err_lib_dane = ERR_get_next_error_library();
+
+#ifndef OPENSSL_NO_ERR
+if (err_lib_dane > 0)
+ {
+ dane_str_functs[0].error |= ERR_PACK(err_lib_dane, 0, 0);
+ ERR_load_strings(err_lib_dane, dane_str_functs);
+ ERR_load_strings(err_lib_dane, dane_str_reasons);
+ }
+#endif
+
+/*
+ * Register SHA-2 digests, if implemented and not already registered.
+ */
+#if defined(LN_sha256) && defined(NID_sha256) && !defined(OPENSSL_NO_SHA256)
+if (!EVP_get_digestbyname(LN_sha224)) EVP_add_digest(EVP_sha224());
+if (!EVP_get_digestbyname(LN_sha256)) EVP_add_digest(EVP_sha256());
+#endif
+#if defined(LN_sha512) && defined(NID_sha512) && !defined(OPENSSL_NO_SHA512)
+if (!EVP_get_digestbyname(LN_sha384)) EVP_add_digest(EVP_sha384());
+if (!EVP_get_digestbyname(LN_sha512)) EVP_add_digest(EVP_sha512());
+#endif
+
+/*
+ * Register an SSL index for the connection-specific ssl_dane structure.
+ * Using a separate index makes it possible to add DANE support to
+ * existing OpenSSL releases that don't have a suitable pointer in the
+ * SSL structure.
+ */
+dane_idx = SSL_get_ex_new_index(0, 0, 0, 0, 0);
+}
+
+
+#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
+static void
+run_once(volatile int * once, void (*init)(void))
+{
+int wlock = 0;
+
+CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
+if (!*once)
+ {
+ CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
+ CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
+ wlock = 1;
+ if (!*once)
+ {
+ *once = 1;
+ init();
+ }
+ }
+if (wlock)
+ CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
+else
+ CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
+}
+#endif
+
+
+
+/*
+
+Call this once. Probably early in startup will do; may need
+to be after SSL library init.
+
+=> put after call to tls_init() for now
+
+Return
+ 1 Success
+ 0 Fail
+*/
+
+int
+DANESSL_library_init(void)
+{
+static CRYPTO_ONCE once = CRYPTO_ONCE_STATIC_INIT;
+
+DEBUG(D_tls) debug_printf("Dane lib-init\n");
+(void) CRYPTO_THREAD_run_once(&once, dane_init);
+
+#if defined(LN_sha256)
+/* No DANE without SHA256 support */
+if (dane_idx >= 0 && EVP_get_digestbyname(LN_sha256) != 0)
+ return 1;
+#endif
+DANEerr(DANESSL_F_LIBRARY_INIT, DANESSL_R_SUPPORT);
+return 0;
+}
+
+
+/* vi: aw ai sw=2
+*/
diff --git a/src/dane.c b/src/dane.c
new file mode 100644
index 0000000..5ba6196
--- /dev/null
+++ b/src/dane.c
@@ -0,0 +1,48 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012, 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This module provides DANE (RFC6659) support for Exim. See also
+the draft RFC for DANE-over-SMTP, "SMTP security via opportunistic DANE TLS"
+(V. Dukhovni, W. Hardaker) - version 10, dated May 25, 2014.
+
+The code for DANE support with Openssl was provided by V.Dukhovni.
+
+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"
+
+/* 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_DANE
+static void dummy(int x) { dummy(x-1); }
+#else
+
+/* Enabling DANE without enabling TLS cannot work. Abort the compilation. */
+# ifdef DISABLE_TLS
+# error DANE support requires that TLS support must be enabled. Abort build.
+# endif
+
+/* DNSSEC support is also required */
+# ifndef RES_USE_DNSSEC
+# error DANE support requires that the DNS resolver library supports DNSSEC
+# endif
+
+# ifdef USE_OPENSSL
+# include "dane-openssl.c"
+# endif
+
+
+#endif /* SUPPORT_DANE */
+
+/* End of dane.c */
diff --git a/src/danessl.h b/src/danessl.h
new file mode 100644
index 0000000..1d6439e
--- /dev/null
+++ b/src/danessl.h
@@ -0,0 +1,47 @@
+/*
+ * Author: Viktor Dukhovni
+ * License: THIS CODE IS IN THE PUBLIC DOMAIN.
+ */
+#ifndef HEADER_DANESSL_H
+#define HEADER_DANESSL_H
+
+#include <stdint.h>
+#include <openssl/ssl.h>
+
+/*-
+ * Certificate usages:
+ * https://tools.ietf.org/html/rfc6698#section-2.1.1
+ */
+#define DANESSL_USAGE_PKIX_TA 0
+#define DANESSL_USAGE_PKIX_EE 1
+#define DANESSL_USAGE_DANE_TA 2
+#define DANESSL_USAGE_DANE_EE 3
+#define DANESSL_USAGE_LAST DANESSL_USAGE_DANE_EE
+
+/*-
+ * Selectors:
+ * https://tools.ietf.org/html/rfc6698#section-2.1.2
+ */
+#define DANESSL_SELECTOR_CERT 0
+#define DANESSL_SELECTOR_SPKI 1
+#define DANESSL_SELECTOR_LAST DANESSL_SELECTOR_SPKI
+
+/*-
+ * Matching types:
+ * https://tools.ietf.org/html/rfc6698#section-2.1.3
+ */
+#define DANESSL_MATCHING_FULL 0
+#define DANESSL_MATCHING_2256 1
+#define DANESSL_MATCHING_2512 2
+#define DANESSL_MATCHING_LAST DANESSL_MATCHING_2512
+
+extern int DANESSL_library_init(void);
+extern int DANESSL_CTX_init(SSL_CTX *);
+extern int DANESSL_init(SSL *, const char *, const char **);
+extern void DANESSL_cleanup(SSL *);
+extern int DANESSL_add_tlsa(SSL *, uint8_t, uint8_t, const char *,
+ unsigned const char *, size_t);
+extern int DANESSL_get_match_cert(SSL *, X509 **, const char **, int *);
+extern int DANESSL_verify_chain(SSL *, STACK_OF(X509) *);
+
+#endif
diff --git a/src/dbfn.c b/src/dbfn.c
new file mode 100644
index 0000000..ea94b7f
--- /dev/null
+++ b/src/dbfn.c
@@ -0,0 +1,681 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "exim.h"
+
+/* We have buffers holding path names for database files.
+PATH_MAX could be used here, but would be wasting memory, as we deal
+with database files like $spooldirectory/db/<name> */
+#define PATHLEN 256
+
+
+/* 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. */
+
+
+
+/*************************************************
+* 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).
+ panic If TRUE, panic on failure to create the db directory
+
+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, BOOL panic)
+{
+int rc, save_errno;
+BOOL read_only = flags == O_RDONLY;
+flock_t lock_data;
+uschar dirname[PATHLEN], filename[PATHLEN];
+
+DEBUG(D_hints_lookup) acl_level++;
+
+/* 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. */
+
+snprintf(CS dirname, sizeof(dirname), "%s/db", spool_directory);
+snprintf(CS filename, sizeof(filename), "%s/%s.lockfile", dirname, name);
+
+priv_drop_temp(exim_uid, exim_gid);
+if ((dbblock->lockfd = Uopen(filename, O_RDWR, EXIMDB_LOCKFILE_MODE)) < 0)
+ {
+ (void)directory_make(spool_directory, US"db", EXIMDB_DIRECTORY_MODE, panic);
+ dbblock->lockfd = Uopen(filename, O_RDWR|O_CREAT, EXIMDB_LOCKFILE_MODE);
+ }
+priv_restore();
+
+if (dbblock->lockfd < 0)
+ {
+ log_write(0, LOG_MAIN, "%s",
+ string_open_failed("database lock file %s", filename));
+ errno = 0; /* Indicates locking failure */
+ DEBUG(D_hints_lookup) acl_level--;
+ 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_indent("locking %s\n", filename);
+
+sigalrm_seen = FALSE;
+ALARM(EXIMDB_LOCK_TIMEOUT);
+rc = fcntl(dbblock->lockfd, F_SETLKW, &lock_data);
+ALARM_CLR(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", filename,
+ errno == ETIMEDOUT ? "timed out" : strerror(errno));
+ (void)close(dbblock->lockfd);
+ errno = 0; /* Indicates locking failure */
+ DEBUG(D_hints_lookup) acl_level--;
+ return NULL;
+ }
+
+DEBUG(D_hints_lookup) debug_printf_indent("locked %s\n", filename);
+
+/* 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. */
+
+snprintf(CS filename, sizeof(filename), "%s/%s", dirname, name);
+
+priv_drop_temp(exim_uid, exim_gid);
+dbblock->dbptr = exim_dbopen(filename, dirname, flags, EXIMDB_MODE);
+if (!dbblock->dbptr && errno == ENOENT && flags == O_RDWR)
+ {
+ DEBUG(D_hints_lookup)
+ debug_printf_indent("%s appears not to exist: trying to create\n", filename);
+ dbblock->dbptr = exim_dbopen(filename, dirname, flags|O_CREAT, EXIMDB_MODE);
+ }
+save_errno = errno;
+priv_restore();
+
+/* If the open has failed, return NULL, leaving errno set. If lof is TRUE,
+log the event - also for debugging - but debug only if the file just doesn't
+exist. */
+
+if (!dbblock->dbptr)
+ {
+ errno = save_errno;
+ if (lof && save_errno != ENOENT)
+ log_write(0, LOG_MAIN, "%s", string_open_failed("DB file %s",
+ filename));
+ else
+ DEBUG(D_hints_lookup)
+ debug_printf_indent("%s\n", CS string_open_failed("DB file %s",
+ filename));
+ (void)close(dbblock->lockfd);
+ errno = save_errno;
+ DEBUG(D_hints_lookup) acl_level--;
+ return NULL;
+ }
+
+DEBUG(D_hints_lookup)
+ debug_printf_indent("opened hints database %s: flags=%s\n", filename,
+ 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);
+DEBUG(D_hints_lookup)
+ { debug_printf_indent("closed hints database and lockfile\n"); acl_level--; }
+}
+
+
+
+
+/*************************************************
+* 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, const uschar *key, int *length)
+{
+void *yield;
+EXIM_DATUM key_datum, result_datum;
+int klen = Ustrlen(key) + 1;
+uschar * key_copy = store_get(klen, key);
+
+memcpy(key_copy, key, klen);
+
+DEBUG(D_hints_lookup) debug_printf_indent("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_set(&key_datum, key_copy);
+exim_datum_size_set(&key_datum, klen);
+
+if (!exim_dbget(dbblock->dbptr, &key_datum, &result_datum)) return NULL;
+
+/* Assume the data store could have been tainted. Properly, we should
+store the taint status with the data. */
+
+yield = store_get(exim_datum_size_get(&result_datum), GET_TAINTED);
+memcpy(yield, exim_datum_data_get(&result_datum), exim_datum_size_get(&result_datum));
+if (length) *length = exim_datum_size_get(&result_datum);
+
+exim_datum_free(&result_datum); /* Some DBM libs require freeing */
+return yield;
+}
+
+
+/* Read a record. If the length is not as expected then delete it, write
+an error log line, delete the record and return NULL.
+Use this for fixed-size records (so not retry or wait records).
+
+Arguments:
+ dbblock a pointer to an open database block
+ key the key of the record to be read
+ length the expected record length
+
+Returns: a pointer to the retrieved record, or
+ NULL if the record is not found/bad
+*/
+
+void *
+dbfn_read_enforce_length(open_db * dbblock, const uschar * key, size_t length)
+{
+int rlen;
+void * yield = dbfn_read_with_length(dbblock, key, &rlen);
+
+if (yield)
+ {
+ if (rlen == length) return yield;
+ log_write(0, LOG_MAIN|LOG_PANIC, "Bad db record size for '%s'", key);
+ dbfn_delete(dbblock, key);
+ }
+return NULL;
+}
+
+
+/*************************************************
+* 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, const uschar *key, void *ptr, int length)
+{
+EXIM_DATUM key_datum, value_datum;
+dbdata_generic *gptr = (dbdata_generic *)ptr;
+int klen = Ustrlen(key) + 1;
+uschar * key_copy = store_get(klen, key);
+
+memcpy(key_copy, key, klen);
+gptr->time_stamp = time(NULL);
+
+DEBUG(D_hints_lookup) debug_printf_indent("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_set(&key_datum, key_copy);
+exim_datum_size_set(&key_datum, klen);
+exim_datum_data_set(&value_datum, ptr);
+exim_datum_size_set(&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, const uschar *key)
+{
+int klen = Ustrlen(key) + 1;
+uschar * key_copy = store_get(klen, key);
+EXIM_DATUM key_datum;
+
+DEBUG(D_hints_lookup) debug_printf_indent("dbfn_delete: key=%s\n", key);
+
+memcpy(key_copy, key, klen);
+exim_datum_init(&key_datum); /* Some DBM libraries require clearing */
+exim_datum_data_set(&key_datum, key_copy);
+exim_datum_size_set(&key_datum, klen);
+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;
+
+DEBUG(D_hints_lookup) debug_printf_indent("dbfn_scan\n");
+
+/* Some dbm require an initialization */
+
+if (start) *cursor = exim_dbcreate_cursor(dbblock->dbptr);
+
+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_get(&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, TRUE);
+ stop = clock();
+
+ if (odb)
+ {
+ 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
index 0000000..07d4a62
--- /dev/null
+++ b/src/dbfunctions.h
@@ -0,0 +1,38 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2021 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#ifndef DBFUNCTIONS_H
+#define DBFUNCTIONS_H
+
+/* Functions for reading/writing exim database files */
+
+void dbfn_close(open_db *);
+int dbfn_delete(open_db *, const uschar *);
+open_db *dbfn_open(uschar *, int, open_db *, BOOL, BOOL);
+void *dbfn_read_with_length(open_db *, const uschar *, int *);
+void *dbfn_read_enforce_length(open_db *, const uschar *, size_t);
+uschar *dbfn_scan(open_db *, BOOL, EXIM_CURSOR **);
+int dbfn_write(open_db *, const 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
+
+#endif
+/* End of dbfunctions.h */
diff --git a/src/dcc.c b/src/dcc.c
new file mode 100644
index 0000000..a9124a0
--- /dev/null
+++ b/src/dcc.c
@@ -0,0 +1,490 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Wolfgang Breyha 2005 - 2019
+ * Vienna University Computer Center
+ * wbreyha@gmx.net
+ * See the file NOTICE for conditions of use and distribution.
+ *
+ * Copyright (c) The Exim Maintainers 2015 - 2021
+ */
+
+/* Code for calling dccifd. Called from acl.c. */
+
+#include "exim.h"
+#ifdef EXPERIMENTAL_DCC
+#include "dcc.h"
+#include "unistd.h"
+
+#define DCC_HEADER_LIMIT 120
+
+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. */
+
+static int flushbuffer
+(int socket, gstring *buffer)
+{
+int rsp;
+
+rsp = write(socket, buffer->s, buffer->ptr);
+DEBUG(D_acl)
+ debug_printf("DCC: flushbuffer(): Result of the write() = %d\n", rsp);
+if(rsp < 0)
+ {
+ DEBUG(D_acl)
+ debug_printf("DCC: flushbuffer(): Error writing buffer to socket: %s\n", strerror(errno));
+ return errno;
+ }
+DEBUG(D_acl)
+ debug_printf("DCC: flushbuffer(): Wrote buffer to socket:\n%.*s\n", buffer->ptr, buffer->s);
+return 0;
+}
+
+int
+dcc_process(uschar **listptr)
+{
+int sep = 0;
+const uschar *list = *listptr;
+FILE *data_file;
+uschar *dcc_default_ip_option = US"127.0.0.1";
+uschar *dcc_helo_option = US"localhost";
+uschar *xtra_hdrs = NULL;
+uschar *override_client_ip = NULL;
+
+/* from local_scan */
+int dcc_resplen, retval, sockfd, resp;
+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];
+gstring *dcc_headers;
+gstring *sendbuf;
+uschar *dcc_return_text = US"''";
+struct header_line *mail_headers;
+uschar *dcc_acl_options;
+gstring *dcc_xtra_hdrs;
+gstring *dcc_header_str;
+
+/* grep 1st option */
+if ((dcc_acl_options = string_nextinlist(&list, &sep, NULL, 0)))
+ {
+ /* parse 1st option */
+ if ( strcmpic(dcc_acl_options, US"false") == 0
+ || Ustrcmp(dcc_acl_options, "0") == 0)
+ return FAIL; /* explicitly no matching */
+ }
+else
+ return FAIL; /* empty means "don't match anything" */
+
+sep = 0;
+
+/* if we scanned this message last time, just return */
+if (dcc_ok)
+ return dcc_rc;
+
+/* open the spooled body */
+for (int i = 0; i < 2; i++)
+ {
+ uschar message_subdir[2];
+ set_subdir_str(message_subdir, message_id, i);
+ if ((data_file = Ufopen(
+ spool_fname(US"input", message_subdir, message_id, US"-D"), "rb")))
+ break;
+ }
+
+if (!data_file)
+ {
+ /* error while spooling */
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "DCC: 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: warning - invalid dccifd address: '%s'", dccifd_address);
+ (void)fclose(data_file);
+ return DEFER;
+ }
+ }
+
+/* dcc_headers is what we send as dccifd options - see man dccifd */
+/* We don't support any other option than 'header' so just copy that */
+dcc_headers = string_cat(NULL, dccifd_options);
+/* 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);
+ }
+/* build options block */
+dcc_headers = string_append(dcc_headers, 5, US"\n", client_ip, US"\nHELO ", dcc_helo_option, US"\n");
+
+/* initialize the other variables */
+mail_headers = header_list;
+/* we set the default return value to DEFER */
+retval = DEFER;
+
+/* send a null return path as "<>". */
+dcc_headers = string_cat (dcc_headers, *sender_address ? sender_address : US"<>");
+dcc_headers = string_catn(dcc_headers, US"\n", 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(CS sockip);
+ bzero(CS &serv_addr_in, sizeof(serv_addr_in));
+ serv_addr_in.sin_family = AF_INET;
+ bcopy(CS ipaddress->h_addr, CS &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(&serv_addr, sizeof(serv_addr));
+ serv_addr.sun_family = AF_UNIX;
+ Ustrncpy(US serv_addr.sun_path, sockpath, sizeof(serv_addr.sun_path));
+ 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("DCC: -----------------------------------\nDCC: Socket opened; now sending input\n"
+ "DCC: -----------------------------------\n");
+
+/* let's send each of the recipients to dccifd */
+for (int i = 0; i < recipients_count; i++)
+ {
+ DEBUG(D_acl)
+ debug_printf("DCC: recipient = %s\n",recipients_list[i].address);
+ dcc_headers = string_append(dcc_headers, 2, recipients_list[i].address, "\n");
+ }
+/* send a blank line between options and message */
+dcc_headers = string_catn(dcc_headers, US"\n", 1);
+/* Now we send the input buffer */
+(void) string_from_gstring(dcc_headers);
+DEBUG(D_acl)
+ debug_printf("DCC: ***********************************\nDCC: Sending options:\n%s"
+ "DCC: ***********************************\n", dcc_headers->s);
+if (flushbuffer(sockfd, dcc_headers) != 0)
+ {
+ (void)fclose(data_file);
+ return retval;
+ }
+
+/* now send the message */
+/* First send the headers */
+DEBUG(D_acl)
+ debug_printf("DCC: ***********************************\nDCC: Sending headers:\n");
+sendbuf = string_get(8192);
+sendbuf = string_catn(sendbuf, mail_headers->text, mail_headers->slen);
+while((mail_headers=mail_headers->next))
+ sendbuf = string_catn(sendbuf, mail_headers->text, mail_headers->slen);
+
+/* a blank line separates header from body */
+sendbuf = string_catn(sendbuf, US"\r\n", 2);
+(void) string_from_gstring(sendbuf);
+gstring_release_unused(sendbuf);
+DEBUG(D_acl)
+ debug_printf("%sDCC: ***********************************\n", sendbuf->s);
+if (flushbuffer(sockfd, sendbuf) != 0)
+ {
+ (void)fclose(data_file);
+ return retval;
+ }
+
+/* now send the body */
+DEBUG(D_acl)
+ debug_printf("DCC: ***********************************\nDCC: Writing body:\n");
+(void)fseek(data_file, SPOOL_DATA_START_OFFSET, SEEK_SET);
+
+gstring filebuf = { .size = big_buffer_size, .ptr = 0, .s = big_buffer };
+
+while((filebuf.ptr = fread(filebuf.s, 1, filebuf.size, data_file)) > 0)
+ if (flushbuffer(sockfd, &filebuf) != 0)
+ {
+ (void)fclose(data_file);
+ return retval;
+ }
+DEBUG(D_acl)
+ debug_printf("DCC: ***********************************\n");
+
+/* shutdown() the socket */
+if(shutdown(sockfd, SHUT_WR) < 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("DCC: Input sent.\n"
+ "DCC: +++++++++++++++++++++++++++++++++++\n"
+ "DCC: Now receiving output from server\n"
+ "DCC: -----------------------------------\n");
+
+/********************************
+ * receiving output from dccifd *
+ ********************************/
+
+/******************************************************************
+ * 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. *
+ ******************************************************************/
+
+int line = 1; /* we start at the first line of the output */
+int bufoffset;
+
+dcc_header_str = string_get(DCC_HEADER_LIMIT + 2);
+/* Let's read from the socket until there's nothing left to read */
+while((dcc_resplen = read(sockfd, big_buffer, big_buffer_size-1)) > 0)
+ {
+ /* fail on read error */
+ if(dcc_resplen < 0)
+ {
+ DEBUG(D_acl)
+ debug_printf("DCC: Error reading from socket: %s\n", strerror(errno));
+ (void)fclose(data_file);
+ return retval;
+ }
+ /* make the answer 0-terminated. only needed for debug_printf */
+ DEBUG(D_acl)
+ debug_printf("DCC: Length of the output buffer is: %d\nDCC: Output buffer is:\n"
+ "DCC: -----------------------------------\n%.*s\n"
+ "DCC: -----------------------------------\n", dcc_resplen, dcc_resplen, big_buffer);
+
+ /* Now let's read each character and see what we've got */
+ for(bufoffset = 0; bufoffset < dcc_resplen && line <= 2; bufoffset++)
+ {
+ /* First check if we reached the end of the line and
+ then increment the line counter */
+ if(big_buffer[bufoffset] == '\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(bufoffset == 0)
+ {
+ /* Now get the value and set the return value accordingly */
+ switch (big_buffer[bufoffset])
+ {
+ case 'A':
+ DEBUG(D_acl)
+ debug_printf("DCC: Overall result = A\treturning OK\n");
+ dcc_return_text = US"Mail accepted by DCC";
+ dcc_result = US"A";
+ retval = OK;
+ break;
+ case 'R':
+ DEBUG(D_acl)
+ debug_printf("DCC: Overall result = R\treturning FAIL\n");
+ dcc_return_text = US"Rejected by DCC";
+ 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);
+ break;
+ case 'S':
+ DEBUG(D_acl)
+ debug_printf("DCC: Overall result = S\treturning OK\n");
+ dcc_return_text = US"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;
+ break;
+ case 'G':
+ DEBUG(D_acl)
+ debug_printf("DCC: Overall result = G\treturning FAIL\n");
+ dcc_return_text = US"Greylisted by DCC";
+ dcc_result = US"G";
+ retval = FAIL;
+ break;
+ case 'T':
+ DEBUG(D_acl)
+ debug_printf("DCC: Overall result = T\treturning DEFER\n");
+ dcc_return_text = US"Temporary error with DCC";
+ dcc_result = US"T";
+ retval = DEFER;
+ log_write(0,LOG_MAIN,"Temporary error with DCC: %s\n", big_buffer);
+ break;
+ default:
+ DEBUG(D_acl)
+ debug_printf("DCC: Overall result = something else\treturning DEFER\n");
+ dcc_return_text = US"Unknown DCC response";
+ dcc_result = US"T";
+ retval = DEFER;
+ log_write(0,LOG_MAIN,"Unknown DCC response: %s\n", big_buffer);
+ break;
+ }
+ }
+ 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 bufoffset = %d != 0"
+ " character is %c - This is wrong!\n", line, bufoffset, big_buffer[bufoffset]);
+ log_write(0,LOG_MAIN,"Wrong header from DCC, output is %s\n", big_buffer);
+ }
+ }
+ 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. */
+ }
+ }
+ }
+ if(line > 2)
+ {
+ /* The third and following lines are the X-DCC header, so we store it in
+ dcc_header_str up to our limit. */
+ /* check if buffer contains the end of the header .."\n\n" and truncate it */
+ if ((big_buffer[dcc_resplen-1] == '\n') &&
+ (big_buffer[dcc_resplen-2] == '\n'))
+ dcc_resplen -= 2;
+ dcc_resplen -= bufoffset;
+ if (dcc_header_str->ptr + dcc_resplen > DCC_HEADER_LIMIT)
+ {
+ dcc_resplen = DCC_HEADER_LIMIT - dcc_header_str->ptr;
+ DEBUG(D_acl) debug_printf("DCC: We got more output than we can store"
+ "in the X-DCC header. Truncating at 120 characters.\n");
+ }
+ dcc_header_str = string_catn(dcc_header_str, &big_buffer[bufoffset], dcc_resplen);
+ }
+ }
+/* We have read everything from the socket. make sure the header ends with "\n" */
+dcc_header_str = string_catn(dcc_header_str, US"\n", 1);
+
+(void) string_from_gstring(dcc_header_str);
+/* Now let's sum up what we've got. */
+DEBUG(D_acl)
+ debug_printf("\nDCC: --------------------------\nDCC: Overall result = %d\n"
+ "DCC: X-DCC header: %sReturn message: %s\nDCC: dcc_result: %s\n",
+ retval, dcc_header_str->s, dcc_return_text, dcc_result);
+
+/* We only add the X-DCC header if it starts with X-DCC */
+if(!(Ustrncmp(dcc_header_str->s, "X-DCC", 5)))
+ {
+ dcc_header = dcc_header_str->s;
+ if(dcc_direct_add_header)
+ {
+ header_add(' ' , "%s", dcc_header_str->s);
+/* 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->ptr, dcc_header_str->s);
+
+/* 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'))
+ {
+ dcc_xtra_hdrs = string_cat(NULL, xtra_hdrs);
+ if (dcc_xtra_hdrs->s[dcc_xtra_hdrs->ptr - 1] != '\n')
+ dcc_xtra_hdrs = string_catn(dcc_xtra_hdrs, US"\n", 1);
+ header_add(' ', "%s", string_from_gstring(dcc_xtra_hdrs));
+ DEBUG(D_acl)
+ debug_printf("DCC: adding additional headers in $acl_m_dcc_add_header: %.*s", dcc_xtra_hdrs->ptr, dcc_xtra_hdrs->s);
+ }
+ }
+
+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\n"
+ "DCC: 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
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
index 0000000..26d09ea
--- /dev/null
+++ b/src/debug.c
@@ -0,0 +1,498 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2015 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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;
+
+static unsigned pretrigger_writeoff;
+static unsigned pretrigger_readoff;
+
+
+const uschar * rc_names[] = { /* Mostly for debug output */
+ [OK] = US"OK",
+ [DEFER] = US"DEFER",
+ [FAIL] = US"FAIL",
+ [ERROR] = US"ERROR",
+ [FAIL_FORCED] = US"FAIL_FORCED",
+ [DECLINE] = US"DECLINE",
+ [PASS] = US"PASS",
+ [DISCARD] = US"DISCARD",
+ [SKIP] = US"SKIP",
+ [REROUTED] = US"REROUTED",
+ [PANIC] = US"PANIC",
+ [BAD64] = US"BAD64",
+ [UNEXPECTED] = US"UNEXPECTED",
+ [CANCELLED] = US"CANCELLED",
+ [FAIL_SEND] = US"FAIL_SEND",
+ [FAIL_DROP] = US"FAIL_DROP",
+ [DANE] = US"DANE",
+};
+
+const uschar * dns_rc_names[] = {
+ [DNS_SUCCEED] = US"DNS_SUCCEED",
+ [DNS_NOMATCH] = US"DNS_NOMATCH",
+ [DNS_NODATA] = US"DNS_NODATA",
+ [DNS_AGAIN] = US"DNS_AGAIN",
+ [DNS_FAIL] = US"DNS_FAIL",
+};
+
+
+/*************************************************
+* 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 print
+ barswitch if TRUE print | at the pos value
+
+Returns: nothing
+*/
+
+static void
+tree_printsub(tree_node * p, int pos, int barswitch)
+{
+if (p->right) tree_printsub(p->right, pos+2, 1);
+for (int i = 0; i <= pos-1; i++) debug_printf_indent(" %c", tree_printline[i]);
+debug_printf_indent(" -->%s [%d]\n", p->name, p->balance);
+tree_printline[pos] = barswitch ? '|' : ' ';
+if (p->left)
+ {
+ tree_printline[pos+2] = '|';
+ tree_printsub(p->left, pos+2, 0);
+ }
+}
+
+/* The external function, with just a tree node argument. */
+
+void
+debug_print_tree(const char * title, tree_node * p)
+{
+debug_printf_indent("%s:\n", title);
+for (int i = 0; i < TREE_PRINTLINESIZE; i++) tree_printline[i] = ' ';
+if (!p) debug_printf_indent(" Empty Tree\n"); else tree_printsub(p, 0, 0);
+debug_printf_indent("---- End of tree ----\n");
+}
+
+
+
+/*************************************************
+* Print an argv vector *
+*************************************************/
+
+/* Called when about to obey execv().
+
+Argument: the argv vector
+Returns: nothing
+*/
+
+void
+debug_print_argv(const uschar ** argv)
+{
+debug_printf("exec");
+while (*argv) 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) return;
+HDEBUG(D_any|D_v)
+ {
+ uschar *s = expand_string(debug_string);
+ if (!s)
+ 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());
+}
+
+/************************************************/
+
+/* Give a string for a return-code */
+
+const uschar *
+rc_to_string(int rc)
+{
+return rc < 0 || rc >= nelem(rc_names) ? US"?" : rc_names[rc];
+}
+
+
+
+
+
+/*************************************************
+* Print debugging message *
+*************************************************/
+
+/* There are two entries, one for use when being called directly from a
+function with a variable argument list, one for prepending an indent.
+
+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.
+Take care to not disturb errno. */
+
+
+/* Debug printf indented by ACL nest depth */
+void
+debug_printf_indent(const char * format, ...)
+{
+va_list ap;
+va_start(ap, format);
+debug_vprintf(acl_level + expand_level, format, ap);
+va_end(ap);
+}
+
+void
+debug_printf(const char *format, ...)
+{
+va_list ap;
+va_start(ap, format);
+debug_vprintf(0, format, ap);
+va_end(ap);
+}
+
+void
+debug_vprintf(int indent, const char *format, va_list ap)
+{
+int save_errno = errno;
+
+if (!debug_file) 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)
+ {
+ struct timeval now;
+ time_t tmp;
+ struct tm * t;
+
+ gettimeofday(&now, NULL);
+ tmp = now.tv_sec;
+ t = f.timestamps_utc ? gmtime(&tmp) : localtime(&tmp);
+ debug_ptr += sprintf(CS debug_ptr,
+ LOGGING(millisec) ? "%02d:%02d:%02d.%03d " : "%02d:%02d:%02d ",
+ t->tm_hour, t->tm_min, t->tm_sec, (int)(now.tv_usec/1000));
+ }
+
+ DEBUG(D_pid)
+ debug_ptr += sprintf(CS debug_ptr, "%5d ", (int)getpid());
+
+ /* Set up prefix if outputting for host checking and not debugging */
+
+ if (host_checking && debug_selector == 0)
+ {
+ Ustrcpy(debug_ptr, US">>> ");
+ debug_ptr += 4;
+ }
+
+ debug_prefix_length = debug_ptr - debug_buffer;
+ }
+
+if (indent > 0)
+ {
+ for (int i = indent >> 2; i > 0; i--)
+ DEBUG(D_noutf8)
+ {
+ Ustrcpy(debug_ptr, US" !");
+ debug_ptr += 4; /* 3 spaces + shriek */
+ debug_prefix_length += 4;
+ }
+ else
+ {
+ Ustrcpy(debug_ptr, US" " UTF8_VERT_2DASH);
+ debug_ptr += 6; /* 3 spaces + 3 UTF-8 octets */
+ debug_prefix_length += 6;
+ }
+
+ Ustrncpy(debug_ptr, US" ", indent &= 3);
+ debug_ptr += indent;
+ debug_prefix_length += indent;
+ }
+
+/* Use the lengthchecked formatting routine to ensure that the buffer
+does not overflow. Ensure there's space for a newline at the end.
+However, use taint-unchecked routines for writing into the buffer
+so that we can write tainted info into the static debug_buffer -
+we trust that we will never expand the results. */
+
+ {
+ gstring gs = { .size = (int)sizeof(debug_buffer) - 1,
+ .ptr = debug_ptr - debug_buffer,
+ .s = debug_buffer };
+ if (!string_vformat(&gs, SVFMT_TAINT_NOCHK, format, ap))
+ {
+ uschar * s = US"**** debug string too long - truncated ****\n";
+ uschar * p = gs.s + gs.ptr;
+ int maxlen = gs.size - Ustrlen(s) - 2;
+ if (p > gs.s + maxlen) p = gs.s + maxlen;
+ if (p > gs.s && p[-1] != '\n') *p++ = '\n';
+ Ustrcpy(p, s);
+ while(*debug_ptr) debug_ptr++;
+ }
+ else
+ {
+ string_from_gstring(&gs);
+ debug_ptr = gs.s + gs.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;
+ }
+ }
+
+ if (debug_pretrigger_buf)
+ {
+ int needed = Ustrlen(debug_buffer)+1, avail;
+ char c;
+
+ if (needed > debug_pretrigger_bsize)
+ needed = debug_pretrigger_bsize;
+ if ((avail = pretrigger_readoff - pretrigger_writeoff) <= 0)
+ avail += debug_pretrigger_bsize;
+
+ /* We have a pretrigger set up, trigger not yet hit. Copy the line(s) to the
+ pretrig buffer, dropping earlier lines if needed but truncating this line if
+ the pbuf is maxed out. In the PTB the lines are NOT nul-terminated. */
+
+ while (avail < needed)
+ do
+ {
+ avail++;
+ c = debug_pretrigger_buf[pretrigger_readoff];
+ if (++pretrigger_readoff >= debug_pretrigger_bsize) pretrigger_readoff = 0;
+ }
+ while (c && c != '\n' && pretrigger_readoff != pretrigger_writeoff);
+
+ needed--;
+ for (int i = 0; needed; i++, needed--)
+ {
+ debug_pretrigger_buf[pretrigger_writeoff] = debug_buffer[i];
+ if (++pretrigger_writeoff >= debug_pretrigger_bsize) pretrigger_writeoff = 0;
+ }
+ }
+ else
+ {
+ fprintf(debug_file, "%s", CS debug_buffer);
+ fflush(debug_file);
+ }
+ debug_ptr = debug_buffer;
+ debug_prefix_length = 0;
+ }
+errno = save_errno;
+}
+
+
+
+/* Output the details of a socket */
+
+void
+debug_print_socket(int fd)
+{
+struct stat s;
+if (fstat(fd, &s) == 0 && (s.st_mode & S_IFMT) == S_IFSOCK)
+ {
+ gstring * g = NULL;
+ int val;
+ socklen_t vlen = sizeof(val);
+ struct sockaddr_storage a;
+ socklen_t alen = sizeof(a);
+ struct sockaddr_in * sinp = (struct sockaddr_in *)&a;
+ struct sockaddr_in6 * sin6p = (struct sockaddr_in6 *)&a;
+ struct sockaddr_un * sunp = (struct sockaddr_un *)&a;
+
+ if (getsockname(fd, (struct sockaddr*)&a, &alen) == 0)
+ switch (a.ss_family)
+ {
+ case AF_INET:
+ g = string_cat(g, US"domain AF_INET");
+ g = string_fmt_append(g, " lcl [%s]:%u",
+ inet_ntoa(sinp->sin_addr), ntohs(sinp->sin_port));
+ alen = sizeof(*sinp);
+ if (getpeername(fd, (struct sockaddr *)sinp, &alen) == 0)
+ g = string_fmt_append(g, " rmt [%s]:%u",
+ inet_ntoa(sinp->sin_addr), ntohs(sinp->sin_port));
+ break;
+ case AF_INET6:
+ {
+ uschar buf[46];
+ g = string_cat(g, US"domain AF_INET6");
+ g = string_fmt_append(g, " lcl [%s]:%u",
+ inet_ntop(AF_INET6, &sin6p->sin6_addr, CS buf, sizeof(buf)),
+ ntohs(sin6p->sin6_port));
+ alen = sizeof(*sin6p);
+ if (getpeername(fd, (struct sockaddr *)sin6p, &alen) == 0)
+ g = string_fmt_append(g, " rmt [%s]:%u",
+ inet_ntop(AF_INET6, &sin6p->sin6_addr, CS buf, sizeof(buf)),
+ ntohs(sin6p->sin6_port));
+ break;
+ }
+ case AF_UNIX:
+ g = string_cat(g, US"domain AF_UNIX");
+ if (alen > sizeof(sa_family_t)) /* not unix(7) "unnamed socket" */
+ g = string_fmt_append(g, " lcl %s%s",
+ sunp->sun_path[0] ? US"" : US"@",
+ sunp->sun_path[0] ? sunp->sun_path : sunp->sun_path+1);
+ alen = sizeof(*sunp);
+ if (getpeername(fd, (struct sockaddr *)sunp, &alen) == 0)
+ g = string_fmt_append(g, " rmt %s%s",
+ sunp->sun_path[0] ? US"" : US"@",
+ sunp->sun_path[0] ? sunp->sun_path : sunp->sun_path+1);
+ break;
+ default:
+ g = string_fmt_append(g, "domain %u", sinp->sin_family);
+ break;
+ }
+ if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &val, &vlen) == 0)
+ switch (val)
+ {
+ case SOCK_STREAM: g = string_cat(g, US" type SOCK_STREAM"); break;
+ case SOCK_DGRAM: g = string_cat(g, US" type SOCK_DGRAM"); break;
+ default: g = string_fmt_append(g, " type %d", val); break;
+ }
+#ifdef SO_PROTOCOL
+ if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &val, &vlen) == 0)
+ {
+ struct protoent * p = getprotobynumber(val);
+ g = p
+ ? string_fmt_append(g, " proto %s", p->p_name)
+ : string_fmt_append(g, " proto %d", val);
+ }
+#endif
+ debug_printf_indent(" socket: %s\n", string_from_gstring(g));
+ }
+else
+ debug_printf_indent(" fd st_mode 0%o\n", s.st_mode);
+}
+
+
+/**************************************************************/
+/* Pretrigger handling for debug. The debug_printf implementation
+diverts output to a circular buffer if the buffer is set up.
+The routines here set up the buffer, and unload it to file (and release it).
+What ends up in the buffer is subject to the usual debug_selector. */
+
+void
+debug_pretrigger_setup(const uschar * size_string)
+{
+long size = Ustrtol(size_string, NULL, 0);
+if (size > 0)
+ {
+ unsigned bufsize = MIN(size, 16384);
+
+ dtrigger_selector |= BIT(DTi_pretrigger);
+ if (debug_pretrigger_buf) store_free(debug_pretrigger_buf);
+ debug_pretrigger_buf = store_malloc((size_t)(debug_pretrigger_bsize = bufsize));
+ pretrigger_readoff = pretrigger_writeoff = 0;
+ }
+}
+
+void
+debug_trigger_fire(void)
+{
+int nbytes;
+
+if (!debug_pretrigger_buf) return;
+
+if (debug_file && (nbytes = pretrigger_writeoff - pretrigger_readoff) != 0)
+ if (nbytes > 0)
+ fwrite(debug_pretrigger_buf + pretrigger_readoff, 1, nbytes, debug_file);
+ else
+ {
+ fwrite(debug_pretrigger_buf + pretrigger_readoff, 1,
+ debug_pretrigger_bsize - pretrigger_readoff, debug_file);
+ fwrite(debug_pretrigger_buf, 1, pretrigger_writeoff, debug_file);
+ }
+
+debug_pretrigger_discard();
+}
+
+void
+debug_pretrigger_discard(void)
+{
+if (debug_pretrigger_buf) store_free(debug_pretrigger_buf);
+debug_pretrigger_buf = NULL;
+dtrigger_selector = 0;
+}
+
+
+/* End of debug.c */
diff --git a/src/deliver.c b/src/deliver.c
new file mode 100644
index 0000000..8a9a174
--- /dev/null
+++ b/src/deliver.c
@@ -0,0 +1,8642 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* The main code for delivering a message. */
+
+
+#include "exim.h"
+#include "transports/smtp.h"
+#include <sys/uio.h>
+#include <assert.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;
+static address_item *addr_senddsn = NULL;
+
+static FILE *message_log = NULL;
+static BOOL update_spool;
+static BOOL remove_journal;
+static int parcount = 0;
+static pardata *parlist = NULL;
+static struct pollfd *parpoll;
+static int return_count;
+static uschar *frozen_info = US"";
+static uschar *used_return_path = NULL;
+
+
+
+/*************************************************
+* read as much as requested *
+*************************************************/
+
+/* The syscall read(2) doesn't always returns as much as we want. For
+several reasons it might get less. (Not talking about signals, as syscalls
+are restartable). When reading from a network or pipe connection the sender
+might send in smaller chunks, with delays between these chunks. The read(2)
+may return such a chunk.
+
+The more the writer writes and the smaller the pipe between write and read is,
+the more we get the chance of reading leass than requested. (See bug 2130)
+
+This function read(2)s until we got all the data we *requested*.
+
+Note: This function may block. Use it only if you're sure about the
+amount of data you will get.
+
+Argument:
+ fd the file descriptor to read from
+ buffer pointer to a buffer of size len
+ len the requested(!) amount of bytes
+
+Returns: the amount of bytes read
+*/
+static ssize_t
+readn(int fd, void * buffer, size_t len)
+{
+uschar * next = buffer;
+uschar * end = next + len;
+
+while (next < end)
+ {
+ ssize_t got = read(fd, next, end - next);
+
+ /* I'm not sure if there are signals that can interrupt us,
+ for now I assume the worst */
+ if (got == -1 && errno == EINTR) continue;
+ if (got <= 0) return next - US buffer;
+ next += got;
+ }
+
+return len;
+}
+
+
+/*************************************************
+* 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), GET_UNTAINTED);
+*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)
+ {
+ const uschar ***p = address_expansions;
+ while (*p) **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)
+ {
+ deliver_host = deliver_host_address = US"";
+ deliver_host_port = 0;
+ }
+else
+ {
+ deliver_host = addr->host_list->name;
+ deliver_host_address = addr->host_list->address;
+ deliver_host_port = addr->host_list->port;
+ }
+
+deliver_recipients = addr;
+deliver_address_data = addr->prop.address_data;
+deliver_domain_data = addr->prop.domain_data;
+deliver_localpart_data = addr->prop.localpart_data;
+router_var = addr->prop.variables;
+
+/* 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)
+ {
+ address_item *addr_orig;
+
+ deliver_localpart = addr->local_part;
+ deliver_localpart_prefix = addr->prefix;
+ deliver_localpart_prefix_v = addr->prefix_v;
+ deliver_localpart_suffix = addr->suffix;
+ deliver_localpart_suffix_v = addr->suffix_v;
+
+ for (addr_orig = addr; addr_orig->parent; 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
+ ? 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)
+ {
+ deliver_domain_parent = addr->parent->domain;
+ deliver_localpart_parent = !addr->parent->router
+ ? 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_prefix_v = addr->parent->prefix_v;
+ deliver_localpart_suffix = addr->parent->suffix;
+ deliver_localpart_suffix_v = addr->parent->suffix_v;
+ }
+ }
+
+#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
+ {
+ 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 (address_item * addr2 = addr->next; addr2; addr2 = addr2->next)
+ {
+ if (deliver_domain && Ustrcmp(deliver_domain, addr2->domain) != 0)
+ deliver_domain = NULL;
+ if ( self_hostname
+ && ( !addr2->self_hostname
+ || Ustrcmp(self_hostname, addr2->self_hostname) != 0
+ ) )
+ self_hostname = NULL;
+ if (!deliver_domain && !self_hostname) 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.
+
+Called from deliver_message(), can be operating as root.
+
+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)
+{
+if (Ustrstr(filename, US"/../"))
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "Attempt to open msglog file path with upward-traversal: '%s'\n", filename);
+
+for (int i = 2; i > 0; i--)
+ {
+ int fd = Uopen(filename,
+#ifdef O_CLOEXEC
+ O_CLOEXEC |
+#endif
+#ifdef O_NOFOLLOW
+ O_NOFOLLOW |
+#endif
+ O_WRONLY|O_APPEND|O_CREAT, mode);
+ if (fd >= 0)
+ {
+ /* 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. */
+
+#ifndef O_CLOEXEC
+ (void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+#endif
+ if (exim_fchown(fd, exim_uid, exim_gid, filename) < 0)
+ {
+ *error = US"chown";
+ return -1;
+ }
+ if (fchmod(fd, mode) < 0)
+ {
+ *error = US"chmod";
+ return -1;
+ }
+ return fd;
+ }
+ if (errno != ENOENT)
+ break;
+
+ (void)directory_make(spool_directory,
+ spool_sname(US"msglog", message_subdir),
+ MSGLOG_DIRECTORY_MODE, TRUE);
+ }
+
+*error = US"create or open";
+return -1;
+}
+
+
+
+
+/*************************************************
+* 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)
+{
+for (address_item * addr2 = addr->next; addr2; 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->delivery_time = addr->delivery_time;
+ 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.
+
+We do not try to batch up different A-record host names that refer to the
+same IP.
+
+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 && two)
+ {
+ 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 && end_one->next->mx == mx
+ && end_two->next && 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;
+ }
+
+ /* if the names matched but ports do not, mismatch */
+ else if (one->port != two->port)
+ return FALSE;
+
+#ifdef SUPPORT_DANE
+ /* DNSSEC equality */
+ if (one->dnssec != two->dnssec) return FALSE;
+#endif
+
+ /* 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 (;; one = one->next, two = two->next)
+ {
+ if (one == two) return TRUE; /* Includes the case where both NULL */
+ if (!one || !two) return FALSE;
+ if (Ustrcmp(one->text, two->text) != 0) return FALSE;
+ }
+}
+
+
+
+/*************************************************
+* 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 || !two) 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
+ && !tp->deliver_as_creator
+ && ( 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
+ && ( 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)
+{
+update_spool = TRUE; /* Ensure spool gets updated */
+
+/* Top-level address */
+
+if (!addr->parent)
+ {
+ tree_add_nonrecipient(addr->unique);
+ tree_add_nonrecipient(addr->address);
+ }
+
+/* Homonymous child address */
+
+else if (testflag(addr, af_homonym))
+ {
+ if (addr->transport)
+ 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 (address_item * dup = addr_duplicate; dup; 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)
+{
+while (addr->parent)
+ {
+ address_item *aa;
+
+ addr = addr->parent;
+ if (--addr->child_count > 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; aa = aa->parent)
+ if (Ustrcmp(aa->address, addr->address) == 0) break;
+ if (aa) continue;
+
+ deliver_msglog("%s %s: children all complete\n", now, addr->address);
+ DEBUG(D_deliver) debug_printf("%s: children all complete\n", addr->address);
+ }
+}
+
+
+
+/*************************************************
+* Delivery logging support functions *
+*************************************************/
+
+/* The LOGGING() checks in d_log_interface() are complicated for backwards
+compatibility. When outgoing interface logging was originally added, it was
+conditional on just incoming_interface (which is off by default). The
+outgoing_interface option is on by default to preserve this behaviour, but
+you can enable incoming_interface and disable outgoing_interface to get I=
+fields on incoming lines only.
+
+Arguments:
+ g The log line
+ addr The address to be logged
+
+Returns: New value for s
+*/
+
+static gstring *
+d_log_interface(gstring * g)
+{
+if (LOGGING(incoming_interface) && LOGGING(outgoing_interface)
+ && sending_ip_address)
+ {
+ g = string_fmt_append(g, " I=[%s]", sending_ip_address);
+ if (LOGGING(outgoing_port))
+ g = string_fmt_append(g, ":%d", sending_port);
+ }
+return g;
+}
+
+
+
+static gstring *
+d_hostlog(gstring * g, address_item * addr)
+{
+host_item * h = addr->host_used;
+
+g = string_append(g, 2, US" H=", h->name);
+
+if (LOGGING(dnssec) && h->dnssec == DS_YES)
+ g = string_catn(g, US" DS", 3);
+
+g = string_append(g, 3, US" [", h->address, US"]");
+
+if (LOGGING(outgoing_port))
+ g = string_fmt_append(g, ":%d", h->port);
+
+if (continue_sequence > 1) /*XXX this is wrong for a dropped proxyconn. Would have to pass back from transport */
+ g = string_catn(g, US"*", 1);
+
+#ifdef SUPPORT_SOCKS
+if (LOGGING(proxy) && proxy_local_address)
+ {
+ g = string_append(g, 3, US" PRX=[", proxy_local_address, US"]");
+ if (LOGGING(outgoing_port))
+ g = string_fmt_append(g, ":%d", proxy_local_port);
+ }
+#endif
+
+g = d_log_interface(g);
+
+if (testflag(addr, af_tcp_fastopen))
+ g = string_catn(g, US" TFO*", testflag(addr, af_tcp_fastopen_data) ? 5 : 4);
+
+return g;
+}
+
+
+
+
+
+#ifndef DISABLE_TLS
+static gstring *
+d_tlslog(gstring * g, address_item * addr)
+{
+if (LOGGING(tls_cipher) && addr->cipher)
+ {
+ g = string_append(g, 2, US" X=", addr->cipher);
+#ifndef DISABLE_TLS_RESUME
+ if (LOGGING(tls_resumption) && testflag(addr, af_tls_resume))
+ g = string_catn(g, US"*", 1);
+#endif
+ }
+if (LOGGING(tls_certificate_verified) && addr->cipher)
+ g = string_append(g, 2, US" CV=",
+ testflag(addr, af_cert_verified)
+ ?
+#ifdef SUPPORT_DANE
+ testflag(addr, af_dane_verified)
+ ? "dane"
+ :
+#endif
+ "yes"
+ : "no");
+if (LOGGING(tls_peerdn) && addr->peerdn)
+ g = string_append(g, 3, US" DN=\"", string_printing(addr->peerdn), US"\"");
+return g;
+}
+#endif
+
+
+
+
+#ifndef DISABLE_EVENT
+/* Distribute a named event to any listeners.
+
+Args: action config option specifying listener
+ event name of the event
+ ev_data associated data for the event
+ errnop pointer to errno for modification, or null
+
+Return: string expansion from listener, or NULL
+*/
+
+uschar *
+event_raise(uschar * action, const uschar * event, uschar * ev_data, int * errnop)
+{
+uschar * s;
+if (action)
+ {
+ DEBUG(D_deliver)
+ debug_printf("Event(%s): event_action=|%s| delivery_IP=%s\n",
+ event,
+ action, deliver_host_address);
+
+ event_name = event;
+ event_data = ev_data;
+
+ if (!(s = expand_string(action)) && *expand_string_message)
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "failed to expand event_action %s in %s: %s\n",
+ event, transport_name ? transport_name : US"main", expand_string_message);
+
+ event_name = event_data = NULL;
+
+ /* If the expansion returns anything but an empty string, flag for
+ the caller to modify his normal processing
+ */
+ if (s && *s)
+ {
+ DEBUG(D_deliver)
+ debug_printf("Event(%s): event_action returned \"%s\"\n", event, s);
+ if (errnop)
+ *errnop = ERRNO_EVENT;
+ return s;
+ }
+ }
+return NULL;
+}
+
+void
+msg_event_raise(const uschar * event, const address_item * addr)
+{
+const uschar * save_domain = deliver_domain;
+uschar * save_local = deliver_localpart;
+const uschar * save_host = deliver_host;
+const uschar * save_address = deliver_host_address;
+const int save_port = deliver_host_port;
+
+router_name = addr->router ? addr->router->name : NULL;
+deliver_domain = addr->domain;
+deliver_localpart = addr->local_part;
+deliver_host = addr->host_used ? addr->host_used->name : NULL;
+
+if (!addr->transport)
+ {
+ if (Ustrcmp(event, "msg:fail:delivery") == 0)
+ {
+ /* An address failed with no transport involved. This happens when
+ a filter was used which triggered a fail command (in such a case
+ a transport isn't needed). Convert it to an internal fail event. */
+
+ (void) event_raise(event_action, US"msg:fail:internal", addr->message, NULL);
+ }
+ }
+else
+ {
+ transport_name = addr->transport->name;
+
+ (void) event_raise(addr->transport->event_action, event,
+ addr->host_used
+ || Ustrcmp(addr->transport->driver_name, "smtp") == 0
+ || Ustrcmp(addr->transport->driver_name, "lmtp") == 0
+ || Ustrcmp(addr->transport->driver_name, "autoreply") == 0
+ ? addr->message : NULL,
+ NULL);
+ }
+
+deliver_host_port = save_port;
+deliver_host_address = save_address;
+deliver_host = save_host;
+deliver_localpart = save_local;
+deliver_domain = save_domain;
+router_name = transport_name = NULL;
+}
+#endif /*DISABLE_EVENT*/
+
+
+
+/******************************************************************************/
+
+
+/*************************************************
+* Generate local part for logging *
+*************************************************/
+
+static uschar *
+string_get_lpart_sub(const address_item * addr, uschar * s)
+{
+#ifdef SUPPORT_I18N
+if (testflag(addr, af_utf8_downcvt))
+ {
+ uschar * t = string_localpart_utf8_to_alabel(s, NULL);
+ return t ? t : s; /* t is NULL on a failed conversion */
+ }
+#endif
+return s;
+}
+
+/* This function is a subroutine for use in string_log_address() below.
+
+Arguments:
+ addr the address being logged
+ yield the current dynamic buffer pointer
+
+Returns: the new value of the buffer pointer
+*/
+
+static gstring *
+string_get_localpart(address_item * addr, gstring * yield)
+{
+uschar * s;
+
+if (testflag(addr, af_include_affixes) && (s = addr->prefix))
+ yield = string_cat(yield, string_get_lpart_sub(addr, s));
+
+yield = string_cat(yield, string_get_lpart_sub(addr, addr->local_part));
+
+if (testflag(addr, af_include_affixes) && (s = addr->suffix))
+ yield = string_cat(yield, string_get_lpart_sub(addr, s));
+
+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:
+ g points to growing-string struct
+ addr bottom (ultimate) address
+ all_parents if TRUE, include all parents
+ success TRUE for successful delivery
+
+Returns: a growable string in dynamic store
+*/
+
+static gstring *
+string_log_address(gstring * g,
+ address_item *addr, BOOL all_parents, BOOL success)
+{
+BOOL add_topaddr = TRUE;
+address_item *topaddr;
+
+/* Find the ultimate parent */
+
+for (topaddr = addr; topaddr->parent; 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 going 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 && addr->router->log_as_local
+ && addr->transport && addr->transport->info->local
+ ) )
+ {
+ if (testflag(addr, af_file) && addr->local_part[0] != '/')
+ g = string_catn(g, CUS"save ", 5);
+ g = string_get_localpart(addr, g);
+ }
+
+/* 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
+ {
+ uschar * cmp;
+ int off = g->ptr; /* start of the "full address" */
+
+ if (addr->local_part)
+ {
+ const uschar * s;
+ g = string_get_localpart(addr, g);
+ g = string_catn(g, US"@", 1);
+ s = addr->domain;
+#ifdef SUPPORT_I18N
+ if (testflag(addr, af_utf8_downcvt))
+ s = string_localpart_utf8_to_alabel(s, NULL);
+#endif
+ g = string_cat(g, s);
+ }
+ else
+ g = string_cat(g, addr->address);
+
+ /* 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. */
+
+ cmp = g->s + off; /* only now, as rebuffer likely done */
+ string_from_gstring(g); /* ensure nul-terminated */
+ if ( strcmpic(cmp, topaddr->address) == 0
+ && Ustrncmp(cmp, topaddr->address, Ustrchr(cmp, '@') - cmp) == 0
+ && !addr->onetime_parent
+ && (!all_parents || !addr->parent || 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
+ && addr->parent != topaddr)
+ {
+ uschar *s = US" (";
+ for (address_item * addr2 = addr->parent; addr2 != topaddr; addr2 = addr2->parent)
+ {
+ g = string_catn(g, s, 2);
+ g = string_cat (g, addr2->address);
+ if (!all_parents) break;
+ s = US", ";
+ }
+ g = string_catn(g, US")", 1);
+ }
+
+/* Add the top address if it is required */
+
+if (add_topaddr)
+ g = string_append(g, 3,
+ US" <",
+ addr->onetime_parent ? addr->onetime_parent : topaddr->address,
+ US">");
+
+return g;
+}
+
+
+
+/******************************************************************************/
+
+
+
+/* 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)
+{
+gstring * g; /* Used for a temporary, expanding buffer, for building log lines */
+rmark reset_point;
+
+/* 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. */
+
+#ifndef DISABLE_EVENT
+ /* presume no successful remote delivery */
+ lookup_dnssec_authenticated = NULL;
+#endif
+
+reset_point = store_mark();
+g = string_get_tainted(256, GET_TAINTED); /* addrs will be tainted, so avoid copy */
+
+if (msg)
+ g = string_append(g, 2, host_and_ident(TRUE), US" ");
+else
+ {
+ g->s[0] = logchar; g->ptr = 1;
+ g = string_catn(g, US"> ", 2);
+ }
+g = string_log_address(g, addr, LOGGING(all_parents), TRUE);
+
+if (LOGGING(sender_on_delivery) || msg)
+ g = string_append(g, 3, US" F=<",
+#ifdef SUPPORT_I18N
+ testflag(addr, af_utf8_downcvt)
+ ? string_address_utf8_to_alabel(sender_address, NULL)
+ :
+#endif
+ sender_address,
+ US">");
+
+if (*queue_name)
+ g = string_append(g, 2, US" Q=", queue_name);
+
+/* 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 && LOGGING(return_path_on_delivery))
+ g = string_append(g, 3, US" P=<", used_return_path, US">");
+
+if (msg)
+ g = string_append(g, 2, US" ", msg);
+
+/* For a delivery from a system filter, there may not be a router */
+if (addr->router)
+ g = string_append(g, 2, US" R=", addr->router->name);
+
+g = string_append(g, 2, US" T=", addr->transport->name);
+
+if (LOGGING(delivery_size))
+ g = string_fmt_append(g, " S=%d", transport_count);
+
+/* Local delivery */
+
+if (addr->transport->info->local)
+ {
+ if (addr->host_list)
+ g = string_append(g, 2, US" H=", addr->host_list->name);
+ g = d_log_interface(g);
+ if (addr->shadow_message)
+ g = string_cat(g, addr->shadow_message);
+ }
+
+/* Remote delivery */
+
+else
+ {
+ if (addr->host_used)
+ {
+ g = d_hostlog(g, addr);
+
+#ifndef DISABLE_EVENT
+ deliver_host_address = addr->host_used->address;
+ deliver_host_port = addr->host_used->port;
+ deliver_host = addr->host_used->name;
+
+ /* DNS lookup status */
+ lookup_dnssec_authenticated = addr->host_used->dnssec==DS_YES ? US"yes"
+ : addr->host_used->dnssec==DS_NO ? US"no"
+ : NULL;
+#endif
+ }
+
+#ifndef DISABLE_TLS
+ g = d_tlslog(g, addr);
+#endif
+
+ if (addr->authenticator)
+ {
+ g = string_append(g, 2, US" A=", addr->authenticator);
+ if (addr->auth_id)
+ {
+ g = string_append(g, 2, US":", addr->auth_id);
+ if (LOGGING(smtp_mailauth) && addr->auth_sndr)
+ g = string_append(g, 2, US":", addr->auth_sndr);
+ }
+ }
+
+ if (LOGGING(pipelining))
+ {
+ if (testflag(addr, af_pipelining))
+ g = string_catn(g, US" L", 2);
+#ifndef DISABLE_PIPE_CONNECT
+ if (testflag(addr, af_early_pipe))
+ g = string_catn(g, US"*", 1);
+#endif
+ }
+
+#ifndef DISABLE_PRDR
+ if (testflag(addr, af_prdr_used))
+ g = string_catn(g, US" PRDR", 5);
+#endif
+
+ if (testflag(addr, af_chunking_used))
+ g = string_catn(g, US" K", 2);
+ }
+
+/* confirmation message (SMTP (host_used) and LMTP (driver_name)) */
+
+if ( LOGGING(smtp_confirmation)
+ && addr->message
+ && (addr->host_used || Ustrcmp(addr->transport->driver_name, "lmtp") == 0)
+ )
+ {
+ unsigned lim = big_buffer_size < 1024 ? big_buffer_size : 1024;
+ uschar *p = big_buffer;
+ uschar *ss = addr->message;
+ *p++ = '\"';
+ for (int i = 0; i < lim && ss[i] != 0; i++) /* limit logged amount */
+ {
+ if (ss[i] == '\"' || ss[i] == '\\') *p++ = '\\'; /* quote \ and " */
+ *p++ = ss[i];
+ }
+ *p++ = '\"';
+ *p = 0;
+ g = string_append(g, 2, US" C=", big_buffer);
+ }
+
+/* Time on queue and actual time taken to deliver */
+
+if (LOGGING(queue_time))
+ g = string_append(g, 2, US" QT=", string_timesince(
+ LOGGING(queue_time_exclusive) ? &received_time_complete : &received_time));
+
+if (LOGGING(deliver_time))
+ g = string_append(g, 2, US" DT=", string_timediff(&addr->delivery_time));
+
+/* string_cat() always leaves room for the terminator. Release the
+store we used to build the line after writing it. */
+
+log_write(0, flags, "%s", string_from_gstring(g));
+
+#ifndef DISABLE_EVENT
+if (!msg) msg_event_raise(US"msg:delivery", addr);
+#endif
+
+store_reset(reset_point);
+return;
+}
+
+
+
+static void
+deferral_log(address_item * addr, uschar * now,
+ int logflags, uschar * driver_name, uschar * driver_kind)
+{
+rmark reset_point = store_mark();
+gstring * g = string_get(256);
+
+/* Build up the line that is used for both the message log and the main
+log. */
+
+/* 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. */
+
+g = string_log_address(g, addr, LOGGING(all_parents), FALSE);
+
+if (*queue_name)
+ g = string_append(g, 2, US" Q=", queue_name);
+
+/* 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)
+ {
+ if (driver_kind[1] == 't' && addr->router)
+ g = string_append(g, 2, US" R=", addr->router->name);
+ g = string_fmt_append(g, " %c=%s", toupper(driver_kind[1]), driver_name);
+ }
+else if (driver_kind)
+ g = string_append(g, 2, US" ", driver_kind);
+
+g = string_fmt_append(g, " defer (%d)", addr->basic_errno);
+
+if (addr->basic_errno > 0)
+ g = string_append(g, 2, US": ", US strerror(addr->basic_errno));
+
+if (addr->host_used)
+ g = d_hostlog(g, addr);
+
+if (LOGGING(deliver_time))
+ g = string_append(g, 2, US" DT=", string_timediff(&addr->delivery_time));
+
+if (addr->message)
+ g = string_append(g, 2, US": ", addr->message);
+
+(void) string_from_gstring(g);
+
+/* Log the deferment in the message log, but don't clutter it
+up with retry-time defers after the first delivery attempt. */
+
+if (f.deliver_firsttime || addr->basic_errno > ERRNO_RETRY_BASE)
+ deliver_msglog("%s %s\n", now, g->s);
+
+/* Write the main log and reset the store.
+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. */
+
+
+log_write(addr->basic_errno <= ERRNO_RETRY_BASE ? L_retry_defer : 0, logflags,
+ "== %s", g->s);
+
+store_reset(reset_point);
+return;
+}
+
+
+
+static void
+failure_log(address_item * addr, uschar * driver_kind, uschar * now)
+{
+rmark reset_point = store_mark();
+gstring * g = string_get(256);
+
+#ifndef DISABLE_EVENT
+/* Message failures for which we will send a DSN get their event raised
+later so avoid doing it here. */
+
+if ( !addr->prop.ignore_error
+ && !(addr->dsn_flags & (rf_dsnflags & ~rf_notify_failure))
+ )
+ msg_event_raise(US"msg:fail:delivery", addr);
+#endif
+
+/* Build up the log line for the message and main logs */
+
+/* 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. */
+
+g = string_log_address(g, addr, LOGGING(all_parents), FALSE);
+
+if (LOGGING(sender_on_delivery))
+ g = string_append(g, 3, US" F=<", sender_address, US">");
+
+if (*queue_name)
+ g = string_append(g, 2, US" Q=", queue_name);
+
+/* Return path may not be set if no delivery actually happened */
+
+if (used_return_path && LOGGING(return_path_on_delivery))
+ g = string_append(g, 3, US" P=<", used_return_path, US">");
+
+if (addr->router)
+ g = string_append(g, 2, US" R=", addr->router->name);
+if (addr->transport)
+ g = string_append(g, 2, US" T=", addr->transport->name);
+
+if (addr->host_used)
+ g = d_hostlog(g, addr);
+
+#ifndef DISABLE_TLS
+g = d_tlslog(g, addr);
+#endif
+
+if (addr->basic_errno > 0)
+ g = string_append(g, 2, US": ", US strerror(addr->basic_errno));
+
+if (addr->message)
+ g = string_append(g, 2, US": ", addr->message);
+
+if (LOGGING(deliver_time))
+ g = string_append(g, 2, US" DT=", string_timediff(&addr->delivery_time));
+
+(void) string_from_gstring(g);
+
+/* Do the logging. For the message log, "routing failed" for those cases,
+just to make it clearer. */
+
+if (driver_kind)
+ deliver_msglog("%s %s failed for %s\n", now, driver_kind, g->s);
+else
+ deliver_msglog("%s %s\n", now, g->s);
+
+log_write(0, LOG_MAIN, "** %s", g->s);
+
+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;
+
+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 == EXIM_DTYPE_TRANSPORT)
+ {
+ if (addr->transport)
+ {
+ driver_name = addr->transport->name;
+ driver_kind = US" transport";
+ f.disable_logging = addr->transport->disable_logging;
+ }
+ else driver_kind = US"transporting";
+ }
+else if (driver_type == EXIM_DTYPE_ROUTER)
+ {
+ if (addr->router)
+ {
+ driver_name = addr->router->name;
+ driver_kind = US" router";
+ f.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)
+ {
+ const uschar * s = string_printing(addr->message);
+
+ /* deconst cast ok as string_printing known to have alloc'n'copied */
+ addr->message = expand_hide_passwords(US s);
+ }
+
+/* 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)
+ {
+ 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)
+ 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
+ if ((s = US Ufgets(big_buffer, big_buffer_size, f)))
+ {
+ uschar *p = big_buffer + Ustrlen(big_buffer);
+ const uschar * sp;
+ while (p > big_buffer && isspace(p[-1])) p--;
+ *p = 0;
+ sp = string_printing(big_buffer);
+ log_write(0, LOG_MAIN, "<%s>: %s transport output: %s",
+ addr->address, tb->name, sp);
+ }
+ (void)fclose(f);
+ }
+
+ /* Handle returning options, but only if there is an address to return
+ the text to. */
+
+ if (sender_address[0] != 0 || addr->prop.errors_address)
+ if (tb->return_output)
+ {
+ addr->transport_return = result = FAIL;
+ if (addr->basic_errno == 0 && !addr->message)
+ 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);
+ }
+
+/* Check if the transport notifed continue-conn status explicitly, and
+update our knowlege. */
+
+if (testflag(addr, af_new_conn)) continue_sequence = 1;
+else if (testflag(addr, af_cont_conn)) continue_sequence++;
+
+/* 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)
+ 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 events) */
+#ifndef DISABLE_TLS
+ tls_out.ourcert = addr->ourcert;
+ addr->ourcert = NULL;
+ tls_out.peercert = addr->peercert;
+ addr->peercert = NULL;
+
+ tls_out.ver = addr->tlsver;
+ tls_out.cipher = addr->cipher;
+ tls_out.peerdn = addr->peerdn;
+ tls_out.ocsp = addr->ocsp;
+# ifdef SUPPORT_DANE
+ tls_out.dane_verified = testflag(addr, af_dane_verified);
+# endif
+#endif
+
+ delivery_log(LOG_MAIN, addr, logchar, NULL);
+
+#ifndef DISABLE_TLS
+ tls_free_cert(&tls_out.ourcert);
+ tls_free_cert(&tls_out.peercert);
+ tls_out.ver = NULL;
+ tls_out.cipher = NULL;
+ tls_out.peerdn = NULL;
+ tls_out.ocsp = OCSP_NOT_REQ;
+# ifdef SUPPORT_DANE
+ tls_out.dane_verified = FALSE;
+# endif
+#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)
+ {
+ f.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 (!f.queue_2stage || addr->basic_errno != 0)
+ deferral_log(addr, now, logflags, driver_name, driver_kind);
+ }
+
+
+/* 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 && message_age >= ignore_bounce_errors_after)
+ addr->prop.ignore_error = TRUE;
+
+ /* 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 ( !addr->prop.ignore_error
+ && ( addr->special_action == SPECIAL_FREEZE
+ || (sender_address[0] == 0 && !addr->prop.errors_address)
+ ) )
+ {
+ frozen_info = addr->special_action == SPECIAL_FREEZE
+ ? US""
+ : f.sender_local && !f.local_error_message
+ ? US" (message created with -f <>)"
+ : US" (delivery error message)";
+ f.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;
+ }
+
+ failure_log(addr, driver_name ? NULL : driver_kind, now);
+ }
+
+/* Ensure logging is turned on again in all cases */
+
+f.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, ...)
+{
+addr->basic_errno = code;
+
+if (format)
+ {
+ va_list ap;
+ gstring * g;
+
+ va_start(ap, format);
+ g = string_vformat(NULL, SVFMT_EXTEND|SVFMT_REBUFFER, CS format, ap);
+ va_end(ap);
+ addr->message = string_from_gstring(g);
+ }
+
+for (address_item * addr2 = addr->next; addr2; 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)
+{
+if (!nusers) return FALSE;
+for (int 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;
+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)
+ {
+ if (!route_find_expanded_group(tp->expand_gid, tp->name, US"transport", gidp,
+ &(addr->message)))
+ {
+ common_error(FALSE, addr, ERRNO_GIDFAIL, NULL);
+ return FALSE;
+ }
+ gid_set = TRUE;
+ }
+
+/* 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)
+ {
+ 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)
+ {
+ *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. */
+
+nuname = check_never_users(*uidp, never_users)
+ ? US"never_users"
+ : check_never_users(*uidp, fixed_never_users)
+ ? US"fixed_never_users"
+ : NULL;
+if (nuname)
+ {
+ 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)
+ {
+ rc = DEFER;
+ addr->message = size_limit == -1
+ ? string_sprintf("failed to expand message_size_limit "
+ "in %s transport: %s", tp->name, expand_string_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)
+{
+uschar * s = string_sprintf("%s/%s",
+ addr->unique + (testflag(addr, af_homonym)? 3:0), addr->transport->name);
+
+if (tree_search(tree_nonrecipients, s) != 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 for delivery. 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
+*/
+
+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->prop.errors_address)
+ return_path = addr->prop.errors_address;
+else
+ return_path = sender_address;
+
+if (tp->return_path)
+ {
+ uschar * new_return_path = expand_string(tp->return_path);
+ if (new_return_path)
+ return_path = new_return_path;
+ else if (!f.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;
+ }
+ }
+
+/* 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) /* Set in transport, or */
+ || ( (deliver_home = addr->home_dir) /* Set in address and */
+ && !testflag(addr, af_home_expanded) /* not expanded */
+ ) )
+ {
+ uschar *rawhome = deliver_home;
+ deliver_home = NULL; /* in case it contains $home */
+ if (!(deliver_home = expand_string(rawhome)))
+ {
+ 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 ? tp->current_dir : addr->current_dir;
+if (working_directory)
+ {
+ uschar *raw = working_directory;
+ if (!(working_directory = expand_string(raw)))
+ {
+ 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 ? deliver_home : US"/";
+
+/* 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 || tp->log_defer_output
+ ) )
+ {
+ uschar * error;
+
+ addr->return_filename =
+ spool_fname(US"msglog", message_subdir, message_id,
+ string_sprintf("-%d-%d", getpid(), return_count++));
+
+ if ((addr->return_file = open_msglog_file(addr->return_filename, 0400, &error)) < 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 = exim_fork(US"delivery-local")) == 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)
+ 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)
+ {
+ debug_printf(" home=%s current=%s\n", deliver_home, working_directory);
+ for (address_item * batched = addr->next; batched; 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, tp->name);
+
+ /* Setting these globals in the subprocess means we need never clear them */
+ transport_name = addr->transport->name;
+ driver_srcfile = tp->srcfile;
+ driver_srcline = tp->srcline;
+
+ /* If a transport filter has been specified, set up its argument list.
+ Any errors will get put into the address, and FALSE yielded. */
+
+ if (tp->filter_command)
+ {
+ ok = transport_set_up_command(&transport_filter_argv,
+ tp->filter_command,
+ TRUE, PANIC, addr, FALSE, US"transport filter", NULL);
+ transport_filter_timeout = tp->filter_timeout;
+ }
+ else transport_filter_argv = NULL;
+
+ if (ok)
+ {
+ debug_print_string(tp->debug_string);
+ replicate = !(tp->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; addr2 = addr2->next)
+ {
+ int i;
+ int local_part_length = Ustrlen(addr2->local_part);
+ uschar *s;
+ int ret;
+
+ if( (i = addr2->transport_return, (ret = write(pfd[pipe_write], &i, sizeof(int))) != sizeof(int))
+ || (ret = write(pfd[pipe_write], &transport_count, sizeof(transport_count))) != sizeof(transport_count)
+ || (ret = write(pfd[pipe_write], &addr2->flags, sizeof(addr2->flags))) != sizeof(addr2->flags)
+ || (ret = write(pfd[pipe_write], &addr2->basic_errno, sizeof(int))) != sizeof(int)
+ || (ret = write(pfd[pipe_write], &addr2->more_errno, sizeof(int))) != sizeof(int)
+ || (ret = write(pfd[pipe_write], &addr2->delivery_time, sizeof(struct timeval))) != sizeof(struct timeval)
+ || (i = addr2->special_action, (ret = write(pfd[pipe_write], &i, sizeof(int))) != sizeof(int))
+ || (ret = write(pfd[pipe_write], &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], &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",
+ 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 ? Ustrlen(s) + 1 : 0;
+ if( (ret = write(pfd[pipe_write], &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",
+ 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; addr2 = addr2->next)
+ {
+ if ((len = read(pfd[pipe_read], &status, sizeof(int))) > 0)
+ {
+ int i;
+ uschar **sptr;
+
+ addr2->transport_return = status;
+ len = read(pfd[pipe_read], &transport_count,
+ sizeof(transport_count));
+ len = read(pfd[pipe_read], &addr2->flags, sizeof(addr2->flags));
+ len = read(pfd[pipe_read], &addr2->basic_errno, sizeof(int));
+ len = read(pfd[pipe_read], &addr2->more_errno, sizeof(int));
+ len = read(pfd[pipe_read], &addr2->delivery_time, sizeof(struct timeval));
+ len = read(pfd[pipe_read], &i, sizeof(int)); addr2->special_action = i;
+ len = read(pfd[pipe_read], &addr2->transport,
+ sizeof(transport_instance *));
+
+ if (testflag(addr2, af_file))
+ {
+ int llen;
+ if ( read(pfd[pipe_read], &llen, sizeof(int)) != sizeof(int)
+ || llen > 64*4 /* limit from rfc 5821, times I18N factor */
+ )
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "bad local_part length read"
+ " from delivery subprocess");
+ break;
+ }
+ /* sanity-checked llen so disable the Coverity error */
+ /* coverity[tainted_data] */
+ if (read(pfd[pipe_read], big_buffer, llen) != llen)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "bad local_part read"
+ " from delivery subprocess");
+ break;
+ }
+ big_buffer[llen] = 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], &message_length, sizeof(int));
+ if (message_length > 0)
+ {
+ len = read(pfd[pipe_read], big_buffer, message_length);
+ big_buffer[big_buffer_size-1] = '\0'; /* guard byte */
+ 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; addr2 = addr2->next)
+ if (addr2->transport_return == OK)
+ {
+ 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. */
+
+ testharness_pause_ms(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)
+ {
+ int fd;
+ uschar *warn_message;
+ pid_t pid;
+
+ DEBUG(D_deliver) debug_printf("Warning message requested by transport\n");
+
+ if (!(warn_message = expand_string(addr->transport->warn_message)))
+ 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 if ((pid = child_open_exim(&fd, US"tpt-warning-message")) > 0)
+ {
+ FILE *f = fdopen(fd, "wb");
+ if (errors_reply_to && !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;
+ }
+}
+
+
+
+
+/* Check transport for the given concurrency limit. Return TRUE if over
+the limit (or an expansion failure), else FALSE and if there was a limit,
+the key for the hints database used for the concurrency count. */
+
+static BOOL
+tpt_parallel_check(transport_instance * tp, address_item * addr, uschar ** key)
+{
+unsigned max_parallel;
+
+if (!tp->max_parallel) return FALSE;
+
+max_parallel = (unsigned) expand_string_integer(tp->max_parallel, TRUE);
+if (expand_string_message)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "Failed to expand max_parallel option "
+ "in %s transport (%s): %s", tp->name, addr->address,
+ expand_string_message);
+ return TRUE;
+ }
+
+if (max_parallel > 0)
+ {
+ uschar * serialize_key = string_sprintf("tpt-serialize-%s", tp->name);
+ if (!enq_start(serialize_key, max_parallel))
+ {
+ address_item * next;
+ DEBUG(D_transport)
+ debug_printf("skipping tpt %s because concurrency limit %u reached\n",
+ tp->name, max_parallel);
+ do
+ {
+ next = addr->next;
+ addr->message = US"concurrency limit reached for transport";
+ addr->basic_errno = ERRNO_TRETRY;
+ post_process_one(addr, DEFER, LOG_MAIN, EXIM_DTYPE_TRANSPORT, 0);
+ } while ((addr = next));
+ return TRUE;
+ }
+ *key = serialize_key;
+ }
+return FALSE;
+}
+
+
+
+/*************************************************
+* 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)
+ {
+ struct timeval delivery_start;
+ struct timeval deliver_time;
+ address_item *addr2, *addr3, *nextaddr;
+ int logflags = LOG_MAIN;
+ int logchar = f.dont_deliver? '*' : '=';
+ transport_instance *tp;
+ uschar * serialize_key = NULL;
+
+ /* 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))
+ {
+ logflags |= LOG_PANIC;
+ f.disable_logging = FALSE; /* Jic */
+ addr->message = addr->router
+ ? string_sprintf("No transport set by %s router", addr->router->name)
+ : US"No transport set by system filter";
+ post_process_one(addr, DEFER, logflags, EXIM_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 */
+
+ f.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)
+ {
+ 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)
+ {
+ deliver_set_expansions(addr);
+ batch_id = expand_string(tp->batch_id);
+ deliver_set_expansions(NULL);
+ if (!batch_id)
+ {
+ 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) && batch_count < tp->batch_max)
+ {
+ BOOL ok =
+ tp == next->transport
+ && !previously_transported(next, TRUE)
+ && testflag(addr, af_pfr) == testflag(next, af_pfr)
+ && testflag(addr, af_file) == testflag(next, af_file)
+ && (!uses_lp || Ustrcmp(next->local_part, addr->local_part) == 0)
+ && (!uses_dom || Ustrcmp(next->domain, addr->domain) == 0)
+ && same_strings(next->prop.errors_address, addr->prop.errors_address)
+ && same_headers(next->prop.extra_headers, addr->prop.extra_headers)
+ && same_strings(next->prop.remove_headers, addr->prop.remove_headers)
+ && same_ugid(tp, addr, next)
+ && ( !addr->host_list && !next->host_list
+ || addr->host_list
+ && next->host_list
+ && 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)
+ {
+ 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)
+ {
+ 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)
+ {
+ int rc = check_message_size(tp, addr);
+ if (rc != OK)
+ {
+ replicate_status(addr);
+ while (addr)
+ {
+ addr2 = addr->next;
+ post_process_one(addr, rc, logflags, EXIM_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. */
+
+ if (!(dbm_file = dbfn_open(US"retry", O_RDONLY, &dbblock, FALSE, TRUE)))
+ DEBUG(D_deliver|D_retry|D_hints_lookup)
+ debug_printf("no retry data available\n");
+
+ addr2 = addr;
+ addr3 = NULL;
+ while (addr2)
+ {
+ 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)
+ {
+ 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)
+ {
+ 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 (f.queue_running && !f.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;
+ addr2 = addr3 ? (addr3->next = addr2->next)
+ : (addr = addr2->next);
+ post_process_one(this, DEFER, logflags, EXIM_DTYPE_TRANSPORT, 0);
+ }
+ }
+
+ if (dbm_file) 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) continue;
+
+ /* If the transport is limited for parallellism, enforce that here.
+ We use a hints DB entry, incremented here and decremented after
+ the transport (and any shadow transport) completes. */
+
+ if (tpt_parallel_check(tp, addr, &serialize_key))
+ {
+ if (expand_string_message)
+ {
+ logflags |= LOG_PANIC;
+ do
+ {
+ addr = addr->next;
+ post_process_one(addr, DEFER, logflags, EXIM_DTYPE_TRANSPORT, 0);
+ } while ((addr = addr2));
+ }
+ continue; /* Loop for the next set of addresses. */
+ }
+
+
+ /* 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);
+
+ gettimeofday(&delivery_start, NULL);
+ deliver_local(addr, FALSE);
+ timesince(&deliver_time, &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
+ && ( !tp->shadow_condition
+ || 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; stp = stp->next)
+ if (Ustrcmp(stp->name, tp->shadow) == 0) break;
+
+ if (!stp)
+ 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; addr2 = addr2->next)
+ if (addr2->transport_return == OK)
+ {
+ addr3 = store_get(sizeof(address_item), GET_UNTAINTED);
+ *addr3 = *addr2;
+ addr3->next = NULL;
+ addr3->shadow_message = US &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)
+ {
+ int save_count = transport_count;
+
+ DEBUG(D_deliver|D_transport)
+ debug_printf(">>>>>>>>>>>>>>>> Shadow delivery >>>>>>>>>>>>>>>>\n");
+ deliver_local(shadow_addr, TRUE);
+
+ for(; shadow_addr; 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
+ ? US""
+ : US": ",
+ shadow_addr->message
+ ? 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, rc_to_string(sresult), 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);
+
+ /* If the transport was parallelism-limited, decrement the hints DB record. */
+
+ if (serialize_key) enq_end(serialize_key);
+
+ /* 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; 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, rc_to_string(result), 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 */
+
+ addr2->delivery_time = deliver_time;
+ post_process_one(addr2, result, logflags, EXIM_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; 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;
+const uschar *listptr = remote_sort_domains;
+uschar *pattern;
+uschar patbuf[256];
+
+/*XXX The list is used before expansion. Not sure how that ties up with the docs */
+while ( *aptr
+ && (pattern = string_nextinlist(&listptr, &sep, patbuf, sizeof(patbuf)))
+ )
+ {
+ address_item *moved = NULL;
+ address_item **bptr = &moved;
+
+ while (*aptr)
+ {
+ address_item **next;
+ deliver_domain = (*aptr)->domain; /* set $domain */
+ if (match_isinlist(deliver_domain, (const uschar **)&pattern, UCHAR_MAX+1,
+ &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL) == OK)
+ {
+ aptr = &(*aptr)->next;
+ continue;
+ }
+
+ next = &(*aptr)->next;
+ while ( *next
+ && (deliver_domain = (*next)->domain, /* Set $domain */
+ match_isinlist(deliver_domain, (const uschar **)&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)
+ {
+ *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) *aptr = moved;
+ }
+
+DEBUG(D_deliver)
+ {
+ debug_printf("remote addresses after sorting:\n");
+ for (address_item * addr = addr_remote; addr; 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.
+
+hs12: This assumption is not true anymore, since we get quite large items (certificate
+information and such).
+
+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 *msg = p->msg;
+BOOL done = p->done;
+
+/* Loop through all items, reading from the pipe when necessary. The pipe
+used to be non-blocking. But I do not see a reason for using non-blocking I/O
+here, as the preceding poll() tells us, if data is available for reading.
+
+A read() on a "selected" handle should never block, but(!) it may return
+less data then we expected. (The buffer size we pass to read() shouldn't be
+understood as a "request", but as a "limit".)
+
+Each separate item is written to the pipe in a timely manner. But, especially for
+larger items, the read(2) may already return partial data from the write(2).
+
+The write is atomic mostly (depending on the amount written), but atomic does
+not imply "all or noting", it just is "not intermixed" with other writes on the
+same channel (pipe).
+
+*/
+
+DEBUG(D_deliver) debug_printf("reading pipe for subprocess %d (%s)\n",
+ (int)p->pid, eop? "ended" : "not ended yet");
+
+while (!done)
+ {
+ retry_item *r, **rp;
+ uschar pipeheader[PIPE_HEADER_SIZE+1];
+ uschar *id = &pipeheader[0];
+ uschar *subid = &pipeheader[1];
+ uschar *ptr = big_buffer;
+ size_t required = PIPE_HEADER_SIZE; /* first the pipehaeder, later the data */
+ ssize_t got;
+
+ DEBUG(D_deliver) debug_printf(
+ "expect %lu bytes (pipeheader) from tpt process %d\n", (u_long)required, pid);
+
+ /* We require(!) all the PIPE_HEADER_SIZE bytes here, as we know,
+ they're written in a timely manner, so waiting for the write shouldn't hurt a lot.
+ If we get less, we can assume the subprocess do be done and do not expect any further
+ information from it. */
+
+ if ((got = readn(fd, pipeheader, required)) != required)
+ {
+ msg = string_sprintf("got " SSIZE_T_FMT " of %d bytes (pipeheader) "
+ "from transport process %d for transport %s",
+ got, PIPE_HEADER_SIZE, pid, addr->transport->driver_name);
+ done = TRUE;
+ break;
+ }
+
+ pipeheader[PIPE_HEADER_SIZE] = '\0';
+ DEBUG(D_deliver)
+ debug_printf("got %ld bytes (pipeheader) from transport process %d\n",
+ (long) got, pid);
+
+ {
+ /* If we can't decode the pipeheader, the subprocess seems to have a
+ problem, we do not expect any furher information from it. */
+ char *endc;
+ required = Ustrtol(pipeheader+2, &endc, 10);
+ if (*endc)
+ {
+ msg = string_sprintf("failed to read pipe "
+ "from transport process %d for transport %s: error decoding size from header",
+ pid, addr->transport->driver_name);
+ done = TRUE;
+ break;
+ }
+ }
+
+ DEBUG(D_deliver)
+ debug_printf("expect %lu bytes (pipedata) from transport process %d\n",
+ (u_long)required, pid);
+
+ /* Same as above, the transport process will write the bytes announced
+ in a timely manner, so we can just wait for the bytes, getting less than expected
+ is considered a problem of the subprocess, we do not expect anything else from it. */
+ if ((got = readn(fd, big_buffer, required)) != required)
+ {
+ msg = string_sprintf("got only " SSIZE_T_FMT " of " SIZE_T_FMT
+ " bytes (pipedata) from transport process %d for transport %s",
+ got, required, pid, addr->transport->driver_name);
+ done = TRUE;
+ break;
+ }
+
+ /* Handle each possible type of item, assuming the complete item is
+ available in store. */
+
+ switch (*id)
+ {
+ /* 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; h = h->next)
+ {
+ if (!h->address || 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) 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); rp = &r->next)
+ if (Ustrcmp(r->key, ptr+1) == 0) /* Found item with same key */
+ {
+ if (!(r->flags & rf_delete)) 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 || !(*ptr & rf_delete))
+ {
+ r = store_get(sizeof(retry_item), GET_UNTAINTED);
+ 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 ? "delete" : "retry");
+ }
+
+ 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. */
+
+#ifndef DISABLE_TLS
+ case 'X':
+ if (!addr) goto ADDR_MISMATCH; /* Below, in 'A' handler */
+ switch (*subid)
+ {
+ case '1':
+ addr->tlsver = addr->cipher = addr->peerdn = NULL;
+
+ if (*ptr)
+ {
+ addr->cipher = string_copy(ptr);
+ addr->tlsver = string_copyn(ptr, Ustrchr(ptr, ':') - ptr);
+ }
+ while (*ptr++);
+ if (*ptr)
+ addr->peerdn = string_copy(ptr);
+ break;
+
+ case '2':
+ if (*ptr)
+ (void) tls_import_cert(ptr, &addr->peercert);
+ else
+ addr->peercert = NULL;
+ break;
+
+ case '3':
+ if (*ptr)
+ (void) tls_import_cert(ptr, &addr->ourcert);
+ else
+ addr->ourcert = NULL;
+ break;
+
+# ifndef DISABLE_OCSP
+ case '4':
+ addr->ocsp = *ptr ? *ptr - '0' : OCSP_NOT_REQ;
+ break;
+# endif
+ }
+ while (*ptr++);
+ break;
+#endif /*DISABLE_TLS*/
+
+ case 'C': /* client authenticator information */
+ switch (*subid)
+ {
+ 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':
+ setflag(addr, af_prdr_used);
+ break;
+#endif
+
+ case 'L':
+ switch (*subid)
+ {
+#ifndef DISABLE_PIPE_CONNECT
+ case 2: setflag(addr, af_early_pipe); /*FALLTHROUGH*/
+#endif
+ case 1: setflag(addr, af_pipelining); break;
+ }
+ break;
+
+ case 'K':
+ setflag(addr, af_chunking_used);
+ break;
+
+ case 'T':
+ setflag(addr, af_tcp_fastopen_conn);
+ if (*subid > '0') setflag(addr, af_tcp_fastopen);
+ if (*subid > '1') setflag(addr, af_tcp_fastopen_data);
+ break;
+
+ case 'D':
+ if (!addr) goto ADDR_MISMATCH;
+ 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;
+
+ case 'A':
+ if (!addr)
+ {
+ 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;
+ }
+
+ switch (*subid)
+ {
+ case 3: /* explicit notification of continued-connection (non)use;
+ overrides caller's knowlege. */
+ if (*ptr & BIT(1)) setflag(addr, af_new_conn);
+ else if (*ptr & BIT(2)) setflag(addr, af_cont_conn);
+ break;
+
+#ifdef SUPPORT_SOCKS
+ case '2': /* proxy information; must arrive before A0 and applies to that addr XXX oops*/
+ proxy_session = TRUE; /*XXX should this be cleared somewhere? */
+ if (*ptr == 0)
+ ptr++;
+ else
+ {
+ proxy_local_address = string_copy(ptr);
+ while(*ptr++);
+ memcpy(&proxy_local_port, ptr, sizeof(proxy_local_port));
+ ptr += sizeof(proxy_local_port);
+ }
+ break;
+#endif
+
+#ifdef EXPERIMENTAL_DSN_INFO
+ case '1': /* must arrive before A0, and applies to that addr */
+ /* Two strings: smtp_greeting and helo_response */
+ addr->smtp_greeting = string_copy(ptr);
+ while(*ptr++);
+ addr->helo_response = string_copy(ptr);
+ while(*ptr++);
+ break;
+#endif
+
+ case '0':
+ DEBUG(D_deliver) debug_printf("A0 %s tret %d\n", addr->address, *ptr);
+ 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->delivery_time, ptr, sizeof(addr->delivery_time));
+ ptr += sizeof(addr->delivery_time);
+ 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)
+ {
+ h = store_get(sizeof(host_item), GET_UNTAINTED);
+ 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;
+ }
+ break;
+
+ /* Local interface address/port */
+ case 'I':
+ if (*ptr) sending_ip_address = string_copy(ptr);
+ while (*ptr++) ;
+ if (*ptr) sending_port = atoi(CS ptr);
+ while (*ptr++) ;
+ 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("Z0%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 if we 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 && addr)
+ 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)
+ for (addr = addrlist; addr; addr = addr->next)
+ {
+ addr->transport_return = DEFER;
+ addr->special_action = SPECIAL_FREEZE;
+ addr->message = msg;
+ log_write(0, LOG_MAIN|LOG_PANIC, "Delivery status for %s: %s\n", addr->address, addr->message);
+ }
+
+/* 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)
+{
+/* If any host addresses were found to be unusable, add them to the unusable
+tree so that subsequent deliveries don't try them. */
+
+for (host_item * h = addr->host_list; h; h = h->next)
+ if (h->address)
+ 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)
+ {
+ 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
+ && !fallback
+ && !msg
+ )
+ {
+ 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)
+ {
+ addr->message = msg;
+ addr->transport_return = DEFER;
+ }
+ (void)post_process_one(addr, addr->transport_return, logflags,
+ EXIM_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) 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 poll(), 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 poll() 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 poll(), 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)
+ {
+ int 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 poll() 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("polling subprocess pipes\n");
+
+ for (poffset = 0; poffset < remote_max_parallel; poffset++)
+ if (parlist[poffset].pid != 0)
+ {
+ parpoll[poffset].fd = parlist[poffset].fd;
+ parpoll[poffset].events = POLLIN;
+ }
+ else
+ parpoll[poffset].fd = -1;
+
+ /* Stick in a 60-second timeout, just in case. */
+
+ readycount = poll(parpoll, remote_max_parallel, 60 * 1000);
+
+ /* Scan through the pipes and read any that are ready; use the count
+ returned by poll() 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
+ && parpoll[poffset].revents
+ )
+ {
+ 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 poll(), 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; 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)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "remote delivery process count got out of step");
+ parcount = 0;
+ }
+ else
+ {
+ transport_instance * tp = doneaddr->transport;
+ if (tp->max_parallel)
+ enq_end(string_sprintf("tpt-serialize-%s", tp->name));
+
+ remote_post_process(doneaddr, LOG_MAIN, NULL, fallback);
+ }
+ }
+}
+
+static void
+rmt_dlv_checked_write(int fd, char id, char subid, void * buf, ssize_t size)
+{
+uschar pipe_header[PIPE_HEADER_SIZE+1];
+size_t total_len = PIPE_HEADER_SIZE + size;
+
+struct iovec iov[2] = {
+ { pipe_header, PIPE_HEADER_SIZE }, /* indication about the data to expect */
+ { buf, size } /* *the* data */
+};
+
+ssize_t ret;
+
+/* we assume that size can't get larger then BIG_BUFFER_SIZE which currently is set to 16k */
+/* complain to log if someone tries with buffer sizes we can't handle*/
+
+if (size > BIG_BUFFER_SIZE-1)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "Failed writing transport result to pipe: can't handle buffers > %d bytes. truncating!\n",
+ BIG_BUFFER_SIZE-1);
+ size = BIG_BUFFER_SIZE;
+ }
+
+/* Should we check that we do not write more than PIPE_BUF? What would
+that help? */
+
+/* convert size to human readable string prepended by id and subid */
+if (PIPE_HEADER_SIZE != snprintf(CS pipe_header, PIPE_HEADER_SIZE+1, "%c%c%05ld",
+ id, subid, (long)size))
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "header snprintf failed\n");
+
+DEBUG(D_deliver) debug_printf("header write id:%c,subid:%c,size:%ld,final:%s\n",
+ id, subid, (long)size, pipe_header);
+
+if ((ret = writev(fd, iov, 2)) != total_len)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "Failed writing transport result to pipe (%ld of %ld bytes): %s",
+ (long)ret, (long)total_len, 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 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) 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)
+ {
+ parlist = store_get(remote_max_parallel * sizeof(pardata), GET_UNTAINTED);
+ for (poffset = 0; poffset < remote_max_parallel; poffset++)
+ parlist[poffset].pid = 0;
+ parpoll = store_get(remote_max_parallel * sizeof(struct pollfd), GET_UNTAINTED);
+ }
+
+/* Now loop for each remote delivery */
+
+for (int delivery_count = 0; addr_remote; 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;
+ uschar * panicmsg;
+ uschar * serialize_key = NULL;
+
+ /* 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))
+ {
+ f.disable_logging = FALSE; /* Jic */
+ panicmsg = US"No transport set by router";
+ goto panic_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)
+ {
+ int rc = check_message_size(tp, addr);
+ if (rc != OK)
+ {
+ addr->transport_return = rc;
+ remote_post_process(addr, LOG_MAIN, NULL, fallback);
+ continue;
+ }
+ }
+
+/*XXX need to defeat this when DANE is used - but we don't know that yet.
+So look out for the place it gets used.
+*/
+
+ /* Get the flag which specifies whether the transport can handle different
+ domains that nevertheless resolve to the same set of hosts. If it needs
+ expanding, get variables set: $address_data, $domain_data, $localpart_data,
+ $host, $host_address, $host_port. */
+ if (tp->expand_multi_domain)
+ deliver_set_expansions(addr);
+
+ if (exp_bool(addr, US"transport", tp->name, D_transport,
+ US"multi_domain", tp->multi_domain, tp->expand_multi_domain,
+ &multi_domain) != OK)
+ {
+ deliver_set_expansions(NULL);
+ panicmsg = addr->message;
+ goto panic_continue;
+ }
+
+ /* 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;
+ }
+
+ /************************************************************************/
+
+
+/*XXX don't know yet if DANE will be used. So tpt will have to
+check at the point if gets next addr from list, and skip/defer any
+nonmatch domains
+*/
+
+ /* 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).
+ If the transport does not handle multiple domains, enforce that also,
+ and if it might need a per-address check for this, re-evaluate it.
+ */
+
+ while ((next = *anchor) && address_count < address_count_max)
+ {
+ BOOL md;
+ if ( (multi_domain || Ustrcmp(next->domain, addr->domain) == 0)
+ && tp == next->transport
+ && same_hosts(next->host_list, addr->host_list)
+ && same_strings(next->prop.errors_address, addr->prop.errors_address)
+ && same_headers(next->prop.extra_headers, addr->prop.extra_headers)
+ && same_ugid(tp, next, addr)
+ && ( next->prop.remove_headers == addr->prop.remove_headers
+ || ( next->prop.remove_headers
+ && addr->prop.remove_headers
+ && Ustrcmp(next->prop.remove_headers, addr->prop.remove_headers) == 0
+ ) )
+ && ( !multi_domain
+ || ( (
+ (void)(!tp->expand_multi_domain || ((void)deliver_set_expansions(next), 1)),
+ exp_bool(addr,
+ US"transport", next->transport->name, D_transport,
+ US"multi_domain", next->transport->multi_domain,
+ next->transport->expand_multi_domain, &md) == OK
+ )
+ && md
+ ) ) )
+ {
+ *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);
+ deliver_set_expansions(NULL);
+ }
+
+ /* 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)
+ {
+ last->next = addr_remote;
+ addr_remote = addr;
+ return FALSE;
+ }
+
+ /* If the transport is limited for parallellism, enforce that here.
+ The hints DB entry is decremented in par_reduce(), when we reap the
+ transport process. */
+
+ if (tpt_parallel_check(tp, addr, &serialize_key))
+ if ((panicmsg = expand_string_message))
+ goto panic_continue;
+ else
+ continue; /* Loop for the next set of addresses. */
+
+ /* 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->prop.errors_address)
+ return_path = addr->prop.errors_address;
+ else
+ return_path = sender_address;
+
+ if (tp->return_path)
+ {
+ uschar *new_return_path = expand_string(tp->return_path);
+ if (new_return_path)
+ return_path = new_return_path;
+ else if (!f.expand_string_forcedfail)
+ {
+ panicmsg = string_sprintf("Failed to expand return path \"%s\": %s",
+ tp->return_path, expand_string_message);
+ goto enq_continue;
+ }
+ }
+
+ /* 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))
+ {
+ panicmsg = NULL;
+ goto enq_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)
+ (void)((tp->setup)(addr->transport, addr, NULL, uid, gid, NULL));
+
+ /* If we have a connection still open from a verify stage (lazy-close)
+ treat it as if it is a continued connection (apart from the counter used
+ for the log line mark). */
+
+ if (cutthrough.cctx.sock >= 0 && cutthrough.callout_hold_only)
+ {
+ DEBUG(D_deliver)
+ debug_printf("lazy-callout-close: have conn still open from verification\n");
+ continue_transport = cutthrough.transport;
+ continue_hostname = string_copy(cutthrough.host.name);
+ continue_host_address = string_copy(cutthrough.host.address);
+ continue_sequence = 1;
+ sending_ip_address = cutthrough.snd_ip;
+ sending_port = cutthrough.snd_port;
+ smtp_peer_options = cutthrough.peer_options;
+ }
+
+ /* 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. */
+
+ f.continue_more = FALSE; /* In case got set for the last lot */
+ if (continue_transport)
+ {
+ BOOL ok = Ustrcmp(continue_transport, tp->name) == 0;
+/*XXX do we need to check for a DANEd conn vs. a change of domain? */
+
+ /* If the transport is about to override the host list do not check
+ it here but take the cost of running the transport process to discover
+ if the continued_hostname connection is suitable. This is a layering
+ violation which is unfortunate as it requires we haul in the smtp
+ include file. */
+
+ if (ok)
+ {
+ smtp_transport_options_block * ob;
+
+ if ( !( Ustrcmp(tp->info->driver_name, "smtp") == 0
+ && (ob = (smtp_transport_options_block *)tp->options_block)
+ && ob->hosts_override && ob->hosts
+ )
+ && addr->host_list
+ )
+ {
+ ok = FALSE;
+ for (host_item * h = addr->host_list; h; h = h->next)
+ if (Ustrcmp(h->name, continue_hostname) == 0)
+ /*XXX should also check port here */
+ { 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 (%s)\n",
+ Ustrcmp(continue_transport, tp->name) != 0
+ ? string_sprintf("tpt %s vs %s", continue_transport, tp->name)
+ : string_sprintf("no host matching %s", continue_hostname));
+ if (serialize_key) enq_end(serialize_key);
+
+ if (addr->fallback_hosts && !fallback)
+ {
+ for (next = addr; ; next = next->next)
+ {
+ next->host_list = next->fallback_hosts;
+ DEBUG(D_deliver) debug_printf("%s queued for fallback host(s)\n", next->address);
+ if (!next->next) break;
+ }
+ next->next = addr_fallback;
+ addr_fallback = addr;
+ }
+
+ else
+ {
+ for (next = addr; ; next = next->next)
+ {
+ DEBUG(D_deliver) debug_printf(" %s to def list\n", next->address);
+ if (!next->next) break;
+ }
+ 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. We'd like to do that
+ for non-continue_transport cases too but the knowlege of which host is
+ connected to is too hard to manage. Perhaps we need a finer-grain
+ interface to the transport. */
+
+ for (next = addr_remote; next && !f.continue_more; next = next->next)
+ for (host_item * h = next->host_list; h; h = h->next)
+ if (Ustrcmp(h->name, continue_hostname) == 0)
+ { f.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. */
+
+/* The data appears in a timely manner and we already did a poll on
+all pipes, so I do not see a reason to use non-blocking IO here
+
+#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)
+ {
+ panicmsg = string_sprintf("unable to create pipe: %s", strerror(errno));
+ goto enq_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]);
+ panicmsg = US"Unexpectedly no free subprocess slot";
+ goto enq_continue;
+ }
+
+ /* Now fork a subprocess to do the remote delivery, but before doing so,
+ ensure that any cached resources are released so as not to interfere with
+ what happens in the subprocess. */
+
+ search_tidyup();
+
+ if ((pid = exim_fork(US"transport")) == 0)
+ {
+ int fd = pfd[pipe_write];
+ host_item *h;
+
+ /* Setting these globals in the subprocess means we need never clear them */
+ transport_name = addr->transport->name;
+ driver_srcfile = tp->srcfile;
+ driver_srcline = tp->srcline;
+
+ /* There are weird circumstances in which logging is disabled */
+ f.disable_logging = tp->disable_logging;
+
+ /* Show pids on debug output if parallelism possible */
+
+ if (parmax > 1 && (parcount > 0 || addr_remote))
+ DEBUG(D_any|D_v) debug_selector |= D_pid;
+
+ /* 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 = f.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);
+ {
+ uschar * fname = spool_fname(US"input", message_subdir, message_id, US"-D");
+
+ if ((deliver_datafile = Uopen(fname,
+#ifdef O_CLOEXEC
+ O_CLOEXEC |
+#endif
+ O_RDWR | O_APPEND, 0)) < 0)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Failed to reopen %s for remote "
+ "parallel delivery: %s", fname, strerror(errno));
+ }
+
+ /* Set the close-on-exec flag */
+#ifndef O_CLOEXEC
+ (void)fcntl(deliver_datafile, F_SETFD, fcntl(deliver_datafile, F_GETFD) |
+ FD_CLOEXEC);
+#endif
+
+ /* 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 ? ", ..." : "");
+
+ /* 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; h = h->next)
+ {
+ if (!h->address || h->status < hstatus_unusable) continue;
+ sprintf(CS big_buffer, "%c%c%s", h->status, h->why, h->address);
+ rmt_dlv_checked_write(fd, 'H', '0', big_buffer, Ustrlen(big_buffer+2) + 3);
+ }
+
+ /* 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(). */
+
+ memcpy(big_buffer, &transport_count, sizeof(transport_count));
+ rmt_dlv_checked_write(fd, 'S', '0', big_buffer, sizeof(transport_count));
+
+ /* 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; addr = addr->next)
+ {
+ uschar *ptr;
+
+ /* The certificate verification status goes into the flags */
+ if (tls_out.certificate_verified) setflag(addr, af_cert_verified);
+#ifdef SUPPORT_DANE
+ if (tls_out.dane_verified) setflag(addr, af_dane_verified);
+#endif
+# ifndef DISABLE_TLS_RESUME
+ if (tls_out.resumption & RESUME_USED) setflag(addr, af_tls_resume);
+# endif
+
+ /* Use an X item only if there's something to send */
+#ifndef DISABLE_TLS
+ if (addr->cipher)
+ {
+ ptr = big_buffer + sprintf(CS big_buffer, "%.128s", addr->cipher) + 1;
+ if (!addr->peerdn)
+ *ptr++ = 0;
+ else
+ ptr += sprintf(CS ptr, "%.512s", addr->peerdn) + 1;
+
+ rmt_dlv_checked_write(fd, 'X', '1', big_buffer, ptr - big_buffer);
+ }
+ else if (continue_proxy_cipher)
+ {
+ ptr = big_buffer + sprintf(CS big_buffer, "%.128s", continue_proxy_cipher) + 1;
+ *ptr++ = 0;
+ rmt_dlv_checked_write(fd, 'X', '1', big_buffer, ptr - big_buffer);
+ }
+
+ if (addr->peercert)
+ {
+ ptr = big_buffer;
+ if (tls_export_cert(ptr, big_buffer_size-2, addr->peercert))
+ while(*ptr++);
+ else
+ *ptr++ = 0;
+ rmt_dlv_checked_write(fd, 'X', '2', big_buffer, ptr - big_buffer);
+ }
+ if (addr->ourcert)
+ {
+ ptr = big_buffer;
+ if (tls_export_cert(ptr, big_buffer_size-2, addr->ourcert))
+ while(*ptr++);
+ else
+ *ptr++ = 0;
+ rmt_dlv_checked_write(fd, 'X', '3', big_buffer, ptr - big_buffer);
+ }
+# ifndef DISABLE_OCSP
+ if (addr->ocsp > OCSP_NOT_REQ)
+ {
+ ptr = big_buffer + sprintf(CS big_buffer, "%c", addr->ocsp + '0') + 1;
+ rmt_dlv_checked_write(fd, 'X', '4', big_buffer, ptr - big_buffer);
+ }
+# endif
+#endif /*DISABLE_TLS*/
+
+ if (client_authenticator)
+ {
+ ptr = big_buffer + sprintf(CS big_buffer, "%.64s", client_authenticator) + 1;
+ rmt_dlv_checked_write(fd, 'C', '1', big_buffer, ptr - big_buffer);
+ }
+ if (client_authenticated_id)
+ {
+ ptr = big_buffer + sprintf(CS big_buffer, "%.64s", client_authenticated_id) + 1;
+ rmt_dlv_checked_write(fd, 'C', '2', big_buffer, ptr - big_buffer);
+ }
+ if (client_authenticated_sender)
+ {
+ ptr = big_buffer + sprintf(CS big_buffer, "%.64s", client_authenticated_sender) + 1;
+ rmt_dlv_checked_write(fd, 'C', '3', big_buffer, ptr - big_buffer);
+ }
+
+#ifndef DISABLE_PRDR
+ if (testflag(addr, af_prdr_used))
+ rmt_dlv_checked_write(fd, 'P', '0', NULL, 0);
+#endif
+
+ if (testflag(addr, af_pipelining))
+#ifndef DISABLE_PIPE_CONNECT
+ if (testflag(addr, af_early_pipe))
+ rmt_dlv_checked_write(fd, 'L', '2', NULL, 0);
+ else
+#endif
+ rmt_dlv_checked_write(fd, 'L', '1', NULL, 0);
+
+ if (testflag(addr, af_chunking_used))
+ rmt_dlv_checked_write(fd, 'K', '0', NULL, 0);
+
+ if (testflag(addr, af_tcp_fastopen_conn))
+ rmt_dlv_checked_write(fd, 'T',
+ testflag(addr, af_tcp_fastopen) ? testflag(addr, af_tcp_fastopen_data)
+ ? '2' : '1' : '0',
+ NULL, 0);
+
+ memcpy(big_buffer, &addr->dsn_aware, sizeof(addr->dsn_aware));
+ rmt_dlv_checked_write(fd, 'D', '0', big_buffer, sizeof(addr->dsn_aware));
+
+ /* Retry information: for most success cases this will be null. */
+
+ for (retry_item * r = addr->retries; r; r = r->next)
+ {
+ sprintf(CS big_buffer, "%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) *ptr++ = 0; else
+ {
+ sprintf(CS ptr, "%.512s", r->message);
+ while(*ptr++);
+ }
+ rmt_dlv_checked_write(fd, 'R', '0', big_buffer, ptr - big_buffer);
+ }
+
+ if (testflag(addr, af_new_conn) || testflag(addr, af_cont_conn))
+ {
+ DEBUG(D_deliver) debug_printf("%scontinued-connection\n",
+ testflag(addr, af_new_conn) ? "non-" : "");
+ big_buffer[0] = testflag(addr, af_new_conn) ? BIT(1) : BIT(2);
+ rmt_dlv_checked_write(fd, 'A', '3', big_buffer, 1);
+ }
+
+#ifdef SUPPORT_SOCKS
+ if (LOGGING(proxy) && proxy_session)
+ {
+ ptr = big_buffer;
+ if (proxy_local_address)
+ {
+ DEBUG(D_deliver) debug_printf("proxy_local_address '%s'\n", proxy_local_address);
+ ptr = big_buffer + sprintf(CS ptr, "%.128s", proxy_local_address) + 1;
+ DEBUG(D_deliver) debug_printf("proxy_local_port %d\n", proxy_local_port);
+ memcpy(ptr, &proxy_local_port, sizeof(proxy_local_port));
+ ptr += sizeof(proxy_local_port);
+ }
+ else
+ *ptr++ = '\0';
+ rmt_dlv_checked_write(fd, 'A', '2', big_buffer, ptr - big_buffer);
+ }
+#endif
+
+#ifdef EXPERIMENTAL_DSN_INFO
+/*um, are they really per-addr? Other per-conn stuff is not (auth, tls). But host_used is! */
+ if (addr->smtp_greeting)
+ {
+ DEBUG(D_deliver) debug_printf("smtp_greeting '%s'\n", addr->smtp_greeting);
+ ptr = big_buffer + sprintf(CS big_buffer, "%.128s", addr->smtp_greeting) + 1;
+ if (addr->helo_response)
+ {
+ DEBUG(D_deliver) debug_printf("helo_response '%s'\n", addr->helo_response);
+ ptr += sprintf(CS ptr, "%.128s", addr->helo_response) + 1;
+ }
+ else
+ *ptr++ = '\0';
+ rmt_dlv_checked_write(fd, 'A', '1', big_buffer, ptr - big_buffer);
+ }
+#endif
+
+ /* The rest of the information goes in an 'A0' item. */
+
+ sprintf(CS big_buffer, "%c%c", addr->transport_return, addr->special_action);
+ ptr = big_buffer + 2;
+ 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->delivery_time, sizeof(addr->delivery_time));
+ ptr += sizeof(addr->delivery_time);
+ memcpy(ptr, &addr->flags, sizeof(addr->flags));
+ ptr += sizeof(addr->flags);
+
+ if (!addr->message) *ptr++ = 0; else
+ ptr += sprintf(CS ptr, "%.1024s", addr->message) + 1;
+
+ if (!addr->user_message) *ptr++ = 0; else
+ ptr += sprintf(CS ptr, "%.1024s", addr->user_message) + 1;
+
+ if (!addr->host_used) *ptr++ = 0; else
+ {
+ ptr += sprintf(CS ptr, "%.256s", addr->host_used->name) + 1;
+ ptr += sprintf(CS ptr, "%.64s", addr->host_used->address) + 1;
+ 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, 'A', '0', big_buffer, ptr - big_buffer);
+ }
+
+ /* Local interface address/port */
+#ifdef EXPERIMENTAL_DSN_INFO
+ if (sending_ip_address)
+#else
+ if (LOGGING(incoming_interface) && sending_ip_address)
+#endif
+ {
+ uschar * ptr;
+ ptr = big_buffer + sprintf(CS big_buffer, "%.128s", sending_ip_address) + 1;
+ ptr += sprintf(CS ptr, "%d", sending_port) + 1;
+ rmt_dlv_checked_write(fd, 'I', '0', 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] = continue_transport ? '1' : '0';
+ rmt_dlv_checked_write(fd, 'Z', '0', big_buffer, 1);
+ (void)close(fd);
+ exit(EXIT_SUCCESS);
+ }
+
+ /* Back in the mainline: close the unwanted half of the pipe. */
+
+ (void)close(pfd[pipe_write]);
+
+ /* If we have a connection still open from a verify stage (lazy-close)
+ release its TLS library context (if any) as responsibility was passed to
+ the delivery child process. */
+
+ if (cutthrough.cctx.sock >= 0 && cutthrough.callout_hold_only)
+ {
+#ifndef DISABLE_TLS
+ if (cutthrough.is_tls)
+ tls_close(cutthrough.cctx.tls_ctx, TLS_NO_SHUTDOWN);
+#endif
+ (void) close(cutthrough.cctx.sock);
+ release_cutthrough_connection(US"passed to transport proc");
+ }
+
+ /* Fork failed; defer with error message */
+
+ if (pid == -1)
+ {
+ (void)close(pfd[pipe_read]);
+ panicmsg = string_sprintf("fork failed for remote delivery to %s: %s",
+ addr->domain, strerror(errno));
+ goto enq_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) 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. Wait long enough for most cases to complete
+ the transport. */
+
+ else testharness_pause_ms(600);
+
+ continue;
+
+enq_continue:
+ if (serialize_key) enq_end(serialize_key);
+panic_continue:
+ remote_post_process(addr, LOG_MAIN|LOG_PANIC, panicmsg, fallback);
+ continue;
+ }
+
+/* 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;
+uschar * t;
+int len;
+
+if (!(domain = Ustrrchr(address, '@')))
+ return DEFER; /* should always have a domain, but just in case... */
+
+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, address);
+while(len-- > 0)
+ {
+ 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)
+ {
+ 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, (const uschar **)&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)
+ {
+ address_item * new_parent = store_get(sizeof(address_item), GET_UNTAINTED);
+ *new_parent = *addr;
+ addr->parent = new_parent;
+ new_parent->child_count = 1;
+ 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)
+{
+uschar *yield;
+gstring * para;
+uschar buffer[256];
+
+if (!f) return NULL;
+
+if (!Ufgets(buffer, sizeof(buffer), f) || Ustrcmp(buffer, "****\n") == 0)
+ return NULL;
+
+para = string_get(256);
+for (;;)
+ {
+ para = string_cat(para, buffer);
+ if (!Ufgets(buffer, sizeof(buffer), f) || Ustrcmp(buffer, "****\n") == 0)
+ break;
+ }
+if ((yield = expand_string(string_from_gstring(para))))
+ 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)
+ for (transport_instance * t = transports; t; t = t->next)
+ if (Ustrcmp(t->name, continue_transport) == 0)
+ {
+ if (t->info->closedown) (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) ancestor = ancestor->parent;
+
+fprintf(f, "%s", CS si);
+
+if (addr->parent && testflag(addr, af_hide_child))
+ {
+ printed = US"an undisclosed address";
+ yield = FALSE;
+ }
+else if (!testflag(addr, af_pfr) || !addr->parent)
+ 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;
+ if (!original) original= ancestor->address;
+ if (strcmpic(original, printed) != 0)
+ fprintf(f, "%s(%sgenerated from %s)", sc,
+ ancestor != addr->parent ? "ultimately " : "",
+ string_printing(original));
+ }
+
+if (addr->host_used)
+ fprintf(f, "\n host %s [%s]",
+ addr->host_used->name, addr->host_used->address);
+
+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, const uschar * t)
+{
+int count = Ustrlen(t);
+uschar * s = testflag(addr, af_pass_message) ? addr->message : NULL;
+
+if (!s && !(s = addr->user_message))
+ return;
+
+fprintf(f, "\n %s", t);
+
+while (*s)
+ 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;
+ }
+ }
+}
+
+
+/***********************************************************
+* Print Diagnostic-Code 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 as
+required by RFC 3461 by adding a space after each newline
+
+it uses the same logic as print_address_error() above. if af_pass_message is true
+and addr->message is set it uses the remote host answer. if not addr->user_message
+is used instead if available.
+
+Arguments:
+ addr the address
+ f the FILE to print on
+
+Returns: nothing
+*/
+
+static void
+print_dsn_diagnostic_code(const address_item *addr, FILE *f)
+{
+uschar * s = testflag(addr, af_pass_message) ? addr->message : NULL;
+unsigned cnt;
+
+/* af_pass_message and addr->message set ? print remote host answer */
+if (s)
+ {
+ DEBUG(D_deliver)
+ debug_printf("DSN Diagnostic-Code: addr->message = %s\n", addr->message);
+
+ /* search first ": ". we assume to find the remote-MTA answer there */
+ if (!(s = Ustrstr(addr->message, ": ")))
+ return; /* not found, bail out */
+ s += 2; /* skip ": " */
+ cnt = fprintf(f, "Diagnostic-Code: smtp; ");
+ }
+/* no message available. do nothing */
+else return;
+
+while (*s)
+ {
+ if (cnt > 950) /* RFC line length limit: 998 */
+ {
+ DEBUG(D_deliver) debug_printf("print_dsn_diagnostic_code() truncated line\n");
+ fputs("[truncated]", f);
+ break;
+ }
+
+ if (*s == '\\' && s[1] == 'n')
+ {
+ fputs("\n ", f); /* as defined in RFC 3461 */
+ s += 2;
+ cnt += 2;
+ }
+ else
+ {
+ fputc(*s++, f);
+ cnt++;
+ }
+ }
+
+fputc('\n', f);
+}
+
+
+/*************************************************
+* 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))
+ {
+ tree_node *tnode;
+ if (testflag(addr, af_pfr))
+ {
+ anchor = &(addr->next);
+ }
+ else if ((tnode = tree_search(tree_duplicates, addr->unique)))
+ {
+ 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);
+ }
+ }
+}
+
+
+
+
+/************************************************/
+
+static void
+print_dsn_addr_action(FILE * f, address_item * addr,
+ uschar * action, uschar * status)
+{
+address_item * pa;
+
+if (addr->dsn_orcpt)
+ fprintf(f,"Original-Recipient: %s\n", addr->dsn_orcpt);
+
+for (pa = addr; pa->parent; ) pa = pa->parent;
+fprintf(f, "Action: %s\n"
+ "Final-Recipient: rfc822;%s\n"
+ "Status: %s\n",
+ action, pa->address, status);
+}
+
+
+
+/* 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. */
+
+int
+test_harness_fudged_queue_time(int actual_time)
+{
+int qt;
+if ( f.running_in_test_harness && *fudged_queue_times
+ && (qt = readconf_readtime(fudged_queue_times, '/', FALSE)) >= 0)
+ {
+ DEBUG(D_deliver) debug_printf("fudged queue_times = %s\n",
+ fudged_queue_times);
+ return qt;
+ }
+return actual_time;
+}
+
+/************************************************/
+
+static FILE *
+expand_open(const uschar * filename,
+ const uschar * varname, const uschar * reason)
+{
+const uschar * s = expand_cstring(filename);
+FILE * fp = NULL;
+
+if (!s || !*s)
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "Failed to expand %s: '%s'\n", varname, filename);
+else if (*s != '/' || is_tainted(s))
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s is not %s after expansion: '%s'\n",
+ varname, *s == '/' ? "untainted" : "absolute", s);
+else if (!(fp = Ufopen(s, "rb")))
+ log_write(0, LOG_MAIN|LOG_PANIC, "Failed to open %s for %s "
+ "message texts: %s", s, reason, strerror(errno));
+return fp;
+}
+
+/*************************************************
+* 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.
+
+Liable to be called as root.
+
+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;
+int process_recipients = RECIP_ACCEPT;
+open_db dbblock;
+open_db *dbm_file;
+extern int acl_where;
+uschar *info;
+
+#ifdef MEASURE_TIMING
+report_time_since(&timestamp_startup, US"delivery start"); /* testcase 0022, 2100 */
+#endif
+
+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)
+ && (debug_selector & (D_deliver|D_queue_run|D_v))
+ )
+ 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. */
+
+if (id != message_id)
+ Ustrcpy(message_id, id);
+f.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 ((deliver_datafile = spool_open_datafile(id)) < 0)
+ 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. */
+
+ {
+ uschar * spoolname = string_sprintf("%s-H", id);
+ if ((rc = spool_read_header(spoolname, TRUE, TRUE)) != spool_read_OK)
+ {
+ if (errno == ERRNO_SPOOLFORMAT)
+ {
+ struct stat statbuf;
+ if (Ustat(spool_fname(US"input", message_subdir, spoolname, US""),
+ &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.tv_sec = received_time.tv_usec = 0;
+ /*XXX subsec precision?*/
+ for (i = 0; i < 6; i++)
+ received_time.tv_sec = received_time.tv_sec * BASE_62 + tab62[id[i] - '0'];
+ }
+
+ /* If we've had this malformed message too long, sling it. */
+
+ if (now - received_time.tv_sec > keep_malformed)
+ {
+ Uunlink(spool_fname(US"msglog", message_subdir, id, US""));
+ Uunlink(spool_fname(US"input", message_subdir, id, US"-D"));
+ Uunlink(spool_fname(US"input", message_subdir, id, US"-H"));
+ Uunlink(spool_fname(US"input", message_subdir, id, US"-J"));
+ 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. */
+
+ {
+ uschar * fname = spool_fname(US"input", message_subdir, id, US"-J");
+ FILE * jread;
+
+ if ( (journal_fd = Uopen(fname, O_RDWR|O_APPEND
+#ifdef O_CLOEXEC
+ | O_CLOEXEC
+#endif
+#ifdef O_NOFOLLOW
+ | O_NOFOLLOW
+#endif
+ , SPOOL_MODE)) >= 0
+ && lseek(journal_fd, 0, SEEK_SET) == 0
+ && (jread = fdopen(journal_fd, "rb"))
+ )
+ {
+ while (Ufgets(big_buffer, big_buffer_size, jread))
+ {
+ 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);
+ }
+ rewind(jread);
+ if ((journal_fd = dup(fileno(jread))) < 0)
+ journal_fd = fileno(jread);
+ else
+ (void) fclose(jread); /* Try to not leak the FILE resource */
+
+ /* 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)
+ {
+ (void)close(deliver_datafile);
+ deliver_datafile = -1;
+ log_write(0, LOG_MAIN, "Spool error: no recipients for %s", fname);
+ 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 (f.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 && 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 || !f.deliver_force_thaw
+ || !f.admin_user || continue_hostname
+ ) )
+ {
+ (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)
+ {
+ f.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. */
+
+ f.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 * fname = spool_fname(US"msglog", message_subdir, id, US"");
+ uschar * error;
+ int fd;
+
+ if ((fd = open_msglog_file(fname, SPOOL_MODE, &error)) < 0)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't %s message log %s: %s", error,
+ fname, strerror(errno));
+ return continue_closedown(); /* yields DELIVER_NOT_ATTEMPTED */
+ }
+
+ /* Make a stdio stream out of it. */
+
+ if (!(message_log = fdopen(fd, "a")))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't fdopen message log %s: %s",
+ fname, 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 ? 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 && 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;
+ f.enable_dollar_recipients = TRUE; /* Permit $recipients in system filter */
+ f.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. */
+
+ f.system_filtering = FALSE;
+ f.enable_dollar_recipients = FALSE;
+ if (filter_message && 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 && !f.deliver_manual_thaw)
+ {
+ f.deliver_freeze = TRUE;
+ deliver_frozen_at = time(NULL);
+ process_recipients = RECIP_DEFER;
+ frozen_info = string_sprintf(" by the system filter%s%s",
+ filter_message ? US": " : US"",
+ filter_message ? filter_message : US"");
+ }
+
+ /* 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)
+ {
+ uschar *logend;
+ colon = US": ";
+ if ( filter_message[0] == '<'
+ && filter_message[1] == '<'
+ && (logend = Ustrstr(filter_message, ">>"))
+ )
+ {
+ 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)
+ log_write(0, LOG_MAIN, "original recipients ignored (system filter)");
+ else
+ log_write(0, LOG_MAIN, "=> discarded (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)
+ {
+ 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)
+ {
+ if (parent->child_count == USHRT_MAX)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "system filter generated more "
+ "than %d delivery addresses", USHRT_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);
+ setflag(p, af_gid_set);
+ setflag(p, af_allow_file);
+ setflag(p, af_allow_pipe);
+ setflag(p, 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)
+ {
+ uschar *tmp = expand_string(tpname);
+ address_file = address_pipe = NULL;
+ if (!tmp)
+ p->message = string_sprintf("failed to expand \"%s\" as a "
+ "system filter transport name", tpname);
+ if (is_tainted(tmp))
+ p->message = string_sprintf("attempt to used tainted value '%s' for"
+ "transport '%s' as a system filter", tmp, tpname);
+ tpname = tmp;
+ }
+ else
+ p->message = string_sprintf("system_filter_%s_transport is unset",
+ type);
+
+ if (tpname)
+ {
+ transport_instance *tp;
+ for (tp = transports; tp; tp = tp->next)
+ if (Ustrcmp(tp->name, tpname) == 0)
+ {
+ p->transport = tp;
+ break;
+ }
+ if (!tp)
+ 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)
+ {
+ address_item *badp = p;
+ p = p->next;
+ if (!addr_last) 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, EXIM_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))
+ {
+ recipient_item *r = recipients_list + i;
+ address_item *new = deliver_make_addr(r->address, FALSE);
+ new->prop.errors_address = r->errors_to;
+#ifdef SUPPORT_I18N
+ if ((new->prop.utf8_msg = message_smtputf8))
+ {
+ new->prop.utf8_downcvt = message_utf8_downconvert == 1;
+ new->prop.utf8_downcvt_maybe = message_utf8_downconvert == -1;
+ DEBUG(D_deliver) debug_printf("utf8, downconvert %s\n",
+ new->prop.utf8_downcvt ? "yes"
+ : new->prop.utf8_downcvt_maybe ? "ifneeded"
+ : "no");
+ }
+#endif
+
+ if (r->pno >= 0)
+ new->onetime_parent = recipients_list[r->pno].address;
+
+ /* 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: 0x%x\n",
+ new->dsn_orcpt ? new->dsn_orcpt : US"", new->dsn_flags);
+
+ 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 ? filter_message : US"delivery cancelled";
+ 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])
+ {
+ 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, EXIM_DTYPE_ROUTER, 0);
+ break;
+
+
+ /* Value should be RECIP_ACCEPT; take this as the safe default. */
+
+ default:
+ if (!addr_new) addr_new = new; else addr_last->next = new;
+ addr_last = new;
+ break;
+ }
+
+#ifndef DISABLE_EVENT
+ if (process_recipients != RECIP_ACCEPT && event_action)
+ {
+ uschar * save_local = deliver_localpart;
+ const uschar * save_domain = deliver_domain;
+ uschar * addr = new->address, * errmsg = NULL;
+ int start, end, dom;
+
+ if (!parse_extract_address(addr, &errmsg, &start, &end, &dom, TRUE))
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "failed to parse address '%.100s': %s\n", addr, errmsg);
+ else
+ {
+ deliver_localpart =
+ string_copyn(addr+start, dom ? (dom-1) - start : end - start);
+ deliver_domain = dom ? CUS string_copyn(addr+dom, end - dom) : CUS"";
+
+ (void) event_raise(event_action, US"msg:fail:internal", new->message, NULL);
+
+ deliver_localpart = save_local;
+ deliver_domain = save_domain;
+ }
+ }
+#endif
+ }
+
+DEBUG(D_deliver)
+ {
+ debug_printf("Delivery address list:\n");
+ for (address_item * p = addr_new; p; p = p->next)
+ debug_printf(" %s %s\n", p->address,
+ p->onetime_parent ? p->onetime_parent : US"");
+ }
+
+/* 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.
+*/
+
+f.header_rewritten = FALSE; /* No headers rewritten yet */
+while (addr_new) /* Loop until all addresses dealt with */
+ {
+ address_item *addr, *parent;
+
+ /* 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 = dbfn_open(US"retry", O_RDONLY, &dbblock, FALSE, TRUE)))
+ 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)
+ {
+ int rc;
+ tree_node * tnode;
+ dbdata_retry * domain_retry_record, * 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";
+ addr->prop.ignore_error = TRUE;
+ (void) post_process_one(addr, FAIL, LOG_MAIN, EXIM_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))
+ addr->unique = string_sprintf(">%s", addr->unique);
+
+ else if ((tnode = tree_search(tree_duplicates, addr->unique)))
+ {
+ 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))
+ {
+ 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, EXIM_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, EXIM_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, EXIM_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, EXIM_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**".
+ Copy the transport for this fairly unusual case rather than having
+ to make all transports mutable. */
+
+ if (Ustrcmp(addr->address, "/dev/null") == 0)
+ {
+ transport_instance * save_t = addr->transport;
+ transport_instance * t = store_get(sizeof(*t), save_t);
+ *t = *save_t;
+ t->name = US"**bypassed**";
+ addr->transport = t;
+ (void)post_process_one(addr, OK, LOG_MAIN, EXIM_DTYPE_TRANSPORT, '=');
+ addr->transport= save_t;
+ 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, EXIM_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
+ && (rc = match_isinlist(addr->domain, (const uschar **)&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, EXIM_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; 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)
+ {
+ 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. */
+
+ for (uschar * p = Ustrrchr(addr->unique, '@'); *p; p++) *p = tolower(*p);
+
+ DEBUG(D_deliver|D_route) debug_printf("unique = %s\n", addr->unique);
+
+ if (tree_search(tree_nonrecipients, addr->unique))
+ {
+ 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)
+ {
+ domain_retry_record = dbfn_read(dbm_file, addr->domain_retry_key);
+ if ( domain_retry_record
+ && now - domain_retry_record->time_stamp > retry_data_expire
+ )
+ {
+ DEBUG(D_deliver|D_retry)
+ debug_printf("domain retry record present but expired\n");
+ domain_retry_record = NULL; /* Ignore if too old */
+ }
+
+ address_retry_record = dbfn_read(dbm_file, addr->address_retry_key);
+ if ( address_retry_record
+ && now - address_retry_record->time_stamp > retry_data_expire
+ )
+ {
+ DEBUG(D_deliver|D_retry)
+ debug_printf("address retry record present but expired\n");
+ address_retry_record = NULL; /* Ignore if too old */
+ }
+
+ if (!address_retry_record)
+ {
+ uschar *altkey = string_sprintf("%s:<%s>", addr->address_retry_key,
+ sender_address);
+ address_retry_record = dbfn_read(dbm_file, altkey);
+ if ( address_retry_record
+ && now - address_retry_record->time_stamp > retry_data_expire)
+ {
+ DEBUG(D_deliver|D_retry)
+ debug_printf("address<sender> retry record present but expired\n");
+ address_retry_record = NULL; /* Ignore if too old */
+ }
+ }
+ }
+ else
+ domain_retry_record = address_retry_record = NULL;
+
+ DEBUG(D_deliver|D_retry)
+ {
+ if (!domain_retry_record)
+ debug_printf("no domain retry record\n");
+ else
+ debug_printf("have domain retry record; next_try = now%+d\n",
+ f.running_in_test_harness ? 0 :
+ (int)(domain_retry_record->next_try - now));
+
+ if (!address_retry_record)
+ debug_printf("no address retry record\n");
+ else
+ debug_printf("have address retry record; next_try = now%+d\n",
+ f.running_in_test_harness ? 0 :
+ (int)(address_retry_record->next_try - now));
+ }
+
+ /* 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 && domain_retry_record)
+ {
+ addr->message = US"reusing SMTP connection skips previous routing defer";
+ addr->basic_errno = ERRNO_RRETRY;
+ (void)post_process_one(addr, DEFER, LOG_MAIN, EXIM_DTYPE_ROUTER, 0);
+
+ addr->message = domain_retry_record->text;
+ setflag(addr, af_pass_message);
+ }
+
+ /* 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 ( ( f.queue_running && !f.deliver_force
+ || continue_hostname
+ )
+ && ( ( domain_retry_record
+ && now < domain_retry_record->next_try
+ && !domain_retry_record->expired
+ )
+ || ( address_retry_record
+ && now < address_retry_record->next_try
+ ) )
+ && ( domain_retry_record
+ || !address_retry_record
+ || !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, EXIM_DTYPE_ROUTER, 0);
+
+ /* For remote-retry errors (here and just above) that we've not yet
+ hit the retry time, use the error recorded in the retry database
+ as info in the warning message. This lets us send a message even
+ when we're not failing on a fresh attempt. We assume that this
+ info is not sensitive. */
+
+ addr->message = domain_retry_record
+ ? domain_retry_record->text : address_retry_record->text;
+ setflag(addr, af_pass_message);
+ }
+
+ /* 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 || address_retry_record)
+ 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) 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 (!f.deliver_force && queue_domains)
+ {
+ address_item *okaddr = NULL;
+ while (addr_route)
+ {
+ address_item *addr = addr_route;
+ addr_route = addr->next;
+
+ deliver_domain = addr->domain; /* set $domain */
+ if ((rc = match_isinlist(addr->domain, CUSS &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, EXIM_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, EXIM_DTYPE_ROUTER, 0);
+ }
+ }
+
+ addr_route = okaddr;
+ }
+
+ /* Now route those addresses that are not deferred. */
+
+ while (addr_route)
+ {
+ int rc;
+ address_item *addr = addr_route;
+ const 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. */
+
+ if (!(return_path = addr->prop.errors_address))
+ return_path = 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, EXIM_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->prop.extra_headers
+ && !addr->prop.remove_headers
+ && old_domain == addr->domain
+ )
+ {
+ address_item **chain = &addr_route;
+ while (*chain)
+ {
+ 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->prop.errors_address = addr->prop.errors_address;
+ copyflag(addr2, addr, af_hide_child);
+ copyflag(addr2, addr, 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)
+ {
+ debug_printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+ debug_printf("After routing:\n Local deliveries:\n");
+ for (address_item * p = addr_local; p; p = p->next)
+ debug_printf(" %s\n", p->address);
+
+ debug_printf(" Remote deliveries:\n");
+ for (address_item * p = addr_remote; p; p = p->next)
+ debug_printf(" %s\n", p->address);
+
+ debug_printf(" Failed addresses:\n");
+ for (address_item * p = addr_failed; p; p = p->next)
+ debug_printf(" %s\n", p->address);
+
+ debug_printf(" Deferred addresses:\n");
+ for (address_item * p = addr_defer; p; p = p->next)
+ debug_printf(" %s\n", p->address);
+ }
+
+/* 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 || addr_failed || addr_defer)
+ )
+ {
+ address_item *addr;
+ uschar *which, *colon, *msg;
+
+ if (addr_local)
+ {
+ addr = addr_local;
+ which = US"local";
+ }
+ else if (addr_defer)
+ {
+ addr = addr_defer;
+ which = US"deferred";
+ }
+ else
+ {
+ addr = addr_failed;
+ which = US"failed";
+ }
+
+ while (addr->parent) addr = addr->parent;
+
+ if (addr->message)
+ {
+ 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.
+
+jgh 2020/12/20: I don't see why; locals should be quick.
+The defer goes back to version 1.62 in 1997. A local being still deliverable
+during a continued run might result from something like a defer during the
+original delivery, eg. in a DB lookup. Unlikely but possible.
+
+To avoid delaying a local when combined with a callout-hold for a remote
+delivery, test continue_sequence rather than continue_transport. */
+
+if (continue_sequence > 1 && addr_local)
+ {
+ DEBUG(D_deliver|D_retry|D_route)
+ debug_printf("deferring local deliveries due to continued-transport\n");
+ if (addr_defer)
+ {
+ address_item * addr = addr_defer;
+ while (addr->next) addr = addr->next;
+ addr->next = addr_local;
+ }
+ else
+ addr_defer = 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 ( f.header_rewritten
+ && ( addr_local && (addr_local->next || addr_remote)
+ || addr_remote && addr_remote->next
+ ) )
+ {
+ /* Panic-dies on error */
+ (void)spool_write_header(message_id, SW_DELIVERING, NULL);
+ f.header_rewritten = FALSE;
+ }
+
+
+/* If there are any deliveries to do and we do not already have the journal
+file, create it. 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 || addr_remote)
+ {
+ if (journal_fd < 0)
+ {
+ uschar * fname = spool_fname(US"input", message_subdir, id, US"-J");
+
+ if ((journal_fd = Uopen(fname,
+#ifdef O_CLOEXEC
+ O_CLOEXEC |
+#endif
+ O_WRONLY|O_APPEND|O_CREAT|O_EXCL, SPOOL_MODE)) < 0)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't open journal file %s: %s",
+ fname, 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( exim_fchown(journal_fd, exim_uid, exim_gid, fname)
+ || fchmod(journal_fd, SPOOL_MODE)
+#ifndef O_CLOEXEC
+ || fcntl(journal_fd, F_SETFD, fcntl(journal_fd, F_GETFD) | FD_CLOEXEC)
+#endif
+ )
+ {
+ int ret = Uunlink(fname);
+ log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't set perms on journal file %s: %s",
+ fname, strerror(errno));
+ if(ret && errno != ENOENT)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s: %s",
+ fname, strerror(errno));
+ return DELIVER_NOT_ATTEMPTED;
+ }
+ }
+ }
+else if (journal_fd >= 0)
+ {
+ close(journal_fd);
+ journal_fd = -1;
+ }
+
+
+
+/* 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)
+ regex_IGNOREQUOTA =
+ regex_must_compile(US"\\n250[\\s\\-]IGNOREQUOTA(\\s|\\n|$)", FALSE, TRUE);
+
+/* Handle local deliveries */
+
+if (addr_local)
+ {
+ DEBUG(D_deliver|D_transport)
+ debug_printf(">>>>>>>>>>>>>>>> Local deliveries >>>>>>>>>>>>>>>>\n");
+ do_local_deliveries();
+ f.disable_logging = FALSE;
+ }
+
+/* If queue_run_local is set, we do not want to attempt any remote deliveries,
+so just queue them all. */
+
+if (f.queue_run_local)
+ while (addr_remote)
+ {
+ 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, EXIM_DTYPE_TRANSPORT, 0);
+ }
+
+/* Handle remote deliveries */
+
+if (addr_remote)
+ {
+ 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. */
+
+ smtp_deliver_init();
+
+ /* 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) 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 && !mua_wrapper)
+ {
+ DEBUG(D_deliver) debug_printf("Delivering to fallback hosts\n");
+ addr_remote = addr_fallback;
+ addr_fallback = NULL;
+ if (remote_sort_domains) sort_remote_deliveries();
+ do_remote_deliveries(TRUE);
+ }
+ f.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");
+cancel_cutthrough_connection(TRUE, US"deliveries are done");
+
+/* 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)
+ {
+ address_item * nextaddr;
+ for (address_item * addr = addr_defer; addr; 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)
+ final_yield = DELIVER_MUA_SUCCEEDED;
+ else
+ {
+ host_item * host;
+ uschar *s = addr_failed->user_message;
+
+ if (!s) s = addr_failed->message;
+
+ fprintf(stderr, "Delivery failed: ");
+ if (addr_failed->basic_errno > 0)
+ {
+ fprintf(stderr, "%s", strerror(addr_failed->basic_errno));
+ if (s) fprintf(stderr, ": ");
+ }
+ if ((host = addr_failed->host_used))
+ fprintf(stderr, "H=%s [%s]: ", host->name, host->address);
+ if (s)
+ fprintf(stderr, "%s", CS s);
+ else if (addr_failed->basic_errno <= 0)
+ fprintf(stderr, "unknown error");
+ 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 (!f.dont_deliver)
+ retry_update(&addr_defer, &addr_failed, &addr_succeed);
+
+/* Send DSN for successful messages if requested */
+addr_senddsn = NULL;
+
+for (address_item * a = addr_succeed; a; a = a->next)
+ {
+ /* af_ignore_error not honored here. it's not an error */
+ DEBUG(D_deliver) debug_printf("DSN: processing router : %s\n"
+ "DSN: processing successful delivery address: %s\n"
+ "DSN: Sender_address: %s\n"
+ "DSN: orcpt: %s flags: 0x%x\n"
+ "DSN: envid: %s ret: %d\n"
+ "DSN: Final recipient: %s\n"
+ "DSN: Remote SMTP server supports DSN: %d\n",
+ a->router ? a->router->name : US"(unknown)",
+ a->address,
+ sender_address,
+ a->dsn_orcpt ? a->dsn_orcpt : US"NULL",
+ a->dsn_flags,
+ dsn_envid ? dsn_envid : US"NULL", dsn_ret,
+ a->address,
+ a->dsn_aware
+ );
+
+ /* send report if next hop not DSN aware or a router flagged "last DSN hop"
+ and a report was requested */
+
+ if ( (a->dsn_aware != dsn_support_yes || a->dsn_flags & rf_dsnlasthop)
+ && a->dsn_flags & rf_notify_success
+ )
+ {
+ /* copy and relink address_item and send report with all of them at once later */
+ address_item * addr_next = addr_senddsn;
+ addr_senddsn = store_get(sizeof(address_item), GET_UNTAINTED);
+ *addr_senddsn = *a;
+ addr_senddsn->next = addr_next;
+ }
+ else
+ DEBUG(D_deliver) debug_printf("DSN: not sending DSN success message\n");
+ }
+
+if (addr_senddsn)
+ {
+ pid_t pid;
+ int fd;
+
+ /* create exim process to send message */
+ pid = child_open_exim(&fd, US"DSN");
+
+ 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 success-dsn 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 */
+ uschar * bound;
+ transport_ctx tctx = {{0}};
+
+ DEBUG(D_deliver)
+ debug_printf("sending success-dsn to: %s\n", sender_address);
+
+ /* build unique id for MIME boundary */
+ bound = string_sprintf(TIME_T_FMT "-eximdsn-%d", time(NULL), rand());
+ DEBUG(D_deliver) debug_printf("DSN: MIME boundary: %s\n", bound);
+
+ if (errors_reply_to)
+ fprintf(f, "Reply-To: %s\n", errors_reply_to);
+
+ moan_write_from(f);
+ fprintf(f, "Auto-Submitted: auto-generated\n"
+ "To: %s\n"
+ "Subject: Delivery Status Notification\n",
+ sender_address);
+ moan_write_references(f, NULL);
+ fprintf(f, "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",
+ bound, bound);
+
+ for (address_item * a = addr_senddsn; a; a = a->next)
+ fprintf(f, "<%s> (relayed %s)\n\n",
+ a->address,
+ a->dsn_flags & rf_dsnlasthop ? "via non DSN router"
+ : a->dsn_aware == dsn_support_no ? "to non-DSN-aware mailer"
+ : "via non \"Remote SMTP\" router"
+ );
+
+ fprintf(f, "--%s\n"
+ "Content-type: message/delivery-status\n\n"
+ "Reporting-MTA: dns; %s\n",
+ bound, 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 formatted ENVID\n");
+ }
+ fputc('\n', f);
+
+ for (address_item * a = addr_senddsn; a; a = a->next)
+ {
+ host_item * hu;
+
+ print_dsn_addr_action(f, a, US"delivered", US"2.0.0");
+
+ if ((hu = a->host_used) && hu->name)
+ fprintf(f, "Remote-MTA: dns; %s\nDiagnostic-Code: smtp; 250 Ok\n\n",
+ hu->name);
+ else
+ fprintf(f, "Diagnostic-Code: X-Exim; relayed via non %s router\n\n",
+ a->dsn_flags & rf_dsnlasthop ? "DSN" : "SMTP");
+ }
+
+ fprintf(f, "--%s\nContent-type: text/rfc822-headers\n\n", bound);
+
+ fflush(f);
+ transport_filter_argv = NULL; /* Just in case */
+ return_path = sender_address; /* In case not previously set */
+
+ /* Write the original email out */
+
+ tctx.u.fd = fd;
+ tctx.options = topt_add_return_path | topt_no_body;
+ /*XXX hmm, FALSE(fail) retval ignored.
+ Could error for any number of reasons, and they are not handled. */
+ transport_write_message(&tctx, 0);
+ fflush(f);
+
+ fprintf(f,"\n--%s--\n", bound);
+
+ fflush(f);
+ fclose(f);
+ rc = child_close(pid, 0); /* Waits for child to close, no timeout */
+ }
+ }
+
+/* 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)
+ {
+ 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). */
+
+ f.disable_logging = FALSE;
+ if (addr_failed->transport)
+ f.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->prop.errors_address)
+ {
+ if ( !testflag(addr_failed, af_retry_timedout)
+ && !addr_failed->prop.ignore_error)
+ log_write(0, LOG_MAIN|LOG_PANIC, "internal error: bounce message "
+ "failure is neither frozen nor ignored (it's been ignored)");
+
+ addr_failed->prop.ignore_error = TRUE;
+ }
+
+ /* 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 ( addr_failed->prop.ignore_error
+ || addr_failed->dsn_flags & rf_dsnflags
+ && !(addr_failed->dsn_flags & rf_notify_failure)
+ )
+ {
+ addr = addr_failed;
+ addr_failed = addr->next;
+ if (addr->return_filename) Uunlink(addr->return_filename);
+
+#ifndef DISABLE_EVENT
+ msg_event_raise(US"msg:fail:delivery", addr);
+#endif
+ log_write(0, LOG_MAIN, "%s%s%s%s: error ignored%s",
+ addr->address,
+ !addr->parent ? US"" : US" <",
+ !addr->parent ? US"" : addr->parent->address,
+ !addr->parent ? US"" : US">",
+ addr->prop.ignore_error
+ ? US"" : US": RFC 3461 DSN, failure notify not requested");
+
+ 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 accessed by $bounce_recipient while creating a customized
+ error message. */
+
+ else
+ {
+ if (!(bounce_recipient = addr_failed->prop.errors_address))
+ bounce_recipient = sender_address;
+
+ /* Make a subprocess to send a message */
+
+ if ((pid = child_open_exim(&fd, US"bounce-message")) < 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 * fp = 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;
+ uschar * bound;
+ uschar *dsnlimitmsg;
+ uschar *dsnnotifyhdr;
+ int topt;
+
+ 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; addr = *paddr)
+ if (Ustrcmp(bounce_recipient, addr->prop.errors_address
+ ? addr->prop.errors_address : sender_address) == 0)
+ { /* The same - dechain */
+ *paddr = addr->next;
+ *pmsgchain = addr;
+ addr->next = NULL;
+ pmsgchain = &(addr->next);
+ }
+ else
+ paddr = &addr->next; /* Not the same; skip */
+
+ /* 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; addr = addr->next)
+ {
+ if (testflag(addr, af_hide_child)) continue;
+ if (rcount >= 50)
+ {
+ fprintf(fp, "\n");
+ rcount = 0;
+ }
+ fprintf(fp, "%s%s",
+ rcount++ == 0
+ ? "X-Failed-Recipients: "
+ : ",\n ",
+ testflag(addr, af_pfr) && addr->parent
+ ? string_printing(addr->parent->address)
+ : string_printing(addr->address));
+ }
+ if (rcount > 0) fprintf(fp, "\n");
+
+ /* Output the standard headers */
+
+ if (errors_reply_to)
+ fprintf(fp, "Reply-To: %s\n", errors_reply_to);
+ fprintf(fp, "Auto-Submitted: auto-replied\n");
+ moan_write_from(fp);
+ fprintf(fp, "To: %s\n", bounce_recipient);
+ moan_write_references(fp, NULL);
+
+ /* generate boundary string and output MIME-Headers */
+ bound = string_sprintf(TIME_T_FMT "-eximdsn-%d", time(NULL), rand());
+
+ fprintf(fp, "Content-Type: multipart/report;"
+ " report-type=delivery-status; boundary=%s\n"
+ "MIME-Version: 1.0\n",
+ bound);
+
+ /* Open a template file if one is provided. Log failure to open, but
+ carry on - default texts will be used. */
+
+ if (bounce_message_file)
+ emf = expand_open(bounce_message_file,
+ US"bounce_message_file", US"error");
+
+ /* Quietly copy to configured additional addresses if required. */
+
+ if ((bcc = moan_check_errorcopy(bounce_recipient)))
+ fprintf(fp, "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(fp, "%s\n", emf_text);
+ else
+ fprintf(fp, "Subject: Mail delivery failed%s\n\n",
+ to_sender? ": returning message to sender" : "");
+
+ /* output human readable part as text/plain section */
+ fprintf(fp, "--%s\n"
+ "Content-type: text/plain; charset=us-ascii\n\n",
+ bound);
+
+ if ((emf_text = next_emf(emf, US"intro")))
+ fprintf(fp, "%s", CS emf_text);
+ else
+ {
+ fprintf(fp,
+/* 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(fp, "%s", CS bounce_message_text);
+ if (to_sender)
+ fprintf(fp,
+"\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(fp,
+"\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', fp);
+
+ /* 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; addr = *paddr)
+ {
+ if (print_address_information(addr, fp, US" ", US"\n ", US""))
+ print_address_error(addr, fp, US"");
+
+ /* End the final line for the address */
+
+ fputc('\n', fp);
+
+ /* 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', fp);
+
+ /* 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(fp, "%s", CS emf_text);
+ else
+ fprintf(fp,
+ "The following text was generated during the delivery "
+ "attempt%s:\n", (filecount > 1)? "s" : "");
+
+ for (addr = msgchain; addr; addr = nextaddr)
+ {
+ FILE *fm;
+ address_item *topaddr = addr;
+
+ /* List all the addresses that relate to this file */
+
+ fputc('\n', fp);
+ while(addr) /* Insurance */
+ {
+ print_address_information(addr, fp, US"------ ", US"\n ",
+ US" ------\n");
+ if (addr->return_filename) break;
+ addr = addr->next;
+ }
+ fputc('\n', fp);
+
+ /* Now copy the file */
+
+ if (!(fm = Ufopen(addr->return_filename, "rb")))
+ fprintf(fp, " +++ Exim error... failed to open text file: %s\n",
+ strerror(errno));
+ else
+ {
+ while ((ch = fgetc(fm)) != EOF) fputc(ch, fp);
+ (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', fp);
+ }
+
+ /* output machine readable part */
+#ifdef SUPPORT_I18N
+ if (message_smtputf8)
+ fprintf(fp, "--%s\n"
+ "Content-type: message/global-delivery-status\n\n"
+ "Reporting-MTA: dns; %s\n",
+ bound, smtp_active_hostname);
+ else
+#endif
+ fprintf(fp, "--%s\n"
+ "Content-type: message/delivery-status\n\n"
+ "Reporting-MTA: dns; %s\n",
+ bound, 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(fp, "Original-Envelope-ID: %s\n", dsn_envid);
+ else
+ fprintf(fp, "X-Original-Envelope-ID: error decoding xtext formatted ENVID\n");
+ }
+ fputc('\n', fp);
+
+ for (addr = handled_addr; addr; addr = addr->next)
+ {
+ host_item * hu;
+
+ print_dsn_addr_action(fp, addr, US"failed", US"5.0.0");
+
+ if ((hu = addr->host_used) && hu->name)
+ {
+ fprintf(fp, "Remote-MTA: dns; %s\n", hu->name);
+#ifdef EXPERIMENTAL_DSN_INFO
+ {
+ const uschar * s;
+ if (hu->address)
+ {
+ uschar * p = hu->port == 25
+ ? US"" : string_sprintf(":%d", hu->port);
+ fprintf(fp, "Remote-MTA: X-ip; [%s]%s\n", hu->address, p);
+ }
+ if ((s = addr->smtp_greeting) && *s)
+ fprintf(fp, "X-Remote-MTA-smtp-greeting: X-str; %.900s\n", s);
+ if ((s = addr->helo_response) && *s)
+ fprintf(fp, "X-Remote-MTA-helo-response: X-str; %.900s\n", s);
+ if ((s = addr->message) && *s)
+ fprintf(fp, "X-Exim-Diagnostic: X-str; %.900s\n", s);
+ }
+#endif
+ print_dsn_diagnostic_code(addr, fp);
+ }
+ fputc('\n', fp);
+ }
+
+ /* 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");
+
+ /* 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(fp, "--%s\n", bound);
+
+ 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
+ {
+ struct stat statbuf;
+
+ /* 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;
+ }
+ /* line length limited... return headers only if oversize */
+ /* size limited ... return headers only if limit reached */
+ else if ( max_received_linelength > bounce_return_linesize_limit
+ || ( bounce_return_size_limit > 0
+ && fstat(deliver_datafile, &statbuf) == 0
+ && statbuf.st_size > max
+ ) )
+ {
+ topt |= topt_no_body;
+ dsnnotifyhdr = dsnlimitmsg;
+ }
+ }
+
+#ifdef SUPPORT_I18N
+ if (message_smtputf8)
+ fputs(topt & topt_no_body ? "Content-type: message/global-headers\n\n"
+ : "Content-type: message/global\n\n",
+ fp);
+ else
+#endif
+ fputs(topt & topt_no_body ? "Content-type: text/rfc822-headers\n\n"
+ : "Content-type: message/rfc822\n\n",
+ fp);
+
+ fflush(fp);
+ transport_filter_argv = NULL; /* Just in case */
+ return_path = sender_address; /* In case not previously set */
+ { /* Dummy transport for headers add */
+ transport_ctx tctx = {{0}};
+ transport_instance tb = {0};
+
+ tctx.u.fd = fileno(fp);
+ tctx.tblock = &tb;
+ tctx.options = topt;
+ tb.add_headers = dsnnotifyhdr;
+
+ /*XXX no checking for failure! buggy! */
+ transport_write_message(&tctx, 0);
+ }
+ fflush(fp);
+
+ /* we never add the final text. close the file */
+ if (emf)
+ (void)fclose(emf);
+
+ fprintf(fp, "\n--%s--\n", bound);
+
+ /* Close the file, which should send an EOF to the child process
+ that is receiving the message. Wait for it to finish. */
+
+ (void)fclose(fp);
+ rc = child_close(pid, 0); /* Waits for child to close, no timeout */
+
+ /* 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.tv_sec < retry_maximum_timeout && !addr_defer)
+ {
+ addr_defer = (address_item *)(+1);
+ f.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; addr = addr->next)
+ {
+ address_done(addr, logtod);
+ child_done(addr, logtod);
+ }
+ /* Panic-dies on error */
+ (void)spool_write_header(message_id, SW_DELIVERING, NULL);
+ }
+ }
+ }
+ }
+
+f.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)
+ {
+ uschar * fname;
+
+ if (message_logs)
+ {
+ fname = spool_fname(US"msglog", message_subdir, id, US"");
+ if (preserve_message_logs)
+ {
+ int rc;
+ uschar * moname = spool_fname(US"msglog.OLD", US"", id, US"");
+
+ if ((rc = Urename(fname, moname)) < 0)
+ {
+ (void)directory_make(spool_directory,
+ spool_sname(US"msglog.OLD", US""),
+ MSGLOG_DIRECTORY_MODE, TRUE);
+ rc = Urename(fname, moname);
+ }
+ if (rc < 0)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to move %s to the "
+ "msglog.OLD directory", fname);
+ }
+ else
+ if (Uunlink(fname) < 0)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s: %s",
+ fname, strerror(errno));
+ }
+
+ /* Remove the two message files. */
+
+ fname = spool_fname(US"input", message_subdir, id, US"-D");
+ if (Uunlink(fname) < 0)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s: %s",
+ fname, strerror(errno));
+ fname = spool_fname(US"input", message_subdir, id, US"-H");
+ if (Uunlink(fname) < 0)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s: %s",
+ fname, strerror(errno));
+
+ /* Log the end of this message, with queue time if requested. */
+
+ if (LOGGING(queue_time_overall))
+ log_write(0, LOG_MAIN, "Completed QT=%s", string_timesince(&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 */
+ f.deliver_freeze = FALSE;
+
+#ifndef DISABLE_EVENT
+ (void) event_raise(event_action, US"msg:complete", NULL, NULL);
+#endif
+ }
+
+/* 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.
+ Exception: for retries caused by a remote peer we use the error message
+ store in the retry DB as the reason.
+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))
+ {
+ uschar *recipients = US"";
+ BOOL want_warning_msg = FALSE;
+
+ deliver_domain = testflag(addr_defer, af_pfr)
+ ? addr_defer->parent->domain : addr_defer->domain;
+
+ for (address_item * addr = addr_defer; addr; addr = addr->next)
+ {
+ address_item *otaddr;
+
+ if (addr->basic_errno > ERRNO_WARN_BASE) want_warning_msg = TRUE;
+
+ if (deliver_domain)
+ {
+ const 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 || Ustrcmp(d, deliver_domain) != 0)
+ deliver_domain = NULL;
+ }
+
+ if (addr->return_filename) 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; otaddr = otaddr->parent)
+ if (otaddr->onetime_parent) break;
+
+ if (otaddr)
+ {
+ 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, and they really are different
+ (i.e. not from an identity-redirect). After adding the recipient,
+ update the errors address in the recipients list. */
+
+ if ( i >= recipients_count && t < recipients_count
+ && Ustrcmp(otaddr->address, otaddr->parent->address) != 0)
+ {
+ 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->prop.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])
+ {
+ uschar * s = addr->prop.errors_address;
+ if (!s) s = sender_address;
+ if (Ustrstr(recipients, s) == NULL)
+ recipients = string_sprintf("%s%s%s", recipients,
+ recipients[0] ? "," : "", s);
+ }
+ }
+
+ /* 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 ( !f.queue_2stage
+ && want_warning_msg
+ && ( !(addr_defer->dsn_flags & rf_dsnflags)
+ || addr_defer->dsn_flags & rf_notify_delay
+ )
+ && delay_warning[1] > 0
+ && sender_address[0] != 0
+ && ( !delay_warning_condition
+ || expand_check_condition(delay_warning_condition,
+ US"delay_warning", US"option")
+ )
+ )
+ {
+ int count;
+ int show_time;
+ int queue_time = time(NULL) - received_time.tv_sec;
+
+ queue_time = test_harness_fudged_queue_time(queue_time);
+
+ /* 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 id %s addr %s\n",
+ readconf_printtime(queue_time), message_id, addr_defer->address);
+ 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, US"delay-warning-message");
+
+ if (pid > 0)
+ {
+ uschar * wmf_text;
+ FILE * wmf = NULL;
+ FILE * f = fdopen(fd, "wb");
+ uschar * bound;
+ transport_ctx tctx = {{0}};
+
+ if (warn_message_file)
+ wmf = expand_open(warn_message_file,
+ US"warn_message_file", US"warning");
+
+ 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);
+ moan_write_references(f, NULL);
+
+ /* generated boundary string and output MIME-Headers */
+ bound = string_sprintf(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",
+ bound);
+
+ 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);
+
+ /* output human readable part as text/plain section */
+ fprintf(f, "--%s\n"
+ "Content-type: text/plain; charset=us-ascii\n\n",
+ bound);
+
+ 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; 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 */
+
+ fputc('\n', f);
+ for (address_item * addr = addr_defer; addr; addr = 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");
+ }
+
+ /* output machine readable part */
+ fprintf(f, "\n--%s\n"
+ "Content-type: message/delivery-status\n\n"
+ "Reporting-MTA: dns; %s\n",
+ bound,
+ 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 formatted ENVID\n");
+ }
+ fputc('\n', f);
+
+ for (address_item * addr = addr_defer; addr; addr = addr->next)
+ {
+ host_item * hu;
+
+ print_dsn_addr_action(f, addr, US"delayed", US"4.0.0");
+
+ if ((hu = addr->host_used) && hu->name)
+ {
+ fprintf(f, "Remote-MTA: dns; %s\n", hu->name);
+ print_dsn_diagnostic_code(addr, f);
+ }
+ fputc('\n', f);
+ }
+
+ fprintf(f, "--%s\n"
+ "Content-type: text/rfc822-headers\n\n",
+ bound);
+
+ fflush(f);
+ /* header only as required by RFC. only failure DSN needs to honor RET=FULL */
+ tctx.u.fd = fileno(f);
+ tctx.options = 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 */
+ /*XXX no checking for failure! buggy! */
+ transport_write_message(&tctx, 0);
+ fflush(f);
+
+ fprintf(f,"\n--%s--\n", bound);
+
+ fflush(f);
+
+ /* 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 (f.deliver_firsttime && !f.queue_2stage)
+ {
+ f.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 (f.deliver_freeze)
+ {
+ if (freeze_tell && freeze_tell[0] != 0 && !f.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, f.header_rewritten);
+
+ if (update_spool || f.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)
+ {
+ uschar * fname = spool_fname(US"input", message_subdir, id, US"-J");
+
+ if (Uunlink(fname) < 0 && errno != ENOENT)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s: %s", fname,
+ strerror(errno));
+
+ /* Move the message off the spool if requested */
+
+#ifdef SUPPORT_MOVE_FROZEN_MESSAGES
+ if (f.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);
+#ifdef MEASURE_TIMING
+report_time_since(&timestamp_startup, US"delivery end"); /* testcase 0005 */
+#endif
+
+/* 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;
+}
+
+
+
+void
+tcp_init(void)
+{
+#ifdef EXIM_TFO_PROBE
+tfo_probe();
+#else
+f.tcp_fastopen_ok = TRUE;
+#endif
+}
+
+
+
+/* Called from a commandline, or from the daemon, to do a delivery.
+We need to regain privs; do this by exec of the exim binary. */
+
+void
+delivery_re_exec(int exec_type)
+{
+uschar * where;
+
+if (cutthrough.cctx.sock >= 0 && cutthrough.callout_hold_only)
+ {
+ int channel_fd = cutthrough.cctx.sock;
+
+ smtp_peer_options = cutthrough.peer_options;
+ continue_sequence = 0;
+
+#ifndef DISABLE_TLS
+ if (cutthrough.is_tls)
+ {
+ int pfd[2], pid;
+
+ smtp_peer_options |= OPTION_TLS;
+ sending_ip_address = cutthrough.snd_ip;
+ sending_port = cutthrough.snd_port;
+
+ where = US"socketpair";
+ if (socketpair(AF_UNIX, SOCK_STREAM, 0, pfd) != 0)
+ goto fail;
+
+ where = US"fork";
+ testharness_pause_ms(150);
+ if ((pid = exim_fork(US"tls-proxy-interproc")) < 0)
+ goto fail;
+
+ if (pid == 0) /* child: will fork again to totally disconnect */
+ {
+ smtp_proxy_tls(cutthrough.cctx.tls_ctx, big_buffer, big_buffer_size,
+ pfd, 5*60, cutthrough.host.name);
+ /* does not return */
+ }
+
+ close(pfd[0]);
+ waitpid(pid, NULL, 0);
+ (void) close(channel_fd); /* release the client socket */
+ channel_fd = pfd[1];
+ }
+#endif
+
+ transport_do_pass_socket(cutthrough.transport, cutthrough.host.name,
+ cutthrough.host.address, message_id, channel_fd);
+ }
+else
+ {
+ cancel_cutthrough_connection(TRUE, US"non-continued delivery");
+ (void) child_exec_exim(exec_type, FALSE, NULL, FALSE, 2, US"-Mc", message_id);
+ }
+return; /* compiler quietening; control does not reach here. */
+
+#ifndef DISABLE_TLS
+fail:
+ log_write(0,
+ LOG_MAIN | (exec_type == CEE_EXEC_EXIT ? LOG_PANIC : LOG_PANIC_DIE),
+ "delivery re-exec %s failed: %s", where, strerror(errno));
+
+ /* Get here if exec_type == CEE_EXEC_EXIT.
+ Note: this must be _exit(), not exit(). */
+
+ _exit(EX_EXECFAILED);
+#endif
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of deliver.c */
diff --git a/src/directory.c b/src/directory.c
new file mode 100644
index 0000000..1890208
--- /dev/null
+++ b/src/directory.c
@@ -0,0 +1,95 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2010 - 2022 */
+/* 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;
+uschar c = 1;
+struct stat statbuf;
+uschar * path;
+
+if (is_tainted(name))
+ { p = US"create"; path = US name; errno = ERRNO_TAINT; goto bad; }
+
+if (parent)
+ {
+ path = string_sprintf("%s%s%s", parent, US"/", name);
+ p = path + Ustrlen(parent);
+ }
+else
+ {
+ path = string_copy(name);
+ p = path + 1;
+ }
+
+/* Walk the path creating any missing directories */
+
+while (c && *p)
+ {
+ while (*p && *p != '/') p++;
+ c = *p;
+ *p = '\0';
+ if (Ustat(path, &statbuf) != 0)
+ {
+ if (mkdir(CS path, mode) < 0 && errno != EEXIST)
+ { p = US"create"; goto bad; }
+
+ /* Set the ownership if necessary. */
+
+ if (use_chown && exim_chown(path, exim_uid, exim_gid))
+ { p = US"set owner on"; goto bad; }
+
+ /* 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) (void) Uchmod(path, mode);
+ }
+ *p++ = c;
+ }
+
+return TRUE;
+
+bad:
+ if (panic) log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "Failed to %s directory \"%s\": %s\n", p, path, exim_errstr(errno));
+ return FALSE;
+}
+
+/* End of directory.c */
diff --git a/src/dkim.c b/src/dkim.c
new file mode 100644
index 0000000..bb916d2
--- /dev/null
+++ b/src/dkim.c
@@ -0,0 +1,895 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge, 1995 - 2018 */
+/* 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"
+
+# ifdef MACRO_PREDEF
+# include "macro_predef.h"
+
+void
+params_dkim(void)
+{
+builtin_macro_create_var(US"_DKIM_SIGN_HEADERS", US PDKIM_DEFAULT_SIGN_HEADERS);
+builtin_macro_create_var(US"_DKIM_OVERSIGN_HEADERS", US PDKIM_OVERSIGN_HEADERS);
+}
+# else /*!MACRO_PREDEF*/
+
+
+
+pdkim_ctx dkim_sign_ctx;
+
+int dkim_verify_oldpool;
+pdkim_ctx *dkim_verify_ctx = NULL;
+pdkim_signature *dkim_cur_sig = NULL;
+static const uschar * dkim_collect_error = NULL;
+
+#define DKIM_MAX_SIGNATURES 20
+
+
+
+/* Look up the DKIM record in DNS for the given hostname.
+Will use the first found if there are multiple.
+The return string is tainted, having come from off-site.
+*/
+
+uschar *
+dkim_exim_query_dns_txt(const uschar * name)
+{
+dns_answer * dnsa = store_get_dns_answer();
+dns_scan dnss;
+rmark reset_point = store_mark();
+gstring * g = string_get_tainted(256, GET_TAINTED);
+
+lookup_dnssec_authenticated = NULL;
+if (dns_lookup(dnsa, name, T_TXT, NULL) != DNS_SUCCEED)
+ goto bad;
+
+/* Search for TXT record */
+
+for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS);
+ rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT))
+ if (rr->type == T_TXT)
+ { /* Copy record content to the answer buffer */
+ for (int rr_offset = 0; rr_offset < rr->size; )
+ {
+ uschar len = rr->data[rr_offset++];
+
+ g = string_catn(g, US(rr->data + rr_offset), len);
+ if (g->ptr >= PDKIM_DNS_TXT_MAX_RECLEN)
+ goto bad;
+
+ rr_offset += len;
+ }
+
+ /* Check if this looks like a DKIM record */
+ if (Ustrncmp(g->s, "v=", 2) != 0 || strncasecmp(CS g->s, "v=dkim", 6) == 0)
+ {
+ store_free_dns_answer(dnsa);
+ gstring_release_unused(g);
+ return string_from_gstring(g);
+ }
+
+ g->ptr = 0; /* overwrite previous record */
+ }
+
+bad:
+store_reset(reset_point);
+store_free_dns_answer(dnsa);
+return NULL; /*XXX better error detail? logging? */
+}
+
+
+void
+dkim_exim_init(void)
+{
+if (f.dkim_init_done) return;
+f.dkim_init_done = TRUE;
+pdkim_init();
+}
+
+
+
+void
+dkim_exim_verify_init(BOOL dot_stuffing)
+{
+dkim_exim_init();
+
+/* There is a store-reset between header & body reception for the main pool
+(actually, after every header line) so cannot use that as we need the data we
+store per-header, during header processing, at the end of body reception
+for evaluating the signature. Any allocs done for dkim verify
+memory-handling must use a different pool. We use a separate one that we
+can reset per message. */
+
+dkim_verify_oldpool = store_pool;
+store_pool = POOL_MESSAGE;
+
+/* 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(&dkim_exim_query_dns_txt, dot_stuffing);
+dkim_collect_input = dkim_verify_ctx ? DKIM_MAX_SIGNATURES : 0;
+dkim_collect_error = NULL;
+
+/* Start feed up with any cached data, but limited to message data */
+receive_get_cache(chunking_state == CHUNKING_LAST
+ ? chunking_data_left : GETC_BUFFER_UNLIMITED);
+
+store_pool = dkim_verify_oldpool;
+}
+
+
+/* Submit a chunk of data for verification input.
+Only use the data when the feed is activated. */
+void
+dkim_exim_verify_feed(uschar * data, int len)
+{
+int rc;
+
+store_pool = POOL_MESSAGE;
+if ( dkim_collect_input
+ && (rc = pdkim_feed(dkim_verify_ctx, data, len)) != PDKIM_OK)
+ {
+ dkim_collect_error = pdkim_errstr(rc);
+ log_write(0, LOG_MAIN,
+ "DKIM: validation error: %.100s", dkim_collect_error);
+ dkim_collect_input = 0;
+ }
+store_pool = dkim_verify_oldpool;
+}
+
+
+/* Log the result for the given signature */
+static void
+dkim_exim_verify_log_sig(pdkim_signature * sig)
+{
+gstring * logmsg;
+uschar * s;
+
+if (!sig) return;
+
+/* Remember the domain for the first pass result */
+
+if ( !dkim_verify_overall
+ && dkim_verify_status
+ ? Ustrcmp(dkim_verify_status, US"pass") == 0
+ : sig->verify_status == PDKIM_VERIFY_PASS
+ )
+ dkim_verify_overall = string_copy(sig->domain);
+
+/* Rewrite the sig result if the ACL overrode it. This is only
+needed because the DMARC code (sigh) peeks at the dkim sigs.
+Mark the sig for this having been done. */
+
+if ( dkim_verify_status
+ && ( dkim_verify_status != dkim_exim_expand_query(DKIM_VERIFY_STATUS)
+ || dkim_verify_reason != dkim_exim_expand_query(DKIM_VERIFY_REASON)
+ ) )
+ { /* overridden by ACL */
+ sig->verify_ext_status = -1;
+ if (Ustrcmp(dkim_verify_status, US"fail") == 0)
+ sig->verify_status = PDKIM_VERIFY_POLICY | PDKIM_VERIFY_FAIL;
+ else if (Ustrcmp(dkim_verify_status, US"invalid") == 0)
+ sig->verify_status = PDKIM_VERIFY_POLICY | PDKIM_VERIFY_INVALID;
+ else if (Ustrcmp(dkim_verify_status, US"none") == 0)
+ sig->verify_status = PDKIM_VERIFY_POLICY | PDKIM_VERIFY_NONE;
+ else if (Ustrcmp(dkim_verify_status, US"pass") == 0)
+ sig->verify_status = PDKIM_VERIFY_POLICY | PDKIM_VERIFY_PASS;
+ else
+ sig->verify_status = -1;
+ }
+
+if (!LOGGING(dkim_verbose)) return;
+
+
+logmsg = string_catn(NULL, US"DKIM: ", 6);
+if (!(s = sig->domain)) s = US"<UNSET>";
+logmsg = string_append(logmsg, 2, "d=", s);
+if (!(s = sig->selector)) s = US"<UNSET>";
+logmsg = string_append(logmsg, 2, " s=", s);
+logmsg = string_fmt_append(logmsg, " c=%s/%s a=%s b=" SIZE_T_FMT,
+ sig->canon_headers == PDKIM_CANON_SIMPLE ? "simple" : "relaxed",
+ sig->canon_body == PDKIM_CANON_SIMPLE ? "simple" : "relaxed",
+ dkim_sig_to_a_tag(sig),
+ (int)sig->sighash.len > -1 ? sig->sighash.len * 8 : (size_t)0);
+if ((s= sig->identity)) logmsg = string_append(logmsg, 2, " i=", s);
+if (sig->created > 0) logmsg = string_fmt_append(logmsg, " t=%lu",
+ sig->created);
+if (sig->expires > 0) logmsg = string_fmt_append(logmsg, " x=%lu",
+ sig->expires);
+if (sig->bodylength > -1) logmsg = string_fmt_append(logmsg, " l=%lu",
+ sig->bodylength);
+
+if (sig->verify_status & PDKIM_VERIFY_POLICY)
+ logmsg = string_append(logmsg, 5,
+ US" [", dkim_verify_status, US" - ", dkim_verify_reason, US"]");
+else
+ switch (sig->verify_status)
+ {
+ case PDKIM_VERIFY_NONE:
+ logmsg = string_cat(logmsg, US" [not verified]");
+ break;
+
+ case PDKIM_VERIFY_INVALID:
+ logmsg = string_cat(logmsg, US" [invalid - ");
+ switch (sig->verify_ext_status)
+ {
+ case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE:
+ logmsg = string_cat(logmsg,
+ US"public key record (currently?) unavailable]");
+ break;
+
+ case PDKIM_VERIFY_INVALID_BUFFER_SIZE:
+ logmsg = string_cat(logmsg, US"overlong public key record]");
+ break;
+
+ case PDKIM_VERIFY_INVALID_PUBKEY_DNSRECORD:
+ case PDKIM_VERIFY_INVALID_PUBKEY_IMPORT:
+ logmsg = string_cat(logmsg, US"syntax error in public key record]");
+ break;
+
+ case PDKIM_VERIFY_INVALID_SIGNATURE_ERROR:
+ logmsg = string_cat(logmsg, US"signature tag missing or invalid]");
+ break;
+
+ case PDKIM_VERIFY_INVALID_DKIM_VERSION:
+ logmsg = string_cat(logmsg, US"unsupported DKIM version]");
+ break;
+
+ default:
+ logmsg = string_cat(logmsg, US"unspecified problem]");
+ }
+ break;
+
+ case PDKIM_VERIFY_FAIL:
+ logmsg = string_cat(logmsg, US" [verification failed - ");
+ switch (sig->verify_ext_status)
+ {
+ case PDKIM_VERIFY_FAIL_BODY:
+ logmsg = string_cat(logmsg,
+ US"body hash mismatch (body probably modified in transit)]");
+ break;
+
+ case PDKIM_VERIFY_FAIL_MESSAGE:
+ logmsg = string_cat(logmsg,
+ US"signature did not verify "
+ "(headers probably modified in transit)]");
+ break;
+
+ case PDKIM_VERIFY_INVALID_PUBKEY_KEYSIZE:
+ logmsg = string_cat(logmsg,
+ US"signature invalid (key too short)]");
+ break;
+
+ default:
+ logmsg = string_cat(logmsg, US"unspecified reason]");
+ }
+ break;
+
+ case PDKIM_VERIFY_PASS:
+ logmsg = string_cat(logmsg, US" [verification succeeded]");
+ break;
+ }
+
+log_write(0, LOG_MAIN, "%s", string_from_gstring(logmsg));
+return;
+}
+
+
+/* Log a line for each signature */
+void
+dkim_exim_verify_log_all(void)
+{
+for (pdkim_signature * sig = dkim_signatures; sig; sig = sig->next)
+ dkim_exim_verify_log_sig(sig);
+}
+
+
+void
+dkim_exim_verify_finish(void)
+{
+int rc;
+gstring * g = NULL;
+const uschar * errstr = NULL;
+
+store_pool = POOL_MESSAGE;
+
+/* Delete eventual previous signature chain */
+
+dkim_signers = NULL;
+dkim_signatures = NULL;
+
+if (dkim_collect_error)
+ {
+ log_write(0, LOG_MAIN,
+ "DKIM: Error during validation, disabling signature verification: %.100s",
+ dkim_collect_error);
+ f.dkim_disable_verify = TRUE;
+ goto out;
+ }
+
+dkim_collect_input = 0;
+
+/* Finish DKIM operation and fetch link to signatures chain */
+
+rc = pdkim_feed_finish(dkim_verify_ctx, (pdkim_signature **)&dkim_signatures,
+ &errstr);
+if (rc != PDKIM_OK && errstr)
+ log_write(0, LOG_MAIN, "DKIM: validation error: %s", errstr);
+
+/* Build a colon-separated list of signing domains (and identities, if present) in dkim_signers */
+
+for (pdkim_signature * sig = dkim_signatures; sig; sig = sig->next)
+ {
+ if (sig->domain) g = string_append_listele(g, ':', sig->domain);
+ if (sig->identity) g = string_append_listele(g, ':', sig->identity);
+ }
+
+if (g) dkim_signers = g->s;
+
+out:
+store_pool = dkim_verify_oldpool;
+}
+
+
+
+/* Args as per dkim_exim_acl_run() below */
+static int
+dkim_acl_call(uschar * id, gstring ** res_ptr,
+ uschar ** user_msgptr, uschar ** log_msgptr)
+{
+int rc;
+DEBUG(D_receive)
+ debug_printf("calling acl_smtp_dkim for dkim_cur_signer='%s'\n", id);
+
+rc = acl_check(ACL_WHERE_DKIM, NULL, acl_smtp_dkim, user_msgptr, log_msgptr);
+dkim_exim_verify_log_sig(dkim_cur_sig);
+*res_ptr = string_append_listele(*res_ptr, ':', dkim_verify_status);
+return rc;
+}
+
+
+
+/* For the given identity, run the DKIM ACL once for each matching signature.
+
+Arguments
+ id Identity to look for in dkim signatures
+ res_ptr ptr to growable string-list of status results,
+ appended to per ACL run
+ 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
+dkim_exim_acl_run(uschar * id, gstring ** res_ptr,
+ uschar ** user_msgptr, uschar ** log_msgptr)
+{
+uschar * cmp_val;
+int rc = -1;
+
+dkim_verify_status = US"none";
+dkim_verify_reason = US"";
+dkim_cur_signer = id;
+
+if (f.dkim_disable_verify || !id || !dkim_verify_ctx)
+ return OK;
+
+/* Find signatures to run ACL on */
+
+for (pdkim_signature * sig = dkim_signatures; sig; sig = sig->next)
+ if ( (cmp_val = Ustrchr(id, '@') != NULL ? US sig->identity : US sig->domain)
+ && strcmpic(cmp_val, id) == 0
+ )
+ {
+ /* 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
+ dkim_exim_expand_query() below). */
+
+ dkim_cur_sig = sig;
+ dkim_signing_domain = US sig->domain;
+ dkim_signing_selector = US sig->selector;
+ dkim_key_length = sig->keybits;
+
+ /* These two return static strings, so we can compare the addr
+ later to see if the ACL overwrote them. Check that when logging */
+
+ dkim_verify_status = dkim_exim_expand_query(DKIM_VERIFY_STATUS);
+ dkim_verify_reason = dkim_exim_expand_query(DKIM_VERIFY_REASON);
+
+ if ((rc = dkim_acl_call(id, res_ptr, user_msgptr, log_msgptr)) != OK)
+ return rc;
+ }
+
+if (rc != -1)
+ return rc;
+
+/* No matching sig found. Call ACL once anyway. */
+
+dkim_cur_sig = NULL;
+return dkim_acl_call(id, res_ptr, user_msgptr, log_msgptr);
+}
+
+
+static 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_expand_query(int what)
+{
+if (!dkim_verify_ctx || f.dkim_disable_verify || !dkim_cur_sig)
+ return dkim_exim_expand_defaults(what);
+
+switch (what)
+ {
+ case DKIM_ALGO:
+ return dkim_sig_to_a_tag(dkim_cur_sig);
+
+ case DKIM_BODYLENGTH:
+ return dkim_cur_sig->bodylength >= 0
+ ? string_sprintf("%ld", 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
+ ? US dkim_cur_sig->copiedheaders : dkim_exim_expand_defaults(what);
+
+ case DKIM_CREATED:
+ return dkim_cur_sig->created > 0
+ ? string_sprintf("%lu", dkim_cur_sig->created)
+ : dkim_exim_expand_defaults(what);
+
+ case DKIM_EXPIRES:
+ return dkim_cur_sig->expires > 0
+ ? string_sprintf("%lu", dkim_cur_sig->expires)
+ : dkim_exim_expand_defaults(what);
+
+ case DKIM_HEADERNAMES:
+ return dkim_cur_sig->headernames
+ ? dkim_cur_sig->headernames : dkim_exim_expand_defaults(what);
+
+ case DKIM_IDENTITY:
+ return dkim_cur_sig->identity
+ ? US dkim_cur_sig->identity : dkim_exim_expand_defaults(what);
+
+ case DKIM_KEY_GRANULARITY:
+ return dkim_cur_sig->pubkey
+ ? dkim_cur_sig->pubkey->granularity
+ ? US 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
+ ? US 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
+ ? US 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_DNSRECORD:return US"pubkey_dns_syntax";
+ case PDKIM_VERIFY_INVALID_PUBKEY_IMPORT: return US"pubkey_der_syntax";
+ case PDKIM_VERIFY_INVALID_PUBKEY_KEYSIZE: return US"pubkey_too_short";
+ case PDKIM_VERIFY_FAIL_BODY: return US"bodyhash_mismatch";
+ case PDKIM_VERIFY_FAIL_MESSAGE: return US"signature_incorrect";
+ }
+
+ default:
+ return US"";
+ }
+}
+
+
+void
+dkim_exim_sign_init(void)
+{
+int old_pool = store_pool;
+
+dkim_exim_init();
+store_pool = POOL_MAIN;
+pdkim_init_context(&dkim_sign_ctx, FALSE, &dkim_exim_query_dns_txt);
+store_pool = old_pool;
+}
+
+
+/* Generate signatures for the given file.
+If a prefix is given, prepend it to the file for the calculations.
+
+Return:
+ NULL: error; error string written
+ string: signature header(s), or a zero-length string (not an error)
+*/
+
+gstring *
+dkim_exim_sign(int fd, off_t off, uschar * prefix,
+ struct ob_dkim * dkim, const uschar ** errstr)
+{
+const uschar * dkim_domain = NULL;
+int sep = 0;
+gstring * seen_doms = NULL;
+pdkim_signature * sig;
+gstring * sigbuf;
+int pdkim_rc;
+int sread;
+uschar buf[4096];
+int save_errno = 0;
+int old_pool = store_pool;
+uschar * errwhen;
+const uschar * s;
+
+if (dkim->dot_stuffed)
+ dkim_sign_ctx.flags |= PDKIM_DOT_TERM;
+
+store_pool = POOL_MAIN;
+
+if ((s = dkim->dkim_domain) && !(dkim_domain = expand_cstring(s)))
+ /* expansion error, do not send message. */
+ { errwhen = US"dkim_domain"; goto expand_bad; }
+
+/* Set $dkim_domain expansion variable to each unique domain in list. */
+
+if (dkim_domain)
+ while ((dkim_signing_domain = string_nextinlist(&dkim_domain, &sep, NULL, 0)))
+ {
+ const uschar * dkim_sel;
+ int sel_sep = 0;
+
+ if (dkim_signing_domain[0] == '\0')
+ continue;
+
+ /* Only sign once for each domain, no matter how often it
+ appears in the expanded list. */
+
+ dkim_signing_domain = string_copylc(dkim_signing_domain);
+ if (match_isinlist(dkim_signing_domain, CUSS &seen_doms,
+ 0, NULL, NULL, MCL_STRING, TRUE, NULL) == OK)
+ continue;
+
+ seen_doms = string_append_listele(seen_doms, ':', dkim_signing_domain);
+
+ /* Set $dkim_selector expansion variable to each selector in list,
+ for this domain. */
+
+ if (!(dkim_sel = expand_string(dkim->dkim_selector)))
+ { errwhen = US"dkim_selector"; goto expand_bad; }
+
+ while ((dkim_signing_selector = string_nextinlist(&dkim_sel, &sel_sep,
+ NULL, 0)))
+ {
+ uschar * dkim_canon_expanded;
+ int pdkim_canon;
+ uschar * dkim_sign_headers_expanded = NULL;
+ uschar * dkim_private_key_expanded;
+ uschar * dkim_hash_expanded;
+ uschar * dkim_identity_expanded = NULL;
+ uschar * dkim_timestamps_expanded = NULL;
+ unsigned long tval = 0, xval = 0;
+
+ /* Get canonicalization to use */
+
+ dkim_canon_expanded = dkim->dkim_canon
+ ? expand_string(dkim->dkim_canon) : US"relaxed";
+ if (!dkim_canon_expanded) /* expansion error, do not send message. */
+ { errwhen = US"dkim_canon"; goto expand_bad; }
+
+ 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->dkim_sign_headers
+ && !(dkim_sign_headers_expanded = expand_string(dkim->dkim_sign_headers)))
+ { errwhen = US"dkim_sign_header"; goto expand_bad; }
+ /* else pass NULL, which means default header list */
+
+ /* Get private key to use. */
+
+ if (!(dkim_private_key_expanded = expand_string(dkim->dkim_private_key)))
+ { errwhen = US"dkim_private_key"; goto expand_bad; }
+
+ if ( Ustrlen(dkim_private_key_expanded) == 0
+ || Ustrcmp(dkim_private_key_expanded, "0") == 0
+ || Ustrcmp(dkim_private_key_expanded, "false") == 0
+ )
+ continue; /* don't sign, but no error */
+
+ if ( dkim_private_key_expanded[0] == '/'
+ && !(dkim_private_key_expanded =
+ expand_file_big_buffer(dkim_private_key_expanded)))
+ goto bad;
+
+ if (!(dkim_hash_expanded = expand_string(dkim->dkim_hash)))
+ { errwhen = US"dkim_hash"; goto expand_bad; }
+
+ if (dkim->dkim_identity)
+ if (!(dkim_identity_expanded = expand_string(dkim->dkim_identity)))
+ { errwhen = US"dkim_identity"; goto expand_bad; }
+ else if (!*dkim_identity_expanded)
+ dkim_identity_expanded = NULL;
+
+ if (dkim->dkim_timestamps)
+ if (!(dkim_timestamps_expanded = expand_string(dkim->dkim_timestamps)))
+ { errwhen = US"dkim_timestamps"; goto expand_bad; }
+ else
+ xval = (tval = (unsigned long) time(NULL))
+ + strtoul(CCS dkim_timestamps_expanded, NULL, 10);
+
+ if (!(sig = pdkim_init_sign(&dkim_sign_ctx, dkim_signing_domain,
+ dkim_signing_selector,
+ dkim_private_key_expanded,
+ dkim_hash_expanded,
+ errstr
+ )))
+ goto bad;
+ dkim_private_key_expanded[0] = '\0';
+
+ pdkim_set_optional(sig,
+ CS dkim_sign_headers_expanded,
+ CS dkim_identity_expanded,
+ pdkim_canon,
+ pdkim_canon, -1, tval, xval);
+
+ if (!pdkim_set_sig_bodyhash(&dkim_sign_ctx, sig))
+ goto bad;
+
+ if (!dkim_sign_ctx.sig) /* link sig to context chain */
+ dkim_sign_ctx.sig = sig;
+ else
+ {
+ pdkim_signature * n = dkim_sign_ctx.sig;
+ while (n->next) n = n->next;
+ n->next = sig;
+ }
+ }
+ }
+
+/* We may need to carry on with the data-feed even if there are no DKIM sigs to
+produce, if some other package (eg. ARC) is signing. */
+
+if (!dkim_sign_ctx.sig && !dkim->force_bodyhash)
+ {
+ DEBUG(D_transport) debug_printf("DKIM: no viable signatures to use\n");
+ sigbuf = string_get(1); /* return a zero-len string */
+ }
+else
+ {
+ if (prefix && (pdkim_rc = pdkim_feed(&dkim_sign_ctx, prefix, Ustrlen(prefix))) != PDKIM_OK)
+ goto pk_bad;
+
+ if (lseek(fd, off, SEEK_SET) < 0)
+ sread = -1;
+ else
+ while ((sread = read(fd, &buf, sizeof(buf))) > 0)
+ if ((pdkim_rc = pdkim_feed(&dkim_sign_ctx, buf, sread)) != PDKIM_OK)
+ goto pk_bad;
+
+ /* Handle failed read above. */
+ if (sread == -1)
+ {
+ debug_printf("DKIM: Error reading -K file.\n");
+ save_errno = errno;
+ goto bad;
+ }
+
+ /* Build string of headers, one per signature */
+
+ if ((pdkim_rc = pdkim_feed_finish(&dkim_sign_ctx, &sig, errstr)) != PDKIM_OK)
+ goto pk_bad;
+
+ if (!sig)
+ {
+ DEBUG(D_transport) debug_printf("DKIM: no signatures to use\n");
+ sigbuf = string_get(1); /* return a zero-len string */
+ }
+ else for (sigbuf = NULL; sig; sig = sig->next)
+ sigbuf = string_append(sigbuf, 2, US sig->signature_header, US"\r\n");
+ }
+
+CLEANUP:
+ (void) string_from_gstring(sigbuf);
+ store_pool = old_pool;
+ errno = save_errno;
+ return sigbuf;
+
+pk_bad:
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "DKIM: signing failed: %.100s", pdkim_errstr(pdkim_rc));
+bad:
+ sigbuf = NULL;
+ goto CLEANUP;
+
+expand_bad:
+ *errstr = string_sprintf("failed to expand %s: %s",
+ errwhen, expand_string_message);
+ log_write(0, LOG_MAIN | LOG_PANIC, "%s", *errstr);
+ goto bad;
+}
+
+
+
+
+gstring *
+authres_dkim(gstring * g)
+{
+int start = 0; /* compiler quietening */
+
+DEBUG(D_acl) start = g->ptr;
+
+for (pdkim_signature * sig = dkim_signatures; sig; sig = sig->next)
+ {
+ g = string_catn(g, US";\n\tdkim=", 8);
+
+ if (sig->verify_status & PDKIM_VERIFY_POLICY)
+ g = string_append(g, 5,
+ US"policy (", dkim_verify_status, US" - ", dkim_verify_reason, US")");
+ else switch(sig->verify_status)
+ {
+ case PDKIM_VERIFY_NONE: g = string_cat(g, US"none"); break;
+ case PDKIM_VERIFY_INVALID:
+ switch (sig->verify_ext_status)
+ {
+ case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE:
+ g = string_cat(g, US"tmperror (pubkey unavailable)\n\t\t"); break;
+ case PDKIM_VERIFY_INVALID_BUFFER_SIZE:
+ g = string_cat(g, US"permerror (overlong public key record)\n\t\t"); break;
+ case PDKIM_VERIFY_INVALID_PUBKEY_DNSRECORD:
+ case PDKIM_VERIFY_INVALID_PUBKEY_IMPORT:
+ g = string_cat(g, US"neutral (public key record import problem)\n\t\t");
+ break;
+ case PDKIM_VERIFY_INVALID_SIGNATURE_ERROR:
+ g = string_cat(g, US"neutral (signature tag missing or invalid)\n\t\t");
+ break;
+ case PDKIM_VERIFY_INVALID_DKIM_VERSION:
+ g = string_cat(g, US"neutral (unsupported DKIM version)\n\t\t");
+ break;
+ default:
+ g = string_cat(g, US"permerror (unspecified problem)\n\t\t"); break;
+ }
+ break;
+ case PDKIM_VERIFY_FAIL:
+ switch (sig->verify_ext_status)
+ {
+ case PDKIM_VERIFY_FAIL_BODY:
+ g = string_cat(g,
+ US"fail (body hash mismatch; body probably modified in transit)\n\t\t");
+ break;
+ case PDKIM_VERIFY_FAIL_MESSAGE:
+ g = string_cat(g,
+ US"fail (signature did not verify; headers probably modified in transit)\n\t\t");
+ break;
+ case PDKIM_VERIFY_INVALID_PUBKEY_KEYSIZE: /* should this really be "polcy"? */
+ g = string_fmt_append(g, "fail (public key too short: %u bits)\n\t\t", sig->keybits);
+ break;
+ default:
+ g = string_cat(g, US"fail (unspecified reason)\n\t\t");
+ break;
+ }
+ break;
+ case PDKIM_VERIFY_PASS: g = string_cat(g, US"pass"); break;
+ default: g = string_cat(g, US"permerror"); break;
+ }
+ if (sig->domain) g = string_append(g, 2, US" header.d=", sig->domain);
+ if (sig->identity) g = string_append(g, 2, US" header.i=", sig->identity);
+ if (sig->selector) g = string_append(g, 2, US" header.s=", sig->selector);
+ g = string_append(g, 2, US" header.a=", dkim_sig_to_a_tag(sig));
+ }
+
+DEBUG(D_acl)
+ if (g->ptr == start)
+ debug_printf("DKIM: no authres\n");
+ else
+ debug_printf("DKIM: authres '%.*s'\n", g->ptr - start - 3, g->s + start + 3);
+return g;
+}
+
+
+# endif /*!MACRO_PREDEF*/
+#endif /*!DISABLE_DKIM*/
diff --git a/src/dkim.h b/src/dkim.h
new file mode 100644
index 0000000..7b94f22
--- /dev/null
+++ b/src/dkim.h
@@ -0,0 +1,32 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge, 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+void dkim_exim_init(void);
+gstring * dkim_exim_sign(int, off_t, uschar *, struct ob_dkim *, const uschar **);
+void dkim_exim_verify_init(BOOL);
+void dkim_exim_verify_feed(uschar *, int);
+void dkim_exim_verify_finish(void);
+void dkim_exim_verify_log_all(void);
+int dkim_exim_acl_run(uschar *, gstring **, uschar **, uschar **);
+uschar *dkim_exim_expand_query(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/dkim_transport.c b/src/dkim_transport.c
new file mode 100644
index 0000000..cfd4b90
--- /dev/null
+++ b/src/dkim_transport.c
@@ -0,0 +1,412 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Transport shim for dkim signing */
+
+
+#include "exim.h"
+
+#ifndef DISABLE_DKIM /* rest of file */
+
+
+static BOOL
+dkt_sign_fail(struct ob_dkim * dkim, int * errp)
+{
+if (dkim->dkim_strict)
+ {
+ uschar * dkim_strict_result = expand_string(dkim->dkim_strict);
+
+ if (dkim_strict_result)
+ if ( strcmpic(dkim_strict_result, US"1") == 0
+ || strcmpic(dkim_strict_result, US"true") == 0)
+ {
+ /* Set errno to something halfway meaningful */
+ *errp = EACCES;
+ log_write(0, LOG_MAIN, "DKIM: message could not be signed,"
+ " and dkim_strict is set. Deferring message delivery.");
+ return FALSE;
+ }
+ }
+return TRUE;
+}
+
+/* Send the file at in_fd down the output fd */
+
+static BOOL
+dkt_send_file(int out_fd, int in_fd, off_t off
+#ifdef OS_SENDFILE
+ , size_t size
+#endif
+ )
+{
+#ifdef OS_SENDFILE
+DEBUG(D_transport) debug_printf("send file fd=%d size=%u\n", out_fd, (unsigned)(size - off));
+#else
+DEBUG(D_transport) debug_printf("send file fd=%d\n", out_fd);
+#endif
+
+/*XXX should implement timeout, like transport_write_block_fd() ? */
+
+#ifdef OS_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.sock != out_fd)
+ {
+ ssize_t copied = 0;
+
+ while(copied >= 0 && off < size)
+ copied = os_sendfile(out_fd, in_fd, &off, size - off);
+ if (copied < 0)
+ return FALSE;
+ }
+else
+
+#endif
+
+ {
+ int sread, wwritten;
+
+ /* Rewind file */
+ if (lseek(in_fd, off, SEEK_SET) < 0) return FALSE;
+
+ /* Send file down the original fd */
+ while((sread = read(in_fd, deliver_out_buffer, DELIVER_OUT_BUFFER_SIZE)) > 0)
+ {
+ uschar * p = deliver_out_buffer;
+ /* write the chunk */
+
+ while (sread)
+ {
+#ifndef DISABLE_TLS
+ wwritten = tls_out.active.sock == out_fd
+ ? tls_write(tls_out.active.tls_ctx, p, sread, FALSE)
+ : write(out_fd, CS p, sread);
+#else
+ wwritten = write(out_fd, CS p, sread);
+#endif
+ if (wwritten == -1)
+ return FALSE;
+ p += wwritten;
+ sread -= wwritten;
+ }
+ }
+
+ if (sread == -1)
+ return FALSE;
+ }
+
+return TRUE;
+}
+
+
+
+
+/* This function is a wrapper around transport_write_message().
+ It is only called from the smtp transport if DKIM or Domainkeys support
+ is active and no transport filter is to be used.
+
+Arguments:
+ As for transport_write_message() in transort.c, with additional arguments
+ for DKIM.
+
+Returns: TRUE on success; FALSE (with errno) for any failure
+*/
+
+static BOOL
+dkt_direct(transport_ctx * tctx, struct ob_dkim * dkim,
+ const uschar ** err)
+{
+int save_fd = tctx->u.fd;
+int save_options = tctx->options;
+BOOL save_wireformat = f.spool_file_wireformat;
+uschar * hdrs;
+gstring * dkim_signature;
+int hsize;
+const uschar * errstr;
+BOOL rc;
+
+DEBUG(D_transport) debug_printf("dkim signing direct-mode\n");
+
+/* Get headers in string for signing and transmission. Do CRLF
+and dotstuffing (but no body nor dot-termination) */
+
+tctx->u.msg = NULL;
+tctx->options = tctx->options & ~(topt_end_dot | topt_use_bdat)
+ | topt_output_string | topt_no_body;
+
+rc = transport_write_message(tctx, 0);
+hdrs = string_from_gstring(tctx->u.msg);
+hsize = tctx->u.msg->ptr;
+
+tctx->u.fd = save_fd;
+tctx->options = save_options;
+if (!rc) return FALSE;
+
+/* Get signatures for headers plus spool data file */
+
+#ifdef EXPERIMENTAL_ARC
+arc_sign_init();
+#endif
+
+/* The dotstuffed status of the datafile depends on whether it was stored
+in wireformat. */
+
+dkim->dot_stuffed = f.spool_file_wireformat;
+if (!(dkim_signature = dkim_exim_sign(deliver_datafile, SPOOL_DATA_START_OFFSET,
+ hdrs, dkim, &errstr)))
+ if (!(rc = dkt_sign_fail(dkim, &errno)))
+ {
+ *err = errstr;
+ return FALSE;
+ }
+
+#ifdef EXPERIMENTAL_ARC
+if (dkim->arc_signspec) /* Prepend ARC headers */
+ {
+ uschar * e = NULL;
+ if (!(dkim_signature = arc_sign(dkim->arc_signspec, dkim_signature, &e)))
+ {
+ *err = e;
+ return FALSE;
+ }
+ }
+#endif
+
+/* Write the signature and headers into the deliver-out-buffer. This should
+mean they go out in the same packet as the MAIL, RCPT and (first) BDAT commands
+(transport_write_message() sizes the BDAT for the buffered amount) - for short
+messages, the BDAT LAST command. We want no dotstuffing expansion here, it
+having already been done - but we have to say we want CRLF output format, and
+temporarily set the marker for possible already-CRLF input. */
+
+tctx->options &= ~topt_escape_headers;
+f.spool_file_wireformat = TRUE;
+transport_write_reset(0);
+if ( ( dkim_signature
+ && dkim_signature->ptr > 0
+ && !write_chunk(tctx, dkim_signature->s, dkim_signature->ptr)
+ )
+ || !write_chunk(tctx, hdrs, hsize)
+ )
+ return FALSE;
+
+f.spool_file_wireformat = save_wireformat;
+tctx->options = save_options | topt_no_headers | topt_continuation;
+
+if (!(transport_write_message(tctx, 0)))
+ return FALSE;
+
+tctx->options = save_options;
+return TRUE;
+}
+
+
+/* This function is a wrapper around transport_write_message().
+ It is only called from the smtp transport if DKIM or Domainkeys support
+ is active and a transport filter is to be used. 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 transport_write_message() in transort.c, with additional arguments
+ for DKIM.
+
+Returns: TRUE on success; FALSE (with errno) for any failure
+*/
+
+static BOOL
+dkt_via_kfile(transport_ctx * tctx, struct ob_dkim * dkim, const uschar ** err)
+{
+int dkim_fd;
+int save_errno = 0;
+BOOL rc;
+uschar * dkim_spool_name;
+gstring * dkim_signature;
+int options, dlen;
+off_t k_file_size;
+const uschar * errstr;
+
+dkim_spool_name = spool_fname(US"input", message_subdir, message_id,
+ string_sprintf("-%d-K", (int)getpid()));
+
+DEBUG(D_transport) debug_printf("dkim signing via file %s\n", dkim_spool_name);
+
+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;
+ *err = string_sprintf("dkim spoolfile create: %s", strerror(errno));
+ goto CLEANUP;
+ }
+
+/* Call transport utility function to write the -K file; does the CRLF expansion
+(but, in the CHUNKING case, neither dot-stuffing nor dot-termination). */
+
+ {
+ int save_fd = tctx->u.fd;
+ tctx->u.fd = dkim_fd;
+ options = tctx->options;
+ tctx->options &= ~topt_use_bdat;
+
+ rc = transport_write_message(tctx, 0);
+
+ tctx->u.fd = save_fd;
+ tctx->options = options;
+ }
+
+/* Save error state. We must clean up before returning. */
+if (!rc)
+ {
+ save_errno = errno;
+ goto CLEANUP;
+ }
+
+#ifdef EXPERIMENTAL_ARC
+arc_sign_init();
+#endif
+
+/* Feed the file to the goats^W DKIM lib. At this point the dotstuffed
+status of the file depends on the output of transport_write_message() just
+above, which should be the result of the end_dot flag in tctx->options. */
+
+dkim->dot_stuffed = !!(options & topt_end_dot);
+if (!(dkim_signature = dkim_exim_sign(dkim_fd, 0, NULL, dkim, &errstr)))
+ {
+ dlen = 0;
+ if (!(rc = dkt_sign_fail(dkim, &save_errno)))
+ {
+ *err = errstr;
+ goto CLEANUP;
+ }
+ }
+else
+ dlen = dkim_signature->ptr;
+
+#ifdef EXPERIMENTAL_ARC
+if (dkim->arc_signspec) /* Prepend ARC headers */
+ {
+ if (!(dkim_signature = arc_sign(dkim->arc_signspec, dkim_signature, USS err)))
+ goto CLEANUP;
+ dlen = dkim_signature->ptr;
+ }
+#endif
+
+#ifndef OS_SENDFILE
+if (options & topt_use_bdat)
+#endif
+ if ((k_file_size = lseek(dkim_fd, 0, SEEK_END)) < 0)
+ {
+ *err = string_sprintf("dkim spoolfile seek: %s", strerror(errno));
+ goto CLEANUP;
+ }
+
+if (options & topt_use_bdat)
+ {
+ /* On big messages output a precursor chunk to get any pipelined
+ MAIL & RCPT commands flushed, then reap the responses so we can
+ error out on RCPT rejects before sending megabytes. */
+
+ if ( dlen + k_file_size > DELIVER_OUT_BUFFER_SIZE
+ && dlen > 0)
+ {
+ if ( tctx->chunk_cb(tctx, dlen, 0) != OK
+ || !transport_write_block(tctx,
+ dkim_signature->s, dlen, FALSE)
+ || tctx->chunk_cb(tctx, 0, tc_reap_prev) != OK
+ )
+ goto err;
+ dlen = 0;
+ }
+
+ /* Send the BDAT command for the entire message, as a single LAST-marked
+ chunk. */
+
+ if (tctx->chunk_cb(tctx, dlen + k_file_size, tc_chunk_last) != OK)
+ goto err;
+ }
+
+if(dlen > 0 && !transport_write_block(tctx, dkim_signature->s, dlen, TRUE))
+ goto err;
+
+if (!dkt_send_file(tctx->u.fd, dkim_fd, 0
+#ifdef OS_SENDFILE
+ , k_file_size
+#endif
+ ))
+ {
+ save_errno = errno;
+ rc = FALSE;
+ }
+
+CLEANUP:
+ /* unlink -K file */
+ if (dkim_fd >= 0) (void)close(dkim_fd);
+ Uunlink(dkim_spool_name);
+ errno = save_errno;
+ return rc;
+
+err:
+ save_errno = errno;
+ rc = FALSE;
+ goto CLEANUP;
+}
+
+
+
+/***************************************************************************************************
+* 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.
+
+Arguments:
+ As for transport_write_message() in transort.c, with additional arguments
+ for DKIM.
+
+Returns: TRUE on success; FALSE (with errno) for any failure
+*/
+
+BOOL
+dkim_transport_write_message(transport_ctx * tctx,
+ struct ob_dkim * dkim, const uschar ** err)
+{
+/* If we can't sign, just call the original function. */
+
+if ( !(dkim->dkim_private_key && dkim->dkim_domain && dkim->dkim_selector)
+ && !dkim->force_bodyhash)
+ return transport_write_message(tctx, 0);
+
+/* If there is no filter command set up, construct the message and calculate
+a dkim signature of it, send the signature and a reconstructed message. This
+avoids using a temprary file. */
+
+if ( !transport_filter_argv
+ || !*transport_filter_argv
+ || !**transport_filter_argv
+ )
+ return dkt_direct(tctx, dkim, err);
+
+/* Use the transport path to write a file, calculate a dkim signature,
+send the signature and then send the file. */
+
+return dkt_via_kfile(tctx, dkim, err);
+}
+
+#endif /* whole file */
+
+/* vi: aw ai sw=2
+*/
+/* End of dkim_transport.c */
diff --git a/src/dmarc.c b/src/dmarc.c
new file mode 100644
index 0000000..17bba9d
--- /dev/null
+++ b/src/dmarc.c
@@ -0,0 +1,655 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+/* DMARC support.
+ Copyright (c) The Exim Maintainers 2019 - 2022
+ 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 SUPPORT_DMARC
+# if !defined SUPPORT_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";
+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[] = {
+ /* name value */
+ { 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 }
+};
+
+
+gstring *
+dmarc_version_report(gstring * g)
+{
+return string_fmt_append(g, "Library version: dmarc: Compile: %d.%d.%d.%d\n",
+ (OPENDMARC_LIB_VERSION & 0xff000000) >> 24, (OPENDMARC_LIB_VERSION & 0x00ff0000) >> 16,
+ (OPENDMARC_LIB_VERSION & 0x0000ff00) >> 8, OPENDMARC_LIB_VERSION & 0x000000ff);
+}
+
+
+/* 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 = store_malloc(sizeof(error_block));
+if (!eblock)
+ eblock = eb;
+else
+ {
+ /* Find the end of the eblock struct and point it at eb */
+ error_block *tmp = eblock;
+ while(tmp->next)
+ 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;
+
+/* 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"";
+f.dmarc_has_been_checked = FALSE;
+header_from_sender = NULL;
+spf_sender_domain = NULL;
+spf_human_readable = NULL;
+
+/* ACLs have "control=dmarc_disable_verify" */
+if (f.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 || !*dmarc_tld_file)
+ {
+ DEBUG(D_receive) debug_printf("DMARC: no dmarc_tld_file\n");
+ dmarc_abort = TRUE;
+ }
+else if (opendmarc_tld_read_file(CS dmarc_tld_file, NULL, NULL, NULL))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "DMARC failure to load tld list '%s': %s",
+ dmarc_tld_file, strerror(errno));
+ dmarc_abort = TRUE;
+ }
+if (!sender_host_address)
+ {
+ DEBUG(D_receive) debug_printf("DMARC: no sender_host_address\n");
+ 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;
+ if (!(dmarc_pctx = opendmarc_policy_connect_init(sender_host_address, is_ipv6)))
+ {
+ 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 (!f.dmarc_disable_verify)
+ from_header = hdr;
+return OK;
+}
+
+
+static void
+dmarc_send_forensic_report(u_char **ruf)
+{
+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 (!f.dmarc_enable_forensic)
+ return;
+
+if ( dmarc_policy == DMARC_POLICY_REJECT && action == DMARC_RESULT_REJECT
+ || dmarc_policy == DMARC_POLICY_QUARANTINE && action == DMARC_RESULT_QUARANTINE
+ || dmarc_policy == DMARC_POLICY_NONE && action == DMARC_RESULT_REJECT
+ || dmarc_policy == DMARC_POLICY_NONE && 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);
+
+ for (int 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 || f.running_in_test_harness) ? " (not really)" : "");
+ if (host_checking || f.running_in_test_harness)
+ continue;
+
+ if (!moan_send_message(recipient, ERRMESS_DMARC_FORENSIC, eblock,
+ header_list, message_file, NULL))
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "failure to send DMARC forensic report to %s", recipient);
+ }
+ }
+}
+
+
+/* Look up a DNS dmarc record for the given domain. Return it or NULL */
+
+static uschar *
+dmarc_dns_lookup(uschar * dom)
+{
+dns_answer * dnsa = store_get_dns_answer();
+dns_scan dnss;
+int rc = dns_lookup(dnsa, string_sprintf("_dmarc.%s", dom), T_TXT, NULL);
+
+if (rc == DNS_SUCCEED)
+ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT))
+ if (rr->type == T_TXT && rr->size > 3)
+ {
+ store_free_dns_answer(dnsa);
+ return string_copyn_taint(US rr->data, rr->size, GET_TAINTED);
+ }
+store_free_dns_answer(dnsa);
+return NULL;
+}
+
+
+static 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)
+ {
+ DEBUG(D_receive) debug_printf("DMARC history file not set\n");
+ return DMARC_HIST_DISABLED;
+ }
+history_file_fd = log_open_as_exim(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 || f.running_in_test_harness) ? " (not really)" : "");
+if (host_checking || f.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;
+}
+
+
+/* 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 = dkim_signatures;
+uschar * rr;
+BOOL has_dmarc_record = TRUE;
+u_char **ruf; /* forensic report addressees, if called for */
+
+/* ACLs have "control=dmarc_disable_verify" */
+if (f.dmarc_disable_verify)
+ 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)
+ {
+ DEBUG(D_receive) debug_printf("DMARC: no From: header\n");
+ dmarc_abort = TRUE;
+ }
+else if (!dmarc_abort)
+ {
+ uschar * errormsg;
+ int dummy, domain;
+ uschar * p;
+ uschar saveend;
+
+ f.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)
+ {
+ uschar * dmarc_domain;
+
+ /* 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 = US 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. */
+ dkim_history_buffer = US"";
+ while (sig)
+ {
+ int dkim_result, dkim_ares_result, vs, ves;
+
+ vs = sig->verify_status & ~PDKIM_VERIFY_POLICY;
+ 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, US 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_DNSRECORD ? ARES_RESULT_PERMERROR :
+ ves == PDKIM_VERIFY_INVALID_PUBKEY_IMPORT ? 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;
+ }
+
+ /* Look up DMARC policy record in DNS. We do this explicitly, rather than
+ letting the dmarc library do it with opendmarc_policy_query_dmarc(), so that
+ our dns access path is used for debug tracing and for the testsuite
+ diversion. */
+
+ libdm_status = (rr = dmarc_dns_lookup(header_from_sender))
+ ? opendmarc_policy_store_dmarc(dmarc_pctx, rr, header_from_sender, NULL)
+ : DMARC_DNS_ERROR_NO_RECORD;
+ 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. */
+
+ dmarc_domain = store_get(DMARC_MAXHOSTNAMELEN, GET_TAINTED);
+ libdm_status = opendmarc_policy_fetch_utilized_domain(dmarc_pctx,
+ dmarc_domain, DMARC_MAXHOSTNAMELEN-1);
+ store_release_above(dmarc_domain + Ustrlen(dmarc_domain)+1);
+ dmarc_used_domain = 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));
+
+ dmarc_policy = libdm_status = opendmarc_get_policy_to_enforce(dmarc_pctx);
+ 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)
+ {
+ 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);
+ }
+ }
+
+/* shut down libopendmarc */
+if (dmarc_pctx)
+ (void) opendmarc_policy_connect_shutdown(dmarc_pctx);
+if (!f.dmarc_disable_verify)
+ (void) opendmarc_policy_library_shutdown(&dmarc_ctx);
+
+return OK;
+}
+
+uschar *
+dmarc_exim_expand_query(int what)
+{
+if (f.dmarc_disable_verify || !dmarc_pctx)
+ return dmarc_exim_expand_defaults(what);
+
+if (what == DMARC_VERIFY_STATUS)
+ return dmarc_status;
+return US"";
+}
+
+uschar *
+dmarc_exim_expand_defaults(int what)
+{
+if (what == DMARC_VERIFY_STATUS)
+ return f.dmarc_disable_verify ? US"off" : US"none";
+return US"";
+}
+
+
+gstring *
+authres_dmarc(gstring * g)
+{
+if (f.dmarc_has_been_checked)
+ {
+ g = string_append(g, 2, US";\n\tdmarc=", dmarc_pass_fail);
+ if (header_from_sender)
+ g = string_append(g, 2, US" header.from=", header_from_sender);
+ }
+return g;
+}
+
+# endif /* SUPPORT_SPF */
+#endif /* SUPPORT_DMARC */
+/* vi: aw ai sw=2
+ */
diff --git a/src/dmarc.h b/src/dmarc.h
new file mode 100644
index 0000000..899cd7e
--- /dev/null
+++ b/src/dmarc.h
@@ -0,0 +1,60 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Experimental DMARC support.
+ Copyright (c) The Exim Maintainers 2021 - 2022
+ 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 SUPPORT_DMARC
+
+# include "opendmarc/dmarc.h"
+# ifdef SUPPORT_SPF
+# include "spf2/spf.h"
+# endif /* SUPPORT_SPF */
+
+/* prototypes */
+gstring * dmarc_version_report(gstring *);
+int dmarc_init();
+int dmarc_store_data(header_line *);
+int dmarc_process();
+uschar *dmarc_exim_expand_query(int);
+uschar *dmarc_exim_expand_defaults(int);
+
+#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 /* SUPPORT_DMARC */
diff --git a/src/dns.c b/src/dns.c
new file mode 100644
index 0000000..7d7ee0c
--- /dev/null
+++ b/src/dns.c
@@ -0,0 +1,1332 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for interfacing with the DNS. */
+
+#include "exim.h"
+
+
+/*************************************************
+* 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 suite 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(const uschar *domain, int type, uschar *answerptr, int size)
+{
+int len = Ustrlen(domain);
+int asize = size; /* Locally modified */
+uschar * name;
+uschar utilname[256];
+uschar *aptr = answerptr; /* Locally modified */
+struct stat statbuf;
+
+/* Remove terminating dot. */
+
+if (domain[len - 1] == '.') len--;
+name = string_copyn(domain, len);
+
+/* Look for the fakens utility, and if it exists, call it. */
+
+(void)string_format(utilname, sizeof(utilname), "%s/bin/fakens",
+ config_main_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] = config_main_directory;
+ argv[2] = name;
+ argv[3] = dns_text_type(type);
+ argv[4] = NULL;
+
+ pid = child_open(argv, NULL, 0000, &infd, &outfd, FALSE, US"fakens-search");
+ 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 we ran out of output buffer before exhausting the return,
+ carry on reading and counting it. */
+
+ if (asize == 0)
+ while ((rc = read(outfd, name, sizeof(name))) > 0)
+ len += rc;
+
+ 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");
+ }
+ }
+else
+ {
+ DEBUG(D_dns) debug_printf("fakens (%s) not found\n", utilname);
+ }
+
+/* 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
+
+Returns: an allocated string
+*/
+
+uschar *
+dns_build_reverse(const uschar * string)
+{
+const uschar * p = string + Ustrlen(string);
+gstring * g = NULL;
+
+/* Handle IPv4 address */
+
+#if HAVE_IPV6
+if (Ustrchr(string, ':') == NULL)
+#endif
+ {
+ for (int i = 0; i < 4; i++)
+ {
+ const uschar * ppp = p;
+ while (ppp > string && ppp[-1] != '.') ppp--;
+ g = string_catn(g, ppp, p - ppp);
+ g = string_catn(g, US".", 1);
+ p = ppp - 1;
+ }
+ g = string_catn(g, US"in-addr.arpa", 12);
+ }
+
+/* Handle IPv6 address; convert to binary so as to fill out any
+abbreviation in the textual form. */
+
+#if HAVE_IPV6
+else
+ {
+ int v6[4];
+
+ g = string_get_tainted(32, string);
+ (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 (int i = 3; i >= 0; i--)
+ for (int j = 0; j < 32; j += 4)
+ g = string_fmt_append(g, "%x.", (v6[i] >> j) & 15);
+ g = string_catn(g, US"ip6.arpa.", 9);
+
+ /* 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 (int i = 0; i < 4; i++)
+ {
+ sprintf(pp, "%08X", v6[i]);
+ pp += 8;
+ }
+ Ustrcpy(pp, US"].ip6.arpa.");
+ **************************************************/
+
+ }
+#endif
+return string_from_gstring(g);
+}
+
+
+
+
+/* Increment the aptr in dnss, checking against dnsa length.
+Return: TRUE for a bad result
+*/
+static BOOL
+dnss_inc_aptr(const dns_answer * dnsa, dns_scan * dnss, unsigned delta)
+{
+return (dnss->aptr += delta) >= dnsa->answer + dnsa->answerlen;
+}
+
+/*************************************************
+* 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 specifying what portion to scan, as described above
+
+Returns: next dns record, or NULL when no more
+*/
+
+dns_record *
+dns_next_rr(const dns_answer *dnsa, dns_scan *dnss, int reset)
+{
+const HEADER * h = (const HEADER *)dnsa->answer;
+int namelen;
+
+char * trace = NULL;
+#ifdef rr_trace
+# define TRACE DEBUG(D_dns)
+#else
+# define TRACE if (FALSE)
+#endif
+
+/* Reset the saved data when requested to, and skip to the first required RR */
+
+if (reset != RESET_NEXT)
+ {
+ dnss->rrcount = ntohs(h->qdcount);
+ TRACE debug_printf("%s: reset (Q rrcount %d)\n", __FUNCTION__, dnss->rrcount);
+ dnss->aptr = dnsa->answer + sizeof(HEADER);
+
+ /* Skip over questions; failure to expand the name just gives up */
+
+ while (dnss->rrcount-- > 0)
+ {
+ TRACE trace = "Q-namelen";
+ namelen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen,
+ dnss->aptr, (DN_EXPAND_ARG4_TYPE) &dnss->srr.name, DNS_MAXNAME);
+ if (namelen < 0) goto null_return;
+ /* skip name & type & class */
+ TRACE trace = "Q-skip";
+ if (dnss_inc_aptr(dnsa, dnss, namelen+4)) goto null_return;
+ }
+
+ /* Get the number of answer records. */
+
+ dnss->rrcount = ntohs(h->ancount);
+ TRACE debug_printf("%s: reset (A rrcount %d)\n", __FUNCTION__, dnss->rrcount);
+
+ /* 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)
+ {
+ TRACE debug_printf("%s: additional\n", __FUNCTION__);
+ dnss->rrcount += ntohs(h->nscount);
+ TRACE debug_printf("%s: reset (NS rrcount %d)\n", __FUNCTION__, dnss->rrcount);
+ }
+
+ if (reset == RESET_AUTHORITY || reset == RESET_ADDITIONAL)
+ {
+ TRACE if (reset == RESET_AUTHORITY)
+ debug_printf("%s: authority\n", __FUNCTION__);
+ while (dnss->rrcount-- > 0)
+ {
+ TRACE trace = "A-namelen";
+ namelen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen,
+ dnss->aptr, (DN_EXPAND_ARG4_TYPE) &dnss->srr.name, DNS_MAXNAME);
+ if (namelen < 0) goto null_return;
+ /* skip name, type, class & TTL */
+ TRACE trace = "A-hdr";
+ if (dnss_inc_aptr(dnsa, dnss, namelen+8)) goto null_return;
+ GETSHORT(dnss->srr.size, dnss->aptr); /* size of data portion */
+ /* skip over it */
+ TRACE trace = "A-skip";
+ if (dnss_inc_aptr(dnsa, dnss, dnss->srr.size)) goto null_return;
+ }
+ dnss->rrcount = reset == RESET_AUTHORITY
+ ? ntohs(h->nscount) : ntohs(h->arcount);
+ TRACE debug_printf("%s: reset (%s rrcount %d)\n", __FUNCTION__,
+ reset == RESET_AUTHORITY ? "NS" : "AR", dnss->rrcount);
+ }
+ TRACE debug_printf("%s: %d RRs to read\n", __FUNCTION__, dnss->rrcount);
+ }
+else
+ TRACE debug_printf("%s: next (%d left)\n", __FUNCTION__, dnss->rrcount);
+
+/* 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). */
+
+TRACE trace = "R-namelen";
+namelen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen, dnss->aptr,
+ (DN_EXPAND_ARG4_TYPE) &dnss->srr.name, DNS_MAXNAME);
+if (namelen < 0) goto null_return;
+
+/* Move the pointer past the name and fill in the rest of the data structure
+from the following bytes. */
+
+TRACE trace = "R-name";
+if (dnss_inc_aptr(dnsa, dnss, namelen)) goto null_return;
+
+GETSHORT(dnss->srr.type, dnss->aptr); /* Record type */
+TRACE trace = "R-class";
+if (dnss_inc_aptr(dnsa, dnss, 2)) goto null_return; /* Don't want class */
+GETLONG(dnss->srr.ttl, dnss->aptr); /* TTL */
+GETSHORT(dnss->srr.size, dnss->aptr); /* Size of data portion */
+dnss->srr.data = dnss->aptr; /* The record's data follows */
+
+/* Unchecked increment ok here since no further access on this iteration;
+will be checked on next at "R-name". */
+
+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. */
+
+TRACE debug_printf("%s: return %s\n", __FUNCTION__, dns_text_type(dnss->srr.type));
+return &dnss->srr;
+
+null_return:
+ TRACE debug_printf("%s: terminate (%d RRs left). Last op: %s; errno %d %s\n",
+ __FUNCTION__, dnss->rrcount, trace, errno, strerror(errno));
+ dnss->rrcount = 0;
+ return NULL;
+}
+
+
+/* Extract the AUTHORITY information from the answer. If the answer isn't
+authoritative (AA not set), we do not extract anything.
+
+The AUTHORITY section contains NS records if the name in question was found,
+it contains a SOA record otherwise. (This is just from experience and some
+tests, is there some spec?)
+
+Scan the whole AUTHORITY section, since it may contain other records
+(e.g. NSEC3) too.
+
+Return: name for the authority, in an allocated string, or NULL if none found */
+
+static const uschar *
+dns_extract_auth_name(const dns_answer * dnsa) /* FIXME: const dns_answer */
+{
+dns_scan dnss;
+const HEADER * h = (const HEADER *) dnsa->answer;
+
+if (h->nscount && h->aa)
+ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_AUTHORITY);
+ rr; rr = dns_next_rr(dnsa, &dnss, RESET_NEXT))
+ if (rr->type == (h->ancount ? T_NS : T_SOA))
+ return string_copy(rr->name);
+return NULL;
+}
+
+
+
+
+/*************************************************
+* 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, AA = Authoritative Answer)
+
+Argument: pointer to dns answer block
+Returns: bool indicating presence of AD bit
+*/
+
+BOOL
+dns_is_secure(const 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
+const HEADER * h = (const HEADER *) dnsa->answer;
+const uschar * auth_name;
+const uschar * trusted;
+
+if (dnsa->answerlen < 0) return FALSE;
+/* Beware that newer versions of glibc on Linux will filter out the ad bit
+unless their shiny new RES_TRUSTAD bit is set for the resolver. */
+if (h->ad) return TRUE;
+
+/* If the resolver we ask is authoritative for the domain in question, it may
+not set the AD but the AA bit. If we explicitly trust the resolver for that
+domain (via a domainlist in dns_trust_aa), we return TRUE to indicate a secure
+answer. */
+
+if ( !h->aa
+ || !dns_trust_aa
+ || !(trusted = expand_string(dns_trust_aa))
+ || !*trusted
+ || !(auth_name = dns_extract_auth_name(dnsa))
+ || OK != match_isinlist(auth_name, &trusted, 0, &domainlist_anchor, NULL,
+ MCL_DOMAIN, TRUE, NULL)
+ )
+ return FALSE;
+
+DEBUG(D_dns) debug_printf("DNS faked the AD bit "
+ "(got AA and matched with dns_trust_aa (%s in %s))\n",
+ auth_name, dns_trust_aa);
+
+return TRUE;
+#endif
+}
+
+static void
+dns_set_insecure(dns_answer * dnsa)
+{
+#ifndef DISABLE_DNSSEC
+HEADER * h = (HEADER *)dnsa->answer;
+h->aa = h->ad = 0;
+#endif
+}
+
+/************************************************
+ * Check whether the AA bit is set *
+ * We need this to warn if we requested AD *
+ * from an authoritative server *
+ ************************************************/
+
+BOOL
+dns_is_aa(const dns_answer * dnsa)
+{
+#ifdef DISABLE_DNSSEC
+return FALSE;
+#else
+return dnsa->answerlen >= 0 && ((const HEADER *)dnsa->answer)->aa;
+#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 *
+*************************************************/
+
+static void
+dns_fail_tag(uschar * buf, const uschar * name, int dns_type)
+{
+res_state resp = os_get_dns_resolver_res();
+
+/*XX buf needs to be 255 +1 + (max(typetext) == 5) +1 + max(chars_for_long-max) +1
+We truncate the name here for safety... could use a dynamic string. */
+
+sprintf(CS buf, "%.255s-%s-%lx", name, dns_text_type(dns_type),
+ (unsigned long) resp->options);
+}
+
+
+/* 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
+ expiry time TTL expires, or zero for unlimited
+ rc the return code
+
+Returns: the return code
+*/
+
+/* we need: 255 +1 + (max(typetext) == 5) +1 + max(chars_for_long-max) +1 */
+#define DNS_FAILTAG_MAX 290
+#define DNS_FAILNODE_SIZE \
+ (sizeof(expiring_data) + sizeof(tree_node) + DNS_FAILTAG_MAX)
+
+static int
+dns_fail_return(const uschar * name, int type, time_t expiry, int rc)
+{
+uschar node_name[DNS_FAILTAG_MAX];
+tree_node * previous, * new;
+expiring_data * e;
+
+dns_fail_tag(node_name, name, type);
+if ((previous = tree_search(tree_dns_fails, node_name)))
+ e = previous->data.ptr;
+else
+ {
+ e = store_get_perm(DNS_FAILNODE_SIZE, name);
+ new = (void *)(e+1);
+ dns_fail_tag(new->name, name, type);
+ new->data.ptr = e;
+ (void)tree_insertnode(&tree_dns_fails, new);
+ }
+
+DEBUG(D_dns) debug_printf(" %s neg-cache entry for %s, ttl %d\n",
+ previous ? "update" : "writing",
+ node_name, expiry ? (int)(expiry - time(NULL)) : -1);
+e->expiry = expiry;
+e->data.val = rc;
+return rc;
+}
+
+
+/* Return the cached result of a known-bad lookup, or -1.
+*/
+static int
+dns_fail_cache_hit(const uschar * name, int type)
+{
+uschar node_name[DNS_FAILTAG_MAX];
+tree_node * previous;
+expiring_data * e;
+int val, rc;
+
+dns_fail_tag(node_name, name, type);
+if (!(previous = tree_search(tree_dns_fails, node_name)))
+ return -1;
+
+e = previous->data.ptr;
+val = e->data.val;
+rc = e->expiry && e->expiry <= time(NULL) ? -1 : val;
+
+DEBUG(D_dns) debug_printf("DNS lookup of %.255s (%s): %scached value %s%s\n",
+ name, dns_text_type(type),
+ rc == -1 ? "" : "using ",
+ dns_rc_names[val],
+ rc == -1 ? " past valid time" : "");
+
+return rc;
+}
+
+
+
+/* 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.
+
+Return TRUE iff it seemed ok */
+
+static BOOL
+fake_dnsa_len_for_fail(dns_answer * dnsa, int type)
+{
+const HEADER * h = (const HEADER *)dnsa->answer;
+
+if ( h->qr == 1 /* a response */
+ && h->opcode == QUERY
+ && h->tc == 0 /* nmessage not truncated */
+ && (h->rcode == NOERROR || h->rcode == NXDOMAIN)
+ && ( ntohs(h->qdcount) == 1 /* one question record */
+ || f.running_in_test_harness)
+ && ntohs(h->ancount) == 0 /* no answer records */
+ && ntohs(h->nscount) >= 1) /* authority records */
+ {
+ DEBUG(D_dns) debug_printf("faking res_search(%s) response length as %d\n",
+ dns_text_type(type), (int)sizeof(dnsa->answer));
+ dnsa->answerlen = sizeof(dnsa->answer);
+ return TRUE;
+ }
+DEBUG(D_dns) debug_printf("DNS: couldn't fake dnsa len\n");
+/* Maybe we should just do a second lookup for an SOA? */
+return FALSE;
+}
+
+
+/* Return the TTL suitable for an NXDOMAIN result, which is given
+in the SOA. We hope that one was returned in the lookup, and do not
+bother doing a separate lookup; if not found return a forever TTL.
+*/
+
+time_t
+dns_expire_from_soa(dns_answer * dnsa, int type)
+{
+dns_scan dnss;
+
+if (fake_dnsa_len_for_fail(dnsa, type))
+ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_AUTHORITY);
+ rr; rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)
+ ) if (rr->type == T_SOA)
+ {
+ const uschar * p = rr->data;
+ uschar discard_buf[256];
+ int len;
+ unsigned long ttl;
+
+ /* Skip the mname & rname strings */
+
+ if ((len = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen,
+ p, (DN_EXPAND_ARG4_TYPE)discard_buf, 256)) < 0)
+ break;
+ p += len;
+ if ((len = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen,
+ p, (DN_EXPAND_ARG4_TYPE)discard_buf, 256)) < 0)
+ break;
+ p += len;
+
+ /* Skip the SOA serial, refresh, retry & expire. Grab the TTL */
+
+ if (p > dnsa->answer + dnsa->answerlen - 5 * INT32SZ)
+ break;
+ p += 4 * INT32SZ;
+ GETLONG(ttl, p);
+
+ return time(NULL) + ttl;
+ }
+
+DEBUG(D_dns) debug_printf("DNS: no SOA record found for neg-TTL\n");
+return 0;
+}
+
+
+/*************************************************
+* 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-
+Authoritative 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.
+
+The dns_answer structure is pretty big; enough to hold a max-sized DNS message
+- so best allocated from fast-release memory. As of writing, all our callers
+use a stack-auto variable.
+
+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, const uschar * name, int type)
+{
+int rc;
+#ifndef STAND_ALONE
+const uschar * save_domain;
+#endif
+
+/* 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.
+*/
+
+if ((rc = dns_fail_cache_hit(name, type)) > 0)
+ {
+ dnsa->answerlen = -1;
+ return rc;
+ }
+
+#ifdef SUPPORT_I18N
+/* Convert all names to a-label form before doing lookup */
+ {
+ uschar * alabel;
+ uschar * errstr = NULL;
+ DEBUG(D_dns) if (string_is_utf8(name))
+ debug_printf("convert utf8 '%s' to alabel for for lookup\n", name);
+ if ((alabel = string_domain_utf8_to_alabel(name, &errstr)), errstr)
+ {
+ DEBUG(D_dns)
+ debug_printf("DNS name '%s' utf8 conversion to alabel failed: %s\n", name,
+ errstr);
+ f.host_find_failed_syntax = TRUE;
+ return DNS_NOMATCH;
+ }
+ name = alabel;
+ }
+#endif
+
+/* If configured, check the hygiene 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 they aren't used any
+more.) */
+
+#ifndef STAND_ALONE /* Omit this for stand-alone tests */
+
+if (check_dns_names_pattern[0] != 0 && type != T_PTR && type != T_TXT)
+ {
+ dns_pattern_init();
+ if (!regex_match(regex_check_dns_names, name, -1, NULL))
+ {
+ DEBUG(D_dns)
+ debug_printf("DNS name syntax check failed: %s (%s)\n", name,
+ dns_text_type(type));
+ f.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 ((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. */
+
+h_errno = 0;
+dnsa->answerlen = f.running_in_test_harness
+ ? fakens_search(name, type, dnsa->answer, sizeof(dnsa->answer))
+ : res_search(CCS name, C_IN, type, dnsa->answer, sizeof(dnsa->answer));
+
+if (dnsa->answerlen > (int) sizeof(dnsa->answer))
+ {
+ DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) resulted in overlong packet"
+ " (size %d), truncating to %u.\n",
+ name, dns_text_type(type), dnsa->answerlen, (unsigned int) sizeof(dnsa->answer));
+ dnsa->answerlen = sizeof(dnsa->answer);
+ }
+
+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_fail_return(name, type, dns_expire_from_soa(dnsa, 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_domain = deliver_domain;
+ deliver_domain = string_copy(name); /* set $domain */
+ rc = match_isinlist(name, CUSS &dns_again_means_nonexist, 0,
+ &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL);
+ deliver_domain = save_domain;
+ if (rc != OK)
+ {
+ DEBUG(D_dns) debug_printf("returning DNS_AGAIN\n");
+ return dns_fail_return(name, type, 0, DNS_AGAIN);
+ }
+ DEBUG(D_dns) debug_printf("%s is in dns_again_means_nonexist: returning "
+ "DNS_NOMATCH\n", name);
+ return dns_fail_return(name, type, dns_expire_from_soa(dnsa, type), DNS_NOMATCH);
+
+#else /* For stand-alone tests */
+ return dns_fail_return(name, type, 0, 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_fail_return(name, type, 0, 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_fail_return(name, type, dns_expire_from_soa(dnsa, 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_fail_return(name, type, 0, 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.
+By default, follow one CNAME since a resolver has been seen, faced with
+an MX request and a CNAME (to an A) but no MX present, returning the CNAME.
+
+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. In international mode "different" means "alabel
+forms are different".
+
+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, const uschar *name, int type,
+ const uschar **fully_qualified_name)
+{
+const uschar *orig_name = name;
+BOOL secure_so_far = TRUE;
+
+/* By default, assume the resolver follows CNAME chains (and returns NODATA for
+an unterminated one). If it also does that for a CNAME loop, fine; if it returns
+a CNAME (maybe the last?) whine about it. However, retain the coding for dumb
+resolvers hiding behind a config variable. Loop to follow CNAME chains so far,
+but no further... The testsuite tests the latter case, mostly assuming that the
+former will work. */
+
+for (int i = 0; i <= dns_cname_loops; i++)
+ {
+ uschar * data;
+ dns_record cname_rr, type_rr;
+ dns_scan dnss;
+ int 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 (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS);
+ rr; 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)
+ {
+ uschar * rr_name = cname_rr.data
+ ? cname_rr.name : type_rr.data ? type_rr.name : NULL;
+ if ( rr_name
+ && Ustrcmp(rr_name, *fully_qualified_name) != 0
+ && rr_name[0] != '*'
+#ifdef SUPPORT_I18N
+ && ( !string_is_utf8(*fully_qualified_name)
+ || Ustrcmp(rr_name,
+ string_domain_utf8_to_alabel(*fully_qualified_name, NULL)) != 0
+ )
+#endif
+ )
+ *fully_qualified_name = string_copy_dnsdomain(rr_name);
+ }
+
+ /* If any data records of the correct type were found, we are done. */
+
+ if (type_rr.data)
+ {
+ if (!secure_so_far) /* mark insecure if any element of CNAME chain was */
+ dns_set_insecure(dnsa);
+ 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)
+ return DNS_FAIL;
+
+ /* DNS data comes from the outside, hence tainted */
+ data = store_get(256, GET_TAINTED);
+ if (dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen,
+ cname_rr.data, (DN_EXPAND_ARG4_TYPE)data, 256) < 0)
+ return DNS_FAIL;
+ name = data;
+
+ if (!dns_is_secure(dnsa))
+ secure_so_far = FALSE;
+
+ 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 features
+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, const uschar *name, int type,
+ const uschar **fully_qualified_name)
+{
+switch (type)
+ {
+ /* The "mx hosts only" type doesn't require any special action here */
+ case 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. */
+ case T_ZNS:
+ type = T_NS;
+ /* FALLTHROUGH */
+ case T_SOA:
+ {
+ const uschar *d = name;
+ while (d != 0)
+ {
+ int rc = dns_lookup(dnsa, d, type, 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. */
+ case T_CSA:
+ {
+ uschar *srvname, *namesuff, *tld;
+ int priority, dummy_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 = string_copy(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) return DNS_NOMATCH;
+
+ 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; rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)) if (rr->type == T_SRV)
+ {
+ const uschar * p = rr->data;
+
+ /* Extract the numerical SRV fields (p is incremented) */
+ GETSHORT(priority, p);
+ GETSHORT(dummy_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;
+ }
+
+ default:
+ if (type >= 0)
+ return dns_lookup(dnsa, name, type, fully_qualified_name);
+ }
+
+/* Control should never reach here */
+
+return DNS_FAIL;
+}
+
+
+
+
+
+/*************************************************
+* Get address(es) from DNS record *
+*************************************************/
+
+/* The record type is either T_A for an IPv4 address or T_AAAA for an IPv6 address.
+
+Argument:
+ dnsa the DNS answer block
+ rr the RR
+
+Returns: pointer to a chain of dns_address items; NULL when the dnsa was overrun
+*/
+
+dns_address *
+dns_address_from_rr(dns_answer *dnsa, dns_record *rr)
+{
+dns_address * yield = NULL;
+uschar * dnsa_lim = dnsa->answer + dnsa->answerlen;
+
+if (rr->type == T_A)
+ {
+ uschar *p = US rr->data;
+ if (p + 4 <= dnsa_lim)
+ {
+ /* the IP is not regarded as tainted */
+ yield = store_get(sizeof(dns_address) + 20, GET_UNTAINTED);
+ (void)sprintf(CS yield->address, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
+ yield->next = NULL;
+ }
+ }
+
+#if HAVE_IPV6
+
+else
+ {
+ if (rr->data + 16 <= dnsa_lim)
+ {
+ struct in6_addr in6;
+ for (int i = 0; i < 16; i++) in6.s6_addr[i] = rr->data[i];
+ yield = store_get(sizeof(dns_address) + 50, GET_UNTAINTED);
+ inet_ntop(AF_INET6, &in6, CS yield->address, 50);
+ yield->next = NULL;
+ }
+ }
+#endif /* HAVE_IPV6 */
+
+return yield;
+}
+
+
+
+void
+dns_pattern_init(void)
+{
+if (check_dns_names_pattern[0] != 0 && !regex_check_dns_names)
+ regex_check_dns_names =
+ regex_must_compile(check_dns_names_pattern, FALSE, TRUE);
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of dns.c */
diff --git a/src/dnsbl.c b/src/dnsbl.c
new file mode 100644
index 0000000..db839af
--- /dev/null
+++ b/src/dnsbl.c
@@ -0,0 +1,651 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions concerned with dnsbls */
+
+
+#include "exim.h"
+
+/* Structure for caching DNSBL lookups */
+
+typedef struct dnsbl_cache_block {
+ time_t expiry;
+ 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
+
+
+/*************************************************
+* 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 = store_get_dns_answer();
+dns_scan dnss;
+tree_node *t;
+dnsbl_cache_block *cb;
+int old_pool = store_pool;
+uschar * query;
+int qlen, yield;
+
+/* Construct the specific query domainname */
+
+query = string_sprintf("%s.%s", prepend, domain);
+if ((qlen = Ustrlen(query)) >= 256)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "dnslist query is too long "
+ "(ignored): %s...", query);
+ yield = FAIL;
+ goto out;
+ }
+
+/* Look for this query in the cache. */
+
+if ( (t = tree_search(dnsbl_cache, query))
+ && (cb = t->data.ptr)->expiry > time(NULL)
+ )
+
+/* Previous lookup was cached */
+
+ {
+ HDEBUG(D_dnsbl) debug_printf("dnslists: using result of previous lookup\n");
+ }
+
+/* If not cached from a previous lookup, we must do a DNS lookup, and
+cache the result in permanent memory. */
+
+else
+ {
+ uint ttl = 3600; /* max TTL for positive cache entries */
+
+ store_pool = POOL_PERM;
+
+ if (t)
+ {
+ HDEBUG(D_dnsbl) debug_printf("cached data found but past valid time; ");
+ }
+
+ else
+ { /* Set up a tree entry to cache the lookup */
+ t = store_get(sizeof(tree_node) + qlen + 1 + 1, query);
+ Ustrcpy(t->name, query);
+ t->data.ptr = cb = store_get(sizeof(dnsbl_cache_block), GET_UNTAINTED);
+ (void)tree_insertnode(&dnsbl_cache, t);
+ }
+
+ /* Do the DNS lookup . */
+
+ 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 are no longer supported. 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.
+
+ Mark the cache entry with the "now" plus the minimum of the address TTLs,
+ or the RFC 2308 negative-cache value from the SOA if none were found. */
+
+ switch (cb->rc)
+ {
+ case DNS_SUCCEED:
+ {
+ dns_address ** addrp = &cb->rhs;
+ dns_address * da;
+ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT))
+ if (rr->type == T_A && (da = dns_address_from_rr(dnsa, rr)))
+ {
+ *addrp = da;
+ while (da->next) da = da->next;
+ addrp = &da->next;
+ if (ttl > rr->ttl) ttl = rr->ttl;
+ }
+
+ if (cb->rhs)
+ {
+ cb->expiry = time(NULL) + ttl;
+ break;
+ }
+
+ /* 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. */
+
+ cb->rc = DNS_NODATA;
+ }
+ /*FALLTHROUGH*/
+
+ case DNS_NOMATCH:
+ case DNS_NODATA:
+ {
+ /* Although there already is a neg-cache layer maintained by
+ dns_basic_lookup(), we have a dnslist cache entry allocated and
+ tree-inserted. So we may as well use it. */
+
+ time_t soa_negttl = dns_expire_from_soa(dnsa, T_A);
+ cb->expiry = soa_negttl ? soa_negttl : time(NULL) + ttl;
+ break;
+ }
+
+ default:
+ cb->expiry = time(NULL) + ttl;
+ break;
+ }
+
+ store_pool = old_pool;
+ HDEBUG(D_dnsbl) debug_printf("dnslists: wrote cache entry, ttl=%d\n",
+ (int)(cb->expiry - time(NULL)));
+ }
+
+/* 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; 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)
+ {
+ for (da = cb->rhs; da; da = da->next)
+ {
+ int ipsep = ',';
+ const uschar *ptr = iplist;
+ uschar *res;
+
+ /* Handle exact matching */
+
+ if (!bitmask)
+ {
+ while ((res = string_nextinlist(&ptr, &ipsep, NULL, 0)))
+ if (Ustrcmp(CS da->address, res) == 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)
+ if ((address[0] & 0xff000000) != 0x7f000000) /* 127.0.0.0/8 */
+ log_write(0, LOG_MAIN,
+ "DNS list lookup for %s at %s returned %s;"
+ " not in 127.0/8 and discarded",
+ keydomain, domain, da->address);
+
+ else
+ mask = address[0];
+
+ /* Scan the returned addresses, skipping any that are IPv6 */
+
+ while ((res = string_nextinlist(&ptr, &ipsep, NULL, 0)))
+ if (host_aton(res, address) == 1)
+ 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 ? "=" : "",
+ bitmask ? '&' : '=', iplist);
+ }
+ yield = FAIL;
+ goto out;
+ }
+ }
+
+ /* No address list check; discard any illegal returns and give up if
+ none remain. */
+
+ else
+ {
+ BOOL ok = FALSE;
+ for (da = cb->rhs; da; da = da->next)
+ {
+ int address[4];
+
+ if ( host_aton(da->address, address) == 1 /* ipv4 */
+ && (address[0] & 0xff000000) == 0x7f000000 /* 127.0.0.0/8 */
+ )
+ ok = TRUE;
+ else
+ log_write(0, LOG_MAIN,
+ "DNS list lookup for %s at %s returned %s;"
+ " not in 127.0/8 and discarded",
+ keydomain, domain, da->address);
+ }
+ if (!ok)
+ {
+ yield = FAIL;
+ goto out;
+ }
+ }
+
+ /* 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)
+ {
+ yield = one_check_dnsbl(domain_txt, domain_txt, keydomain, prepend, NULL,
+ FALSE, match_type, defer_return);
+ goto out;
+ }
+
+ /* 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)
+ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT))
+ if (rr->type == T_TXT)
+ {
+ int len = (rr->data)[0];
+ if (len > 511) len = 127;
+ store_pool = POOL_PERM;
+ cb->text = string_copyn_taint(CUS (rr->data+1), len, GET_TAINTED);
+ store_pool = old_pool;
+ break;
+ }
+ }
+
+ dnslist_value = addlist;
+ dnslist_text = cb->text;
+ yield = OK;
+ goto out;
+ }
+
+/* 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");
+ yield = defer_return;
+ goto out;
+ }
+
+/* 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);
+ }
+
+yield = FAIL;
+
+out:
+
+store_free_dns_answer(dnsa);
+return yield;
+}
+
+
+
+
+/*************************************************
+* 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:
+ where the acl type
+ listptr the domain/address/data list
+ log_msgptr log message on error
+
+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(int where, const uschar ** listptr, uschar ** log_msgptr)
+{
+int sep = 0;
+int defer_return = FAIL;
+const uschar *list = *listptr;
+uschar *domain;
+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, NULL, 0)))
+ {
+ int rc;
+ BOOL bitmask = FALSE;
+ int match_type = 0;
+ uschar *domain_txt;
+ uschar *comma;
+ uschar *iplist;
+ uschar *key;
+
+ HDEBUG(D_dnsbl) debug_printf("dnslists 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 */
+
+ if ((key = Ustrchr(domain, '/'))) *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. */
+
+ if (!(iplist = Ustrchr(domain, '=')))
+ {
+ bitmask = TRUE;
+ iplist = Ustrchr(domain, '&');
+ }
+
+ if (iplist) /* 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;
+ if ((comma = Ustrchr(domain, ',')))
+ {
+ *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 (uschar * s = domain; *s; 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 (uschar * s = domain_txt; *s; 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)
+ {
+ if (where == ACL_WHERE_NOTSMTP_START || where == ACL_WHERE_NOTSMTP)
+ {
+ *log_msgptr = string_sprintf
+ ("cannot test auto-keyed dnslists condition in %s ACL",
+ acl_wherenames[where]);
+ return ERROR;
+ }
+ if (!sender_host_address) 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 keyrevadd[128];
+
+ while ((keydomain = string_nextinlist(CUSS &key, &keysep, NULL, 0)))
+ {
+ 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 dnsbl.c.c */
diff --git a/src/drtables.c b/src/drtables.c
new file mode 100644
index 0000000..513ef6c
--- /dev/null
+++ b/src/drtables.c
@@ -0,0 +1,818 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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;
+
+/* Table of information about all possible authentication mechanisms. 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_EXTERNAL
+#include "auths/external.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
+
+#ifdef AUTH_TLS
+#include "auths/tls.h"
+#endif
+
+auth_info auths_available[] = {
+
+/* Checking by an expansion condition on plain text */
+
+#ifdef AUTH_CRAM_MD5
+ {
+ .driver_name = US"cram_md5", /* lookup name */
+ .options = auth_cram_md5_options,
+ .options_count = &auth_cram_md5_options_count,
+ .options_block = &auth_cram_md5_option_defaults,
+ .options_len = sizeof(auth_cram_md5_options_block),
+ .init = auth_cram_md5_init,
+ .servercode = auth_cram_md5_server,
+ .clientcode = auth_cram_md5_client,
+ .version_report = NULL,
+ .macros_create = NULL,
+ },
+#endif
+
+#ifdef AUTH_CYRUS_SASL
+ {
+ .driver_name = US"cyrus_sasl",
+ .options = auth_cyrus_sasl_options,
+ .options_count = &auth_cyrus_sasl_options_count,
+ .options_block = &auth_cyrus_sasl_option_defaults,
+ .options_len = sizeof(auth_cyrus_sasl_options_block),
+ .init = auth_cyrus_sasl_init,
+ .servercode = auth_cyrus_sasl_server,
+ .clientcode = NULL,
+ .version_report = auth_cyrus_sasl_version_report,
+ .macros_create = NULL,
+ },
+#endif
+
+#ifdef AUTH_DOVECOT
+ {
+ .driver_name = US"dovecot",
+ .options = auth_dovecot_options,
+ .options_count = &auth_dovecot_options_count,
+ .options_block = &auth_dovecot_option_defaults,
+ .options_len = sizeof(auth_dovecot_options_block),
+ .init = auth_dovecot_init,
+ .servercode = auth_dovecot_server,
+ .clientcode = NULL,
+ .version_report = NULL,
+ .macros_create = NULL,
+ },
+#endif
+
+#ifdef AUTH_EXTERNAL
+ {
+ .driver_name = US"external",
+ .options = auth_external_options,
+ .options_count = &auth_external_options_count,
+ .options_block = &auth_external_option_defaults,
+ .options_len = sizeof(auth_external_options_block),
+ .init = auth_external_init,
+ .servercode = auth_external_server,
+ .clientcode = auth_external_client,
+ .version_report = NULL,
+ .macros_create = NULL,
+ },
+#endif
+
+#ifdef AUTH_GSASL
+ {
+ .driver_name = US"gsasl",
+ .options = auth_gsasl_options,
+ .options_count = &auth_gsasl_options_count,
+ .options_block = &auth_gsasl_option_defaults,
+ .options_len = sizeof(auth_gsasl_options_block),
+ .init = auth_gsasl_init,
+ .servercode = auth_gsasl_server,
+ .clientcode = auth_gsasl_client,
+ .version_report = auth_gsasl_version_report,
+ .macros_create = auth_gsasl_macros,
+ },
+#endif
+
+#ifdef AUTH_HEIMDAL_GSSAPI
+ {
+ .driver_name = US"heimdal_gssapi",
+ .options = auth_heimdal_gssapi_options,
+ .options_count = &auth_heimdal_gssapi_options_count,
+ .options_block = &auth_heimdal_gssapi_option_defaults,
+ .options_len = sizeof(auth_heimdal_gssapi_options_block),
+ .init = auth_heimdal_gssapi_init,
+ .servercode = auth_heimdal_gssapi_server,
+ .clientcode = NULL,
+ .version_report = auth_heimdal_gssapi_version_report,
+ .macros_create = NULL,
+ },
+#endif
+
+#ifdef AUTH_PLAINTEXT
+ {
+ .driver_name = US"plaintext",
+ .options = auth_plaintext_options,
+ .options_count = &auth_plaintext_options_count,
+ .options_block = &auth_plaintext_option_defaults,
+ .options_len = sizeof(auth_plaintext_options_block),
+ .init = auth_plaintext_init,
+ .servercode = auth_plaintext_server,
+ .clientcode = auth_plaintext_client,
+ .version_report = NULL,
+ .macros_create = NULL,
+ },
+#endif
+
+#ifdef AUTH_SPA
+ {
+ .driver_name = US"spa",
+ .options = auth_spa_options,
+ .options_count = &auth_spa_options_count,
+ .options_block = &auth_spa_option_defaults,
+ .options_len = sizeof(auth_spa_options_block),
+ .init = auth_spa_init,
+ .servercode = auth_spa_server,
+ .clientcode = auth_spa_client,
+ .version_report = NULL,
+ .macros_create = NULL,
+ },
+#endif
+
+#ifdef AUTH_TLS
+ {
+ .driver_name = US"tls",
+ .options = auth_tls_options,
+ .options_count = &auth_tls_options_count,
+ .options_block = &auth_tls_option_defaults,
+ .options_len = sizeof(auth_tls_options_block),
+ .init = auth_tls_init,
+ .servercode = auth_tls_server,
+ .clientcode = NULL,
+ .version_report = NULL,
+ .macros_create = NULL,
+ },
+#endif
+
+ { .driver_name = US"" } /* end marker */
+};
+
+
+/* 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 EXPERIMENTAL_QUEUEFILE
+#include "transports/queuefile.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
+ {
+ .driver_name = US"accept",
+ .options = accept_router_options,
+ .options_count = &accept_router_options_count,
+ .options_block = &accept_router_option_defaults,
+ .options_len = sizeof(accept_router_options_block),
+ .init = accept_router_init,
+ .code = accept_router_entry,
+ .tidyup = NULL, /* no tidyup entry */
+ .ri_flags = ri_yestransport
+ },
+#endif
+#ifdef ROUTER_DNSLOOKUP
+ {
+ .driver_name = US"dnslookup",
+ .options = dnslookup_router_options,
+ .options_count = &dnslookup_router_options_count,
+ .options_block = &dnslookup_router_option_defaults,
+ .options_len = sizeof(dnslookup_router_options_block),
+ .init = dnslookup_router_init,
+ .code = dnslookup_router_entry,
+ .tidyup = NULL, /* no tidyup entry */
+ .ri_flags = ri_yestransport
+ },
+#endif
+#ifdef ROUTER_IPLITERAL
+ {
+ .driver_name = US"ipliteral",
+ .options = ipliteral_router_options,
+ .options_count = &ipliteral_router_options_count,
+ .options_block = &ipliteral_router_option_defaults,
+ .options_len = sizeof(ipliteral_router_options_block),
+ .init = ipliteral_router_init,
+ .code = ipliteral_router_entry,
+ .tidyup = NULL, /* no tidyup entry */
+ .ri_flags = ri_yestransport
+ },
+#endif
+#ifdef ROUTER_IPLOOKUP
+ {
+ .driver_name = US"iplookup",
+ .options = iplookup_router_options,
+ .options_count = &iplookup_router_options_count,
+ .options_block = &iplookup_router_option_defaults,
+ .options_len = sizeof(iplookup_router_options_block),
+ .init = iplookup_router_init,
+ .code = iplookup_router_entry,
+ .tidyup = NULL, /* no tidyup entry */
+ .ri_flags = ri_notransport
+ },
+#endif
+#ifdef ROUTER_MANUALROUTE
+ {
+ .driver_name = US"manualroute",
+ .options = manualroute_router_options,
+ .options_count = &manualroute_router_options_count,
+ .options_block = &manualroute_router_option_defaults,
+ .options_len = sizeof(manualroute_router_options_block),
+ .init = manualroute_router_init,
+ .code = manualroute_router_entry,
+ .tidyup = NULL, /* no tidyup entry */
+ .ri_flags = 0
+ },
+#endif
+#ifdef ROUTER_QUERYPROGRAM
+ {
+ .driver_name = US"queryprogram",
+ .options = queryprogram_router_options,
+ .options_count = &queryprogram_router_options_count,
+ .options_block = &queryprogram_router_option_defaults,
+ .options_len = sizeof(queryprogram_router_options_block),
+ .init = queryprogram_router_init,
+ .code = queryprogram_router_entry,
+ .tidyup = NULL, /* no tidyup entry */
+ .ri_flags = 0
+ },
+#endif
+#ifdef ROUTER_REDIRECT
+ {
+ .driver_name = US"redirect",
+ .options = redirect_router_options,
+ .options_count = &redirect_router_options_count,
+ .options_block = &redirect_router_option_defaults,
+ .options_len = sizeof(redirect_router_options_block),
+ .init = redirect_router_init,
+ .code = redirect_router_entry,
+ .tidyup = NULL, /* no tidyup entry */
+ .ri_flags = ri_notransport
+ },
+#endif
+ { US"" }
+};
+
+
+
+transport_info transports_available[] = {
+#ifdef TRANSPORT_APPENDFILE
+ {
+ .driver_name = US"appendfile",
+ .options = appendfile_transport_options,
+ .options_count = &appendfile_transport_options_count,
+ .options_block = &appendfile_transport_option_defaults, /* private options defaults */
+ .options_len = sizeof(appendfile_transport_options_block),
+ .init = appendfile_transport_init,
+ .code = appendfile_transport_entry,
+ .tidyup = NULL,
+ .closedown = NULL,
+ .local = TRUE
+ },
+#endif
+#ifdef TRANSPORT_AUTOREPLY
+ {
+ .driver_name = US"autoreply",
+ .options = autoreply_transport_options,
+ .options_count = &autoreply_transport_options_count,
+ .options_block = &autoreply_transport_option_defaults,
+ .options_len = sizeof(autoreply_transport_options_block),
+ .init = autoreply_transport_init,
+ .code = autoreply_transport_entry,
+ .tidyup = NULL,
+ .closedown = NULL,
+ .local = TRUE
+ },
+#endif
+#ifdef TRANSPORT_LMTP
+ {
+ .driver_name = US"lmtp",
+ .options = lmtp_transport_options,
+ .options_count = &lmtp_transport_options_count,
+ .options_block = &lmtp_transport_option_defaults,
+ .options_len = sizeof(lmtp_transport_options_block),
+ .init = lmtp_transport_init,
+ .code = lmtp_transport_entry,
+ .tidyup = NULL,
+ .closedown = NULL,
+ .local = TRUE
+ },
+#endif
+#ifdef TRANSPORT_PIPE
+ {
+ .driver_name = US"pipe",
+ .options = pipe_transport_options,
+ .options_count = &pipe_transport_options_count,
+ .options_block = &pipe_transport_option_defaults,
+ .options_len = sizeof(pipe_transport_options_block),
+ .init = pipe_transport_init,
+ .code = pipe_transport_entry,
+ .tidyup = NULL,
+ .closedown = NULL,
+ .local = TRUE
+ },
+#endif
+#ifdef EXPERIMENTAL_QUEUEFILE
+ {
+ .driver_name = US"queuefile",
+ .options = queuefile_transport_options,
+ .options_count = &queuefile_transport_options_count,
+ .options_block = &queuefile_transport_option_defaults,
+ .options_len = sizeof(queuefile_transport_options_block),
+ .init = queuefile_transport_init,
+ .code = queuefile_transport_entry,
+ .tidyup = NULL,
+ .closedown = NULL,
+ .local = TRUE
+ },
+#endif
+#ifdef TRANSPORT_SMTP
+ {
+ .driver_name = US"smtp",
+ .options = smtp_transport_options,
+ .options_count = &smtp_transport_options_count,
+ .options_block = &smtp_transport_option_defaults,
+ .options_len = sizeof(smtp_transport_options_block),
+ .init = smtp_transport_init,
+ .code = smtp_transport_entry,
+ .tidyup = NULL,
+ .closedown = smtp_transport_closedown,
+ .local = FALSE
+ },
+#endif
+ { US"" }
+};
+
+#ifndef MACRO_PREDEF
+
+gstring *
+auth_show_supported(gstring * g)
+{
+g = string_cat(g, US"Authenticators:");
+for (auth_info * ai = auths_available; ai->driver_name[0]; ai++)
+ g = string_fmt_append(g, " %s", ai->driver_name);
+return string_cat(g, US"\n");
+}
+
+gstring *
+route_show_supported(gstring * g)
+{
+g = string_cat(g, US"Routers:");
+for (router_info * rr = routers_available; rr->driver_name[0]; rr++)
+ g = string_fmt_append(g, " %s", rr->driver_name);
+return string_cat(g, US"\n");
+}
+
+gstring *
+transport_show_supported(gstring * g)
+{
+g = string_cat(g, US"Transports:");
+#ifdef TRANSPORT_APPENDFILE
+ g = string_cat(g, US" appendfile");
+ #ifdef SUPPORT_MAILDIR
+ g = string_cat(g, US"/maildir"); /* damn these subclasses */
+ #endif
+ #ifdef SUPPORT_MAILSTORE
+ g = string_cat(g, US"/mailstore");
+ #endif
+ #ifdef SUPPORT_MBX
+ g = string_cat(g, US"/mbx");
+ #endif
+#endif
+#ifdef TRANSPORT_AUTOREPLY
+ g = string_cat(g, US" autoreply");
+#endif
+#ifdef TRANSPORT_LMTP
+ g = string_cat(g, US" lmtp");
+#endif
+#ifdef TRANSPORT_PIPE
+ g = string_cat(g, US" pipe");
+#endif
+#ifdef EXPERIMENTAL_QUEUEFILE
+ g = string_cat(g, US" queuefile");
+#endif
+#ifdef TRANSPORT_SMTP
+ g = string_cat(g, US" smtp");
+#endif
+return string_cat(g, US"\n");
+}
+
+
+
+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_get(sizeof(struct lookupmodulestr), GET_UNTAINTED);
+
+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_CDB) && LOOKUP_CDB!=2
+extern lookup_module_info cdb_lookup_module_info;
+#endif
+#if defined(LOOKUP_DBM) && LOOKUP_DBM!=2
+extern lookup_module_info dbmdb_lookup_module_info;
+#endif
+#if defined(LOOKUP_DNSDB) && LOOKUP_DNSDB!=2
+extern lookup_module_info dnsdb_lookup_module_info;
+#endif
+#if defined(LOOKUP_DSEARCH) && LOOKUP_DSEARCH!=2
+extern lookup_module_info dsearch_lookup_module_info;
+#endif
+#if defined(LOOKUP_IBASE) && LOOKUP_IBASE!=2
+extern lookup_module_info ibase_lookup_module_info;
+#endif
+#if defined(LOOKUP_JSON)
+extern lookup_module_info json_lookup_module_info;
+#endif
+#if defined(LOOKUP_LDAP)
+extern lookup_module_info ldap_lookup_module_info;
+#endif
+#if defined(LOOKUP_LSEARCH) && LOOKUP_LSEARCH!=2
+extern lookup_module_info lsearch_lookup_module_info;
+#endif
+#if defined(LOOKUP_MYSQL) && LOOKUP_MYSQL!=2
+extern lookup_module_info mysql_lookup_module_info;
+#endif
+#if defined(LOOKUP_NIS) && LOOKUP_NIS!=2
+extern lookup_module_info nis_lookup_module_info;
+#endif
+#if defined(LOOKUP_NISPLUS) && LOOKUP_NISPLUS!=2
+extern lookup_module_info nisplus_lookup_module_info;
+#endif
+#if defined(LOOKUP_ORACLE) && LOOKUP_ORACLE!=2
+extern lookup_module_info oracle_lookup_module_info;
+#endif
+#if defined(LOOKUP_PASSWD) && LOOKUP_PASSWD!=2
+extern lookup_module_info passwd_lookup_module_info;
+#endif
+#if defined(LOOKUP_PGSQL) && LOOKUP_PGSQL!=2
+extern lookup_module_info pgsql_lookup_module_info;
+#endif
+#if defined(LOOKUP_REDIS) && LOOKUP_REDIS!=2
+extern lookup_module_info redis_lookup_module_info;
+#endif
+#if defined(LOOKUP_LMDB)
+extern lookup_module_info lmdb_lookup_module_info;
+#endif
+#if defined(SUPPORT_SPF)
+extern lookup_module_info spf_lookup_module_info;
+#endif
+#if defined(LOOKUP_SQLITE) && LOOKUP_SQLITE!=2
+extern lookup_module_info sqlite_lookup_module_info;
+#endif
+#if defined(LOOKUP_TESTDB) && LOOKUP_TESTDB!=2
+extern lookup_module_info testdb_lookup_module_info;
+#endif
+#if defined(LOOKUP_WHOSON) && LOOKUP_WHOSON!=2
+extern lookup_module_info whoson_lookup_module_info;
+#endif
+
+extern lookup_module_info readsock_lookup_module_info;
+
+
+void
+init_lookup_list(void)
+{
+#ifdef LOOKUP_MODULE_DIR
+DIR *dd;
+struct dirent *ent;
+int countmodules = 0;
+int moduleerrors = 0;
+#endif
+static BOOL lookup_list_init_done = FALSE;
+rmark reset_point;
+
+if (lookup_list_init_done)
+ return;
+reset_point = store_mark();
+lookup_list_init_done = TRUE;
+
+#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
+
+#ifdef LOOKUP_JSON
+addlookupmodule(NULL, &json_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
+
+#if defined(LOOKUP_REDIS) && LOOKUP_REDIS!=2
+addlookupmodule(NULL, &redis_lookup_module_info);
+#endif
+
+#ifdef LOOKUP_LMDB
+addlookupmodule(NULL, &lmdb_lookup_module_info);
+#endif
+
+#ifdef SUPPORT_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
+
+addlookupmodule(NULL, &readsock_lookup_module_info);
+
+#ifdef LOOKUP_MODULE_DIR
+if (!(dd = exim_opendir(LOOKUP_MODULE_DIR)))
+ {
+ 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
+ {
+ const pcre2_code *regex_islookupmod = regex_must_compile(
+ US"\\." DYNLIB_FN_EXT "$", FALSE, TRUE);
+
+ DEBUG(D_lookup) debug_printf("Loading lookup modules from %s\n", LOOKUP_MODULE_DIR);
+ while ((ent = readdir(dd)))
+ {
+ char * name = ent->d_name;
+ int len = (int)strlen(name);
+ if (regex_match(regex_islookupmod, US name, len, NUL))
+ {
+ 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);
+
+ if (!(dl = dlopen(CS big_buffer, RTLD_NOW)))
+ {
+ errormsg = dlerror();
+ fprintf(stderr, "Error loading %s: %s\n", name, errormsg);
+ log_write(0, LOG_MAIN|LOG_PANIC, "Error loading lookup module %s: %s\n", name, errormsg);
+ moduleerrors++;
+ 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()))
+ {
+ fprintf(stderr, "%s does not appear to be a lookup module (%s)\n", name, errormsg);
+ log_write(0, LOG_MAIN|LOG_PANIC, "%s does not appear to be a lookup module (%s)\n", name, errormsg);
+ dlclose(dl);
+ moduleerrors++;
+ continue;
+ }
+ if (info->magic != LOOKUP_MODULE_INFO_MAGIC)
+ {
+ fprintf(stderr, "Lookup module %s is not compatible with this version of Exim\n", name);
+ log_write(0, LOG_MAIN|LOG_PANIC, "Lookup module %s is not compatible with this version of Exim\n", name);
+ dlclose(dl);
+ moduleerrors++;
+ continue;
+ }
+
+ addlookupmodule(dl, info);
+ DEBUG(D_lookup) debug_printf("Loaded \"%s\" (%d lookup types)\n", name, info->lookupcount);
+ countmodules++;
+ }
+ }
+ store_free((void*)regex_islookupmod);
+ closedir(dd);
+ }
+
+DEBUG(D_lookup) debug_printf("Loaded %d lookup modules\n", countmodules);
+#endif
+
+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 */
+for (struct lookupmodulestr * p = lookupmodules; p; p = p->next)
+ for (int j = 0; j < p->info->lookupcount; j++)
+ add_lookup_to_list(p->info->lookups[j]);
+store_reset(reset_point);
+/* just to be sure */
+lookupmodules = NULL;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of drtables.c */
diff --git a/src/dummies.c b/src/dummies.c
new file mode 100644
index 0000000..38b514b
--- /dev/null
+++ b/src/dummies.c
@@ -0,0 +1,139 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* Copyright (c) The Exim Maintainers 2021 */
+/* 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 gstring * string_vformat(gstring *, unsigned, const 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);
+}
+
+
+/*************************************************
+* Handle calls to print debug output *
+*************************************************/
+
+/* The message just gets written to stderr.
+We use tainted memory to format into just so that we can handle
+tainted arguments.
+
+Arguments:
+ format a printf() format
+ ... arguments for format
+
+Returns: nothing
+*/
+
+void
+debug_printf(char *format, ...)
+{
+va_list ap;
+rmark reset_point = store_mark();
+gstring * g = string_get_tainted(1024, TRUE);
+
+va_start(ap, format);
+
+if (!string_vformat(g, 0, format, ap))
+ {
+ char * s = "**** debug string overflowed buffer ****\n";
+ char * p = CS g->s + g->ptr;
+ int maxlen = g->size - (int)strlen(s) - 3;
+ if (p > g->s + maxlen) p = g->s + maxlen;
+ if (p > g->s && p[-1] != '\n') *p++ = '\n';
+ strcpy(p, s);
+ }
+
+fprintf(stderr, "%s", string_from_gstring(g));
+fflush(stderr);
+store_reset(reset_point);
+va_end(ap);
+}
+
+
+
+/*************************************************
+* SIGALRM handler *
+*************************************************/
+
+extern int sigalrm_seen;
+
+void
+sigalrm_handler(int sig)
+{
+sigalrm_seen = TRUE;
+}
+
+
+
+/*************************************************
+* Complete Dummies *
+*************************************************/
+
+int
+header_checkname(void *h, char *name, int len)
+{
+return 0;
+}
+
+void
+directory_make(char *parent, char *name, int mode, int 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)
+{
+return NULL;
+}
+#endif
+
+
+/* End of dummies.c */
diff --git a/src/enq.c b/src/enq.c
new file mode 100644
index 0000000..f7f8c9c
--- /dev/null
+++ b/src/enq.c
@@ -0,0 +1,123 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* Copyright (c) The Exim Maintainers 2021 */
+/* 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 increasing the count would take us past the given limit
+value return FALSE. If not, bump it and return TRUE. If not found, create
+one with value 1 and return TRUE.
+
+Arguments:
+ key string on which to serialize
+ lim parallelism limit
+
+Returns: TRUE if OK to proceed; FALSE otherwise
+*/
+
+
+BOOL
+enq_start(uschar *key, unsigned lim)
+{
+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. */
+
+if (!(dbm_file = dbfn_open(US"misc", O_RDWR, &dbblock, TRUE, TRUE)))
+ 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_enforce_length(dbm_file, key, sizeof(dbdata_serialize));
+if (serial_record && time(NULL) - serial_record->time_stamp < 6*60*60)
+ {
+ if (serial_record->count >= lim)
+ {
+ dbfn_close(dbm_file);
+ DEBUG(D_transport) debug_printf("outstanding serialization record for %s\n",
+ key);
+ return FALSE;
+ }
+ new_record.count = serial_record->count + 1;
+ }
+else
+ new_record.count = 1;
+
+/* We can proceed - insert a new record or update the old one. */
+
+DEBUG(D_transport) debug_printf("write serialization record for %s val %d\n",
+ key, new_record.count);
+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;
+dbdata_serialize *serial_record;
+
+DEBUG(D_transport) debug_printf("end serialized: %s\n", key);
+
+if ( !(dbm_file = dbfn_open(US"misc", O_RDWR, &dbblock, TRUE, TRUE))
+ || !(serial_record = dbfn_read_enforce_length(dbm_file, key, sizeof(dbdata_serialize)))
+ )
+ return;
+if (--serial_record->count > 0)
+ {
+ DEBUG(D_transport) debug_printf("write serialization record for %s val %d\n",
+ key, serial_record->count);
+ dbfn_write(dbm_file, key, serial_record, (int)sizeof(dbdata_serialize));
+ }
+else
+ {
+ DEBUG(D_transport) debug_printf("remove serialization record for %s\n", key);
+ dbfn_delete(dbm_file, key);
+ }
+dbfn_close(dbm_file);
+}
+
+/* End of enq.c */
diff --git a/src/environment.c b/src/environment.c
new file mode 100644
index 0000000..9cb90c8
--- /dev/null
+++ b/src/environment.c
@@ -0,0 +1,83 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Heiko Schlittermann 2016
+ * hs@schlittermann.de
+ * See the file NOTICE for conditions of use and distribution.
+ */
+
+#include "exim.h"
+
+extern char **environ;
+
+/* The cleanup_environment() function is used during the startup phase
+of the Exim process, right after reading the configurations main
+part, before any expansions take place. It retains the environment
+variables we trust (via the keep_environment option) and allows to
+set additional variables (via add_environment).
+
+Returns: TRUE if successful
+ FALSE otherwise
+*/
+
+BOOL
+cleanup_environment()
+{
+int old_pool = store_pool;
+store_pool = POOL_PERM; /* Need perm memory for any created env vars */
+
+if (!keep_environment || *keep_environment == '\0')
+ {
+ /* From: https://github.com/dovecot/core/blob/master/src/lib/env-util.c#L55
+ Try to clear the environment.
+ a) environ = NULL crashes on OS X.
+ b) *environ = NULL doesn't work on FreeBSD 7.0.
+ c) environ = emptyenv doesn't work on Haiku OS
+ d) environ = calloc() should work everywhere */
+
+ if (environ) *environ = NULL;
+
+ }
+else if (Ustrcmp(keep_environment, "*") != 0)
+ {
+ rmark reset_point = store_mark();
+ if (environ) for (uschar ** p = USS environ; *p; /* see below */)
+ {
+ /* It's considered broken if we do not find the '=', according to
+ Florian Weimer. For now we ignore such strings. unsetenv() would complain,
+ getenv() would complain. */
+ uschar * eqp = Ustrchr(*p, '=');
+
+ if (eqp)
+ {
+ uschar * name = string_copyn(*p, eqp - *p);
+
+ if (OK != match_isinlist(name, CUSS &keep_environment,
+ 0, NULL, NULL, MCL_NOEXPAND, FALSE, NULL))
+ if (os_unsetenv(name) < 0) return FALSE;
+ else p = USS environ; /* RESTART from the beginning */
+ else p++;
+ }
+ }
+ store_reset(reset_point);
+ }
+if (add_environment)
+ {
+ uschar * p;
+ int sep = 0;
+ const uschar * envlist = add_environment;
+
+ while ((p = string_nextinlist(&envlist, &sep, NULL, 0)))
+ {
+ DEBUG(D_expand) debug_printf("adding %s\n", p);
+ putenv(CS p);
+ }
+ }
+#ifndef DISABLE_TLS
+tls_clean_env();
+#endif
+
+store_pool = old_pool;
+return TRUE;
+}
diff --git a/src/exicyclog.src b/src/exicyclog.src
new file mode 100644
index 0000000..20bf9fc
--- /dev/null
+++ b/src/exicyclog.src
@@ -0,0 +1,350 @@
+#! /bin/sh
+
+# Copyright (c) University of Cambridge, 1995 - 2015
+# 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
+ ;;
+ --version)
+ echo "`basename $0`: $0"
+ echo "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION"
+ exit 0
+ ;;
+ *) 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 rotation=0$keep; else rotation=$keep; fi;
+
+if [ -f $mainlog.$rotation ]; then $rm $mainlog.$rotation; fi;
+if [ -f $mainlog.$rotation.$suffix ]; then $rm $mainlog.$rotation.$suffix; fi;
+
+if [ -f $rejectlog.$rotation ]; then $rm $rejectlog.$rotation; fi;
+if [ -f $rejectlog.$rotation.$suffix ]; then $rm $rejectlog.$rotation.$suffix; fi;
+
+if [ -f $paniclog.$rotation ]; then $rm $paniclog.$rotation; fi;
+if [ -f $paniclog.$rotation.$suffix ]; then $rm $paniclog.$rotation.$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
+
+# Grab our pid ro avoid race in file creation
+ourpid=$$
+
+if [ -f $mainlog ]; then
+ $mv $mainlog $mainlog.$first
+ $chown $user:$group $mainlog.$first
+ $touch $mainlog.$ourpid
+ $chown $user:$group $mainlog.$ourpid
+ $chmod 640 $mainlog.$ourpid
+ $mv $mainlog.$ourpid $mainlog
+fi
+
+if [ -f $rejectlog ]; then
+ $mv $rejectlog $rejectlog.$first
+ $chown $user:$group $rejectlog.$first
+ $touch $rejectlog.$ourpid
+ $chown $user:$group $rejectlog.$ourpid
+ $chmod 640 $rejectlog.$ourpid
+ $mv $rejectlog.$ourpid $rejectlog
+fi
+
+if [ -f $paniclog ]; then
+ $mv $paniclog $paniclog.$first
+ $chown $user:$group $paniclog.$first
+ $touch $paniclog.$ourpid
+ $chown $user:$group $paniclog.$ourpid
+ $chmod 640 $paniclog.$ourpid
+ $mv $paniclog.$ourpid $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
index 0000000..2c414fd
--- /dev/null
+++ b/src/exigrep.src
@@ -0,0 +1,380 @@
+#! PERL_COMMAND
+
+use warnings;
+use strict;
+BEGIN { pop @INC if $INC[-1] eq '.' };
+
+use Pod::Usage;
+use Getopt::Long qw(:config no_ignore_case);
+use File::Basename;
+
+# Copyright (c) 2007-2017 University of Cambridge.
+# Copyright (c) The Exim Maintainers 2020 - 2021
+# 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 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)(?:.\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);
+
+my $queue_time = -1;
+my $insensitive = 1;
+my $invert = 0;
+my $related = 0;
+my $use_pager = 1;
+my $literal = 0;
+
+
+# If using "related" option, have to track extra message IDs
+my $related_re='';
+my @Mids = ();
+
+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+)? (?:[+-]\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
+ {
+ if (defined $id_list{$id} ||
+ ($insensitive && /$pattern/io) || /$pattern/o)
+ {
+ $id_list{$id} = 1;
+ get_related_ids($id) if $related;
+ }
+ elsif ($related && $related_re)
+ {
+ grep_for_related($_, $id);
+ }
+ }
+
+ # 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+)? (?:[+-]\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' },
+ zst => { cmd => 'zstdcat', args => '' },
+};
+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 occurs 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;
+ }
+
+sub grep_for_related
+ {
+ my ($line,$id) = @_;
+ $id_list{$id} = 1 if $line =~ m/$related_re/;
+ }
+
+sub get_related_ids
+ {
+ my ($id) = @_;
+ push @Mids, $id unless grep /\b$id\b/, @Mids;
+ my $re = join '|', @Mids;
+ $related_re = qr/$re/;
+ }
+
+# 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. The -M flag will also display "related"
+# loglines (msgid from matched lines is searched in following lines).
+
+GetOptions(
+ 'I|sensitive' => sub { $insensitive = 0 },
+ 'l|literal' => \$literal,
+ 'M|related' => \$related,
+ 't|queue-time=i' => \$queue_time,
+ 'pager!' => \$use_pager,
+ 'v|invert' => \$invert,
+ 'h|help' => sub { pod2usage(-exit => 0, -verbose => 1) },
+ 'm|man' => sub {
+ pod2usage(
+ -exit => 0,
+ -verbose => 2,
+ -noperldoc => system('perldoc -V 2>/dev/null >&2')
+ );
+ },
+ 'version' => sub {
+ print basename($0) . ": $0\n",
+ "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION\n",
+ "perl(runtime): $]\n";
+ exit 0;
+ },
+) and @ARGV or pod2usage;
+
+$pattern = shift @ARGV;
+$pattern = quotemeta $pattern if $literal;
+
+# Start a pager if output goes to a terminal
+if (-t 1 and $use_pager)
+ {
+ # for perl >= v5.10.x: foreach ($ENV{PAGER}//(), 'less', 'more')
+ foreach (defined $ENV{PAGER} ? $ENV{PAGER} : (), 'less', 'more')
+ {
+ local $ENV{LESS} .= ' --no-init --quit-if-one-screen';
+ open(my $pager, '|-', $_) or next;
+ select $pager;
+ last;
+ }
+ }
+
+# 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 (-x 'ZCAT_COMMAND' && $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__
+
+=head1 NAME
+
+exigrep - search Exim's main log
+
+=head1 SYNOPSIS
+
+B<exigrep> [options] pattern [log] ...
+
+=head1 DESCRIPTION
+
+The B<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, B<exigrep> can extract complete log
+entries for a given message, or all mail for a given user, or for a
+given host, for example.
+
+If no file names are given on the command line, the standard input is read.
+
+For known file extensions indicating compression (F<.gz>, F<.bz2>, F<.xz>,
+F<.lzma>, and F<.zst>) a suitable de-compressor is used, if available.
+
+The output is sent through a pager if a terminal is connected to STDOUT. As
+pager are considered: C<$ENV{PAGER}>, C<less>, C<more>.
+
+=head1 OPTIONS
+
+=over
+
+=item B<-l>|B<--literal>
+
+This means 'literal', that is, treat all characters in the
+pattern as standing for themselves. Otherwise the pattern must be a
+Perl regular expression. The pattern match is case-insensitive.
+
+=item B<-t>|B<--queue-time> I<seconds>
+
+Limit the output to messages that spent at least I<seconds> in the
+queue.
+
+=item B<-I>|B<--sensitive>
+
+Do a case sensitive search.
+
+=item B<-v>|B<--invert>
+
+Invert the meaning of the search pattern. That is, print message log
+entries that are not related to that pattern.
+
+=item B<-M>|B<--related>
+
+Search for related messages too.
+
+=item B<--no-pager>
+
+Do not use a pager, even if STDOUT is connected to a terminal.
+
+=item B<-h>|B<--help>
+
+Print a short reference help. For more detailed help try L<exigrep(8)>,
+or C<exigrep --man>.
+
+=item B<-m>|B<--man>
+
+Print this manual page of B<exigrep>.
+
+=back
+
+=head1 SEE ALSO
+
+L<exim(8)>, L<perlre(1)>, L<Exim|http://exim.org/>
+
+=head1 AUTHOR
+
+This manual page was stitched together from spec.txt by Andreas Metzler L<ametzler at downhill.at.eu.org>
+and updated by Heiko Schlittermann L<hs@schlittermann.de>.
+
+=cut
diff --git a/src/exim.c b/src/exim.c
new file mode 100644
index 0000000..fd01d13
--- /dev/null
+++ b/src/exim.c
@@ -0,0 +1,6097 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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"
+
+#if defined(__GLIBC__) && !defined(__UCLIBC__)
+# include <gnu/libc-version.h>
+#endif
+
+#ifdef USE_GNUTLS
+# include <gnutls/gnutls.h>
+# if GNUTLS_VERSION_NUMBER < 0x030103 && !defined(DISABLE_OCSP)
+# define DISABLE_OCSP
+# endif
+#endif
+
+#ifndef _TIME_H
+# include <time.h>
+#endif
+
+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. */
+
+static void *
+function_store_malloc(PCRE2_SIZE size, void * tag)
+{
+return store_malloc((int)size);
+}
+
+static void
+function_store_free(void * block, void * tag)
+{
+/* At least some version of pcre2 pass a null pointer */
+if (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 pcre2_code *
+regex_must_compile(const uschar * pattern, BOOL caseless, BOOL use_malloc)
+{
+size_t offset;
+int options = caseless ? PCRE_COPT|PCRE2_CASELESS : PCRE_COPT;
+const pcre2_code * yield;
+int err;
+pcre2_general_context * gctx;
+pcre2_compile_context * cctx;
+
+if (use_malloc)
+ {
+ gctx = pcre2_general_context_create(function_store_malloc, function_store_free, NULL);
+ cctx = pcre2_compile_context_create(gctx);
+ }
+else
+ cctx = pcre_cmp_ctx;
+
+if (!(yield = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, options,
+ &err, &offset, cctx)))
+ {
+ uschar errbuf[128];
+ pcre2_get_error_message(err, errbuf, sizeof(errbuf));
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "regular expression error: "
+ "%s at offset %ld while compiling %s", errbuf, (long)offset, pattern);
+ }
+
+if (use_malloc)
+ {
+ pcre2_compile_context_free(cctx);
+ pcre2_general_context_free(gctx);
+ }
+return yield;
+}
+
+
+static void
+pcre_init(void)
+{
+pcre_gen_ctx = pcre2_general_context_create(function_store_malloc, function_store_free, NULL);
+pcre_cmp_ctx = pcre2_compile_context_create(pcre_gen_ctx);
+pcre_mtc_ctx = pcre2_match_context_create(pcre_gen_ctx);
+}
+
+
+
+
+/*************************************************
+* Execute regular expression and set strings *
+*************************************************/
+
+/* This function runs a regular expression match, and sets up the pointers to
+the matched substrings. The matched strings are copied so the lifetime of
+the subject is not a problem.
+
+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 if matched, or FALSE
+*/
+
+BOOL
+regex_match_and_setup(const pcre2_code * re, const uschar * subject, int options, int setup)
+{
+pcre2_match_data * md = pcre2_match_data_create_from_pattern(re, pcre_gen_ctx);
+int res = pcre2_match(re, (PCRE2_SPTR)subject, PCRE2_ZERO_TERMINATED, 0,
+ PCRE_EOPT | options, md, pcre_mtc_ctx);
+BOOL yield;
+
+if ((yield = (res >= 0)))
+ {
+ res = pcre2_get_ovector_count(md);
+ expand_nmax = setup < 0 ? 0 : setup + 1;
+ for (int matchnum = setup < 0 ? 0 : 1; matchnum < res; matchnum++)
+ {
+ PCRE2_SIZE len;
+ pcre2_substring_get_bynumber(md, matchnum,
+ (PCRE2_UCHAR **)&expand_nstring[expand_nmax], &len);
+ expand_nlength[expand_nmax++] = (int)len;
+ }
+ expand_nmax--;
+ }
+else if (res != PCRE2_ERROR_NOMATCH) DEBUG(D_any)
+ {
+ uschar errbuf[128];
+ pcre2_get_error_message(res, errbuf, sizeof(errbuf));
+ debug_printf_indent("pcre2: %s\n", errbuf);
+ }
+pcre2_match_data_free(md);
+return yield;
+}
+
+
+/* Check just for match with regex. Uses the common memory-handling.
+
+Arguments:
+ re compiled regex
+ subject string to be checked
+ slen length of subject; -1 for nul-terminated
+ rptr pointer for matched string, copied, or NULL
+
+Return: TRUE for a match.
+*/
+
+BOOL
+regex_match(const pcre2_code * re, const uschar * subject, int slen, uschar ** rptr)
+{
+pcre2_match_data * md = pcre2_match_data_create(1, pcre_gen_ctx);
+int rc = pcre2_match(re, (PCRE2_SPTR)subject,
+ slen >= 0 ? slen : PCRE2_ZERO_TERMINATED,
+ 0, PCRE_EOPT, md, pcre_mtc_ctx);
+PCRE2_SIZE * ovec = pcre2_get_ovector_pointer(md);
+if (rc < 0)
+ return FALSE;
+if (rptr)
+ *rptr = string_copyn(subject + ovec[0], ovec[1] - ovec[0]);
+return TRUE;
+}
+
+
+
+/*************************************************
+* 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, ...)
+{
+gstring gs = { .size = PROCESS_INFO_SIZE - 2, .ptr = 0, .s = process_info };
+gstring * g;
+int len;
+va_list ap;
+
+g = string_fmt_append(&gs, "%5d ", (int)getpid());
+len = g->ptr;
+va_start(ap, format);
+if (!string_vformat(g, 0, format, ap))
+ {
+ gs.ptr = len;
+ g = string_cat(&gs, US"**** string overflowed buffer ****");
+ }
+g = string_catn(g, US"\n", 1);
+string_from_gstring(g);
+process_info_len = g->ptr;
+DEBUG(D_process_info) debug_printf("set_process_info: %s", process_info);
+va_end(ap);
+}
+
+/***********************************************
+* Handler for SIGTERM *
+***********************************************/
+
+static void
+term_handler(int sig)
+{
+exit(1);
+}
+
+
+/***********************************************
+* Handler for SIGSEGV *
+***********************************************/
+
+static void
+#ifdef SA_SIGINFO
+segv_handler(int sig, siginfo_t * info, void * uctx)
+{
+log_write(0, LOG_MAIN|LOG_PANIC, "SIGSEGV (fault address: %p)", info->si_addr);
+# if defined(SEGV_MAPERR) && defined(SEGV_ACCERR) && defined(SEGV_BNDERR) && defined(SEGV_PKUERR)
+switch (info->si_code)
+ {
+ case SEGV_MAPERR: log_write(0, LOG_MAIN|LOG_PANIC, "SEGV_MAPERR"); break;
+ case SEGV_ACCERR: log_write(0, LOG_MAIN|LOG_PANIC, "SEGV_ACCERR"); break;
+ case SEGV_BNDERR: log_write(0, LOG_MAIN|LOG_PANIC, "SEGV_BNDERR"); break;
+ case SEGV_PKUERR: log_write(0, LOG_MAIN|LOG_PANIC, "SEGV_PKUERR"); break;
+ }
+# endif
+if (US info->si_addr < US 4096)
+ log_write(0, LOG_MAIN|LOG_PANIC, "SIGSEGV (null pointer indirection)");
+else
+ log_write(0, LOG_MAIN|LOG_PANIC, "SIGSEGV (maybe attempt to write to immutable memory)");
+if (process_info_len > 0)
+ log_write(0, LOG_MAIN|LOG_PANIC, "SIGSEGV (%.*s)", process_info_len, process_info);
+signal(SIGSEGV, SIG_DFL);
+kill(getpid(), sig);
+}
+
+#else
+segv_handler(int sig)
+{
+log_write(0, LOG_MAIN|LOG_PANIC, "SIGSEGV (maybe attempt to write to immutable memory)");
+if (process_info_len > 0)
+ log_write(0, LOG_MAIN|LOG_PANIC, "SIGSEGV (%.*s)", process_info_len, process_info);
+signal(SIGSEGV, SIG_DFL);
+kill(getpid(), sig);
+}
+#endif
+
+
+/*************************************************
+* 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);
+
+if (!process_log_path) return;
+fd = log_open_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;
+
+(void)write(fd, process_info, process_info_len);
+(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)
+{
+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. :-)
+
+We assume it to be not worth sleeping for under 50us; this value will
+require revisiting as hardware advances. This avoids the issue of
+a zero-valued timer setting meaning "never fire".
+
+Argument: an itimerval structure containing the interval
+Returns: nothing
+*/
+
+static void
+milliwait(struct itimerval *itval)
+{
+sigset_t sigmask;
+sigset_t old_sigmask;
+int save_errno = errno;
+
+if (itval->it_value.tv_usec < 50 && itval->it_value.tv_sec == 0)
+ return;
+(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 */
+errno = save_errno;
+sigalrm_seen = FALSE;
+}
+
+
+
+
+/*************************************************
+* 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 = {.it_interval = {.tv_sec = 0, .tv_usec = 0},
+ .it_value = {.tv_sec = msec/1000,
+ .tv_usec = (msec % 1000) * 1000}};
+milliwait(&itval);
+}
+
+
+
+/*************************************************
+* Compare microsecond times *
+*************************************************/
+
+/*
+Arguments:
+ tv1 the first time
+ tv2 the second time
+
+Returns: -1, 0, or +1
+*/
+
+static 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 *
+*************************************************/
+
+#ifdef _POSIX_MONOTONIC_CLOCK
+# ifdef CLOCK_BOOTTIME
+# define EXIM_CLOCKTYPE CLOCK_BOOTTIME
+# else
+# define EXIM_CLOCKTYPE CLOCK_MONOTONIC
+# endif
+
+/* Amount EXIM_CLOCK is behind realtime, at startup. */
+static struct timespec offset_ts;
+
+static void
+exim_clock_init(void)
+{
+struct timeval tv;
+if (clock_gettime(EXIM_CLOCKTYPE, &offset_ts) != 0) return;
+(void)gettimeofday(&tv, NULL);
+offset_ts.tv_sec = tv.tv_sec - offset_ts.tv_sec;
+offset_ts.tv_nsec = tv.tv_usec * 1000 - offset_ts.tv_nsec;
+if (offset_ts.tv_nsec >= 0) return;
+offset_ts.tv_sec--;
+offset_ts.tv_nsec += 1000*1000*1000;
+}
+#endif
+
+
+void
+exim_gettime(struct timeval * tv)
+{
+#ifdef _POSIX_MONOTONIC_CLOCK
+struct timespec now_ts;
+
+if (clock_gettime(EXIM_CLOCKTYPE, &now_ts) == 0)
+ {
+ now_ts.tv_sec += offset_ts.tv_sec;
+ if ((now_ts.tv_nsec += offset_ts.tv_nsec) >= 1000*1000*1000)
+ {
+ now_ts.tv_sec++;
+ now_ts.tv_nsec -= 1000*1000*1000;
+ }
+ tv->tv_sec = now_ts.tv_sec;
+ tv->tv_usec = now_ts.tv_nsec / 1000;
+ }
+else
+#endif
+ (void)gettimeofday(tv, NULL);
+}
+
+
+/* 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 certainty, 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:
+ prev_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.
+ On return, updated to current (rounded down).
+ 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 * prev_tv, int resolution)
+{
+struct timeval now_tv;
+long int now_true_usec;
+
+exim_gettime(&now_tv);
+now_true_usec = now_tv.tv_usec;
+now_tv.tv_usec = (now_true_usec/resolution) * resolution;
+
+while (exim_tvcmp(&now_tv, prev_tv) <= 0)
+ {
+ struct itimerval itval;
+ itval.it_interval.tv_sec = 0;
+ itval.it_interval.tv_usec = 0;
+ itval.it_value.tv_sec = prev_tv->tv_sec - now_tv.tv_sec;
+ itval.it_value.tv_usec = prev_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 "tgt". 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 (!f.running_in_test_harness)
+ {
+ debug_printf("tick check: " TIME_T_FMT ".%06lu " TIME_T_FMT ".%06lu\n",
+ prev_tv->tv_sec, (long) prev_tv->tv_usec,
+ now_tv.tv_sec, (long) now_tv.tv_usec);
+ debug_printf("waiting " TIME_T_FMT ".%06lu sec\n",
+ itval.it_value.tv_sec, (long) itval.it_value.tv_usec);
+ }
+ }
+
+ milliwait(&itval);
+
+ /* Be prapared to go around if the kernel does not implement subtick
+ granularity (GNU Hurd) */
+
+ exim_gettime(&now_tv);
+ now_true_usec = now_tv.tv_usec;
+ now_tv.tv_usec = (now_true_usec/resolution) * resolution;
+ }
+*prev_tv = now_tv;
+}
+
+
+
+
+/*************************************************
+* 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 devnull = -1;
+struct stat statbuf;
+for (int 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("/dev/null", 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)
+ {
+#ifndef DISABLE_TLS
+ tls_close(NULL, TLS_NO_SHUTDOWN); /* 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 (!f.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, const 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)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "cannot run initgroups(): "
+ "no passwd entry for uid=%ld", (long int)uid);
+
+ 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));
+ }
+
+ 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[EXIM_GROUPLIST_SIZE];
+ 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(nelem(group_list), group_list);
+ save_errno = errno;
+ debug_printf(" auxiliary group list:");
+ if (group_count > 0)
+ for (int 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();
+store_exit();
+DEBUG(D_any)
+ debug_printf(">>>>>>>>>>>>>>>> Exim pid=%d (%s) terminating with rc=%d "
+ ">>>>>>>>>>>>>>>>\n",
+ (int)getpid(), process_purpose, rc);
+exit(rc);
+}
+
+
+void
+exim_underbar_exit(int rc)
+{
+store_exit();
+DEBUG(D_any)
+ debug_printf(">>>>>>>>>>>>>>>> Exim pid=%d (%s) terminating with rc=%d "
+ ">>>>>>>>>>>>>>>>\n",
+ (int)getpid(), process_purpose, rc);
+_exit(rc);
+}
+
+
+
+/* Print error string, then die */
+static void
+exim_fail(const char * fmt, ...)
+{
+va_list ap;
+va_start(ap, fmt);
+vfprintf(stderr, fmt, ap);
+exit(EXIT_FAILURE);
+}
+
+/* fail if a length is too long */
+static inline void
+exim_len_fail_toolong(int itemlen, int maxlen, const char *description)
+{
+if (itemlen <= maxlen)
+ return;
+fprintf(stderr, "exim: length limit exceeded (%d > %d) for: %s\n",
+ itemlen, maxlen, description);
+exit(EXIT_FAILURE);
+}
+
+/* only pass through the string item back to the caller if it's short enough */
+static inline const uschar *
+exim_str_fail_toolong(const uschar *item, int maxlen, const char *description)
+{
+exim_len_fail_toolong(Ustrlen(item), maxlen, description);
+return item;
+}
+
+/* exim_chown_failure() called from exim_chown()/exim_fchown() on failure
+of chown()/fchown(). See src/functions.h for more explanation */
+int
+exim_chown_failure(int fd, const uschar *name, uid_t owner, gid_t group)
+{
+int saved_errno = errno; /* from the preceeding chown call */
+#if 1
+log_write(0, LOG_MAIN|LOG_PANIC,
+ __FILE__ ":%d: chown(%s, %d:%d) failed (%s)."
+ " Please contact the authors and refer to https://bugs.exim.org/show_bug.cgi?id=2391",
+ __LINE__, name?name:US"<unknown>", owner, group, strerror(errno));
+#else
+/* I leave this here, commented, in case the "bug"(?) comes up again.
+ It is not an Exim bug, but we can provide a workaround.
+ See Bug 2391
+ HS 2019-04-18 */
+
+struct stat buf;
+
+if (0 == (fd < 0 ? stat(name, &buf) : fstat(fd, &buf)))
+{
+ if (buf.st_uid == owner && buf.st_gid == group) return 0;
+ log_write(0, LOG_MAIN|LOG_PANIC, "Wrong ownership on %s", name);
+}
+else log_write(0, LOG_MAIN|LOG_PANIC, "Stat failed on %s: %s", name, strerror(errno));
+
+#endif
+errno = saved_errno;
+return -1;
+}
+
+
+/*************************************************
+* 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)
+ exim_fail("exim abandoned: \"%s\" is not an IP address\n", address);
+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)
+ {
+ 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 *
+*************************************************/
+
+static void
+show_string(BOOL is_stdout, gstring * g)
+{
+const uschar * s = string_from_gstring(g);
+if (s)
+ if (is_stdout) fputs(CCS s, stdout);
+ else debug_printf("%s", s);
+}
+
+
+static gstring *
+show_db_version(gstring * g)
+{
+#ifdef DB_VERSION_STRING
+DEBUG(D_any)
+ {
+ g = string_fmt_append(g, "Library version: BDB: Compile: %s\n", DB_VERSION_STRING);
+ g = string_fmt_append(g, " Runtime: %s\n",
+ db_version(NULL, NULL, NULL));
+ }
+else
+ g = string_fmt_append(g, "Berkeley DB: %s\n", DB_VERSION_STRING);
+
+#elif defined(BTREEVERSION) && defined(HASHVERSION)
+# ifdef USE_DB
+ g = string_cat(g, US"Probably Berkeley DB version 1.8x (native mode)\n");
+# else
+ g = string_cat(g, US"Probably Berkeley DB version 1.8x (compatibility mode)\n");
+# endif
+
+#elif defined(_DBM_RDONLY) || defined(dbm_dirfno)
+g = string_cat(g, US"Probably ndbm\n");
+#elif defined(USE_TDB)
+g = string_cat(g, US"Using tdb\n");
+#else
+# ifdef USE_GDBM
+ g = string_cat(g, US"Probably GDBM (native mode)\n");
+# else
+ g = string_cat(g, US"Probably GDBM (compatibility mode)\n");
+# endif
+#endif
+return g;
+}
+
+
+/* This function is called for -bV/--version and for -d to output the optional
+features of the current Exim binary.
+
+Arguments: BOOL, true for stdout else debug channel
+Returns: nothing
+*/
+
+static void
+show_whats_supported(BOOL is_stdout)
+{
+rmark reset_point = store_mark();
+gstring * g = NULL;
+
+DEBUG(D_any) {} else g = show_db_version(g);
+
+g = string_cat(g, US"Support for:");
+#ifdef SUPPORT_CRYPTEQ
+ g = string_cat(g, US" crypteq");
+#endif
+#if HAVE_ICONV
+ g = string_cat(g, US" iconv()");
+#endif
+#if HAVE_IPV6
+ g = string_cat(g, US" IPv6");
+#endif
+#ifdef HAVE_SETCLASSRESOURCES
+ g = string_cat(g, US" use_setclassresources");
+#endif
+#ifdef SUPPORT_PAM
+ g = string_cat(g, US" PAM");
+#endif
+#ifdef EXIM_PERL
+ g = string_cat(g, US" Perl");
+#endif
+#ifdef EXPAND_DLFUNC
+ g = string_cat(g, US" Expand_dlfunc");
+#endif
+#ifdef USE_TCP_WRAPPERS
+ g = string_cat(g, US" TCPwrappers");
+#endif
+#ifdef USE_GNUTLS
+ g = string_cat(g, US" GnuTLS");
+#endif
+#ifdef USE_OPENSSL
+ g = string_cat(g, US" OpenSSL");
+#endif
+#ifndef DISABLE_TLS_RESUME
+ g = string_cat(g, US" TLS_resume");
+#endif
+#ifdef SUPPORT_TRANSLATE_IP_ADDRESS
+ g = string_cat(g, US" translate_ip_address");
+#endif
+#ifdef SUPPORT_MOVE_FROZEN_MESSAGES
+ g = string_cat(g, US" move_frozen_messages");
+#endif
+#ifdef WITH_CONTENT_SCAN
+ g = string_cat(g, US" Content_Scanning");
+#endif
+#ifdef SUPPORT_DANE
+ g = string_cat(g, US" DANE");
+#endif
+#ifndef DISABLE_DKIM
+ g = string_cat(g, US" DKIM");
+#endif
+#ifdef SUPPORT_DMARC
+ g = string_cat(g, US" DMARC");
+#endif
+#ifndef DISABLE_DNSSEC
+ g = string_cat(g, US" DNSSEC");
+#endif
+#ifndef DISABLE_EVENT
+ g = string_cat(g, US" Event");
+#endif
+#ifdef SUPPORT_I18N
+ g = string_cat(g, US" I18N");
+#endif
+#ifndef DISABLE_OCSP
+ g = string_cat(g, US" OCSP");
+#endif
+#ifndef DISABLE_PIPE_CONNECT
+ g = string_cat(g, US" PIPECONNECT");
+#endif
+#ifndef DISABLE_PRDR
+ g = string_cat(g, US" PRDR");
+#endif
+#ifdef SUPPORT_PROXY
+ g = string_cat(g, US" PROXY");
+#endif
+#ifndef DISABLE_QUEUE_RAMP
+ g = string_cat(g, US" Queue_Ramp");
+#endif
+#ifdef SUPPORT_SOCKS
+ g = string_cat(g, US" SOCKS");
+#endif
+#ifdef SUPPORT_SPF
+ g = string_cat(g, US" SPF");
+#endif
+#if defined(SUPPORT_SRS)
+ g = string_cat(g, US" SRS");
+#endif
+#ifdef TCP_FASTOPEN
+ tcp_init();
+ if (f.tcp_fastopen_ok) g = string_cat(g, US" TCP_Fast_Open");
+#endif
+#ifdef EXPERIMENTAL_ARC
+ g = string_cat(g, US" Experimental_ARC");
+#endif
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+ g = string_cat(g, US" Experimental_Brightmail");
+#endif
+#ifdef EXPERIMENTAL_DCC
+ g = string_cat(g, US" Experimental_DCC");
+#endif
+#ifdef EXPERIMENTAL_DSN_INFO
+ g = string_cat(g, US" Experimental_DSN_info");
+#endif
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ g = string_cat(g, US" Experimental_ESMTP_Limits");
+#endif
+#ifdef EXPERIMENTAL_QUEUEFILE
+ g = string_cat(g, US" Experimental_QUEUEFILE");
+#endif
+g = string_cat(g, US"\n");
+
+g = string_cat(g, US"Lookups (built-in):");
+#if defined(LOOKUP_LSEARCH) && LOOKUP_LSEARCH!=2
+ g = string_cat(g, US" lsearch wildlsearch nwildlsearch iplsearch");
+#endif
+#if defined(LOOKUP_CDB) && LOOKUP_CDB!=2
+ g = string_cat(g, US" cdb");
+#endif
+#if defined(LOOKUP_DBM) && LOOKUP_DBM!=2
+ g = string_cat(g, US" dbm dbmjz dbmnz");
+#endif
+#if defined(LOOKUP_DNSDB) && LOOKUP_DNSDB!=2
+ g = string_cat(g, US" dnsdb");
+#endif
+#if defined(LOOKUP_DSEARCH) && LOOKUP_DSEARCH!=2
+ g = string_cat(g, US" dsearch");
+#endif
+#if defined(LOOKUP_IBASE) && LOOKUP_IBASE!=2
+ g = string_cat(g, US" ibase");
+#endif
+#if defined(LOOKUP_JSON) && LOOKUP_JSON!=2
+ g = string_cat(g, US" json");
+#endif
+#if defined(LOOKUP_LDAP) && LOOKUP_LDAP!=2
+ g = string_cat(g, US" ldap ldapdn ldapm");
+#endif
+#ifdef LOOKUP_LMDB
+ g = string_cat(g, US" lmdb");
+#endif
+#if defined(LOOKUP_MYSQL) && LOOKUP_MYSQL!=2
+ g = string_cat(g, US" mysql");
+#endif
+#if defined(LOOKUP_NIS) && LOOKUP_NIS!=2
+ g = string_cat(g, US" nis nis0");
+#endif
+#if defined(LOOKUP_NISPLUS) && LOOKUP_NISPLUS!=2
+ g = string_cat(g, US" nisplus");
+#endif
+#if defined(LOOKUP_ORACLE) && LOOKUP_ORACLE!=2
+ g = string_cat(g, US" oracle");
+#endif
+#if defined(LOOKUP_PASSWD) && LOOKUP_PASSWD!=2
+ g = string_cat(g, US" passwd");
+#endif
+#if defined(LOOKUP_PGSQL) && LOOKUP_PGSQL!=2
+ g = string_cat(g, US" pgsql");
+#endif
+#if defined(LOOKUP_REDIS) && LOOKUP_REDIS!=2
+ g = string_cat(g, US" redis");
+#endif
+#if defined(LOOKUP_SQLITE) && LOOKUP_SQLITE!=2
+ g = string_cat(g, US" sqlite");
+#endif
+#if defined(LOOKUP_TESTDB) && LOOKUP_TESTDB!=2
+ g = string_cat(g, US" testdb");
+#endif
+#if defined(LOOKUP_WHOSON) && LOOKUP_WHOSON!=2
+ g = string_cat(g, US" whoson");
+#endif
+g = string_cat(g, US"\n");
+
+g = auth_show_supported(g);
+g = route_show_supported(g);
+g = transport_show_supported(g);
+
+#ifdef WITH_CONTENT_SCAN
+g = malware_show_supported(g);
+#endif
+show_string(is_stdout, g); g = NULL;
+
+if (fixed_never_users[0] > 0)
+ {
+ int i;
+ g = string_cat(g, US"Fixed never_users: ");
+ for (i = 1; i <= (int)fixed_never_users[0] - 1; i++)
+ string_fmt_append(g, "%u:", (unsigned)fixed_never_users[i]);
+ g = string_fmt_append(g, "%u\n", (unsigned)fixed_never_users[i]);
+ }
+
+g = string_fmt_append(g, "Configure owner: %d:%d\n", config_uid, config_gid);
+
+g = string_fmt_append(g, "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)
+ {
+
+/* clang defines __GNUC__ (at least, for me) so test for it first */
+#if defined(__clang__)
+ g = string_fmt_append(g, "Compiler: CLang [%s]\n", __clang_version__);
+#elif defined(__GNUC__)
+ g = string_fmt_append(g, "Compiler: GCC [%s]\n",
+# ifdef __VERSION__
+ __VERSION__
+# else
+ "? unknown version ?"
+# endif
+ );
+#else
+ g = string_cat(g, US"Compiler: <unknown>\n");
+#endif
+
+#if defined(__GLIBC__) && !defined(__UCLIBC__)
+ g = string_fmt_append(g, "Library version: Glibc: Compile: %d.%d\n",
+ __GLIBC__, __GLIBC_MINOR__);
+ if (__GLIBC_PREREQ(2, 1))
+ g = string_fmt_append(g, " Runtime: %s\n",
+ gnu_get_libc_version());
+#endif
+
+g = show_db_version(g);
+
+#ifndef DISABLE_TLS
+ g = tls_version_report(g);
+#endif
+#ifdef SUPPORT_I18N
+ g = utf8_version_report(g);
+#endif
+#ifdef SUPPORT_DMARC
+ g = dmarc_version_report(g);
+#endif
+#ifdef SUPPORT_SPF
+ g = spf_lib_version_report(g);
+#endif
+
+show_string(is_stdout, g);
+g = NULL;
+
+for (auth_info * authi = auths_available; *authi->driver_name != '\0'; ++authi)
+ if (authi->version_report)
+ g = (*authi->version_report)(g);
+
+ /* 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)
+ {
+ uschar buf[24];
+ pcre2_config(PCRE2_CONFIG_VERSION, buf);
+ g = string_fmt_append(g, "Library version: PCRE2: Compile: %d.%d%s\n"
+ " Runtime: %s\n",
+ PCRE2_MAJOR, PCRE2_MINOR,
+ EXPAND_AND_QUOTE(PCRE2_PRERELEASE) "",
+ buf);
+ }
+#undef QUOTE
+#undef EXPAND_AND_QUOTE
+
+show_string(is_stdout, g);
+g = NULL;
+
+init_lookup_list();
+for (int i = 0; i < lookup_list_count; i++)
+ if (lookup_list[i]->version_report)
+ g = lookup_list[i]->version_report(g);
+show_string(is_stdout, g);
+g = NULL;
+
+#ifdef WHITELIST_D_MACROS
+ g = string_fmt_append(g, "WHITELIST_D_MACROS: \"%s\"\n", WHITELIST_D_MACROS);
+#else
+ g = string_cat(g, US"WHITELIST_D_MACROS unset\n");
+#endif
+#ifdef TRUSTED_CONFIG_LIST
+ g = string_fmt_append(g, "TRUSTED_CONFIG_LIST: \"%s\"\n", TRUSTED_CONFIG_LIST);
+#else
+ g = string_cat(g, US"TRUSTED_CONFIG_LIST unset\n");
+#endif
+ }
+
+show_string(is_stdout, g);
+store_reset(reset_point);
+}
+
+
+/*************************************************
+* Show auxiliary information about Exim *
+*************************************************/
+
+static void
+show_exim_information(enum commandline_info request, FILE *stream)
+{
+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 list of known dscp value keywords\n"
+" exim -bI:sieve list of supported sieve extensions\n"
+);
+ return;
+ case CMDINFO_SIEVE:
+ for (const uschar ** 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;
+gstring * g;
+
+for (uschar * 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;
+
+g = string_catn(NULL, US"\"", 1);
+
+for (;;)
+ {
+ uschar *nq = US Ustrpbrk(lpart, "\\\"");
+ if (nq == NULL)
+ {
+ g = string_cat(g, lpart);
+ break;
+ }
+ g = string_catn(g, lpart, nq - lpart);
+ g = string_catn(g, US"\\", 1);
+ g = string_catn(g, nq, 1);
+ lpart = nq + 1;
+ }
+
+g = string_catn(g, US"\"", 1);
+return string_from_gstring(g);
+}
+
+
+
+#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) dlclose(dlhandle_curses);
+
+if (dlhandle)
+ {
+ /* 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 *))
+{
+gstring * g = NULL;
+BOOL had_input = FALSE;
+
+if (!fn_readline) { printf("> "); fflush(stdout); }
+
+for (int i = 0;; i++)
+ {
+ uschar buffer[1024];
+ uschar * p, * ss;
+
+#ifdef USE_READLINE
+ char *readline_line = NULL;
+ if (fn_readline)
+ {
+ if (!(readline_line = fn_readline((i > 0)? "":"> "))) break;
+ if (*readline_line && fn_addhist) fn_addhist(readline_line);
+ p = US readline_line;
+ }
+ else
+#endif
+
+ /* readline() not in use */
+
+ {
+ if (Ufgets(buffer, sizeof(buffer), stdin) == NULL) break; /*EOF*/
+ p = buffer;
+ }
+
+ /* Handle the line */
+
+ had_input = TRUE;
+ ss = p + Ustrlen(p);
+ while (ss > p && isspace(ss[-1])) ss--; /* strip trailing newline (and spaces) */
+
+ if (i > 0)
+ while (p < ss && isspace(*p)) p++; /* strip leading space after cont */
+
+ g = string_catn(g, p, ss - p);
+
+#ifdef USE_READLINE
+ if (fn_readline) free(readline_line);
+#endif
+
+ /* g can only be NULL if ss==p */
+ if (ss == p || g->s[g->ptr-1] != '\\') /* not continuation; done */
+ break;
+
+ --g->ptr; /* drop the \ */
+ }
+
+if (had_input) return g ? string_from_gstring(g) : US"";
+printf("\n");
+return NULL;
+}
+
+
+
+/*************************************************
+* 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 variants */
+if (Ustrcmp(progname, US"-mailq") == 0)
+ exim_fail(
+ "mailq - list the contents of the mail queue\n\n"
+ "For a list of options, see the Exim documentation.\n");
+
+/* Generic usage - we output this whatever happens */
+exim_fail(
+ "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");
+}
+
+
+
+/*************************************************
+* 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: opt_D_used - true if the commandline had a "-D" option
+Returns: true if trusted, false otherwise
+*/
+
+static BOOL
+macros_trusted(BOOL opt_D_used)
+{
+#ifdef WHITELIST_D_MACROS
+uschar *whitelisted, *end, *p, **whites;
+int white_count, i, n;
+size_t len;
+BOOL prev_char_item, found;
+#endif
+
+if (!opt_D_used)
+ 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_perm(US WHITELIST_D_MACROS, FALSE);
+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 commandline macros should be very short.
+Accept the N*M complexity. */
+for (macro_item * m = macros_user; m; m = m->next) if (m->command_line)
+ {
+ found = FALSE;
+ for (uschar ** w = whites; *w; ++w)
+ if (Ustrcmp(*w, m->name) == 0)
+ {
+ found = TRUE;
+ break;
+ }
+ if (!found)
+ return FALSE;
+ if (!m->replacement)
+ continue;
+ if ((len = m->replen) == 0)
+ continue;
+ if (!regex_match(regex_whitelisted_macro, m->replacement, len, NULL))
+ return FALSE;
+ }
+DEBUG(D_any) debug_printf("macros_trusted overridden to true by whitelisting\n");
+return TRUE;
+#endif
+}
+
+
+/*************************************************
+* Expansion testing *
+*************************************************/
+
+/* Expand and print one item, doing macro-processing.
+
+Arguments:
+ item line for expansion
+*/
+
+static void
+expansion_test_line(const uschar * line)
+{
+int len;
+BOOL dummy_macexp;
+uschar * s;
+
+Ustrncpy(big_buffer, line, big_buffer_size);
+big_buffer[big_buffer_size-1] = '\0';
+len = Ustrlen(big_buffer);
+
+(void) macros_expand(0, &len, &dummy_macexp);
+
+if (isupper(big_buffer[0]))
+ {
+ if (macro_read_assignment(big_buffer))
+ printf("Defined macro '%s'\n", mlast->name);
+ }
+else
+ if ((s = expand_string(big_buffer))) printf("%s\n", CS s);
+ else printf("Failed: %s\n", expand_string_message);
+}
+
+
+
+/*************************************************
+* 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] ? Ustrlen(argv[0]) : 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;
+gid_t original_egid;
+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 list_config = FALSE;
+BOOL local_queue_only;
+BOOL one_msg_action = FALSE;
+BOOL opt_D_used = 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 rcpt_verify_quota = 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;
+const uschar *ftest_domain = NULL;
+const uschar *ftest_localpart = NULL;
+const uschar *ftest_prefix = NULL;
+const uschar *ftest_suffix = NULL;
+uschar *log_oneline = NULL;
+uschar *malware_test_file = NULL;
+uschar *real_sender_address;
+uschar *originator_home = US"/";
+size_t sz;
+
+struct passwd *pw;
+struct stat statbuf;
+pid_t passed_qr_pid = (pid_t)0;
+int passed_qr_pipe = -1;
+gid_t group_list[EXIM_GROUPLIST_SIZE];
+
+/* 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;
+
+#ifdef MEASURE_TIMING
+(void)gettimeofday(&timestamp_startup, NULL);
+#endif
+
+store_init(); /* Initialise the memory allocation susbsystem */
+pcre_init(); /* Set up memory handling for pcre */
+
+/* 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)
+ exim_fail("exim: refusing to run with uid 0 for \"%s\"\n", EXIM_USERNAME);
+
+ /* 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
+ exim_fail(
+ "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");
+#endif
+ }
+else
+ exim_fail("exim: failed to find uid for user name \"%s\"\n", EXIM_USERNAME);
+#endif
+
+#ifdef EXIM_GROUPNAME
+if (!route_findgroup(US EXIM_GROUPNAME, &exim_gid))
+ exim_fail("exim: failed to find gid for group name \"%s\"\n", EXIM_GROUPNAME);
+#endif
+
+#ifdef CONFIGURE_OWNERNAME
+if (!route_finduser(US CONFIGURE_OWNERNAME, NULL, &config_uid))
+ exim_fail("exim: failed to find uid for user name \"%s\"\n",
+ CONFIGURE_OWNERNAME);
+#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))
+ exim_fail("exim: failed to find gid for group name \"%s\"\n",
+ CONFIGURE_GROUPNAME);
+#endif
+
+/* In the Cygwin environment, some initialization used to need doing.
+It was fudged in by means of this macro; now no longer but we'll leave
+it in case of others. */
+
+#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. */
+
+f.running_in_test_harness =
+ *running_status == '<' && Ustrcmp(running_status, "<<<testing>>>") == 0;
+if (f.running_in_test_harness)
+ debug_store = TRUE;
+
+/* Protect against abusive argv[0] */
+if (!argv[0] || !argc) exim_fail("exim: executable name required\n");
+exim_str_fail_toolong(argv[0], PATH_MAX, "argv[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");
+
+/* Get the offset between CLOCK_MONOTONIC/CLOCK_BOOTTIME and wallclock */
+
+#ifdef _POSIX_MONOTONIC_CLOCK
+exim_clock_init();
+#endif
+
+/* 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. */
+
+if (!(log_buffer = US malloc(LOG_BUFFER_SIZE)))
+ exim_fail("exim: failed to get store for log buffer\n");
+
+/* Initialize the default log options. */
+
+bits_set(log_selector, log_selector_size, log_default);
+
+/* 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;
+
+/* 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. */
+
+process_info = store_get(PROCESS_INFO_SIZE, GET_TAINTED);
+set_process_info("initializing");
+os_restarting_signal(SIGUSR1, usr1_handler); /* exiwhat */
+#ifdef SA_SIGINFO
+ {
+ struct sigaction act = { .sa_sigaction = segv_handler, .sa_flags = SA_RESETHAND | SA_SIGINFO };
+ sigaction(SIGSEGV, &act, NULL);
+ }
+#else
+signal(SIGSEGV, segv_handler); /* log faults */
+#endif
+
+/* If running in a dockerized environment, the TERM signal is only
+delegated to the PID 1 if we request it by setting an signal handler */
+if (getpid() == 1) signal(SIGTERM, term_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
+
+for (i = 0; i < REGEX_VARS; i++) regex_vars[i] = NULL;
+
+/* 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))
+ {
+ f.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();
+original_egid = getegid();
+
+/* 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)
+ {
+ if ((rv = setgid(real_gid)))
+ exim_fail("exim: setgid(%ld) failed: %s\n",
+ (long int)real_gid, strerror(errno));
+ if ((rv = setuid(real_uid)))
+ exim_fail("exim: setuid(%ld) failed: %s\n",
+ (long int)real_uid, strerror(errno));
+ }
+
+/* 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);
+
+/* For most of the args-parsing we need to use permanent pool memory */
+ {
+ int old_pool = store_pool;
+ store_pool = POOL_PERM;
+
+/* 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 consisting 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;
+ f.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) { 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;
+
+ /* -Btype is a sendmail option for 7bit/8bit setting. Exim is 8-bit clean
+ so has no need of it. */
+
+ case 'B':
+ if (!*argrest) i++; /* Skip over the type */
+ break;
+
+
+ case 'b':
+ {
+ receiving_message = FALSE; /* Reset TRUE for -bm, -bS, -bs below */
+
+ switch (*argrest++)
+ {
+ /* -bd: Run in daemon mode, awaiting SMTP connections.
+ -bdf: Ditto, but in the foreground.
+ */
+ case 'd':
+ f.daemon_listen = TRUE;
+ if (*argrest == 'f') f.background_daemon = FALSE;
+ else if (*argrest) badarg = TRUE;
+ break;
+
+ /* -be: Run in expansion test mode
+ -bem: Ditto, but read a message from a file first
+ */
+ case 'e':
+ expansion_test = checking = TRUE;
+ if (*argrest == 'm')
+ {
+ if (++i >= argc) { badarg = TRUE; break; }
+ expansion_test_message = argv[i];
+ argrest++;
+ }
+ if (*argrest) badarg = TRUE;
+ break;
+
+ /* -bF: Run system filter test */
+ case 'F':
+ filter_test |= checking = FTEST_SYSTEM;
+ if (*argrest) badarg = TRUE;
+ else if (++i < argc) filter_test_sfile = argv[i];
+ else exim_fail("exim: file name expected after %s\n", argv[i-1]);
+ break;
+
+ /* -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
+ */
+ case 'f':
+ if (!*argrest)
+ {
+ filter_test |= checking = FTEST_USER;
+ if (++i < argc) filter_test_ufile = argv[i];
+ else exim_fail("exim: file name expected after %s\n", argv[i-1]);
+ }
+ else
+ {
+ if (++i >= argc)
+ exim_fail("exim: string expected after %s\n", arg);
+ if (Ustrcmp(argrest, "d") == 0) ftest_domain = exim_str_fail_toolong(argv[i], EXIM_DOMAINNAME_MAX, "-bfd");
+ else if (Ustrcmp(argrest, "l") == 0) ftest_localpart = exim_str_fail_toolong(argv[i], EXIM_LOCALPART_MAX, "-bfl");
+ else if (Ustrcmp(argrest, "p") == 0) ftest_prefix = exim_str_fail_toolong(argv[i], EXIM_LOCALPART_MAX, "-bfp");
+ else if (Ustrcmp(argrest, "s") == 0) ftest_suffix = exim_str_fail_toolong(argv[i], EXIM_LOCALPART_MAX, "-bfs");
+ else badarg = TRUE;
+ }
+ break;
+
+ /* -bh: Host checking - an IP address must follow. */
+ case 'h':
+ if (!*argrest || Ustrcmp(argrest, "c") == 0)
+ {
+ if (++i >= argc) { badarg = TRUE; break; }
+ sender_host_address = string_copy_taint(
+ exim_str_fail_toolong(argv[i], EXIM_IPADDR_MAX, "-bh"),
+ GET_TAINTED);
+ host_checking = checking = f.log_testing_mode = TRUE;
+ f.host_checking_callout = *argrest == 'c';
+ message_logs = FALSE;
+ }
+ else badarg = TRUE;
+ break;
+
+ /* -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. */
+ case 'i':
+ if (!*argrest) bi_option = TRUE;
+ else badarg = TRUE;
+ break;
+
+ /* -bI: provide information, of the type to follow after a colon.
+ This is an Exim flag. */
+ case 'I':
+ if (Ustrlen(argrest) >= 1 && *argrest == ':')
+ {
+ uschar *p = argrest+1;
+ 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;
+ }
+ else badarg = TRUE;
+ break;
+
+ /* -bm: Accept and deliver message - the default option. Reinstate
+ receiving_message, which got turned off for all -b options.
+ -bmalware: test the filename given for malware */
+ case 'm':
+ if (!*argrest) receiving_message = TRUE;
+ else if (Ustrcmp(argrest, "alware") == 0)
+ {
+ if (++i >= argc) { badarg = TRUE; break; }
+ checking = TRUE;
+ malware_test_file = argv[i];
+ }
+ else badarg = TRUE;
+ break;
+
+ /* -bnq: For locally originating messages, do not qualify unqualified
+ addresses. In the envelope, this causes errors; in header lines they
+ just get left. */
+ case 'n':
+ if (Ustrcmp(argrest, "q") == 0)
+ {
+ f.allow_unqualified_sender = FALSE;
+ f.allow_unqualified_recipient = FALSE;
+ }
+ else badarg = TRUE;
+ break;
+
+ /* -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. */
+ case 'p':
+ if (*argrest == 'c')
+ {
+ count_queue = TRUE;
+ if (*++argrest) 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) {}
+
+ /* -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. */
+ case 'P':
+
+ /* -bP config: we need to setup here, because later,
+ when list_options is checked, the config is read already */
+ if (*argrest)
+ badarg = TRUE;
+ else if (argv[i+1] && Ustrcmp(argv[i+1], "config") == 0)
+ {
+ list_config = TRUE;
+ readconf_save_config(version_string);
+ }
+ else
+ {
+ list_options = TRUE;
+ debug_selector |= D_v;
+ debug_file = stderr;
+ }
+ break;
+
+ /* -brt: Test retry configuration lookup */
+ case 'r':
+ if (Ustrcmp(argrest, "t") == 0)
+ {
+ checking = TRUE;
+ test_retry_arg = i + 1;
+ goto END_ARG;
+ }
+
+ /* -brw: Test rewrite configuration */
+
+ else if (Ustrcmp(argrest, "w") == 0)
+ {
+ checking = TRUE;
+ test_rewrite_arg = i + 1;
+ goto END_ARG;
+ }
+ else badarg = TRUE;
+ break;
+
+ /* -bS: Read SMTP commands on standard input, but produce no replies -
+ all errors are reported by sending messages. */
+ case 'S':
+ if (!*argrest)
+ smtp_input = smtp_batched_input = receiving_message = TRUE;
+ else badarg = TRUE;
+ break;
+
+ /* -bs: Read SMTP commands on standard input and produce SMTP replies
+ on standard output. */
+ case 's':
+ if (!*argrest) smtp_input = receiving_message = TRUE;
+ else badarg = TRUE;
+ break;
+
+ /* -bt: address testing mode */
+ case 't':
+ if (!*argrest)
+ f.address_test_mode = checking = f.log_testing_mode = TRUE;
+ else badarg = TRUE;
+ break;
+
+ /* -bv: verify addresses */
+ case 'v':
+ if (!*argrest)
+ verify_address_mode = checking = f.log_testing_mode = TRUE;
+
+ /* -bvs: verify sender addresses */
+
+ else if (Ustrcmp(argrest, "s") == 0)
+ {
+ verify_address_mode = checking = f.log_testing_mode = TRUE;
+ verify_as_sender = TRUE;
+ }
+ else badarg = TRUE;
+ break;
+
+ /* -bV: Print version string and support details */
+ case 'V':
+ if (!*argrest)
+ {
+ 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(TRUE);
+ f.log_testing_mode = TRUE;
+ }
+ else badarg = TRUE;
+ break;
+
+ /* -bw: inetd wait mode, accept a listening socket as stdin */
+ case 'w':
+ f.inetd_wait_mode = TRUE;
+ f.background_daemon = FALSE;
+ f.daemon_listen = TRUE;
+ if (*argrest)
+ if ((inetd_wait_timeout = readconf_readtime(argrest, 0, FALSE)) <= 0)
+ exim_fail("exim: bad time value %s: abandoned\n", argv[i]);
+ break;
+
+ default:
+ badarg = TRUE;
+ break;
+ }
+ break;
+ }
+
+
+ /* -C: change configuration file list; ignore if it isn't really
+ a change! Enforce a prefix check if required. */
+
+ case 'C':
+ if (!*argrest)
+ 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);
+ const uschar *list = argrest;
+ uschar *filename;
+ /* The argv is untainted, so big_buffer (also untainted) is ok to use */
+ while((filename = string_nextinlist(&list, &sep, big_buffer,
+ big_buffer_size)))
+ if ( ( Ustrlen(filename) < len
+ || Ustrncmp(filename, ALT_CONFIG_PREFIX, len) != 0
+ || Ustrstr(filename, "/../") != NULL
+ )
+ && (Ustrcmp(filename, "/dev/null") != 0 || real_uid != root_uid)
+ )
+ exim_fail("-C Permission denied\n");
+ #endif
+ if (real_uid != root_uid)
+ {
+ #ifdef TRUSTED_CONFIG_LIST
+
+ if (real_uid != exim_uid
+ #ifdef CONFIGURE_OWNER
+ && real_uid != config_uid
+ #endif
+ )
+ f.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 */
+ {
+ f.trusted_config = FALSE;
+ fclose(trust_list);
+ }
+ else
+ {
+ /* Well, the trust list at least is up to scratch... */
+ rmark reset_point;
+ uschar *trusted_configs[32];
+ int nr_configs = 0;
+ int i = 0;
+ int old_pool = store_pool;
+ store_pool = POOL_MAIN;
+
+ reset_point = store_mark();
+ 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 == nelem(trusted_configs))
+ break;
+ }
+ fclose(trust_list);
+
+ if (nr_configs)
+ {
+ int sep = 0;
+ const uschar *list = argrest;
+ uschar *filename;
+ while (f.trusted_config && (filename = string_nextinlist(&list,
+ &sep, big_buffer, big_buffer_size)))
+ {
+ for (i=0; i < nr_configs; i++)
+ if (Ustrcmp(filename, trusted_configs[i]) == 0)
+ break;
+ if (i == nr_configs)
+ {
+ f.trusted_config = FALSE;
+ break;
+ }
+ }
+ }
+ else /* No valid prefixes found in trust_list file. */
+ f.trusted_config = FALSE;
+ store_reset(reset_point);
+ store_pool = old_pool;
+ }
+ }
+ else /* Could not open trust_list file. */
+ f.trusted_config = FALSE;
+ }
+ #else
+ /* Not root; don't trust config */
+ f.trusted_config = FALSE;
+ #endif
+ }
+
+ config_main_filelist = argrest;
+ f.config_changed = TRUE;
+ }
+ break;
+
+
+ /* -D: set up a macro definition */
+
+ case 'D':
+#ifdef DISABLE_D_OPTION
+ exim_fail("exim: -D is not available in this Exim binary\n");
+#else
+ {
+ int ptr = 0;
+ macro_item *m;
+ uschar name[24];
+ uschar *s = argrest;
+
+ opt_D_used = TRUE;
+ while (isspace(*s)) s++;
+
+ if (*s < 'A' || *s > 'Z')
+ exim_fail("exim: macro name set by -D must start with "
+ "an upper case letter\n");
+
+ 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_user; m; m = m->next)
+ if (Ustrcmp(m->name, name) == 0)
+ exim_fail("exim: duplicated -D in command line\n");
+
+ m = macro_create(name, s, TRUE);
+
+ if (clmacro_count >= MAX_CLMACROS)
+ exim_fail("exim: too many -D options on command line\n");
+ clmacros[clmacro_count++] =
+ string_sprintf("-D%s=%s", m->name, m->replacement);
+ }
+ #endif
+ break;
+
+ case 'd':
+
+ /* -dropcr: Set this option. Now a no-op, retained for compatibility only. */
+
+ if (Ustrcmp(argrest, "ropcr") == 0)
+ {
+ /* drop_cr = TRUE; */
+ }
+
+ /* -dp: Set up a debug pretrigger buffer with given size. */
+
+ else if (Ustrcmp(argrest, "p") == 0)
+ if (++i >= argc)
+ badarg = TRUE;
+ else
+ debug_pretrigger_setup(argv[i]);
+
+ /* -dt: Set a debug trigger selector */
+
+ else if (Ustrncmp(argrest, "t=", 2) == 0)
+ dtrigger_selector = (unsigned int) Ustrtol(argrest + 2, NULL, 0);
+
+ /* -d: Set debug level (see also -v below).
+ If -dd is used, debugging subprocesses of the daemon is disabled. */
+
+ else
+ {
+ /* Use an intermediate variable so that we don't set debugging while
+ decoding the debugging bits. */
+
+ unsigned int selector = D_default;
+ debug_selector = 0;
+ debug_file = NULL;
+ if (*argrest == 'd')
+ {
+ f.debug_daemon = TRUE;
+ argrest++;
+ }
+ if (*argrest)
+ decode_bits(&selector, 1, debug_notall, 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':
+ f.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)
+ if (++i < argc) argrest = argv[i]; else { badarg = TRUE; break; }
+ originator_name = string_copy_taint(
+ exim_str_fail_toolong(argrest, EXIM_HUMANNAME_MAX, "-F"),
+ GET_TAINTED);
+ f.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 dummy_start, dummy_end;
+ uschar *errmess;
+ if (!*argrest)
+ if (i+1 < argc) argrest = argv[++i]; else { badarg = TRUE; break; }
+ (void) exim_str_fail_toolong(argrest, EXIM_DISPLAYMAIL_MAX, "-f");
+ if (!*argrest)
+ *(sender_address = store_get(1, GET_UNTAINTED)) = '\0'; /* 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;
+#ifdef SUPPORT_I18N
+ allow_utf8_domains = TRUE;
+#endif
+ if (!(sender_address = parse_extract_address(argrest, &errmess,
+ &dummy_start, &dummy_end, &sender_address_domain, TRUE)))
+ exim_fail("exim: bad -f address \"%s\": %s\n", argrest, errmess);
+
+ sender_address = string_copy_taint(sender_address, GET_TAINTED);
+#ifdef SUPPORT_I18N
+ message_smtputf8 = string_is_utf8(sender_address);
+ allow_utf8_domains = FALSE;
+#endif
+ allow_domain_literals = FALSE;
+ strip_trailing_dot = FALSE;
+ }
+ f.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)
+ 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) f.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)
+ if (++i < argc) argrest = argv[i]; else { badarg = TRUE; break; }
+ if ((sz = Ustrlen(argrest)) > 32)
+ exim_fail("exim: the -L syslog name is too long: \"%s\"\n", argrest);
+ if (sz < 1)
+ exim_fail("exim: the -L syslog name is too short\n");
+ cmdline_syslog_name = string_copy_taint(argrest, GET_TAINTED);
+ 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)
+ exim_fail("exim: too many or too few arguments after -MC\n");
+
+ if (msg_action_arg >= 0)
+ exim_fail("exim: incompatible arguments\n");
+
+ continue_transport = string_copy_taint(
+ exim_str_fail_toolong(argv[++i], EXIM_DRIVERNAME_MAX, "-C internal transport"),
+ GET_TAINTED);
+ continue_hostname = string_copy_taint(
+ exim_str_fail_toolong(argv[++i], EXIM_HOSTNAME_MAX, "-C internal hostname"),
+ GET_TAINTED);
+ continue_host_address = string_copy_taint(
+ exim_str_fail_toolong(argv[++i], EXIM_IPADDR_MAX, "-C internal hostaddr"),
+ GET_TAINTED);
+ 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]))
+ exim_fail("exim: malformed message id %s after -MC option\n",
+ argv[i]);
+
+ /* Set up $sending_ip_address and $sending_port, unless proxied */
+
+ if (!continue_proxy_cipher)
+ if (getsockname(fileno(stdin), (struct sockaddr *)(&interface_sock),
+ &size) == 0)
+ sending_ip_address = host_ntoa(-1, &interface_sock, NULL,
+ &sending_port);
+ else
+ exim_fail("exim: getsockname() failed after -MC option: %s\n",
+ strerror(errno));
+
+ testharness_pause_ms(500);
+ break;
+ }
+
+ else if (*argrest == 'C' && argrest[1] && !argrest[2])
+ {
+ switch(argrest[1])
+ {
+ /* -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. */
+
+ case 'A': f.smtp_authenticated = TRUE; break;
+
+ /* -MCD: set the smtp_use_dsn flag; this indicates that the host
+ that exim is connected to supports the esmtp extension DSN */
+
+ case 'D': smtp_peer_options |= OPTION_DSN; break;
+
+ /* -MCd: for debug, set a process-purpose string */
+
+ case 'd': if (++i < argc)
+ process_purpose = string_copy_taint(
+ exim_str_fail_toolong(argv[i], EXIM_DRIVERNAME_MAX, "-MCd"),
+ GET_TAINTED);
+ else badarg = TRUE;
+ break;
+
+ /* -MCG: set the queue name, to a non-default value. Arguably, anything
+ from the commandline should be tainted - but we will need an untainted
+ value for the spoolfile when doing a -odi delivery process. */
+
+ case 'G': if (++i < argc) queue_name = string_copy_taint(
+ exim_str_fail_toolong(argv[i], EXIM_DRIVERNAME_MAX, "-MCG"),
+ GET_UNTAINTED);
+ else badarg = TRUE;
+ break;
+
+ /* -MCK: the peer offered CHUNKING. Must precede -MC */
+
+ case 'K': smtp_peer_options |= OPTION_CHUNKING; break;
+
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ /* -MCL: peer used LIMITS RCPTMAX and/or RCPTDOMAINMAX */
+ case 'L': if (++i < argc) continue_limit_mail = Uatoi(argv[i]);
+ else badarg = TRUE;
+ if (++i < argc) continue_limit_rcpt = Uatoi(argv[i]);
+ else badarg = TRUE;
+ if (++i < argc) continue_limit_rcptdom = Uatoi(argv[i]);
+ else badarg = TRUE;
+ break;
+#endif
+
+ /* -MCP: set the smtp_use_pipelining flag; this is useful only when
+ it preceded -MC (see above) */
+
+ case 'P': smtp_peer_options |= OPTION_PIPE; break;
+
+#ifdef SUPPORT_SOCKS
+ /* -MCp: Socks proxy in use; nearside IP, port, external IP, port */
+ case 'p': proxy_session = TRUE;
+ if (++i < argc)
+ {
+ proxy_local_address = string_copy_taint(argv[i], GET_TAINTED);
+ if (++i < argc)
+ {
+ proxy_local_port = Uatoi(argv[i]);
+ if (++i < argc)
+ {
+ proxy_external_address = string_copy_taint(argv[i], GET_TAINTED);
+ if (++i < argc)
+ {
+ proxy_external_port = Uatoi(argv[i]);
+ break;
+ } } } }
+ badarg = TRUE;
+ break;
+#endif
+ /* -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) */
+
+ case 'Q': 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;
+
+ /* -MCq: do a quota check on the given recipient for the given size
+ of message. Separate from -MC. */
+ case 'q': rcpt_verify_quota = TRUE;
+ if (++i < argc) message_size = Uatoi(argv[i]);
+ else badarg = TRUE;
+ break;
+
+ /* -MCS: set the smtp_use_size flag; this is useful only when it
+ precedes -MC (see above) */
+
+ case 'S': smtp_peer_options |= OPTION_SIZE; break;
+
+#ifndef DISABLE_TLS
+ /* -MCs: used with -MCt; SNI was sent */
+ /* -MCr: ditto, DANE */
+
+ case 'r':
+ case 's': if (++i < argc)
+ {
+ continue_proxy_sni = string_copy_taint(
+ exim_str_fail_toolong(argv[i], EXIM_HOSTNAME_MAX, "-MCr/-MCs"),
+ GET_TAINTED);
+ if (argrest[1] == 'r') continue_proxy_dane = TRUE;
+ }
+ else badarg = TRUE;
+ break;
+
+ /* -MCt: similar to -MCT below but the connection is still open
+ via a proxy process which handles the TLS context and coding.
+ Require three arguments for the proxied local address and port,
+ and the TLS cipher. */
+
+ case 't': if (++i < argc)
+ sending_ip_address = string_copy_taint(
+ exim_str_fail_toolong(argv[i], EXIM_IPADDR_MAX, "-MCt IP"),
+ GET_TAINTED);
+ else badarg = TRUE;
+ if (++i < argc)
+ sending_port = (int)(Uatol(argv[i]));
+ else badarg = TRUE;
+ if (++i < argc)
+ continue_proxy_cipher = string_copy_taint(
+ exim_str_fail_toolong(argv[i], EXIM_CIPHERNAME_MAX, "-MCt cipher"),
+ GET_TAINTED);
+ else badarg = TRUE;
+ /*FALLTHROUGH*/
+
+ /* -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. */
+
+ case 'T': smtp_peer_options |= OPTION_TLS; break;
+#endif
+
+ default: badarg = TRUE; break;
+ }
+ break;
+ }
+
+ /* -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)
+ -MG move to a different queue
+ -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)
+ {
+ msg_action = MSG_DELIVER;
+ forced_delivery = f.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, "G") == 0)
+ {
+ msg_action = MSG_SETQUEUE;
+ queue_name_dest = string_copy_taint(
+ exim_str_fail_toolong(argv[++i], EXIM_DRIVERNAME_MAX, "-MG"),
+ GET_TAINTED);
+ }
+ 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)
+ exim_fail("exim: no message ids given after %s option\n", arg);
+
+ /* Some require only message ids to follow */
+
+ if (!one_msg_action)
+ {
+ for (int j = msg_action_arg; j < argc; j++) if (!mac_ismsgid(argv[j]))
+ exim_fail("exim: malformed message id %s after %s option\n",
+ argv[j], arg);
+ 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]))
+ exim_fail("exim: malformed message id %s after %s option\n",
+ argv[msg_action_arg], arg);
+ 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) 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)
+ {
+ f.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)
+ if (++i >= argc)
+ exim_fail("exim: string expected after -O\n");
+ break;
+
+ case 'o':
+ switch (*argrest++)
+ {
+ /* -oA: Set an argument for the bi command (sendmail's "alternate alias
+ file" option). */
+ case 'A':
+ if (!*(alias_arg = argrest))
+ if (i+1 < argc) alias_arg = argv[++i];
+ else exim_fail("exim: string expected after -oA\n");
+ break;
+
+ /* -oB: Set a connection message max value for remote deliveries */
+ case 'B':
+ {
+ uschar * p = argrest;
+ if (!*p)
+ if (i+1 < argc && isdigit((argv[i+1][0])))
+ p = argv[++i];
+ else
+ {
+ connection_max_messages = 1;
+ p = NULL;
+ }
+
+ if (p)
+ {
+ if (!isdigit(*p))
+ exim_fail("exim: number expected after -oB\n");
+ connection_max_messages = Uatoi(p);
+ }
+ }
+ break;
+
+ /* -odb: background delivery */
+
+ case 'd':
+ if (Ustrcmp(argrest, "b") == 0)
+ {
+ f.synchronous_delivery = FALSE;
+ arg_queue_only = FALSE;
+ queue_only_set = TRUE;
+ }
+
+ /* -odd: testsuite-only: add no inter-process delays */
+
+ else if (Ustrcmp(argrest, "d") == 0)
+ f.testsuite_delays = FALSE;
+
+ /* -odf: foreground delivery (smail-compatible option); same effect as
+ -odi: interactive (synchronous) delivery (sendmail-compatible option)
+ */
+
+ else if (Ustrcmp(argrest, "f") == 0 || Ustrcmp(argrest, "i") == 0)
+ {
+ f.synchronous_delivery = TRUE;
+ arg_queue_only = FALSE;
+ queue_only_set = TRUE;
+ }
+
+ /* -odq: queue only */
+
+ else if (Ustrcmp(argrest, "q") == 0)
+ {
+ f.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, "qs") == 0)
+ {
+ f.queue_smtp = TRUE;
+ arg_queue_only = FALSE;
+ queue_only_set = TRUE;
+ }
+ else badarg = TRUE;
+ break;
+
+ /* -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 */
+
+ case 'i':
+ if (!*argrest || Ustrcmp(argrest, "true") == 0)
+ f.dot_ends = FALSE;
+ else badarg = TRUE;
+ break;
+
+ /* -oM*: Set various characteristics for an incoming message; actually
+ acted on for trusted callers only. */
+
+ case 'M':
+ {
+ if (i+1 >= argc)
+ exim_fail("exim: data expected after -oM%s\n", argrest);
+
+ /* -oMa: Set sender host address */
+
+ if (Ustrcmp(argrest, "a") == 0)
+ sender_host_address = string_copy_taint(
+ exim_str_fail_toolong(argv[++i], EXIM_IPADDR_MAX, "-oMa"),
+ GET_TAINTED);
+
+ /* -oMaa: Set authenticator name */
+
+ else if (Ustrcmp(argrest, "aa") == 0)
+ sender_host_authenticated = string_copy_taint(
+ exim_str_fail_toolong(argv[++i], EXIM_DRIVERNAME_MAX, "-oMaa"),
+ GET_TAINTED);
+
+ /* -oMas: setting authenticated sender */
+
+ else if (Ustrcmp(argrest, "as") == 0)
+ authenticated_sender = string_copy_taint(
+ exim_str_fail_toolong(argv[++i], EXIM_EMAILADDR_MAX, "-oMas"),
+ GET_TAINTED);
+
+ /* -oMai: setting authenticated id */
+
+ else if (Ustrcmp(argrest, "ai") == 0)
+ authenticated_id = string_copy_taint(
+ exim_str_fail_toolong(argv[++i], EXIM_EMAILADDR_MAX, "-oMas"),
+ GET_TAINTED);
+
+ /* -oMi: Set incoming interface address */
+
+ else if (Ustrcmp(argrest, "i") == 0)
+ interface_address = string_copy_taint(
+ exim_str_fail_toolong(argv[++i], EXIM_IPADDR_MAX, "-oMi"),
+ GET_TAINTED);
+
+ /* -oMm: Message reference */
+
+ else if (Ustrcmp(argrest, "m") == 0)
+ {
+ if (!mac_ismsgid(argv[i+1]))
+ exim_fail("-oMm must be a valid message ID\n");
+ if (!f.trusted_config)
+ exim_fail("-oMm must be called by a trusted user/config\n");
+ message_reference = argv[++i];
+ }
+
+ /* -oMr: Received protocol */
+
+ else if (Ustrcmp(argrest, "r") == 0)
+
+ if (received_protocol)
+ exim_fail("received_protocol is set already\n");
+ else
+ received_protocol = string_copy_taint(
+ exim_str_fail_toolong(argv[++i], EXIM_DRIVERNAME_MAX, "-oMr"),
+ GET_TAINTED);
+
+ /* -oMs: Set sender host name */
+
+ else if (Ustrcmp(argrest, "s") == 0)
+ sender_host_name = string_copy_taint(
+ exim_str_fail_toolong(argv[++i], EXIM_HOSTNAME_MAX, "-oMs"),
+ GET_TAINTED);
+
+ /* -oMt: Set sender ident */
+
+ else if (Ustrcmp(argrest, "t") == 0)
+ {
+ sender_ident_set = TRUE;
+ sender_ident = string_copy_taint(
+ exim_str_fail_toolong(argv[++i], EXIM_IDENTUSER_MAX, "-oMt"),
+ GET_TAINTED);
+ }
+
+ /* 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). */
+ /* -oo: An ancient flag for old-style addresses which still seems to
+ crop up in some calls (see in SCO). */
+
+ case 'm':
+ case 'o':
+ if (*argrest) badarg = TRUE;
+ break;
+
+ /* -oP <name>: set pid file path for daemon
+ -oPX: delete pid file of daemon */
+
+ case 'P':
+ if (!f.running_in_test_harness && real_uid != root_uid && real_uid != exim_uid)
+ exim_fail("exim: only uid=%d or uid=%d can use -oP and -oPX "
+ "(uid=%d euid=%d | %d)\n",
+ root_uid, exim_uid, getuid(), geteuid(), real_uid);
+ if (!*argrest) override_pid_file_path = argv[++i];
+ else if (Ustrcmp(argrest, "X") == 0) delete_pid_file();
+ else badarg = TRUE;
+ break;
+
+
+ /* -or <n>: set timeout for non-SMTP acceptance
+ -os <n>: set timeout for SMTP acceptance */
+
+ case 'r':
+ case 's':
+ {
+ int * tp = argrest[-1] == 'r'
+ ? &arg_receive_timeout : &arg_smtp_receive_timeout;
+ if (*argrest)
+ *tp = readconf_readtime(argrest, 0, FALSE);
+ else if (i+1 < argc)
+ *tp = readconf_readtime(argv[++i], 0, FALSE);
+
+ if (*tp < 0)
+ exim_fail("exim: bad time value %s: abandoned\n", argv[i]);
+ }
+ break;
+
+ /* -oX <list>: Override local_interfaces and/or default daemon ports */
+ /* Limits: Is there a real limit we want here? 1024 is very arbitrary. */
+
+ case 'X':
+ if (*argrest) badarg = TRUE;
+ else override_local_interfaces = string_copy_taint(
+ exim_str_fail_toolong(argv[++i], 1024, "-oX"),
+ GET_TAINTED);
+ break;
+
+ /* -oY: Override creation of daemon notifier socket */
+
+ case 'Y':
+ if (*argrest) badarg = TRUE;
+ else notifier_socket = NULL;
+ break;
+
+ /* Unknown -o argument */
+
+ default:
+ 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)
+ if (i+1 < argc) argrest = argv[++i]; else { badarg = TRUE; break; }
+
+ if (*argrest)
+ {
+ uschar * hn = Ustrchr(argrest, ':');
+
+ if (received_protocol)
+ exim_fail("received_protocol is set already\n");
+
+ if (!hn)
+ received_protocol = string_copy_taint(
+ exim_str_fail_toolong(argrest, EXIM_DRIVERNAME_MAX, "-p<protocol>"),
+ GET_TAINTED);
+ else
+ {
+ (void) exim_str_fail_toolong(argrest, (EXIM_DRIVERNAME_MAX+1+EXIM_HOSTNAME_MAX), "-p<protocol>:<host>");
+ received_protocol = string_copyn_taint(argrest, hn - argrest, GET_TAINTED);
+ sender_host_name = string_copy_taint(hn + 1, GET_TAINTED);
+ }
+ }
+ break;
+
+
+ case 'q':
+ receiving_message = FALSE;
+ if (queue_interval >= 0)
+ exim_fail("exim: -q specified more than once\n");
+
+ /* -qq...: Do queue runs in a 2-stage manner */
+
+ if (*argrest == 'q')
+ {
+ f.queue_2stage = TRUE;
+ argrest++;
+ }
+
+ /* -qi...: Do only first (initial) deliveries */
+
+ if (*argrest == 'i')
+ {
+ f.queue_run_first_delivery = TRUE;
+ argrest++;
+ }
+
+ /* -qf...: Run the queue, forcing deliveries
+ -qff..: Ditto, forcing thawing as well */
+
+ if (*argrest == 'f')
+ {
+ f.queue_run_force = TRUE;
+ if (*++argrest == 'f')
+ {
+ f.deliver_force_thaw = TRUE;
+ argrest++;
+ }
+ }
+
+ /* -q[f][f]l...: Run the queue only on local deliveries */
+
+ if (*argrest == 'l')
+ {
+ f.queue_run_local = TRUE;
+ argrest++;
+ }
+
+ /* -q[f][f][l][G<name>]... Work on the named queue */
+
+ if (*argrest == 'G')
+ {
+ int i;
+ for (argrest++, i = 0; argrest[i] && argrest[i] != '/'; ) i++;
+ exim_len_fail_toolong(i, EXIM_DRIVERNAME_MAX, "-q*G<name>");
+ queue_name = string_copyn(argrest, i);
+ argrest += i;
+ if (*argrest == '/') argrest++;
+ }
+
+ /* -q[f][f][l][G<name>]: Run the queue, optionally forced, optionally local
+ only, optionally named, optionally starting from a given message id. */
+
+ if (!(list_queue || count_queue))
+ if ( !*argrest
+ && (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 = string_copy_taint(argv[++i], GET_TAINTED);
+ if (i+1 < argc && mac_ismsgid(argv[i+1]))
+ stop_queue_run_id = string_copy_taint(argv[++i], GET_TAINTED);
+ }
+
+ /* -q[f][f][l][G<name>/]<n>: Run the queue at regular intervals, optionally
+ forced, optionally local only, optionally named. */
+
+ else if ((queue_interval = readconf_readtime(*argrest ? argrest : argv[++i],
+ 0, FALSE)) <= 0)
+ exim_fail("exim: bad time value %s: abandoned\n", argv[i]);
+ break;
+
+
+ case 'R': /* Synonymous with -qR... */
+ {
+ const uschar *tainted_selectstr;
+
+ 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)
+ for (int i = 0; i < nelem(rsopts); i++)
+ if (Ustrcmp(argrest, rsopts[i]) == 0)
+ {
+ if (i != 2) f.queue_run_force = TRUE;
+ if (i >= 2) f.deliver_selectstring_regex = TRUE;
+ if (i == 1 || i == 4) f.deliver_force_thaw = TRUE;
+ argrest += Ustrlen(rsopts[i]);
+ }
+
+ /* -R: Set string to match in addresses for forced queue run to
+ pick out particular messages. */
+
+ /* Avoid attacks from people providing very long strings, and do so before
+ we make copies. */
+ if (*argrest)
+ tainted_selectstr = argrest;
+ else if (i+1 < argc)
+ tainted_selectstr = argv[++i];
+ else
+ exim_fail("exim: string expected after -R\n");
+ deliver_selectstring = string_copy_taint(
+ exim_str_fail_toolong(tainted_selectstr, EXIM_EMAILADDR_MAX, "-R"),
+ GET_TAINTED);
+ }
+ break;
+
+ /* -r: an obsolete synonym for -f (see above) */
+
+
+ /* -S: Like -R but works on sender. */
+
+ case 'S': /* Synonymous with -qS... */
+ {
+ const uschar *tainted_selectstr;
+
+ 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)
+ for (int i = 0; i < nelem(rsopts); i++)
+ if (Ustrcmp(argrest, rsopts[i]) == 0)
+ {
+ if (i != 2) f.queue_run_force = TRUE;
+ if (i >= 2) f.deliver_selectstring_sender_regex = TRUE;
+ if (i == 1 || i == 4) f.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)
+ tainted_selectstr = argrest;
+ else if (i+1 < argc)
+ tainted_selectstr = argv[++i];
+ else
+ exim_fail("exim: string expected after -S\n");
+ deliver_selectstring_sender = string_copy_taint(
+ exim_str_fail_toolong(tainted_selectstr, EXIM_EMAILADDR_MAX, "-S"),
+ GET_TAINTED);
+ }
+ 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 (f.running_in_test_harness && Ustrcmp(argrest, "qt") == 0)
+ fudged_queue_times = string_copy_taint(argv[++i], GET_TAINTED);
+ else badarg = TRUE;
+ break;
+
+
+ /* -t: Set flag to extract recipients from body of message. */
+
+ case 't':
+ if (!*argrest) 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;
+ f.dot_ends = FALSE;
+ }
+
+ /* -tls-on-connect: don't wait for STARTTLS (for old clients) */
+
+ #ifndef DISABLE_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)
+ {
+ 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) 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)
+ if (++i >= argc)
+ exim_fail("exim: string expected after -X\n");
+ break;
+
+ /* -z: a line of text to log */
+
+ case 'z':
+ if (!*argrest)
+ if (++i < argc)
+ log_oneline = string_copy_taint(
+ exim_str_fail_toolong(argv[i], 2048, "-z logtext"),
+ GET_TAINTED);
+ else
+ exim_fail("exim: file name expected after %s\n", argv[i-1]);
+ 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)
+ exim_fail("exim abandoned: unknown, malformed, or incomplete "
+ "option %s\n", arg);
+ }
+
+
+/* If -R or -S have been specified without -q, assume a single queue run. */
+
+ if ( (deliver_selectstring || deliver_selectstring_sender)
+ && queue_interval < 0)
+ queue_interval = 0;
+
+
+END_ARG:
+ store_pool = old_pool;
+ }
+
+/* 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)
+ && ( f.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
+ && ( f.daemon_listen || queue_interval > 0 || list_options
+ || checking && msg_action != MSG_LOAD
+ || bi_option || test_retry_arg >= 0 || test_rewrite_arg >= 0
+ ) )
+ || ( (f.daemon_listen || queue_interval > 0)
+ && ( sender_address || list_options || list_queue || checking
+ || bi_option
+ ) )
+ || f.daemon_listen && queue_interval == 0
+ || f.inetd_wait_mode && queue_interval >= 0
+ || ( list_options
+ && ( checking || smtp_input || extract_recipients
+ || filter_test != FTEST_NONE || bi_option
+ ) )
+ || ( verify_address_mode
+ && ( f.address_test_mode || smtp_input || extract_recipients
+ || filter_test != FTEST_NONE || bi_option
+ ) )
+ || ( f.address_test_mode
+ && ( smtp_input || extract_recipients || filter_test != FTEST_NONE
+ || bi_option
+ ) )
+ || ( smtp_input
+ && (sender_address || filter_test != FTEST_NONE || extract_recipients)
+ )
+ || deliver_selectstring && queue_interval < 0
+ || msg_action == MSG_LOAD && (!expansion_test || expansion_test_message)
+ )
+ exim_fail("exim: incompatible command-line options or arguments\n");
+
+/* 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);
+ f.background_daemon = FALSE;
+ testharness_pause_ms(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(FALSE);
+ }
+ }
+
+/* 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. */
+
+if ((group_count = getgroups(nelem(group_list), group_list)) < 0)
+ exim_fail("exim: getgroups() failed: %s\n", strerror(errno));
+
+/* 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.
+
+ Unfortunately, recent MacOS, which should be a FreeBSD, "helpfully" succeeds
+ the "setgroups() with zero groups" - and changes the egid.
+ Thanks to that we had to stash the original_egid above, for use below
+ in the call to exim_setugid().
+
+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.
+Except, sigh, for Hurd - where you can.
+Not being root here happens only in some unusual configurations. */
+
+if ( !unprivileged
+#ifndef OS_SETGROUPS_ZERO_DROPS_ALL
+ && setgroups(0, NULL) != 0
+#endif
+ && setgroups(1, group_list) != 0)
+ exim_fail("exim: setgroups() failed: %s\n", strerror(errno));
+
+/* 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 */
+ (!f.trusted_config || /* Config changed, or */
+ !macros_trusted(opt_D_used)) && /* impermissible macros and */
+ real_uid != root_uid && /* Not root, and */
+ !f.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 && real_uid != exim_uid)
+ f.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(), original_egid, 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)
+ if ((filter_sfd = Uopen(filter_test_sfile, O_RDONLY, 0)) < 0)
+ exim_fail("exim: failed to open %s: %s\n", filter_test_sfile,
+ strerror(errno));
+
+if (filter_test & FTEST_USER)
+ if ((filter_ufd = Uopen(filter_test_ufile, O_RDONLY, 0)) < 0)
+ exim_fail("exim: failed to open %s: %s\n", filter_test_ufile,
+ strerror(errno));
+
+/* 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();
+
+/*XXX this excrescence could move to the testsuite standard config setup file */
+#ifdef SUPPORT_I18N
+if (f.running_in_test_harness) smtputf8_advertise_hosts = NULL;
+#endif
+
+/* 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.
+
+NOTE: immediately after opening the configuration file we change the working
+directory to "/"! Later we change to $spool_directory. We do it there, because
+during readconf_main() some expansion takes place already. */
+
+/* Store the initial cwd before we change directories. Can be NULL if the
+dir has already been unlinked. */
+initial_cwd = os_getcwd(NULL, 0);
+if (!initial_cwd && errno)
+ exim_fail("exim: getting initial cwd failed: %s\n", strerror(errno));
+
+if (initial_cwd && (strlen(CCS initial_cwd) >= BIG_BUFFER_SIZE))
+ exim_fail("exim: initial cwd is far too long (%d)\n", Ustrlen(CCS initial_cwd));
+
+/* checking:
+ -be[m] expansion test -
+ -b[fF] filter test new
+ -bh[c] host test -
+ -bmalware malware_test_file new
+ -brt retry test new
+ -brw rewrite test new
+ -bt address test -
+ -bv[s] address verify -
+ list_options:
+ -bP <option> (except -bP config, which sets list_config)
+
+If any of these options is set, we suppress warnings about configuration
+issues (currently about tls_advertise_hosts and keep_environment not being
+defined) */
+
+ {
+ int old_pool = store_pool;
+#ifdef MEASURE_TIMING
+ struct timeval t0;
+ (void)gettimeofday(&t0, NULL);
+#endif
+
+ store_pool = POOL_CONFIG;
+ readconf_main(checking || list_options);
+ store_pool = old_pool;
+
+#ifdef MEASURE_TIMING
+ report_time_since(&t0, US"readconf_main (delta)");
+#endif
+ }
+
+/* Now in directory "/" */
+
+if (cleanup_environment() == FALSE)
+ log_write(0, LOG_PANIC_DIE, "Can't cleanup environment");
+
+
+/* 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)
+ f.admin_user = TRUE;
+else
+ for (int i = 0; i < group_count && !f.admin_user; i++)
+ if (group_list[i] == exim_gid)
+ f.admin_user = TRUE;
+ else if (admin_groups)
+ for (int j = 1; j <= (int)admin_groups[0] && !f.admin_user; j++)
+ if (admin_groups[j] == group_list[i])
+ f.admin_user = TRUE;
+
+/* 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)
+ f.trusted_caller = TRUE;
+else
+ {
+ if (trusted_users)
+ for (int i = 1; i <= (int)trusted_users[0] && !f.trusted_caller; i++)
+ if (trusted_users[i] == real_uid)
+ f.trusted_caller = TRUE;
+
+ if (trusted_groups)
+ for (int i = 1; i <= (int)trusted_groups[0] && !f.trusted_caller; i++)
+ if (trusted_groups[i] == real_gid)
+ f.trusted_caller = TRUE;
+ else for (int j = 0; j < group_count && !f.trusted_caller; j++)
+ if (trusted_groups[i] == group_list[j])
+ f.trusted_caller = TRUE;
+ }
+
+/* At this point, we know if the user is privileged and some command-line
+options become possibly impermissible, depending upon the configuration file. */
+
+if (checking && commandline_checks_require_admin && !f.admin_user)
+ exim_fail("exim: those command-line flags are set to require admin\n");
+
+/* Handle the decoding of logging options. */
+
+decode_bits(log_selector, log_selector_size, log_notall,
+ 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 =");
+ for (int i = 0; i < log_selector_size; i++)
+ debug_printf(" %08x", log_selector[i]);
+ debug_printf("\n");
+ }
+
+/* If domain literals are not allowed, check the sender address that was
+supplied with -f. Ditto for a stripped trailing dot. */
+
+if (sender_address)
+ {
+ if (sender_address[sender_address_domain] == '[' && !allow_domain_literals)
+ exim_fail("exim: bad -f address \"%s\": domain literals not "
+ "allowed\n", sender_address);
+ if (f_end_dot && !strip_trailing_dot)
+ exim_fail("exim: bad -f address \"%s.\": domain is malformed "
+ "(trailing dot not allowed)\n", sender_address);
+ }
+
+/* See if an admin user overrode our logging. */
+
+if (cmdline_syslog_name)
+ if (f.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 */
+ exim_fail(
+ "exim: you lack sufficient privilege to specify syslog process name\n");
+
+/* 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");
+
+if (log_oneline)
+ if (f.admin_user)
+ {
+ log_write(0, LOG_MAIN, "%s", log_oneline);
+ return EXIT_SUCCESS;
+ }
+ else
+ return EXIT_FAILURE;
+
+/* 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
+EXIM_TMPDIR macro, if this macro is defined. For backward compatibility this
+macro may be called TMPDIR in old "Local/Makefile"s. It's converted to
+EXIM_TMPDIR by the build scripts.
+*/
+
+#ifdef EXIM_TMPDIR
+ if (environ) for (uschar ** p = USS environ; *p; p++)
+ if (Ustrncmp(*p, "TMPDIR=", 7) == 0 && Ustrcmp(*p+7, EXIM_TMPDIR) != 0)
+ {
+ uschar * newp = store_malloc(Ustrlen(EXIM_TMPDIR) + 8);
+ sprintf(CS newp, "TMPDIR=%s", EXIM_TMPDIR);
+ *p = newp;
+ DEBUG(D_any) debug_printf("reset TMPDIR=%s in environment\n", EXIM_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 && strcmpic(timezone_string, US"UTC") == 0)
+ f.timestamps_utc = TRUE;
+else
+ {
+ uschar *envtz = US getenv("TZ");
+ if (envtz
+ ? !timezone_string || Ustrcmp(timezone_string, envtz) != 0
+ : timezone_string != NULL
+ )
+ {
+ uschar **p = USS environ;
+ uschar **new;
+ uschar **newp;
+ int count = 0;
+ if (environ) while (*p++) count++;
+ if (!envtz) count++;
+ newp = new = store_malloc(sizeof(uschar *) * (count + 1));
+ if (environ) for (p = USS environ; *p; p++)
+ if (Ustrncmp(*p, "TZ=", 3) != 0) *newp++ = *p;
+ if (timezone_string)
+ {
+ *newp = store_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
+ && (!f.trusted_config || opt_D_used)
+ && real_uid == exim_uid)
+ if (deliver_drop_privilege)
+ f.really_exim = TRUE; /* let logging work normally */
+ else
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "exim user lost privilege for using %s option",
+ f.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");
+ if ((errstr = init_perl(opt_perl_startup)))
+ exim_fail("exim: error in perl_startup code: %s\n", errstr);
+ 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 || LOGGING(arguments))
+ && f.really_exim && !list_options && !checking)
+ {
+ uschar *p = big_buffer;
+ Ustrcpy(p, US"cwd= (failed)");
+
+ if (!initial_cwd)
+ p += 13;
+ else
+ {
+ p += 4;
+ snprintf(CS p, big_buffer_size - (p - big_buffer), "%s", CCS initial_cwd);
+ p += Ustrlen(CCS p);
+ }
+
+ (void)string_format(p, big_buffer_size - (p - big_buffer), " %d args:", argc);
+ while (*p) p++;
+ for (int i = 0; i < argc; i++)
+ {
+ int len = Ustrlen(argv[i]);
+ const uschar *printing;
+ uschar *quote;
+ if (p + len + 8 >= big_buffer + big_buffer_size)
+ {
+ Ustrcpy(p, US" ...");
+ log_write(0, LOG_MAIN, "%s", big_buffer);
+ Ustrcpy(big_buffer, US"...");
+ p = big_buffer + 3;
+ }
+ printing = string_printing(argv[i]);
+ if (!*printing) quote = US"\"";
+ else
+ {
+ const uschar *pp = printing;
+ quote = US"";
+ while (*pp) if (isspace(*pp++)) { quote = US"\""; break; }
+ }
+ p += sprintf(CS p, " %s%.*s%s", quote, (int)(big_buffer_size -
+ (p - big_buffer) - 4), printing, quote);
+ }
+
+ if (LOGGING(arguments))
+ 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)
+ {
+ (void) directory_make(spool_directory, US"", SPOOL_DIRECTORY_MODE, FALSE);
+ (void) 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 && *bi_command)
+ {
+ int i = 0;
+ uschar *argv[3];
+ argv[i++] = bi_command; /* nonexpanded option so assume untainted */
+ if (alias_arg) 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' %s%.256s%s\n", argv[0],
+ argv[1] ? "'" : "", argv[1] ? argv[1] : US"", argv[1] ? "'" : "");
+
+ execv(CS argv[0], (char *const *)argv);
+ exim_fail("exim: exec '%s' failed: %s\n", argv[0], strerror(errno));
+ }
+ 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 (f.trusted_caller) DEBUG(D_any) debug_printf("trusted user\n");
+if (f.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 (!f.admin_user)
+ {
+ BOOL debugset = (debug_selector & ~D_v) != 0;
+ if ( deliver_give_up || f.daemon_listen || malware_test_file
+ || count_queue && queue_list_requires_admin
+ || list_queue && queue_list_requires_admin
+ || queue_interval >= 0 && prod_requires_admin
+ || queue_name_dest && prod_requires_admin
+ || debugset && !f.running_in_test_harness
+ )
+ exim_fail("exim:%s permission denied\n", debugset ? " debugging" : "");
+ }
+
+/* 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
+ || ( f.dont_deliver
+ && (queue_interval >= 0 || f.daemon_listen || msg_action_arg > 0)
+ ) )
+ && !f.running_in_test_harness
+ )
+ exim_fail("exim: Permission denied\n");
+
+/* 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 (!f.trusted_caller && !checking)
+ {
+ 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)
+ sender_host_port = check_port(sender_host_address);
+ if (interface_address)
+ interface_port = check_port(interface_address);
+ }
+
+/* If the caller is trusted, then they can use -G to suppress_local_fixups. */
+if (flag_G)
+ {
+ if (f.trusted_caller)
+ {
+ f.suppress_local_fixups = f.suppress_local_fixups_default = TRUE;
+ DEBUG(D_acl) debug_printf("suppress_local_fixups forced on by -G\n");
+ }
+ else
+ exim_fail("exim: permission denied (-G requires a trusted user)\n");
+ }
+
+/* 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)
+ {
+ f.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
+ exim_fail(
+ "exim: Permission denied (unprivileged user, unprivileged port)\n");
+ }
+ }
+ }
+
+/* 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 || (f.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 */
+ && !f.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 || !f.address_test_mode) /* not address checking */
+ && !rcpt_verify_quota /* and not quota 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;
+ DEBUG(D_any) debug_printf("dropping to exim gid; retaining priv uid\n");
+ 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))
+ exim_fail("exim: changing group failed: %s\n", strerror(errno));
+ 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");
+ if ((result = malware_in_file(malware_test_file)) == 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");
+ fprintf(stdout, "%u\n", 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");
+
+ /* ACL definitions may be needed when removing a message (-Mrm) because
+ event_action gets expanded */
+
+ if (msg_action == MSG_REMOVE)
+ {
+ int old_pool = store_pool;
+ store_pool = POOL_CONFIG;
+ readconf_rest();
+ store_pool = old_pool;
+ store_writeprotect(POOL_CONFIG);
+ }
+
+ 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;
+ switch (msg_action)
+ {
+ case MSG_REMOVE: case MSG_FREEZE: case MSG_THAW: break;
+ default: printf("\n"); break;
+ }
+ }
+
+ 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 && !f.daemon_listen)
+Now, since the intro of the ${acl } expansion, ACL definitions may be
+needed in transports so we lost the optimisation. */
+
+ {
+ int old_pool = store_pool;
+#ifdef MEASURE_TIMING
+ struct timeval t0;
+ (void)gettimeofday(&t0, NULL);
+#endif
+
+ store_pool = POOL_CONFIG;
+ readconf_rest();
+ store_pool = old_pool;
+
+ /* -be can add macro definitions, needing to link to the macro structure
+ chain. Otherwise, make the memory used for config data readonly. */
+
+ if (!expansion_test)
+ store_writeprotect(POOL_CONFIG);
+
+#ifdef MEASURE_TIMING
+ report_time_since(&t0, US"readconf_rest (delta)");
+#endif
+ }
+
+/* Handle a request to check quota */
+if (rcpt_verify_quota)
+ if (real_uid != root_uid && real_uid != exim_uid)
+ exim_fail("exim: Permission denied\n");
+ else if (recipients_arg >= argc)
+ exim_fail("exim: missing recipient for quota check\n");
+ else
+ {
+ verify_quota(argv[recipients_arg]);
+ exim_exit(EXIT_SUCCESS);
+ }
+
+/* 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;
+ const uschar *s1, *s2;
+
+ if (test_retry_arg >= argc)
+ {
+ printf("-brt needs a domain or address argument\n");
+ exim_exit(EXIT_FAILURE);
+ }
+ s1 = exim_str_fail_toolong(argv[test_retry_arg++], EXIM_EMAILADDR_MAX, "-brt");
+ 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 = exim_str_fail_toolong(argv[test_retry_arg++], EXIM_DOMAINNAME_MAX, "-brt 2nd");
+
+ /* The final arg is an error name */
+
+ if (test_retry_arg < argc)
+ {
+ const uschar *ss = exim_str_fail_toolong(argv[test_retry_arg], EXIM_DRIVERNAME_MAX, "-brt 3rd");
+ 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);
+ }
+ }
+
+ if (!(yield = retry_find_config(s1, s2, basic_errno, more_errno)))
+ printf("No retry information found\n");
+ else
+ {
+ 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 (retry_rule * r = yield->rules; r; 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)
+ {
+ BOOL fail = FALSE;
+ set_process_info("listing variables");
+ if (recipients_arg >= argc)
+ fail = !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 ||
+ Ustrcmp(argv[i], "environment") == 0))
+ {
+ fail |= !readconf_print(exim_str_fail_toolong(argv[i+1], EXIM_DRIVERNAME_MAX, "-bP name"), argv[i], flag_n);
+ i++;
+ }
+ else
+ fail = !readconf_print(exim_str_fail_toolong(argv[i], EXIM_DRIVERNAME_MAX, "-bP item"), NULL, flag_n);
+ }
+ exim_exit(fail ? EXIT_FAILURE : EXIT_SUCCESS);
+ }
+
+if (list_config)
+ {
+ set_process_info("listing config");
+ exim_exit(readconf_print(US"config", NULL, flag_n)
+ ? EXIT_SUCCESS : EXIT_FAILURE);
+ }
+
+
+/* Initialise subsystems as required. */
+
+tcp_init();
+
+/* 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 && !f.admin_user)
+ {
+ fprintf(stderr, "exim: Permission denied\n");
+ exim_exit(EXIT_FAILURE);
+ }
+ set_process_info("delivering specified messages");
+ if (deliver_give_up) forced_delivery = f.deliver_force_thaw = TRUE;
+ for (i = msg_action_arg; i < argc; i++)
+ {
+ int status;
+ pid_t pid;
+ /*XXX This use of argv[i] for msg_id should really be tainted, but doing
+ that runs into a later copy into the untainted global message_id[] */
+ /*XXX Do we need a length limit check here? */
+ if (i == argc - 1)
+ (void)deliver_message(argv[i], forced_delivery, deliver_give_up);
+ else if ((pid = exim_fork(US"cmdline-delivery")) == 0)
+ {
+ (void)deliver_message(argv[i], forced_delivery, deliver_give_up);
+ exim_underbar_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 && !f.daemon_listen)
+ {
+ DEBUG(D_queue_run) debug_printf("Single queue run%s%s%s%s\n",
+ start_queue_run_id ? US" starting at " : US"",
+ start_queue_run_id ? start_queue_run_id: US"",
+ stop_queue_run_id ? US" stopping at " : US"",
+ stop_queue_run_id ? stop_queue_run_id : US"");
+ if (*queue_name)
+ set_process_info("running the '%s' queue (single queue run)", queue_name);
+ else
+ 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)
+ {
+ if (!sender_address || (!f.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)
+ {
+ int loffset;
+ string_format(buffer, sizeof(buffer), "%.*s%n%s%s",
+ (int)(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 && gecos_name)
+ {
+ const pcre2_code *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)
+ {
+ 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 || f.running_in_test_harness)
+ {
+ if (unknown_login)
+ {
+ originator_login = expand_string(unknown_login);
+ if (!originator_name && unknown_username)
+ originator_name = expand_string(unknown_username);
+ if (!originator_name) originator_name = US"";
+ }
+ if (!originator_login)
+ 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 = US parse_fix_phrase(originator_name, Ustrlen(originator_name));
+
+/* 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 (f.daemon_listen || f.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");
+ }
+
+# ifndef DISABLE_TLS
+ /* 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 */
+ {
+# ifdef MEASURE_TIMING
+ struct timeval t0;
+ (void)gettimeofday(&t0, NULL);
+# endif
+ if (!tls_dropprivs_validate_require_cipher(FALSE))
+ exit(1);
+# ifdef MEASURE_TIMING
+ report_time_since(&t0, US"validate_ciphers (delta)");
+# endif
+ }
+#endif
+
+ 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) sender_ident = originator_login;
+else if (!*sender_ident) 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)
+ {
+ f.really_exim = FALSE;
+ if (test_rewrite_arg >= argc)
+ {
+ printf("-brw needs an address argument\n");
+ exim_exit(EXIT_FAILURE);
+ }
+ rewrite_test(exim_str_fail_toolong(argv[test_rewrite_arg], EXIM_EMAILADDR_MAX, "-brw"));
+ 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 && !smtp_input
+ || !f.trusted_caller && filter_test == FTEST_NONE)
+ {
+ f.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 && !host_checking)
+ authenticated_sender = string_sprintf("%s@%s", originator_login,
+ qualify_domain_sender);
+ if (!authenticated_id && !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
+ || !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 /* No sender_address set */
+ || /* OR */
+ (sender_address[0] != 0 && /* Non-empty sender address, AND */
+ !checking)) /* Not running tests, including filter tests */
+ {
+ sender_address = originator_login;
+ f.sender_address_forced = FALSE;
+ sender_address_domain = 0;
+ }
+ }
+
+/* Remember whether an untrusted caller set the sender address */
+
+f.sender_set_untrusted = sender_address != originator_login && !f.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 && *sender_address && 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 || f.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)
+ {
+ /* Supplied addresses are tainted since they come from a user */
+ uschar * s = string_copy_taint(
+ exim_str_fail_toolong(argv[recipients_arg++], EXIM_DISPLAYMAIL_MAX, "address verification"),
+ GET_TAINTED);
+ while (*s)
+ {
+ 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 == ',' || isspace(*s)) ;
+ }
+ }
+
+ else for (;;)
+ {
+ uschar * s = get_stdinput(NULL, NULL);
+ if (!s) break;
+ test_address(string_copy_taint(
+ exim_str_fail_toolong(s, EXIM_DISPLAYMAIL_MAX, "address verification (stdin)"),
+ GET_TAINTED),
+ 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)
+ {
+ dns_init(FALSE, FALSE, FALSE);
+ if (msg_action_arg > 0 && msg_action == MSG_LOAD)
+ {
+ uschar * spoolname;
+ if (!f.admin_user)
+ exim_fail("exim: permission denied\n");
+ message_id = US exim_str_fail_toolong(argv[msg_action_arg], MESSAGE_ID_LENGTH, "message-id");
+ /* Checking the length of the ID is sufficient to validate it.
+ Get an untainted version so file opens can be done. */
+ message_id = string_copy_taint(message_id, GET_UNTAINTED);
+
+ spoolname = string_sprintf("%s-H", message_id);
+ if ((deliver_datafile = spool_open_datafile(message_id)) < 0)
+ 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)
+ {
+ int save_stdin = dup(0);
+ int fd = Uopen(expansion_test_message, O_RDONLY, 0);
+ if (fd < 0)
+ exim_fail("exim: failed to open %s: %s\n", expansion_test_message,
+ strerror(errno));
+ (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 */
+ }
+
+ /* Only admin users may see config-file macros this way */
+
+ if (!f.admin_user) macros_user = macros = mlast = NULL;
+
+ /* Allow $recipients for this testing */
+
+ f.enable_dollar_recipients = TRUE;
+
+ /* Expand command line items */
+
+ if (recipients_arg < argc)
+ while (recipients_arg < argc)
+ expansion_test_line(exim_str_fail_toolong(argv[recipients_arg++], EXIM_EMAILADDR_MAX, "recipient"));
+
+ /* Read stdin */
+
+ else
+ {
+ char *(*fn_readline)(const char *) = NULL;
+ void (*fn_addhist)(const char *) = NULL;
+ uschar * s;
+
+#ifdef USE_READLINE
+ void *dlhandle = set_readline(&fn_readline, &fn_addhist);
+#endif
+
+ while (s = get_stdinput(fn_readline, fn_addhist))
+ expansion_test_line(s);
+
+#ifdef USE_READLINE
+ if (dlhandle) 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 (!f.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 (f.running_in_test_harness && sender_host_port
+ && interface_address && interface_port)
+ verify_get_ident(1223); /* note hardwired port number */
+ }
+
+ /* In case the given address is a non-canonical IPv6 address, canonicalize
+ 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, GET_UNTAINTED); /* 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;
+ f.sender_local = FALSE;
+ f.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);
+
+ memset(sender_host_cache, 0, sizeof(sender_host_cache));
+ if (verify_check_host(&hosts_connection_nolog) == OK)
+ BIT_CLEAR(log_selector, log_selector_size, Li_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())
+ {
+ rmark reset_point;
+ for (; (reset_point = store_mark()); store_reset(reset_point))
+ {
+ if (smtp_setup_msg() <= 0) break;
+ if (!receive_msg(FALSE)) break;
+
+ return_path = sender_address = NULL;
+ dnslist_domain = dnslist_matched = NULL;
+#ifndef DISABLE_DKIM
+ dkim_cur_signer = NULL;
+#endif
+ acl_var_m = NULL;
+ deliver_localpart_orig = NULL;
+ deliver_domain_orig = NULL;
+ callout_address = sending_ip_address = NULL;
+ deliver_localpart_data = deliver_domain_data =
+ recipient_data = sender_data = NULL;
+ sender_rate = sender_rate_limit = sender_rate_period = NULL;
+ }
+ 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)
+ {
+ if (Ustrchr(config_main_filelist, ':'))
+ printf("Configuration file search path is %s\n", config_main_filelist);
+ 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)
+ {
+ f.synchronous_delivery = TRUE;
+ arg_error_handling = ERRORS_STDERR;
+ remote_max_parallel = 1;
+ deliver_drop_privilege = TRUE;
+ f.queue_smtp = FALSE;
+ queue_smtp_domains = NULL;
+#ifdef SUPPORT_I18N
+ message_utf8_downconvert = -1; /* convert-if-needed */
+#endif
+ }
+
+
+/* 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 (f.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 && !sender_fullhost)
+ {
+ host_build_sender_fullhost();
+ set_process_info("handling incoming connection from %s via -oMa",
+ sender_fullhost);
+ f.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 (!f.is_inetd) f.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 (!f.is_inetd) set_process_info("accepting a local %sSMTP message from <%s>",
+ smtp_batched_input? "batched " : "",
+ sender_address ? sender_address : originator_login);
+ }
+else
+ {
+ int old_pool = store_pool;
+ store_pool = POOL_PERM;
+ if (!received_protocol)
+ received_protocol = string_sprintf("local%s", called_as);
+ store_pool = old_pool;
+ 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))
+ exim_fail("exim: insufficient disk space\n");
+
+/* 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;
+ memset(sender_host_cache, 0, sizeof(sender_host_cache));
+ if (verify_check_host(&hosts_connection_nolog) == OK)
+ BIT_CLEAR(log_selector, log_selector_size, Li_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 and set no stdin stdio buffer
+(we handle buferring so as to have visibility of fill level). */
+
+else
+ {
+ thismessage_size_limit = expand_string_integer(message_size_limit, TRUE);
+ if (expand_string_message)
+ 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);
+
+ setvbuf(stdin, NULL, _IONBF, 0);
+ }
+
+/* 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 consequence of this, the waitpid() below is now excluded if we are sure
+that SIG_IGN works. */
+
+if (!f.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. */
+
+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). */
+
+for (BOOL more = TRUE; more; )
+ {
+ rmark reset_point = store_mark();
+ 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;
+ f.enable_dollar_recipients = TRUE;
+ (void)acl_check(ACL_WHERE_NOTSMTP_START, NULL, acl_not_smtp_start,
+ &user_msg, &log_msg);
+ f.enable_dollar_recipients = FALSE;
+ }
+
+ /* Now get the data for the message */
+
+ more = receive_msg(extract_recipients);
+ if (!message_id[0])
+ {
+ cancel_cutthrough_connection(TRUE, US"receive dropped");
+ if (more) goto MORELOOP;
+ smtp_log_no_mail(); /* Log no mail if configured */
+ exim_exit(EXIT_FAILURE);
+ }
+ }
+ else
+ {
+ cancel_cutthrough_connection(TRUE, US"message setup dropped");
+ smtp_log_no_mail(); /* Log no mail if configured */
+ exim_exit(rc ? EXIT_FAILURE : EXIT_SUCCESS);
+ }
+ }
+
+ /* 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 rcount = 0;
+ int count = argc - recipients_arg;
+ uschar **list = argv + recipients_arg;
+
+ /* These options cannot be changed dynamically for non-SMTP messages */
+
+ f.active_local_sender_retain = local_sender_retain;
+ f.active_local_from_check = local_from_check;
+
+ /* Save before any rewriting */
+
+ raw_sender = string_copy(sender_address);
+
+ /* Loop for each argument (supplied by user hence tainted) */
+
+ for (int i = 0; i < count; i++)
+ {
+ int start, end, domain;
+ uschar * errmess;
+ /* There can be multiple addresses, so EXIM_DISPLAYMAIL_MAX (tuned for 1) is too short.
+ * We'll still want to cap it to something, just in case. */
+ uschar * s = string_copy_taint(
+ exim_str_fail_toolong(list[i], BIG_BUFFER_SIZE, "address argument"),
+ GET_TAINTED);
+
+ /* Loop for each comma-separated address */
+
+ while (*s)
+ {
+ 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;
+
+#ifdef SUPPORT_I18N
+ {
+ BOOL b = allow_utf8_domains;
+ allow_utf8_domains = TRUE;
+#endif
+ recipient =
+ parse_extract_address(s, &errmess, &start, &end, &domain, FALSE);
+
+#ifdef SUPPORT_I18N
+ if (recipient)
+ if (string_is_utf8(recipient)) message_smtputf8 = TRUE;
+ else allow_utf8_domains = b;
+ }
+#else
+ ;
+#endif
+ if (domain == 0 && !f.allow_unqualified_recipient)
+ {
+ recipient = NULL;
+ errmess = US"unqualified recipient address not allowed";
+ }
+
+ if (!recipient)
+ 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(string_copy_taint(recipient, GET_TAINTED), -1);
+ s = ss;
+ if (!finished)
+ while (*(++s) != 0 && (*s == ',' || isspace(*s)));
+ }
+ }
+
+ /* Show the recipients when debugging */
+
+ DEBUG(D_receive)
+ {
+ if (sender_address) debug_printf("Sender: %s\n", sender_address);
+ if (recipients_list)
+ {
+ debug_printf("Recipients:\n");
+ for (int 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)
+ {
+ uschar *user_msg, *log_msg;
+ f.enable_dollar_recipients = TRUE;
+ (void)acl_check(ACL_WHERE_NOTSMTP_START, NULL, acl_not_smtp_start,
+ &user_msg, &log_msg);
+ f.enable_dollar_recipients = FALSE;
+ }
+
+ /* Pause for a while waiting for input. If none received in that time,
+ close the logfile, if we had one open; then if we wait for a long-running
+ datasource (months, in one use-case) log rotation will not leave us holding
+ the file copy. */
+
+ if (!receive_timeout)
+ if (poll_one_fd(0, POLLIN, 30*60*1000) == 0) /* 30 minutes */
+ mainlog_close();
+
+ /* 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]) 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 ? ftest_domain : qualify_domain_recipient;
+ deliver_domain_orig = deliver_domain;
+ deliver_localpart = ftest_localpart ? US ftest_localpart : originator_login;
+ deliver_localpart_orig = deliver_localpart;
+ deliver_localpart_prefix = US ftest_prefix;
+ deliver_localpart_suffix = US ftest_suffix;
+ deliver_home = originator_home;
+
+ if (!return_path)
+ {
+ 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 ? ftest_prefix : US"",
+ deliver_localpart,
+ ftest_suffix ? ftest_suffix : US"",
+ deliver_domain), -1);
+
+ printf("Recipient = %s\n", recipients_list[0].address);
+ if (ftest_prefix) printf("Prefix = %s\n", ftest_prefix);
+ if (ftest_suffix) 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)
+ 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)
+ 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. */
+
+ if (!(local_queue_only = session_local_queue_only) && queue_only_load >= 0)
+ if ((local_queue_only = (load_average = OS_GETLOADAVG()) > queue_only_load))
+ {
+ 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 = f.queue_only_policy = f.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)
+ {
+ cancel_cutthrough_connection(TRUE, US"no delivery; queueing");
+ 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 if (f.queue_only_policy || f.deliver_freeze)
+ cancel_cutthrough_connection(TRUE, US"no delivery; queueing");
+
+ /* 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
+ {
+ pid_t pid;
+ search_tidyup();
+
+ if ((pid = exim_fork(US"local-accept-delivery")) == 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)
+ {
+ delivery_re_exec(CEE_EXEC_EXIT);
+ /* Control does not return here. */
+ }
+
+ /* No need to re-exec */
+
+ rc = deliver_message(message_id, FALSE, FALSE);
+ search_tidyup();
+ exim_underbar_exit(!mua_wrapper || rc == DELIVER_MUA_SUCCEEDED
+ ? EXIT_SUCCESS : EXIT_FAILURE);
+ }
+
+ if (pid < 0)
+ {
+ cancel_cutthrough_connection(TRUE, US"delivery fork failed");
+ log_write(0, LOG_MAIN|LOG_PANIC, "failed to fork automatic delivery "
+ "process: %s", strerror(errno));
+ }
+ else
+ {
+ release_cutthrough_connection(US"msg passed for delivery");
+
+ /* In the parent, wait if synchronous delivery is required. This will
+ always be the case in MUA wrapper mode. */
+
+ if (f.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
+
+MORELOOP:
+ return_path = sender_address = NULL;
+ authenticated_sender = NULL;
+ deliver_localpart_orig = NULL;
+ deliver_domain_orig = NULL;
+ deliver_host = deliver_host_address = NULL;
+ dnslist_domain = dnslist_matched = NULL;
+#ifdef WITH_CONTENT_SCAN
+ malware_name = NULL;
+#endif
+ callout_address = NULL;
+ sending_ip_address = NULL;
+ deliver_localpart_data = deliver_domain_data =
+ recipient_data = sender_data = NULL;
+ acl_var_m = NULL;
+ for(int i = 0; i < REGEX_VARS; i++) regex_vars[i] = NULL;
+
+ store_reset(reset_point);
+ }
+
+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
index 0000000..61642b5
--- /dev/null
+++ b/src/exim.h
@@ -0,0 +1,671 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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. */
+
+#ifndef EXIM_H
+#define EXIM_H
+
+/* 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
+
+#ifdef EXIM_HAVE_INOTIFY
+# include <sys/inotify.h>
+#endif
+#ifdef EXIM_HAVE_KEVENT
+# include <sys/event.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
+
+/* RFC 5321 specifies that the maximum length of a local-part is 64 octets
+and the maximum length of a domain is 255 octets, but then also defines
+the maximum length of a forward/reverse path as 256 not 64+1+255.
+For an IP address, the maximum is 45 without a scope and we don't work
+with scoped addresses, so go with that. (IPv6 with mapped IPv4).
+
+A hostname maximum length is in practice the same as the domainname, for
+the same core reasons (maximum length of a DNS name), but the semantics
+are different and seeing "DOMAIN" in source is confusing when talking about
+hostnames; so we define a second macro. We'll use RFC 2181 as the reference
+for this one.
+
+There is no known (to me) specification on the maximum length of a human name
+in email addresses and we should be careful about imposing such a limit on
+received email, but in terms of limiting what untrusted callers specify, or
+local generation, having a limit makes sense. Err on the side of generosity.
+
+For a display mail address, we have a human name, an email in brackets,
+possibly some (Comments), so it needs to be at least 512+3 and some more to
+avoid extraneous errors.
+Since the sane SMTP line length limit is 998, constraining such parameters to
+be 1024 seems generous and unlikely to spuriously reject legitimate
+invocations.
+
+The driver name is a name of a router/transport/authenticator etc in the
+configuration file. We also use this for some other short strings, such
+as queue names.
+Also TLS ciphersuite name (no real known limit since the protocols use
+integers, but max seen in reality is 45 octets).
+
+RFC 1413 gives us the 512 limit on IDENT protocol userids.
+*/
+
+#define EXIM_EMAILADDR_MAX 256
+#define EXIM_LOCALPART_MAX 64
+#define EXIM_DOMAINNAME_MAX 255
+#define EXIM_IPADDR_MAX 45
+#define EXIM_HOSTNAME_MAX 255
+#define EXIM_HUMANNAME_MAX 256
+#define EXIM_DISPLAYMAIL_MAX 1024
+#define EXIM_DRIVERNAME_MAX 64
+#define EXIM_CIPHERNAME_MAX 64
+#define EXIM_IDENTUSER_MAX 512
+
+
+#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>
+
+
+/* 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 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 */
+
+#define PCRE2_CODE_UNIT_WIDTH 8
+#include <pcre2.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 "hintsdb.h"
+#include "hintsdb_structs.h"
+#include "structs.h"
+#include "blob.h"
+#include "hash.h"
+#include "globals.h"
+#include "functions.h"
+#include "dbfunctions.h"
+#include "osfunctions.h"
+
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+# include "bmi_spam.h"
+#endif
+#ifdef SUPPORT_SPF
+# include "spf.h"
+#endif
+#ifndef DISABLE_DKIM
+# include "dkim.h"
+#endif
+#ifdef SUPPORT_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 DISABLE_TLS is defined, ensure that USE_GNUTLS is not defined
+so that if USE_GNUTLS *is* set, we can assume DISABLE_TLS is not set.
+Ditto USE_OPENSSL.
+Likewise, OSCP, AUTH_TLS and CERTNAMES cannot be supported. */
+
+#ifdef DISABLE_TLS
+# undef USE_OPENSSL
+# undef USE_GNUTLS
+# ifndef DISABLE_OCSP
+# define DISABLE_OCSP
+# endif
+# undef EXPERIMENTAL_CERTNAMES
+# undef AUTH_TLS
+#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
+
+/* DANE w/o DNSSEC is useless */
+#if defined(SUPPORT_DANE) && defined(DISABLE_DNSSEC)
+# error DANE support requires DNSSEC support
+#endif
+
+/* Some platforms (FreeBSD, OpenBSD, Solaris) do not seem to define this */
+
+#ifndef POLLRDHUP
+# define POLLRDHUP (POLLIN | POLLHUP)
+#endif
+
+/* Some platforms (Darwin) have to define a larger limit on groups membership */
+
+#ifndef EXIM_GROUPLIST_SIZE
+# define EXIM_GROUPLIST_SIZE NGROUPS_MAX
+#endif
+
+/* Linux has TCP_CORK, FreeBSD has TCP_NOPUSH; they do pretty much the same */
+
+#ifdef TCP_CORK
+# define EXIM_TCP_CORK TCP_CORK
+#elif defined(TCP_NOPUSH)
+# define EXIM_TCP_CORK TCP_NOPUSH
+#endif
+
+/* LibreSSL seems to not push out the SMTP response to QUIT with our usual
+handling which is trying to get the client to FIN first so that the server does
+not get the TIME_WAIT */
+
+#if !defined(DISABLE_TLS) && defined(USE_OPENSSL) && defined(LIBRESSL_VERSION_NUMBER)
+# define SERVERSIDE_CLOSE_NOWAIT
+#endif
+
+#endif
+/* End of exim.h */
diff --git a/src/exim_checkaccess.src b/src/exim_checkaccess.src
new file mode 100755
index 0000000..360f307
--- /dev/null
+++ b/src/exim_checkaccess.src
@@ -0,0 +1,181 @@
+#! /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'
+
+BEGIN { pop @INC if $INC[-1] eq '.' };
+use FileHandle;
+use File::Basename;
+use IPC::Open2;
+
+if ($ARGV[0] eq '--version') {
+ print basename($0) . ": $0\n",
+ "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION\n",
+ "perl(runtime): $]\n";
+ exit 0;
+}
+
+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
index 0000000..5c04634
--- /dev/null
+++ b/src/exim_dbmbuild.c
@@ -0,0 +1,536 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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"
+
+uschar * spool_directory = NULL; /* dummy for hintsdb.h */
+
+/******************************************************************************/
+ /* dummies needed by Solaris build */
+void
+millisleep(int msec)
+{}
+uschar *
+readconf_printtime(int t)
+{ return NULL; }
+void *
+store_get_3(int size, const void * proto_mem, const char *filename, int linenumber)
+{ return NULL; }
+void **
+store_reset_3(void **ptr, const char *filename, int linenumber)
+{ return NULL; }
+void
+store_release_above_3(void *ptr, const char *func, int linenumber)
+{ }
+gstring *
+string_vformat_trc(gstring * g, const uschar * func, unsigned line,
+ unsigned size_limit, unsigned flags, const char *format, va_list ap)
+{ return NULL; }
+uschar *
+string_sprintf_trc(const char * a, const uschar * b, unsigned c, ...)
+{ return NULL; }
+BOOL
+string_format_trc(uschar * buf, int len, const uschar * func, unsigned line,
+ const char * fmt, ...)
+{ return FALSE; }
+void
+log_write(unsigned int selector, int flags, const char *format, ...)
+{ }
+
+
+struct global_flags f;
+unsigned int log_selector[1];
+uschar * queue_name;
+BOOL split_spool_directory;
+
+
+/* These introduced by the taintwarn handling */
+rmark
+store_mark_3(const char *func, int linenumber)
+{ return NULL; }
+#ifdef ALLOW_INSECURE_TAINTED_DATA
+BOOL allow_insecure_tainted_data;
+#endif
+
+/******************************************************************************/
+
+
+#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 */
+
+
+
+/*************************************************
+* 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(const uschar **pp)
+{
+int ch;
+const uschar *p = *pp;
+ch = *(++p);
+if (ch == '\0') return **pp;
+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 dirname[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 if (!(f = fopen(argv[arg], "rb")))
+ {
+ 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, US argv[arg+1]);
+Ustrcat(temp_dbmname, US".dbmbuild_temp");
+
+Ustrcpy(dirname, temp_dbmname);
+if ((bptr = Ustrrchr(dirname, '/')))
+ *bptr = '\0';
+else
+ Ustrcpy(dirname, US".");
+
+/* 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. */
+
+if (!(d = exim_dbopen(temp_dbmname, dirname, O_RDWR|O_CREAT|O_EXCL, 0644)))
+ {
+ 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_set(&content, buffer);
+ exim_datum_size_set(&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_set(&key, 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 != '\"')
+ {
+ *t++ = *s == '\\'
+ ? string_interpret_escape((const uschar **)&s)
+ : *s;
+ s++;
+ }
+ if (*s != 0) s++; /* Past terminating " */
+ exim_datum_size_set(&key, t - keystart + add_zero);
+ }
+ else
+ {
+ keystart = s;
+ while (*s != 0 && *s != ':' && !isspace(*s)) s++;
+ exim_datum_size_set(&key, s - keystart + add_zero);
+ }
+
+ if (exim_datum_size_get(&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_get(&key) - add_zero; i++)
+ keybuffer[i] = tolower(keystart[i]);
+ else
+ for (i = 0; i < exim_datum_size_get(&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_set(&content, buffer);
+ exim_datum_size_set(&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, US 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)
+ /* We created it, so safe to delete despite the name coming from outside */
+ /* coverity[tainted_string] */
+ 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
index 0000000..3824309
--- /dev/null
+++ b/src/exim_dbutil.c
@@ -0,0 +1,1423 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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:
+
+ callout: callout verification cache
+ misc: miscellaneous hints data
+ ratelimit: record for ACL "ratelimit" condition
+ retry: etry delivery information
+ seen: imestamp records for ACL "seen" condition
+ tls: TLS session resumption cache
+ wait-<t>: message waiting information; <t> is a transport name
+
+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
+#define type_tls 6
+#define type_seen 7
+
+
+/* This is used by our cut-down dbfn_open(). */
+
+uschar *spool_directory;
+
+BOOL keyonly = FALSE;
+BOOL utc = FALSE;
+
+
+/******************************************************************************/
+ /* dummies needed by Solaris build */
+void
+millisleep(int msec)
+{}
+uschar *
+readconf_printtime(int t)
+{ return NULL; }
+gstring *
+string_vformat_trc(gstring * g, const uschar * func, unsigned line,
+ unsigned size_limit, unsigned flags, const char *format, va_list ap)
+{ return NULL; }
+uschar *
+string_sprintf_trc(const char * fmt, const uschar * func, unsigned line, ...)
+{ return NULL; }
+BOOL
+string_format_trc(uschar * buf, int len, const uschar * func, unsigned line,
+ const char * fmt, ...)
+{ return FALSE; }
+
+struct global_flags f;
+unsigned int log_selector[1];
+uschar * queue_name;
+BOOL split_spool_directory;
+
+
+/* These introduced by the taintwarn handling */
+#ifdef ALLOW_INSECURE_TAINTED_DATA
+BOOL allow_insecure_tainted_data;
+#endif
+
+/******************************************************************************/
+
+
+/*************************************************
+* SIGALRM handler *
+*************************************************/
+
+SIGNAL_BOOL sigalrm_seen;
+
+void
+sigalrm_handler(int sig)
+{
+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 | tls | seen\n");
+exit(EXIT_FAILURE);
+}
+
+
+
+/*************************************************
+* 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)
+{
+uschar * aname = argv[optind + 1];
+if (argc - optind == 2)
+ {
+ if (Ustrcmp(aname, "retry") == 0) return type_retry;
+ if (Ustrcmp(aname, "misc") == 0) return type_misc;
+ if (Ustrncmp(aname, "wait-", 5) == 0) return type_wait;
+ if (Ustrcmp(aname, "callout") == 0) return type_callout;
+ if (Ustrcmp(aname, "ratelimit") == 0) return type_ratelimit;
+ if (Ustrcmp(aname, "tls") == 0) return type_tls;
+ if (Ustrcmp(aname, "seen") == 0) return type_seen;
+ }
+usage(name, options);
+return -1; /* Never obeyed */
+}
+
+
+FUNC_MAYBE_UNUSED
+static void
+options(int argc, uschar * argv[], uschar * name, const uschar * opts)
+{
+int opt;
+
+opterr = 0;
+while ((opt = getopt(argc, (char * const *)argv, CCS opts)) != -1)
+ switch (opt)
+ {
+ case 'k': keyonly = TRUE; break;
+ case 'z': utc = TRUE; break;
+ default: usage(name, US" [-z] [-k]");
+ }
+}
+
+
+
+
+/*************************************************
+* 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);
+}
+
+
+
+/*************************************************
+* 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 = utc ? gmtime(&t) : 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)
+{
+int field = 0;
+int value;
+time_t now = time(NULL);
+struct tm *tm = localtime(&now);
+
+tm->tm_sec = 0;
+tm->tm_isdst = -1;
+
+for (uschar * 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.
+ panic 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, BOOL panic)
+{
+int rc;
+struct flock lock_data;
+BOOL read_only = flags == O_RDONLY;
+uschar * dirname, * filename;
+
+/* 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. */
+
+#ifdef COMPILE_UTILITY
+if ( asprintf(CSS &dirname, "%s/db", spool_directory) < 0
+ || asprintf(CSS &filename, "%s/%s.lockfile", dirname, name) < 0)
+ return NULL;
+#else
+dirname = string_sprintf("%s/db", spool_directory);
+filename = string_sprintf("%s/%s.lockfile", dirname, name);
+#endif
+
+dbblock->lockfd = Uopen(filename, flags, 0);
+if (dbblock->lockfd < 0)
+ {
+ printf("** Failed to open database lock file %s: %s\n", filename,
+ 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_CLR(0);
+
+if (sigalrm_seen) errno = ETIMEDOUT;
+if (rc < 0)
+ {
+ printf("** Failed to get %s lock for %s: %s",
+ flags & O_WRONLY ? "write" : "read",
+ filename,
+ 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. */
+
+#ifdef COMPILE_UTILITY
+if (asprintf(CSS &filename, "%s/%s", dirname, name) < 0) return NULL;
+#else
+filename = string_sprintf("%s/%s", dirname, name);
+#endif
+dbblock->dbptr = exim_dbopen(filename, dirname, flags, 0);
+
+if (!dbblock->dbptr)
+ {
+ printf("** Failed to open DBM file %s for %s:\n %s%s\n", filename,
+ 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). Includes overhead.
+
+Returns: a pointer to the retrieved record, or
+ NULL if the record is not found
+*/
+
+void *
+dbfn_read_with_length(open_db *dbblock, const uschar *key, int *length)
+{
+void *yield;
+EXIM_DATUM key_datum, result_datum;
+int klen = Ustrlen(key) + 1;
+uschar * key_copy = store_get(klen, key);
+
+memcpy(key_copy, key, klen);
+
+exim_datum_init(&key_datum); /* Some DBM libraries require the datum */
+exim_datum_init(&result_datum); /* to be cleared before use. */
+exim_datum_data_set(&key_datum, key_copy);
+exim_datum_size_set(&key_datum, klen);
+
+if (!exim_dbget(dbblock->dbptr, &key_datum, &result_datum)) return NULL;
+
+/* Assume for now that anything stored could have been tainted. Properly
+we should store the taint status along with the data. */
+
+yield = store_get(exim_datum_size_get(&result_datum), GET_TAINTED);
+memcpy(yield, exim_datum_data_get(&result_datum), exim_datum_size_get(&result_datum));
+if (length) *length = exim_datum_size_get(&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, const uschar *key, void *ptr, int length)
+{
+EXIM_DATUM key_datum, value_datum;
+dbdata_generic *gptr = (dbdata_generic *)ptr;
+int klen = Ustrlen(key) + 1;
+uschar * key_copy = store_get(klen, key);
+
+memcpy(key_copy, key, klen);
+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_set(&key_datum, key_copy);
+exim_datum_size_set(&key_datum, klen);
+exim_datum_data_set(&value_datum, ptr);
+exim_datum_size_set(&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, const uschar *key)
+{
+int klen = Ustrlen(key) + 1;
+uschar * key_copy = store_get(klen, key);
+EXIM_DATUM key_datum;
+
+memcpy(key_copy, key, klen);
+exim_datum_init(&key_datum); /* Some DBM libraries require clearing */
+exim_datum_data_set(&key_datum, key_copy);
+exim_datum_size_set(&key_datum, klen);
+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;
+
+/* Some dbm require an initialization */
+
+if (start) *cursor = exim_dbcreate_cursor(dbblock->dbptr);
+
+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_get(&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 keybuffer[1024];
+
+store_init();
+options(argc, argv, US"dumpdb", US"kz");
+
+/* Check the arguments, and open the database */
+
+dbdata_type = check_args(argc, argv, US"dumpdb", US" [-z] [-k]");
+argc -= optind; argv += optind;
+spool_directory = argv[0];
+
+if (!(dbm = dbfn_open(argv[1], O_RDONLY, &dbblock, FALSE, TRUE)))
+ 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. */
+
+for (uschar * key = dbfn_scan(dbm, TRUE, &cursor);
+ key;
+ key = dbfn_scan(dbm, FALSE, &cursor))
+ {
+ dbdata_retry *retry;
+ dbdata_wait *wait;
+ dbdata_callout_cache *callout;
+ dbdata_ratelimit *ratelimit;
+ dbdata_ratelimit_unique *rate_unique;
+ dbdata_tls_session *session;
+ dbdata_seen *seen;
+ int count_bad = 0;
+ int length;
+ uschar *t;
+ uschar name[MESSAGE_ID_LENGTH + 1];
+ void *value;
+ rmark reset_point = store_mark();
+
+ /* 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);
+
+ if (keyonly)
+ printf(" %s\n", keybuffer);
+ else if (!(value = dbfn_read_with_length(dbm, keybuffer, &length)))
+ 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;
+
+ /* Leave corrupt records alone */
+ 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 (int 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)
+ {
+ fprintf(stderr,
+ "**** Data for %s corrupted: bad character in message id\n",
+ CS keybuffer);
+ for (int 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");
+ }
+
+ 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;
+
+ case type_tls:
+ session = (dbdata_tls_session *)value;
+ printf(" %s %.*s\n", keybuffer, length, session->session);
+ break;
+
+ case type_seen:
+ seen = (dbdata_seen *)value;
+ printf("%s\t%s\n", keybuffer, print_time(seen->time_stamp));
+ break;
+ }
+ store_reset(reset_point);
+ }
+
+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];
+rmark reset_point;
+uschar * aname;
+
+store_init();
+options(argc, argv, US"fixdb", US"z");
+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" [-z]");
+argc -= optind; argv += optind;
+spool_directory = argv[0];
+aname = argv[1];
+
+printf("Modifying Exim hints database %s/db/%s\n", spool_directory, aname);
+
+for(; (reset_point = store_mark()); store_reset(reset_point))
+ {
+ 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;
+ dbdata_tls_session *session;
+ int oldlength;
+ uschar *t;
+ uschar field[256], value[256];
+
+ 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;
+
+ if (!(dbm = dbfn_open(aname, O_RDWR, &dbblock, FALSE, TRUE)))
+ 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 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 (unsigned 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;
+
+ case type_tls:
+ printf("Can't change contents of tls database record\n");
+ 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. */
+
+ if (!(dbm = dbfn_open(aname, O_RDONLY, &dbblock, FALSE, TRUE)))
+ continue;
+
+ if (!(record = dbfn_read_with_length(dbm, name, &oldlength)))
+ {
+ 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 (int 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)
+ {
+ printf("\n**** Data corrupted: bad character in message id ****\n");
+ for (int 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;
+
+ case type_tls:
+ session = (dbdata_tls_session *)value;
+ printf("0 time stamp: %s\n", print_time(session->time_stamp));
+ printf("1 session: .%s\n", session->session);
+ 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;
+rmark reset_point;
+open_db dbblock;
+open_db *dbm;
+EXIM_CURSOR *cursor;
+uschar **argv = USS cargv;
+uschar buffer[256];
+uschar *key;
+
+store_init();
+
+/* 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];
+if (!(dbm = dbfn_open(argv[2], O_RDWR, &dbblock, FALSE, TRUE)))
+ 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. */
+
+for (key = dbfn_scan(dbm, TRUE, &cursor);
+ key;
+ key = dbfn_scan(dbm, FALSE, &cursor))
+ {
+ key_item * k = store_get(sizeof(key_item) + Ustrlen(key), key);
+ k->next = keychain;
+ keychain = k;
+ Ustrcpy(k->key, key);
+ }
+
+/* Now scan the collected keys and operate on the records, resetting
+the store each time round. */
+
+for (; keychain && (reset_point = store_mark()); store_reset(reset_point))
+ {
+ dbdata_generic *value;
+
+ 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) 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->time_stamp > time(NULL))
+ {
+ printf("**** Data for '%s' corrupted\n time in future: %s\n",
+ key, print_time(((dbdata_generic *)value)->time_stamp));
+ continue;
+ }
+ 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;
+ }
+ if (wait->sequence > WAIT_CONT_MAX)
+ {
+ printf("**** Data for '%s' corrupted\n sequence=%d=0x%x max=%d\n",
+ key, wait->sequence, wait->sequence, WAIT_CONT_MAX);
+ continue;
+ }
+
+ /* Record over 1 year old; just remove it */
+
+ if (wait->time_stamp < time(NULL) - 365*24*60*60)
+ {
+ dbfn_delete(dbm, key);
+ printf("deleted %s (too old)\n", key);
+ 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 length = wait->count * MESSAGE_ID_LENGTH;
+
+ for (int 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
index 0000000..427d22c
--- /dev/null
+++ b/src/exim_lock.c
@@ -0,0 +1,663 @@
+/* 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
+
+Copyright (c) The Exim Maintainers 2016 - 2021
+*/
+
+#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 unsigned 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)
+{
+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 = FALSE;
+
+/* 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, rc2;
+ 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)
+ rc2 = fstat(hd, &statbuf);
+ (void)close(hd);
+ unlink(hitchname);
+
+ if (rc != 0 && (rc2 != 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. */
+
+ if ((fd = open(filename, O_RDWR + O_APPEND)) < 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;
+#ifdef EXIM_HAVE_FUTIMENS
+ int fd = open(filename, O_RDWR); /* use fd for both get & restore */
+ struct timespec tt[2];
+
+ if (fd < 0)
+ {
+ printf("open '%s': %s\n", filename, strerror(errno));
+ yield = 1;
+ goto CLEAN_UP;
+ }
+ if (fstat(fd, &strestore) != 0)
+ {
+ printf("fstat '%s': %s\n", filename, strerror(errno));
+ yield = 1;
+ close(fd);
+ goto CLEAN_UP;
+ }
+ i = system(command);
+ tt[0] = strestore.st_atim;
+ tt[1] = strestore.st_mtim;
+ (void) futimens(fd, tt);
+ (void) close(fd);
+#else
+ struct utimbuf ut;
+
+ stat(filename, &strestore);
+ i = system(command);
+ ut.actime = strestore.st_atime;
+ ut.modtime = strestore.st_mtime;
+ utime(filename, &ut);
+#endif
+ }
+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
index 0000000..6293a7c
--- /dev/null
+++ b/src/eximon.src
@@ -0,0 +1,221 @@
+# 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 - 2015 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
+#
+if test "x$1" = x--version
+then
+ echo "`basename $0`: $0"
+ echo "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION"
+ exit 0
+fi
+
+# 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. If log_file_path is unset (i.e. empty) the default
+# is "mainlog" in the "log" directory in the spool directory. 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, check whether
+# LOG_FILE_NAME was set a compile time and contains a path. Otherwise fall
+# back to the default 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 \*\*\*
+elif [ "$LOG_FILE_PATH" = "" ] ; then
+ LOG_FILE_NAME=$SPOOL_DIRECTORY/log/mainlog
+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
+ COMPILETIMEDEFAULT=`$EXIM_PATH -C /dev/null -bP log_file_path | \
+ sed -e 's/.*=[ ]*//' \
+ -e 's/ *: *syslog *: */:/' \
+ -e 's/ *: *syslog *$//' \
+ -e 's/^ *syslog *: *//' \
+ -e 's/%s/main/'`
+ if [ "$COMPILETIMEDEFAULT" != "" ] ; then
+ LOG_FILE_NAME="$COMPILETIMEDEFAULT"
+ else
+ LOG_FILE_NAME=$SPOOL_DIRECTORY/log/mainlog
+ fi
+ 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
index 0000000..5e1a084
--- /dev/null
+++ b/src/eximstats.src
@@ -0,0 +1,4246 @@
+#!PERL_COMMAND
+
+# Copyright (c) 2001-2017 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 conversion.
+#
+# 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 caching 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 evaluation 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 outputting 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 website at https://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 warnings;
+use integer;
+BEGIN { pop @INC if $INC[-1] eq '.' };
+use strict;
+use IO::File;
+use File::Basename;
+
+# use Time::Local; # PH/FANF
+use POSIX;
+
+if (@ARGV and $ARGV[0] eq '--version') {
+ print basename($0) . ": $0\n",
+ "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION\n",
+ "perl(runtime): $]\n";
+ exit 0;
+}
+
+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 conversion done.
+use vars qw($last_date $date_seconds); #The last date conversion done.
+use vars qw($last_offset $offset_seconds); #The last time offset conversion 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 Spreadsheet::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 formatted output which on one hand avoids numbers from
+ # being interpreted as string by Spreadsheet 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($formatted_time) = pop @_;
+ my $time = 0;
+
+ while ($formatted_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
+# seconds value can be followed by decimals, which we ignore. 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\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 caching. Also note we want seconds not minutes.
+ #my($this_offset) = ($10 * 60 + $12) * ($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 $8) {
+ #$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 guaranteed 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 # 1: YYYYMMDD HHMMSS
+ (\\d\\d) # 2: HH
+ :
+ (\\d\\d) # 3: MM
+ :\\d\\d
+ )
+ (\\.\\d+)? # 4: subseconds
+ (\s[-+]\\d\\d\\d\\d)? # 5: tz-offset
+ (\s\\[\\d+\\])? # 6: pid
+ /ox;
+
+ $tod = defined($5) ? $1 . $5 : $1;
+ ($m_hour,$m_min) = ($2,$3);
+
+ # PH - watch for GMT offsets in the timestamp.
+ if (defined($5)) {
+ $extra = 6;
+ next if ($length < 44);
+ }
+ else {
+ $extra = 0;
+ }
+
+ # watch for subsecond precision
+ if (defined($4)) {
+ $extra += length($4);
+ next if ($length < 38 + $extra);
+ }
+
+ # PH - watch for PID added after the timestamp.
+ if (defined($6)) {
+ $extra += length($6);
+ 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 distinct 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,$formatted_time,$count) = ($1,$2,$3);
+ my $seconds = unformat_time($formatted_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 separated 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 separated 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 essential 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 separated 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
index 0000000..9138018
--- /dev/null
+++ b/src/exinext.src
@@ -0,0 +1,262 @@
+#! /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 test "x$1" = x--version
+then
+ echo "`basename $0`: $0"
+ echo "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION"
+ exit 0
+fi
+
+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'
+
+ # We don't import anything, but guard against future changes which do
+ BEGIN { pop @INC if $INC[-1] eq '.' };
+
+ # 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
index 0000000..a631333
--- /dev/null
+++ b/src/exipick.src
@@ -0,0 +1,1841 @@
+#!PERL_COMMAND
+# Copyright (c) 1995 - 2018 University of Cambridge.
+# See the file NOTICE for conditions of use and distribution.
+
+
+# This variables should be set by the building process
+my $spool = 'SPOOL_DIRECTORY'; # may be overridden later
+my $exim = 'BIN_DIRECTORY/exim';
+
+# 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;
+BEGIN { pop @INC if $INC[-1] eq '.' };
+use Getopt::Long;
+use File::Basename;
+use Pod::Usage;
+
+my $p_name = basename $0;
+my $p_version = "20100323.0";
+my $p_usage = "Usage: $p_name [--help|--man|--version] (see --help for details)";
+my $p_cp = <<EOM;
+ Copyright (c) 2003-2010 John Jetmore <jj33\@pobox.com>
+ Copyright (c) 2019 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.
+
+ 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
+
+$| = 1; # unbuffer STDOUT
+
+Getopt::Long::Configure("bundling_override");
+GetOptions(
+ 'spool=s' => \$G::spool, # exim spool dir
+ 'C|Config=s' => \$G::config, # use alternative Exim configuration file
+ 'input-dir=s' => \$G::input_dir, # name of the "input" dir
+ 'queue=s' => \$G::queue, # name of the queue
+ '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
+ 'man' => sub { pod2usage(-verbose => 2, -exit => 0, -noperldoc => system('perldoc -V >/dev/null 2>&1')) },
+ 'help' => sub { pod2usage(-verbose => 1, -exit => 0) },
+ 'version' => sub {
+ print "$p_name: $0\n",
+ "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION\n",
+ "perl(runtime): $]\n";
+ exit 0;
+ },
+) or pod2usage;
+
+# if both freeze and thaw specified, only thaw as it is less destructive
+$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 = defined $G::spool ? $G::spool
+ : do { chomp($_ = `$exim @{[defined $G::config ? "-C $G::config" : '']} -n -bP spool_directory`)
+ and $_ or $spool };
+my $input_dir = (defined $G::queue ? "$G::queue/" : '')
+ . (defined $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_* pseudo 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/) { # whitespace 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};
+
+ TAGGED: while (<I>) {
+ my ($tag, $arg) = /^-?(-\S+)(?:\s+(.*))?$/ or last TAGGED;
+ chomp;
+
+ 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;
+ }
+ }
+
+ # when we drop out of the while loop, we have the first line of the
+ # delivered tree in $_
+ do {
+ chomp;
+ if ($_ eq 'XX') {
+ ; # noop
+ } elsif ($_ =~ s/^[YN][YN]\s+//) {
+ $self->{_del_tree}{$_} = 1;
+ } else {
+ return(0);
+ }
+ $_ = <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
+
+__END__
+
+=head1 NAME
+
+ exipick - selectively display messages from an Exim queue
+
+=head1 SYNOPSIS
+
+ exipick [<options>] [<criterion> [<criterion> ...]]
+ exipick --help|--man
+
+=head1 DESCRIPTION
+
+B<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. B<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 B<--and>
+
+Display messages matching all criteria (default)
+
+=item B<-b>
+
+Display messages in brief format (exiqgrep)
+
+=item B<-bp> | B<-l>
+
+Display messages in standard mailq format (default).
+(exiqgrep: C<-l>)
+
+=item B<-bpa>
+
+Same as C<-bp>, show generated addresses also (exim)
+
+=item B<-bpc>
+
+Show a count of matching messages (exim)
+
+=item B<-bpr>
+
+Same as C<-bp --unsorted> (exim)
+
+=item B<-bpra>
+
+Same as C<-bpa --unsorted> (exim)
+
+=item B<-bpru>
+
+Same as C<-bpu --unsorted> (exim)
+
+=item B<-bpu>
+
+Same as C<-bp>, but only show undelivered messages (exim)
+
+=item B<-C> | B<--config> I<config>
+
+Use I<config> to determine the proper spool directory. (See C<--spool>
+or C<--input> for alternative ways to specify the directories to operate on.)
+
+=item B<-c>
+
+Show a count of matching messages (exiqgrep)
+
+=item B<--caseful>
+
+Make operators involving C<=> honor case
+
+=item B<--charset>
+
+Override the default local character set for C<$header_> decoding
+
+=item B<-f> I<regexp>
+
+Same as C<< $sender_address =~ /<regexp>/ >> (exiqgrep). Note that this preserves the default case sensitivity of exiqgrep's interface.
+
+=item B<--finput>
+
+Same as C<--input-dir Finput>. F<Finput> is where exim copies frozen messages when compiled with SUPPORT_MOVE_FROZEN_MESSAGES.
+
+=item B<--flatq>
+
+Use a single-line output format
+
+=item B<--freeze> I<cache file>
+
+Save queue information in an quickly retrievable format
+
+=item B<--help>
+
+Display this output
+
+=item B<-i>
+
+Display only the message IDs (exiqgrep)
+
+=item B<--input-dir> I<inputname>
+
+Set the name of the directory under the spool directory. By default this is F<input>. If this starts with F</>,
+the value of C<--spool> is ignored. See also C<--finput>.
+
+=item B<--not>
+
+Negate all tests.
+
+=item B<-o> I<seconds>
+
+Same as C<< $message_age > <seconds> >> (exiqgrep)
+
+=item B<--or>
+
+Display messages matching any criteria
+
+=item B<--queue> I<name>
+
+Name of the queue (default: ''). See "named queues" in the spec.
+
+=item B<-r> I<regexp>
+
+Same as C<< $recipients =~ /<regexp>/ >> (exiqgrep). Note that this preserves the default case sensitivity of exiqgrep's interface.
+
+=item B<--random>
+
+Display messages in random order
+
+=item B<--reverse> | B<-R>
+
+Display messages in reverse order (exiqgrep: C<-R>)
+
+=item B<-s> I<string>
+
+Same as C<< $shown_message_size eq <string> >> (exiqgrep)
+
+=item B<--spool> I<path>
+
+Set the path to the exim spool to use. This value will have the arguments to C<--queue>, and C<--input> or F<input> appended, or be ignored if C<--input> is a full path. If not specified, B<exipick> uses the value from C<exim [-C config] -n -bP spool_directory>, and if this call fails, the F</opt/exim/spool> from build time (F<Local/Makefile>) is used. See also C<--config>.
+
+=item B<--show-rules>
+
+Show the internal representation of each criterion specified
+
+=item B<--show-tests>
+
+Show the result of each criterion on each message
+
+=item B<--show-vars> I<variable>[,I<variable>...]
+
+Show the value for I<variable> for each displayed message. I<variable> will be a regular expression if it begins with a circumflex.
+
+=item B<--size>
+
+Show the total bytes used by each displayed message
+
+=item B<--thaw> I<cache file>
+
+Read queue information cached from a previous C<--freeze> run
+
+=item B<--sort> I<variable>[,I<variable>...]
+
+Display matching messages sorted according to I<variable>
+
+=item B<--unsorted>
+
+Do not apply any sorting to output
+
+=item B<--version>
+
+Display the version of this command
+
+=item B<-x>
+
+Same as C<!$deliver_freeze> (exiqgrep)
+
+=item B<-y>
+
+Same as C<< $message_age < <seconds> >> (exiqgrep)
+
+=item B<-z>
+
+Same as C<$deliver_freeze> (exiqgrep)
+
+=back
+
+=head1 CRITERIA
+
+B<Exipick> decides which messages to display by applying a test against each message. The rules take the general form of "I<VARIABLE> I<OPERATOR> I<VALUE>". For example, C<< $message_age > 60 >>. When B<exipick> is deciding which messages to display, it checks the C<$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 C<--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 C<< = >>, 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 C<< = >> 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, C<< !$acl_m1 =~ /^DENY$/ >> is the same as C<< $acl_m1 !~ /^DENY$/ >>. Finally, every test can be specified by using the command line argument C<--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 C<< $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 . B<$acl_c0>-B<$acl_c9>, B<$acl_m0>-B<$acl_m9>
+
+User definable variables.
+
+=item B + B<$allow_unqualified_recipient>
+
+TRUE if unqualified recipient addresses are permitted in header lines.
+
+=item B + B<$allow_unqualified_sender>
+
+TRUE if unqualified sender addresses are permitted in header lines.
+
+=item S . B<$authenticated_id>
+
+Optional saved information from authenticators, or the login name of the calling process for locally submitted messages.
+
+=item S . B<$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 . B<$bheader_*>, B<$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 + B<$bmi_verdicts>
+
+The verdict string provided by a Brightmail content scan
+
+=item N . B<$body_linecount>
+
+The number of lines in the message's body.
+
+=item N . B<$body_zerocount>
+
+The number of binary zero bytes in the message's body.
+
+=item S + B<$data_path>
+
+The path to the body file's location in the filesystem.
+
+=item B + B<$deliver_freeze>
+
+TRUE if the message is currently frozen.
+
+=item N + B<$deliver_frozen_at>
+
+The epoch time at which message was frozen.
+
+=item B + B<$dont_deliver>
+
+TRUE if, under normal circumstances, Exim will not try to deliver the message.
+
+=item S + B<$each_recipients>
+
+This is a pseudo variable which allows you to apply a test against each address in $recipients individually. Whereas C<< $recipients =~ /@aol.com/ >> will match if any recipient address contains aol.com, C<< $each_recipients =~ /@aol.com$/ >> will only be true if every recipient matches that pattern. Note that this obeys C<--and> or C<--or> being set. Using it with C<--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 + B<$each_recipients_del>
+
+Like $each_recipients, but for $recipients_del
+
+=item S + B<$each_recipients_undel>
+
+Like $each_recipients, but for $recipients_undel
+
+=item B . B<$first_delivery>
+
+TRUE if the message has never been deferred.
+
+=item S . B<$header_*>, B<$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 + B<$header_path>
+
+The path to the header file's location in the filesystem.
+
+=item B . 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 . 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 + B<$local_error_message>
+
+TRUE if the message is a locally-generated error message.
+
+=item S . B<$local_scan_data>
+
+The text returned by the local_scan() function when a message is received.
+
+=item B . B<$manually_thawed>
+
+TRUE when the message has been manually thawed.
+
+=item N . B<$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 . B<$message_age>
+
+The number of seconds since the message was received.
+
+=item S # B<$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 + B<$message_body_missing>
+
+TRUE is a message's spool data file (-D file) is missing or unreadable.
+
+=item N . B<$message_body_size>
+
+The size of the body in bytes.
+
+=item S . B<$message_exim_id>, B<$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 . B<$message_headers>
+
+A concatenation of all the header lines except for lines added by routers or transports. RFC2047 decoding is performed
+
+=item S . B<$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 . B<$message_linecount>
+
+The number of lines in the entire message (body and headers).
+
+=item N . B<$message_size>
+
+The size of the message in bytes.
+
+=item N . B<$originator_gid>
+
+The group id under which the process that called Exim was running as when the message was received.
+
+=item S + B<$originator_login>
+
+The login of the process which called Exim.
+
+=item N . B<$originator_uid>
+
+The user id under which the process that called Exim was running as when the message was received.
+
+=item S . B<$received_ip_address>, B<$interface_address>
+
+The address of the local IP interface for network-originated messages. $interface_address is deprecated as of Exim 4.64
+
+=item N . B<$received_port>, B<$interface_port>
+
+The local port number if network-originated messages. $interface_port is deprecated as of Exim 4.64
+
+=item N . B<$received_count>
+
+The number of Received: header lines in the message.
+
+=item S . B<$received_protocol>
+
+The name of the protocol by which the message was received.
+
+=item N . B<$received_time>
+
+The epoch time at which the message was received.
+
+=item S # B<$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 . B<$recipients_count>
+
+The number of envelope recipients for the message.
+
+=item S + B<$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 + B<$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 + B<$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 + B<$recipients_undel_count>
+
+The number of envelope recipients for the message which have not yet been delivered.
+
+=item S . B<$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 . B<$rheader_*>, B<$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 . B<$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 . B<$sender_address_domain>
+
+The domain part of $sender_address.
+
+=item S . B<$sender_address_local_part>
+
+The local part of $sender_address.
+
+=item S . B<$sender_helo_name>
+
+The HELO or EHLO value supplied for smtp or bsmtp messages.
+
+=item S . B<$sender_host_address>
+
+The remote host's IP address.
+
+=item S . B<$sender_host_authenticated>
+
+The name of the authenticator driver which successfully authenticated the client from which the message was received.
+
+=item S . B<$sender_host_name>
+
+The remote host's name as obtained by looking up its IP address.
+
+=item N . B<$sender_host_port>
+
+The port number that was used on the remote host for network-originated messages.
+
+=item S . B<$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 + B<$sender_local>
+
+TRUE if the message was locally generated.
+
+=item B + B<$sender_set_untrusted>
+
+TRUE if the envelope sender of this message was set by an untrusted local caller.
+
+=item S + B<$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 . B<$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 . B<$spam_score>
+
+The spam score of the message, for example '3.4' or '30.5'. (Requires exiscan or WITH_CONTENT_SCAN)
+
+=item S . B<$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 . B<$tls_certificate_verified>
+
+TRUE if a TLS certificate was verified when the message was received.
+
+=item S . B<$tls_cipher>
+
+The cipher suite that was negotiated for encrypted SMTP connections.
+
+=item S . B<$tls_peerdn>
+
+The value of the Distinguished Name of the certificate if Exim is configured to request one
+
+=item S . B<$tls_sni>
+
+The value of the Server Name Indication TLS extension sent by a client, if one was sent.
+
+=item N + B<$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: L<https://jetmore.org/john/code/#exipick>
+
+This script was incorporated into the main Exim distribution some years ago.
+
+=back
+
+=cut
+
+# vim:ft=perl
diff --git a/src/exiqgrep.src b/src/exiqgrep.src
new file mode 100644
index 0000000..0661c57
--- /dev/null
+++ b/src/exiqgrep.src
@@ -0,0 +1,216 @@
+#!PERL_COMMAND
+
+# Utility for searching and displaying queue information.
+# Written by Matt Hubbard 15 August 2002
+#
+# Copyright (c) The Exim Maintainers 2021 - 2022
+
+# 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;
+BEGIN { pop @INC if $INC[-1] eq '.' };
+
+use Getopt::Std;
+use File::Basename;
+
+# 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;
+};
+
+if ($ARGV[0] eq '--version') {
+ print basename($0) . ": $0\n",
+ "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION\n",
+ "perl(runtime): $]\n";
+ exit 0;
+}
+
+if (!getopts('hf:r:y:o:s:C:zxlibRcaG:E:',\%opt)) { &help; exit; }
+if ($opt{h}) { &help; exit; }
+if ($ARGV[0] || !($opt{f} || $opt{r} || $opt{s} || $opt{y} || $opt{o} || $opt{z} || $opt{x} || $opt{c}))
+ { &help; exit(1); }
+if ($opt{a}) { $eargs = '-bp'; }
+if ($opt{C} && -e $opt{C} && -f $opt{C} && -R $opt{C}) { $eargs .= ' -C '.$opt{C}; }
+if ($opt{G}) { $eargs .= ' -qG'.$opt{G}; }
+if ($opt{E}) { $exim = $opt{E}; }
+
+# 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.
+ -E Specify exim binary 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)
+ -G <queuename> Match in given queue only
+
+[ 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 opening 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
index 0000000..67772f5
--- /dev/null
+++ b/src/exiqsumm.src
@@ -0,0 +1,178 @@
+#! PERL_COMMAND
+
+# 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
+# Added 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
+
+use warnings;
+BEGIN { pop @INC if $INC[-1] eq '.' };
+use File::Basename;
+
+if (@ARGV && $ARGV[0] eq '--version') {
+ print basename($0) . ": $0\n",
+ "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION\n",
+ "perl(runtime): $]\n";
+ exit 0;
+}
+
+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
index 0000000..a1f748e
--- /dev/null
+++ b/src/exiwhat.src
@@ -0,0 +1,145 @@
+#! /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
+# RM_COMMAND
+
+# 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.
+
+rm=RM_COMMAND
+
+# 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 test "x$1" = x--version
+then
+ echo "`basename $0`: $0"
+ echo "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION"
+ exit 0
+fi
+
+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.
+
+$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
index 0000000..36c9f42
--- /dev/null
+++ b/src/expand.c
@@ -0,0 +1,8837 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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(const uschar *, BOOL, const uschar **, BOOL, BOOL, BOOL *);
+static int_eximarith_t expanded_string_integer(const 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. Characters 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>
+*/
+
+
+
+/*************************************************
+* 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"authresults",
+ US"certextract",
+ US"dlfunc",
+ US"env",
+ US"extract",
+ US"filter",
+ US"hash",
+ US"hmac",
+ US"if",
+#ifdef SUPPORT_I18N
+ US"imapfolder",
+#endif
+ US"length",
+ US"listextract",
+ US"listquote",
+ US"lookup",
+ US"map",
+ US"nhash",
+ US"perl",
+ US"prvs",
+ US"prvscheck",
+ US"readfile",
+ US"readsocket",
+ US"reduce",
+ US"run",
+ US"sg",
+ US"sort",
+#ifdef SUPPORT_SRS
+ US"srs_encode",
+#endif
+ US"substr",
+ US"tr" };
+
+enum {
+ EITEM_ACL,
+ EITEM_AUTHRESULTS,
+ EITEM_CERTEXTRACT,
+ EITEM_DLFUNC,
+ EITEM_ENV,
+ EITEM_EXTRACT,
+ EITEM_FILTER,
+ EITEM_HASH,
+ EITEM_HMAC,
+ EITEM_IF,
+#ifdef SUPPORT_I18N
+ EITEM_IMAPFOLDER,
+#endif
+ EITEM_LENGTH,
+ EITEM_LISTEXTRACT,
+ EITEM_LISTQUOTE,
+ EITEM_LOOKUP,
+ EITEM_MAP,
+ EITEM_NHASH,
+ EITEM_PERL,
+ EITEM_PRVS,
+ EITEM_PRVSCHECK,
+ EITEM_READFILE,
+ EITEM_READSOCK,
+ EITEM_REDUCE,
+ EITEM_RUN,
+ EITEM_SG,
+ EITEM_SORT,
+#ifdef SUPPORT_SRS
+ EITEM_SRS_ENCODE,
+#endif
+ 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"
+#ifdef SUPPORT_I18N
+ ,US"utf8_domain_from_alabel",
+ US"utf8_domain_to_alabel",
+ US"utf8_localpart_from_alabel",
+ US"utf8_localpart_to_alabel"
+#endif
+ };
+
+enum {
+ EOP_FROM_UTF8,
+ EOP_LOCAL_PART,
+ EOP_QUOTE_LOCAL_PART,
+ EOP_REVERSE_IP,
+ EOP_TIME_EVAL,
+ EOP_TIME_INTERVAL
+#ifdef SUPPORT_I18N
+ ,EOP_UTF8_DOMAIN_FROM_ALABEL,
+ EOP_UTF8_DOMAIN_TO_ALABEL,
+ EOP_UTF8_LOCALPART_FROM_ALABEL,
+ EOP_UTF8_LOCALPART_TO_ALABEL
+#endif
+ };
+
+static uschar *op_table_main[] = {
+ US"address",
+ US"addresses",
+ US"base32",
+ US"base32d",
+ US"base62",
+ US"base62d",
+ US"base64",
+ US"base64d",
+ US"domain",
+ US"escape",
+ US"escape8bit",
+ US"eval",
+ US"eval10",
+ US"expand",
+ US"h",
+ US"hash",
+ US"hex2b64",
+ US"hexquote",
+ US"ipv6denorm",
+ US"ipv6norm",
+ 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"sha2",
+ US"sha256",
+ US"sha3",
+ US"stat",
+ US"str2b64",
+ US"strlen",
+ US"substr",
+ US"uc",
+ US"utf8clean" };
+
+enum {
+ EOP_ADDRESS = nelem(op_table_underscore),
+ EOP_ADDRESSES,
+ EOP_BASE32,
+ EOP_BASE32D,
+ EOP_BASE62,
+ EOP_BASE62D,
+ EOP_BASE64,
+ EOP_BASE64D,
+ EOP_DOMAIN,
+ EOP_ESCAPE,
+ EOP_ESCAPE8BIT,
+ EOP_EVAL,
+ EOP_EVAL10,
+ EOP_EXPAND,
+ EOP_H,
+ EOP_HASH,
+ EOP_HEX2B64,
+ EOP_HEXQUOTE,
+ EOP_IPV6DENORM,
+ EOP_IPV6NORM,
+ 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_SHA2,
+ EOP_SHA256,
+ EOP_SHA3,
+ 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"forall_json",
+ US"forall_jsons",
+ US"forany",
+ US"forany_json",
+ US"forany_jsons",
+ US"ge",
+ US"gei",
+ US"gt",
+ US"gti",
+#ifdef SUPPORT_SRS
+ US"inbound_srs",
+#endif
+ 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_FORALL_JSON,
+ ECOND_FORALL_JSONS,
+ ECOND_FORANY,
+ ECOND_FORANY_JSON,
+ ECOND_FORANY_JSONS,
+ ECOND_STR_GE,
+ ECOND_STR_GEI,
+ ECOND_STR_GT,
+ ECOND_STR_GTI,
+#ifdef SUPPORT_SRS
+ ECOND_INBOUND_SRS,
+#endif
+ 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);
+typedef uschar * stringptr_fn_t(void);
+static uschar * fn_queue_size(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 },
+#ifdef EXPERIMENTAL_ARC
+ { "arc_domains", vtype_string_func, (void *) &fn_arc_domains },
+ { "arc_oldest_pass", vtype_int, &arc_oldest_pass },
+ { "arc_state", vtype_stringptr, &arc_state },
+ { "arc_state_reason", vtype_stringptr, &arc_state_reason },
+#endif
+ { "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 },
+ { "callout_address", vtype_stringptr, &callout_address },
+ { "compile_date", vtype_stringptr, &version_date },
+ { "compile_number", vtype_stringptr, &version_cnumber },
+ { "config_dir", vtype_stringptr, &config_main_directory },
+ { "config_file", vtype_stringptr, &config_main_filename },
+ { "csa_status", vtype_stringptr, &csa_status },
+#ifdef EXPERIMENTAL_DCC
+ { "dcc_header", vtype_stringptr, &dcc_header },
+ { "dcc_result", vtype_stringptr, &dcc_result },
+#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_length", vtype_int, &dkim_key_length },
+ { "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_stringptr, &dkim_verify_reason },
+ { "dkim_verify_status", vtype_stringptr, &dkim_verify_status },
+#endif
+#ifdef SUPPORT_DMARC
+ { "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 },
+#ifndef DISABLE_EVENT
+ { "event_data", vtype_stringptr, &event_data },
+
+ /*XXX want to use generic vars for as many of these as possible*/
+ { "event_defer_errno", vtype_int, &event_defer_errno },
+
+ { "event_name", vtype_stringptr, &event_name },
+#endif
+ { "exim_gid", vtype_gid, &exim_gid },
+ { "exim_path", vtype_stringptr, &exim_path },
+ { "exim_uid", vtype_uid, &exim_uid },
+ { "exim_version", vtype_stringptr, &version_string },
+ { "headers_added", vtype_string_func, (void *) &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 },
+ { "host_port", vtype_int, &deliver_host_port },
+ { "initial_cwd", vtype_stringptr, &initial_cwd },
+ { "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_prefix_v", vtype_stringptr, &deliver_localpart_prefix_v },
+ { "local_part_suffix", vtype_stringptr, &deliver_localpart_suffix },
+ { "local_part_suffix_v", vtype_stringptr, &deliver_localpart_suffix_v },
+#ifdef HAVE_LOCAL_SCAN
+ { "local_scan_data", vtype_stringptr, &local_scan_data },
+#endif
+ { "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 SUPPORT_I18N
+ { "message_smtputf8", vtype_bool, &message_smtputf8 },
+#endif
+#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 },
+#ifndef DISABLE_PRDR
+ { "prdr_requested", vtype_bool, &prdr_requested },
+#endif
+ { "primary_hostname", vtype_stringptr, &primary_hostname },
+#if defined(SUPPORT_PROXY) || defined(SUPPORT_SOCKS)
+ { "proxy_external_address",vtype_stringptr, &proxy_external_address },
+ { "proxy_external_port", vtype_int, &proxy_external_port },
+ { "proxy_local_address", vtype_stringptr, &proxy_local_address },
+ { "proxy_local_port", vtype_int, &proxy_local_port },
+ { "proxy_session", vtype_bool, &proxy_session },
+#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 },
+ { "queue_name", vtype_stringptr, &queue_name },
+ { "queue_size", vtype_string_func, &fn_queue_size },
+ { "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.tv_sec },
+ { "recipient_data", vtype_stringptr, &recipient_data },
+ { "recipient_verify_failure",vtype_stringptr,&recipient_verify_failure },
+ { "recipients", vtype_string_func, (void *) &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_dnssec", vtype_bool, &sender_helo_dnssec },
+ { "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_command_history", vtype_string_func, (void *) &smtp_cmd_hist },
+ { "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_action", vtype_stringptr, &spam_action },
+ { "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 SUPPORT_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_result_guessed", vtype_bool, &spf_result_guessed },
+ { "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 SUPPORT_SRS
+ { "srs_recipient", vtype_stringptr, &srs_recipient },
+#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_cipher_std", vtype_stringptr, &tls_in.cipher_stdname },
+ { "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 },
+#ifndef DISABLE_TLS_RESUME
+ { "tls_in_resumption", vtype_int, &tls_in.resumption },
+#endif
+#ifndef DISABLE_TLS
+ { "tls_in_sni", vtype_stringptr, &tls_in.sni },
+#endif
+ { "tls_in_ver", vtype_stringptr, &tls_in.ver },
+ { "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_cipher_std", vtype_stringptr, &tls_out.cipher_stdname },
+#ifdef SUPPORT_DANE
+ { "tls_out_dane", vtype_bool, &tls_out.dane_verified },
+#endif
+ { "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 },
+#ifndef DISABLE_TLS_RESUME
+ { "tls_out_resumption", vtype_int, &tls_out.resumption },
+#endif
+#ifndef DISABLE_TLS
+ { "tls_out_sni", vtype_stringptr, &tls_out.sni },
+#endif
+#ifdef SUPPORT_DANE
+ { "tls_out_tlsa_usage", vtype_int, &tls_out.tlsa_usage },
+#endif
+ { "tls_out_ver", vtype_stringptr, &tls_out.ver },
+
+ { "tls_peerdn", vtype_stringptr, &tls_in.peerdn }, /* mind the alphabetical order! */
+#ifndef DISABLE_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 },
+ { "transport_name", vtype_stringptr, &transport_name },
+ { "value", vtype_stringptr, &lookup_value },
+ { "verify_mode", vtype_stringptr, &verify_mode },
+ { "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 = nelem(var_table);
+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" };
+
+/* flags for find_header() */
+#define FH_EXISTS_ONLY BIT(0)
+#define FH_WANT_RAW BIT(1)
+#define FH_WANT_LIST BIT(2)
+
+
+/*************************************************
+* 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; \
+ } \
+ }
+
+
+
+static uschar * base32_chars = US"abcdefghijklmnopqrstuvwxyz234567";
+
+/*************************************************
+* 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)
+{
+uschar * ss = expand_string(condition);
+if (!ss)
+ {
+ if (!f.expand_string_forcedfail && !f.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;
+ }
+return *ss && Ustrcmp(ss, "0") != 0 && strcmpic(ss, US"no") != 0 &&
+ strcmpic(ss, US"false") != 0;
+}
+
+
+
+
+/*************************************************
+* 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 DISABLE_TLS is not 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]
+*/
+
+#ifndef DISABLE_TLS
+# define vaguely_random_number vaguely_random_number_fallback
+#endif
+int
+vaguely_random_number(int max)
+{
+#ifndef DISABLE_TLS
+# undef vaguely_random_number
+#endif
+static pid_t pid = 0;
+pid_t p2;
+
+if ((p2 = getpid()) != pid)
+ {
+ if (pid != 0)
+ {
+
+#ifdef HAVE_ARC4RANDOM
+ /* cryptographically strong randomness, common on *BSD platforms, not
+ so much elsewhere. Alas. */
+# ifndef NOT_HAVE_ARC4RANDOM_STIR
+ arc4random_stir();
+# endif
+#elif defined(HAVE_SRANDOM) || defined(HAVE_SRANDOMDEV)
+# ifdef HAVE_SRANDOMDEV
+ /* uses random(4) for seeding */
+ srandomdev();
+# else
+ {
+ struct timeval tv;
+ 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 const uschar *
+read_name(uschar *name, int max, const uschar *s, uschar *extras)
+{
+int ptr = 0;
+while (*s && (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 const uschar *
+read_header_name(uschar *name, int max, const 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
+*/
+/*XXX consider expanding to int_eximarith_t. But the test for
+"overbig numbers" in 0002 still needs to overflow it. */
+
+static uschar *
+read_number(int *n, uschar *s)
+{
+*n = 0;
+while (isdigit(*s)) *n = *n * 10 + (*s++ - '0');
+return s;
+}
+
+static const uschar *
+read_cnumber(int *n, const 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
+*/
+
+uschar *
+expand_getkeyed(const uschar * key, const uschar * s)
+{
+int length = Ustrlen(key);
+Uskip_whitespace(&s);
+
+/* Loop to search for the key */
+
+while (*s)
+ {
+ int dkeylength;
+ uschar * data;
+ const uschar * dkey = s;
+
+ while (*s && *s != '=' && !isspace(*s)) s++;
+ dkeylength = s - dkey;
+ if (Uskip_whitespace(&s) == '=') while (isspace(*++s));
+
+ data = string_dequote(&s);
+ if (length == dkeylength && strncmpic(key, dkey, length) == 0)
+ return data;
+
+ Uskip_whitespace(&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, const uschar * list)
+{
+const uschar * tlist = list;
+int sep = 0;
+/* Tainted mem for the throwaway element copies */
+uschar * dummy = store_get(2, GET_TAINTED);
+
+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 */
+
+#ifndef DISABLE_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;
+
+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 (certfield * cp = certfields;
+ cp < certfields + nelem(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 /*DISABLE_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 = nelem(prime) - 1;
+ total += prime[i--] * (unsigned int)(*s++);
+ }
+
+/* If value2 is unset, just compute one number */
+
+if (value2 < 0)
+ s = string_sprintf("%lu", total % value1);
+
+/* Otherwise do a div/mod hash */
+
+else
+ {
+ total = total % (value1 * value2);
+ s = string_sprintf("%lu/%lu", 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.
+
+Arguments:
+ name the name of the header, without the leading $header_ or $h_,
+ or NULL if a concatenation of all headers is required
+ newsize return the size of memory block that was obtained; may be NULL
+ if exists_only is TRUE
+ flags FH_EXISTS_ONLY
+ set 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
+ FH_WANT_RAW
+ set if called for $rh_ or $rheader_ items; no processing,
+ other than concatenating, will be done on the header. Also used
+ for $message_headers_raw.
+ FH_WANT_LIST
+ Double colon chars in the content, and replace newline with
+ colon between each element when concatenating; returning a
+ colon-sep list (elements might contain newlines)
+ 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, int *newsize, unsigned flags, const uschar *charset)
+{
+BOOL found = !name;
+int len = name ? Ustrlen(name) : 0;
+BOOL comma = FALSE;
+gstring * g = NULL;
+
+for (header_line * h = header_list; h; h = h->next)
+ if (h->type != htype_old && h->text) /* NULL => Received: placeholder */
+ if (!name || (len <= h->slen && strncmpic(name, h->text, len) == 0))
+ {
+ uschar * s, * t;
+ size_t inc;
+
+ if (flags & FH_EXISTS_ONLY)
+ return US"1"; /* don't need actual string */
+
+ found = TRUE;
+ s = h->text + len; /* text to insert */
+ if (!(flags & FH_WANT_RAW)) /* unless wanted raw, */
+ Uskip_whitespace(&s); /* remove leading white space */
+ t = h->text + h->slen; /* end-point */
+
+ /* Unless wanted raw, remove trailing whitespace, including the
+ newline. */
+
+ if (flags & FH_WANT_LIST)
+ while (t > s && t[-1] == '\n') t--;
+ else if (!(flags & FH_WANT_RAW))
+ {
+ while (t > s && isspace(t[-1])) t--;
+
+ /* Set comma 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 (name && !comma && Ustrchr("BCFRST", h->type)) comma = TRUE;
+ }
+
+ /* Trim the header roughly if we're approaching limits */
+ inc = t - s;
+ if (gstring_length(g) + inc > header_insert_maxlen)
+ inc = header_insert_maxlen - gstring_length(g);
+
+ /* For raw just copy the data; for a list, add the data as a colon-sep
+ list-element; for comma-list add as an unchecked comma,newline sep
+ list-elemment; for other nonraw add as an unchecked newline-sep list (we
+ stripped trailing WS above including the newline). We ignore the potential
+ expansion due to colon-doubling, just leaving the loop if the limit is met
+ or exceeded. */
+
+ if (flags & FH_WANT_LIST)
+ g = string_append_listele_n(g, ':', s, (unsigned)inc);
+ else if (flags & FH_WANT_RAW)
+ g = string_catn(g, s, (unsigned)inc);
+ else if (inc > 0)
+ g = string_append2_listele_n(g, comma ? US",\n" : US"\n",
+ s, (unsigned)inc);
+
+ if (gstring_length(g) >= header_insert_maxlen) break;
+ }
+
+if (!found) return NULL; /* No header found */
+if (!g) return US"";
+
+/* That's all we do for raw header expansion. */
+
+*newsize = g->size;
+if (flags & FH_WANT_RAW)
+ return string_from_gstring(g);
+
+/* Otherwise 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 * error, * decoded = rfc2047_decode2(string_from_gstring(g),
+ check_rfc2047_length, charset, '?', NULL, newsize, &error);
+ if (error)
+ DEBUG(D_any) debug_printf("*** error in RFC 2047 decoding: %s\n"
+ " input was: %s\n", error, g->s);
+ return decoded ? decoded : string_from_gstring(g);
+ }
+}
+
+
+
+
+/* Append a "local" element to an Authentication-Results: header
+if this was a non-smtp message.
+*/
+
+static gstring *
+authres_local(gstring * g, const uschar * sysname)
+{
+if (!f.authentication_local)
+ return g;
+g = string_append(g, 3, US";\n\tlocal=pass (non-smtp, ", sysname, US")");
+if (authenticated_id) g = string_append(g, 2, " u=", authenticated_id);
+return g;
+}
+
+
+/* Append an "iprev" element to an Authentication-Results: header
+if we have attempted to get the calling host's name.
+*/
+
+static gstring *
+authres_iprev(gstring * g)
+{
+if (sender_host_name)
+ g = string_append(g, 3, US";\n\tiprev=pass (", sender_host_name, US")");
+else if (host_lookup_deferred)
+ g = string_cat(g, US";\n\tiprev=temperror");
+else if (host_lookup_failed)
+ g = string_cat(g, US";\n\tiprev=fail");
+else
+ return g;
+
+if (sender_host_address)
+ g = string_append(g, 2, US" smtp.remote-ip=", sender_host_address);
+return g;
+}
+
+
+
+/*************************************************
+* 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)
+{
+uschar * s;
+gstring * g = NULL;
+
+if (!f.enable_dollar_recipients) return NULL;
+
+for (int i = 0; i < recipients_count; i++)
+ {
+ s = recipients_list[i].address;
+ g = string_append2_listele_n(g, US", ", s, Ustrlen(s));
+ }
+return g ? g->s : NULL;
+}
+
+
+/*************************************************
+* Return size of queue *
+*************************************************/
+/* Ask the daemon for the queue size */
+
+static uschar *
+fn_queue_size(void)
+{
+struct sockaddr_un sa_un = {.sun_family = AF_UNIX};
+uschar buf[16];
+int fd;
+ssize_t len;
+const uschar * where;
+#ifndef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+uschar * sname;
+#endif
+
+if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0)
+ {
+ DEBUG(D_expand) debug_printf(" socket: %s\n", strerror(errno));
+ return NULL;
+ }
+
+#ifdef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+sa_un.sun_path[0] = 0; /* Abstract local socket addr - Linux-specific? */
+len = offsetof(struct sockaddr_un, sun_path) + 1
+ + snprintf(sa_un.sun_path+1, sizeof(sa_un.sun_path)-1, "exim_%d", getpid());
+#else
+sname = string_sprintf("%s/p_%d", spool_directory, getpid());
+len = offsetof(struct sockaddr_un, sun_path)
+ + snprintf(sa_un.sun_path, sizeof(sa_un.sun_path), "%s", sname);
+#endif
+
+if (bind(fd, (const struct sockaddr *)&sa_un, len) < 0)
+ { where = US"bind"; goto bad; }
+
+#ifdef notdef
+debug_printf("local addr '%s%s'\n",
+ *sa_un.sun_path ? "" : "@",
+ sa_un.sun_path + (*sa_un.sun_path ? 0 : 1));
+#endif
+
+#ifdef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+sa_un.sun_path[0] = 0; /* Abstract local socket addr - Linux-specific? */
+len = offsetof(struct sockaddr_un, sun_path) + 1
+ + snprintf(sa_un.sun_path+1, sizeof(sa_un.sun_path)-1, "%s",
+ expand_string(notifier_socket));
+#else
+len = offsetof(struct sockaddr_un, sun_path)
+ + snprintf(sa_un.sun_path, sizeof(sa_un.sun_path), "%s",
+ expand_string(notifier_socket));
+#endif
+
+if (connect(fd, (const struct sockaddr *)&sa_un, len) < 0)
+ { where = US"connect"; goto bad2; }
+
+buf[0] = NOTIFY_QUEUE_SIZE_REQ;
+if (send(fd, buf, 1, 0) < 0) { where = US"send"; goto bad; }
+
+if (poll_one_fd(fd, POLLIN, 2 * 1000) != 1)
+ {
+ DEBUG(D_expand) debug_printf("no daemon response; using local evaluation\n");
+ len = snprintf(CS buf, sizeof(buf), "%u", queue_count_cached());
+ }
+else if ((len = recv(fd, buf, sizeof(buf), 0)) < 0)
+ { where = US"recv"; goto bad2; }
+
+close(fd);
+#ifndef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+Uunlink(sname);
+#endif
+return string_copyn(buf, len);
+
+bad2:
+#ifndef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+ Uunlink(sname);
+#endif
+bad:
+ close(fd);
+ DEBUG(D_expand) debug_printf(" %s: %s\n", where, strerror(errno));
+ return NULL;
+}
+
+
+/*************************************************
+* 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 const 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 ? node->data.ptr : strict_acl_vars ? NULL : US"";
+ }
+else if (Ustrncmp(name, "r_", 2) == 0)
+ {
+ tree_node * node = tree_search(router_var, name + 2);
+ return node ? node->data.ptr : strict_acl_vars ? NULL : US"";
+ }
+
+/* Handle $auth<n> variables. */
+
+if (Ustrncmp(name, "auth", 4) == 0)
+ {
+ uschar *endptr;
+ int n = Ustrtoul(name + 4, &endptr, 10);
+ if (!*endptr && n != 0 && n <= AUTH_VARS)
+ return auth_vars[n-1] ? auth_vars[n-1] : US"";
+ }
+else if (Ustrncmp(name, "regex", 5) == 0)
+ {
+ uschar *endptr;
+ int n = Ustrtoul(name + 5, &endptr, 10);
+ if (!*endptr && n != 0 && n <= REGEX_VARS)
+ return regex_vars[n-1] ? regex_vars[n-1] : US"";
+ }
+
+/* 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 (!f.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 */
+ return (s = *((uschar **)(val))) ? s : US"";
+
+ 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 && sender_host_address
+ && !host_lookup_failed && host_name_lookup() == OK)
+ host_build_sender_fullhost();
+ return sender_host_name ? sender_host_name : US"";
+
+ case vtype_localpart: /* Get local part from address */
+ if (!(s = *((uschar **)(val)))) return US"";
+ if (!(domain = Ustrrchr(s, '@'))) 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));
+ return string_copyn(s, domain - s);
+
+ case vtype_domain: /* Get domain from address */
+ if (!(s = *((uschar **)(val)))) return US"";
+ domain = Ustrrchr(s, '@');
+ return domain ? domain + 1 : US"";
+
+ case vtype_msgheaders:
+ return find_header(NULL, newsize, exists_only ? FH_EXISTS_ONLY : 0, NULL);
+
+ case vtype_msgheaders_raw:
+ return find_header(NULL, newsize,
+ exists_only ? FH_EXISTS_ONLY|FH_WANT_RAW : FH_WANT_RAW, NULL);
+
+ case vtype_msgbody: /* Pointer to msgbody string */
+ case vtype_msgbody_end: /* Ditto, the end of the msg */
+ ss = (uschar **)(val);
+ if (!*ss && 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_get(len+1, GET_TAINTED);
+ 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;
+ }
+ }
+ if (lseek(deliver_datafile, start_offset, SEEK_SET) < 0)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "deliver_datafile lseek: %s",
+ strerror(errno));
+ if ((len = read(deliver_datafile, body, 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 ? *ss : US"";
+
+ 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:", newsize,
+ exists_only ? FH_EXISTS_ONLY|FH_WANT_RAW : FH_WANT_RAW,
+ headers_charset);
+ if (s) Uskip_whitespace(&s);
+ if (!s || !*s)
+ {
+ *newsize = 0; /* For the *s==0 case */
+ s = find_header(US"from:", newsize,
+ exists_only ? FH_EXISTS_ONLY|FH_WANT_RAW : FH_WANT_RAW,
+ headers_charset);
+ }
+ if (s)
+ {
+ uschar *t;
+ Uskip_whitespace(&s);
+ for (t = s; *t; t++) if (*t == '\n') *t = ' ';
+ while (t > s && isspace(t[-1])) t--;
+ *t = 0;
+ }
+ return s ? s : US"";
+
+ case vtype_string_func:
+ {
+ stringptr_fn_t * fn = (stringptr_fn_t *) val;
+ uschar* s = fn();
+ return s ? s : US"";
+ }
+
+ case vtype_pspace:
+ {
+ int inodes;
+ sprintf(CS var_buffer, PR_EXIM_ARITH,
+ 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, const uschar **sptr, BOOL skipping,
+ BOOL check_end, uschar *name, BOOL *resetok)
+{
+const uschar *s = *sptr;
+
+Uskip_whitespace(&s);
+for (int i = 0; i < n; i++)
+ {
+ if (*s != '{')
+ {
+ if (i < m)
+ {
+ expand_string_message = string_sprintf("Not enough arguments for '%s' "
+ "(min is %d)", name, m);
+ return 1;
+ }
+ sub[i] = NULL;
+ break;
+ }
+ if (!(sub[i] = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, resetok)))
+ return 3;
+ if (*s++ != '}') return 1;
+ Uskip_whitespace(&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;
+ }
+ expand_string_message = string_sprintf("missing '}' after '%s'", name);
+ 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 (apparently) 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;
+int sav_narg = acl_narg;
+int ret;
+uschar * dummy_logmsg;
+extern int acl_where;
+
+if(--nsub > nelem(acl_arg)) nsub = nelem(acl_arg);
+for (i = 0; i < nsub && sub[i+1]; i++)
+ {
+ uschar * 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_indent("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, &dummy_logmsg);
+
+for (i = 0; i < nsub; i++)
+ acl_arg[i] = sub[i+1]; /* restore old args */
+acl_narg = sav_narg;
+
+return ret;
+}
+
+
+
+
+/* Return pointer to dewrapped string, with enclosing specified chars removed.
+The given string is modified on return. Leading whitespace is skipped while
+looking for the opening wrap character, then the rest is scanned for the trailing
+(non-escaped) wrap character. A backslash in the string will act as an escape.
+
+A nul is written over the trailing wrap, and a pointer to the char after the
+leading wrap is returned.
+
+Arguments:
+ s String for de-wrapping
+ wrap Two-char string, the first being the opener, second the closer wrapping
+ character
+Return:
+ Pointer to de-wrapped string, or NULL on error (with expand_string_message set).
+*/
+
+static uschar *
+dewrap(uschar * s, const uschar * wrap)
+{
+uschar * p = s;
+unsigned depth = 0;
+BOOL quotesmode = wrap[0] == wrap[1];
+
+if (Uskip_whitespace(&p) == *wrap)
+ {
+ s = ++p;
+ wrap++;
+ while (*p)
+ {
+ if (*p == '\\') p++;
+ else if (!quotesmode && *p == wrap[-1]) depth++;
+ else if (*p == *wrap)
+ if (depth == 0)
+ {
+ *p = '\0';
+ return s;
+ }
+ else
+ depth--;
+ p++;
+ }
+ }
+expand_string_message = string_sprintf("missing '%c'", *wrap);
+return NULL;
+}
+
+
+/* Pull off the leading array or object element, returning
+a copy in an allocated string. Update the list pointer.
+
+The element may itself be an abject or array.
+Return NULL when the list is empty.
+*/
+
+static uschar *
+json_nextinlist(const uschar ** list)
+{
+unsigned array_depth = 0, object_depth = 0;
+const uschar * s = *list, * item;
+
+skip_whitespace(&s);
+
+for (item = s;
+ *s && (*s != ',' || array_depth != 0 || object_depth != 0);
+ s++)
+ switch (*s)
+ {
+ case '[': array_depth++; break;
+ case ']': array_depth--; break;
+ case '{': object_depth++; break;
+ case '}': object_depth--; break;
+ }
+*list = *s ? s+1 : s;
+if (item == s) return NULL;
+item = string_copyn(item, s - item);
+DEBUG(D_expand) debug_printf_indent(" json ele: '%s'\n", item);
+return US item;
+}
+
+
+
+/************************************************/
+/* Return offset in ops table, or -1 if not found.
+Repoint to just after the operator in the string.
+
+Argument:
+ ss string representation of operator
+ opname split-out operator name
+*/
+
+static int
+identify_operator(const uschar ** ss, uschar ** opname)
+{
+const uschar * s = *ss;
+uschar name[256];
+
+/* 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, sizeof(name), s, US"_");
+*ss = s;
+
+/* If we haven't read a name, it means some non-alpha character is first. */
+
+if (!name[0])
+ {
+ expand_string_message = string_sprintf("condition name expected, "
+ "but found \"%.16s\"", s);
+ return -1;
+ }
+if (opname)
+ *opname = string_copy(name);
+
+return chop_match(name, cond_table, nelem(cond_table));
+}
+
+
+/*************************************************
+* 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((hctx *)base);
+}
+
+static void
+chash_mid(int type, void * base, const uschar * string)
+{
+if (type == HMAC_MD5)
+ md5_mid((md5 *)base, string);
+else
+ sha1_mid((hctx *)base, string);
+}
+
+static void
+chash_end(int type, void * base, const uschar * string, int length,
+ uschar * digest)
+{
+if (type == HMAC_MD5)
+ md5_end((md5 *)base, string, length, digest);
+else
+ sha1_end((hctx *)base, string, length, digest);
+}
+
+
+
+
+#ifdef SUPPORT_SRS
+/* Do an hmac_md5. The result is _not_ nul-terminated, and is sized as
+the smaller of a full hmac_md5 result (16 bytes) or the supplied output buffer.
+
+Arguments:
+ key encoding key, nul-terminated
+ src data to be hashed, nul-terminated
+ buf output buffer
+ len size of output buffer
+*/
+
+static void
+hmac_md5(const uschar * key, const uschar * src, uschar * buf, unsigned len)
+{
+md5 md5_base;
+const uschar * keyptr;
+uschar * p;
+unsigned int keylen;
+
+#define MD5_HASHLEN 16
+#define MD5_HASHBLOCKLEN 64
+
+uschar keyhash[MD5_HASHLEN];
+uschar innerhash[MD5_HASHLEN];
+uschar finalhash[MD5_HASHLEN];
+uschar innerkey[MD5_HASHBLOCKLEN];
+uschar outerkey[MD5_HASHBLOCKLEN];
+
+keyptr = key;
+keylen = Ustrlen(keyptr);
+
+/* If the key is longer than the hash block length, then hash the key
+first */
+
+if (keylen > MD5_HASHBLOCKLEN)
+ {
+ chash_start(HMAC_MD5, &md5_base);
+ chash_end(HMAC_MD5, &md5_base, keyptr, keylen, keyhash);
+ keyptr = keyhash;
+ keylen = MD5_HASHLEN;
+ }
+
+/* Now make the inner and outer key values */
+
+memset(innerkey, 0x36, MD5_HASHBLOCKLEN);
+memset(outerkey, 0x5c, MD5_HASHBLOCKLEN);
+
+for (int i = 0; i < keylen; i++)
+ {
+ innerkey[i] ^= keyptr[i];
+ outerkey[i] ^= keyptr[i];
+ }
+
+/* Now do the hashes */
+
+chash_start(HMAC_MD5, &md5_base);
+chash_mid(HMAC_MD5, &md5_base, innerkey);
+chash_end(HMAC_MD5, &md5_base, src, Ustrlen(src), innerhash);
+
+chash_start(HMAC_MD5, &md5_base);
+chash_mid(HMAC_MD5, &md5_base, outerkey);
+chash_end(HMAC_MD5, &md5_base, innerhash, MD5_HASHLEN, finalhash);
+
+/* Encode the final hash as a hex string, limited by output buffer size */
+
+p = buf;
+for (int i = 0, j = len; i < MD5_HASHLEN; i++)
+ {
+ if (j-- <= 0) break;
+ *p++ = hex_digits[(finalhash[i] & 0xf0) >> 4];
+ if (j-- <= 0) break;
+ *p++ = hex_digits[finalhash[i] & 0x0f];
+ }
+return;
+}
+#endif /*SUPPORT_SRS*/
+
+
+/*************************************************
+* 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 const uschar *
+eval_condition(const uschar *s, BOOL *resetok, BOOL *yield)
+{
+BOOL testfor = TRUE;
+BOOL tempcond, combined_cond;
+BOOL *subcondptr;
+BOOL sub2_honour_dollar = TRUE;
+BOOL is_forany, is_json, is_jsons;
+int rc, cond_type;
+int_eximarith_t num[2];
+struct stat statbuf;
+uschar * opname;
+uschar name[256];
+const uschar *sub[10];
+
+for (;;)
+ if (Uskip_whitespace(&s) == '!') { testfor = !testfor; s++; } else break;
+
+switch(cond_type = identify_operator(&s, &opname))
+ {
+ /* 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:
+ {
+ const uschar * t;
+
+ if (*s != ':')
+ {
+ expand_string_message = US"\":\" expected after \"def\"";
+ return NULL;
+ }
+
+ s = read_name(name, sizeof(name), 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 ( ( *(t = name) == 'h'
+ || (*t == 'r' || *t == 'l' || *t == 'b') && *++t == 'h'
+ )
+ && (*++t == '_' || Ustrncmp(t, "eader_", 6) == 0)
+ )
+ {
+ s = read_header_name(name, sizeof(name), s);
+ /* {-for-text-editors */
+ if (Ustrchr(name, '}') != NULL) malformed_header = TRUE;
+ if (yield) *yield =
+ (find_header(name, NULL, FH_EXISTS_ONLY, NULL) != NULL) == testfor;
+ }
+
+ /* Test for a variable's having a non-empty value. A non-existent variable
+ causes an expansion failure. */
+
+ else
+ {
+ if (!(t = find_variable(name, TRUE, yield == NULL, NULL)))
+ {
+ expand_string_message = name[0]
+ ? string_sprintf("unknown variable \"%s\" after \"def:\"", name)
+ : US"variable name omitted after \"def:\"";
+ check_variable_error_message(name);
+ return NULL;
+ }
+ if (yield) *yield = (t[0] != 0) == testfor;
+ }
+
+ return s;
+ }
+
+
+ /* first_delivery tests for first delivery attempt */
+
+ case ECOND_FIRST_DELIVERY:
+ if (yield) *yield = f.deliver_firsttime == testfor;
+ return s;
+
+
+ /* queue_running tests for any process started by a queue runner */
+
+ case ECOND_QUEUE_RUNNING:
+ if (yield) *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:
+
+ if (Uskip_whitespace(&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]) return NULL;
+ /* {-for-text-editors */
+ if (*s++ != '}') goto COND_FAILED_CURLY_END;
+
+ if (!yield) 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 *sub[10];
+ uschar *user_msg;
+ BOOL cond = FALSE;
+
+ Uskip_whitespace(&s);
+ if (*s++ != '{') goto COND_FAILED_CURLY_START; /*}*/
+
+ switch(read_subs(sub, nelem(sub), 1,
+ &s, yield == NULL, TRUE, name, resetok))
+ {
+ case 1: expand_string_message = US"too few arguments or bracketing "
+ "error for acl";
+ case 2:
+ case 3: return NULL;
+ }
+
+ if (yield)
+ {
+ int rc;
+ *resetok = FALSE; /* eval_acl() might allocate; do not reclaim */
+ switch(rc = eval_acl(sub, nelem(sub), &user_msg))
+ {
+ case OK:
+ cond = TRUE;
+ case FAIL:
+ lookup_value = NULL;
+ if (user_msg)
+ lookup_value = string_copy(user_msg);
+ *yield = cond == testfor;
+ break;
+
+ case DEFER:
+ f.expand_string_forcedfail = TRUE;
+ /*FALLTHROUGH*/
+ default:
+ expand_string_message = string_sprintf("%s from acl \"%s\"",
+ rc_names[rc], 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
+ {
+ uschar *sub[4];
+ Uskip_whitespace(&s);
+ if (*s++ != '{') goto COND_FAILED_CURLY_START; /* }-for-text-editors */
+ switch(read_subs(sub, nelem(sub), 2, &s, yield == NULL, TRUE, name,
+ resetok))
+ {
+ case 1: expand_string_message = US"too few arguments or bracketing "
+ "error for saslauthd";
+ case 2:
+ case 3: return NULL;
+ }
+ if (!sub[2]) sub[3] = NULL; /* realm if no service */
+ if (yield)
+ {
+ int 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 (int 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;
+
+ if (Uskip_whitespace(&s) != '{')
+ {
+ if (i == 0) goto COND_FAILED_CURLY_START;
+ expand_string_message = string_sprintf("missing 2nd string in {} "
+ "after \"%s\"", opname);
+ return NULL;
+ }
+ if (!(sub[i] = expand_string_internal(s+1, TRUE, &s, yield == NULL,
+ honour_dollar, resetok)))
+ return NULL;
+ DEBUG(D_expand) if (i == 1 && !sub2_honour_dollar && Ustrchr(sub[1], '$'))
+ debug_printf_indent("WARNING: the second arg is NOT expanded,"
+ " for security reasons\n");
+ 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(opname[0]) && yield)
+ if (sub[i][0] == 0)
+ {
+ num[i] = 0;
+ DEBUG(D_expand)
+ debug_printf_indent("empty string cast to zero for numerical comparison\n");
+ }
+ else
+ {
+ num[i] = expanded_string_integer(sub[i], FALSE);
+ if (expand_string_message) return NULL;
+ }
+ }
+
+ /* Result not required */
+
+ if (!yield) 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 */
+ {
+ const pcre2_code * re;
+ PCRE2_SIZE offset;
+ int err;
+
+ if (!(re = pcre2_compile((PCRE2_SPTR)sub[1], PCRE2_ZERO_TERMINATED,
+ PCRE_COPT, &err, &offset, pcre_cmp_ctx)))
+ {
+ uschar errbuf[128];
+ pcre2_get_error_message(err, errbuf, sizeof(errbuf));
+ expand_string_message = string_sprintf("regular expression error in "
+ "\"%s\": %s at offset %ld", sub[1], errbuf, (long)offset);
+ 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,
+ CUSS &lookup_value);
+ 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, CUSS &lookup_value);
+ 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 */
+ CUSS &lookup_value); /* 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, CUSS &lookup_value);
+ /* 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, 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 = b64encode(CUS 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)
+ {
+ uschar coded[36];
+ for (int 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);
+ hctx h;
+ uschar digest[20];
+
+ sha1_start(&h);
+ sha1_end(&h, 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 = b64encode(CUS 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)
+ {
+ uschar coded[44];
+ for (int 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! */
+
+ if (coded)
+ tempcond = Ustrlen(sub[1]) < 2 ? FALSE : Ustrcmp(coded, sub[1]) == 0;
+ else if (errno == EINVAL)
+ tempcond = FALSE;
+ else
+ {
+ expand_string_message = string_sprintf("crypt error: %s\n",
+ US strerror(errno));
+ return NULL;
+ }
+ }
+ break;
+ #endif /* SUPPORT_CRYPTEQ */
+
+ case ECOND_INLIST:
+ case ECOND_INLISTI:
+ {
+ const uschar * list = sub[1];
+ int sep = 0;
+ uschar *save_iterate_item = iterate_item;
+ int (*compare)(const uschar *, const uschar *);
+
+ DEBUG(D_expand) debug_printf_indent("condition: %s item: %s\n", opname, sub[0]);
+
+ tempcond = FALSE;
+ compare = cond_type == ECOND_INLISTI
+ ? strcmpic : (int (*)(const uschar *, const uschar *)) strcmp;
+
+ while ((iterate_item = string_nextinlist(&list, &sep, NULL, 0)))
+ {
+ DEBUG(D_expand) debug_printf_indent(" compare %s\n", iterate_item);
+ if (compare(sub[0], iterate_item) == 0)
+ {
+ tempcond = TRUE;
+ lookup_value = iterate_item;
+ 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);
+
+ Uskip_whitespace(&s);
+ if (*s++ != '{') goto COND_FAILED_CURLY_START; /* }-for-text-editors */
+
+ for (;;)
+ {
+ /* {-for-text-editors */
+ if (Uskip_whitespace(&s) == '}') break;
+ if (*s != '{') /* }-for-text-editors */
+ {
+ expand_string_message = string_sprintf("each subcondition "
+ "inside an \"%s{...}\" condition must be in its own {}", opname);
+ return NULL;
+ }
+
+ if (!(s = eval_condition(s+1, resetok, subcondptr)))
+ {
+ expand_string_message = string_sprintf("%s inside \"%s{...}\" condition",
+ expand_string_message, opname);
+ return NULL;
+ }
+ Uskip_whitespace(&s);
+
+ /* {-for-text-editors */
+ if (*s++ != '}')
+ {
+ /* {-for-text-editors */
+ expand_string_message = string_sprintf("missing } at end of condition "
+ "inside \"%s\" group", opname);
+ return NULL;
+ }
+
+ if (yield)
+ 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) *yield = (combined_cond == testfor);
+ return ++s;
+
+
+ /* forall/forany: iterates a condition with different values */
+
+ case ECOND_FORALL: is_forany = FALSE; is_json = FALSE; is_jsons = FALSE; goto FORMANY;
+ case ECOND_FORANY: is_forany = TRUE; is_json = FALSE; is_jsons = FALSE; goto FORMANY;
+ case ECOND_FORALL_JSON: is_forany = FALSE; is_json = TRUE; is_jsons = FALSE; goto FORMANY;
+ case ECOND_FORANY_JSON: is_forany = TRUE; is_json = TRUE; is_jsons = FALSE; goto FORMANY;
+ case ECOND_FORALL_JSONS: is_forany = FALSE; is_json = TRUE; is_jsons = TRUE; goto FORMANY;
+ case ECOND_FORANY_JSONS: is_forany = TRUE; is_json = TRUE; is_jsons = TRUE; goto FORMANY;
+
+ FORMANY:
+ {
+ const uschar * list;
+ int sep = 0;
+ uschar *save_iterate_item = iterate_item;
+
+ DEBUG(D_expand) debug_printf_indent("condition: %s\n", opname);
+
+ Uskip_whitespace(&s);
+ if (*s++ != '{') goto COND_FAILED_CURLY_START; /* }-for-text-editors */
+ if (!(sub[0] = expand_string_internal(s, TRUE, &s, yield == NULL, TRUE, resetok)))
+ return NULL;
+ /* {-for-text-editors */
+ if (*s++ != '}') goto COND_FAILED_CURLY_END;
+
+ Uskip_whitespace(&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, opname);
+ return NULL;
+ }
+ Uskip_whitespace(&s);
+
+ /* {-for-text-editors */
+ if (*s++ != '}')
+ {
+ /* {-for-text-editors */
+ expand_string_message = string_sprintf("missing } at end of condition "
+ "inside \"%s\"", opname);
+ return NULL;
+ }
+
+ if (yield) *yield = !testfor;
+ list = sub[0];
+ if (is_json) list = dewrap(string_copy(list), US"[]");
+ while ((iterate_item = is_json
+ ? json_nextinlist(&list) : string_nextinlist(&list, &sep, NULL, 0)))
+ {
+ if (is_jsons)
+ if (!(iterate_item = dewrap(iterate_item, US"\"\"")))
+ {
+ expand_string_message =
+ string_sprintf("%s wrapping string result for extract jsons",
+ expand_string_message);
+ iterate_item = save_iterate_item;
+ return NULL;
+ }
+
+ DEBUG(D_expand) debug_printf_indent("%s: $item = \"%s\"\n", opname, iterate_item);
+ if (!eval_condition(sub[1], resetok, &tempcond))
+ {
+ expand_string_message = string_sprintf("%s inside \"%s\" condition",
+ expand_string_message, opname);
+ iterate_item = save_iterate_item;
+ return NULL;
+ }
+ DEBUG(D_expand) debug_printf_indent("%s: condition evaluated to %s\n", opname,
+ tempcond? "true":"false");
+
+ if (yield) *yield = (tempcond == testfor);
+ if (tempcond == is_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;
+
+ if (Uskip_whitespace(&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];
+ Uskip_whitespace(&t);
+ if ((len = Ustrlen(t)))
+ {
+ /* 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_indent("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;
+ }
+ DEBUG(D_expand) debug_printf_indent("%s: condition evaluated to %s\n", ourname,
+ boolvalue? "true":"false");
+ if (yield) *yield = (boolvalue == testfor);
+ return s;
+ }
+
+#ifdef SUPPORT_SRS
+ case ECOND_INBOUND_SRS:
+ /* ${if inbound_srs {local_part}{secret} {yes}{no}} */
+ {
+ uschar * sub[2];
+ const pcre2_code * re;
+ pcre2_match_data * md;
+ PCRE2_SIZE * ovec;
+ int quoting = 0;
+ uschar cksum[4];
+ BOOL boolvalue = FALSE;
+
+ switch(read_subs(sub, 2, 2, CUSS &s, yield == NULL, FALSE, name, resetok))
+ {
+ case 1: expand_string_message = US"too few arguments or bracketing "
+ "error for inbound_srs";
+ case 2:
+ case 3: return NULL;
+ }
+
+ /* Match the given local_part against the SRS-encoded pattern */
+
+ re = regex_must_compile(US"^(?i)SRS0=([^=]+)=([A-Z2-7]+)=([^=]*)=(.*)$",
+ TRUE, FALSE);
+ md = pcre2_match_data_create(4+1, pcre_gen_ctx);
+ if (pcre2_match(re, sub[0], PCRE2_ZERO_TERMINATED, 0, PCRE_EOPT,
+ md, pcre_mtc_ctx) < 0)
+ {
+ DEBUG(D_expand) debug_printf("no match for SRS'd local-part pattern\n");
+ goto srs_result;
+ }
+ ovec = pcre2_get_ovector_pointer(md);
+
+ if (sub[0][0] == '"')
+ quoting = 1;
+ else for (uschar * s = sub[0]; *s; s++)
+ if (!isalnum(*s) && Ustrchr(".!#$%&'*+-/=?^_`{|}~", *s) == NULL)
+ { quoting = 1; break; }
+ if (quoting)
+ DEBUG(D_expand) debug_printf_indent("auto-quoting local part\n");
+
+ /* Record the (quoted, if needed) decoded recipient as $srs_recipient */
+
+ srs_recipient = string_sprintf("%.*s%.*S%.*s@%.*S", /* lowercased */
+ quoting, "\"",
+ (int) (ovec[9]-ovec[8]), sub[0] + ovec[8], /* substr 4 */
+ quoting, "\"",
+ (int) (ovec[7]-ovec[6]), sub[0] + ovec[6]); /* substr 3 */
+
+ /* If a zero-length secret was given, we're done. Otherwise carry on
+ and validate the given SRS local_part againt our secret. */
+
+ if (!*sub[1])
+ {
+ boolvalue = TRUE;
+ goto srs_result;
+ }
+
+ /* check the timestamp */
+ {
+ struct timeval now;
+ uschar * ss = sub[0] + ovec[4]; /* substring 2, the timestamp */
+ long d;
+ int n;
+
+ gettimeofday(&now, NULL);
+ now.tv_sec /= 86400; /* days since epoch */
+
+ /* Decode substring 2 from base32 to a number */
+
+ for (d = 0, n = ovec[5]-ovec[4]; n; n--)
+ {
+ uschar * t = Ustrchr(base32_chars, *ss++);
+ d = d * 32 + (t - base32_chars);
+ }
+
+ if (((now.tv_sec - d) & 0x3ff) > 10) /* days since SRS generated */
+ {
+ DEBUG(D_expand) debug_printf("SRS too old\n");
+ goto srs_result;
+ }
+ }
+
+ /* check length of substring 1, the offered checksum */
+
+ if (ovec[3]-ovec[2] != 4)
+ {
+ DEBUG(D_expand) debug_printf("SRS checksum wrong size\n");
+ goto srs_result;
+ }
+
+ /* Hash the address with our secret, and compare that computed checksum
+ with the one extracted from the arg */
+
+ hmac_md5(sub[1], srs_recipient, cksum, sizeof(cksum));
+ if (Ustrncmp(cksum, sub[0] + ovec[2], 4) != 0)
+ {
+ DEBUG(D_expand) debug_printf("SRS checksum mismatch\n");
+ goto srs_result;
+ }
+ boolvalue = TRUE;
+
+srs_result:
+ if (yield) *yield = (boolvalue == testfor);
+ return s;
+ }
+#endif /*SUPPORT_SRS*/
+
+ /* Unknown condition */
+
+ default:
+ if (!expand_string_message || !*expand_string_message)
+ expand_string_message = string_sprintf("unknown condition \"%s\"", opname);
+ 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\"", opname);
+return NULL;
+
+COND_FAILED_CURLY_END:
+expand_string_message = string_sprintf("missing } at end of \"%s\" condition",
+ opname);
+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",
+ opname);
+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(const uschar **save_expand_nstring, int *save_expand_nlength)
+{
+for (int 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, const uschar **save_expand_nstring,
+ int *save_expand_nlength)
+{
+expand_nmax = save_expand_nmax;
+for (int 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 growable-string pointer
+ type "lookup", "if", "extract", "run", "env", "listextract" or
+ "certextract" 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, const uschar **sptr,
+ gstring ** yieldptr, uschar *type, BOOL *resetok)
+{
+int rc = 0;
+const uschar *s = *sptr; /* Local value */
+uschar *sub1, *sub2;
+const uschar * errwhere;
+
+/* 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. */
+
+if (skip_whitespace(&s) == '}')
+ {
+ if (type[0] == 'i')
+ {
+ if (yes && !skipping)
+ *yieldptr = string_catn(*yieldptr, US"true", 4);
+ }
+ else
+ {
+ if (yes && lookup_value && !skipping)
+ *yieldptr = string_cat(*yieldptr, lookup_value);
+ lookup_value = save_lookup;
+ }
+ s++;
+ goto RETURN;
+ }
+
+/* The first following string must be braced. */
+
+if (*s++ != '{')
+ {
+ errwhere = US"'yes' part did not start with '{'";
+ 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 || !f.expand_string_forcedfail)) goto FAILED;
+f.expand_string_forcedfail = FALSE;
+if (*s++ != '}')
+ {
+ errwhere = US"'yes' part did not end with '}'";
+ goto FAILED_CURLY;
+ }
+
+/* If we want the first string, add it to the output */
+
+if (yes)
+ *yieldptr = string_cat(*yieldptr, sub1);
+
+/* If this is called from a lookup/env or a (cert)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. */
+
+if (skip_whitespace(&s) == '{')
+ {
+ sub2 = expand_string_internal(s+1, TRUE, &s, yes || skipping, TRUE, resetok);
+ if (sub2 == NULL && (!yes || !f.expand_string_forcedfail)) goto FAILED;
+ f.expand_string_forcedfail = FALSE;
+ if (*s++ != '}')
+ {
+ errwhere = US"'no' part did not start with '{'";
+ goto FAILED_CURLY;
+ }
+
+ /* If we want the second string, add it to the output */
+
+ if (!yes)
+ *yieldptr = string_cat(*yieldptr, 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];
+ /* deconst cast ok here as source is s anyway */
+ s = US read_name(name, sizeof(name), s, US"_");
+ if (Ustrcmp(name, "fail") == 0)
+ {
+ if (!yes && !skipping)
+ {
+ Uskip_whitespace(&s);
+ if (*s++ != '}')
+ {
+ errwhere = US"did not close with '}' after forcedfail";
+ goto FAILED_CURLY;
+ }
+ expand_string_message =
+ string_sprintf("\"%s\" failed and \"fail\" requested", type);
+ f.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. */
+
+skip_whitespace(&s);
+if (*s++ != '}')
+ {
+ errwhere = US"did not close with '}'";
+ goto FAILED_CURLY;
+ }
+
+
+RETURN:
+/* Update the input pointer value before returning */
+*sptr = s;
+return rc;
+
+FAILED_CURLY:
+ /* Get here if there is a bracketing failure */
+ expand_string_message = string_sprintf(
+ "curly-bracket problem in conditional yes/no parsing: %s\n"
+ " remaining string is '%s'", errwhere, --s);
+ rc = 2;
+ goto RETURN;
+
+FAILED:
+ /* Get here for other failures */
+ rc = 1;
+ goto RETURN;
+}
+
+
+
+
+/********************************************************
+* 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, GET_UNTAINTED); /* 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)
+{
+gstring * hash_source;
+uschar * p;
+hctx h;
+uschar innerhash[20];
+uschar finalhash[20];
+uschar innerkey[64];
+uschar outerkey[64];
+uschar *finalhash_hex;
+
+if (!key_num)
+ key_num = US"0";
+
+if (Ustrlen(key) > 64)
+ return NULL;
+
+hash_source = string_catn(NULL, key_num, 1);
+hash_source = string_catn(hash_source, daystamp, 3);
+hash_source = string_cat(hash_source, address);
+(void) string_from_gstring(hash_source);
+
+DEBUG(D_expand)
+ debug_printf_indent("prvs: hash source is '%s'\n", hash_source->s);
+
+memset(innerkey, 0x36, 64);
+memset(outerkey, 0x5c, 64);
+
+for (int i = 0; i < Ustrlen(key); i++)
+ {
+ innerkey[i] ^= key[i];
+ outerkey[i] ^= key[i];
+ }
+
+chash_start(HMAC_SHA1, &h);
+chash_mid(HMAC_SHA1, &h, innerkey);
+chash_end(HMAC_SHA1, &h, hash_source->s, hash_source->ptr, innerhash);
+
+chash_start(HMAC_SHA1, &h);
+chash_mid(HMAC_SHA1, &h, outerkey);
+chash_end(HMAC_SHA1, &h, innerhash, 20, finalhash);
+
+/* Hashing is deemed sufficient to de-taint any input data */
+
+p = finalhash_hex = store_get(40, GET_UNTAINTED);
+for (int 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/readsock and after a run expansion.
+It joins the contents of a file onto the output string, globally replacing
+newlines with a given string (optionally).
+
+Arguments:
+ f the FILE
+ yield pointer to the expandable string struct
+ eol newline replacement string, or NULL
+
+Returns: new pointer for expandable string, terminated if non-null
+*/
+
+gstring *
+cat_file(FILE * f, gstring * yield, uschar * eol)
+{
+uschar buffer[1024];
+
+while (Ufgets(buffer, sizeof(buffer), f))
+ {
+ int len = Ustrlen(buffer);
+ if (eol && buffer[len-1] == '\n') len--;
+ yield = string_catn(yield, buffer, len);
+ if (eol && buffer[len])
+ yield = string_cat(yield, eol);
+ }
+return yield;
+}
+
+
+#ifndef DISABLE_TLS
+gstring *
+cat_file_tls(void * tls_ctx, gstring * yield, uschar * eol)
+{
+int rc;
+uschar buffer[1024];
+
+/*XXX could we read direct into a pre-grown string? */
+
+while ((rc = tls_read(tls_ctx, buffer, sizeof(buffer))) > 0)
+ for (uschar * s = buffer; rc--; s++)
+ yield = eol && *s == '\n'
+ ? string_cat(yield, eol) : string_catn(yield, s, 1);
+
+/* We assume that all errors, and any returns of zero bytes,
+are actually EOF. */
+
+return yield;
+}
+#endif
+
+
+/*************************************************
+* 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)
+ if (endket)
+ if (*s != ')')
+ *error = US"expecting closing parenthesis";
+ else
+ while (isspace(*++s));
+ else if (*s)
+ *error = US"expecting operator";
+*sptr = s;
+return x;
+}
+
+
+static int_eximarith_t
+eval_number(uschar **sptr, BOOL decimal, uschar **error)
+{
+int c;
+int_eximarith_t n;
+uschar *s = *sptr;
+
+if (isdigit((c = Uskip_whitespace(&s))))
+ {
+ 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;
+ }
+ Uskip_whitespace(&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;
+Uskip_whitespace(&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)
+ {
+ while (*s == '*' || *s == '/' || *s == '%')
+ {
+ int op = *s++;
+ int_eximarith_t y = eval_op_unary(&s, decimal, error);
+ if (*error) 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 yield 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)
+ {
+ while (*s == '+' || *s == '-')
+ {
+ int op = *s++;
+ int_eximarith_t y = eval_op_mult(&s, decimal, error);
+ if (*error) break;
+ if ( (x >= EXIM_ARITH_MAX/2 && x >= EXIM_ARITH_MAX/2)
+ || (x <= -(EXIM_ARITH_MAX/2) && y <= -(EXIM_ARITH_MAX/2)))
+ { /* over-conservative check */
+ *error = op == '+'
+ ? US"overflow in sum" : US"overflow in difference";
+ 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)
+ {
+ while ((*s == '<' || *s == '>') && s[1] == s[0])
+ {
+ int_eximarith_t y;
+ int op = *s++;
+ s++;
+ y = eval_op_sum(&s, decimal, error);
+ if (*error) 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)
+ {
+ while (*s == '&')
+ {
+ int_eximarith_t y;
+ s++;
+ y = eval_op_shift(&s, decimal, error);
+ if (*error) 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)
+ {
+ while (*s == '^')
+ {
+ int_eximarith_t y;
+ s++;
+ y = eval_op_and(&s, decimal, error);
+ if (*error) 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)
+ {
+ while (*s == '|')
+ {
+ int_eximarith_t y;
+ s++;
+ y = eval_op_xor(&s, decimal, error);
+ if (*error) break;
+ x |= y;
+ }
+ }
+*sptr = s;
+return x;
+}
+
+
+
+/************************************************/
+/* Comparison operation for sort expansion. We need to avoid
+re-expanding the fields being compared, so need a custom routine.
+
+Arguments:
+ cond_type Comparison operator code
+ leftarg, rightarg Arguments for comparison
+
+Return true iff (leftarg compare rightarg)
+*/
+
+static BOOL
+sortsbefore(int cond_type, BOOL alpha_cond,
+ const uschar * leftarg, const uschar * rightarg)
+{
+int_eximarith_t l_num, r_num;
+
+if (!alpha_cond)
+ {
+ l_num = expanded_string_integer(leftarg, FALSE);
+ if (expand_string_message) return FALSE;
+ r_num = expanded_string_integer(rightarg, FALSE);
+ if (expand_string_message) return FALSE;
+
+ switch (cond_type)
+ {
+ case ECOND_NUM_G: return l_num > r_num;
+ case ECOND_NUM_GE: return l_num >= r_num;
+ case ECOND_NUM_L: return l_num < r_num;
+ case ECOND_NUM_LE: return l_num <= r_num;
+ default: break;
+ }
+ }
+else
+ switch (cond_type)
+ {
+ case ECOND_STR_LT: return Ustrcmp (leftarg, rightarg) < 0;
+ case ECOND_STR_LTI: return strcmpic(leftarg, rightarg) < 0;
+ case ECOND_STR_LE: return Ustrcmp (leftarg, rightarg) <= 0;
+ case ECOND_STR_LEI: return strcmpic(leftarg, rightarg) <= 0;
+ case ECOND_STR_GT: return Ustrcmp (leftarg, rightarg) > 0;
+ case ECOND_STR_GTI: return strcmpic(leftarg, rightarg) > 0;
+ case ECOND_STR_GE: return Ustrcmp (leftarg, rightarg) >= 0;
+ case ECOND_STR_GEI: return strcmpic(leftarg, rightarg) >= 0;
+ default: break;
+ }
+return FALSE; /* should not happen */
+}
+
+
+/* Expand a named list. Return false on failure. */
+static gstring *
+expand_listnamed(gstring * yield, const uschar * name, const uschar * listtype)
+{
+tree_node *t = NULL;
+const uschar * list;
+int sep = 0;
+uschar * item;
+BOOL needsep = FALSE;
+#define LISTNAMED_BUF_SIZE 256
+uschar b[LISTNAMED_BUF_SIZE];
+uschar * buffer = b;
+
+if (*name == '+') name++;
+if (!listtype) /* no-argument version */
+ {
+ if ( !(t = tree_search(addresslist_anchor, name))
+ && !(t = tree_search(domainlist_anchor, name))
+ && !(t = tree_search(hostlist_anchor, name)))
+ t = tree_search(localpartlist_anchor, name);
+ }
+else switch(*listtype) /* specific list-type version */
+ {
+ case 'a': t = tree_search(addresslist_anchor, name); break;
+ case 'd': t = tree_search(domainlist_anchor, name); break;
+ case 'h': t = tree_search(hostlist_anchor, name); break;
+ case 'l': t = tree_search(localpartlist_anchor, name); break;
+ default:
+ expand_string_message = US"bad suffix on \"list\" operator";
+ return yield;
+ }
+
+if(!t)
+ {
+ expand_string_message = string_sprintf("\"%s\" is not a %snamed list",
+ name, !listtype?""
+ : *listtype=='a'?"address "
+ : *listtype=='d'?"domain "
+ : *listtype=='h'?"host "
+ : *listtype=='l'?"localpart "
+ : 0);
+ return yield;
+ }
+
+list = ((namedlist_block *)(t->data.ptr))->string;
+
+/* The list could be quite long so we (re)use a buffer for each element
+rather than getting each in new memory */
+
+if (is_tainted(list)) buffer = store_get(LISTNAMED_BUF_SIZE, GET_TAINTED);
+while ((item = string_nextinlist(&list, &sep, buffer, LISTNAMED_BUF_SIZE)))
+ {
+ uschar * buf = US" : ";
+ if (needsep)
+ yield = string_catn(yield, buf, 3);
+ else
+ needsep = TRUE;
+
+ if (*item == '+') /* list item is itself a named list */
+ {
+ yield = expand_listnamed(yield, item, listtype);
+ if (expand_string_message)
+ return yield;
+ }
+
+ else if (sep != ':') /* item from non-colon-sep list, re-quote for colon list-separator */
+ {
+ char tok[3];
+ tok[0] = sep; tok[1] = ':'; tok[2] = 0;
+
+ for(char * cp; cp = strpbrk(CCS item, tok); item = US cp)
+ {
+ yield = string_catn(yield, item, cp - CS item);
+ if (*cp++ == ':') /* colon in a non-colon-sep list item, needs doubling */
+ yield = string_catn(yield, US"::", 2);
+ else /* sep in item; should already be doubled; emit once */
+ {
+ yield = string_catn(yield, US tok, 1);
+ if (*cp == sep) cp++;
+ }
+ }
+ yield = string_cat(yield, item);
+ }
+ else
+ yield = string_cat(yield, item);
+ }
+return yield;
+}
+
+
+
+/************************************************/
+static void
+debug_expansion_interim(const uschar * what, const uschar * value, int nchar,
+ BOOL skipping)
+{
+DEBUG(D_noutf8)
+ debug_printf_indent("|");
+else
+ debug_printf_indent(UTF8_VERT_RIGHT);
+
+for (int fill = 11 - Ustrlen(what); fill > 0; fill--)
+ DEBUG(D_noutf8)
+ debug_printf("-");
+ else
+ debug_printf(UTF8_HORIZ);
+
+debug_printf("%s: %.*s\n", what, nchar, value);
+if (is_tainted(value))
+ {
+ DEBUG(D_noutf8)
+ debug_printf_indent("%s \\__", skipping ? "| " : " ");
+ else
+ debug_printf_indent("%s",
+ skipping
+ ? UTF8_VERT " " : " " UTF8_UP_RIGHT UTF8_HORIZ UTF8_HORIZ);
+ debug_printf("(tainted)\n");
+ }
+}
+
+
+/*************************************************
+* 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 distinguished 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(const uschar *string, BOOL ket_ends, const uschar **left,
+ BOOL skipping, BOOL honour_dollar, BOOL *resetok_p)
+{
+rmark reset_point = store_mark();
+gstring * yield = string_get(Ustrlen(string) + 64);
+int item_type;
+const uschar * s = string;
+const uschar * save_expand_nstring[EXPAND_MAXN+1];
+int save_expand_nlength[EXPAND_MAXN+1];
+BOOL resetok = TRUE, first = TRUE;
+
+expand_level++;
+f.expand_string_forcedfail = FALSE;
+expand_string_message = US"";
+
+if (is_tainted(string))
+ {
+ expand_string_message =
+ string_sprintf("attempt to expand tainted string '%s'", s);
+ log_write(0, LOG_MAIN|LOG_PANIC, "%s", expand_string_message);
+ goto EXPAND_FAILED;
+ }
+
+while (*s)
+ {
+ uschar name[256];
+
+ DEBUG(D_expand)
+ {
+ DEBUG(D_noutf8)
+ debug_printf_indent("%c%s: %s\n",
+ first ? '/' : '|',
+ skipping ? "---scanning" : "considering", s);
+ else
+ debug_printf_indent("%s%s: %s\n",
+ first ? UTF8_DOWN_RIGHT : UTF8_VERT_RIGHT,
+ skipping
+ ? UTF8_HORIZ UTF8_HORIZ UTF8_HORIZ "scanning"
+ : "considering",
+ s);
+ first = FALSE;
+ }
+
+ /* \ 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')
+ {
+ const uschar * t = s + 2;
+ for (s = t; *s ; s++) if (*s == '\\' && s[1] == 'N') break;
+
+ DEBUG(D_expand)
+ debug_expansion_interim(US"protected", t, (int)(s - t), skipping);
+ yield = string_catn(yield, t, s - t);
+ if (*s) s += 2;
+ }
+ else
+ {
+ uschar ch[1];
+ DEBUG(D_expand)
+ DEBUG(D_noutf8)
+ debug_printf_indent("|backslashed: '\\%c'\n", s[1]);
+ else
+ debug_printf_indent(UTF8_VERT_RIGHT "backslashed: '\\%c'\n", s[1]);
+ ch[0] = string_interpret_escape(&s);
+ s++;
+ yield = string_catn(yield, 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)
+ {
+ int i = 1; /*{*/
+ for (const uschar * t = s+1;
+ *t && *t != '$' && *t != '}' && *t != '\\'; t++) i++;
+
+ DEBUG(D_expand) debug_expansion_interim(US"text", s, i, skipping);
+
+ yield = string_catn(yield, s, i);
+ s += i;
+ 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))
+ {
+ const uschar * value;
+ int newsize = 0, len;
+ gstring * g = NULL;
+ uschar * t;
+
+ s = read_name(name, sizeof(name), s, US"_");
+
+ /* If this is the first thing to be expanded, release the pre-allocated
+ buffer. */
+
+ if (!yield)
+ g = store_get(sizeof(gstring), GET_UNTAINTED);
+ else if (yield->ptr == 0)
+ {
+ if (resetok) reset_point = store_reset(reset_point);
+ yield = NULL;
+ reset_point = store_mark();
+ g = store_get(sizeof(gstring), GET_UNTAINTED); /* alloc _before_ calling find_variable() */
+ }
+
+ /* Header */
+
+ if ( ( *(t = name) == 'h'
+ || (*t == 'r' || *t == 'l' || *t == 'b') && *++t == 'h'
+ )
+ && (*++t == '_' || Ustrncmp(t, "eader_", 6) == 0)
+ )
+ {
+ unsigned flags = *name == 'r' ? FH_WANT_RAW
+ : *name == 'l' ? FH_WANT_RAW|FH_WANT_LIST
+ : 0;
+ const uschar * charset = *name == 'b' ? NULL : headers_charset;
+
+ s = read_header_name(name, sizeof(name), s);
+ value = find_header(name, &newsize, flags, 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)
+ { /*{*/
+ if (Ustrchr(name, '}')) malformed_header = TRUE;
+ continue;
+ }
+ }
+
+ /* Variable */
+
+ else if (!(value = find_variable(name, FALSE, skipping, &newsize)))
+ {
+ 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. We need to use a gstring
+ structure that is not allocated after that new-buffer, else a later store
+ reset in the middle of the buffer will make it inaccessible. */
+
+ len = Ustrlen(value);
+ if (!yield && newsize != 0)
+ {
+ yield = g;
+ yield->size = newsize;
+ yield->ptr = len;
+ yield->s = US value; /* known to be in new store i.e. a copy, so deconst safe */
+ }
+ else
+ yield = string_catn(yield, value, len);
+
+ continue;
+ }
+
+ if (isdigit(*s))
+ {
+ int n;
+ s = read_cnumber(&n, s);
+ if (n >= 0 && n <= expand_nmax)
+ yield = string_catn(yield, 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_cnumber(&n, s); /*{{*/
+ if (*s++ != '}')
+ {
+ expand_string_message = US"} expected after number";
+ goto EXPAND_FAILED;
+ }
+ if (n >= 0 && n <= expand_nmax)
+ yield = string_catn(yield, 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, nelem(item_table));
+
+ /* Switch on item type. All nondefault choices should "continue* when
+ skipping, but "break" otherwise so we get debug output for the item
+ expansion. */
+ {
+ int start = gstring_length(yield);
+ 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;
+ int rc;
+
+ switch(read_subs(sub, nelem(sub), 1, &s, skipping, TRUE, name,
+ &resetok))
+ {
+ case 1: goto EXPAND_FAILED_CURLY;
+ case 2:
+ case 3: goto EXPAND_FAILED;
+ }
+ if (skipping) continue;
+
+ resetok = FALSE;
+ switch(rc = eval_acl(sub, nelem(sub), &user_msg))
+ {
+ case OK:
+ case FAIL:
+ DEBUG(D_expand)
+ debug_printf_indent("acl expansion yield: %s\n", user_msg);
+ if (user_msg)
+ yield = string_cat(yield, user_msg);
+ break;
+
+ case DEFER:
+ f.expand_string_forcedfail = TRUE;
+ /*FALLTHROUGH*/
+ default:
+ expand_string_message = string_sprintf("%s from acl \"%s\"",
+ rc_names[rc], sub[0]);
+ goto EXPAND_FAILED;
+ }
+ break;
+ }
+
+ case EITEM_AUTHRESULTS:
+ /* ${authresults {mysystemname}} */
+ {
+ uschar * sub_arg[1];
+
+ switch(read_subs(sub_arg, nelem(sub_arg), 1, &s, skipping, TRUE, name,
+ &resetok))
+ {
+ case 1: goto EXPAND_FAILED_CURLY;
+ case 2:
+ case 3: goto EXPAND_FAILED;
+ }
+
+ yield = string_append(yield, 3,
+ US"Authentication-Results: ", sub_arg[0], US"; none");
+ yield->ptr -= 6;
+
+ yield = authres_local(yield, sub_arg[0]);
+ yield = authres_iprev(yield);
+ yield = authres_smtpauth(yield);
+#ifdef SUPPORT_SPF
+ yield = authres_spf(yield);
+#endif
+#ifndef DISABLE_DKIM
+ yield = authres_dkim(yield);
+#endif
+#ifdef SUPPORT_DMARC
+ yield = authres_dmarc(yield);
+#endif
+#ifdef EXPERIMENTAL_ARC
+ yield = authres_arc(yield);
+#endif
+ break;
+ }
+
+ /* 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;
+ const uschar *next_s;
+ int save_expand_nmax =
+ save_expand_strings(save_expand_nstring, save_expand_nlength);
+ uschar * save_lookup_value = lookup_value;
+
+ Uskip_whitespace(&s);
+ if (!(next_s = eval_condition(s, &resetok, skipping ? NULL : &cond)))
+ goto EXPAND_FAILED; /* message already set */
+
+ DEBUG(D_expand)
+ {
+ debug_expansion_interim(US"condition", s, (int)(next_s - s), skipping);
+ debug_expansion_interim(US"result",
+ cond ? US"true" : US"false", cond ? 4 : 5, skipping);
+ }
+
+ 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 */
+ 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. */
+
+ lookup_value = save_lookup_value;
+ restore_expand_strings(save_expand_nmax, save_expand_nstring,
+ save_expand_nlength);
+ break;
+ }
+
+#ifdef SUPPORT_I18N
+ case EITEM_IMAPFOLDER:
+ { /* ${imapfolder {name}{sep}{specials}} */
+ uschar *sub_arg[3];
+ uschar *encoded;
+
+ switch(read_subs(sub_arg, nelem(sub_arg), 1, &s, skipping, TRUE, name,
+ &resetok))
+ {
+ case 1: goto EXPAND_FAILED_CURLY;
+ case 2:
+ case 3: goto EXPAND_FAILED;
+ }
+
+ if (!sub_arg[1]) /* One argument */
+ {
+ sub_arg[1] = US"/"; /* default separator */
+ sub_arg[2] = NULL;
+ }
+ else if (Ustrlen(sub_arg[1]) != 1)
+ {
+ expand_string_message =
+ string_sprintf(
+ "IMAP folder separator must be one character, found \"%s\"",
+ sub_arg[1]);
+ goto EXPAND_FAILED;
+ }
+
+ if (skipping) continue;
+
+ if (!(encoded = imap_utf7_encode(sub_arg[0], headers_charset,
+ sub_arg[1][0], sub_arg[2], &expand_string_message)))
+ goto EXPAND_FAILED;
+ yield = string_cat(yield, encoded);
+ break;
+ }
+#endif
+
+ /* 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;
+ const uschar * affix, * opts;
+ uschar * save_lookup_value = lookup_value;
+ int save_expand_nmax =
+ save_expand_strings(save_expand_nstring, save_expand_nlength);
+
+ if (expand_forbid & RDO_LOOKUP)
+ {
+ 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. */
+
+ if (Uskip_whitespace(&s) == '{') /*}*/
+ {
+ key = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, &resetok);
+ if (!key) goto EXPAND_FAILED; /*{{*/
+ if (*s++ != '}')
+ {
+ expand_string_message = US"missing '}' after lookup key";
+ goto EXPAND_FAILED_CURLY;
+ }
+ Uskip_whitespace(&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 && *s != '{' && !isspace(*s)) /*}*/
+ {
+ if (nameptr < sizeof(name) - 1) name[nameptr++] = *s;
+ s++;
+ }
+ name[nameptr] = '\0';
+ Uskip_whitespace(&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. */
+
+ if ((stype = search_findtype_partial(name, &partial, &affix, &affixlen,
+ &starflags, &opts)) < 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)
+ {
+ expand_string_message = string_sprintf("missing {key} for single-"
+ "key \"%s\" lookup", name);
+ goto EXPAND_FAILED;
+ }
+ }
+ else if (key)
+ {
+ 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 != '{')
+ {
+ expand_string_message = US"missing '{' for lookup file-or-query arg";
+ goto EXPAND_FAILED_CURLY; /*}}*/
+ }
+ if (!(filename = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, &resetok)))
+ goto EXPAND_FAILED;
+ /*{{*/
+ if (*s++ != '}')
+ {
+ expand_string_message = US"missing '}' closing lookup file-or-query arg";
+ goto EXPAND_FAILED_CURLY;
+ }
+ Uskip_whitespace(&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)
+ key = search_args(stype, name, filename, &filename, opts);
+
+ /* 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)
+ {
+ expand_string_message = search_error_message;
+ goto EXPAND_FAILED;
+ }
+ lookup_value = search_find(handle, filename, key, partial, affix,
+ affixlen, starflags, &expand_setup, opts);
+ if (f.search_find_defer)
+ {
+ expand_string_message =
+ string_sprintf("lookup of \"%s\" gave DEFER: %s",
+ string_printing2(key, SP_TAB), 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 */
+ 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);
+
+ if (skipping) continue;
+ break;
+ }
+
+ /* 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];
+ gstring * new_yield;
+
+ if (expand_forbid & RDO_PERL)
+ {
+ 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,
+ name, &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)
+ {
+ 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");
+ if ((initerror = init_perl(opt_perl_startup)))
+ {
+ 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, &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)
+ {
+ if (!expand_string_message)
+ {
+ expand_string_message =
+ string_sprintf("Perl subroutine \"%s\" returned undef to force "
+ "failure", sub_arg[0]);
+ f.expand_string_forcedfail = TRUE;
+ }
+ goto EXPAND_FAILED;
+ }
+
+ /* Yield succeeded. Ensure forcedfail is unset, just in case it got
+ set during a callback from Perl. */
+
+ f.expand_string_forcedfail = FALSE;
+ yield = new_yield;
+ break;
+ }
+#endif /* EXIM_PERL */
+
+ /* Transform email address to "prvs" scheme to use
+ as BATV-signed return path */
+
+ case EITEM_PRVS:
+ {
+ uschar * sub_arg[3], * p, * domain;
+
+ switch(read_subs(sub_arg, 3, 2, &s, skipping, TRUE, name, &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 */
+ if ( !(domain = Ustrrchr(sub_arg[0],'@'))
+ || 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 third argument must be a single-digit
+ key number, or unset. */
+
+ if ( sub_arg[2]
+ && (!isdigit(sub_arg[2][0]) || sub_arg[2][1] != 0))
+ {
+ expand_string_message = US"prvs third 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)
+ {
+ expand_string_message = US"prvs hmac-sha1 conversion failed";
+ goto EXPAND_FAILED;
+ }
+
+ /* Now separate the domain from the local part */
+ *domain++ = '\0';
+
+ yield = string_catn(yield, US"prvs=", 5);
+ yield = string_catn(yield, sub_arg[2] ? sub_arg[2] : US"0", 1);
+ yield = string_catn(yield, prvs_daystamp(7), 3);
+ yield = string_catn(yield, p, 6);
+ yield = string_catn(yield, US"=", 1);
+ yield = string_cat (yield, sub_arg[0]);
+ yield = string_catn(yield, US"@", 1);
+ yield = string_cat (yield, domain);
+
+ break;
+ }
+
+ /* Check a prvs-encoded address for validity */
+
+ case EITEM_PRVSCHECK:
+ {
+ uschar * sub_arg[3], * p;
+ gstring * g;
+ const pcre2_code * re;
+
+ /* 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, name, &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_indent("prvscheck localpart: %s\n", local_part);
+ DEBUG(D_expand) debug_printf_indent("prvscheck key number: %s\n", key_num);
+ DEBUG(D_expand) debug_printf_indent("prvscheck daystamp: %s\n", daystamp);
+ DEBUG(D_expand) debug_printf_indent("prvscheck hash: %s\n", hash);
+ DEBUG(D_expand) debug_printf_indent("prvscheck domain: %s\n", domain);
+
+ /* Set up expansion variables */
+ g = string_cat (NULL, local_part);
+ g = string_catn(g, US"@", 1);
+ g = string_cat (g, domain);
+ prvscheck_address = string_from_gstring(g);
+ prvscheck_keynum = string_copy(key_num);
+
+ /* Now expand the second argument */
+ switch(read_subs(sub_arg, 1, 1, &s, skipping, FALSE, name, &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)
+ {
+ expand_string_message = US"hmac-sha1 conversion failed";
+ goto EXPAND_FAILED;
+ }
+
+ DEBUG(D_expand) debug_printf_indent("prvscheck: received hash is %s\n", hash);
+ DEBUG(D_expand) debug_printf_indent("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 occurred.
+ Adjust "inow" accordingly. */
+ if ( (iexpire < 7) && (inow >= 993) ) inow = 0;
+
+ if (iexpire >= inow)
+ {
+ prvscheck_result = US"1";
+ DEBUG(D_expand) debug_printf_indent("prvscheck: success, $pvrs_result set to 1\n");
+ }
+ else
+ {
+ prvscheck_result = NULL;
+ DEBUG(D_expand) debug_printf_indent("prvscheck: signature expired, $pvrs_result unset\n");
+ }
+ }
+ else
+ {
+ prvscheck_result = NULL;
+ DEBUG(D_expand) debug_printf_indent("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, name, &resetok))
+ {
+ case 1: goto EXPAND_FAILED_CURLY;
+ case 2:
+ case 3: goto EXPAND_FAILED;
+ }
+
+ yield = string_cat(yield,
+ !sub_arg[0] || !*sub_arg[0] ? prvscheck_address : 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, name, &resetok))
+ {
+ case 1: goto EXPAND_FAILED_CURLY;
+ case 2:
+ case 3: goto EXPAND_FAILED;
+ }
+
+ if (skipping) continue;
+ break;
+ }
+
+ /* 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, name, &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 */
+
+ if (!(f = Ufopen(sub_arg[0], "rb")))
+ {
+ expand_string_message = string_open_failed("%s", sub_arg[0]);
+ goto EXPAND_FAILED;
+ }
+
+ yield = cat_file(f, yield, sub_arg[1]);
+ (void)fclose(f);
+ break;
+ }
+
+ /* Handle "readsocket" to insert data from a socket, either
+ Inet or Unix domain */
+
+ case EITEM_READSOCK:
+ {
+ uschar * arg;
+ uschar * sub_arg[4];
+
+ if (expand_forbid & RDO_READSOCK)
+ {
+ 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, name, &resetok))
+ {
+ case 1: goto EXPAND_FAILED_CURLY;
+ case 2: /* Won't occur: no end check */
+ case 3: goto EXPAND_FAILED;
+ }
+
+ /* If skipping, we don't actually do anything. Otherwise, arrange to
+ connect to either an IP or a Unix socket. */
+
+ if (!skipping)
+ {
+ int stype = search_findtype(US"readsock", 8);
+ gstring * g = NULL;
+ void * handle;
+ int expand_setup = -1;
+ uschar * s;
+
+ /* If the reqstr is empty, flag that and set a dummy */
+
+ if (!sub_arg[1][0])
+ {
+ g = string_append_listele(g, ',', US"send=no");
+ sub_arg[1] = US"DUMMY";
+ }
+
+ /* Re-marshall the options */
+
+ if (sub_arg[2])
+ {
+ const uschar * list = sub_arg[2];
+ uschar * item;
+ int sep = 0;
+
+ /* First option has no tag and is timeout */
+ if ((item = string_nextinlist(&list, &sep, NULL, 0)))
+ g = string_append_listele(g, ',',
+ string_sprintf("timeout=%s", item));
+
+ /* The rest of the options from the expansion */
+ while ((item = string_nextinlist(&list, &sep, NULL, 0)))
+ g = string_append_listele(g, ',', item);
+
+ /* possibly plus an EOL string. Process with escapes, to protect
+ from list-processing. The only current user of eol= in search
+ options is the readsock expansion. */
+
+ if (sub_arg[3] && *sub_arg[3])
+ g = string_append_listele(g, ',',
+ string_sprintf("eol=%s",
+ string_printing2(sub_arg[3], SP_TAB|SP_SPACE)));
+ }
+
+ /* Gat a (possibly cached) handle for the connection */
+
+ if (!(handle = search_open(sub_arg[0], stype, 0, NULL, NULL)))
+ {
+ if (*expand_string_message) goto EXPAND_FAILED;
+ expand_string_message = search_error_message;
+ search_error_message = NULL;
+ goto SOCK_FAIL;
+ }
+
+ /* Get (possibly cached) results for the lookup */
+ /* sspec: sub_arg[0] req: sub_arg[1] opts: g */
+
+ if ((s = search_find(handle, sub_arg[0], sub_arg[1], -1, NULL, 0, 0,
+ &expand_setup, string_from_gstring(g))))
+ yield = string_cat(yield, s);
+ else if (f.search_find_defer)
+ {
+ expand_string_message = search_error_message;
+ search_error_message = NULL;
+ goto SOCK_FAIL;
+ }
+ else
+ { /* should not happen, at present */
+ expand_string_message = search_error_message;
+ search_error_message = NULL;
+ 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))
+ goto EXPAND_FAILED; /*{*/
+ if (*s++ != '}')
+ { /*{*/
+ expand_string_message = US"missing '}' closing failstring for readsocket";
+ goto EXPAND_FAILED_CURLY;
+ }
+ Uskip_whitespace(&s);
+ }
+
+ READSOCK_DONE: /*{*/
+ if (*s++ != '}')
+ { /*{*/
+ expand_string_message = US"missing '}' closing readsocket";
+ goto EXPAND_FAILED_CURLY;
+ }
+ if (skipping) continue;
+ break;
+
+ /* 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);
+ if (!(arg = expand_string_internal(s+1, TRUE, &s, FALSE, TRUE, &resetok)))
+ goto EXPAND_FAILED;
+ yield = string_cat(yield, arg); /*{*/
+ if (*s++ != '}')
+ { /*{*/
+ expand_string_message = US"missing '}' closing failstring for readsocket";
+ goto EXPAND_FAILED_CURLY;
+ }
+ Uskip_whitespace(&s);
+ goto READSOCK_DONE;
+ }
+
+ /* Handle "run" to execute a program. */
+
+ case EITEM_RUN:
+ {
+ FILE * f;
+ const uschar * arg, ** argv;
+ BOOL late_expand = TRUE;
+
+ if ((expand_forbid & RDO_RUN) != 0)
+ {
+ expand_string_message = US"running a command is not permitted";
+ goto EXPAND_FAILED;
+ }
+
+ /* Handle options to the "run" */
+
+ while (*s == ',')
+ {
+ if (Ustrncmp(++s, "preexpand", 9) == 0)
+ { late_expand = FALSE; s += 9; }
+ else
+ {
+ const uschar * t = s;
+ while (isalpha(*++t)) ;
+ expand_string_message = string_sprintf("bad option '%.*s' for run",
+ (int)(t-s), s);
+ goto EXPAND_FAILED;
+ }
+ }
+ Uskip_whitespace(&s);
+
+ if (*s != '{') /*}*/
+ {
+ expand_string_message = US"missing '{' for command arg of run";
+ goto EXPAND_FAILED_CURLY; /*"}*/
+ }
+ s++;
+
+ if (late_expand) /* this is the default case */
+ {
+ int n = Ustrcspn(s, "}");
+ arg = skipping ? NULL : string_copyn(s, n);
+ s += n;
+ }
+ else
+ {
+ if (!(arg = expand_string_internal(s, TRUE, &s, skipping, TRUE, &resetok)))
+ goto EXPAND_FAILED;
+ Uskip_whitespace(&s);
+ }
+ /*{*/
+ if (*s++ != '}')
+ { /*{*/
+ expand_string_message = US"missing '}' closing command arg of run";
+ goto EXPAND_FAILED_CURLY;
+ }
+
+ if (skipping) /* Just pretend it worked when we're skipping */
+ {
+ runrc = 0;
+ lookup_value = NULL;
+ }
+ else
+ {
+ int fd_in, fd_out;
+ pid_t pid;
+
+ if (!transport_set_up_command(&argv, /* anchor for arg list */
+ arg, /* raw command */
+ late_expand, /* expand args if not already done */
+ 0, /* not relevant when... */
+ NULL, /* no transporting address */
+ late_expand, /* allow tainted args, when expand-after-split */
+ 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. */
+
+ if ((pid = child_open(USS argv, NULL, 0077, &fd_in, &fd_out, TRUE,
+ US"expand-run")) < 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. Remember to not release
+ memory just allocated for $value. */
+
+ resetok = FALSE;
+ f = fdopen(fd_out, "rb");
+ sigalrm_seen = FALSE;
+ ALARM(60);
+ lookup_value = string_from_gstring(cat_file(f, NULL, NULL));
+ ALARM_CLR(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 || (runrc = child_close(pid, 30)) < 0)
+ {
+ if (sigalrm_seen || runrc == -256)
+ {
+ expand_string_message = US"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 */
+ 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 */
+ }
+
+ if (skipping) continue;
+ break;
+ }
+
+ /* Handle character translation for "tr" */
+
+ case EITEM_TR:
+ {
+ int oldptr = gstring_length(yield);
+ int o2m;
+ uschar * sub[3];
+
+ switch(read_subs(sub, 3, 3, &s, skipping, TRUE, name, &resetok))
+ {
+ case 1: goto EXPAND_FAILED_CURLY;
+ case 2:
+ case 3: goto EXPAND_FAILED;
+ }
+
+ yield = string_cat(yield, sub[0]);
+ o2m = Ustrlen(sub[2]) - 1;
+
+ if (o2m >= 0) for (; oldptr < yield->ptr; oldptr++)
+ {
+ uschar *m = Ustrrchr(sub[1], yield->s[oldptr]);
+ if (m)
+ {
+ int o = m - sub[1];
+ yield->s[oldptr] = sub[2][(o < o2m)? o : o2m];
+ }
+ }
+
+ if (skipping) continue;
+ break;
+ }
+
+ /* 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 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])
+ {
+ sub[2] = sub[1];
+ sub[1] = NULL;
+ if (item_type == EITEM_LENGTH)
+ {
+ sub[1] = sub[0];
+ sub[0] = NULL;
+ }
+ }
+
+ for (int i = 0; i < 2; i++) if (sub[i])
+ {
+ 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)
+ goto EXPAND_FAILED;
+ yield = string_catn(yield, ret, len);
+ if (skipping) continue;
+ break;
+ }
+
+ /* 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;
+ hctx sha1_ctx;
+ void * use_base;
+ int type;
+ 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 (skipping) continue;
+
+ 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_ctx;
+ 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 (int 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 (int 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, sub[2], hashlen*2, finalhash_hex);
+
+ yield = string_catn(yield, finalhash_hex, hashlen*2);
+ break;
+ }
+
+ /* 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 pcre2_code * re;
+ int moffset, moffsetextra, slen;
+ PCRE2_SIZE roffset;
+ pcre2_match_data * md;
+ int err, emptyopt;
+ uschar * subject, * sub[3];
+ int save_expand_nmax =
+ save_expand_strings(save_expand_nstring, save_expand_nlength);
+
+ switch(read_subs(sub, 3, 3, &s, skipping, TRUE, name, &resetok))
+ {
+ case 1: goto EXPAND_FAILED_CURLY;
+ case 2:
+ case 3: goto EXPAND_FAILED;
+ }
+
+ /*XXX no handling of skipping? */
+ /* Compile the regular expression */
+
+ if (!(re = pcre2_compile((PCRE2_SPTR)sub[1], PCRE2_ZERO_TERMINATED,
+ PCRE_COPT, &err, &roffset, pcre_cmp_ctx)))
+ {
+ uschar errbuf[128];
+ pcre2_get_error_message(err, errbuf, sizeof(errbuf));
+ expand_string_message = string_sprintf("regular expression error in "
+ "\"%s\": %s at offset %ld", sub[1], errbuf, (long)roffset);
+ goto EXPAND_FAILED;
+ }
+ md = pcre2_match_data_create(EXPAND_MAXN + 1, pcre_gen_ctx);
+
+ /* 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 (;;)
+ {
+ PCRE2_SIZE * ovec = pcre2_get_ovector_pointer(md);
+ int n = pcre2_match(re, (PCRE2_SPTR)subject, slen, moffset + moffsetextra,
+ PCRE_EOPT | emptyopt, md, pcre_mtc_ctx);
+ 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_catn(yield, subject+moffset, slen-moffset);
+ break;
+ }
+
+ /* Match - set up for expanding the replacement. */
+ DEBUG(D_expand) debug_printf_indent("%s: match\n", name);
+
+ if (n == 0) n = EXPAND_MAXN + 1;
+ expand_nmax = 0;
+ for (int nn = 0; nn < n*2; nn += 2)
+ {
+ expand_nstring[expand_nmax] = subject + ovec[nn];
+ expand_nlength[expand_nmax++] = ovec[nn+1] - ovec[nn];
+ }
+ expand_nmax--;
+
+ /* Copy the characters before the match, plus the expanded insertion. */
+
+ yield = string_catn(yield, subject + moffset, ovec[0] - moffset);
+
+ if (!(insert = expand_string(sub[2])))
+ goto EXPAND_FAILED;
+ yield = string_cat(yield, insert);
+
+ moffset = ovec[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 (ovec[0] == ovec[1])
+ {
+ if (ovec[0] == slen) break;
+ emptyopt = PCRE2_NOTEMPTY | PCRE2_ANCHORED;
+ }
+ }
+
+ /* All done - restore numerical variables. */
+
+ restore_expand_strings(save_expand_nmax, save_expand_nstring,
+ save_expand_nlength);
+ if (skipping) continue;
+ break;
+ }
+
+ /* Handle keyed and numbered substring extraction. If the first argument
+ consists entirely of digits, then a numerical extraction is assumed. */
+
+ case EITEM_EXTRACT:
+ {
+ int field_number = 1;
+ BOOL field_number_set = FALSE;
+ uschar * save_lookup_value = lookup_value, * sub[3];
+ int save_expand_nmax =
+ save_expand_strings(save_expand_nstring, save_expand_nlength);
+
+ /* On reflection the original behaviour of extract-json for a string
+ result, leaving it quoted, was a mistake. But it was already published,
+ hence the addition of jsons. In a future major version, make json
+ work like josons, and withdraw jsons. */
+
+ enum {extract_basic, extract_json, extract_jsons} fmt = extract_basic;
+
+ /* Check for a format-variant specifier */
+
+ if (Uskip_whitespace(&s) != '{') /*}*/
+ if (Ustrncmp(s, "json", 4) == 0)
+ if (*(s += 4) == 's')
+ {fmt = extract_jsons; s++;}
+ else
+ fmt = extract_json;
+
+ /* While skipping we cannot rely on the data for expansions being
+ available (eg. $item) hence cannot decide on numeric vs. keyed.
+ Read a maximum of 5 arguments (including the yes/no) */
+
+ if (skipping)
+ {
+ for (int j = 5; j > 0 && *s == '{'; j--) /*'}'*/
+ {
+ if (!expand_string_internal(s+1, TRUE, &s, skipping, TRUE, &resetok))
+ goto EXPAND_FAILED; /*'{'*/
+ if (*s++ != '}')
+ {
+ expand_string_message = US"missing '{' for arg of extract";
+ goto EXPAND_FAILED_CURLY;
+ }
+ Uskip_whitespace(&s);
+ }
+ if ( Ustrncmp(s, "fail", 4) == 0 /*'{'*/
+ && (s[4] == '}' || s[4] == ' ' || s[4] == '\t' || !s[4])
+ )
+ {
+ s += 4;
+ Uskip_whitespace(&s);
+ } /*'{'*/
+ if (*s != '}')
+ {
+ expand_string_message = US"missing '}' closing extract";
+ goto EXPAND_FAILED_CURLY;
+ }
+ }
+
+ else for (int i = 0, j = 2; i < j; i++) /* Read the proper number of arguments */
+ {
+ if (Uskip_whitespace(&s) == '{') /*'}'*/
+ {
+ if (!(sub[i] = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, &resetok)))
+ goto EXPAND_FAILED; /*'{'*/
+ if (*s++ != '}')
+ {
+ expand_string_message = string_sprintf(
+ "missing '}' closing arg %d of extract", i+1);
+ 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 (normal) or 2 (json). */
+
+ if (i == 0)
+ {
+ int len;
+ int x = 0;
+ uschar * p = sub[0];
+
+ Uskip_whitespace(&p);
+ sub[0] = p;
+
+ len = Ustrlen(p);
+ while (len > 0 && isspace(p[len-1])) len--;
+ p[len] = 0;
+
+ if (!*p)
+ {
+ expand_string_message = US"first argument of \"extract\" must "
+ "not be empty";
+ goto EXPAND_FAILED;
+ }
+
+ if (*p == '-')
+ {
+ field_number = -1;
+ p++;
+ }
+ while (*p && isdigit(*p)) x = x * 10 + *p++ - '0';
+ if (!*p)
+ {
+ field_number *= x;
+ if (fmt == extract_basic) j = 3; /* Need 3 args */
+ field_number_set = TRUE;
+ }
+ }
+ }
+ else
+ {
+ expand_string_message = string_sprintf(
+ "missing '{' for arg %d of extract", i+1);
+ goto EXPAND_FAILED_CURLY;
+ }
+ }
+
+ /* Extract either the numbered or the keyed substring into $value. If
+ skipping, just pretend the extraction failed. */
+
+ if (skipping)
+ lookup_value = NULL;
+ else switch (fmt)
+ {
+ case extract_basic:
+ lookup_value = field_number_set
+ ? expand_gettokened(field_number, sub[1], sub[2])
+ : expand_getkeyed(sub[0], sub[1]);
+ break;
+
+ case extract_json:
+ case extract_jsons:
+ {
+ uschar * s, * item;
+ const uschar * list;
+
+ /* Array: Bracket-enclosed and comma-separated.
+ Object: Brace-enclosed, comma-sep list of name:value pairs */
+
+ if (!(s = dewrap(sub[1], field_number_set ? US"[]" : US"{}")))
+ {
+ expand_string_message =
+ string_sprintf("%s wrapping %s for extract json",
+ expand_string_message,
+ field_number_set ? "array" : "object");
+ goto EXPAND_FAILED_CURLY;
+ }
+
+ list = s;
+ if (field_number_set)
+ {
+ if (field_number <= 0)
+ {
+ expand_string_message = US"first argument of \"extract\" must "
+ "be greater than zero";
+ goto EXPAND_FAILED;
+ }
+ while (field_number > 0 && (item = json_nextinlist(&list)))
+ field_number--;
+ if ((lookup_value = s = item))
+ {
+ while (*s) s++;
+ while (--s >= lookup_value && isspace(*s)) *s = '\0';
+ }
+ }
+ else
+ {
+ lookup_value = NULL;
+ while ((item = json_nextinlist(&list)))
+ {
+ /* Item is: string name-sep value. string is quoted.
+ Dequote the string and compare with the search key. */
+
+ if (!(item = dewrap(item, US"\"\"")))
+ {
+ expand_string_message =
+ string_sprintf("%s wrapping string key for extract json",
+ expand_string_message);
+ goto EXPAND_FAILED_CURLY;
+ }
+ if (Ustrcmp(item, sub[0]) == 0) /*XXX should be a UTF8-compare */
+ {
+ s = item + Ustrlen(item) + 1;
+ if (Uskip_whitespace(&s) != ':')
+ {
+ expand_string_message =
+ US"missing object value-separator for extract json";
+ goto EXPAND_FAILED_CURLY;
+ }
+ s++;
+ Uskip_whitespace(&s);
+ lookup_value = s;
+ break;
+ }
+ }
+ }
+ }
+
+ if ( fmt == extract_jsons
+ && lookup_value
+ && !(lookup_value = dewrap(lookup_value, US"\"\"")))
+ {
+ expand_string_message =
+ string_sprintf("%s wrapping string result for extract jsons",
+ expand_string_message);
+ goto EXPAND_FAILED_CURLY;
+ }
+ break; /* json/s */
+ }
+
+ /* 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 */
+ 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);
+
+ if (skipping) continue;
+ break;
+ }
+
+ /* return the Nth item from a list */
+
+ case EITEM_LISTEXTRACT:
+ {
+ int field_number = 1;
+ uschar * save_lookup_value = lookup_value, * sub[2];
+ int save_expand_nmax =
+ save_expand_strings(save_expand_nstring, save_expand_nlength);
+
+ /* Read the field & list arguments */
+
+ for (int i = 0; i < 2; i++)
+ {
+ if (Uskip_whitespace(&s) != '{') /*}*/
+ {
+ expand_string_message = string_sprintf(
+ "missing '{' for arg %d of listextract", i+1); /*}*/
+ goto EXPAND_FAILED_CURLY;
+ }
+
+ sub[i] = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, &resetok);
+ if (!sub[i]) goto EXPAND_FAILED; /*{{*/
+ if (*s++ != '}')
+ {
+ expand_string_message = string_sprintf(
+ "missing '}' closing arg %d of listextract", i+1);
+ 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];
+
+ Uskip_whitespace(&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 */
+ US"listextract", /* 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);
+
+ if (skipping) continue;
+ break;
+ }
+
+ case EITEM_LISTQUOTE:
+ {
+ uschar * sub[2];
+ switch(read_subs(sub, 2, 2, &s, skipping, TRUE, name, &resetok))
+ {
+ case 1: goto EXPAND_FAILED_CURLY;
+ case 2:
+ case 3: goto EXPAND_FAILED;
+ }
+ if (*sub[1]) for (uschar sep = *sub[0], c; c = *sub[1]; sub[1]++)
+ {
+ if (c == sep) yield = string_catn(yield, sub[1], 1);
+ yield = string_catn(yield, sub[1], 1);
+ }
+ else yield = string_catn(yield, US" ", 1);
+ if (skipping) continue;
+ break;
+ }
+
+#ifndef DISABLE_TLS
+ case EITEM_CERTEXTRACT:
+ {
+ uschar * save_lookup_value = lookup_value, * sub[2];
+ int save_expand_nmax =
+ save_expand_strings(save_expand_nstring, save_expand_nlength);
+
+ /* Read the field argument */
+ if (Uskip_whitespace(&s) != '{') /*}*/
+ {
+ expand_string_message = US"missing '{' for field arg of certextract";
+ goto EXPAND_FAILED_CURLY; /*}*/
+ }
+ sub[0] = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, &resetok);
+ if (!sub[0]) goto EXPAND_FAILED; /*{{*/
+ if (*s++ != '}')
+ {
+ expand_string_message = US"missing '}' closing field arg of certextract";
+ goto EXPAND_FAILED_CURLY;
+ }
+ /* strip spaces fore & aft */
+ {
+ int len;
+ uschar *p = sub[0];
+
+ Uskip_whitespace(&p);
+ sub[0] = p;
+
+ len = Ustrlen(p);
+ while (len > 0 && isspace(p[len-1])) len--;
+ p[len] = 0;
+ }
+
+ /* inspect the cert argument */
+ if (Uskip_whitespace(&s) != '{') /*}*/
+ {
+ expand_string_message = US"missing '{' for cert variable arg of certextract";
+ 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++ != '}')
+ {
+ expand_string_message = US"missing '}' closing cert variable arg of certextract";
+ 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 */
+ US"certextract", /* 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);
+ if (skipping) continue;
+ break;
+ }
+#endif /*DISABLE_TLS*/
+
+ /* Handle list operations */
+
+ case EITEM_FILTER:
+ case EITEM_MAP:
+ case EITEM_REDUCE:
+ {
+ int sep = 0, save_ptr = gstring_length(yield);
+ uschar outsep[2] = { '\0', '\0' };
+ const uschar *list, *expr, *temp;
+ uschar * save_iterate_item = iterate_item;
+ uschar * save_lookup_value = lookup_value;
+
+ Uskip_whitespace(&s);
+ if (*s++ != '{') /*}*/
+ {
+ expand_string_message =
+ string_sprintf("missing '{' for first arg of %s", name);
+ goto EXPAND_FAILED_CURLY; /*}*/
+ }
+
+ if (!(list = expand_string_internal(s, TRUE, &s, skipping, TRUE, &resetok)))
+ goto EXPAND_FAILED; /*{{*/
+ if (*s++ != '}')
+ {
+ expand_string_message =
+ string_sprintf("missing '}' closing first arg of %s", name);
+ goto EXPAND_FAILED_CURLY;
+ }
+
+ if (item_type == EITEM_REDUCE)
+ {
+ uschar * t;
+ Uskip_whitespace(&s);
+ if (*s++ != '{') /*}*/
+ {
+ expand_string_message = US"missing '{' for second arg of reduce";
+ goto EXPAND_FAILED_CURLY; /*}*/
+ }
+ t = expand_string_internal(s, TRUE, &s, skipping, TRUE, &resetok);
+ if (!t) goto EXPAND_FAILED;
+ lookup_value = t; /*{{*/
+ if (*s++ != '}')
+ {
+ expand_string_message = US"missing '}' closing second arg of reduce";
+ goto EXPAND_FAILED_CURLY;
+ }
+ }
+
+ Uskip_whitespace(&s);
+ if (*s++ != '{') /*}*/
+ {
+ expand_string_message =
+ string_sprintf("missing '{' for last arg of %s", name); /*}*/
+ 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 = expand_string_internal(s, TRUE, &s, TRUE, TRUE, &resetok);
+ else
+ if ((temp = eval_condition(expr, &resetok, NULL))) s = temp;
+
+ if (!temp)
+ {
+ expand_string_message = string_sprintf("%s inside \"%s\" item",
+ expand_string_message, name);
+ goto EXPAND_FAILED;
+ }
+
+ Uskip_whitespace(&s); /*{{{*/
+ if (*s++ != '}')
+ {
+ expand_string_message = string_sprintf("missing } at end of condition "
+ "or expression inside \"%s\"; could be an unquoted } in the content",
+ name);
+ goto EXPAND_FAILED;
+ }
+
+ Uskip_whitespace(&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)))
+ {
+ *outsep = (uschar)sep; /* Separator as a string */
+
+ DEBUG(D_expand) debug_printf_indent("%s: $item = '%s' $value = '%s'\n",
+ name, iterate_item, lookup_value);
+
+ if (item_type == EITEM_FILTER)
+ {
+ BOOL condresult;
+ if (!eval_condition(expr, &resetok, &condresult))
+ {
+ 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_indent("%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
+ {
+ uschar * t = expand_string_internal(expr, TRUE, NULL, skipping, TRUE, &resetok);
+ temp = t;
+ if (!temp)
+ {
+ 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 = t; /* 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. */
+
+/*XXX is there not a standard support function for this, appending to a list? */
+/* yes, string_append_listele(), but it depends on lack of text before the list */
+
+ if ( yield && yield->ptr != save_ptr
+ && (temp[0] == *outsep || temp[0] == 0))
+ yield = string_catn(yield, 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_catn(yield, 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]) { yield->ptr--; break; }
+ yield = string_catn(yield, outsep, 1);
+ temp += seglen + 1;
+ }
+
+ /* Output a separator after the string: we will remove the redundant
+ final one at the end. */
+
+ yield = string_catn(yield, 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, 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 (yield && yield->ptr != save_ptr) yield->ptr--;
+
+ /* Restore preserved $item */
+
+ iterate_item = save_iterate_item;
+ if (skipping) continue;
+ break;
+ }
+
+ case EITEM_SORT:
+ {
+ int sep = 0, cond_type;
+ const uschar * srclist, * cmp, * xtract;
+ uschar * opname, * srcitem;
+ const uschar * dstlist = NULL, * dstkeylist = NULL;
+ uschar * tmp, * save_iterate_item = iterate_item;
+
+ Uskip_whitespace(&s);
+ if (*s++ != '{') /*}*/
+ {
+ expand_string_message = US"missing '{' for list arg of sort";
+ goto EXPAND_FAILED_CURLY; /*}*/
+ }
+
+ srclist = expand_string_internal(s, TRUE, &s, skipping, TRUE, &resetok);
+ if (!srclist) goto EXPAND_FAILED; /*{{*/
+ if (*s++ != '}')
+ {
+ expand_string_message = US"missing '}' closing list arg of sort";
+ goto EXPAND_FAILED_CURLY;
+ }
+
+ Uskip_whitespace(&s);
+ if (*s++ != '{') /*}*/
+ {
+ expand_string_message = US"missing '{' for comparator arg of sort";
+ goto EXPAND_FAILED_CURLY; /*}*/
+ }
+
+ cmp = expand_string_internal(s, TRUE, &s, skipping, FALSE, &resetok);
+ if (!cmp) goto EXPAND_FAILED; /*{{*/
+ if (*s++ != '}')
+ {
+ expand_string_message = US"missing '}' closing comparator arg of sort";
+ goto EXPAND_FAILED_CURLY;
+ }
+
+ if ((cond_type = identify_operator(&cmp, &opname)) == -1)
+ {
+ if (!expand_string_message)
+ expand_string_message = string_sprintf("unknown condition \"%s\"", s);
+ goto EXPAND_FAILED;
+ }
+ switch(cond_type)
+ {
+ case ECOND_NUM_L: case ECOND_NUM_LE:
+ case ECOND_NUM_G: case ECOND_NUM_GE:
+ case ECOND_STR_GE: case ECOND_STR_GEI: case ECOND_STR_GT: case ECOND_STR_GTI:
+ case ECOND_STR_LE: case ECOND_STR_LEI: case ECOND_STR_LT: case ECOND_STR_LTI:
+ break;
+
+ default:
+ expand_string_message = US"comparator not handled for sort";
+ goto EXPAND_FAILED;
+ }
+
+ Uskip_whitespace(&s);
+ if (*s++ != '{') /*}*/
+ {
+ expand_string_message = US"missing '{' for extractor arg of sort";
+ goto EXPAND_FAILED_CURLY; /*}*/
+ }
+
+ xtract = s;
+ if (!(tmp = expand_string_internal(s, TRUE, &s, TRUE, TRUE, &resetok)))
+ goto EXPAND_FAILED;
+ xtract = string_copyn(xtract, s - xtract);
+ /*{{*/
+ if (*s++ != '}')
+ {
+ expand_string_message = US"missing '}' closing extractor arg of sort";
+ goto EXPAND_FAILED_CURLY;
+ }
+ /*{{*/
+ if (*s++ != '}')
+ {
+ expand_string_message = US"missing } at end of \"sort\"";
+ goto EXPAND_FAILED;
+ }
+
+ if (skipping) continue;
+
+ while ((srcitem = string_nextinlist(&srclist, &sep, NULL, 0)))
+ {
+ uschar * srcfield, * dstitem;
+ gstring * newlist = NULL, * newkeylist = NULL;
+
+ DEBUG(D_expand) debug_printf_indent("%s: $item = \"%s\"\n", name, srcitem);
+
+ /* extract field for comparisons */
+ iterate_item = srcitem;
+ if ( !(srcfield = expand_string_internal(xtract, FALSE, NULL, FALSE,
+ TRUE, &resetok))
+ || !*srcfield)
+ {
+ expand_string_message = string_sprintf(
+ "field-extract in sort: \"%s\"", xtract);
+ goto EXPAND_FAILED;
+ }
+
+ /* Insertion sort */
+
+ /* copy output list until new-item < list-item */
+ while ((dstitem = string_nextinlist(&dstlist, &sep, NULL, 0)))
+ {
+ uschar * dstfield;
+
+ /* field for comparison */
+ if (!(dstfield = string_nextinlist(&dstkeylist, &sep, NULL, 0)))
+ goto SORT_MISMATCH;
+
+ /* String-comparator names start with a letter; numeric names do not */
+
+ if (sortsbefore(cond_type, isalpha(opname[0]),
+ srcfield, dstfield))
+ {
+ /* New-item sorts before this dst-item. Append new-item,
+ then dst-item, then remainder of dst list. */
+
+ newlist = string_append_listele(newlist, sep, srcitem);
+ newkeylist = string_append_listele(newkeylist, sep, srcfield);
+ srcitem = NULL;
+
+ newlist = string_append_listele(newlist, sep, dstitem);
+ newkeylist = string_append_listele(newkeylist, sep, dstfield);
+
+/*XXX why field-at-a-time copy? Why not just dup the rest of the list? */
+ while ((dstitem = string_nextinlist(&dstlist, &sep, NULL, 0)))
+ {
+ if (!(dstfield = string_nextinlist(&dstkeylist, &sep, NULL, 0)))
+ goto SORT_MISMATCH;
+ newlist = string_append_listele(newlist, sep, dstitem);
+ newkeylist = string_append_listele(newkeylist, sep, dstfield);
+ }
+
+ break;
+ }
+
+ newlist = string_append_listele(newlist, sep, dstitem);
+ newkeylist = string_append_listele(newkeylist, sep, dstfield);
+ }
+
+ /* If we ran out of dstlist without consuming srcitem, append it */
+ if (srcitem)
+ {
+ newlist = string_append_listele(newlist, sep, srcitem);
+ newkeylist = string_append_listele(newkeylist, sep, srcfield);
+ }
+
+ dstlist = newlist->s;
+ dstkeylist = newkeylist->s;
+
+ DEBUG(D_expand) debug_printf_indent("%s: dstlist = \"%s\"\n", name, dstlist);
+ DEBUG(D_expand) debug_printf_indent("%s: dstkeylist = \"%s\"\n", name, dstkeylist);
+ }
+
+ if (dstlist)
+ yield = string_cat(yield, dstlist);
+
+ /* Restore preserved $item */
+ iterate_item = save_iterate_item;
+ break;
+
+ SORT_MISMATCH:
+ expand_string_message = US"Internal error in sort (list mismatch)";
+ goto EXPAND_FAILED;
+ }
+
+
+ /* 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)
+ {
+ 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, name, &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. */
+
+ if (!(t = tree_search(dlobj_anchor, argv[0])))
+ {
+ void * handle = dlopen(CS argv[0], RTLD_LAZY);
+ if (!handle)
+ {
+ 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]), 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. */
+
+ if (!(func = (exim_dlfunc_t *)dlsym(t->data.ptr, CS argv[1])))
+ {
+ 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]; argc++) ;
+
+ if ((status = func(&result, argc - 2, &argv[2])) != OK)
+ {
+ expand_string_message = result ? result : US"(no message)";
+ if (status == FAIL_FORCED)
+ f.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;
+ }
+
+ if (result) yield = string_cat(yield, result);
+ break;
+ }
+#endif /* EXPAND_DLFUNC */
+
+ case EITEM_ENV: /* ${env {name} {val_if_found} {val_if_unfound}} */
+ {
+ uschar * key;
+ uschar *save_lookup_value = lookup_value;
+
+ if (Uskip_whitespace(&s) != '{') /*}*/
+ goto EXPAND_FAILED;
+
+ key = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, &resetok);
+ if (!key) goto EXPAND_FAILED; /*{{*/
+ if (*s++ != '}')
+ {
+ expand_string_message = US"missing '}' for name arg of env";
+ goto EXPAND_FAILED_CURLY;
+ }
+
+ lookup_value = US getenv(CS key);
+
+ 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 */
+ US"env", /* condition type */
+ &resetok))
+ {
+ case 1: goto EXPAND_FAILED; /* when all is well, the */
+ case 2: goto EXPAND_FAILED_CURLY; /* returned value is 0 */
+ }
+ if (skipping) continue;
+ break;
+ }
+
+#ifdef SUPPORT_SRS
+ case EITEM_SRS_ENCODE:
+ /* ${srs_encode {secret} {return_path} {orig_domain}} */
+ {
+ uschar * sub[3];
+ uschar cksum[4];
+ gstring * g = NULL;
+ BOOL quoted = FALSE;
+
+ switch (read_subs(sub, 3, 3, CUSS &s, skipping, TRUE, name, &resetok))
+ {
+ case 1: goto EXPAND_FAILED_CURLY;
+ case 2:
+ case 3: goto EXPAND_FAILED;
+ }
+ if (skipping) continue;
+
+ if (sub[1] && *(sub[1]))
+ {
+ g = string_catn(g, US"SRS0=", 5);
+
+ /* ${l_4:${hmac{md5}{SRS_SECRET}{${lc:$return_path}}}}= */
+ hmac_md5(sub[0], string_copylc(sub[1]), cksum, sizeof(cksum));
+ g = string_catn(g, cksum, sizeof(cksum));
+ g = string_catn(g, US"=", 1);
+
+ /* ${base32:${eval:$tod_epoch/86400&0x3ff}}= */
+ {
+ struct timeval now;
+ unsigned long i;
+ gstring * h = NULL;
+
+ gettimeofday(&now, NULL);
+ for (unsigned long i = (now.tv_sec / 86400) & 0x3ff; i; i >>= 5)
+ h = string_catn(h, &base32_chars[i & 0x1f], 1);
+ if (h) while (h->ptr > 0)
+ g = string_catn(g, &h->s[--h->ptr], 1);
+ }
+ g = string_catn(g, US"=", 1);
+
+ /* ${domain:$return_path}=${local_part:$return_path} */
+ {
+ int start, end, domain;
+ uschar * t = parse_extract_address(sub[1], &expand_string_message,
+ &start, &end, &domain, FALSE);
+ uschar * s;
+
+ if (!t)
+ goto EXPAND_FAILED;
+
+ if (domain > 0) g = string_cat(g, t + domain);
+ g = string_catn(g, US"=", 1);
+
+ s = domain > 0 ? string_copyn(t, domain - 1) : t;
+ if ((quoted = Ustrchr(s, '"') != NULL))
+ {
+ gstring * h = NULL;
+ DEBUG(D_expand) debug_printf_indent("auto-quoting local part\n");
+ while (*s) /* de-quote */
+ {
+ while (*s && *s != '"') h = string_catn(h, s++, 1);
+ if (*s) s++;
+ while (*s && *s != '"') h = string_catn(h, s++, 1);
+ if (*s) s++;
+ }
+ gstring_release_unused(h);
+ s = string_from_gstring(h);
+ }
+ g = string_cat(g, s);
+ }
+
+ /* Assume that if the original local_part had quotes
+ it was for good reason */
+
+ if (quoted) yield = string_catn(yield, US"\"", 1);
+ yield = string_catn(yield, g->s, g->ptr);
+ if (quoted) yield = string_catn(yield, US"\"", 1);
+
+ /* @$original_domain */
+ yield = string_catn(yield, US"@", 1);
+ yield = string_cat(yield, sub[2]);
+ }
+ else
+ DEBUG(D_expand) debug_printf_indent("null return_path for srs-encode\n");
+
+ break;
+ }
+#endif /*SUPPORT_SRS*/
+
+ default:
+ goto NOT_ITEM;
+ } /* EITEM_* switch */
+ /*NOTREACHED*/
+
+ DEBUG(D_expand)
+ if (yield && (start > 0 || *s)) /* only if not the sole expansion of the line */
+ debug_expansion_interim(US"item-res",
+ yield->s + start, yield->ptr - start, skipping);
+ continue;
+
+NOT_ITEM: ;
+ }
+
+ /* 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, * sub;
+#ifndef DISABLE_TLS
+ var_entry * vp = NULL;
+#endif
+
+ /* 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,
+ nelem(op_table_underscore))) < 0)
+ {
+ if ((arg = Ustrchr(name, '_')))
+ *arg = 0;
+ if ((c = chop_match(name, op_table_main, nelem(op_table_main))) >= 0)
+ c += nelem(op_table_underscore);
+ if (arg) *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)
+ {
+#ifndef DISABLE_TLS
+ case EOP_MD5:
+ case EOP_SHA1:
+ case EOP_SHA256:
+ case EOP_BASE64:
+ if (s[1] == '$')
+ {
+ const uschar * s1 = s;
+ sub = expand_string_internal(s+2, TRUE, &s1, skipping,
+ FALSE, &resetok);
+ if (!sub) goto EXPAND_FAILED; /*{*/
+ if (*s1 != '}')
+ { /*{*/
+ expand_string_message =
+ string_sprintf("missing '}' closing cert arg of %s", name);
+ 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. After handling go back
+ to the main loop top. */
+
+ {
+ int start = yield->ptr;
+ switch(c)
+ {
+ case EOP_BASE32:
+ {
+ uschar *t;
+ unsigned long int n = Ustrtoul(sub, &t, 10);
+ gstring * g = NULL;
+
+ if (*t != 0)
+ {
+ expand_string_message = string_sprintf("argument for base32 "
+ "operator is \"%s\", which is not a decimal number", sub);
+ goto EXPAND_FAILED;
+ }
+ for ( ; n; n >>= 5)
+ g = string_catn(g, &base32_chars[n & 0x1f], 1);
+
+ if (g) while (g->ptr > 0) yield = string_catn(yield, &g->s[--g->ptr], 1);
+ break;
+ }
+
+ case EOP_BASE32D:
+ {
+ uschar *tt = sub;
+ unsigned long int n = 0;
+ while (*tt)
+ {
+ uschar * t = Ustrchr(base32_chars, *tt++);
+ if (!t)
+ {
+ expand_string_message = string_sprintf("argument for base32d "
+ "operator is \"%s\", which is not a base 32 number", sub);
+ goto EXPAND_FAILED;
+ }
+ n = n * 32 + (t - base32_chars);
+ }
+ yield = string_fmt_append(yield, "%ld", n);
+ break;
+ }
+
+ 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;
+ }
+ yield = string_cat(yield, string_base62(n));
+ break;
+ }
+
+ /* Note that for Darwin and Cygwin, BASE_62 actually has the value 36 */
+
+ case EOP_BASE62D:
+ {
+ uschar *tt = sub;
+ unsigned long int n = 0;
+ while (*tt != 0)
+ {
+ uschar *t = Ustrchr(base62_chars, *tt++);
+ if (!t)
+ {
+ 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);
+ }
+ yield = string_fmt_append(yield, "%ld", n);
+ break;
+ }
+
+ case EOP_EXPAND:
+ {
+ uschar *expanded = expand_string_internal(sub, FALSE, NULL, skipping, TRUE, &resetok);
+ if (!expanded)
+ {
+ expand_string_message =
+ string_sprintf("internal expansion of \"%s\" failed: %s", sub,
+ expand_string_message);
+ goto EXPAND_FAILED;
+ }
+ yield = string_cat(yield, expanded);
+ break;
+ }
+
+ case EOP_LC:
+ {
+ int count = 0;
+ uschar *t = sub - 1;
+ while (*(++t) != 0) { *t = tolower(*t); count++; }
+ yield = string_catn(yield, sub, count);
+ break;
+ }
+
+ case EOP_UC:
+ {
+ int count = 0;
+ uschar *t = sub - 1;
+ while (*(++t) != 0) { *t = toupper(*t); count++; }
+ yield = string_catn(yield, sub, count);
+ break;
+ }
+
+ case EOP_MD5:
+#ifndef DISABLE_TLS
+ if (vp && *(void **)vp->value)
+ {
+ uschar * cp = tls_cert_fprt_md5(*(void **)vp->value);
+ yield = string_cat(yield, cp);
+ }
+ else
+#endif
+ {
+ md5 base;
+ uschar digest[16];
+ md5_start(&base);
+ md5_end(&base, sub, Ustrlen(sub), digest);
+ for (int j = 0; j < 16; j++)
+ yield = string_fmt_append(yield, "%02x", digest[j]);
+ }
+ break;
+
+ case EOP_SHA1:
+#ifndef DISABLE_TLS
+ if (vp && *(void **)vp->value)
+ {
+ uschar * cp = tls_cert_fprt_sha1(*(void **)vp->value);
+ yield = string_cat(yield, cp);
+ }
+ else
+#endif
+ {
+ hctx h;
+ uschar digest[20];
+ sha1_start(&h);
+ sha1_end(&h, sub, Ustrlen(sub), digest);
+ for (int j = 0; j < 20; j++)
+ yield = string_fmt_append(yield, "%02X", digest[j]);
+ }
+ break;
+
+ case EOP_SHA2:
+ case EOP_SHA256:
+#ifdef EXIM_HAVE_SHA2
+ if (vp && *(void **)vp->value)
+ if (c == EOP_SHA256)
+ yield = string_cat(yield, tls_cert_fprt_sha256(*(void **)vp->value));
+ else
+ expand_string_message = US"sha2_N not supported with certificates";
+ else
+ {
+ hctx h;
+ blob b;
+ hashmethod m = !arg ? HASH_SHA2_256
+ : Ustrcmp(arg, "256") == 0 ? HASH_SHA2_256
+ : Ustrcmp(arg, "384") == 0 ? HASH_SHA2_384
+ : Ustrcmp(arg, "512") == 0 ? HASH_SHA2_512
+ : HASH_BADTYPE;
+
+ if (m == HASH_BADTYPE || !exim_sha_init(&h, m))
+ {
+ expand_string_message = US"unrecognised sha2 variant";
+ goto EXPAND_FAILED;
+ }
+
+ exim_sha_update_string(&h, sub);
+ exim_sha_finish(&h, &b);
+ while (b.len-- > 0)
+ yield = string_fmt_append(yield, "%02X", *b.data++);
+ }
+#else
+ expand_string_message = US"sha256 only supported with TLS";
+#endif
+ break;
+
+ case EOP_SHA3:
+#ifdef EXIM_HAVE_SHA3
+ {
+ hctx h;
+ blob b;
+ hashmethod m = !arg ? HASH_SHA3_256
+ : Ustrcmp(arg, "224") == 0 ? HASH_SHA3_224
+ : Ustrcmp(arg, "256") == 0 ? HASH_SHA3_256
+ : Ustrcmp(arg, "384") == 0 ? HASH_SHA3_384
+ : Ustrcmp(arg, "512") == 0 ? HASH_SHA3_512
+ : HASH_BADTYPE;
+
+ if (m == HASH_BADTYPE || !exim_sha_init(&h, m))
+ {
+ expand_string_message = US"unrecognised sha3 variant";
+ goto EXPAND_FAILED;
+ }
+
+ exim_sha_update_string(&h, sub);
+ exim_sha_finish(&h, &b);
+ while (b.len-- > 0)
+ yield = string_fmt_append(yield, "%02X", *b.data++);
+ }
+ break;
+#else
+ expand_string_message = US"sha3 only supported with GnuTLS 3.5.0 + or OpenSSL 1.1.1 +";
+ goto EXPAND_FAILED;
+#endif
+
+ /* 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; 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 = b64encode(CUS sub, out - sub);
+ yield = string_cat(yield, enc);
+ break;
+ }
+
+ /* 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_fmt_append(yield, "\\x%02x", *t);
+ else
+ yield = string_catn(yield, t, 1);
+ }
+ break;
+ }
+
+ /* count the number of list elements */
+
+ case EOP_LISTCOUNT:
+ {
+ int cnt = 0, sep = 0;
+ uschar * buf = store_get(2, sub);
+
+ while (string_nextinlist(CUSS &sub, &sep, buf, 1)) cnt++;
+ yield = string_fmt_append(yield, "%d", cnt);
+ break;
+ }
+
+ /* expand a named list given the name */
+ /* handles nested named lists; requotes as colon-sep list */
+
+ case EOP_LISTNAMED:
+ expand_string_message = NULL;
+ yield = expand_listnamed(yield, sub, arg);
+ if (expand_string_message)
+ goto EXPAND_FAILED;
+ break;
+
+ /* quote a list-item for the given list-separator */
+
+ /* 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 type, mask, maskoffset;
+ BOOL normalised;
+ uschar buffer[64];
+
+ if ((type = string_is_ip_address(sub, &maskoffset)) == 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 || mask < 0 || mask > (type == 4 ? 32 : 128))
+ {
+ expand_string_message = string_sprintf("mask value too big in \"%s\"",
+ sub);
+ goto EXPAND_FAILED;
+ }
+
+ /* If an optional 'n' was given, ipv6 gets normalised output:
+ colons rather than dots, and zero-compressed. */
+
+ normalised = arg && *arg == 'n';
+
+ /* 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. */
+
+ if (type == 4 || !normalised)
+ yield = string_catn(yield, buffer,
+ host_nmtoa(count, binary, mask, buffer, '.'));
+ else
+ {
+ ipv6_nmtoa(binary, buffer);
+ yield = string_fmt_append(yield, "%s/%d", buffer, mask);
+ }
+ break;
+ }
+
+ case EOP_IPV6NORM:
+ case EOP_IPV6DENORM:
+ {
+ int type = string_is_ip_address(sub, NULL);
+ int binary[4];
+ uschar buffer[44];
+
+ switch (type)
+ {
+ case 6:
+ (void) host_aton(sub, binary);
+ break;
+
+ case 4: /* convert to IPv4-mapped IPv6 */
+ binary[0] = binary[1] = 0;
+ binary[2] = 0x0000ffff;
+ (void) host_aton(sub, binary+3);
+ break;
+
+ case 0:
+ expand_string_message =
+ string_sprintf("\"%s\" is not an IP address", sub);
+ goto EXPAND_FAILED;
+ }
+
+ yield = string_catn(yield, buffer, c == EOP_IPV6NORM
+ ? ipv6_nmtoa(binary, buffer)
+ : host_nmtoa(4, binary, -1, buffer, ':')
+ );
+ break;
+ }
+
+ 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)
+ if (c != EOP_DOMAIN)
+ yield = c == EOP_LOCAL_PART && domain > 0
+ ? string_catn(yield, t, domain - 1)
+ : string_cat(yield, t);
+ else if (domain > 0)
+ yield = string_cat(yield, t + domain);
+ break;
+ }
+
+ case EOP_ADDRESSES:
+ {
+ uschar outsep[2] = { ':', '\0' };
+ uschar *address, *error;
+ int save_ptr = gstring_length(yield);
+ int start, end, domain; /* Not really used */
+
+ if (Uskip_whitespace(&sub) == '>')
+ if (*outsep = *++sub) ++sub;
+ else
+ {
+ expand_string_message = string_sprintf("output separator "
+ "missing in expanding ${addresses:%s}", --sub);
+ goto EXPAND_FAILED;
+ }
+ f.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)
+ {
+ if (yield && yield->ptr != save_ptr && address[0] == *outsep)
+ yield = string_catn(yield, US" ", 1);
+
+ for (;;)
+ {
+ size_t seglen = Ustrcspn(address, outsep);
+ yield = string_catn(yield, address, seglen + 1);
+
+ /* If we got to the end of the string we output one character
+ too many. */
+
+ if (address[seglen] == '\0') { yield->ptr--; break; }
+ yield = string_catn(yield, outsep, 1);
+ address += seglen + 1;
+ }
+
+ /* Output a separator after the string: we will remove the
+ redundant final one at the end. */
+
+ yield = string_catn(yield, outsep, 1);
+ }
+
+ if (saveend == '\0') break;
+ sub = p + 1;
+ }
+
+ /* If we have generated anything, remove the redundant final
+ separator. */
+
+ if (yield && yield->ptr != save_ptr) yield->ptr--;
+ f.parse_allow_group = FALSE;
+ break;
+ }
+
+
+ /* 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)
+ {
+ BOOL needs_quote = (!*sub); /* TRUE for empty string */
+ uschar *t = sub - 1;
+
+ if (c == EOP_QUOTE)
+ while (!needs_quote && *++t)
+ needs_quote = !isalnum(*t) && !strchr("_-.", *t);
+
+ else /* EOP_QUOTE_LOCAL_PART */
+ while (!needs_quote && *++t)
+ needs_quote = !isalnum(*t)
+ && strchr("!#$%&'*+-/=?^_`{|}~", *t) == NULL
+ && (*t != '.' || t == sub || !t[1]);
+
+ if (needs_quote)
+ {
+ yield = string_catn(yield, US"\"", 1);
+ t = sub - 1;
+ while (*++t)
+ if (*t == '\n')
+ yield = string_catn(yield, US"\\n", 2);
+ else if (*t == '\r')
+ yield = string_catn(yield, US"\\r", 2);
+ else
+ {
+ if (*t == '\\' || *t == '"')
+ yield = string_catn(yield, US"\\", 1);
+ yield = string_catn(yield, t, 1);
+ }
+ yield = string_catn(yield, US"\"", 1);
+ }
+ else
+ yield = string_cat(yield, sub);
+ break;
+ }
+
+ /* quote_lookuptype does lookup-specific quoting */
+
+ else
+ {
+ int n;
+ uschar * opt = Ustrchr(arg, '_');
+
+ if (opt) *opt++ = 0;
+
+ if ((n = search_findtype(arg, Ustrlen(arg))) < 0)
+ {
+ expand_string_message = search_error_message;
+ goto EXPAND_FAILED;
+ }
+
+ if (lookup_list[n]->quote)
+ sub = (lookup_list[n]->quote)(sub, opt, (unsigned)n);
+ else if (opt)
+ sub = NULL;
+
+ if (!sub)
+ {
+ expand_string_message = string_sprintf(
+ "\"%s\" unrecognized after \"${quote_%s\"", /*}*/
+ opt, arg);
+ goto EXPAND_FAILED;
+ }
+
+ yield = string_cat(yield, sub);
+ break;
+ }
+
+ /* 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_catn(yield, US"\\", 1);
+ yield = string_catn(yield, t, 1);
+ }
+ break;
+ }
+
+ /* 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:
+ yield = string_cat(yield,
+ parse_quote_2047(sub, Ustrlen(sub), headers_charset,
+ FALSE));
+ break;
+
+ /* RFC 2047 decode */
+
+ case EOP_RFC2047D:
+ {
+ int len;
+ uschar *error;
+ uschar *decoded = rfc2047_decode(sub, check_rfc2047_length,
+ headers_charset, '?', &len, &error);
+ if (error)
+ {
+ expand_string_message = error;
+ goto EXPAND_FAILED;
+ }
+ yield = string_catn(yield, decoded, len);
+ break;
+ }
+
+ /* from_utf8 converts UTF-8 to 8859-1, turning non-existent chars into
+ underscores */
+
+ case EOP_FROM_UTF8:
+ {
+ uschar * buff = store_get(4, sub);
+ while (*sub)
+ {
+ int c;
+ GETUTF8INC(c, sub);
+ if (c > 255) c = '_';
+ buff[0] = c;
+ yield = string_catn(yield, buff, 1);
+ }
+ break;
+ }
+
+ /* replace illegal UTF-8 sequences by replacement character */
+
+ #define UTF8_REPLACEMENT_CHAR US"?"
+
+ case EOP_UTF8CLEAN:
+ {
+ int seq_len = 0, index = 0;
+ int bytes_left = 0;
+ long codepoint = -1;
+ int complete;
+ uschar seq_buff[4]; /* accumulate utf-8 here */
+
+ /* Manually track tainting, as we deal in individual chars below */
+
+ if (!yield->s || !yield->ptr)
+ yield->s = store_get(yield->size = Ustrlen(sub), sub);
+ else if (is_incompatible(yield->s, sub))
+ gstring_rebuffer(yield, sub);
+
+ /* Check the UTF-8, byte-by-byte */
+
+ while (*sub)
+ {
+ complete = 0;
+ uschar 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 (RFC3629 limit) */
+ else
+ { /* finished; output utf-8 sequence */
+ yield = string_catn(yield, seq_buff, seq_len);
+ index = 0;
+ }
+ }
+ }
+ else /* no bytes left: new sequence */
+ {
+ if(!(c & 0x80)) /* 1-byte sequence, US-ASCII, keep it */
+ {
+ yield = string_catn(yield, &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_catn(yield, UTF8_REPLACEMENT_CHAR, 1);
+ }
+ if ((complete == 1) && ((c & 0x80) == 0))
+ /* ASCII character follows incomplete sequence */
+ yield = string_catn(yield, &c, 1);
+ }
+ /* If given a sequence truncated mid-character, we also want to report ?
+ Eg, ${length_1:フィル} is one byte, not one character, so we expect
+ ${utf8clean:${length_1:フィル}} to yield '?' */
+
+ if (bytes_left != 0)
+ yield = string_catn(yield, UTF8_REPLACEMENT_CHAR, 1);
+
+ break;
+ }
+
+#ifdef SUPPORT_I18N
+ case EOP_UTF8_DOMAIN_TO_ALABEL:
+ {
+ uschar * error = NULL;
+ uschar * s = string_domain_utf8_to_alabel(sub, &error);
+ if (error)
+ {
+ expand_string_message = string_sprintf(
+ "error converting utf8 (%s) to alabel: %s",
+ string_printing(sub), error);
+ goto EXPAND_FAILED;
+ }
+ yield = string_cat(yield, s);
+ break;
+ }
+
+ case EOP_UTF8_DOMAIN_FROM_ALABEL:
+ {
+ uschar * error = NULL;
+ uschar * s = string_domain_alabel_to_utf8(sub, &error);
+ if (error)
+ {
+ expand_string_message = string_sprintf(
+ "error converting alabel (%s) to utf8: %s",
+ string_printing(sub), error);
+ goto EXPAND_FAILED;
+ }
+ yield = string_cat(yield, s);
+ break;
+ }
+
+ case EOP_UTF8_LOCALPART_TO_ALABEL:
+ {
+ uschar * error = NULL;
+ uschar * s = string_localpart_utf8_to_alabel(sub, &error);
+ if (error)
+ {
+ expand_string_message = string_sprintf(
+ "error converting utf8 (%s) to alabel: %s",
+ string_printing(sub), error);
+ goto EXPAND_FAILED;
+ }
+ yield = string_cat(yield, s);
+ DEBUG(D_expand) debug_printf_indent("yield: '%s'\n", yield->s);
+ break;
+ }
+
+ case EOP_UTF8_LOCALPART_FROM_ALABEL:
+ {
+ uschar * error = NULL;
+ uschar * s = string_localpart_alabel_to_utf8(sub, &error);
+ if (error)
+ {
+ expand_string_message = string_sprintf(
+ "error converting alabel (%s) to utf8: %s",
+ string_printing(sub), error);
+ goto EXPAND_FAILED;
+ }
+ yield = string_cat(yield, s);
+ break;
+ }
+#endif /* EXPERIMENTAL_INTERNATIONAL */
+
+ /* escape turns all non-printing characters into escape sequences. */
+
+ case EOP_ESCAPE:
+ {
+ const uschar * t = string_printing(sub);
+ yield = string_cat(yield, t);
+ break;
+ }
+
+ case EOP_ESCAPE8BIT:
+ {
+ uschar c;
+
+ for (const uschar * s = sub; (c = *s); s++)
+ yield = c < 127 && c != '\\'
+ ? string_catn(yield, s, 1)
+ : string_fmt_append(yield, "\\%03o", c);
+ break;
+ }
+
+ /* 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)
+ {
+ expand_string_message = string_sprintf("error in expression "
+ "evaluation: %s (after processing \"%.*s\")", error,
+ (int)(sub-save_sub), save_sub);
+ goto EXPAND_FAILED;
+ }
+ yield = string_fmt_append(yield, PR_EXIM_ARITH, n);
+ break;
+ }
+
+ /* Handle time period formatting */
+
+ 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;
+ }
+ yield = string_fmt_append(yield, "%d", n);
+ break;
+ }
+
+ 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, t);
+ break;
+ }
+
+ /* Convert string to base64 encoding */
+
+ case EOP_STR2B64:
+ case EOP_BASE64:
+ {
+#ifndef DISABLE_TLS
+ uschar * s = vp && *(void **)vp->value
+ ? tls_cert_der_b64(*(void **)vp->value)
+ : b64encode(CUS sub, Ustrlen(sub));
+#else
+ uschar * s = b64encode(CUS sub, Ustrlen(sub));
+#endif
+ yield = string_cat(yield, s);
+ break;
+ }
+
+ case EOP_BASE64D:
+ {
+ uschar * s;
+ int len = b64decode(sub, &s);
+ if (len < 0)
+ {
+ expand_string_message = string_sprintf("string \"%s\" is not "
+ "well-formed for \"%s\" operator", sub, name);
+ goto EXPAND_FAILED;
+ }
+ yield = string_cat(yield, s);
+ break;
+ }
+
+ /* strlen returns the length of the string */
+
+ case EOP_STRLEN:
+ yield = string_fmt_append(yield, "%d", Ustrlen(sub));
+ break;
+
+ /* 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)
+ {
+ 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) goto EXPAND_FAILED;
+
+ yield = string_catn(yield, ret, len);
+ break;
+ }
+
+ /* Stat a path */
+
+ case EOP_STAT:
+ {
+ uschar smode[12];
+ uschar **modetable[3];
+ mode_t mode;
+ struct stat st;
+
+ if (expand_forbid & RDO_EXISTS)
+ {
+ 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 (int i = 0; i < 3; i++)
+ {
+ memcpy(CS(smode + 7 - i*3), CS(modetable[i][mode & 7]), 3);
+ mode >>= 3;
+ }
+
+ smode[10] = 0;
+ yield = string_fmt_append(yield,
+ "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);
+ break;
+ }
+
+ /* vaguely random number less than N */
+
+ case EOP_RANDINT:
+ {
+ int_eximarith_t max = expanded_string_integer(sub, TRUE);
+
+ if (expand_string_message)
+ goto EXPAND_FAILED;
+ yield = string_fmt_append(yield, "%d", vaguely_random_number((int)max));
+ break;
+ }
+
+ /* 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, reversed);
+ break;
+ }
+
+ /* Unknown operator */
+
+ default:
+ expand_string_message =
+ string_sprintf("unknown expansion operator \"%s\"", name);
+ goto EXPAND_FAILED;
+ } /* EOP_* switch */
+
+ DEBUG(D_expand)
+ {
+ const uschar * s = yield->s + start;
+ int i = yield->ptr - start;
+ BOOL tainted = is_tainted(s);
+
+ DEBUG(D_noutf8)
+ {
+ debug_printf_indent("|-----op-res: %.*s\n", i, s);
+ if (tainted)
+ {
+ debug_printf_indent("%s \\__", skipping ? "| " : " ");
+ debug_print_taint(yield->s);
+ }
+ }
+ else
+ {
+ debug_printf_indent(UTF8_VERT_RIGHT
+ UTF8_HORIZ UTF8_HORIZ UTF8_HORIZ UTF8_HORIZ UTF8_HORIZ
+ "op-res: %.*s\n", i, s);
+ if (tainted)
+ {
+ debug_printf_indent("%s",
+ skipping
+ ? UTF8_VERT " " : " " UTF8_UP_RIGHT UTF8_HORIZ UTF8_HORIZ);
+ debug_print_taint(yield->s);
+ }
+ }
+ }
+ continue;
+ }
+ }
+
+ /* Not an item or an operator */
+ /* 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++ == '}')
+ {
+ const uschar * value;
+ int len;
+ int newsize = 0;
+ gstring * g = NULL;
+
+ if (!yield)
+ g = store_get(sizeof(gstring), GET_UNTAINTED);
+ else if (yield->ptr == 0)
+ {
+ if (resetok) reset_point = store_reset(reset_point);
+ yield = NULL;
+ reset_point = store_mark();
+ g = store_get(sizeof(gstring), GET_UNTAINTED); /* alloc _before_ calling find_variable() */
+ }
+ if (!(value = find_variable(name, FALSE, skipping, &newsize)))
+ {
+ expand_string_message =
+ string_sprintf("unknown variable in \"${%s}\"", name);
+ check_variable_error_message(name);
+ goto EXPAND_FAILED;
+ }
+ len = Ustrlen(value);
+ if (!yield && newsize)
+ {
+ yield = g;
+ yield->size = newsize;
+ yield->ptr = len;
+ yield->s = US value; /* known to be in new store i.e. a copy, so deconst safe */
+ }
+ else
+ yield = string_catn(yield, 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)
+ {
+ 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)
+ yield = string_get(1);
+(void) string_from_gstring(yield);
+if (left) *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) gstring_release_unused(yield);
+else if (resetok_p) *resetok_p = FALSE;
+
+DEBUG(D_expand)
+ {
+ BOOL tainted = is_tainted(yield->s);
+ DEBUG(D_noutf8)
+ {
+ debug_printf_indent("|--expanding: %.*s\n", (int)(s - string), string);
+ debug_printf_indent("%sresult: %s\n",
+ skipping ? "|-----" : "\\_____", yield->s);
+ if (tainted)
+ {
+ debug_printf_indent("%s \\__", skipping ? "| " : " ");
+ debug_print_taint(yield->s);
+ }
+ if (skipping)
+ debug_printf_indent("\\___skipping: result is not used\n");
+ }
+ else
+ {
+ debug_printf_indent(UTF8_VERT_RIGHT UTF8_HORIZ UTF8_HORIZ
+ "expanding: %.*s\n",
+ (int)(s - string), string);
+ debug_printf_indent("%s" UTF8_HORIZ UTF8_HORIZ UTF8_HORIZ UTF8_HORIZ UTF8_HORIZ
+ "result: %s\n",
+ skipping ? UTF8_VERT_RIGHT : UTF8_UP_RIGHT,
+ yield->s);
+ if (tainted)
+ {
+ debug_printf_indent("%s",
+ skipping
+ ? UTF8_VERT " " : " " UTF8_UP_RIGHT UTF8_HORIZ UTF8_HORIZ);
+ debug_print_taint(yield->s);
+ }
+ if (skipping)
+ debug_printf_indent(UTF8_UP_RIGHT UTF8_HORIZ UTF8_HORIZ UTF8_HORIZ
+ "skipping: result is not used\n");
+ }
+ }
+expand_level--;
+return yield->s;
+
+/* 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:
+if (malformed_header)
+ expand_string_message =
+ US"missing or misplaced { or } - could be header name not terminated by colon";
+
+else if (!expand_string_message || !*expand_string_message)
+ expand_string_message = 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) *left = s;
+DEBUG(D_expand)
+ {
+ DEBUG(D_noutf8)
+ {
+ debug_printf_indent("|failed to expand: %s\n", string);
+ debug_printf_indent("%serror message: %s\n",
+ f.expand_string_forcedfail ? "|---" : "\\___", expand_string_message);
+ if (f.expand_string_forcedfail)
+ debug_printf_indent("\\failure was forced\n");
+ }
+ else
+ {
+ debug_printf_indent(UTF8_VERT_RIGHT "failed to expand: %s\n",
+ string);
+ debug_printf_indent("%s" UTF8_HORIZ UTF8_HORIZ UTF8_HORIZ
+ "error message: %s\n",
+ f.expand_string_forcedfail ? UTF8_VERT_RIGHT : UTF8_UP_RIGHT,
+ expand_string_message);
+ if (f.expand_string_forcedfail)
+ debug_printf_indent(UTF8_UP_RIGHT "failure was forced\n");
+ }
+ }
+if (resetok_p && !resetok) *resetok_p = FALSE;
+expand_level--;
+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
+*/
+
+const uschar *
+expand_cstring(const uschar * string)
+{
+if (Ustrpbrk(string, "$\\") != NULL)
+ {
+ int old_pool = store_pool;
+ uschar * s;
+
+ f.search_find_defer = FALSE;
+ malformed_header = FALSE;
+ store_pool = POOL_MAIN;
+ s = expand_string_internal(string, FALSE, NULL, FALSE, TRUE, NULL);
+ store_pool = old_pool;
+ return s;
+ }
+return string;
+}
+
+
+uschar *
+expand_string(uschar * string)
+{
+return US expand_cstring(CUS string);
+}
+
+
+
+
+
+/*************************************************
+* 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.
+Since we know it has been copied, the de-const cast is safe.
+
+Argument: the string to be expanded
+Returns: the expanded string, always in a new bit of store, or NULL
+*/
+
+uschar *
+expand_string_copy(const uschar *string)
+{
+const uschar *yield = expand_cstring(string);
+if (yield == string) yield = string_copy(string);
+return US 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(const 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) 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))
+ if (Uskip_whitespace(&s) == '\0')
+ {
+ DEBUG(D_expand)
+ debug_printf_indent("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
+ if (Uskip_whitespace(&endptr) == 0) return value;
+ }
+
+expand_string_message = string_sprintf(CS msg, s);
+return -2;
+}
+
+
+/* These values are usually fixed boolean values, but they are permitted to be
+expanded strings.
+
+Arguments:
+ addr address being routed
+ mtype the module type
+ mname the module name
+ dbg_opt debug selectors
+ 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
+*/
+
+int
+exp_bool(address_item *addr,
+ uschar *mtype, uschar *mname, unsigned dbg_opt,
+ uschar *oname, BOOL bvalue,
+ uschar *svalue, BOOL *rvalue)
+{
+uschar *expanded;
+if (!svalue) { *rvalue = bvalue; return OK; }
+
+if (!(expanded = expand_string(svalue)))
+ {
+ if (f.expand_string_forcedfail)
+ {
+ DEBUG(dbg_opt) debug_printf("expansion of \"%s\" forced failure\n", oname);
+ *rvalue = bvalue;
+ return OK;
+ }
+ addr->message = string_sprintf("failed to expand \"%s\" in %s %s: %s",
+ oname, mname, mtype, expand_string_message);
+ DEBUG(dbg_opt) debug_printf("%s\n", addr->message);
+ return DEFER;
+ }
+
+DEBUG(dbg_opt) 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 %s", expanded, oname, mname, mtype);
+ return DEFER;
+ }
+
+return OK;
+}
+
+
+
+/* Avoid potentially exposing a password in a string about to be logged */
+
+uschar *
+expand_hide_passwords(uschar * s)
+{
+return ( ( Ustrstr(s, "failed to expand") != NULL
+ || Ustrstr(s, "expansion of ") != NULL
+ )
+ && ( Ustrstr(s, "mysql") != NULL
+ || Ustrstr(s, "pgsql") != NULL
+ || Ustrstr(s, "redis") != NULL
+ || Ustrstr(s, "sqlite") != NULL
+ || Ustrstr(s, "ldap:") != NULL
+ || Ustrstr(s, "ldaps:") != NULL
+ || Ustrstr(s, "ldapi:") != NULL
+ || Ustrstr(s, "ldapdn:") != NULL
+ || Ustrstr(s, "ldapm:") != NULL
+ ) )
+ ? US"Temporary internal error" : s;
+}
+
+
+/* Read given named file into big_buffer. Use for keying material etc.
+The content will have an ascii NUL appended.
+
+Arguments:
+ filename as it says
+
+Return: pointer to buffer, or NULL on error.
+*/
+
+uschar *
+expand_file_big_buffer(const uschar * filename)
+{
+int fd, off = 0, len;
+
+if ((fd = exim_open2(CS filename, O_RDONLY)) < 0)
+ {
+ log_write(0, LOG_MAIN | LOG_PANIC, "unable to open file for reading: %s",
+ filename);
+ return NULL;
+ }
+
+do
+ {
+ if ((len = read(fd, big_buffer + off, big_buffer_size - 2 - off)) < 0)
+ {
+ (void) close(fd);
+ log_write(0, LOG_MAIN|LOG_PANIC, "unable to read file: %s", filename);
+ return NULL;
+ }
+ off += len;
+ }
+while (len > 0);
+
+(void) close(fd);
+big_buffer[off] = '\0';
+return big_buffer;
+}
+
+
+
+/*************************************************
+* Error-checking for testsuite *
+*************************************************/
+typedef struct {
+ uschar * region_start;
+ uschar * region_end;
+ const uschar *var_name;
+ const uschar *var_data;
+} err_ctx;
+
+/* Called via tree_walk, which allows nonconst name/data. Our usage is const. */
+static void
+assert_variable_notin(uschar * var_name, uschar * var_data, void * ctx)
+{
+err_ctx * e = ctx;
+if (var_data >= e->region_start && var_data < e->region_end)
+ {
+ e->var_name = CUS var_name;
+ e->var_data = CUS var_data;
+ }
+}
+
+void
+assert_no_variables(void * ptr, int len, const char * filename, int linenumber)
+{
+err_ctx e = { .region_start = ptr, .region_end = US ptr + len,
+ .var_name = NULL, .var_data = NULL };
+
+/* check acl_ variables */
+tree_walk(acl_var_c, assert_variable_notin, &e);
+tree_walk(acl_var_m, assert_variable_notin, &e);
+
+/* check auth<n> variables.
+assert_variable_notin() treats as const, so deconst is safe. */
+for (int i = 0; i < AUTH_VARS; i++) if (auth_vars[i])
+ assert_variable_notin(US"auth<n>", US auth_vars[i], &e);
+
+/* check regex<n> variables. assert_variable_notin() treats as const. */
+for (int i = 0; i < REGEX_VARS; i++) if (regex_vars[i])
+ assert_variable_notin(US"regex<n>", US regex_vars[i], &e);
+
+/* check known-name variables */
+for (var_entry * v = var_table; v < var_table + var_table_size; v++)
+ if (v->type == vtype_stringptr)
+ assert_variable_notin(US v->name, *(USS v->value), &e);
+
+/* check dns and address trees */
+tree_walk(tree_dns_fails, assert_variable_notin, &e);
+tree_walk(tree_duplicates, assert_variable_notin, &e);
+tree_walk(tree_nonrecipients, assert_variable_notin, &e);
+tree_walk(tree_unusable, assert_variable_notin, &e);
+
+if (e.var_name)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "live variable '%s' destroyed by reset_store at %s:%d\n- value '%.64s'",
+ e.var_name, filename, linenumber, e.var_data);
+}
+
+
+
+/*************************************************
+**************************************************
+* Stand-alone test program *
+**************************************************
+*************************************************/
+
+#ifdef STAND_ALONE
+
+
+BOOL
+regex_match_and_setup(const pcre2_code *re, uschar *subject, int options, int setup)
+{
+int ovec[3*(EXPAND_MAXN+1)];
+int n = pcre_exec(re, NULL, subject, Ustrlen(subject), 0, PCRE_EOPT|options,
+ ovec, nelem(ovec));
+BOOL yield = n >= 0;
+if (n == 0) n = EXPAND_MAXN + 1;
+if (yield)
+ {
+ expand_nmax = setup < 0 ? 0 : setup + 1;
+ for (int nn = setup < 0 ? 0 : 2; nn < n*2; nn += 2)
+ {
+ expand_nstring[expand_nmax] = subject + ovec[nn];
+ expand_nlength[expand_nmax++] = ovec[nn+1] - ovec[nn];
+ }
+ expand_nmax--;
+ }
+return yield;
+}
+
+
+int main(int argc, uschar **argv)
+{
+uschar buffer[1024];
+
+debug_selector = D_v;
+debug_file = stderr;
+debug_fd = fileno(debug_file);
+big_buffer = malloc(big_buffer_size);
+store_init();
+
+for (int 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 LOOKUP_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 */
+
+/* Thie deliberately regards the input as untainted, so that it can be
+expanded; only reasonable since this is a test for string-expansions. */
+
+while (fgets(buffer, sizeof(buffer), stdin) != NULL)
+ {
+ rmark reset_point = store_mark();
+ uschar *yield = expand_string(buffer);
+ if (yield)
+ printf("%s\n", yield);
+ else
+ {
+ if (f.search_find_defer) printf("search_find deferred\n");
+ printf("Failed: %s\n", expand_string_message);
+ if (f.expand_string_forcedfail) printf("Forced failure\n");
+ printf("\n");
+ }
+ store_reset(reset_point);
+ }
+
+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
index 0000000..ad017e5
--- /dev/null
+++ b/src/filter.c
@@ -0,0 +1,2607 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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;
+ const 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 const 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(const 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 (nelem(mailargs))
+
+/* 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 = nelem(cond_words);
+
+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 = nelem(command_list);
+
+/* 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 const uschar *
+nextsigchar(const 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 const uschar *
+nextword(const 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 const uschar *
+nextitem(const uschar *ptr, uschar *buffer, int size, BOOL bracket)
+{
+uschar *bp = buffer;
+if (*ptr != '\"') return nextword(ptr, buffer, size, bracket);
+
+while (*++ptr && *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 */
+ {
+ const 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(CUSS &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(const 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 const uschar *
+read_condition(const 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)
+ {
+ *error_pointer = US"\"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) 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), GET_UNTAINTED);
+ 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;
+ const uschar * saveptr = ptr;
+ ptr = nextword(ptr, buffer, sizeof(buffer), TRUE);
+ if (*error_pointer) break;
+ if (Ustrcmp(buffer, "alias") != 0)
+ {
+ ptr = saveptr;
+ break;
+ }
+ ptr = nextitem(ptr, buffer, sizeof(buffer), TRUE);
+ if (*error_pointer) break;
+ aa = store_get(sizeof(string_item), GET_UNTAINTED);
+ 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) 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) break;
+ if (*ptr != ')')
+ {
+ *error_pointer = string_sprintf("expected \")\" in line %d of "
+ "filter file", line_number);
+ break;
+ }
+ 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;
+ const uschar *isptr = NULL;
+
+ c->left.u = string_copy(buffer);
+ ptr = nextword(ptr, buffer, sizeof(buffer), TRUE);
+ if (*error_pointer) 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) break;
+ if (strcmpic(buffer, US"not") == 0)
+ {
+ c->testfor = !c->testfor;
+ if (isptr) isptr = ptr;
+ ptr = nextword(ptr, buffer, sizeof(buffer), TRUE);
+ if (*error_pointer) 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)
+ {
+ *error_pointer = string_sprintf("unrecognized condition word \"%s\" "
+ "near line %d of filter file", buffer, line_number);
+ break;
+ }
+ ptr = isptr;
+ }
+
+ /* Get the RH argument. */
+
+ ptr = nextitem(ptr, buffer, sizeof(buffer), TRUE);
+ if (*error_pointer) 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
+ {
+// const uschar *saveptr = ptr;
+ ptr = nextword(ptr, buffer, sizeof(buffer), FALSE);
+ if (*error_pointer) 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. */
+ /*XXX This stops us doing a constification job in this file, unfortunately.
+ Comment it out and see if anything breaks.
+ With one addition down at DEFERFREEZEFAIL it passes the testsuite. */
+
+ if (Ustrcmp(buffer, "then") == 0)
+ {
+// if (toplevel) *saveptr = 0;
+// else
+ if (!toplevel)
+ *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), GET_UNTAINTED);
+ 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), GET_UNTAINTED);
+ condition_block * or_parent = NULL;
+
+ if (current_parent)
+ {
+ while (current_parent->parent &&
+ 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)
+ or_parent = current_parent->parent;
+ }
+
+ orc->parent = or_parent;
+ if (!or_parent) *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);
+}
+
+
+
+/*************************************************
+* Output 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(const 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];
+const uschar *ptr = *pptr;
+const 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. */
+
+*buffer = '\0'; /* compiler quietening */
+
+if (Ustrncmp(ptr, "if(", 3) == 0)
+ {
+ Ustrcpy(buffer, US"if");
+ ptr += 2;
+ }
+else if (Ustrncmp(ptr, "elif(", 5) == 0)
+ {
+ Ustrcpy(buffer, US"elif");
+ ptr += 4;
+ }
+else
+ {
+ ptr = nextword(ptr, buffer, sizeof(buffer), FALSE);
+ if (*error_pointer) 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)
+ *error_pointer = string_sprintf("\"%s\" requires an argument "
+ "near line %d of filter file", command_list[command], line_number);
+
+ if (*error_pointer) 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 || 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)
+ *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 (!f.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)
+ *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, US"\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)
+ {
+ const 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) yield = FALSE;
+ else
+ {
+ new = store_get(sizeof(filter_cmd) + sizeof(union argtypes), GET_UNTAINTED);
+ 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 || 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), GET_UNTAINTED);
+ 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), GET_UNTAINTED);
+ 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) { 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), GET_UNTAINTED);
+ 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) { 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), GET_UNTAINTED);
+ 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 (;;)
+ {
+ const uschar *saveptr = ptr;
+ ptr = nextword(ptr, buffer, sizeof(buffer), FALSE);
+ if (*error_pointer)
+ { 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)
+ { 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)
+ {
+ 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)
+ new->args[mailarg_index_log].u = string_copy(US".vacation.log");
+ if (!new->args[mailarg_index_once].u)
+ new->args[mailarg_index_once].u = string_copy(US".vacation");
+ if (!new->args[mailarg_index_once_repeat].u)
+ new->args[mailarg_index_once_repeat].u = string_copy(US"7d");
+ if (!new->args[mailarg_index_subject].u)
+ 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)
+ {
+ *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)
+ {
+ *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 conditional 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(const uschar **pptr, filter_cmd ***lastcmdptr, BOOL conditional)
+{
+if (conditional) expect_endif++;
+had_else_endif = had_neither;
+while (**pptr && 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 uschar *exp[2], * p, * pp;
+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 = f.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 || f.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 && f.deliver_manual_thaw;
+ break;
+
+ /* The foranyaddress condition loops through a list of addresses */
+
+ case cond_foranyaddress:
+ p = c->left.u;
+ if (!(pp = expand_cstring(p)))
+ {
+ *error_pointer = string_sprintf("failed to expand \"%s\" in "
+ "filter file: %s", p, expand_string_message);
+ return FALSE;
+ }
+
+ yield = FALSE;
+ f.parse_allow_group = TRUE; /* Allow group syntax */
+
+ while (*pp)
+ {
+ uschar *error;
+ int start, end, domain;
+ uschar * s;
+
+ p = parse_find_address_end(pp, FALSE);
+ s = string_copyn(pp, p - pp);
+
+ filter_thisaddress =
+ parse_extract_address(s, &error, &start, &end, &domain, FALSE);
+
+ if (filter_thisaddress)
+ {
+ if ((filter_test != FTEST_NONE && debug_selector != 0) ||
+ (debug_selector & D_filter) != 0)
+ {
+ indent();
+ debug_printf_indent("Extracted address %s\n", filter_thisaddress);
+ }
+ yield = test_condition(c->right.c, FALSE);
+ }
+
+ if (yield) break;
+ if (!*p) break;
+ pp = p + 1;
+ }
+
+ f.parse_allow_group = FALSE; /* Reset group syntax flags */
+ f.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++)
+ {
+ if (!(exp[i] = expand_cstring(p)))
+ {
+ *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_c(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]);
+ const 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:
+ {
+ const pcre2_code *re;
+ int err;
+ PCRE2_SIZE offset;
+
+ if ((filter_test != FTEST_NONE && debug_selector != 0) ||
+ (debug_selector & D_filter) != 0)
+ {
+ debug_printf_indent("Match expanded arguments:\n");
+ debug_printf_indent(" Subject = %s\n", exp[0]);
+ debug_printf_indent(" Pattern = %s\n", exp[1]);
+ }
+
+ if (!(re = pcre2_compile((PCRE2_SPTR)exp[1], PCRE2_ZERO_TERMINATED,
+ PCRE_COPT | (c->type == cond_matches ? PCRE2_CASELESS : 0),
+ &err, &offset, pcre_cmp_ctx)))
+ {
+ uschar errbuf[128];
+ pcre2_get_error_message(err, errbuf, sizeof(errbuf));
+ *error_pointer = string_sprintf("error while compiling "
+ "regular expression \"%s\": %s at offset %ld",
+ exp[1], errbuf, (long)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_indent("%sondition is %s: ",
+ toplevel? "C" : "Sub-c",
+ (yield == c->testfor)? "true" : "false");
+ print_condition(c, TRUE);
+ debug_printf_indent("\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)
+{
+const uschar *s;
+int mode;
+address_item *addr;
+BOOL condition_value;
+
+while (commands)
+ {
+ int ff_ret;
+ uschar *fmsg, *ff_name;
+ const 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++)
+ {
+ const uschar *ss = commands->args[i].u;
+ if (!ss)
+ expargs[i] = NULL;
+ else if (!(expargs[i] = expand_cstring(ss)))
+ {
+ *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++)
+ {
+ const 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)
+ expargs[i] = filter_options & RDO_REWRITE
+ ? 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 && !f.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_indent("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(US expargs[0], TRUE); /* TRUE => copy s, so deconst ok */
+ addr->prop.errors_address = !s ? NULL : string_copy(s); /* Default is NULL */
+ if (commands->noerror) addr->prop.ignore_error = TRUE;
+ 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_indent("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(US s, TRUE); /* TRUE => copy s, so deconst ok */
+ setflag(addr, af_pfr);
+ setflag(addr, af_file);
+ if (commands->noerror) addr->prop.ignore_error = TRUE;
+ 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_indent("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(US s, TRUE); /* TRUE => copy s, so deconst ok */
+ setflag(addr, af_pfr);
+ setflag(addr, af_expand_pipe);
+ if (commands->noerror) addr->prop.ignore_error = TRUE;
+ 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 ecount = expand_nmax >= 0 ? expand_nmax : -1;
+ uschar ** ss = store_get(sizeof(uschar *) * (ecount + 3), GET_UNTAINTED);
+
+ addr->pipe_expandn = ss;
+ if (!filter_thisaddress) filter_thisaddress = US"";
+ *ss++ = string_copy(filter_thisaddress);
+ for (int 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_indent("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_indent("writing filter log as euid %ld\n",
+ (long int)geteuid());
+ if (log_fd < 0)
+ {
+ if (!log_filename)
+ {
+ *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("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_indent("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)
+ {
+ 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;
+ const uschar * list = s;
+
+ for (uschar * ss; ss = string_nextinlist(&list, &sep, NULL, 0); )
+ 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:
+ *error_pointer = fmsg = US string_printing(Ustrlen(expargs[0]) > 1024
+ ? string_sprintf("%.1000s ... (truncated)", expargs[0])
+ : string_copy(expargs[0]));
+ for(uschar * s = fmsg; *s; s++)
+ if (!s[1] && *s == '\n') { *s = '\0'; break; } /* drop trailing newline */
+
+ 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_indent("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_indent("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)
+ 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 || !*return_path)
+ {
+ 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_indent("%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++)
+ {
+ const uschar *s = expargs[i];
+
+ if (!s) continue;
+
+ if (i != mailarg_index_text) for (const uschar * p = s; *p; 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
+ {
+ const uschar *pp;
+ for (pp = p + 1;; pp++)
+ {
+ c = *pp;
+ if (c == ':' && pp != p + 1) break;
+ if (!c || c == ':' || isspace(c))
+ {
+ *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)
+ {
+ const uschar *to = commands->args[mailarg_index_to].u;
+ indent();
+ printf("%sail to: %s%s%s\n", (commands->seen)? "Seen m" : "M",
+ to ? to : US"<default>",
+ commands->command == vacation_command ? " (vacation)" : "",
+ commands->noerror ? " (noerror)" : "");
+ for (i = 1; i < MAILARGS_STRING_COUNT; i++)
+ {
+ const uschar *arg = commands->args[i].u;
+ if (arg)
+ {
+ 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)
+ printf("Return original message\n");
+ }
+ else
+ {
+ const uschar *tt;
+ const uschar *to = commands->args[mailarg_index_to].u;
+ gstring * log_addr = NULL;
+
+ if (!to) to = expand_string(US"$reply_address");
+ while (isspace(*to)) to++;
+
+ for (tt = to; *tt; tt++) /* Get rid of newlines */
+ if (*tt == '\n')
+ {
+ uschar * s = string_copy(to);
+ for (uschar * ss = s; *ss; ss++)
+ if (*ss == '\n') *ss = ' ';
+ to = s;
+ break;
+ }
+
+ DEBUG(D_filter)
+ {
+ debug_printf_indent("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++)
+ {
+ const uschar *arg = commands->args[i].u;
+ if (arg)
+ {
+ int len = Ustrlen(mailargs[i]);
+ while (len++ < 15) debug_printf_indent(" ");
+ debug_printf_indent("%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)
+ {
+ 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)
+ {
+ log_addr = string_catn(log_addr, log_addr ? US"," : US">", 1);
+ log_addr = string_cat (log_addr, recipient);
+ }
+
+ /* Check size */
+
+ if (log_addr && log_addr->ptr > 256)
+ {
+ log_addr = string_catn(log_addr, US", ...", 5);
+ break;
+ }
+
+ /* Move on past this address */
+
+ tt = ss + (*ss ? 1 : 0);
+ while (isspace(*tt)) tt++;
+ }
+
+ if (log_addr)
+ addr = deliver_make_addr(string_from_gstring(log_addr), FALSE);
+ else
+ {
+ addr = deliver_make_addr(US ">**bad-reply**", FALSE);
+ setflag(addr, af_bad_reply);
+ }
+
+ setflag(addr, af_pfr);
+ if (commands->noerror) addr->prop.ignore_error = TRUE;
+ addr->next = *generated;
+ *generated = addr;
+
+ addr->reply = store_get(sizeof(reply_item), GET_UNTAINTED);
+ 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].u);
+ return FF_ERROR;
+ }
+ }
+
+ /* Set up all the remaining string arguments (those other than "to") */
+
+ for (i = 1; i < mailargs_string_passed; i++)
+ {
+ const uschar *ss = commands->args[i].u;
+ *(USS((US addr->reply) + reply_offsets[i])) =
+ ss ? string_copy(ss) : NULL;
+ }
+ }
+ break;
+
+ case testprint_command:
+ if (filter_test != FTEST_NONE || (debug_selector & D_filter) != 0)
+ {
+ const uschar *s = string_printing(expargs[0]);
+ if (filter_test == FTEST_NONE)
+ debug_printf_indent("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)
+{
+const uschar *self, *self_from, *self_to;
+uschar *psself = NULL;
+const uschar *psself_from = NULL, *psself_to = NULL;
+rmark reset_point = store_mark();
+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; h = h->next)
+ {
+ if (h->type == htype_old) continue;
+
+ if (strncmpic(h->text, US"List-", 5) == 0)
+ {
+ uschar * 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)
+ {
+ uschar * s = h->text + 15;
+ Uskip_whitespace(&s);
+ if (strncmpic(s, US"no", 2) != 0) return FALSE;
+ s += 2;
+ Uskip_whitespace(&s);
+ if (*s) 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) self_from = self;
+if (self_to) 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 || deliver_localpart_suffix)
+ {
+ psself = string_sprintf("%s%s%s@%s",
+ deliver_localpart_prefix ? deliver_localpart_prefix : US"",
+ deliver_localpart,
+ deliver_localpart_suffix ? deliver_localpart_suffix : US"",
+ 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(const uschar *filter, int options, address_item **generated,
+ uschar **error)
+{
+int i;
+int yield = FF_ERROR;
+const uschar *ptr = filter;
+const uschar *save_headers_charset = headers_charset;
+filter_cmd *commands = NULL;
+filter_cmd **lastcmdptr = &commands;
+
+DEBUG(D_route) debug_printf("Filter: start of processing\n");
+acl_level++;
+
+/* 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;
+f.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_indent("%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;
+f.filter_running = FALSE;
+headers_charset = save_headers_charset;
+
+acl_level--;
+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
index 0000000..87ebfb1
--- /dev/null
+++ b/src/filtertest.c
@@ -0,0 +1,282 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/* 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 && !stdin_feof())
+ {
+ if (!f.dot_ends)
+ {
+ while ((ch = stdin_getc(GETC_BUFFER_UNLIMITED)) != 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 = stdin_getc(GETC_BUFFER_UNLIMITED)) != 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: ;
+ }
+ if (s == message_body_end || s[-1] != '\n') body_linecount++;
+ }
+debug_printf("%s %d\n", __FUNCTION__, __LINE__);
+
+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, GET_UNTAINTED);
+ 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, filename);
+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) 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)
+ {
+ f.system_filtering = TRUE;
+ f.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);
+ f.enable_dollar_recipients = FALSE;
+ f.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
index 0000000..224666c
--- /dev/null
+++ b/src/functions.h
@@ -0,0 +1,1305 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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. */
+#ifndef _FUNCTIONS_H_
+#define _FUNCTIONS_H_
+
+#include <ctype.h>
+#include <sys/time.h>
+
+
+#ifdef EXIM_PERL
+extern gstring *call_perl_cat(gstring *, uschar **, uschar *,
+ uschar **) WARN_UNUSED_RESULT;
+extern void cleanup_perl(void);
+extern uschar *init_perl(uschar *);
+#endif
+
+
+#ifndef DISABLE_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_der_b64(void * cert);
+extern uschar * tls_cert_fprt_md5(void *);
+extern uschar * tls_cert_fprt_sha1(void *);
+extern uschar * tls_cert_fprt_sha256(void *);
+
+extern void tls_clean_env(void);
+extern BOOL tls_client_start(client_conn_ctx *, smtp_connect_args *,
+ void *, tls_support *, uschar **);
+extern void tls_client_creds_reload(BOOL);
+
+extern void tls_close(void *, int);
+extern BOOL tls_could_getc(void);
+extern void tls_daemon_init(void);
+extern int tls_daemon_tick(void);
+extern BOOL tls_dropprivs_validate_require_cipher(BOOL);
+extern BOOL tls_export_cert(uschar *, size_t, void *);
+extern int tls_feof(void);
+extern int tls_ferror(void);
+extern uschar *tls_field_from_dn(uschar *, const uschar *);
+extern void tls_free_cert(void **);
+extern int tls_getc(unsigned);
+extern uschar *tls_getbuf(unsigned *);
+extern void tls_get_cache(unsigned);
+extern BOOL tls_hasc(void);
+extern BOOL tls_import_cert(const uschar *, void **);
+extern BOOL tls_is_name_for_cert(const uschar *, void *);
+# ifdef USE_OPENSSL
+extern BOOL tls_openssl_options_parse(uschar *, long *);
+# endif
+extern int tls_read(void *, uschar *, size_t);
+extern int tls_server_start(uschar **);
+extern void tls_shutdown_wr(void *);
+extern BOOL tls_smtp_buffered(void);
+extern int tls_ungetc(int);
+#if defined(EXIM_HAVE_INOTIFY) || defined(EXIM_HAVE_KEVENT)
+extern void tls_watch_discard_event(int);
+extern void tls_watch_invalidate(void);
+#endif
+extern int tls_write(void *, const uschar *, size_t, BOOL);
+extern uschar *tls_validate_require_cipher(void);
+extern gstring *tls_version_report(gstring *);
+
+# ifdef SUPPORT_DANE
+extern int tlsa_lookup(const host_item *, dns_answer *, BOOL);
+# endif
+
+#endif /*DISABLE_TLS*/
+
+
+/* Everything else... */
+
+extern acl_block *acl_read(uschar *(*)(void), uschar **);
+extern int acl_check(int, uschar *, uschar *, uschar **, uschar **);
+extern uschar *acl_current_verb(void);
+extern int acl_eval(int, uschar *, uschar **, uschar **);
+
+extern tree_node *acl_var_create(uschar *);
+extern void acl_var_write(uschar *, uschar *, void *);
+
+#ifdef EXPERIMENTAL_ARC
+extern void *arc_ams_setup_sign_bodyhash(void);
+extern const uschar *arc_header_feed(gstring *, BOOL);
+extern gstring *arc_sign(const uschar *, gstring *, uschar **);
+extern void arc_sign_init(void);
+extern const uschar *acl_verify_arc(void);
+extern uschar * fn_arc_domains(void);
+#endif
+
+extern void assert_no_variables(void *, int, const char *, int);
+extern int auth_call_pam(const uschar *, uschar **);
+extern int auth_call_pwcheck(uschar *, uschar **);
+extern int auth_call_radius(const uschar *, uschar **);
+extern int auth_call_saslauthd(const uschar *, const uschar *,
+ const uschar *, const uschar *, uschar **);
+extern int auth_check_serv_cond(auth_instance *);
+extern int auth_check_some_cond(auth_instance *, uschar *, uschar *, int);
+extern int auth_client_item(void *, auth_instance *, const uschar **,
+ unsigned, int, uschar *, int);
+
+
+extern int auth_get_data(uschar **, const uschar *, int);
+extern int auth_get_no64_data(uschar **, uschar *);
+extern int auth_prompt(const uschar *);
+extern int auth_read_input(const uschar *);
+extern gstring * auth_show_supported(gstring *);
+extern uschar *auth_xtextencode(uschar *, int);
+extern int auth_xtextdecode(uschar *, uschar **);
+extern uschar *authenticator_current_name(void);
+
+#ifdef EXPERIMENTAL_ARC
+extern gstring *authres_arc(gstring *);
+#endif
+#ifndef DISABLE_DKIM
+extern gstring *authres_dkim(gstring *);
+#endif
+#ifdef SUPPORT_DMARC
+extern gstring *authres_dmarc(gstring *);
+#endif
+extern gstring *authres_smtpauth(gstring *);
+#ifdef SUPPORT_SPF
+extern gstring *authres_spf(gstring *);
+#endif
+
+extern uschar *b64encode(const uschar *, int);
+extern uschar *b64encode_taint(const uschar *, int, const void *);
+extern int b64decode(const uschar *, uschar **);
+extern int bdat_getc(unsigned);
+extern uschar *bdat_getbuf(unsigned *);
+extern BOOL bdat_hasc(void);
+extern int bdat_ungetc(int);
+extern void bdat_flush_data(void);
+
+extern void bits_clear(unsigned int *, size_t, int *);
+extern void bits_set(unsigned int *, size_t, int *);
+
+extern void cancel_cutthrough_connection(BOOL, const uschar *);
+extern gstring *cat_file(FILE *, gstring *, uschar *);
+extern gstring *cat_file_tls(void *, gstring *, uschar *);
+extern int check_host(void *, const uschar *, const uschar **, uschar **);
+extern uschar **child_exec_exim(int, BOOL, int *, BOOL, int, ...);
+extern pid_t child_open_exim_function(int *, const uschar *);
+extern pid_t child_open_exim2_function(int *, uschar *, uschar *,
+ const uschar *);
+extern pid_t child_open_function(uschar **, uschar **, int,
+ int *, int *, BOOL, const uschar *);
+extern pid_t child_open_uid(const uschar **, const uschar **, int,
+ uid_t *, gid_t *, int *, int *, uschar *, BOOL, const uschar *);
+extern BOOL cleanup_environment(void);
+extern void cutthrough_data_puts(uschar *, int);
+extern void cutthrough_data_put_nl(void);
+extern uschar *cutthrough_finaldot(void);
+extern BOOL cutthrough_flush_send(void);
+extern BOOL cutthrough_headers_send(void);
+extern BOOL cutthrough_predata(void);
+extern void release_cutthrough_connection(const uschar *);
+
+extern void daemon_go(void);
+
+#ifdef EXPERIMENTAL_DCC
+extern int dcc_process(uschar **);
+#endif
+
+extern void debug_logging_activate(const uschar *, const uschar *);
+extern void debug_logging_from_spool(const uschar *);
+extern void debug_logging_stop(BOOL);
+extern void debug_print_argv(const uschar **);
+extern void debug_print_ids(uschar *);
+extern void debug_printf_indent(const char *, ...) PRINTF_FUNCTION(1,2);
+extern void debug_print_string(uschar *);
+extern void debug_print_tree(const char *, tree_node *);
+extern void debug_vprintf(int, const char *, va_list);
+extern void debug_pretrigger_setup(const uschar *);
+extern void debug_pretrigger_discard(void);
+extern void debug_print_socket(int);
+extern void debug_trigger_fire(void);
+
+extern void decode_bits(unsigned int *, size_t, int *,
+ const uschar *, bit_table *, int, uschar *, int);
+extern void delete_pid_file(void);
+extern void deliver_local(address_item *, BOOL);
+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 *);
+
+extern void delivery_re_exec(int);
+
+extern void die_tainted(const uschar *, const uschar *, int);
+extern BOOL directory_make(const uschar *, const uschar *, int, BOOL);
+#ifndef DISABLE_DKIM
+extern uschar *dkim_exim_query_dns_txt(const uschar *);
+extern void dkim_exim_sign_init(void);
+
+extern BOOL dkim_transport_write_message(transport_ctx *,
+ struct ob_dkim *, const uschar ** errstr);
+#endif
+extern dns_address *dns_address_from_rr(dns_answer *, dns_record *);
+extern int dns_basic_lookup(dns_answer *, const uschar *, int);
+extern uschar *dns_build_reverse(const uschar *);
+extern time_t dns_expire_from_soa(dns_answer *, int);
+extern void dns_init(BOOL, BOOL, BOOL);
+extern BOOL dns_is_aa(const dns_answer *);
+extern BOOL dns_is_secure(const dns_answer *);
+extern int dns_lookup(dns_answer *, const uschar *, int, const uschar **);
+extern void dns_pattern_init(void);
+extern int dns_special_lookup(dns_answer *, const uschar *, int, const uschar **);
+extern dns_record *dns_next_rr(const 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 *, unsigned);
+#ifndef DISABLE_EVENT
+extern uschar *event_raise(uschar *, const uschar *, uschar *, int *);
+extern void msg_event_raise(const uschar *, const address_item *);
+#endif
+
+extern int exim_chown_failure(int, const uschar*, uid_t, gid_t);
+extern const uschar * exim_errstr(int);
+extern void exim_exit(int) NORETURN;
+extern void exim_gettime(struct timeval *);
+extern void exim_nullstd(void);
+extern void exim_setugid(uid_t, gid_t, BOOL, const uschar *);
+extern void exim_underbar_exit(int) NORETURN;
+extern void exim_wait_tick(struct timeval *, int);
+extern int exp_bool(address_item *addr,
+ uschar *mtype, uschar *mname, unsigned dgb_opt, uschar *oname, BOOL bvalue,
+ uschar *svalue, BOOL *rvalue);
+extern BOOL expand_check_condition(uschar *, uschar *, uschar *);
+extern uschar *expand_file_big_buffer(const uschar *);
+extern uschar *expand_string(uschar *); /* public, cannot make const */
+extern const uschar *expand_cstring(const uschar *); /* ... so use this one */
+extern uschar *expand_getkeyed(const uschar *, const uschar *);
+
+extern uschar *expand_hide_passwords(uschar * );
+extern uschar *expand_string_copy(const uschar *);
+extern int_eximarith_t expand_string_integer(uschar *, BOOL);
+extern void modify_variable(uschar *, void *);
+
+extern BOOL fd_ready(int, time_t);
+
+extern int filter_interpret(const 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 force_fd(int, int);
+
+extern void header_add(int, const char *, ...);
+extern header_line *header_add_at_position_internal(BOOL, uschar *, BOOL, 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(const uschar *, int *);
+extern void host_build_hostlist(host_item **, const uschar *, BOOL);
+extern ip_address_item *host_build_ifacelist(const uschar *, uschar *);
+extern void host_build_log_info(void);
+extern void host_build_sender_fullhost(void);
+extern int host_find_byname(host_item *, const uschar *, int,
+ const uschar **, BOOL);
+extern int host_find_bydns(host_item *, const uschar *, int, uschar *, uschar *,
+ uschar *, const dnssec_domains *, const uschar **, BOOL *);
+extern ip_address_item *host_find_interfaces(void);
+extern BOOL host_is_in_net(const uschar *, const 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 uschar *imap_utf7_encode(uschar *, const uschar *,
+ uschar, uschar *, uschar **);
+
+extern void invert_address(uschar *, uschar *);
+extern int ip_addr(void *, int, const uschar *, int);
+extern int ip_bind(int, int, uschar *, int);
+extern int ip_connect(int, int, const uschar *, int, int, const blob *);
+extern int ip_connectedsocket(int, const uschar *, int, int,
+ int, host_item *, uschar **, const blob *);
+extern int ip_get_address_family(int);
+extern void ip_keepalive(int, const uschar *, BOOL);
+extern int ip_recv(client_conn_ctx *, uschar *, int, time_t);
+extern int ip_socket(int, int);
+
+extern int ip_tcpsocket(const uschar *, uschar **, int, host_item *);
+extern int ip_unixsocket(const uschar *, uschar **);
+extern int ip_streamsocket(const uschar *, uschar **, int, host_item *);
+
+extern int ipv6_nmtoa(int *, uschar *);
+
+extern uschar *local_part_quote(uschar *);
+extern int log_open_as_exim(const uschar * const);
+extern void log_close_all(void);
+
+extern macro_item * macro_create(const uschar *, const uschar *, BOOL);
+extern BOOL macro_read_assignment(uschar *);
+extern uschar *macros_expand(int, int *, BOOL *);
+extern void mainlog_close(void);
+#ifdef WITH_CONTENT_SCAN
+extern int malware(const uschar *, int);
+extern int malware_in_file(uschar *);
+extern void malware_init(void);
+extern gstring * malware_show_supported(gstring *);
+#endif
+extern int match_address_list(const uschar *, BOOL, BOOL, const uschar **,
+ unsigned int *, int, int, const uschar **);
+extern int match_address_list_basic(const uschar *, const uschar **, int);
+extern int match_check_list(const uschar **, int, tree_node **, unsigned int **,
+ int(*)(void *, const uschar *, const uschar **, uschar **), void *, int,
+ const uschar *, const uschar **);
+extern int match_isinlist(const uschar *, const uschar **, int, tree_node **,
+ unsigned int *, int, BOOL, const uschar **);
+extern int match_check_string(const uschar *, const uschar *, int, BOOL, BOOL, BOOL,
+ const uschar **);
+
+extern void message_start(void);
+extern void message_tidyup(void);
+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(const uschar **);
+extern ssize_t mime_decode_base64(FILE *, FILE *, uschar *);
+extern int mime_regex(const uschar **);
+extern void mime_set_anomaly(int);
+#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 BOOL moan_send_message(uschar *, int, error_block *eblock,
+ header_line *, FILE *, uschar *);
+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 void moan_write_references(FILE *, uschar *);
+extern FILE *modefopen(const uschar *, const char *, mode_t);
+
+extern int open_cutthrough_connection( address_item * addr );
+
+extern uschar *parse_extract_address(const uschar *, uschar **, int *, int *, int *,
+ BOOL);
+extern int parse_forward_list(const uschar *, int, address_item **, uschar **,
+ const uschar *, const uschar *, error_block **);
+extern uschar *parse_find_address_end(const uschar *, BOOL);
+extern const uschar *parse_find_at(const uschar *);
+extern const uschar *parse_fix_phrase(const uschar *, int);
+extern const uschar *parse_message_id(const uschar *, uschar **, uschar **);
+extern const uschar *parse_quote_2047(const uschar *, int, const uschar *,
+ BOOL);
+extern const uschar *parse_date_time(const uschar *str, time_t *t);
+extern void priv_drop_temp(const uid_t, const gid_t);
+extern void priv_restore(void);
+extern int vaguely_random_number(int);
+#ifndef DISABLE_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 unsigned queue_count(void);
+extern unsigned queue_count_cached(void);
+extern void queue_list(int, uschar **, int);
+#ifndef DISABLE_QUEUE_RAMP
+extern void queue_notify_daemon(const uschar * hostname);
+#endif
+extern void queue_run(uschar *, uschar *, BOOL);
+
+extern int random_number(int);
+extern const uschar *rc_to_string(int);
+extern int rda_interpret(redirect_block *, int, const uschar *, const uschar *,
+ const uschar *, const uschar *, const uschar *, const ugid_block *, address_item **,
+ uschar **, error_block **, int *, const 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(BOOL);
+extern void readconf_options_from_list(optionlist *, unsigned, const uschar *, uschar *);
+extern BOOL readconf_print(const uschar *, uschar *, BOOL);
+extern uschar *readconf_printtime(int);
+extern uschar *readconf_readname(uschar *, int, uschar *);
+extern int readconf_readtime(const uschar *, int, BOOL);
+extern void readconf_rest(void);
+extern uschar *readconf_retry_error(const uschar *, const uschar *, int *, int *);
+extern void readconf_save_config(const uschar *);
+extern void read_message_body(BOOL);
+extern void receive_bomb_out(uschar *, uschar *) NORETURN;
+extern BOOL receive_check_fs(int);
+extern BOOL receive_check_set_sender(uschar *);
+extern BOOL receive_msg(BOOL);
+extern int_eximarith_t receive_statvfs(BOOL, int *);
+extern void receive_swallow_smtp(void);
+#ifdef WITH_CONTENT_SCAN
+extern int regex(const uschar **);
+#endif
+extern BOOL regex_match(const pcre2_code *, const uschar *, int, uschar **);
+extern BOOL regex_match_and_setup(const pcre2_code *, const uschar *, int, int);
+extern const pcre2_code *regex_must_compile(const uschar *, BOOL, BOOL);
+extern void retry_add_item(address_item *, uschar *, int);
+extern BOOL retry_check_address(const uschar *, host_item *, uschar *, BOOL,
+ uschar **, uschar **);
+extern retry_config *retry_find_config(const uschar *, const uschar *, int, int);
+extern BOOL retry_ultimate_address_timeout(uschar *, const uschar *,
+ dbdata_retry *, time_t);
+extern void retry_update(address_item **, address_item **, address_item **);
+extern const uschar *rewrite_address(const uschar *, BOOL, BOOL, rewrite_rule *, int);
+extern const uschar *rewrite_address_qualify(const uschar *, BOOL);
+extern header_line *rewrite_header(header_line *,
+ const uschar *, const uschar *,
+ rewrite_rule *, int, BOOL);
+extern const uschar *rewrite_one(const uschar *, int, BOOL *, BOOL, uschar *,
+ rewrite_rule *);
+extern void rewrite_test(const uschar *);
+extern uschar *rfc2047_decode2(uschar *, BOOL, const 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(const uschar *, const uschar *, unsigned *);
+extern int route_check_suffix(const uschar *, const uschar *, unsigned *);
+extern BOOL route_findgroup(uschar *, gid_t *);
+extern BOOL route_finduser(const 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 gstring * route_show_supported(gstring *);
+extern void route_tidyup(void);
+extern uschar *router_current_name(void);
+
+extern uschar *search_args(int, uschar *, uschar *, uschar **, const uschar *);
+extern uschar *search_find(void *, const uschar *, uschar *, int,
+ const uschar *, int, int, int *, const uschar *);
+extern int search_findtype(const uschar *, int);
+extern int search_findtype_partial(const uschar *, int *, const uschar **, int *,
+ int *, const uschar **);
+extern void *search_open(const 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(hctx *, const uschar *, int, uschar *);
+extern void sha1_mid(hctx *, const uschar *);
+extern void sha1_start(hctx *);
+extern int sieve_interpret(const uschar *, int, const uschar *,
+ const uschar *, const uschar *, const uschar *,
+ address_item **, uschar **);
+extern void sigalrm_handler(int);
+extern int smtp_boundsock(smtp_connect_args *);
+extern void smtp_closedown(uschar *);
+extern void smtp_command_timeout_exit(void) NORETURN;
+extern void smtp_command_sigterm_exit(void) NORETURN;
+extern void smtp_data_timeout_exit(void) NORETURN;
+extern void smtp_data_sigint_exit(void) NORETURN;
+extern void smtp_deliver_init(void);
+extern uschar *smtp_cmd_hist(void);
+extern int smtp_connect(smtp_connect_args *, const blob *);
+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 *,
+ uschar **, uschar *);
+extern BOOL smtp_get_port(uschar *, address_item *, int *, uschar *);
+extern int smtp_getc(unsigned);
+extern uschar *smtp_getbuf(unsigned *);
+extern void smtp_get_cache(unsigned);
+extern BOOL smtp_hasc(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 **, BOOL);
+extern void smtp_proxy_tls(void *, uschar *, size_t, int *, int, const uschar *) NORETURN;
+extern BOOL smtp_read_response(void *, uschar *, int, int, int);
+extern void *smtp_reset(void *);
+extern void smtp_respond(uschar *, int, BOOL, uschar *);
+extern void smtp_notquit_exit(uschar *, uschar *, uschar *, ...);
+extern void smtp_port_for_connect(host_item *, int);
+extern void smtp_send_prohibition_message(int, uschar *);
+extern int smtp_setup_msg(void);
+extern int smtp_sock_connect(smtp_connect_args *, int, const blob *);
+extern BOOL smtp_start_session(void);
+extern int smtp_ungetc(int);
+extern BOOL smtp_verify_helo(void);
+extern int smtp_write_command(void *, int, const char *, ...) PRINTF_FUNCTION(3,4);
+#ifdef WITH_CONTENT_SCAN
+extern int spam(const uschar **);
+extern FILE *spool_mbox(unsigned long *, const uschar *, uschar **);
+#endif
+extern void spool_clear_header_globals(void);
+extern BOOL spool_move_message(uschar *, uschar *, uschar *, uschar *);
+extern int spool_open_datafile(uschar *);
+extern int spool_open_temp(uschar *);
+extern int spool_read_header(uschar *, BOOL, BOOL);
+extern uschar *spool_sender_from_msgid(const uschar *);
+extern int spool_write_header(uschar *, int, uschar **);
+extern int stdin_getc(unsigned);
+extern int stdin_feof(void);
+extern int stdin_ferror(void);
+extern BOOL stdin_hasc(void);
+extern int stdin_ungetc(int);
+
+extern void store_exit(void);
+extern void store_init(void);
+extern void store_writeprotect(int);
+
+extern gstring *string_append(gstring *, int, ...) WARN_UNUSED_RESULT;
+extern gstring *string_append_listele(gstring *, uschar, const uschar *) WARN_UNUSED_RESULT;
+extern gstring *string_append_listele_n(gstring *, uschar, const uschar *, unsigned) WARN_UNUSED_RESULT;
+extern gstring *string_append2_listele_n(gstring *, const uschar *, const uschar *, unsigned) WARN_UNUSED_RESULT;
+extern uschar *string_base62(unsigned long int);
+extern gstring *string_cat (gstring *, const uschar * ) WARN_UNUSED_RESULT;
+extern gstring *string_catn(gstring *, const uschar *, int) WARN_UNUSED_RESULT;
+extern int string_compare_by_pointer(const void *, const void *);
+extern uschar *string_copy_dnsdomain(uschar *);
+extern uschar *string_copy_malloc(const uschar *);
+extern uschar *string_dequote(const uschar **);
+extern uschar *string_format_size(int, uschar *);
+extern int string_interpret_escape(const uschar **);
+extern int string_is_ip_address(const uschar *, int *);
+#ifdef SUPPORT_I18N
+extern BOOL string_is_utf8(const uschar *);
+#endif
+extern const uschar *string_printing2(const uschar *, int);
+extern uschar *string_split_message(uschar *);
+extern uschar *string_unprinting(uschar *);
+#ifdef SUPPORT_I18N
+extern uschar *string_address_utf8_to_alabel(const uschar *, uschar **);
+extern uschar *string_domain_alabel_to_utf8(const uschar *, uschar **);
+extern uschar *string_domain_utf8_to_alabel(const uschar *, uschar **);
+extern uschar *string_localpart_alabel_to_utf8(const uschar *, uschar **);
+extern uschar *string_localpart_utf8_to_alabel(const uschar *, uschar **);
+#endif
+
+#define string_format(buf, siz, fmt, ...) \
+ string_format_trc(buf, siz, US __FUNCTION__, __LINE__, fmt, __VA_ARGS__)
+extern BOOL string_format_trc(uschar *, int, const uschar *, unsigned,
+ const char *, ...) ALMOST_PRINTF(5,6);
+
+#define string_vformat(g, flgs, fmt, ap) \
+ string_vformat_trc(g, US __FUNCTION__, __LINE__, \
+ STRING_SPRINTF_BUFFER_SIZE, flgs, fmt, ap)
+extern gstring *string_vformat_trc(gstring *, const uschar *, unsigned,
+ unsigned, unsigned, const char *, va_list);
+
+#define string_open_failed(fmt, ...) \
+ string_open_failed_trc(US __FUNCTION__, __LINE__, fmt, __VA_ARGS__)
+extern uschar *string_open_failed_trc(const uschar *, unsigned,
+ const char *, ...) PRINTF_FUNCTION(3,4);
+
+#define string_nextinlist(lp, sp, b, l) \
+ string_nextinlist_trc((lp), (sp), (b), (l), US __FUNCTION__, __LINE__)
+extern uschar *string_nextinlist_trc(const uschar **listptr, int *separator, uschar *buffer, int buflen,
+ const uschar * func, int line);
+
+extern int strcmpic(const uschar *, const uschar *);
+extern int strncmpic(const uschar *, const uschar *, int);
+extern uschar *strstric(uschar *, uschar *, BOOL);
+extern const uschar *strstric_c(const uschar *, const uschar *, BOOL);
+
+extern int test_harness_fudged_queue_time(int);
+extern void tcp_init(void);
+#ifdef EXIM_TFO_PROBE
+extern void tfo_probe(void);
+#endif
+extern void tls_modify_variables(tls_support *);
+extern uschar *tod_stamp(int);
+
+extern BOOL transport_check_waiting(const uschar *, const uschar *, int, uschar *,
+ oicf, void*);
+extern uschar *transport_current_name(void);
+extern void transport_do_pass_socket(const uschar *, const uschar *,
+ const uschar *, uschar *, int);
+extern void transport_init(void);
+extern BOOL transport_pass_socket(const uschar *, const uschar *, const uschar *, uschar *, int
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ , unsigned, unsigned, unsigned
+#endif
+ );
+extern uschar *transport_rcpt_address(address_item *, BOOL);
+extern BOOL transport_set_up_command(const uschar ***, const uschar *,
+ BOOL, int, address_item *, BOOL, const uschar *, uschar **);
+extern void transport_update_waiting(host_item *, uschar *);
+extern BOOL transport_write_block(transport_ctx *, uschar *, int, BOOL);
+extern void transport_write_reset(int);
+extern BOOL transport_write_string(int, const char *, ...);
+extern BOOL transport_headers_send(transport_ctx *,
+ BOOL (*)(transport_ctx *, uschar *, int));
+extern gstring * transport_show_supported(gstring *);
+extern BOOL transport_write_message(transport_ctx *, int);
+extern void tree_add_duplicate(const uschar *, address_item *);
+extern void tree_add_nonrecipient(const uschar *);
+extern void tree_add_unusable(const host_item *);
+extern void tree_dup(tree_node **, tree_node *);
+extern int tree_insertnode(tree_node **, tree_node *);
+extern tree_node *tree_search(tree_node *, const 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
+#ifdef SUPPORT_I18N
+extern gstring *utf8_version_report(gstring *);
+#endif
+
+extern int verify_address(address_item *, FILE *, int, int, int, int,
+ uschar *, uschar *, BOOL *);
+extern int verify_check_dnsbl(int, const uschar **, 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(BOOL);
+extern int verify_check_given_host(const uschar **, const host_item *);
+extern int verify_check_this_host(const uschar **, unsigned int *,
+ const uschar*, const uschar *, const uschar **);
+extern address_item *verify_checked_sender(uschar *);
+extern void verify_get_ident(int);
+extern void verify_quota(uschar *);
+extern int verify_quota_call(const uschar *, int, int, uschar **);
+extern BOOL verify_sender(int *, uschar **);
+extern BOOL verify_sender_preliminary(int *, uschar **);
+extern void version_init(void);
+
+extern BOOL write_chunk(transport_ctx *, uschar *, int);
+extern ssize_t write_to_fd_buf(int, const uschar *, size_t);
+
+
+/******************************************************************************/
+/* Predicate: if an address is in a tainted pool.
+By extension, a variable pointing to this address is tainted.
+*/
+
+static inline BOOL
+is_tainted(const void * p)
+{
+#if defined(COMPILE_UTILITY) || defined(MACRO_PREDEF) || defined(EM_VERSION_C)
+return FALSE;
+
+#else
+extern BOOL is_tainted_fn(const void *);
+return is_tainted_fn(p);
+#endif
+}
+
+static inline BOOL
+is_incompatible(const void * old, const void * new)
+{
+#if defined(COMPILE_UTILITY) || defined(MACRO_PREDEF) || defined(EM_VERSION_C)
+return FALSE;
+
+#else
+extern BOOL is_incompatible_fn(const void *, const void *);
+return is_incompatible_fn(old, new);
+#endif
+}
+
+/******************************************************************************/
+/* String functions */
+static inline uschar * __Ustrcat(uschar * dst, const uschar * src, const char * func, int line)
+{
+#if !defined(COMPILE_UTILITY) && !defined(MACRO_PREDEF)
+if (!is_tainted(dst) && is_tainted(src)) die_tainted(US"Ustrcat", CUS func, line);
+#endif
+return US strcat(CS dst, CCS src);
+}
+static inline uschar * __Ustrcpy(uschar * dst, const uschar * src, const char * func, int line)
+{
+#if !defined(COMPILE_UTILITY) && !defined(MACRO_PREDEF)
+if (!is_tainted(dst) && is_tainted(src)) die_tainted(US"Ustrcpy", CUS func, line);
+#endif
+return US strcpy(CS dst, CCS src);
+}
+static inline uschar * __Ustrncat(uschar * dst, const uschar * src, size_t n, const char * func, int line)
+{
+#if !defined(COMPILE_UTILITY) && !defined(MACRO_PREDEF)
+if (!is_tainted(dst) && is_tainted(src)) die_tainted(US"Ustrncat", CUS func, line);
+#endif
+return US strncat(CS dst, CCS src, n);
+}
+static inline uschar * __Ustrncpy(uschar * dst, const uschar * src, size_t n, const char * func, int line)
+{
+#if !defined(COMPILE_UTILITY) && !defined(MACRO_PREDEF)
+if (!is_tainted(dst) && is_tainted(src)) die_tainted(US"Ustrncpy", CUS func, line);
+#endif
+return US strncpy(CS dst, CCS src, n);
+}
+/*XXX will likely need unchecked copy also */
+
+
+/* Advance the string pointer given over any whitespace.
+Return the next char as there's enought places using it to be useful. */
+
+#define Uskip_whitespace(sp) skip_whitespace(CUSS sp)
+
+static inline uschar skip_whitespace(const uschar ** sp)
+{ while (isspace(**sp)) (*sp)++; return **sp; }
+
+
+/******************************************************************************/
+
+#if !defined(MACRO_PREDEF) && !defined(COMPILE_UTILITY)
+/* exim_chown - in some NFSv4 setups *seemes* to be an issue with
+chown(<exim-uid>, <exim-gid>).
+
+Probably because the idmapping is broken, misconfigured or set up in
+an unusal way. (see Bug 2931). As I'm not sure, if this was a single
+case of misconfiguration, or if there are more such broken systems
+out, I try to impose as least impact as possible and for now just write
+a panic log entry pointing to the bug report. You're encouraged to
+contact the developers, if you experience this issue.
+
+fd the file descriptor (or -1 if not valid)
+name the file name for error messages or for file operations,
+ if fd is < 0
+owner the owner
+group the group
+
+returns 0 on success, -1 on failure */
+
+static inline int
+exim_fchown(int fd, uid_t owner, gid_t group, const uschar *name)
+{
+return fchown(fd, owner, group)
+ ? exim_chown_failure(fd, name, owner, group) : 0;
+}
+
+static inline int
+exim_chown(const uschar *name, uid_t owner, gid_t group)
+{
+return chown(CCS name, owner, group)
+ ? exim_chown_failure(-1, name, owner, group) : 0;
+}
+#endif /* !MACRO_PREDEF && !COMPILE_UTILITY */
+
+/******************************************************************************/
+/* String functions */
+
+#if !defined(MACRO_PREDEF)
+/*************************************************
+* Copy and save string *
+*************************************************/
+
+/* This function assumes that memcpy() is faster than strcpy().
+The result is explicitly nul-terminated.
+*/
+
+static inline uschar *
+string_copyn_taint_trc(const uschar * s, unsigned len,
+ const void * proto_mem, const char * func, int line)
+{
+uschar * ss;
+unsigned slen = Ustrlen(s);
+if (len > slen) len = slen;
+ss = store_get_3(len + 1, proto_mem, func, line);
+memcpy(ss, s, len);
+ss[len] = '\0';
+return ss;
+}
+
+static inline uschar *
+string_copy_taint_trc(const uschar * s, const void * proto_mem, const char * func, int line)
+{ return string_copyn_taint_trc(s, Ustrlen(s), proto_mem, func, line); }
+
+static inline uschar *
+string_copyn_trc(const uschar * s, unsigned len, const char * func, int line)
+{ return string_copyn_taint_trc(s, len, s, func, line); }
+static inline uschar *
+string_copy_trc(const uschar * s, const char * func, int line)
+{ return string_copy_taint_trc(s, s, func, line); }
+
+
+/* String-copy functions explicitly setting the taint status */
+
+#define string_copyn_taint(s, len, proto_mem) \
+ string_copyn_taint_trc((s), (len), (proto_mem), __FUNCTION__, __LINE__)
+#define string_copy_taint(s, proto_mem) \
+ string_copy_taint_trc((s), (proto_mem), __FUNCTION__, __LINE__)
+
+/* Simple string-copy functions maintaining the taint */
+
+#define string_copyn(s, len) \
+ string_copyn_trc((s), (len), __FUNCTION__, __LINE__)
+#define string_copy(s) \
+ string_copy_trc((s), __FUNCTION__, __LINE__)
+
+
+/*************************************************
+* Copy, lowercase and save string *
+*************************************************/
+
+/*
+Argument: string to copy
+Returns: copy of string in new store, with letters lowercased
+*/
+
+static inline uschar *
+string_copylc(const uschar * s)
+{
+uschar * ss = store_get(Ustrlen(s) + 1, s);
+uschar * p = ss;
+while (*s) *p++ = tolower(*s++);
+*p = 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
+*/
+
+static inline uschar *
+string_copynlc(uschar * s, int n)
+{
+uschar * ss = store_get(n + 1, s);
+uschar * p = ss;
+while (n-- > 0) *p++ = tolower(*s++);
+*p = 0;
+return ss;
+}
+
+
+# ifndef COMPILE_UTILITY
+/*************************************************
+* Copy and save string in longterm store *
+*************************************************/
+
+/* This function assumes that memcpy() is faster than strcpy().
+
+Argument: string to copy
+Returns: copy of string in new store
+*/
+
+static inline uschar *
+string_copy_perm(const uschar *s, BOOL force_taint)
+{
+int old_pool = store_pool;
+int len = Ustrlen(s) + 1;
+uschar *ss;
+
+store_pool = POOL_PERM;
+ss = store_get(len, force_taint ? GET_TAINTED : s);
+memcpy(ss, s, len);
+store_pool = old_pool;
+return ss;
+}
+# endif
+
+
+
+/* sprintf into a buffer, taint-unchecked */
+
+static inline void
+string_format_nt(uschar * buf, int siz, const char * fmt, ...)
+{
+gstring gs = { .size = siz, .ptr = 0, .s = buf };
+va_list ap;
+va_start(ap, fmt);
+(void) string_vformat(&gs, SVFMT_TAINT_NOCHK, fmt, ap);
+va_end(ap);
+}
+
+
+
+/******************************************************************************/
+/* Growable-string functions */
+
+/* Create a growable-string with some preassigned space */
+
+#define string_get_tainted(size, proto_mem) \
+ string_get_tainted_trc((size), (proto_mem), __FUNCTION__, __LINE__)
+
+static inline gstring *
+string_get_tainted_trc(unsigned size, const void * proto_mem, const char * func, unsigned line)
+{
+gstring * g = store_get_3(sizeof(gstring) + size, proto_mem, func, line);
+g->size = size; /*XXX would be good if we could see the actual alloc size */
+g->ptr = 0;
+g->s = US(g + 1);
+return g;
+}
+
+#define string_get(size) \
+ string_get_trc((size), __FUNCTION__, __LINE__)
+
+static inline gstring *
+string_get_trc(unsigned size, const char * func, unsigned line)
+{
+return string_get_tainted_trc(size, GET_UNTAINTED, func, line);
+}
+
+/* NUL-terminate the C string in the growable-string, and return it. */
+
+static inline uschar *
+string_from_gstring(gstring * g)
+{
+if (!g) return NULL;
+g->s[g->ptr] = '\0';
+return g->s;
+}
+
+static inline unsigned
+gstring_length(const gstring * g)
+{
+return g ? (unsigned)g->ptr : 0;
+}
+
+
+#define gstring_release_unused(g) \
+ gstring_release_unused_trc(g, __FUNCTION__, __LINE__)
+
+static inline void
+gstring_release_unused_trc(gstring * g, const char * file, unsigned line)
+{
+if (g) store_release_above_3(g->s + (g->size = g->ptr + 1), file, line);
+}
+
+
+/* sprintf-append to a growable-string */
+
+#define string_fmt_append(g, fmt, ...) \
+ string_fmt_append_f_trc(g, US __FUNCTION__, __LINE__, \
+ SVFMT_EXTEND|SVFMT_REBUFFER, fmt, __VA_ARGS__)
+
+#define string_fmt_append_f(g, flgs, fmt, ...) \
+ string_fmt_append_f_trc(g, US __FUNCTION__, __LINE__, \
+ flgs, fmt, __VA_ARGS__)
+
+static inline gstring *
+string_fmt_append_f_trc(gstring * g, const uschar * func, unsigned line,
+ unsigned flags, const char *format, ...)
+{
+va_list ap;
+va_start(ap, format);
+g = string_vformat_trc(g, func, line, STRING_SPRINTF_BUFFER_SIZE,
+ flags, format, ap);
+va_end(ap);
+return g;
+}
+
+
+/* Copy the content of a string to tainted memory. The proto_mem arg
+will always be tainted, and suitable as a prototype. */
+
+static inline void
+gstring_rebuffer(gstring * g, const void * proto_mem)
+{
+uschar * s = store_get_3(g->size, proto_mem, __FUNCTION__, __LINE__);
+memcpy(s, g->s, g->ptr);
+g->s = s;
+}
+
+
+# ifndef COMPILE_UTILITY
+/******************************************************************************/
+/* Use store_malloc for DNSA structs, and explicit frees. Using the same pool
+for them as the strings we proceed to copy from them meant they could not be
+released, hence blowing 64k for every DNS lookup. That mounted up. With malloc
+we do have to take care over marking tainted all copied strings. A separate pool
+could be used and would handle that implicitly. */
+
+#define store_get_dns_answer() store_get_dns_answer_trc(CUS __FUNCTION__, __LINE__)
+
+static inline dns_answer *
+store_get_dns_answer_trc(const uschar * func, unsigned line)
+{
+return store_malloc_3(sizeof(dns_answer), CCS func, line);
+}
+
+#define store_free_dns_answer(dnsa) store_free_dns_answer_trc(dnsa, CUS __FUNCTION__, __LINE__)
+
+static inline void
+store_free_dns_answer_trc(dns_answer * dnsa, const uschar * func, unsigned line)
+{
+store_free_3(dnsa, CCS func, line);
+}
+
+/******************************************************************************/
+/* Routines with knowledge of spool layout */
+
+static inline void
+spool_pname_buf(uschar * buf, int len)
+{
+snprintf(CS buf, len, "%s/%s/input", spool_directory, queue_name);
+}
+
+static inline uschar *
+spool_dname(const uschar * purpose, uschar * subdir)
+{
+return string_sprintf("%s/%s/%s/%s",
+ spool_directory, queue_name, purpose, subdir);
+}
+# endif
+
+static inline uschar *
+spool_q_sname(const uschar * purpose, const uschar * q, uschar * subdir)
+{
+return string_sprintf("%s%s%s%s%s",
+ q, *q ? "/" : "",
+ purpose,
+ *subdir ? "/" : "", subdir);
+}
+
+static inline uschar *
+spool_sname(const uschar * purpose, uschar * subdir)
+{
+return spool_q_sname(purpose, queue_name, subdir);
+}
+
+static inline uschar *
+spool_q_fname(const uschar * purpose, const uschar * q,
+ const uschar * subdir, const uschar * fname, const uschar * suffix)
+{
+return string_sprintf("%s/%s/%s/%s/%s%s",
+ spool_directory, q, purpose, subdir, fname, suffix);
+}
+
+static inline uschar *
+spool_fname(const uschar * purpose, const uschar * subdir, const uschar * fname,
+ const uschar * suffix)
+{
+#ifdef COMPILE_UTILITY /* version avoiding string-extension */
+int len = Ustrlen(spool_directory) + 1 + Ustrlen(queue_name) + 1 + Ustrlen(purpose) + 1
+ + Ustrlen(subdir) + 1 + Ustrlen(fname) + Ustrlen(suffix) + 1;
+uschar * buf = store_get(len, GET_UNTAINTED);
+string_format(buf, len, "%s/%s/%s/%s/%s%s",
+ spool_directory, queue_name, purpose, subdir, fname, suffix);
+return buf;
+#else
+return spool_q_fname(purpose, queue_name, subdir, fname, suffix);
+#endif
+}
+
+static inline void
+set_subdir_str(uschar * subdir_str, const uschar * name,
+ int search_sequence)
+{
+subdir_str[0] = split_spool_directory == (search_sequence == 0)
+ ? name[5] : '\0';
+subdir_str[1] = '\0';
+}
+
+/******************************************************************************/
+/* Time calculations */
+
+/* Diff two times (later, earlier) returning diff in 1st arg */
+static inline void
+timediff(struct timeval * later, const struct timeval * earlier)
+{
+later->tv_sec -= earlier->tv_sec;
+if ((later->tv_usec -= earlier->tv_usec) < 0)
+ {
+ later->tv_sec--;
+ later->tv_usec += 1000*1000;
+ }
+}
+
+static inline void
+timesince(struct timeval * diff, const struct timeval * then)
+{
+gettimeofday(diff, NULL);
+timediff(diff, then);
+}
+
+static inline uschar *
+string_timediff(const struct timeval * diff)
+{
+static uschar buf[sizeof("0.000s")];
+
+if (diff->tv_sec >= 5 || !LOGGING(millisec))
+ return readconf_printtime((int)diff->tv_sec);
+
+snprintf(CS buf, sizeof(buf), "%u.%03us", (uint)diff->tv_sec, (uint)diff->tv_usec/1000);
+return buf;
+}
+
+
+static inline uschar *
+string_timesince(const struct timeval * then)
+{
+struct timeval diff;
+timesince(&diff, then);
+return string_timediff(&diff);
+}
+
+static inline void
+report_time_since(const struct timeval * t0, const uschar * where)
+{
+# ifdef MEASURE_TIMING
+struct timeval diff;
+timesince(&diff, t0);
+fprintf(stderr, "%d %s:\t%ld.%06ld\n",
+ (uint)getpid(), where, (long)diff.tv_sec, (long)diff.tv_usec);
+# endif
+}
+
+
+static inline void
+testharness_pause_ms(int millisec)
+{
+#ifndef MEASURE_TIMING
+if (f.running_in_test_harness && f.testsuite_delays) millisleep(millisec);
+#endif
+}
+
+/******************************************************************************/
+/* Taint-checked file opens */
+
+static inline int
+exim_open2(const char *pathname, int flags)
+{
+if (!is_tainted(pathname)) return open(pathname, flags);
+log_write(0, LOG_MAIN|LOG_PANIC, "Tainted filename '%s'", pathname);
+errno = EACCES;
+return -1;
+}
+static inline int
+exim_open(const char *pathname, int flags, mode_t mode)
+{
+if (!is_tainted(pathname)) return open(pathname, flags, mode);
+log_write(0, LOG_MAIN|LOG_PANIC, "Tainted filename '%s'", pathname);
+errno = EACCES;
+return -1;
+}
+#ifdef EXIM_HAVE_OPENAT
+static inline int
+exim_openat(int dirfd, const char *pathname, int flags)
+{
+if (!is_tainted(pathname)) return openat(dirfd, pathname, flags);
+log_write(0, LOG_MAIN|LOG_PANIC, "Tainted filename '%s'", pathname);
+errno = EACCES;
+return -1;
+}
+static inline int
+exim_openat4(int dirfd, const char *pathname, int flags, mode_t mode)
+{
+if (!is_tainted(pathname)) return openat(dirfd, pathname, flags, mode);
+log_write(0, LOG_MAIN|LOG_PANIC, "Tainted filename '%s'", pathname);
+errno = EACCES;
+return -1;
+}
+#endif
+
+static inline FILE *
+exim_fopen(const char *pathname, const char *mode)
+{
+if (!is_tainted(pathname)) return fopen(pathname, mode);
+log_write(0, LOG_MAIN|LOG_PANIC, "Tainted filename '%s'", pathname);
+errno = EACCES;
+return NULL;
+}
+
+static inline DIR *
+exim_opendir(const uschar * name)
+{
+if (!is_tainted(name)) return opendir(CCS name);
+log_write(0, LOG_MAIN|LOG_PANIC, "Tainted dirname '%s'", name);
+errno = EACCES;
+return NULL;
+}
+
+/******************************************************************************/
+# if !defined(COMPILE_UTILITY)
+/* Process manipulation */
+
+static inline pid_t
+exim_fork(const unsigned char * purpose)
+{
+pid_t pid;
+DEBUG(D_any) debug_printf("%s forking for %s\n", process_purpose, purpose);
+if ((pid = fork()) == 0)
+ {
+ process_purpose = purpose;
+ DEBUG(D_any) debug_printf("postfork: %s\n", purpose);
+ }
+else
+ {
+ testharness_pause_ms(100); /* let child work */
+ DEBUG(D_any) debug_printf("%s forked for %s: %d\n", process_purpose, purpose, (int)pid);
+ }
+return pid;
+}
+
+
+static inline pid_t
+child_open_exim(int * fdptr, const uschar * purpose)
+{ return child_open_exim_function(fdptr, purpose); }
+
+static inline pid_t
+child_open_exim2(int * fdptr, uschar * sender,
+ uschar * sender_auth, const uschar * purpose)
+{ return child_open_exim2_function(fdptr, sender, sender_auth, purpose); }
+
+static inline pid_t
+child_open(uschar **argv, uschar **envp, int newumask, int *infdptr,
+ int *outfdptr, BOOL make_leader, const uschar * purpose)
+{ return child_open_function(argv, envp, newumask, infdptr,
+ outfdptr, make_leader, purpose);
+}
+
+/* Return 1 if fd is usable per pollbits, else 0 */
+static inline int
+poll_one_fd(int fd, short pollbits, int tmo_millisec)
+{
+struct pollfd p = {.fd = fd, .events = pollbits};
+return poll(&p, 1, tmo_millisec);
+}
+
+/******************************************************************************/
+/* Client-side smtp log string, for debug */
+
+static inline void
+smtp_debug_cmd(const uschar * buf, int mode)
+{
+HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" SMTP%c> %s\n",
+ mode == SCMD_BUFFER ? '|' : mode == SCMD_MORE ? '+' : '>', buf);
+
+# ifndef DISABLE_CLIENT_CMD_LOG
+ {
+ int len = Ustrcspn(buf, " \n");
+ int old_pool = store_pool;
+ store_pool = POOL_PERM; /* Main pool ACL allocations eg. callouts get released */
+ client_cmd_log = string_append_listele_n(client_cmd_log, ':', buf, MIN(len, 8));
+ if (mode == SCMD_BUFFER)
+ {
+ client_cmd_log = string_catn(client_cmd_log, US"|", 1);
+ (void) string_from_gstring(client_cmd_log);
+ }
+ store_pool = old_pool;
+ }
+# endif
+}
+
+
+static inline void
+smtp_debug_cmd_report(void)
+{
+# ifndef DISABLE_CLIENT_CMD_LOG
+debug_printf("cmdlog: '%s'\n", client_cmd_log ? client_cmd_log->s : US"(unset)");
+# endif
+}
+
+
+
+# endif /* !COMPILE_UTILITY */
+
+/******************************************************************************/
+#endif /* !MACRO_PREDEF */
+
+#endif /* _FUNCTIONS_H_ */
+
+/* vi: aw
+*/
+/* End of functions.h */
diff --git a/src/globals.c b/src/globals.c
new file mode 100644
index 0000000..ff246fe
--- /dev/null
+++ b/src/globals.c
@@ -0,0 +1,1653 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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,
+ OPT_OFF(auth_instance, client_condition) },
+ { "client_set_id", opt_stringptr | opt_public,
+ OPT_OFF(auth_instance, set_client_id) },
+ { "driver", opt_stringptr | opt_public,
+ OPT_OFF(auth_instance, driver_name) },
+ { "public_name", opt_stringptr | opt_public,
+ OPT_OFF(auth_instance, public_name) },
+ { "server_advertise_condition", opt_stringptr | opt_public,
+ OPT_OFF(auth_instance, advertise_condition)},
+ { "server_condition", opt_stringptr | opt_public,
+ OPT_OFF(auth_instance, server_condition) },
+ { "server_debug_print", opt_stringptr | opt_public,
+ OPT_OFF(auth_instance, server_debug_string) },
+ { "server_mail_auth_condition", opt_stringptr | opt_public,
+ OPT_OFF(auth_instance, mail_auth_condition) },
+ { "server_set_id", opt_stringptr | opt_public,
+ OPT_OFF(auth_instance, set_id) }
+};
+
+int optionlist_auths_size = nelem(optionlist_auths);
+
+/* 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;
+BOOL opt_perl_taintmode = 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 LOOKUP_REDIS
+uschar *redis_servers = NULL;
+#endif
+
+#ifdef LOOKUP_SQLITE
+uschar *sqlite_dbfile = NULL;
+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. Note that these are
+only used for smtp connections, not for service-daemon access. */
+
+tls_support tls_in = {
+ .active = {.sock = -1}
+ /* all other elements zero */
+};
+tls_support tls_out = {
+ .active = {.sock = -1},
+ /* all other elements zero */
+};
+
+uschar *dsn_envid = NULL;
+int dsn_ret = 0;
+const pcre2_code *regex_DSN = NULL;
+uschar *dsn_advertise_hosts = NULL;
+
+#ifndef DISABLE_TLS
+BOOL gnutls_compat_mode = FALSE;
+BOOL gnutls_allow_auto_pkcs11 = FALSE;
+uschar *hosts_require_alpn = NULL;
+uschar *openssl_options = NULL;
+const pcre2_code *regex_STARTTLS = NULL;
+uschar *tls_advertise_hosts = US"*";
+uschar *tls_alpn = US"smtp:esmtp";
+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;
+uschar *tls_eccurve = US"auto";
+# ifndef DISABLE_OCSP
+uschar *tls_ocsp_file = NULL;
+# endif
+uschar *tls_privatekey = NULL;
+BOOL tls_remember_esmtp = FALSE;
+uschar *tls_require_ciphers = NULL;
+# ifndef DISABLE_TLS_RESUME
+uschar *tls_resumption_hosts = NULL;
+# endif
+uschar *tls_try_verify_hosts = NULL;
+uschar *tls_verify_certificates= US"system";
+uschar *tls_verify_hosts = NULL;
+int tls_watch_fd = -1;
+time_t tls_watch_trigger_time = (time_t)0;
+#else /*DISABLE_TLS*/
+uschar *tls_advertise_hosts = NULL;
+#endif
+
+#ifndef DISABLE_PRDR
+/* Per Recipient Data Response variables */
+BOOL prdr_enable = FALSE;
+BOOL prdr_requested = FALSE;
+const pcre2_code *regex_PRDR = NULL;
+#endif
+
+#ifdef SUPPORT_I18N
+const pcre2_code *regex_UTF8 = 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. */
+
+#if !defined(STAND_ALONE) && !defined(MACRO_PREDEF)
+int (*lwr_receive_getc)(unsigned) = stdin_getc;
+uschar * (*lwr_receive_getbuf)(unsigned *) = NULL;
+int (*lwr_receive_ungetc)(int) = stdin_ungetc;
+BOOL (*lwr_receive_hasc)(void) = stdin_hasc;
+
+int (*receive_getc)(unsigned) = stdin_getc;
+uschar * (*receive_getbuf)(unsigned *) = NULL;
+void (*receive_get_cache)(unsigned) = NULL;
+BOOL (*receive_hasc)(void) = stdin_hasc;
+int (*receive_ungetc)(int) = stdin_ungetc;
+int (*receive_feof)(void) = stdin_feof;
+int (*receive_ferror)(void) = stdin_ferror;
+#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. */
+
+const uschar **address_expansions[ADDRESS_EXPANSIONS_COUNT] = {
+ CUSS &deliver_address_data,
+ CUSS &deliver_domain,
+ CUSS &deliver_domain_data,
+ CUSS &deliver_domain_orig,
+ CUSS &deliver_domain_parent,
+ CUSS &deliver_localpart,
+ CUSS &deliver_localpart_data,
+ CUSS &deliver_localpart_orig,
+ CUSS &deliver_localpart_parent,
+ CUSS &deliver_localpart_prefix,
+ CUSS &deliver_localpart_suffix,
+ CUSS (uschar **)(&deliver_recipients),
+ CUSS &deliver_host,
+ CUSS &deliver_home,
+ CUSS &address_file,
+ CUSS &address_pipe,
+ CUSS &self_hostname,
+ NULL };
+
+int address_expansions_count = sizeof(address_expansions)/sizeof(uschar **);
+
+/******************************************************************************/
+/* General global variables. Boolean flags are done as a group
+so that only one bit each is needed, packed, for all those we never
+need to take a pointer - and only a char for the rest.
+This means a struct, unfortunately since it clutters the sourcecode. */
+
+struct global_flags f =
+{
+ .acl_temp_details = FALSE,
+ .active_local_from_check = FALSE,
+ .active_local_sender_retain = FALSE,
+ .address_test_mode = FALSE,
+ .admin_user = FALSE,
+ .allow_auth_unadvertised= FALSE,
+ .allow_unqualified_recipient = TRUE, /* For local messages */
+ .allow_unqualified_sender = TRUE, /* Reset for SMTP */
+ .authentication_local = FALSE,
+
+ .background_daemon = TRUE,
+ .bdat_readers_wanted = FALSE,
+
+ .chunking_offered = FALSE,
+ .config_changed = FALSE,
+ .continue_more = FALSE,
+
+ .daemon_listen = FALSE,
+ .debug_daemon = FALSE,
+ .deliver_firsttime = FALSE,
+ .deliver_force = FALSE,
+ .deliver_freeze = FALSE,
+ .deliver_force_thaw = FALSE,
+ .deliver_manual_thaw = FALSE,
+ .deliver_selectstring_regex = FALSE,
+ .deliver_selectstring_sender_regex = FALSE,
+ .disable_callout_flush = FALSE,
+ .disable_delay_flush = FALSE,
+ .disable_logging = FALSE,
+#ifndef DISABLE_DKIM
+ .dkim_disable_verify = FALSE,
+ .dkim_init_done = FALSE,
+#endif
+#ifdef SUPPORT_DMARC
+ .dmarc_has_been_checked = FALSE,
+ .dmarc_disable_verify = FALSE,
+ .dmarc_enable_forensic = FALSE,
+#endif
+ .dont_deliver = FALSE,
+ .dot_ends = TRUE,
+
+ .enable_dollar_recipients = FALSE,
+ .expand_string_forcedfail = FALSE,
+
+ .filter_running = FALSE,
+
+ .header_rewritten = FALSE,
+ .helo_verified = FALSE,
+ .helo_verify_failed = FALSE,
+ .host_checking_callout = FALSE,
+ .host_find_failed_syntax= FALSE,
+
+ .inetd_wait_mode = FALSE,
+ .is_inetd = FALSE,
+
+ .local_error_message = FALSE,
+ .log_testing_mode = FALSE,
+
+#ifdef WITH_CONTENT_SCAN
+ .no_mbox_unspool = FALSE,
+#endif
+ .no_multiline_responses = FALSE,
+
+ .parse_allow_group = FALSE,
+ .parse_found_group = FALSE,
+ .pipelining_enable = TRUE,
+#if defined(SUPPORT_PROXY) || defined(SUPPORT_SOCKS)
+ .proxy_session_failed = FALSE,
+#endif
+
+ .queue_2stage = FALSE,
+ .queue_only_policy = FALSE,
+ .queue_run_first_delivery = FALSE,
+ .queue_run_force = FALSE,
+ .queue_run_local = FALSE,
+ .queue_running = FALSE,
+ .queue_smtp = FALSE,
+
+ .really_exim = TRUE,
+ .receive_call_bombout = FALSE,
+ .recipients_discarded = FALSE,
+ .running_in_test_harness = FALSE,
+
+ .search_find_defer = FALSE,
+ .sender_address_forced = FALSE,
+ .sender_host_notsocket = FALSE,
+ .sender_host_unknown = FALSE,
+ .sender_local = FALSE,
+ .sender_name_forced = FALSE,
+ .sender_set_untrusted = FALSE,
+ .smtp_authenticated = FALSE,
+#ifndef DISABLE_PIPE_CONNECT
+ .smtp_in_early_pipe_advertised = FALSE,
+ .smtp_in_early_pipe_no_auth = FALSE,
+ .smtp_in_early_pipe_used = FALSE,
+#endif
+ .smtp_in_pipelining_advertised = FALSE,
+ .smtp_in_pipelining_used = FALSE,
+ .smtp_in_quit = FALSE,
+ .spool_file_wireformat = FALSE,
+ .submission_mode = FALSE,
+ .suppress_local_fixups = FALSE,
+ .suppress_local_fixups_default = FALSE,
+ .synchronous_delivery = FALSE,
+ .system_filtering = FALSE,
+
+ .taint_check_slow = FALSE,
+ .testsuite_delays = TRUE,
+ .tcp_fastopen_ok = FALSE,
+ .tcp_in_fastopen = FALSE,
+ .tcp_in_fastopen_data = FALSE,
+ .tcp_in_fastopen_logged = FALSE,
+ .tcp_out_fastopen_logged= FALSE,
+ .timestamps_utc = FALSE,
+ .transport_filter_timed_out = FALSE,
+ .trusted_caller = FALSE,
+ .trusted_config = TRUE,
+};
+
+/******************************************************************************/
+/* These are the flags which are either variables or mainsection options,
+so an address is needed for access, or are exported to local_scan. */
+
+BOOL accept_8bitmime = TRUE; /* deliberately not RFC compliant */
+BOOL allow_domain_literals = FALSE;
+BOOL allow_mx_to_ip = FALSE;
+BOOL allow_utf8_domains = FALSE;
+BOOL authentication_failed = FALSE;
+
+BOOL bounce_return_body = TRUE;
+BOOL bounce_return_message = TRUE;
+BOOL check_rfc2047_length = TRUE;
+BOOL commandline_checks_require_admin = FALSE;
+
+#ifdef EXPERIMENTAL_DCC
+BOOL dcc_direct_add_header = FALSE;
+#endif
+BOOL debug_store = FALSE;
+BOOL delivery_date_remove = TRUE;
+BOOL deliver_drop_privilege = FALSE;
+#ifdef ENABLE_DISABLE_FSYNC
+BOOL disable_fsync = FALSE;
+#endif
+BOOL disable_ipv6 = FALSE;
+BOOL dns_csa_use_reverse = TRUE;
+BOOL drop_cr = FALSE; /* No longer used */
+
+BOOL envelope_to_remove = TRUE;
+BOOL exim_gid_set = TRUE; /* This gid is always set */
+BOOL exim_uid_set = TRUE; /* This uid is always set */
+BOOL extract_addresses_remove_arguments = TRUE;
+
+BOOL host_checking = FALSE;
+BOOL host_lookup_deferred = FALSE;
+BOOL host_lookup_failed = FALSE;
+BOOL ignore_fromline_local = FALSE;
+
+BOOL local_from_check = TRUE;
+BOOL local_sender_retain = FALSE;
+BOOL log_timezone = FALSE;
+BOOL message_body_newlines = FALSE;
+BOOL message_logs = TRUE;
+#ifdef SUPPORT_I18N
+BOOL message_smtputf8 = FALSE;
+#endif
+BOOL mua_wrapper = FALSE;
+
+BOOL preserve_message_logs = FALSE;
+BOOL print_topbitchars = FALSE;
+BOOL prod_requires_admin = TRUE;
+#if defined(SUPPORT_PROXY) || defined(SUPPORT_SOCKS)
+BOOL proxy_session = FALSE;
+#endif
+
+#ifndef DISABLE_QUEUE_RAMP
+BOOL queue_fast_ramp = FALSE;
+#endif
+BOOL queue_list_requires_admin = TRUE;
+BOOL queue_only = FALSE;
+BOOL queue_only_load_latch = TRUE;
+BOOL queue_only_override = TRUE;
+BOOL queue_run_in_order = FALSE;
+BOOL recipients_max_reject = FALSE;
+BOOL return_path_remove = TRUE;
+
+BOOL smtp_batched_input = FALSE;
+BOOL sender_helo_dnssec = FALSE;
+BOOL sender_host_dnssec = FALSE;
+BOOL smtp_accept_keepalive = TRUE;
+BOOL smtp_check_spool_space = TRUE;
+BOOL smtp_enforce_sync = TRUE;
+BOOL smtp_etrn_serialize = TRUE;
+BOOL smtp_input = FALSE;
+BOOL smtp_return_error_details = FALSE;
+#ifdef SUPPORT_SPF
+BOOL spf_result_guessed = FALSE;
+#endif
+BOOL split_spool_directory = FALSE;
+BOOL spool_wireformat = FALSE;
+BOOL strict_acl_vars = FALSE;
+BOOL strip_excess_angle_brackets = FALSE;
+BOOL strip_trailing_dot = FALSE;
+BOOL syslog_duplication = TRUE;
+BOOL syslog_pid = TRUE;
+BOOL syslog_timestamp = TRUE;
+BOOL system_filter_gid_set = FALSE;
+BOOL system_filter_uid_set = FALSE;
+
+BOOL tcp_nodelay = TRUE;
+BOOL write_rejectlog = TRUE;
+
+/******************************************************************************/
+
+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;
+
+int acl_level = 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 = US"accept";
+#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;
+
+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 */
+ };
+
+uschar *add_environment = NULL;
+address_item *addr_duplicate = NULL;
+
+address_item address_defaults = {
+ .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,
+ .prefix_v = NULL,
+ .suffix = NULL,
+ .suffix_v = 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 = NULL,
+#ifndef DISABLE_TLS
+ .cipher = NULL,
+ .ourcert = NULL,
+ .peercert = NULL,
+ .peerdn = NULL,
+ .ocsp = OCSP_NOT_REQ,
+#endif
+#ifdef EXPERIMENTAL_DSN_INFO
+ .smtp_greeting = NULL,
+ .helo_response = NULL,
+#endif
+ .authenticator = NULL,
+ .auth_id = NULL,
+ .auth_sndr = NULL,
+ .dsn_orcpt = NULL,
+ .dsn_flags = 0,
+ .dsn_aware = 0,
+ .uid = (uid_t)(-1),
+ .gid = (gid_t)(-1),
+ .flags = { 0 },
+ .domain_cache = { 0 }, /* domain_cache - any larger array should be zeroed */
+ .localpart_cache = { 0 }, /* localpart_cache - ditto */
+ .mode = -1,
+ .more_errno = 0,
+ .delivery_time = {.tv_sec = 0, .tv_usec = 0},
+ .basic_errno = ERRNO_UNKNOWNERROR,
+ .child_count = 0,
+ .return_file = -1,
+ .special_action = SPECIAL_NONE,
+ .transport_return = DEFER,
+ .prop = { /* fields that are propagated to children */
+ .address_data = NULL,
+ .domain_data = NULL,
+ .localpart_data = NULL,
+ .errors_address = NULL,
+ .extra_headers = NULL,
+ .remove_headers = NULL,
+ .variables = NULL,
+ .ignore_error = FALSE,
+#ifdef SUPPORT_I18N
+ .utf8_msg = FALSE,
+ .utf8_downcvt = FALSE,
+ .utf8_downcvt_maybe = FALSE
+#endif
+ }
+};
+
+uschar *address_file = NULL;
+uschar *address_pipe = NULL;
+tree_node *addresslist_anchor = NULL;
+int addresslist_count = 0;
+gid_t *admin_groups = NULL;
+
+#ifdef EXPERIMENTAL_ARC
+struct arc_set *arc_received = NULL;
+int arc_received_instance = 0;
+int arc_oldest_pass = 0;
+const uschar *arc_state = NULL;
+const uschar *arc_state_reason = NULL;
+#endif
+
+uschar *authenticated_fail_id = NULL;
+uschar *authenticated_id = NULL;
+uschar *authenticated_sender = NULL;
+auth_instance *auths = NULL;
+uschar *auth_advertise_hosts = US"*";
+auth_instance auth_defaults = {
+ .next = NULL,
+ .name = NULL,
+ .info = NULL,
+ .options_block = NULL,
+ .driver_name = NULL,
+ .advertise_condition = NULL,
+ .client_condition = NULL,
+ .public_name = NULL,
+ .set_id = NULL,
+ .set_client_id = NULL,
+ .mail_auth_condition = NULL,
+ .server_debug_string = NULL,
+ .server_condition = NULL,
+ .client = FALSE,
+ .server = FALSE,
+ .advertised = FALSE
+};
+
+uschar *auth_defer_msg = US"reason not recorded";
+uschar *auth_defer_user_msg = US"";
+const uschar *auth_vars[AUTH_VARS];
+uschar *authenticator_name = NULL;
+int auto_thaw = 0;
+#ifdef WITH_CONTENT_SCAN
+int av_failed = FALSE; /* boolean but accessed as vtype_int*/
+uschar *av_scanner = US"sophie:/var/run/sophie"; /* AV scanner */
+#endif
+
+#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 bsmtp_transaction_linecount = 0;
+int body_8bitmime = 0;
+int body_linecount = 0;
+int body_zerocount = 0;
+uschar *bounce_message_file = NULL;
+uschar *bounce_message_text = NULL;
+uschar *bounce_recipient = NULL;
+int bounce_return_linesize_limit = 998;
+int bounce_return_size_limit = 100*1024;
+uschar *bounce_sender_authentication = NULL;
+
+uschar *callout_address = NULL;
+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 = 100;
+int_eximarith_t check_log_space = 10*1024; /* 10K Kbyte == 10MB */
+int check_spool_inodes = 100;
+int_eximarith_t check_spool_space = 10*1024; /* 10K Kbyte == 10MB */
+
+uschar *chunking_advertise_hosts = US"*";
+unsigned chunking_datasize = 0;
+unsigned chunking_data_left = 0;
+chunking_state_t chunking_state= CHUNKING_NOT_OFFERED;
+const pcre2_code *regex_CHUNKING = NULL;
+
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+const pcre2_code *regex_LIMITS = NULL;
+#endif
+
+uschar *client_authenticator = NULL;
+uschar *client_authenticated_id = NULL;
+uschar *client_authenticated_sender = NULL;
+#ifndef DISABLE_CLIENT_CMD_LOG
+gstring *client_cmd_log = NULL;
+#endif
+int clmacro_count = 0;
+uschar *clmacros[MAX_CLMACROS];
+FILE *config_file = NULL;
+const uschar *config_filename = NULL;
+int config_lineno = 0;
+#ifdef CONFIGURE_GROUP
+gid_t config_gid = CONFIGURE_GROUP;
+#else
+gid_t config_gid = 0;
+#endif
+uschar *config_main_filelist = US CONFIGURE_FILE
+ "\0<-----------Space to patch configure_filename->";
+uschar *config_main_filename = NULL;
+uschar *config_main_directory = NULL;
+
+#ifdef CONFIGURE_OWNER
+uid_t config_uid = CONFIGURE_OWNER;
+#else
+uid_t config_uid = 0;
+#endif
+
+int connection_max_messages= -1;
+uschar *continue_proxy_cipher = NULL;
+BOOL continue_proxy_dane = FALSE;
+uschar *continue_proxy_sni = NULL;
+uschar *continue_hostname = NULL;
+uschar *continue_host_address = NULL;
+int continue_sequence = 1;
+uschar *continue_transport = NULL;
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+unsigned continue_limit_mail = 0;
+unsigned continue_limit_rcpt = 0;
+unsigned continue_limit_rcptdom= 0;
+#endif
+
+uschar *csa_status = NULL;
+cut_t cutthrough = {
+ .callout_hold_only = FALSE, /* verify-only: normal delivery */
+ .delivery = FALSE, /* when to attempt */
+ .defer_pass = FALSE, /* on defer: spool locally */
+ .is_tls = FALSE, /* not a TLS conn yet */
+ .cctx = {.sock = -1}, /* open connection */
+ .nrcpt = 0, /* number of addresses */
+};
+
+int daemon_notifier_fd = -1;
+uschar *daemon_smtp_port = US"smtp";
+int daemon_startup_retries = 9;
+int daemon_startup_sleep = 30;
+
+#ifdef EXPERIMENTAL_DCC
+uschar *dcc_header = NULL;
+uschar *dcc_result = NULL;
+uschar *dccifd_address = US"/usr/local/dcc/var/dccifd";
+uschar *dccifd_options = US"header";
+#endif
+
+int debug_fd = -1;
+FILE *debug_file = NULL;
+int debug_notall[] = {
+ Di_memory,
+ Di_noutf8,
+ -1
+};
+bit_table debug_options[] = { /* must be in alphabetical order and use
+ only the enum values from macro.h */
+ BIT_TABLE(D, acl),
+ BIT_TABLE(D, all),
+ BIT_TABLE(D, auth),
+ BIT_TABLE(D, deliver),
+ BIT_TABLE(D, dns),
+ BIT_TABLE(D, dnsbl),
+ BIT_TABLE(D, exec),
+ BIT_TABLE(D, expand),
+ BIT_TABLE(D, filter),
+ BIT_TABLE(D, hints_lookup),
+ BIT_TABLE(D, host_lookup),
+ BIT_TABLE(D, ident),
+ BIT_TABLE(D, interface),
+ BIT_TABLE(D, lists),
+ BIT_TABLE(D, load),
+ BIT_TABLE(D, local_scan),
+ BIT_TABLE(D, lookup),
+ BIT_TABLE(D, memory),
+ BIT_TABLE(D, noutf8),
+ BIT_TABLE(D, pid),
+ BIT_TABLE(D, process_info),
+ BIT_TABLE(D, queue_run),
+ BIT_TABLE(D, receive),
+ BIT_TABLE(D, resolver),
+ BIT_TABLE(D, retry),
+ BIT_TABLE(D, rewrite),
+ BIT_TABLE(D, route),
+ BIT_TABLE(D, timestamp),
+ BIT_TABLE(D, tls),
+ BIT_TABLE(D, transport),
+ BIT_TABLE(D, uid),
+ BIT_TABLE(D, verify),
+};
+int debug_options_count = nelem(debug_options);
+uschar debuglog_name[LOG_NAME_SIZE] = {0};
+unsigned debug_pretrigger_bsize = 0;
+uschar * debug_pretrigger_buf = NULL;
+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}}";
+uschar *deliver_address_data = NULL;
+int deliver_datafile = -1;
+const uschar *deliver_domain = NULL;
+uschar *deliver_domain_data = NULL;
+const uschar *deliver_domain_orig = NULL;
+const uschar *deliver_domain_parent = NULL;
+time_t deliver_frozen_at = 0;
+uschar *deliver_home = NULL;
+const uschar *deliver_host = NULL;
+const uschar *deliver_host_address = NULL;
+int deliver_host_port = 0;
+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_prefix_v = NULL;
+uschar *deliver_localpart_suffix = NULL;
+uschar *deliver_localpart_suffix_v = NULL;
+uschar *deliver_out_buffer = NULL;
+int deliver_queue_load_max = -1;
+address_item *deliver_recipients = NULL;
+uschar *deliver_selectstring = NULL;
+uschar *deliver_selectstring_sender = NULL;
+
+#ifndef DISABLE_DKIM
+unsigned dkim_collect_input = 0;
+uschar *dkim_cur_signer = NULL;
+int dkim_key_length = 0;
+void *dkim_signatures = NULL;
+uschar *dkim_signers = NULL;
+uschar *dkim_signing_domain = NULL;
+uschar *dkim_signing_selector = NULL;
+uschar *dkim_verify_hashes = US"sha256:sha512";
+uschar *dkim_verify_keytypes = US"ed25519:rsa";
+uschar *dkim_verify_min_keysizes = US"rsa=1024 ed25519=250";
+BOOL dkim_verify_minimal = FALSE;
+uschar *dkim_verify_overall = NULL;
+uschar *dkim_verify_signers = US"$dkim_signers";
+uschar *dkim_verify_status = NULL;
+uschar *dkim_verify_reason = NULL;
+#endif
+#ifdef SUPPORT_DMARC
+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;
+#endif
+
+uschar *dns_again_means_nonexist = NULL;
+int dns_csa_search_limit = 5;
+int dns_cname_loops = 1;
+#ifdef SUPPORT_DANE
+int dns_dane_ok = -1;
+#endif
+uschar *dns_ipv4_lookup = NULL;
+int dns_retrans = 0;
+int dns_retry = 0;
+int dns_dnssec_ok = -1; /* <0 = not coerced */
+uschar *dns_trust_aa = NULL;
+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;
+const uschar *driver_srcfile = NULL;
+int driver_srcline = 0;
+uschar *dsn_from = US DEFAULT_DSN_FROM;
+unsigned int dtrigger_selector = 0;
+
+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;
+#ifndef DISABLE_EVENT
+uschar *event_action = NULL; /* expansion for delivery events */
+uschar *event_data = NULL; /* auxiliary data variable for event */
+int event_defer_errno = 0;
+const uschar *event_name = NULL; /* event name variable */
+#endif
+
+
+gid_t exim_gid = EXIM_GID;
+uschar *exim_path = US BIN_DIRECTORY "/exim"
+ "\0<---------------Space to patch exim_path->";
+uid_t exim_uid = EXIM_UID;
+int expand_level = 0; /* Nesting depth, indent for debug */
+int expand_forbid = 0;
+int expand_nlength[EXPAND_MAXN+1];
+int expand_nmax = -1;
+const uschar *expand_nstring[EXPAND_MAXN+1];
+uschar *expand_string_message;
+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];
+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;
+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;
+
+volatile sig_atomic_t had_command_timeout = 0;
+volatile sig_atomic_t had_command_sigterm = 0;
+volatile sig_atomic_t had_data_timeout = 0;
+volatile sig_atomic_t had_data_sigint = 0;
+const 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[] = {
+ /* name len allow_resent htype */
+ { 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 = nelem(header_names);
+
+uschar *helo_accept_junk_hosts = NULL;
+uschar *helo_allow_chars = US"";
+uschar *helo_lookup_domains = US"@ : @[]";
+uschar *helo_try_verify_hosts = NULL;
+uschar *helo_verify_hosts = NULL;
+const uschar *hex_digits = CUS"0123456789abcdef";
+uschar *hold_domains = NULL;
+uschar *host_data = NULL;
+uschar *host_lookup = NULL;
+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_require_helo = US"*";
+uschar *hosts_connection_nolog = NULL;
+
+int ignore_bounce_errors_after = 10*7*24*60*60; /* 10 weeks */
+uschar *ignore_fromline_hosts = NULL;
+int inetd_wait_timeout = -1;
+uschar *initial_cwd = NULL;
+uschar *interface_address = NULL;
+int interface_port = -1;
+uschar *iterate_item = NULL;
+
+int journal_fd = -1;
+
+uschar *keep_environment = NULL;
+
+int keep_malformed = 4*24*60*60; /* 4 days */
+
+uschar *eldap_dn = NULL;
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+uschar *limits_advertise_hosts = US"*";
+#endif
+int load_average = -2;
+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
+
+#ifdef HAVE_LOCAL_SCAN
+uschar *local_scan_data = NULL;
+int local_scan_timeout = 5*60;
+#endif
+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;
+
+int log_default[] = { /* for initializing log_selector */
+ Li_acl_warn_skipped,
+ Li_connection_reject,
+ Li_delay_delivery,
+ Li_dkim,
+ Li_dnslist_defer,
+ Li_etrn,
+ Li_host_lookup_failed,
+ Li_lost_incoming_connection,
+ Li_outgoing_interface, /* see d_log_interface in deliver.c */
+ Li_msg_id,
+ Li_queue_run,
+ Li_queue_time_exclusive,
+ Li_rejected_header,
+ Li_retry_defer,
+ Li_sender_verify_fail,
+ Li_size_reject,
+ Li_skip_delivery,
+ Li_smtp_confirmation,
+ Li_tls_certificate_verified,
+ Li_tls_cipher,
+ -1
+};
+
+uschar *log_file_path = US LOG_FILE_PATH
+ "\0<--------------Space to patch log_file_path->";
+
+int log_notall[] = {
+ -1
+};
+bit_table log_options[] = { /* must be in alphabetical order,
+ with definitions from enum logbit. */
+ BIT_TABLE(L, 8bitmime),
+ BIT_TABLE(L, acl_warn_skipped),
+ BIT_TABLE(L, address_rewrite),
+ BIT_TABLE(L, all),
+ BIT_TABLE(L, all_parents),
+ BIT_TABLE(L, arguments),
+ BIT_TABLE(L, connection_reject),
+ BIT_TABLE(L, delay_delivery),
+ BIT_TABLE(L, deliver_time),
+ BIT_TABLE(L, delivery_size),
+#ifndef DISABLE_DKIM
+ BIT_TABLE(L, dkim),
+ BIT_TABLE(L, dkim_verbose),
+#endif
+ BIT_TABLE(L, dnslist_defer),
+ BIT_TABLE(L, dnssec),
+ BIT_TABLE(L, etrn),
+ BIT_TABLE(L, host_lookup_failed),
+ BIT_TABLE(L, ident_timeout),
+ BIT_TABLE(L, incoming_interface),
+ BIT_TABLE(L, incoming_port),
+ BIT_TABLE(L, lost_incoming_connection),
+ BIT_TABLE(L, millisec),
+ BIT_TABLE(L, msg_id),
+ BIT_TABLE(L, msg_id_created),
+ BIT_TABLE(L, outgoing_interface),
+ BIT_TABLE(L, outgoing_port),
+ BIT_TABLE(L, pid),
+ BIT_TABLE(L, pipelining),
+ BIT_TABLE(L, protocol_detail),
+#if defined(SUPPORT_PROXY) || defined(SUPPORT_SOCKS)
+ BIT_TABLE(L, proxy),
+#endif
+ BIT_TABLE(L, queue_run),
+ BIT_TABLE(L, queue_time),
+ BIT_TABLE(L, queue_time_exclusive),
+ BIT_TABLE(L, queue_time_overall),
+ BIT_TABLE(L, receive_time),
+ BIT_TABLE(L, received_recipients),
+ BIT_TABLE(L, received_sender),
+ BIT_TABLE(L, rejected_header),
+ { US"rejected_headers", Li_rejected_header },
+ BIT_TABLE(L, retry_defer),
+ BIT_TABLE(L, return_path_on_delivery),
+ BIT_TABLE(L, sender_on_delivery),
+ BIT_TABLE(L, sender_verify_fail),
+ BIT_TABLE(L, size_reject),
+ BIT_TABLE(L, skip_delivery),
+ BIT_TABLE(L, smtp_confirmation),
+ BIT_TABLE(L, smtp_connection),
+ BIT_TABLE(L, smtp_incomplete_transaction),
+ BIT_TABLE(L, smtp_mailauth),
+ BIT_TABLE(L, smtp_no_mail),
+ BIT_TABLE(L, smtp_protocol_error),
+ BIT_TABLE(L, smtp_syntax_error),
+ BIT_TABLE(L, subject),
+ BIT_TABLE(L, tls_certificate_verified),
+ BIT_TABLE(L, tls_cipher),
+ BIT_TABLE(L, tls_peerdn),
+ BIT_TABLE(L, tls_resumption),
+ BIT_TABLE(L, tls_sni),
+ BIT_TABLE(L, unknown_in_list),
+};
+int log_options_count = nelem(log_options);
+
+int log_reject_target = 0;
+unsigned int log_selector[log_selector_size]; /* initialized in main() */
+uschar *log_selector_string = NULL;
+FILE *log_stderr = NULL;
+uschar *login_sender_address = NULL;
+uschar *lookup_dnssec_authenticated = NULL;
+int lookup_open_max = 25;
+uschar *lookup_value = NULL;
+
+macro_item *macros_user = 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;
+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;
+uschar message_id_option[MESSAGE_ID_LENGTH + 3];
+uschar *message_id_external;
+int message_linecount = 0;
+int message_size = 0;
+uschar *message_size_limit = US"50M";
+#ifdef SUPPORT_I18N
+int message_utf8_downconvert = 0; /* -1 ifneeded; 0 never; 1 always */
+#endif
+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
+
+uid_t *never_users = NULL;
+uschar *notifier_socket = US"$spool_directory/" NOTIFIER_SOCKET_NAME ;
+
+const int on = 1; /* for setsockopt */
+const int off = 0;
+
+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;
+
+pcre2_general_context * pcre_gen_ctx = NULL;
+pcre2_compile_context * pcre_cmp_ctx = NULL;
+pcre2_match_context * pcre_mtc_ctx = NULL;
+
+uschar *percent_hack_domains = NULL;
+uschar *pid_file_path = US PID_FILE_PATH
+ "\0<--------------Space to patch pid_file_path->";
+#ifndef DISABLE_PIPE_CONNECT
+uschar *pipe_connect_advertise_hosts = US"*";
+#endif
+uschar *pipelining_advertise_hosts = US"*";
+uschar *primary_hostname = NULL;
+uschar *process_info;
+int process_info_len = 0;
+uschar *process_log_path = NULL;
+const uschar *process_purpose = US"fresh-exec";
+
+#if defined(SUPPORT_PROXY) || defined(SUPPORT_SOCKS)
+uschar *hosts_proxy = NULL;
+uschar *proxy_external_address = NULL;
+int proxy_external_port = 0;
+uschar *proxy_local_address = NULL;
+int proxy_local_port = 0;
+int proxy_protocol_timeout = 3;
+#endif
+
+uschar *prvscheck_address = NULL;
+uschar *prvscheck_keynum = NULL;
+uschar *prvscheck_result = NULL;
+
+
+const uschar *qualify_domain_recipient = NULL;
+uschar *qualify_domain_sender = NULL;
+uschar *queue_domains = NULL;
+int queue_interval = -1;
+uschar *queue_name = US"";
+uschar *queue_name_dest = NULL;
+uschar *queue_only_file = NULL;
+int queue_only_load = -1;
+uschar *queue_run_max = US"5";
+pid_t queue_run_pid = (pid_t)0;
+int queue_run_pipe = -1;
+unsigned queue_size = 0;
+time_t queue_size_next = 0;
+uschar *queue_smtp_domains = NULL;
+
+uint32_t 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;
+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 }}"
+#ifndef DISABLE_TLS
+ "${if def:tls_in_ver { ($tls_in_ver)}}"
+ "${if def:tls_in_cipher_std { tls $tls_in_cipher_std\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;
+struct timeval received_time = { 0, 0 };
+struct timeval received_time_complete = { 0, 0 };
+uschar *recipient_data = NULL;
+uschar *recipient_unqualified_hosts = NULL;
+uschar *recipient_verify_failure = NULL;
+int recipients_count = 0;
+recipient_item *recipients_list = NULL;
+int recipients_list_max = 0;
+int recipients_max = 50000;
+const pcre2_code *regex_AUTH = NULL;
+const pcre2_code *regex_check_dns_names = NULL;
+const pcre2_code *regex_From = NULL;
+const pcre2_code *regex_IGNOREQUOTA = NULL;
+const pcre2_code *regex_PIPELINING = NULL;
+const pcre2_code *regex_SIZE = NULL;
+#ifndef DISABLE_PIPE_CONNECT
+const pcre2_code *regex_EARLY_PIPE = NULL;
+#endif
+const pcre2_code *regex_ismsgid = NULL;
+const pcre2_code *regex_smtp_code = NULL;
+const uschar *regex_vars[REGEX_VARS];
+#ifdef WHITELIST_D_MACROS
+const pcre2_code *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;
+int rewrite_existflags = 0;
+uschar *rfc1413_hosts = US"@[]";
+int rfc1413_query_timeout = 0;
+uid_t root_gid = ROOT_GID;
+uid_t root_uid = ROOT_UID;
+
+router_instance *routers = NULL;
+router_instance router_defaults = {
+ .next = NULL,
+ .name = NULL,
+ .info = NULL,
+ .options_block = NULL,
+ .driver_name = NULL,
+
+ .address_data = NULL,
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+ .bmi_rule = NULL,
+#endif
+ .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 = NULL,
+ .self = US"freeze",
+ .senders = NULL,
+ .suffix = NULL,
+ .translate_ip_address = NULL,
+ .transport_name = NULL,
+
+ .address_test = TRUE,
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+ .bmi_deliver_alternate = FALSE,
+ .bmi_deliver_default = FALSE,
+ .bmi_dont_deliver = FALSE,
+#endif
+ .expn = TRUE,
+ .caseful_local_part = FALSE,
+ .check_local_user = FALSE,
+ .disable_logging = FALSE,
+ .fail_verify_recipient = FALSE,
+ .fail_verify_sender = FALSE,
+ .gid_set = FALSE,
+ .initgroups = FALSE,
+ .log_as_local = TRUE_UNSET,
+ .more = TRUE,
+ .pass_on_timeout = FALSE,
+ .prefix_optional = FALSE,
+ .repeat_use = TRUE,
+ .retry_use_local_part = TRUE_UNSET,
+ .same_domain_copy_routing = FALSE,
+ .self_rewrite = FALSE,
+ .set = NULL,
+ .suffix_optional = FALSE,
+ .verify_only = FALSE,
+ .verify_recipient = TRUE,
+ .verify_sender = TRUE,
+ .uid_set = FALSE,
+ .unseen = FALSE,
+ .dsn_lasthop = FALSE,
+
+ .self_code = self_freeze,
+ .uid = (uid_t)(-1),
+ .gid = (gid_t)(-1),
+
+ .fallback_hostlist = NULL,
+ .transport = NULL,
+ .pass_router = NULL,
+ .redirect_router = NULL,
+
+ .dnssec = { .request= US"*", .require=NULL },
+};
+
+uschar *router_name = NULL;
+tree_node *router_var = NULL;
+
+ip_address_item *running_interfaces = NULL;
+
+/* 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;
+uschar *self_hostname = NULL;
+uschar *sender_address = NULL;
+unsigned int sender_address_cache[(MAX_NAMED_LIST * 2)/32];
+uschar *sender_address_data = NULL;
+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;
+uschar *sender_host_auth_pubname = NULL;
+unsigned int sender_host_cache[(MAX_NAMED_LIST * 2)/32];
+uschar *sender_host_name = NULL;
+int sender_host_port = 0;
+uschar *sender_ident = NULL;
+uschar *sender_rate = NULL;
+uschar *sender_rate_limit = NULL;
+uschar *sender_rate_period = NULL;
+uschar *sender_rcvhost = NULL;
+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;
+uschar *sending_ip_address = NULL;
+int sending_port = -1;
+SIGNAL_BOOL sigalrm_seen = FALSE;
+const uschar *sigalarm_setter = NULL;
+uschar **sighup_argv = NULL;
+int slow_lookup_log = 0; /* millisecs, zero disables */
+int smtp_accept_count = 0;
+int smtp_accept_max = 20;
+int smtp_accept_max_nonmail= 10;
+uschar *smtp_accept_max_nonmail_hosts = US"*";
+uschar *smtp_accept_max_per_connection = US"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;
+int smtp_backlog_monitor = 0;
+uschar *smtp_banner = US"$smtp_active_hostname ESMTP "
+ "Exim $version_number $tod_full"
+ "\0<---------------Space to patch smtp_banner->";
+int smtp_ch_index = 0;
+uschar *smtp_cmd_argument = NULL;
+uschar *smtp_cmd_buffer = NULL;
+struct timeval smtp_connection_start = {0,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;
+FILE *smtp_in = NULL;
+int smtp_listen_backlog = 0;
+int smtp_load_reserve = -1;
+int smtp_mailcmd_count = 0;
+int smtp_mailcmd_max = -1;
+FILE *smtp_out = NULL;
+uschar *smtp_etrn_command = NULL;
+int smtp_max_synprot_errors= 3;
+int smtp_max_unknown_commands = 3;
+uschar *smtp_notquit_reason = NULL;
+unsigned smtp_peer_options = 0;
+unsigned smtp_peer_options_wrap= 0;
+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_receive_timeout_s = NULL;
+uschar *smtp_reserve_hosts = NULL;
+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;
+#ifdef SUPPORT_I18N
+uschar *smtputf8_advertise_hosts = US"*"; /* overridden under test-harness */
+#endif
+
+#ifdef WITH_CONTENT_SCAN
+uschar *spamd_address = US"127.0.0.1 783";
+uschar *spam_bar = NULL;
+uschar *spam_report = NULL;
+uschar *spam_action = NULL;
+uschar *spam_score = NULL;
+uschar *spam_score_int = NULL;
+#endif
+#ifdef SUPPORT_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;
+uschar *spf_smtp_comment_template
+ /* Used to be: "Please%_see%_http://www.open-spf.org/Why?id=%{S}&ip=%{C}&receiver=%{R}" */
+ = US"Please%_see%_http://www.open-spf.org/Why";
+
+#endif
+
+FILE *spool_data_file = NULL;
+uschar *spool_directory = US SPOOL_DIRECTORY
+ "\0<--------------Space to patch spool_directory->";
+#ifdef SUPPORT_SRS
+uschar *srs_recipient = NULL;
+#endif
+int string_datestamp_offset= -1;
+int string_datestamp_length= 0;
+int string_datestamp_type = -1;
+const uschar *submission_domain = NULL;
+const uschar *submission_name = NULL;
+int syslog_facility = LOG_MAIL;
+uschar *syslog_processname = US"exim";
+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;
+uid_t system_filter_uid = (uid_t)-1;
+
+blob tcp_fastopen_nodata = { .data = NULL, .len = 0 };
+tfo_state_t tcp_out_fastopen = TFO_NOT_USED;
+#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;
+#ifdef MEASURE_TIMING
+struct timeval timestamp_startup;
+#endif
+
+transport_instance *transports = NULL;
+
+transport_instance transport_defaults = {
+ /* All non-mentioned elements zero/NULL/FALSE */
+ .batch_max = 1,
+ .multi_domain = TRUE,
+ .max_addresses = 100,
+ .connection_max_messages = 500,
+ .uid = (uid_t)(-1),
+ .gid = (gid_t)(-1),
+ .filter_timeout = 300,
+ .retry_use_local_part = TRUE_UNSET, /* retry_use_local_part: BOOL, but set neither
+ 1 nor 0 so can detect unset */
+};
+
+int transport_count;
+uschar *transport_name = NULL;
+int transport_newlines;
+const uschar **transport_filter_argv = NULL;
+int transport_filter_timeout;
+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;
+
+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 *verify_mode = NULL;
+uschar *version_copyright =
+ US"Copyright (c) University of Cambridge, 1995 - 2018\n"
+ "(c) The Exim Maintainers and contributors in ACKNOWLEDGMENTS file, 2007 - 2022";
+uschar *version_date = US"?";
+uschar *version_cnumber = US"????";
+uschar *version_string = US"?";
+
+uschar *warn_message_file = NULL;
+int warning_count = 0;
+uschar *warnmsg_delay = NULL;
+uschar *warnmsg_recipients = NULL;
+
+
+/* End of globals.c */
diff --git a/src/globals.h b/src/globals.h
new file mode 100644
index 0000000..fe099e4
--- /dev/null
+++ b/src/globals.h
@@ -0,0 +1,1120 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 */
+extern BOOL opt_perl_taintmode; /* Enable taint mode in Perl */
+#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 LOOKUP_REDIS
+extern uschar *redis_servers; /* List of servers and connect info */
+#endif
+
+#ifdef LOOKUP_SQLITE
+extern uschar *sqlite_dbfile; /* Filname for database */
+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 {
+ client_conn_ctx active; /* fd/socket when in a TLS session, and ptr to TLS context */
+ int bits; /* bits used in TLS session */
+ BOOL certificate_verified; /* Client certificate verified */
+#ifdef SUPPORT_DANE
+ BOOL dane_verified; /* ... via DANE */
+ int tlsa_usage; /* TLSA record(s) usage */
+#endif
+ uschar *cipher; /* Cipher used */
+ const uschar *cipher_stdname; /* Cipher used, RFC version */
+ const uschar *ver; /* TLS version */
+
+ 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 */
+ uschar *channelbinding; /* b64'd data identifying channel, for authenticators */
+ 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 */
+#ifndef DISABLE_TLS_RESUME
+ hctx resume_hctx; /* session lookup key accumulation */
+ const uschar * resume_index; /* session lookup key */
+
+ unsigned resumption; /* Session resumption */
+ BOOL host_resumable:1;
+ BOOL ticket_received:1;
+#endif
+ BOOL verify_override:1; /* certificate_verified only due to tls_try_verify_hosts */
+ BOOL ext_master_secret:1; /* extended-master-secret was used */
+} tls_support;
+extern tls_support tls_in;
+extern tls_support tls_out;
+
+#ifndef DISABLE_TLS
+extern BOOL gnutls_compat_mode; /* Less security, more compatibility */
+extern BOOL gnutls_allow_auto_pkcs11; /* Let GnuTLS autoload PKCS11 modules */
+extern uschar *hosts_require_alpn; /* Mandatory ALPN successful nogitiation */
+extern uschar *openssl_options; /* OpenSSL compatibility options */
+extern const pcre2_code *regex_STARTTLS; /* For recognizing STARTTLS settings */
+extern uschar *tls_alpn; /* ALPN names acceptable */
+extern uschar *tls_certificate; /* Certificate file */
+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 */
+extern uschar *tls_eccurve; /* EC curve */
+# ifndef DISABLE_OCSP
+extern uschar *tls_ocsp_file; /* OCSP stapling proof file */
+# endif
+extern uschar *tls_privatekey; /* Private key file */
+extern BOOL tls_remember_esmtp; /* For YAEB */
+extern uschar *tls_require_ciphers; /* So some can be avoided */
+# ifndef DISABLE_TLS_RESUME
+extern uschar *tls_resumption_hosts; /* TLS session resumption */
+# endif
+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 */
+extern int tls_watch_fd; /* for inotify of creds files */
+extern time_t tls_watch_trigger_time; /* non-0: triggered */
+#endif
+extern uschar *tls_advertise_hosts; /* host for which TLS is advertised */
+
+extern uschar *dsn_envid; /* DSN envid string */
+extern int dsn_ret; /* DSN ret type*/
+extern const pcre2_code *regex_DSN; /* For recognizing DSN settings */
+extern uschar *dsn_advertise_hosts; /* host for which TLS is advertised */
+
+/* Input-reading functions for messages, so we can use special ones for
+incoming TCP/IP. */
+
+extern int (*lwr_receive_getc)(unsigned);
+extern uschar * (*lwr_receive_getbuf)(unsigned *);
+extern BOOL (*lwr_receive_hasc)(void);
+extern int (*lwr_receive_ungetc)(int);
+
+extern int (*receive_getc)(unsigned);
+extern uschar * (*receive_getbuf)(unsigned *);
+extern BOOL (*receive_hasc)(void);
+extern void (*receive_get_cache)(unsigned);
+extern int (*receive_ungetc)(int);
+extern int (*receive_feof)(void);
+extern int (*receive_ferror)(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 const uschar **address_expansions[ADDRESS_EXPANSIONS_COUNT];
+
+/* Flags for which we don't need an address ever so can use a bitfield */
+
+extern struct global_flags {
+ BOOL acl_temp_details :1; /* TRUE to give details for 4xx error */
+ BOOL active_local_from_check :1; /* For adding Sender: (switchable) */
+ BOOL active_local_sender_retain :1; /* For keeping Sender: (switchable) */
+ BOOL address_test_mode :1; /* True for -bt */
+ BOOL admin_user :1; /* True if caller can do admin */
+ BOOL allow_auth_unadvertised :1; /* As it says */
+ BOOL allow_unqualified_recipient :1; /* For local messages */ /* As it says */
+ BOOL allow_unqualified_sender :1; /* Reset for SMTP */ /* Ditto */
+ BOOL authentication_local :1; /* TRUE if non-smtp (implicit authentication) */
+
+ BOOL background_daemon :1; /* Set FALSE to keep in foreground */
+ BOOL bdat_readers_wanted :1; /* BDAT-handling to be pushed on readfunc stack */
+
+ BOOL chunking_offered :1;
+ BOOL config_changed :1; /* True if -C used */
+ BOOL continue_more :1; /* Flag more addresses waiting */
+
+ BOOL daemon_listen :1; /* True if listening required */
+ BOOL debug_daemon :1; /* Debug the daemon process only */
+ BOOL deliver_firsttime :1; /* True for first delivery attempt */
+ BOOL deliver_force :1; /* TRUE if delivery was forced */
+ BOOL deliver_freeze :1; /* TRUE if delivery is frozen */
+ BOOL deliver_force_thaw :1; /* TRUE to force thaw in queue run */
+ BOOL deliver_manual_thaw :1; /* TRUE if manually thawed */
+ BOOL deliver_selectstring_regex :1; /* String is regex */
+ BOOL deliver_selectstring_sender_regex :1; /* String is regex */
+ BOOL disable_callout_flush :1; /* Don't flush before callouts */
+ BOOL disable_delay_flush :1; /* Don't flush before "delay" in ACL */
+ BOOL disable_logging :1; /* Disables log writing when TRUE */
+#ifndef DISABLE_DKIM
+ BOOL dkim_disable_verify :1; /* Set via ACL control statement. When set, DKIM verification is disabled for the current message */
+ BOOL dkim_init_done :1; /* lazy-init status */
+#endif
+#ifdef SUPPORT_DMARC
+ BOOL dmarc_has_been_checked :1; /* Global variable to check if test has been called yet */
+ BOOL dmarc_disable_verify :1; /* Set via ACL control statement. When set, DMARC verification is disabled for the current message */
+ BOOL dmarc_enable_forensic :1; /* Set via ACL control statement. When set, DMARC forensic reports are enabled for the current message */
+#endif
+ BOOL dont_deliver :1; /* TRUE for -N option */
+ BOOL dot_ends :1; /* TRUE if "." ends non-SMTP input */
+
+ BOOL enable_dollar_recipients :1; /* Make $recipients available */
+ BOOL expand_string_forcedfail :1; /* TRUE if failure was "expected" */
+
+ BOOL filter_running :1; /* TRUE while running a filter */
+
+ BOOL header_rewritten :1; /* TRUE if header changed by router */
+ BOOL helo_verified :1; /* True if HELO verified */
+ BOOL helo_verify_failed :1; /* True if attempt failed */
+ BOOL host_checking_callout :1; /* TRUE if real callout wanted */
+ BOOL host_find_failed_syntax :1; /* DNS syntax check failure */
+
+ BOOL inetd_wait_mode :1; /* Whether running in inetd wait mode */
+ BOOL is_inetd :1; /* True for inetd calls */
+
+ BOOL local_error_message :1; /* True if handling one of these */
+ BOOL log_testing_mode :1; /* TRUE in various testing modes */
+
+#ifdef WITH_CONTENT_SCAN
+ BOOL no_mbox_unspool :1; /* don't unlink files in /scan directory */
+#endif
+ BOOL no_multiline_responses :1; /* For broken clients */
+
+ BOOL parse_allow_group :1; /* Allow group syntax */
+ BOOL parse_found_group :1; /* In the middle of a group */
+ BOOL pipelining_enable :1; /* As it says */
+#if defined(SUPPORT_PROXY) || defined(SUPPORT_SOCKS)
+ BOOL proxy_session_failed :1; /* TRUE if required proxy negotiation failed */
+#endif
+
+ BOOL queue_2stage :1; /* Run queue in 2-stage manner */
+ BOOL queue_only_policy :1; /* ACL or local_scan wants queue_only */
+ BOOL queue_run_first_delivery :1; /* If TRUE, first deliveries only */
+ BOOL queue_run_force :1; /* TRUE to force during queue run */
+ BOOL queue_run_local :1; /* Local deliveries only in queue run */
+ BOOL queue_running :1; /* TRUE for queue running process and */
+ BOOL queue_smtp :1; /* Disable all immediate SMTP (-odqs)*/
+
+ BOOL really_exim :1; /* FALSE in utilities */
+ BOOL receive_call_bombout :1; /* Flag for crashing log */
+ BOOL recipients_discarded :1; /* By an ACL */
+ BOOL running_in_test_harness :1; /*TRUE when running_status is patched */
+
+ BOOL search_find_defer :1; /* Set TRUE if lookup deferred */
+ BOOL sender_address_forced :1; /* Set by -f */
+ BOOL sender_host_notsocket :1; /* Set for -bs and -bS */
+ BOOL sender_host_unknown :1; /* TRUE for -bs and -bS except inetd */
+ BOOL sender_local :1; /* TRUE for local senders */
+ BOOL sender_name_forced :1; /* Set by -F */
+ BOOL sender_set_untrusted :1; /* Sender set by untrusted caller */
+ BOOL smtp_authenticated :1; /* Sending client has authenticated */
+#ifndef DISABLE_PIPE_CONNECT
+ BOOL smtp_in_early_pipe_advertised :1; /* server advertised PIPECONNECT */
+ BOOL smtp_in_early_pipe_no_auth :1; /* too many authenticator names */
+ BOOL smtp_in_early_pipe_used :1; /* client did send early data */
+#endif
+ BOOL smtp_in_pipelining_advertised :1; /* server advertised PIPELINING */
+ BOOL smtp_in_pipelining_used :1; /* server noted client using PIPELINING */
+ BOOL smtp_in_quit :1; /* server noted QUIT command */
+ BOOL spool_file_wireformat :1; /* current -D file has CRLF rather than NL */
+ BOOL submission_mode :1; /* Can be forced from ACL */
+ BOOL suppress_local_fixups :1; /* Can be forced from ACL */
+ BOOL suppress_local_fixups_default :1; /* former is reset to this; override with -G */
+ BOOL synchronous_delivery :1; /* TRUE if -odi is set */
+ BOOL system_filtering :1; /* TRUE when running system filter */
+
+ BOOL taint_check_slow :1; /* malloc/mmap are not returning distinct ranges */
+ BOOL testsuite_delays :1; /* interprocess sequencing delays, under testsuite */
+ BOOL tcp_fastopen_ok :1; /* appears to be supported by kernel */
+ BOOL tcp_in_fastopen :1; /* conn usefully used fastopen */
+ BOOL tcp_in_fastopen_data :1; /* fastopen carried data */
+ BOOL tcp_in_fastopen_logged :1; /* one-time logging */
+ BOOL tcp_out_fastopen_logged :1; /* one-time logging */
+ BOOL timestamps_utc :1; /* Use UTC for all times */
+ BOOL transport_filter_timed_out :1; /* True if it did */
+ BOOL trusted_caller :1; /* Caller is trusted */
+ BOOL trusted_config :1; /* Configuration file is trusted */
+} f;
+
+
+/* General global variables */
+
+extern BOOL accept_8bitmime; /* Allow *BITMIME incoming */
+extern uschar *add_environment; /* List of environment variables to add */
+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 int acl_level; /* Nesting depth and debug indent */
+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 pcre2_code *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 tree_node *acl_var_c; /* ACL connection variables */
+extern tree_node *acl_var_m; /* ACL message 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 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 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 allow_domain_literals; /* As it says */
+extern BOOL allow_mx_to_ip; /* Allow MX records to -> ip address */
+#ifdef EXPERIMENTAL_ARC
+extern struct arc_set *arc_received; /* highest ARC instance evaluation struct */
+extern int arc_received_instance; /* highest ARC instance number in headers */
+extern int arc_oldest_pass; /* lowest passing instance number in headers */
+extern const uschar *arc_state; /* verification state */
+extern const uschar *arc_state_reason;
+#endif
+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 *authenticator_name; /* for debug and error messages */
+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 const uschar *auth_vars[]; /* $authn variables */
+extern int auto_thaw; /* Auto-thaw interval */
+#ifdef WITH_CONTENT_SCAN
+extern int av_failed; /* TRUE if the AV process failed */
+extern uschar *av_scanner; /* AntiVirus scanner to use for the malware condition */
+#endif
+
+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 int bsmtp_transaction_linecount; /* Start of last transaction */
+extern int body_8bitmime; /* sender declared BODY= ; 7=7BIT, 8=8BITMIME */
+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 int bounce_return_linesize_limit; /* Max line length in return */
+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 uschar *callout_address; /* Address used for a malware/spamd/verify etc. callout */
+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_eximarith_t 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_eximarith_t check_spool_space; /* Minimum for message acceptance */
+extern uschar *chunking_advertise_hosts; /* RFC 3030 CHUNKING */
+extern unsigned chunking_datasize;
+extern unsigned chunking_data_left;
+extern chunking_state_t chunking_state;
+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) */
+#ifndef DISABLE_CLIENT_CMD_LOG
+extern gstring *client_cmd_log; /* debug log of client cmds & responses */
+#endif
+extern int clmacro_count; /* Number of command line macros */
+extern uschar *clmacros[]; /* Copy of them, for re-exec */
+extern BOOL commandline_checks_require_admin; /* belt and braces for insecure setups */
+extern int connection_max_messages;/* Max down one SMTP connection */
+extern FILE *config_file; /* Configuration file */
+extern const uschar *config_filename; /* Configuration file name */
+extern gid_t config_gid; /* Additional group owner */
+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 */
+extern uschar *config_main_directory; /* Directory where the main config file was found */
+extern uid_t config_uid; /* Additional owner */
+extern uschar *continue_proxy_cipher; /* TLS cipher for proxied continued delivery */
+extern BOOL continue_proxy_dane; /* proxied conn is DANE */
+extern uschar *continue_proxy_sni; /* proxied conn SNI */
+extern uschar *continue_hostname; /* Host for continued delivery */
+extern uschar *continue_host_address; /* IP address for ditto */
+extern int continue_sequence; /* Sequence num for continued delivery */
+extern uschar *continue_transport; /* Transport for continued delivery */
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+extern unsigned continue_limit_mail; /* Peer advertised limit */
+extern unsigned continue_limit_rcpt;
+extern unsigned continue_limit_rcptdom;
+#endif
+
+
+extern uschar *csa_status; /* Client SMTP Authorization result */
+
+typedef struct {
+ unsigned callout_hold_only:1; /* Conn is only for verify callout */
+ unsigned delivery:1; /* When to attempt */
+ unsigned defer_pass:1; /* Pass 4xx to caller rather than spooling */
+ unsigned is_tls:1; /* Conn has TLS active */
+ client_conn_ctx cctx; /* Open connection */
+ int nrcpt; /* Count of addresses */
+ uschar * transport; /* Name of transport */
+ uschar * interface; /* (address of) */
+ uschar * snd_ip; /* sending_ip_address */
+ int snd_port; /* sending_port */
+ unsigned peer_options; /* smtp_peer_options */
+ host_item host; /* Host used */
+ address_item addr; /* (Chain of) addresses */
+} cut_t;
+extern cut_t cutthrough; /* Deliver-concurrently */
+
+extern int daemon_notifier_fd; /* Unix socket for notifications */
+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 int debug_fd; /* The fd for debug_file */
+extern FILE *debug_file; /* Where to write debugging info */
+extern int debug_notall[]; /* Debug options excluded from +all */
+extern bit_table debug_options[]; /* Table of debug options */
+extern int debug_options_count; /* Size of table */
+extern unsigned debug_pretrigger_bsize;
+extern uschar *debug_pretrigger_buf; /* circular buffer for precapture */
+extern BOOL debug_store; /* Do extra checks on store_reset */
+extern uschar debuglog_name[LOG_NAME_SIZE]; /* ACL-init debug */
+
+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 const uschar *deliver_domain; /* The local domain for delivery */
+extern uschar *deliver_domain_data; /* From domain lookup */
+extern const uschar *deliver_domain_orig; /* The original local domain for delivery */
+extern const uschar *deliver_domain_parent; /* The parent domain for delivery */
+extern BOOL deliver_drop_privilege; /* TRUE for unprivileged delivery */
+extern time_t deliver_frozen_at; /* Time of freezing */
+extern uschar *deliver_home; /* Home directory for pipes */
+extern const uschar *deliver_host; /* (First) host for routed local deliveries */
+ /* Remote host for filter */
+extern const uschar *deliver_host_address; /* Address for remote delivery filter */
+extern int deliver_host_port; /* 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 (de-tainted) */
+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_prefix_v; /* The stripped-prefix variable portion, if any */
+extern uschar *deliver_localpart_suffix; /* The stripped suffix, if any */
+extern uschar *deliver_localpart_suffix_v; /* The stripped-suffix variable portion, if any */
+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 uschar *deliver_selectstring_sender; /* For selecting by sender */
+#ifdef ENABLE_DISABLE_FSYNC
+extern BOOL disable_fsync; /* Not for normal use */
+#endif
+extern BOOL disable_ipv6; /* Don't do any IPv6 things */
+
+#ifndef DISABLE_DKIM
+extern unsigned dkim_collect_input; /* Runtime count of dkim signtures; tracks whether SMTP input is fed to DKIM validation */
+extern uschar *dkim_cur_signer; /* Expansion variable, holds the current "signer" domain or identity during a acl_smtp_dkim run */
+extern int dkim_key_length; /* Expansion variable, length of signing key in bits */
+extern void *dkim_signatures; /* Actually a (pdkim_signature *) but most files do not need to know */
+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_hashes; /* Preference order for signatures */
+extern uschar *dkim_verify_keytypes; /* Preference order for signatures */
+extern uschar *dkim_verify_min_keysizes; /* list of minimum key sizes, keyed by algo */
+extern BOOL dkim_verify_minimal; /* Shortcircuit signture verification */
+extern uschar *dkim_verify_overall; /* First successful domain verified, or null */
+extern uschar *dkim_verify_signers; /* Colon-separated list of domains for each of which we call the DKIM ACL */
+extern uschar *dkim_verify_status; /* result for this signature */
+extern uschar *dkim_verify_reason; /* result for this signature */
+#endif
+#ifdef SUPPORT_DMARC
+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 */
+#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 int dns_cname_loops; /* Follow CNAMEs returned by resolver to this depth */
+extern uschar *dns_ipv4_lookup; /* For these domains, don't look for AAAA (or A6) */
+#ifdef SUPPORT_DANE
+extern int dns_dane_ok; /* Ok to use DANE when checking TLS authenticity */
+#endif
+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 const uschar * dns_rc_names[]; /* Mostly for debug output */
+extern uschar *dns_trust_aa; /* DNSSEC trust AA as AD */
+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 */
+
+/* This option is now a no-opt, retained for compatibility */
+extern BOOL drop_cr; /* For broken local MUAs */
+extern const uschar *driver_srcfile; /* For debug & errors */
+extern int driver_srcline; /* For debug & errors */
+
+extern unsigned int dtrigger_selector; /* when to start debug */
+
+extern uschar *dsn_from; /* From: string for DSNs */
+
+extern BOOL 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*/
+
+#ifndef DISABLE_EVENT
+extern uschar *event_action; /* expansion for delivery events */
+extern uschar *event_data; /* event data */
+extern int event_defer_errno; /* error number set when a remote delivery is deferred with a host error */
+extern const uschar *event_name; /* event classification */
+#endif
+
+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_level; /* Nesting depth; indent for debug */
+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 const uschar *expand_nstring[]; /* Numbered strings */
+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 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 */
+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 volatile sig_atomic_t had_command_timeout; /* Alarm sighandler called */
+extern volatile sig_atomic_t had_command_sigterm; /* TERM sighandler called */
+extern volatile sig_atomic_t had_data_timeout; /* Alarm sighandler called */
+extern volatile sig_atomic_t had_data_sigint; /* TERM/INT sighandler called */
+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 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 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 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 *hosts_require_helo; /* check for HELO/EHLO before MAIL */
+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 int inetd_wait_timeout; /* Timeout for inetd wait mode */
+extern uschar *initial_cwd; /* The directory we where in at startup */
+extern uschar *iterate_item; /* Item from iterate list */
+
+extern int journal_fd; /* Fd for journal file */
+
+extern uschar *keep_environment; /* Whitelist for environment variables */
+extern int keep_malformed; /* Time to keep malformed messages */
+
+extern uschar *eldap_dn; /* Where LDAP DNs are left */
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+extern uschar *limits_advertise_hosts; /* for banner/EHLO pipelining */
+#endif
+extern int load_average; /* Most recently read load average */
+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 */
+#ifdef HAVE_LOCAL_SCAN
+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() */
+#endif
+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 int log_default[]; /* Initialization list for log_selector */
+extern uschar *log_file_path; /* If unset, use default */
+extern int log_notall[]; /* Log options excluded from +all */
+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 unsigned int log_selector[]; /* Bit map of logging options */
+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_timezone; /* TRUE to include the timezone in log lines */
+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 macro_item *macros_user; /* Non-builtin configuration macros */
+extern macro_item *mlast; /* Last item in macro list */
+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 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 */
+#ifdef SUPPORT_I18N
+extern BOOL message_smtputf8; /* Internationalized mail handling */
+extern int message_utf8_downconvert; /* convert from utf8 */
+const extern pcre2_code *regex_UTF8; /* For recognizing SMTPUTF8 settings */
+#endif
+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 */
+extern uschar *notifier_socket; /* Name for daemon notifier unix-socket */
+
+extern const int on; /* For setsockopt */
+extern const int off;
+
+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 pcre2_general_context * pcre_gen_ctx; /* pcre memory management */
+extern pcre2_compile_context * pcre_cmp_ctx;
+extern pcre2_match_context * pcre_mtc_ctx;
+
+extern uschar *percent_hack_domains; /* Local domains for which '% operates */
+extern uschar *pid_file_path; /* For writing daemon pids */
+#ifndef DISABLE_PIPE_CONNECT
+extern uschar *pipe_connect_advertise_hosts; /* for banner/EHLO pipelining */
+#endif
+extern uschar *pipelining_advertise_hosts; /* 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 const uschar *process_purpose; /* for debug output */
+extern BOOL prod_requires_admin; /* TRUE if prodding requires admin */
+
+#if defined(SUPPORT_PROXY) || defined(SUPPORT_SOCKS)
+extern uschar *hosts_proxy; /* Hostlist which (require) use proxy protocol */
+extern uschar *proxy_external_address; /* IP of remote interface of proxy */
+extern int proxy_external_port; /* Port on remote interface of proxy */
+extern uschar *proxy_local_address; /* IP of local interface of proxy */
+extern int proxy_local_port; /* Port on local interface of proxy */
+extern int proxy_protocol_timeout; /* Timeout for proxy negotiation */
+extern BOOL proxy_session; /* TRUE if receiving mail from valid proxy */
+#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 const uschar *qualify_domain_recipient; /* Domain to qualify recipients with */
+extern uschar *qualify_domain_sender; /* Domain to qualify senders with */
+extern uschar *queue_domains; /* Queue these domains */
+#ifndef DISABLE_QUEUE_RAMP
+extern BOOL queue_fast_ramp; /* 2-phase queue-run overlap */
+#endif
+extern BOOL queue_list_requires_admin; /* TRUE if -bp requires admin */
+ /* 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 uschar *queue_name; /* Name of queue, if nondefault spooling */
+extern uschar *queue_name_dest; /* Destination queue, for moving messages */
+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_run_in_order; /* As opposed to random */
+extern uschar *queue_run_max; /* Max queue runners */
+extern unsigned queue_size; /* items in queue */
+extern time_t queue_size_next; /* next time to evaluate queue_size */
+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 const uschar * rc_names[]; /* Mostly for debug output */
+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 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 struct timeval received_time; /* Time the message started to be received */
+extern struct timeval received_time_complete; /* Time the message completed reception */
+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 int recipients_list_max; /* Maximum number fitting in list */
+extern int recipients_max; /* Max permitted */
+extern BOOL recipients_max_reject; /* If TRUE, reject whole message */
+extern const pcre2_code *regex_AUTH; /* For recognizing AUTH settings */
+extern const pcre2_code *regex_check_dns_names; /* For DNS name checking */
+extern const pcre2_code *regex_From; /* For recognizing "From_" lines */
+extern const pcre2_code *regex_CHUNKING; /* For recognizing CHUNKING (RFC 3030) */
+extern const pcre2_code *regex_IGNOREQUOTA; /* For recognizing IGNOREQUOTA (LMTP) */
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+extern const pcre2_code *regex_LIMITS; /* For recognizing LIMITS */
+#endif
+extern const pcre2_code *regex_PIPELINING; /* For recognizing PIPELINING */
+extern const pcre2_code *regex_SIZE; /* For recognizing SIZE settings */
+#ifndef DISABLE_PIPE_CONNECT
+extern const pcre2_code *regex_EARLY_PIPE; /* For recognizing PIPE_CONNCT */
+#endif
+extern const pcre2_code *regex_ismsgid; /* Compiled r.e. for message ID */
+extern const pcre2_code *regex_smtp_code; /* For recognizing SMTP codes */
+extern const uschar *regex_vars[]; /* $regexN variables */
+#ifdef WHITELIST_D_MACROS
+extern const pcre2_code *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 tree_node *router_var; /* Variables set by router */
+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 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 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 BOOL sender_helo_dnssec; /* True if HELO verify used DNS and was DNSSEC */
+extern uschar *sender_helo_name; /* Host name from HELO/EHLO */
+extern uschar **sender_host_aliases; /* Points to list of alias names */
+extern uschar *sender_host_auth_pubname; /* Public-name of authentication method */
+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 uschar *sender_ident; /* Sender identity via RFC 1413 */
+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 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 const uschar *sigalarm_setter; /* For debug, set to callpoint of alarm() */
+extern uschar **sighup_argv; /* Args for re-execing after SIGHUP */
+extern int slow_lookup_log; /* Log DNS lookups taking longer than N millisecs */
+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 uschar *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 int smtp_backlog_monitor; /* listen backlog level to log */
+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 struct timeval 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_listen_backlog; /* Current listener socket backlog, if monitored */
+extern int smtp_load_reserve; /* Only from reserved if load > this */
+extern int smtp_mailcmd_count; /* Count of MAIL commands */
+extern int smtp_mailcmd_max; /* Limit for MAIL commands */
+extern int smtp_max_synprot_errors;/* Max syntax/protocol errors */
+extern int smtp_max_unknown_commands; /* As it says */
+extern uschar *smtp_names[]; /* decode for command codes */
+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_receive_timeout_s; /* ... expandable version */
+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 unsigned smtp_peer_options; /* Global flags for passed connections */
+extern unsigned smtp_peer_options_wrap; /* stacked version hidden by TLS */
+#ifdef SUPPORT_I18N
+extern uschar *smtputf8_advertise_hosts; /* ingress control */
+#endif
+
+#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_action; /* the spamd recommended-action */
+extern uschar *spam_score; /* the spam score (float) */
+extern uschar *spam_score_int; /* spam_score * 10 (int) */
+#endif
+#ifdef SUPPORT_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 BOOL spf_result_guessed; /* spf result is of best-guess operation */
+extern uschar *spf_smtp_comment; /* spf comment to include in SMTP reply */
+extern uschar *spf_smtp_comment_template;
+ /* template to construct the spf comment by libspf2 */
+#endif
+extern BOOL split_spool_directory; /* TRUE to use multiple subdirs */
+extern FILE *spool_data_file; /* handle for -D file */
+extern uschar *spool_directory; /* Name of spool directory */
+extern BOOL spool_wireformat; /* can write wireformat -D files */
+#ifdef SUPPORT_SRS
+extern uschar *srs_recipient; /* SRS recipient */
+#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 const uschar *submission_domain;/* Domain for submission mode */
+extern const uschar *submission_name; /* User name set from ACL */
+extern BOOL syslog_duplication; /* FALSE => no duplicate logging */
+extern int syslog_facility; /* As defined by Syslog.h */
+extern BOOL syslog_pid; /* TRUE if PID on syslogs */
+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 blob tcp_fastopen_nodata; /* for zero-data TFO connect requests */
+extern BOOL tcp_nodelay; /* Controls TCP_NODELAY on daemon */
+extern tfo_state_t tcp_out_fastopen; /* TCP fast open */
+#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 */
+#ifdef MEASURE_TIMING
+extern struct timeval timestamp_startup; /* For development measurements */
+#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 const uschar **transport_filter_argv; /* For on-the-fly filtering */
+extern int transport_filter_timeout; /* Timeout for same */
+
+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 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 *verify_mode; /* Running a router in verify mode */
+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/hash.c b/src/hash.c
new file mode 100644
index 0000000..51bcd46
--- /dev/null
+++ b/src/hash.c
@@ -0,0 +1,895 @@
+/*
+ * Exim - an Internet mail transport agent
+ *
+ * Copyright (c) The Exim Maintainers 2010 - 2022
+ * Copyright (c) University of Cambridge 1995 - 2009
+ *
+ * Hash interface functions
+ */
+
+#ifndef STAND_ALONE
+# include "exim.h"
+
+#else
+
+/* For stand-alone testing, we need to have the structure defined, and
+to be able to do I/O */
+
+# include <stdio.h>
+# include <stdlib.h>
+typedef unsigned char uschar;
+typedef struct sha1 {
+ unsigned int H[5];
+ unsigned int length;
+ }
+sha1;
+#endif /*STAND_ALONE*/
+
+#include <assert.h>
+
+/******************************************************************************/
+#ifdef SHA_OPENSSL
+# define HAVE_PARTIAL_SHA
+
+BOOL
+exim_sha_init(hctx * h, hashmethod m)
+{
+# if OPENSSL_VERSION_NUMBER < 0x30000000L
+switch (h->method = m)
+ {
+ case HASH_SHA1: h->hashlen = 20; SHA1_Init (&h->u.sha1); break;
+ case HASH_SHA2_256: h->hashlen = 32; SHA256_Init(&h->u.sha2_256); break;
+ case HASH_SHA2_384: h->hashlen = 48; SHA384_Init(&h->u.sha2_512); break;
+ case HASH_SHA2_512: h->hashlen = 64; SHA512_Init(&h->u.sha2_512); break;
+# ifdef EXIM_HAVE_SHA3
+ case HASH_SHA3_224: h->hashlen = 28;
+ EVP_DigestInit(h->u.mctx = EVP_MD_CTX_new(), EVP_sha3_224());
+ break;
+ case HASH_SHA3_256: h->hashlen = 32;
+ EVP_DigestInit(h->u.mctx = EVP_MD_CTX_new(), EVP_sha3_256());
+ break;
+ case HASH_SHA3_384: h->hashlen = 48;
+ EVP_DigestInit(h->u.mctx = EVP_MD_CTX_new(), EVP_sha3_384());
+ break;
+ case HASH_SHA3_512: h->hashlen = 64;
+ EVP_DigestInit(h->u.mctx = EVP_MD_CTX_new(), EVP_sha3_512());
+ break;
+# endif
+ default: h->hashlen = 0; return FALSE;
+ }
+return TRUE;
+
+# else
+EVP_MD * md;
+
+h->hashlen = 0;
+if (!(h->u.mctx = EVP_MD_CTX_new())) return FALSE;
+switch (h->method = m)
+ {
+ case HASH_SHA1: h->hashlen = 20; md = EVP_MD_fetch(NULL, "SHA1", NULL); break;
+ case HASH_SHA2_256: h->hashlen = 32; md = EVP_MD_fetch(NULL, "SHA2-256", NULL); break;
+ case HASH_SHA2_384: h->hashlen = 48; md = EVP_MD_fetch(NULL, "SHA2-384", NULL); break;
+ case HASH_SHA2_512: h->hashlen = 64; md = EVP_MD_fetch(NULL, "SHA2-512", NULL); break;
+ case HASH_SHA3_224: h->hashlen = 28; md = EVP_MD_fetch(NULL, "SHA3-224", NULL); break;
+ case HASH_SHA3_256: h->hashlen = 32; md = EVP_MD_fetch(NULL, "SHA3-256", NULL); break;
+ case HASH_SHA3_384: h->hashlen = 48; md = EVP_MD_fetch(NULL, "SHA3-384", NULL); break;
+ case HASH_SHA3_512: h->hashlen = 64; md = EVP_MD_fetch(NULL, "SHA3-512", NULL); break;
+ default: return FALSE;
+ }
+if (md && EVP_DigestInit_ex(h->u.mctx, md, NULL))
+ return TRUE;
+
+h->hashlen = 0;
+return FALSE;
+# endif
+}
+
+
+void
+exim_sha_update(hctx * h, const uschar * data, int len)
+{
+# if OPENSSL_VERSION_NUMBER < 0x30000000L
+switch (h->method)
+ {
+ case HASH_SHA1: SHA1_Update (&h->u.sha1, data, len); break;
+ case HASH_SHA2_256: SHA256_Update(&h->u.sha2_256, data, len); break;
+ case HASH_SHA2_384: SHA384_Update(&h->u.sha2_512, data, len); break;
+ case HASH_SHA2_512: SHA512_Update(&h->u.sha2_512, data, len); break;
+# ifdef EXIM_HAVE_SHA3
+ case HASH_SHA3_224:
+ case HASH_SHA3_256:
+ case HASH_SHA3_384:
+ case HASH_SHA3_512: EVP_DigestUpdate(h->u.mctx, data, len); break;
+# endif
+ /* should be blocked by init not handling these, but be explicit to
+ guard against accidents later (and hush up clang -Wswitch) */
+ default: assert(0);
+ }
+
+# else
+
+EVP_DigestUpdate(h->u.mctx, data, len);
+# endif
+}
+
+
+void
+exim_sha_finish(hctx * h, blob * b)
+{
+/* Hashing is sufficient to purify any tainted input */
+b->data = store_get(b->len = h->hashlen, GET_UNTAINTED);
+
+# if OPENSSL_VERSION_NUMBER < 0x30000000L
+switch (h->method)
+ {
+ case HASH_SHA1: SHA1_Final (b->data, &h->u.sha1); break;
+ case HASH_SHA2_256: SHA256_Final(b->data, &h->u.sha2_256); break;
+ case HASH_SHA2_384: SHA384_Final(b->data, &h->u.sha2_512); break;
+ case HASH_SHA2_512: SHA512_Final(b->data, &h->u.sha2_512); break;
+# ifdef EXIM_HAVE_SHA3
+ case HASH_SHA3_224:
+ case HASH_SHA3_256:
+ case HASH_SHA3_384:
+ case HASH_SHA3_512: EVP_DigestFinal(h->u.mctx, b->data, NULL); break;
+# endif
+ default: assert(0);
+ }
+
+# else
+
+EVP_DigestFinal_ex(h->u.mctx, b->data, NULL);
+EVP_MD_free((EVP_MD *) EVP_MD_CTX_get0_md(h->u.mctx));
+EVP_MD_CTX_free(h->u.mctx);
+
+# endif
+}
+
+
+
+#elif defined(SHA_GNUTLS)
+# define HAVE_PARTIAL_SHA
+/******************************************************************************/
+
+BOOL
+exim_sha_init(hctx * h, hashmethod m)
+{
+switch (h->method = m)
+ {
+ case HASH_SHA1: h->hashlen = 20; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA1); break;
+ case HASH_SHA2_256: h->hashlen = 32; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA256); break;
+ case HASH_SHA2_384: h->hashlen = 48; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA384); break;
+ case HASH_SHA2_512: h->hashlen = 64; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA512); break;
+#ifdef EXIM_HAVE_SHA3
+ case HASH_SHA3_224: h->hashlen = 28; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA3_224); break;
+ case HASH_SHA3_256: h->hashlen = 32; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA3_256); break;
+ case HASH_SHA3_384: h->hashlen = 48; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA3_384); break;
+ case HASH_SHA3_512: h->hashlen = 64; gnutls_hash_init(&h->sha, GNUTLS_DIG_SHA3_512); break;
+#endif
+ default: h->hashlen = 0; return FALSE;
+ }
+return TRUE;
+}
+
+
+void
+exim_sha_update(hctx * h, const uschar * data, int len)
+{
+gnutls_hash(h->sha, data, len);
+}
+
+
+void
+exim_sha_finish(hctx * h, blob * b)
+{
+b->data = store_get(b->len = h->hashlen, GET_UNTAINTED);
+gnutls_hash_output(h->sha, b->data);
+}
+
+
+
+#elif defined(SHA_GCRYPT)
+# define HAVE_PARTIAL_SHA
+/******************************************************************************/
+
+BOOL
+exim_sha_init(hctx * h, hashmethod m)
+{
+switch (h->method = m)
+ {
+ case HASH_SHA1: h->hashlen = 20; gcry_md_open(&h->sha, GCRY_MD_SHA1, 0); break;
+ case HASH_SHA2_256: h->hashlen = 32; gcry_md_open(&h->sha, GCRY_MD_SHA256, 0); break;
+ case HASH_SHA2_384: h->hashlen = 48; gcry_md_open(&h->sha, GCRY_MD_SHA384, 0); break;
+ case HASH_SHA2_512: h->hashlen = 64; gcry_md_open(&h->sha, GCRY_MD_SHA512, 0); break;
+ case HASH_SHA3_256: h->hashlen = 32; gcry_md_open(&h->sha, GCRY_MD_SHA3_256, 0); break;
+ case HASH_SHA3_384: h->hashlen = 48; gcry_md_open(&h->sha, GCRY_MD_SHA3_384, 0); break;
+ case HASH_SHA3_512: h->hashlen = 64; gcry_md_open(&h->sha, GCRY_MD_SHA3_512, 0); break;
+ default: h->hashlen = 0; return FALSE;
+ }
+return TRUE;
+}
+
+
+void
+exim_sha_update(hctx * h, const uschar * data, int len)
+{
+gcry_md_write(h->sha, data, len);
+}
+
+
+void
+exim_sha_finish(hctx * h, blob * b)
+{
+b->data = store_get(b->len = h->hashlen, GET_UNTAINTED);
+memcpy(b->data, gcry_md_read(h->sha, 0), h->hashlen);
+}
+
+
+
+
+#elif defined(SHA_POLARSSL)
+# define HAVE_PARTIAL_SHA
+/******************************************************************************/
+
+BOOL
+exim_sha_init(hctx * h, hashmethod m)
+{
+/*XXX extend for sha512 */
+switch (h->method = m)
+ {
+ case HASH_SHA1: h->hashlen = 20; sha1_starts(&h->u.sha1); break;
+ case HASH_SHA2_256: h->hashlen = 32; sha2_starts(&h->u.sha2, 0); break;
+ default: h->hashlen = 0; return FALSE;
+ }
+return TRUE;
+}
+
+
+void
+exim_sha_update(hctx * h, const uschar * data, int len)
+{
+switch (h->method)
+ {
+ case HASH_SHA1: sha1_update(h->u.sha1, US data, len); break;
+ case HASH_SHA2_256: sha2_update(h->u.sha2, US data, len); break;
+ }
+}
+
+
+void
+exim_sha_finish(hctx * h, blob * b)
+{
+b->data = store_get(b->len = h->hashlen, GET_INTAINTED);
+switch (h->method)
+ {
+ case HASH_SHA1: sha1_finish(h->u.sha1, b->data); break;
+ case HASH_SHA2_256: sha2_finish(h->u.sha2, b->data); break;
+ }
+}
+
+
+
+
+#elif defined(SHA_NATIVE)
+/******************************************************************************/
+/* Only sha-1 supported */
+
+/*************************************************
+* Start off a new SHA-1 computation. *
+*************************************************/
+
+/*
+Argument: pointer to sha1 storage structure
+Returns: nothing
+*/
+
+static void
+native_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
+*/
+
+static void
+native_sha1_mid(sha1 *base, const uschar *text)
+{
+uint A, B, C, D, E;
+uint W[80];
+
+base->length += 64;
+
+for (int i = 0; i < 16; i++)
+ {
+ W[i] = ((uint)text[0] << 24) | (text[1] << 16) | (text[2] << 8) | text[3];
+ text += 4;
+ }
+
+for (int 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 (int 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 (int 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 (int 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 (int 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
+*/
+
+static void
+native_sha1_end(sha1 *base, const uschar *text, int length, uschar *digest)
+{
+uschar work[64];
+
+/* Process in chunks of 64 until we have less than 64 bytes left. */
+
+while (length >= 64)
+ {
+ native_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);
+ native_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 */
+
+native_sha1_mid(base, work);
+
+/* Pass back the result, high-order byte first in each word. */
+
+for (int 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;
+ }
+}
+
+
+
+
+
+
+# ifdef notdef
+BOOL
+exim_sha_init(hctx * h, hashmethod m)
+{
+h->hashlen = 20;
+native_sha1_start(&h->sha1);
+return TRUE;
+}
+
+
+void
+exim_sha_update(hctx * h, const uschar * data, int len)
+{
+native_sha1_mid(&h->sha1, US data); /* implicit size always 64 */
+}
+
+
+void
+exim_sha_finish(hctx * h, blob * b)
+{
+b->data = store_get(b->len = h->hashlen, GET_UNTAINTED);
+
+native_sha1_end(&h->sha1, NULL, 0, b->data);
+}
+# endif
+
+
+#endif
+
+
+/******************************************************************************/
+/******************************************************************************/
+/******************************************************************************/
+/******************************************************************************/
+/* Original sha-1 interface used by crypteq{shal1},
+${sha1:} ${hmac:} and ${prvs:} */
+
+#ifdef SHA_NATIVE
+
+void
+sha1_start(hctx * h)
+{
+native_sha1_start(&h->sha1);
+}
+
+void
+sha1_mid(hctx * h, const uschar * data)
+{
+native_sha1_mid(&h->sha1, data);
+}
+
+void
+sha1_end(hctx * h, const uschar * data, int len, uschar *digest)
+{
+native_sha1_end(&h->sha1, data, len, digest);
+}
+
+#else
+
+void
+sha1_start(hctx * h)
+{
+(void) exim_sha_init(h, HASH_SHA1);
+}
+
+void
+sha1_mid(hctx * h, const uschar * data)
+{
+exim_sha_update(h, data, 64);
+}
+
+void
+sha1_end(hctx * h, const uschar * data, int len, uschar *digest)
+{
+blob b;
+exim_sha_update(h, data, len);
+exim_sha_finish(h, &b);
+memcpy(digest, b.data, 20);
+}
+
+#endif
+
+
+
+#ifdef HAVE_PARTIAL_SHA
+# undef HAVE_PARTIAL_SHA
+void
+exim_sha_update_string(hctx * h, const uschar * s)
+{
+if (s) exim_sha_update(h, s, Ustrlen(s));
+}
+#endif
+
+
+
+/*************************************************
+**************************************************
+* 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 = US (&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]);
+ native_sha1_start(&base);
+ native_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);
+native_sha1_start(&base);
+native_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 /*STAND_ALONE*/
+
+/* End of File */
diff --git a/src/hash.h b/src/hash.h
new file mode 100644
index 0000000..588325b
--- /dev/null
+++ b/src/hash.h
@@ -0,0 +1,83 @@
+/*
+ * Exim - an Internet mail transport agent
+ * Copyright (c) The Exim Maintainers 1995 - 2022
+ *
+ * Hash interface functions
+ */
+
+#include "exim.h"
+
+#if !defined(HASH_H) /* entire file */
+#define HASH_H
+
+#include "sha_ver.h"
+
+#ifdef SHA_OPENSSL
+# include <openssl/sha.h>
+#elif defined SHA_GNUTLS
+# include <gnutls/crypto.h>
+#elif defined(SHA_GCRYPT)
+# include <gcrypt.h>
+#elif defined(SHA_POLARSSL)
+# include "pdkim/pdkim.h" /*XXX ugly */
+# include "pdkim/polarssl/sha1.h"
+# include "pdkim/polarssl/sha2.h"
+#endif
+
+
+/* Hash context for the exim_sha_* routines */
+
+typedef enum hashmethod {
+ HASH_BADTYPE,
+ HASH_NULL,
+ HASH_SHA1,
+
+ HASH_SHA2_256,
+ HASH_SHA2_384,
+ HASH_SHA2_512,
+
+ HASH_SHA3_224,
+ HASH_SHA3_256,
+ HASH_SHA3_384,
+ HASH_SHA3_512,
+} hashmethod;
+
+typedef struct {
+ hashmethod method;
+ int hashlen;
+
+#ifdef SHA_OPENSSL
+ union {
+ SHA_CTX sha1; /* SHA1 block */
+ SHA256_CTX sha2_256; /* SHA256 or 224 block */
+ SHA512_CTX sha2_512; /* SHA512 or 384 block */
+#ifdef EXIM_HAVE_SHA3
+ EVP_MD_CTX * mctx; /* SHA3 block */
+#endif
+ } u;
+
+#elif defined(SHA_GNUTLS)
+ gnutls_hash_hd_t sha; /* Either SHA1 or SHA256 block */
+
+#elif defined(SHA_GCRYPT)
+ gcry_md_hd_t sha; /* Either SHA1 or SHA256 block */
+
+#elif defined(SHA_POLARSSL)
+ union {
+ sha1_context sha1; /* SHA1 block */
+ sha2_context sha2; /* SHA256 block */
+ } u;
+
+#elif defined(SHA_NATIVE)
+ sha1 sha1;
+#endif
+
+} hctx;
+
+extern BOOL exim_sha_init(hctx *, hashmethod);
+extern void exim_sha_update(hctx *, const uschar *a, int);
+extern void exim_sha_update_string(hctx *, const uschar *a);
+extern void exim_sha_finish(hctx *, blob *);
+
+#endif
+/* End of File */
diff --git a/src/header.c b/src/header.c
new file mode 100644
index 0000000..898d8d5
--- /dev/null
+++ b/src/header.c
@@ -0,0 +1,468 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2016 */
+/* 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: pointer to header struct (last one, if multiple added)
+*/
+
+static header_line *
+header_add_backend(BOOL after, uschar *name, BOOL topnot, int type,
+ const char *format, va_list ap)
+{
+header_line *h, *new = NULL;
+header_line **hptr;
+
+uschar * p, * q, * buf;
+gstring gs;
+
+if (!header_last) return NULL;
+
+gs.s = buf = store_get(HEADER_ADD_BUFFER_SIZE, GET_UNTAINTED);
+gs.size = HEADER_ADD_BUFFER_SIZE;
+gs.ptr = 0;
+
+if (!string_vformat(&gs, SVFMT_REBUFFER, format, ap))
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string too long in header_add: "
+ "%.100s ...", string_from_gstring(&gs));
+
+if (gs.s != buf) store_release_above(buf);
+gstring_release_unused(&gs);
+string_from_gstring(&gs);
+
+/* Find where to insert this header */
+
+if (!name)
+ 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 actually filled in. We want
+ that header to be first, so skip it for now. */
+
+ if (!header_list->text)
+ hptr = &header_list->next;
+ h = *hptr;
+ }
+
+else
+ {
+ int len = Ustrlen(name);
+
+ /* Find the first non-deleted header with the correct name. */
+
+ for (hptr = &header_list; (h = *hptr); 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)
+ {
+ 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 || !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 = gs.s; *p; p = q)
+ {
+ for (;;)
+ {
+ q = Ustrchr(q, '\n');
+ if (!q) q = p + Ustrlen(p);
+ if (*(++q) != ' ' && *q != '\t') break;
+ }
+
+ new = store_get(sizeof(header_line), GET_UNTAINTED);
+ new->text = string_copyn(p, q - p);
+ new->slen = q - p;
+ new->type = type;
+ new->next = h;
+
+ *hptr = new;
+ hptr = &new->next;
+
+ if (!h) header_last = new;
+ }
+return new;
+}
+
+
+/*************************************************
+* 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: pointer to header struct added
+*/
+
+header_line *
+header_add_at_position_internal(BOOL after, uschar *name, BOOL topnot, int type,
+ const char *format, ...)
+{
+header_line * h;
+va_list ap;
+va_start(ap, format);
+h = header_add_backend(after, name, topnot, type, format, ap);
+va_end(ap);
+return h;
+}
+
+
+/* Documented external i/f for local_scan */
+void
+header_add_at_position(BOOL after, uschar *name, BOOL topnot, int type,
+ const char *format, ...)
+{
+va_list ap;
+va_start(ap, format);
+(void) 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);
+(void) 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)
+{
+int hcount = 0;
+int len = Ustrlen(name);
+for (header_line * h = header_list; h; 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;
+ if (Uskip_whitespace(&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;
+const pcre2_code *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 (header_line * h = header_list; !yield && h; 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)
+ {
+ 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) break;
+
+ /* Otherwise, test for the pattern; a non-regex must be an exact match */
+
+ yield = re
+ ? regex_match(re, next, -1, NULL)
+ : (strcmpic(next, pattern) == 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
+ ? regex_match(re, h->text, h->slen, NULL)
+ : (strstric(h->text, pattern, FALSE) != NULL);
+ }
+ }
+
+return yield;
+}
+
+
+/* The externally visible interface */
+
+BOOL
+header_match(uschar *name, BOOL has_addresses, BOOL cond, string_item *strings,
+ int count, ...)
+{
+va_list ap;
+int slen = Ustrlen(name);
+
+for (string_item * s = strings; s; s = s->next)
+ if (one_pattern_match(name, slen, has_addresses, s->text))
+ return cond;
+
+va_start(ap, count);
+for (int i = 0; i < count; i++)
+ if (one_pattern_match(name, slen, has_addresses, va_arg(ap, uschar *)))
+ {
+ va_end(ap);
+ return cond;
+ }
+va_end(ap);
+
+return !cond;
+}
+
+/* End of header.c */
diff --git a/src/hintsdb.h b/src/hintsdb.h
new file mode 100644
index 0000000..b8e6744
--- /dev/null
+++ b/src/hintsdb.h
@@ -0,0 +1,810 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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. */
+
+#ifndef HINTSDB_H
+#define HINTSDB_H
+
+
+#if defined(USE_TDB)
+
+/* ************************* tdb interface ************************ */
+/*XXX https://manpages.org/tdb/3 mentions concurrent writes.
+Could we lose the file lock? */
+
+# 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 - return pointer to an EXIM_DB, NULL if failed */
+static inline EXIM_DB *
+exim_dbopen__(const uschar * name, const uschar * dirname, int flags,
+ unsigned mode)
+{
+return tdb_open(CS name, 0, TDB_DEFAULT, flags, mode);
+}
+
+/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */
+static inline BOOL
+exim_dbget(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * res)
+{
+*res = tdb_fetch(dbp, *key); /* A struct arg and return!! */
+return res->dptr != NULL;
+}
+
+/* EXIM_DBPUT - returns nothing useful, assumes replace mode */
+static inline int
+exim_dbput(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data)
+{ return tdb_store(dbp, *key, *data, TDB_REPLACE); }
+
+/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */
+static inline int
+exim_dbputb(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data)
+{ return tdb_store(dbp, *key, *data, TDB_INSERT); }
+
+/* Returns from EXIM_DBPUTB */
+
+# define EXIM_DBPUTB_OK 0
+# define EXIM_DBPUTB_DUP (-1)
+
+/* EXIM_DBDEL */
+static inline int
+exim_dbdel(EXIM_DB * dbp, EXIM_DATUM * key)
+{ return tdb_delete(dbp, *key); }
+
+/* EXIM_DBCREATE_CURSOR - initialize for scanning operation */
+static inline EXIM_CURSOR *
+exim_dbcreate_cursor(EXIM_DB * dbp)
+{
+EXIM_CURSOR * c = store_malloc(sizeof(TDB_DATA));
+c->dptr = NULL;
+return c;
+}
+
+/* EXIM_DBSCAN - This is complicated because we have to free the last datum
+free() must not die when passed NULL */
+
+static inline BOOL
+exim_dbscan(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * res, BOOL first,
+ EXIM_CURSOR * cursor)
+{
+*key = first ? tdb_firstkey(dbp) : tdb_nextkey(dbp, *cursor);
+free(cursor->dptr);
+*cursor = *key;
+return key->dptr != NULL;
+}
+
+/* EXIM_DBDELETE_CURSOR - terminate scanning operation. */
+static inline void
+exim_dbdelete_cursor(EXIM_CURSOR * cursor)
+{ store_free(cursor); }
+
+/* EXIM_DBCLOSE */
+static inline void
+exim_dbclose__(EXIM_DB * db)
+{ tdb_close(db); }
+
+/* Datum access */
+
+static inline uschar *
+exim_datum_data_get(EXIM_DATUM * dp)
+{ return US dp->dptr; }
+static inline void
+exim_datum_data_set(EXIM_DATUM * dp, void * s)
+{ dp->dptr = s; }
+
+static inline unsigned
+exim_datum_size_get(EXIM_DATUM * dp)
+{ return dp->dsize; }
+static inline void
+exim_datum_size_set(EXIM_DATUM * dp, unsigned n)
+{ dp->dsize = n; }
+
+/* No initialization is needed. */
+
+static inline void
+exim_datum_init(EXIM_DATUM * d)
+{ }
+
+/* Free the stuff inside the datum. */
+
+static inline void
+exim_datum_free(EXIM_DATUM * d)
+{
+free(d->dptr);
+d->dptr = NULL;
+}
+
+/* size limit */
+
+# define EXIM_DB_RLIMIT 150
+
+
+
+
+
+
+/********************* Berkeley db native definitions **********************/
+
+#elif defined USE_DB
+
+# include <db.h>
+
+/* 1.x did no locking
+ 2.x had facilities, but exim does it's own
+ 3.x+ unknown
+*/
+
+/* 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
+
+# if DB_VERSION_MAJOR >= 6
+# error Version 6 and later BDB API is not supported
+# endif
+
+/* 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 */
+# if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
+# define EXIM_DB DB_ENV
+/* 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 (v4.1+)"
+
+/* Only more-recent versions. 5+ ? */
+# ifndef DB_FORCESYNC
+# define DB_FORCESYNC 0
+# endif
+
+/* 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. */
+
+static inline void
+dbfn_bdb_error_callback(const DB_ENV * dbenv, const char * pfx, const char * msg)
+{
+#ifndef MACRO_PREDEF
+log_write(0, LOG_MAIN, "Berkeley DB error: %s", msg);
+#endif
+}
+
+
+
+/* Access functions */
+
+/* EXIM_DBOPEN - return pointer to an EXIM_DB, NULL if failed */
+/* The API changed for DB 4.1. - and we also starting using the "env" with a
+specified working dir, to avoid the DBCONFIG file trap. */
+
+# define ENV_TO_DB(env) ((DB *)(((EXIM_DB *)env)->app_private))
+
+static inline EXIM_DB *
+exim_dbopen__(const uschar * name, const uschar * dirname, int flags,
+ unsigned mode)
+{
+EXIM_DB * dbp;
+DB * b;
+if ( db_env_create(&dbp, 0) != 0
+ || (dbp->set_errcall(dbp, dbfn_bdb_error_callback), 0)
+ || dbp->open(dbp, CS dirname, DB_CREATE|DB_INIT_MPOOL|DB_PRIVATE, 0) != 0
+ )
+ return NULL;
+if (db_create(&b, dbp, 0) == 0)
+ {
+ dbp->app_private = b;
+ if (b->open(b, NULL, CS name, NULL,
+ flags == O_RDONLY ? DB_UNKNOWN : DB_HASH,
+ flags == O_RDONLY ? DB_RDONLY : DB_CREATE,
+ mode) == 0
+ )
+ return dbp;
+
+ b->close(b, 0);
+ }
+dbp->close(dbp, 0);
+return NULL;
+}
+
+/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */
+static inline BOOL
+exim_dbget(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * res)
+{
+DB * b = ENV_TO_DB(dbp);
+return b->get(b, NULL, key, res, 0) == 0;
+}
+
+/* EXIM_DBPUT - returns nothing useful, assumes replace mode */
+static inline int
+exim_dbput(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data)
+{
+DB * b = ENV_TO_DB(dbp);
+return b->put(b, NULL, key, data, 0);
+}
+
+/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */
+static inline int
+exim_dbputb(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data)
+{
+DB * b = ENV_TO_DB(dbp);
+return b->put(b, NULL, key, data, DB_NOOVERWRITE);
+}
+
+/* Return values from EXIM_DBPUTB */
+
+# define EXIM_DBPUTB_OK 0
+# define EXIM_DBPUTB_DUP DB_KEYEXIST
+
+/* EXIM_DBDEL */
+static inline int
+exim_dbdel(EXIM_DB * dbp, EXIM_DATUM * key)
+{
+DB * b = ENV_TO_DB(dbp);
+return b->del(b, NULL, key, 0);
+}
+
+/* EXIM_DBCREATE_CURSOR - initialize for scanning operation */
+
+static inline EXIM_CURSOR *
+exim_dbcreate_cursor(EXIM_DB * dbp)
+{
+DB * b = ENV_TO_DB(dbp);
+EXIM_CURSOR * c;
+b->cursor(b, NULL, &c, 0);
+return c;
+}
+
+/* EXIM_DBSCAN - returns TRUE if data is returned, FALSE at end */
+static inline BOOL
+exim_dbscan(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data, BOOL first,
+ EXIM_CURSOR * cursor)
+{
+return cursor->c_get(cursor, key, data, first ? DB_FIRST : DB_NEXT) == 0;
+}
+
+/* EXIM_DBDELETE_CURSOR - terminate scanning operation */
+static inline void
+exim_dbdelete_cursor(EXIM_CURSOR * cursor)
+{ cursor->c_close(cursor); }
+
+/* EXIM_DBCLOSE */
+static inline void
+exim_dbclose__(EXIM_DB * dbp_o)
+{
+DB_ENV * dbp = dbp_o;
+DB * b = ENV_TO_DB(dbp);
+b->close(b, 0);
+dbp->close(dbp, DB_FORCESYNC);
+}
+
+/* Datum access */
+
+static inline uschar *
+exim_datum_data_get(EXIM_DATUM * dp)
+{ return dp->data; }
+static inline void
+exim_datum_data_set(EXIM_DATUM * dp, void * s)
+{ dp->data = s; }
+
+static inline unsigned
+exim_datum_size_get(EXIM_DATUM * dp)
+{ return dp->size; }
+static inline void
+exim_datum_size_set(EXIM_DATUM * dp, unsigned n)
+{ dp->size = n; }
+
+/* The whole datum structure contains other fields that must be cleared
+before use, but we don't have to free anything after reading data. */
+
+static inline void
+exim_datum_init(EXIM_DATUM * d)
+{ memset(d, 0, sizeof(*d)); }
+
+static inline void
+exim_datum_free(EXIM_DATUM * d)
+{ }
+
+# else /* pre- 4.1 */
+
+# 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 - return pointer to an EXIM_DB, NULL if failed */
+static inline EXIM_DB *
+exim_dbopen__(const uschar * name, const uschar * dirname, int flags,
+ unsigned mode)
+{
+EXIM_DB * dbp;
+return db_create(&dbp, NULL, 0) == 0
+ && ( dbp->set_errcall(dbp, dbfn_bdb_error_callback),
+ dbp->open(dbp, CS name, NULL,
+ flags == O_RDONLY ? DB_UNKNOWN : DB_HASH,
+ flags == O_RDONLY ? DB_RDONLY : DB_CREATE,
+ mode)
+ ) == 0
+ ? dbp : NULL;
+}
+
+/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */
+static inline BOOL
+exim_dbget(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * res)
+{ return dbp->get(dbp, NULL, key, res, 0) == 0; }
+
+/* EXIM_DBPUT - returns nothing useful, assumes replace mode */
+static inline int
+exim_dbput(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data)
+{ return dbp->put(dbp, NULL, key, data, 0); }
+
+/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */
+static inline int
+exim_dbputb(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data)
+{ return dbp->put(dbp, NULL, key, data, DB_NOOVERWRITE); }
+
+/* Return values from EXIM_DBPUTB */
+
+# define EXIM_DBPUTB_OK 0
+# define EXIM_DBPUTB_DUP DB_KEYEXIST
+
+/* EXIM_DBDEL */
+static inline int
+exim_dbdel(EXIM_DB * dbp, EXIM_DATUM * key)
+{ return dbp->del(dbp, NULL, key, 0); }
+
+/* EXIM_DBCREATE_CURSOR - initialize for scanning operation */
+
+static inline EXIM_CURSOR *
+exim_dbcreate_cursor(EXIM_DB * dbp)
+{
+EXIM_CURSOR * c;
+dbp->cursor(dbp, NULL, &c, 0);
+return c;
+}
+
+/* EXIM_DBSCAN - returns TRUE if data is returned, FALSE at end */
+static inline BOOL
+exim_dbscan(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data, BOOL first,
+ EXIM_CURSOR * cursor)
+{
+return cursor->c_get(cursor, key, data, first ? DB_FIRST : DB_NEXT) == 0;
+}
+
+/* EXIM_DBDELETE_CURSOR - terminate scanning operation */
+static inline void
+exim_dbdelete_cursor(EXIM_CURSOR * cursor)
+{ cursor->c_close(cursor); }
+
+/* EXIM_DBCLOSE */
+static inline void
+exim_dbclose__(EXIM_DB * dbp)
+{ dbp->close(dbp, 0); }
+
+/* Datum access */
+
+static inline uschar *
+exim_datum_data_get(EXIM_DATUM * dp)
+{ return US dp->dptr; }
+static inline void
+exim_datum_data_set(EXIM_DATUM * dp, void * s)
+{ dp->dptr = s; }
+
+static inline uschar *
+exim_datum_size_get(EXIM_DATUM * dp)
+{ return US dp->size; }
+static inline void
+exim_datum_size_set(EXIM_DATUM * dp, uschar * s)
+{ dp->size = CS s; }
+
+/* The whole datum structure contains other fields that must be cleared
+before use, but we don't have to free anything after reading data. */
+
+static inline void
+exim_datum_init(EXIM_DATUM * d)
+{ memset(d, 0, sizeof(*d)); }
+
+static inline void
+exim_datum_free(EXIM_DATUM * d)
+{ }
+
+# endif
+
+
+# else /* DB_VERSION_MAJOR >= 3 */
+# error Berkeley DB versions earlier than 3 are not supported */
+# endif /* DB_VERSION_MAJOR */
+# else
+# error Berkeley DB version 1 is no longer supported
+# endif /* DB_VERSION_STRING */
+
+
+/* all BDB versions */
+/* size limit */
+
+# define EXIM_DB_RLIMIT 150
+
+
+
+
+
+
+/********************* gdbm interface definitions **********************/
+
+#elif defined USE_GDBM
+/*XXX TODO: exim's locfile not needed */
+
+# 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 - return pointer to an EXIM_DB, NULL if failed */
+static inline EXIM_DB *
+exim_dbopen__(const uschar * name, const uschar * dirname, int flags,
+ unsigned mode)
+{
+EXIM_DB * dbp = malloc(sizeof(EXIM_DB)); /*XXX why not exim mem-mgmt? */
+if (dbp)
+ {
+ dbp->lkey.dptr = NULL;
+ dbp->gdbm = gdbm_open(CS name, 0,
+ flags & O_CREAT ? GDBM_WRCREAT
+ : flags & (O_RDWR|O_WRONLY) ? GDBM_WRITER
+ : GDBM_READER,
+ mode, 0);
+ if (dbp->gdbm) return dbp;
+ free(dbp);
+ }
+return NULL;
+}
+
+/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */
+static inline BOOL
+exim_dbget(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * res)
+{
+*res = gdbm_fetch(dbp->gdbm, *key); /* A struct arg & return! */
+return res->dptr != NULL;
+}
+
+/* EXIM_DBPUT - returns nothing useful, assumes replace mode */
+static inline int
+exim_dbput(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data)
+{ return gdbm_store(dbp->gdbm, *key, *data, GDBM_REPLACE); }
+
+/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */
+static inline int
+exim_dbputb(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data)
+{ return gdbm_store(dbp->gdbm, *key, *data, GDBM_INSERT); }
+
+/* Returns from EXIM_DBPUTB */
+
+# define EXIM_DBPUTB_OK 0
+# define EXIM_DBPUTB_DUP 1
+
+/* EXIM_DBDEL */
+static inline int
+exim_dbdel(EXIM_DB * dbp, EXIM_DATUM * key)
+{ return gdbm_delete(dbp->gdbm, *key); }
+
+/* EXIM_DBCREATE_CURSOR - initialize for scanning operation (null) */
+static inline EXIM_CURSOR *
+exim_dbcreate_cursor(EXIM_DB * dbp)
+{ return NULL; }
+
+/* EXIM_DBSCAN */
+static inline BOOL
+exim_dbscan(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data, BOOL first,
+ EXIM_CURSOR * cursor)
+{
+char * s;
+*key = first ? gdbm_firstkey(dbp->gdbm) : gdbm_nextkey(dbp->gdbm, dbp->lkey);
+if ((s = dbp->lkey.dptr)) free(s);
+dbp->lkey = *key;
+return key->dptr != NULL;
+}
+
+/* EXIM_DBDELETE_CURSOR - terminate scanning operation (null). */
+static inline void
+exim_dbdelete_cursor(EXIM_CURSOR * cursor)
+{ }
+
+/* EXIM_DBCLOSE */
+static inline void
+exim_dbclose__(EXIM_DB * dbp)
+{
+char * s;
+gdbm_close(dbp->gdbm);
+if ((s = dbp->lkey.dptr)) free(s);
+free(dbp);
+}
+
+/* Datum access types */
+
+static inline uschar *
+exim_datum_data_get(EXIM_DATUM * dp)
+{ return US dp->dptr; }
+static inline void
+exim_datum_data_set(EXIM_DATUM * dp, void * s)
+{ dp->dptr = s; }
+
+static inline unsigned
+exim_datum_size_get(EXIM_DATUM * dp)
+{ return dp->dsize; }
+static inline void
+exim_datum_size_set(EXIM_DATUM * dp, unsigned n)
+{ dp->dsize = n; }
+
+/* There's no clearing required before use, but we have to free the dptr
+after reading data. */
+
+static inline void
+exim_datum_init(EXIM_DATUM * d)
+{ }
+
+static inline void
+exim_datum_free(EXIM_DATUM * d)
+{ free(d->dptr); }
+
+/* size limit */
+
+# define EXIM_DB_RLIMIT 150
+
+#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 */
+/* Check that the name given is not present. This catches
+a directory name; otherwise we would create the name.pag and
+name.dir files in the directory's parent. */
+
+static inline EXIM_DB *
+exim_dbopen__(const uschar * name, const uschar * dirname, int flags,
+ unsigned mode)
+{
+struct stat st;
+if (!(flags & O_CREAT) || lstat(CCS name, &st) != 0 && errno == ENOENT)
+ return dbm_open(CS name, flags, mode);
+#ifndef COMPILE_UTILITY
+debug_printf("%s %d errno %s\n", __FUNCTION__, __LINE__, strerror(errno));
+#endif
+errno = (st.st_mode & S_IFMT) == S_IFDIR ? EISDIR : EEXIST;
+return NULL;
+}
+
+/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */
+static inline BOOL
+exim_dbget(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * res)
+{
+*res = dbm_fetch(dbp, *key); /* A struct arg & return! */
+return res->dptr != NULL;
+}
+
+/* EXIM_DBPUT - returns nothing useful, assumes replace mode */
+static inline int
+exim_dbput(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data)
+{ return dbm_store(dbp, *key, *data, DBM_REPLACE); }
+
+/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */
+static inline int
+exim_dbputb(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data)
+{ return dbm_store(dbp, *key, *data, DBM_INSERT); }
+
+/* Returns from EXIM_DBPUTB */
+
+# define EXIM_DBPUTB_OK 0
+# define EXIM_DBPUTB_DUP 1
+
+/* EXIM_DBDEL */
+static inline int
+exim_dbdel(EXIM_DB * dbp, EXIM_DATUM * key)
+{ return dbm_delete(dbp, *key); }
+
+/* EXIM_DBCREATE_CURSOR - initialize for scanning operation (null) */
+static inline EXIM_CURSOR *
+exim_dbcreate_cursor(EXIM_DB * dbp)
+{ return NULL; }
+
+/* EXIM_DBSCAN */
+static inline BOOL
+exim_dbscan(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data, BOOL first,
+ EXIM_CURSOR * cursor)
+{
+*key = first ? dbm_firstkey(dbp) : dbm_nextkey(dbp);
+return key->dptr != NULL;
+}
+
+/* EXIM_DBDELETE_CURSOR - terminate scanning operation (null). */
+static inline void
+exim_dbdelete_cursor(EXIM_CURSOR * cursor)
+{ }
+
+/* EXIM_DBCLOSE */
+static inline void
+exim_dbclose__(EXIM_DB * dbp)
+{ dbm_close(dbp); }
+
+/* Datum access types */
+
+static inline uschar *
+exim_datum_data_get(EXIM_DATUM * dp)
+{ return US dp->dptr; }
+static inline void
+exim_datum_data_set(EXIM_DATUM * dp, void * s)
+{ dp->dptr = s; }
+
+static inline unsigned
+exim_datum_size_get(EXIM_DATUM * dp)
+{ return dp->dsize; }
+static inline void
+exim_datum_size_set(EXIM_DATUM * dp, unsigned n)
+{ dp->dsize = n; }
+
+/* There's no clearing required before use, and we don't have to free anything
+after reading data. */
+
+static inline void
+exim_datum_init(EXIM_DATUM * d)
+{ }
+
+static inline void
+exim_datum_free(EXIM_DATUM * d)
+{ }
+
+/* size limit */
+
+# define EXIM_DB_RLIMIT 150
+
+#endif /* USE_GDBM */
+
+
+
+
+
+#if defined(COMPILE_UTILITY) || defined(MACRO_PREDEF)
+
+static inline EXIM_DB *
+exim_dbopen(const uschar * name, const uschar * dirname, int flags,
+ unsigned mode)
+{
+return exim_dbopen__(name, dirname, flags, mode);
+}
+
+static inline void
+exim_dbclose(EXIM_DB * dbp)
+{ exim_dbclose__(dbp); }
+
+#else /* exim mainline code */
+
+/* Wrappers for open/close with debug tracing */
+
+extern void debug_printf_indent(const char *, ...);
+static inline BOOL is_tainted(const void *);
+
+static inline EXIM_DB *
+exim_dbopen(const uschar * name, const uschar * dirname, int flags,
+ unsigned mode)
+{
+void * dbp;
+DEBUG(D_hints_lookup)
+ debug_printf_indent("EXIM_DBOPEN: file <%s> dir <%s> flags=%s\n",
+ name, dirname,
+ flags == O_RDONLY ? "O_RDONLY"
+ : flags == O_RDWR ? "O_RDWR"
+ : flags == (O_RDWR|O_CREAT) ? "O_RDWR|O_CREAT"
+ : "??");
+if (is_tainted(name) || is_tainted(dirname))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "Tainted name for DB file not permitted");
+ dbp = NULL;
+ }
+else
+ dbp = exim_dbopen__(name, dirname, flags, mode);
+
+DEBUG(D_hints_lookup) debug_printf_indent("returned from EXIM_DBOPEN: %p\n", dbp);
+return dbp;
+}
+
+static inline void
+exim_dbclose(EXIM_DB * dbp)
+{
+DEBUG(D_hints_lookup) debug_printf_indent("EXIM_DBCLOSE(%p)\n", dbp);
+exim_dbclose__(dbp);
+}
+
+# endif /* defined(COMPILE_UTILITY) || defined(MACRO_PREDEF) */
+
+/********************* End of dbm library definitions **********************/
+
+
+#endif /* whole file */
+/* End of hintsdb.h */
diff --git a/src/hintsdb_structs.h b/src/hintsdb_structs.h
new file mode 100644
index 0000000..e0670a1
--- /dev/null
+++ b/src/hintsdb_structs.h
@@ -0,0 +1,189 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 - 2021 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This header file contains 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. */
+
+#ifndef HINTSDB_STRUCTS_H
+#define HINTSDB_STRUCTS_H
+
+
+/* Structure for carrying around an open DBM file, and an open locking file
+that relates to it. */
+
+typedef struct {
+ void * 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 distinguished 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;
+
+
+/* For "seen" ACL condition */
+typedef struct {
+ time_t time_stamp;
+} dbdata_seen;
+
+#ifndef DISABLE_PIPE_CONNECT
+/* This structure records the EHLO responses, cleartext and crypted,
+for an IP, as bitmasks (cf. OPTION_TLS). For LIMITS, also values
+advertised for MAILMAX, RCPTMAX and RCPTDOMAINMAX; zero meaning no
+value advertised. */
+
+typedef struct {
+ unsigned short cleartext_features;
+ unsigned short crypted_features;
+ unsigned short cleartext_auths;
+ unsigned short crypted_auths;
+
+# ifdef EXPERIMENTAL_ESMTP_LIMITS
+ unsigned int limit_mail;
+ unsigned int limit_rcpt;
+ unsigned int limit_rcptdom;
+# endif
+} ehlo_resp_precis;
+
+typedef struct {
+ time_t time_stamp;
+ /*************/
+ ehlo_resp_precis data;
+} dbdata_ehlo_resp;
+#endif
+
+typedef struct {
+ time_t time_stamp;
+ /*************/
+ uschar verify_override:1;
+ uschar ocsp:3;
+ uschar session[1];
+} dbdata_tls_session;
+
+
+#endif /* whole file */
+/* End of hintsdb_structs.h */
diff --git a/src/host.c b/src/host.c
new file mode 100644
index 0000000..e43b507
--- /dev/null
+++ b/src/host.c
@@ -0,0 +1,3431 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 (f.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;
+}
+
+/*************************************************
+* Wrappers for logging lookup times *
+*************************************************/
+
+/* When the 'slow_lookup_log' variable is enabled, these wrappers will
+write to the log file all (potential) dns lookups that take more than
+slow_lookup_log milliseconds
+*/
+
+static void
+log_long_lookup(const uschar * type, const uschar * data, unsigned long msec)
+{
+log_write(0, LOG_MAIN, "Long %s lookup for '%s': %lu msec",
+ type, data, msec);
+}
+
+
+/* returns the current system epoch time in milliseconds. */
+static unsigned long
+get_time_in_ms()
+{
+struct timeval tmp_time;
+unsigned long seconds, microseconds;
+
+gettimeofday(&tmp_time, NULL);
+seconds = (unsigned long) tmp_time.tv_sec;
+microseconds = (unsigned long) tmp_time.tv_usec;
+return seconds*1000 + microseconds/1000;
+}
+
+
+static int
+dns_lookup_timerwrap(dns_answer *dnsa, const uschar *name, int type,
+ const uschar **fully_qualified_name)
+{
+int retval;
+unsigned long time_msec;
+
+if (!slow_lookup_log)
+ return dns_lookup(dnsa, name, type, fully_qualified_name);
+
+time_msec = get_time_in_ms();
+retval = dns_lookup(dnsa, name, type, fully_qualified_name);
+if ((time_msec = get_time_in_ms() - time_msec) > slow_lookup_log)
+ log_long_lookup(dns_text_type(type), name, time_msec);
+return retval;
+}
+
+
+/*************************************************
+* Replace gethostbyname() when testing *
+*************************************************/
+
+/* This function is called instead of gethostbyname(), gethostbyname2(), or
+getipnodebyname() when running in the test harness. . 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(const 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;
+const uschar *lname = name;
+uschar *adds;
+uschar **alist;
+struct hostent *yield;
+dns_answer * dnsa = store_get_dns_answer();
+dns_scan dnss;
+
+DEBUG(D_host_lookup)
+ debug_printf("using host_fake_gethostbyname for %s (%s)\n", name,
+ af == AF_INET ? "IPv4" : "IPv6");
+
+/* Handle unqualified "localhost" */
+
+if (Ustrcmp(name, "localhost") == 0)
+ lname = af == AF_INET ? US"127.0.0.1" : US"::1";
+
+/* Handle a literal IP address */
+
+if ((ipa = string_is_ip_address(lname, NULL)) != 0)
+ if ( ipa == 4 && af == AF_INET
+ || ipa == 6 && af == AF_INET6)
+ {
+ int x[4];
+ yield = store_get(sizeof(struct hostent), GET_UNTAINTED);
+ alist = store_get(2 * sizeof(char *), GET_UNTAINTED);
+ adds = store_get(alen, GET_UNTAINTED);
+ 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;
+ for (int n = host_aton(lname, x), 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;
+ yield = NULL;
+ goto out;
+ }
+
+/* Handle a host name */
+
+else
+ {
+ int type = af == AF_INET ? T_A:T_AAAA;
+ int rc = dns_lookup_timerwrap(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; yield = NULL; goto out;
+ case DNS_NODATA: *error_num = NO_DATA; yield = NULL; goto out;
+ case DNS_AGAIN: *error_num = TRY_AGAIN; yield = NULL; goto out;
+ default:
+ case DNS_FAIL: *error_num = NO_RECOVERY; yield = NULL; goto out;
+ }
+
+ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS);
+ rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)) if (rr->type == type)
+ count++;
+
+ yield = store_get(sizeof(struct hostent), GET_UNTAINTED);
+ alist = store_get((count + 1) * sizeof(char *), GET_UNTAINTED);
+ adds = store_get(count *alen, GET_UNTAINTED);
+
+ yield->h_name = CS name;
+ yield->h_aliases = NULL;
+ yield->h_addrtype = af;
+ yield->h_length = alen;
+ yield->h_addr_list = CSS alist;
+
+ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS);
+ rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)) if (rr->type == type)
+ {
+ int x[4];
+ dns_address *da;
+ if (!(da = dns_address_from_rr(dnsa, rr))) break;
+ *alist++ = adds;
+ for (int n = host_aton(da->address, x), 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;
+ }
+
+out:
+
+store_free_dns_answer(dnsa);
+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, const uschar *list, BOOL randomize)
+{
+int sep = 0;
+int fake_mx = MX_NONE; /* This value is actually -1 */
+uschar *name;
+
+if (!list) return;
+if (randomize) fake_mx--; /* Start at -2 for randomizing */
+
+*anchor = NULL;
+
+while ((name = string_nextinlist(&list, &sep, NULL, 0)))
+ {
+ 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), GET_UNTAINTED);
+ h->name = 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)
+ {
+ 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 && h->sort_key >= hh->next->sort_key)
+ hh = hh->next;
+ h->next = hh->next;
+ hh->next = h;
+ }
+ }
+ }
+}
+
+
+
+
+
+/*************************************************
+* Extract port from address string *
+*************************************************/
+
+/* 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 uses for output, because it seems to becoming commonly used,
+whereas the dot form confuses some programs/people. So we recognize that form
+too.
+
+The spool file used to use the first form, but this breaks with a v4mapped ipv6
+hybrid, because the parsing here is not clever. So for spool we now use the
+second form.
+
+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)
+{
+const 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. However, STARTTLS has to be forgotten and redone
+on a multi-message conn, so this will be called once per message then. Hence
+we use malloc, so we can free.
+
+Arguments: none
+Returns: nothing
+*/
+
+void
+host_build_sender_fullhost(void)
+{
+BOOL show_helo = TRUE;
+uschar * address, * fullhost, * rcvhost;
+rmark reset_point;
+int len;
+
+if (!sender_host_address) return;
+
+reset_point = store_mark();
+
+/* 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 (!LOGGING(incoming_port) || sender_host_port <= 0)
+ *(Ustrrchr(address, ':')) = 0;
+
+/* If there's no EHLO/HELO data, we can't show it. */
+
+if (!sender_helo_name) 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 canonicalize 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)
+ {
+ uschar *portptr = Ustrstr(address, "]:");
+ gstring * g;
+ int adlen; /* Sun compiler doesn't like ++ in initializers */
+
+ adlen = portptr ? (++portptr - address) : Ustrlen(address);
+ fullhost = sender_helo_name
+ ? string_sprintf("(%s) %s", sender_helo_name, address)
+ : address;
+
+ g = string_catn(NULL, address, adlen);
+
+ if (sender_ident || show_helo || portptr)
+ {
+ int firstptr;
+ g = string_catn(g, US" (", 2);
+ firstptr = g->ptr;
+
+ if (portptr)
+ g = string_append(g, 2, US"port=", portptr + 1);
+
+ if (show_helo)
+ g = string_append(g, 2,
+ firstptr == g->ptr ? US"helo=" : US" helo=", sender_helo_name);
+
+ if (sender_ident)
+ g = string_append(g, 2,
+ firstptr == g->ptr ? US"ident=" : US" ident=", sender_ident);
+
+ g = string_catn(g, US")", 1);
+ }
+
+ rcvhost = string_from_gstring(g);
+ }
+
+/* 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)
+ {
+ fullhost = string_sprintf("%s (%s) %s", sender_host_name,
+ sender_helo_name, address);
+ rcvhost = sender_ident
+ ? string_sprintf("%s\n\t(%s helo=%s ident=%s)", sender_host_name,
+ address, sender_helo_name, sender_ident)
+ : string_sprintf("%s (%s helo=%s)", sender_host_name,
+ address, sender_helo_name);
+ }
+ else
+ {
+ fullhost = string_sprintf("%s %s", sender_host_name, address);
+ rcvhost = sender_ident
+ ? string_sprintf("%s (%s ident=%s)", sender_host_name, address,
+ sender_ident)
+ : string_sprintf("%s (%s)", sender_host_name, address);
+ }
+ }
+
+sender_fullhost = string_copy_perm(fullhost, TRUE);
+sender_rcvhost = string_copy_perm(rcvhost, TRUE);
+
+store_reset(reset_point);
+
+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
+
+Use taint-unchecked routines on the assumption we'll never expand the results.
+
+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)
+ string_format_nt(big_buffer, big_buffer_size, "%s%s", useflag ? "U=" : "",
+ sender_ident ? sender_ident : US"unknown");
+else
+ {
+ uschar * flag = useflag ? US"H=" : US"";
+ uschar * iface = US"";
+ if (LOGGING(incoming_interface) && interface_address)
+ iface = string_sprintf(" I=[%s]:%d", interface_address, interface_port);
+ if (sender_ident)
+ string_format_nt(big_buffer, big_buffer_size, "%s%s%s U=%s",
+ flag, sender_fullhost, iface, sender_ident);
+ else
+ string_format_nt(big_buffer, big_buffer_size, "%s%s%s",
+ flag, sender_fullhost, iface);
+ }
+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(const uschar *list, uschar *name)
+{
+int sep = 0;
+uschar *s;
+ip_address_item * yield = NULL, * last = NULL, * next;
+
+while ((s = string_nextinlist(&list, &sep, NULL, 0)))
+ {
+ int ipv;
+ int port = host_address_extract_port(s); /* Leaves just the IP address */
+
+ if (!(ipv = string_is_ip_address(s, NULL)))
+ 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), list);
+ next->next = NULL;
+ Ustrcpy(next->address, s);
+ next->port = port;
+ next->v6_include_v4 = FALSE;
+ next->log = NULL;
+
+ if (!yield)
+ 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; ipa2 = ipa2->next)
+ if (Ustrcmp(ipa2->address, ipa->address) == 0) return list;
+ipa2 = store_get_perm(sizeof(ip_address_item), FALSE);
+*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)
+ {
+ void *reset_item = store_mark();
+ ip_address_item *dlist = host_build_ifacelist(CUS local_interfaces,
+ US"local_interfaces");
+ ip_address_item *xlist = host_build_ifacelist(CUS extra_local_interfaces,
+ US"extra_local_interfaces");
+ ip_address_item *ipa;
+
+ if (!dlist) dlist = xlist;
+ else
+ {
+ for (ipa = dlist; ipa->next; ipa = ipa->next) ;
+ ipa->next = xlist;
+ }
+
+ for (ipa = dlist; ipa; ipa = ipa->next)
+ {
+ if (Ustrcmp(ipa->address, "0.0.0.0") == 0 ||
+ Ustrcmp(ipa->address, "::0") == 0)
+ {
+ BOOL ipv6 = ipa->address[0] == ':';
+ if (!running_interfaces)
+ running_interfaces = os_find_running_interfaces();
+ for (ip_address_item * ipa2 = running_interfaces; ipa2; 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 = US inet_ntop(family, &(sk->sin6_addr), CS addr_buffer,
+ sizeof(addr_buffer));
+ if (portptr) *portptr = ntohs(sk->sin6_port);
+ }
+ else
+ {
+ struct sockaddr_in *sk = (struct sockaddr_in *)arg;
+ yield = US inet_ntop(family, &(sk->sin_addr), CS addr_buffer,
+ sizeof(addr_buffer));
+ if (portptr) *portptr = ntohs(sk->sin_port);
+ }
+ }
+else
+ {
+ yield = US 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) *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) buffer = store_get(46, GET_UNTAINTED);
+
+/* 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.
+If the library returned apparently an apparently tainted string, clean it;
+we trust IP addresses. */
+
+string_format_nt(buffer, 46, "%s", 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(const 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)
+ {
+ const uschar *p = address;
+ const 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] = ((uint)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)
+{
+if (mask < 0) mask = 99999;
+for (int i = 0; i < count; i++)
+ {
+ int wordmask;
+ if (mask == 0) wordmask = 0;
+ else if (mask < 32)
+ {
+ wordmask = (uint)(-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 j;
+uschar *tt = buffer;
+
+if (count == 1)
+ {
+ j = binary[0];
+ for (int i = 24; i >= 0; i -= 8)
+ tt += sprintf(CS tt, "%d.", (j >> i) & 255);
+ }
+else
+ for (int i = 0; i < 4; i++)
+ {
+ j = binary[i];
+ tt += sprintf(CS tt, "%04x%c%04x%c", (j >> 16) & 0xffff, sep, j & 0xffff, sep);
+ }
+
+tt--; /* lose final separator */
+
+if (mask < 0)
+ *tt = 0;
+else
+ tt += sprintf(CS tt, "/%d", mask);
+
+return tt - buffer;
+}
+
+
+/* Like host_nmtoa() but: ipv6-only, canonical output, no mask
+
+Arguments:
+ binary points to the ints
+ buffer big enough to hold the result
+
+Returns: the number of characters placed in buffer, not counting
+ the final nul.
+*/
+
+int
+ipv6_nmtoa(int * binary, uschar * buffer)
+{
+int i, j, k;
+uschar * c = buffer;
+uschar * d = NULL; /* shut insufficiently "clever" compiler up */
+
+for (i = 0; i < 4; i++)
+ { /* expand to text */
+ j = binary[i];
+ c += sprintf(CS c, "%x:%x:", (j >> 16) & 0xffff, j & 0xffff);
+ }
+
+for (c = buffer, k = -1, i = 0; i < 8; i++)
+ { /* find longest 0-group sequence */
+ if (*c == '0') /* must be "0:" */
+ {
+ uschar * s = c;
+ j = i;
+ while (c[2] == '0') i++, c += 2;
+ if (i-j > k)
+ {
+ k = i-j; /* length of sequence */
+ d = s; /* start of sequence */
+ }
+ }
+ while (*++c != ':') ;
+ c++;
+ }
+
+*--c = '\0'; /* drop trailing colon */
+
+/* debug_printf("%s: D k %d <%s> <%s>\n", __FUNCTION__, k, buffer, buffer + 2*(k+1)); */
+if (k >= 0)
+ { /* collapse */
+ c = d + 2*(k+1);
+ if (d == buffer) c--; /* need extra colon */
+ *d++ = ':'; /* 1st 0 */
+ while ((*d++ = *c++)) ;
+ }
+else
+ d = c;
+
+return d - 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;
+const uschar * list = tls_in.on_connect_ports;
+
+if (tls_in.on_connect) return TRUE;
+
+for (uschar * s, * end; s = string_nextinlist(&list, &sep, NULL, 0); )
+ 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(const uschar *host, const uschar *net, int maskoffset)
+{
+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 (int i = 0; i < size; i++)
+ {
+ int mask;
+ if (mlen == 0) mask = 0;
+ else if (mlen < 32)
+ {
+ mask = (uint)(-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;
+ const uschar *save = deliver_domain;
+ deliver_domain = h->name; /* set $domain */
+ rc = match_isinlist(string_copylc(h->name), CUSS &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)
+ {
+ if (Ustrcmp(h->address, "0.0.0.0") == 0) goto FOUND_LOCAL;
+ for (ip_address_item * ip = local_interface_data; ip; 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)
+{
+struct hostent * hosts;
+struct in_addr addr;
+unsigned long time_msec = 0; /* init to quieten dumb static analysis */
+
+if (slow_lookup_log) time_msec = get_time_in_ms();
+
+/* 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
+
+if ( slow_lookup_log
+ && (time_msec = get_time_in_ms() - time_msec) > slow_lookup_log
+ )
+ log_long_lookup(US"gethostbyaddr", sender_host_address, time_msec);
+
+/* Failed to look up the host. */
+
+if (!hosts)
+ {
+ 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 || !hosts->h_name[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. */
+
+ {
+ int old_pool = store_pool;
+ store_pool = POOL_TAINT_PERM; /* names are tainted */
+
+ sender_host_name = string_copylc(US hosts->h_name);
+
+ /* If the host has aliases, build a copy of the alias list */
+
+ if (hosts->h_aliases)
+ {
+ int count = 1; /* need 1 more for terminating NULL */
+ uschar **ptr;
+
+ for (uschar ** aliases = USS hosts->h_aliases; *aliases; aliases++) count++;
+ store_pool = POOL_PERM;
+ ptr = sender_host_aliases = store_get(count * sizeof(uschar *), GET_UNTAINTED);
+ store_pool = POOL_TAINT_PERM;
+
+ for (uschar ** aliases = USS hosts->h_aliases; *aliases; aliases++)
+ *ptr++ = string_copylc(*aliases);
+ *ptr = NULL;
+ }
+ store_pool = old_pool;
+ }
+
+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 success, 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 *save_hostname;
+uschar **aliases;
+uschar *ordername;
+const uschar *list = host_lookup_order;
+dns_answer * dnsa = store_get_dns_answer();
+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 (f.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, NULL, 0)))
+ {
+ if (strcmpic(ordername, US"bydns") == 0)
+ {
+ uschar * name = dns_build_reverse(sender_host_address);
+
+ dns_init(FALSE, FALSE, FALSE); /* dnssec ctrl by dns_dnssec_ok glbl */
+ rc = dns_lookup_timerwrap(dnsa, name, 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 = 1; /* need 1 more for terminating NULL */
+ int old_pool = store_pool;
+
+ 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 (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS);
+ rr;
+ 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 *), GET_UNTAINTED);
+
+ /* Re-scan and extract the names */
+
+ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS);
+ rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)) if (rr->type == T_PTR)
+ {
+ uschar * s = store_get(ssize, GET_TAINTED); /* names are tainted */
+
+ /* 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,
+ US (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_release_above(s + Ustrlen(s) + 1);
+ if (!s[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) sender_host_name = s;
+ else *aptr++ = s;
+ while (*s) { *s = tolower(*s); s++; }
+ }
+
+ *aptr = NULL; /* End of alias list */
+ store_pool = old_pool; /* Reset store pool */
+
+ /* If we've found a name, break out of the "order" loop */
+
+ if (sender_host_name) 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)
+ {
+ if (host_checking || !f.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) 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 (uschar * hname = sender_host_name; hname; hname = *aliases++)
+ {
+ int rc;
+ BOOL ok = FALSE;
+ host_item h = { .next = NULL, .name = hname, .mx = MX_NONE, .address = NULL };
+ dnssec_domains d =
+ { .request = sender_host_dnssec ? US"*" : NULL, .require = NULL };
+
+ if ( (rc = host_find_bydns(&h, NULL, HOST_FIND_BY_A | HOST_FIND_BY_AAAA,
+ NULL, NULL, NULL, &d, NULL, NULL)) == HOST_FOUND
+ || rc == HOST_FOUND_LOCAL
+ )
+ {
+ HDEBUG(D_host_lookup) debug_printf("checking addresses for %s\n", hname);
+
+ /* If the forward lookup was not secure we cancel the is-secure variable */
+
+ DEBUG(D_dns) debug_printf("Forward DNS security status: %s\n",
+ h.dnssec == DS_YES ? "DNSSEC verified (AD)" : "unverified");
+ if (h.dnssec != DS_YES) sender_host_dnssec = FALSE;
+
+ for (host_item * hh = &h; hh; 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, const uschar *ignore_target_hosts, int flags,
+ const uschar **fully_qualified_name, BOOL local_host_check)
+{
+int yield, times;
+host_item *last = NULL;
+BOOL temp_error = FALSE;
+int af;
+
+#ifndef DISABLE_TLS
+/* Copy the host name at this point to the value which is used for
+TLS certificate name checking, before anything modifies it. */
+
+host->certname = host->name;
+#endif
+
+/* 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); /* Cannot retrieve dnssec status so do not request */
+
+/* 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
+ && match_isinlist(host->name, CUSS &dns_ipv4_lookup, 0,
+ &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL) == OK)
+ #endif
+
+ { af = AF_INET; times = 1; }
+ else
+ { af = AF_INET6; times = 2; }
+
+/* No IPv6 support */
+
+#else /* HAVE_IPV6 */
+ af = AF_INET; 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. */
+
+f.host_find_failed_syntax = FALSE;
+
+/* Loop to look up both kinds of address in an IPv6 world */
+
+for (int 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;
+ unsigned long time_msec = 0; /* compiler quietening */
+
+ #ifdef STAND_ALONE
+ printf("Looking up: %s\n", host->name);
+ #endif
+
+ if (slow_lookup_log) time_msec = get_time_in_ms();
+
+ #if HAVE_IPV6
+ if (f.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 (f.running_in_test_harness)
+ hostdata = host_fake_gethostbyname(host->name, af, &error_num);
+ else
+ {
+ hostdata = gethostbyname(CS host->name);
+ error_num = h_errno;
+ }
+ #endif /* HAVE_IPV6 */
+
+ if ( slow_lookup_log
+ && (time_msec = get_time_in_ms() - time_msec) > slow_lookup_log)
+ log_long_lookup(US"gethostbyname", host->name, time_msec);
+
+ if (!hostdata)
+ {
+ uschar * error;
+ switch (error_num)
+ {
+ case HOST_NOT_FOUND: error = US"HOST_NOT_FOUND"; break;
+ case TRY_AGAIN: error = US"TRY_AGAIN"; temp_error = TRUE; break;
+ case NO_RECOVERY: error = US"NO_RECOVERY"; temp_error = TRUE; 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(af=%s) returned %d (%s)\n",
+ f.running_in_test_harness ? "host_fake_gethostbyname" :
+#if HAVE_IPV6
+# if HAVE_GETIPNODEBYNAME
+ "getipnodebyname",
+# else
+ "gethostbyname2",
+# endif
+#else
+ "gethostbyname",
+#endif
+ af == AF_INET ? "inet" : "inet6", error_num, error);
+
+ continue;
+ }
+ if (!(hostdata->h_addr_list)[0]) continue;
+
+ /* Replace the name with the fully qualified one if necessary, and fill in
+ the fully_qualified_name pointer. */
+
+ if (hostdata->h_name[0] && Ustrcmp(host->name, hostdata->h_name) != 0)
+ host->name = string_copy_dnsdomain(US hostdata->h_name);
+ if (fully_qualified_name) *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 (uschar ** addrlist = USS hostdata->h_addr_list; *addrlist; addrlist++)
+ {
+ uschar *text_address =
+ host_ntoa(ipv4_addr? AF_INET:AF_INET6, *addrlist, NULL, NULL);
+
+ #ifndef STAND_ALONE
+ if ( ignore_target_hosts
+ && 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 is NULL and we put the data in the
+ original block. */
+
+ if (!last)
+ {
+ 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), GET_UNTAINTED);
+ next->name = host->name;
+#ifndef DISABLE_TLS
+ next->certname = host->certname;
+#endif
+ 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)
+ {
+ uschar *msg =
+ #ifndef STAND_ALONE
+ !message_id[0] && smtp_in
+ ? 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 || !f.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)
+ {
+ if (fully_qualified_name)
+ 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 (const host_item * h = host; h != last->next; h = h->next)
+ debug_printf(" name=%s address=%s\n", h->name,
+ h->address ? h->address : US"<null>");
+ }
+
+/* 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;
+ const uschar *save = deliver_domain;
+ deliver_domain = host->name; /* set $domain */
+ rc = match_isinlist(host->name, CUSS &dns_again_means_nonexist, 0,
+ &domainlist_anchor, 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, AAAA records are sought first, then A records.
+
+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)
+ dnssec_request if TRUE request the AD bit
+ dnssec_require if TRUE require the AD bit
+ whichrrs select ipv4, ipv6 results
+
+Returns: HOST_FIND_FAILED couldn't find A record
+ HOST_FIND_AGAIN try again later
+ HOST_FIND_SECURITY dnssec required but not acheived
+ 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,
+ const uschar *ignore_target_hosts, BOOL allow_ip,
+ const uschar **fully_qualified_name,
+ BOOL dnssec_request, BOOL dnssec_require, int whichrrs)
+{
+host_item *thishostlast = NULL; /* Indicates not yet filled in anything */
+BOOL v6_find_again = FALSE;
+BOOL dnssec_fail = FALSE;
+int i;
+dns_answer * dnsa;
+
+#ifndef DISABLE_TLS
+/* Copy the host name at this point to the value which is used for
+TLS certificate name checking, before any CNAME-following modifies it. */
+
+host->certname = host->name;
+#endif
+
+/* 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
+ && 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;
+ }
+
+dnsa = store_get_dns_answer();
+
+/* On an IPv6 system, unless IPv6 is disabled, go round the loop up to twice,
+looking for AAAA records the first time. However, unless doing standalone
+testing, we force an IPv4 lookup if the domain matches dns_ipv4_lookup global.
+On an IPv4 system, go round the loop once only, looking only for A records. */
+
+#if HAVE_IPV6
+ #ifndef STAND_ALONE
+ if ( disable_ipv6
+ || !(whichrrs & HOST_FIND_BY_AAAA)
+ || dns_ipv4_lookup
+ && match_isinlist(host->name, CUSS &dns_ipv4_lookup, 0,
+ &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL) == OK
+ )
+ i = 0; /* look up A records only */
+ else
+ #endif /* STAND_ALONE */
+
+ i = 1; /* look up AAAA and A records */
+
+/* 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 };
+ int type = types[i];
+ int randoffset = i == (whichrrs & HOST_FIND_IPV4_FIRST ? 1 : 0)
+ ? 500 : 0; /* Ensures v6/4 sort order */
+ dns_scan dnss;
+
+ int rc = dns_lookup_timerwrap(dnsa, host->name, type, fully_qualified_name);
+ lookup_dnssec_authenticated = !dnssec_request ? NULL
+ : dns_is_secure(dnsa) ? US"yes" : US"no";
+
+ DEBUG(D_dns)
+ if ( (dnssec_request || dnssec_require)
+ && !dns_is_secure(dnsa)
+ && dns_is_aa(dnsa)
+ )
+ debug_printf("DNS lookup of %.256s (A/AAAA) requested AD, but got AA\n", host->name);
+
+ /* We want to return HOST_FIND_AGAIN if one of the A 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)
+ i = HOST_FOUND; /* AAAA was found */
+ else if (rc == DNS_AGAIN || rc == DNS_FAIL || v6_find_again)
+ i = HOST_FIND_AGAIN;
+ else
+ i = HOST_FIND_FAILED; /* DNS_NOMATCH or DNS_NODATA */
+ goto out;
+ }
+
+ /* Tried for an 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_request)
+ {
+ if (dns_is_secure(dnsa))
+ {
+ DEBUG(D_host_lookup) debug_printf("%s A DNSSEC\n", host->name);
+ if (host->dnssec == DS_UNK) /* set in host_find_bydns() */
+ host->dnssec = DS_YES;
+ }
+ else
+ {
+ if (dnssec_require)
+ {
+ dnssec_fail = TRUE;
+ DEBUG(D_host_lookup) debug_printf("dnssec fail on %s for %.256s",
+ i>0 ? "AAAA" : "A", host->name);
+ continue;
+ }
+ if (host->dnssec == DS_YES) /* set in host_find_bydns() */
+ {
+ DEBUG(D_host_lookup) debug_printf("%s A cancel DNSSEC\n", host->name);
+ host->dnssec = DS_NO;
+ lookup_dnssec_authenticated = US"no";
+ }
+ }
+ }
+
+ /* 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. The lookup had a chance to update the
+ fqdn; we do not want any later times round the loop to do so. */
+
+ fully_qualified_name = NULL;
+
+ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS);
+ rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)) if (rr->type == type)
+ {
+ dns_address * da = dns_address_from_rr(dnsa, rr);
+
+ DEBUG(D_host_lookup)
+ if (!da) 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; 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), GET_UNTAINTED);
+
+ /* 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 second lookup (the A record) succeeded.
+However, the address may not be filled in if it was ignored. */
+
+i = host->address
+ ? HOST_FOUND
+ : dnssec_fail
+ ? HOST_FIND_SECURITY
+ : HOST_IGNORED;
+
+out:
+ store_free_dns_answer(dnsa);
+ return i;
+}
+
+
+
+
+/*************************************************
+* 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
+ HOST_FIND_BY_AAAA => look for AAAA
+ also flags indicating how the lookup is done
+ HOST_FIND_QUALIFY_SINGLE ) passed to the
+ HOST_FIND_SEARCH_PARENTS ) resolver
+ HOST_FIND_IPV4_FIRST => reverse usual result ordering
+ HOST_FIND_IPV4_ONLY => MX results elide ipv6
+ 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_d.request => make dnssec request: domainlist
+ dnssec_d.require => 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_FIND_SECURITY dnsssec required but not acheived
+ 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, const uschar *ignore_target_hosts, int whichrrs,
+ uschar *srv_service, uschar *srv_fail_domains, uschar *mx_fail_domains,
+ const dnssec_domains *dnssec_d,
+ const uschar **fully_qualified_name, BOOL *removed)
+{
+host_item *h, *last;
+int rc = DNS_FAIL;
+int ind_type = 0;
+int yield;
+dns_answer * dnsa = store_get_dns_answer();
+dns_scan dnss;
+BOOL dnssec_require = dnssec_d
+ && match_isinlist(host->name, CUSS &dnssec_d->require,
+ 0, &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL) == OK;
+BOOL dnssec_request = dnssec_require
+ || ( dnssec_d
+ && match_isinlist(host->name, CUSS &dnssec_d->request,
+ 0, &domainlist_anchor, 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);
+f.host_find_failed_syntax = FALSE;
+
+/* First, if requested, look for SRV records. The service name is given; we
+assume TCP protocol. DNS domain names are constrained to a maximum of 256
+characters, so the code below should be safe. */
+
+if (whichrrs & HOST_FIND_BY_SRV)
+ {
+ gstring * g;
+ uschar * temp_fully_qualified_name;
+ int prefix_length;
+
+ g = string_fmt_append(NULL, "_%s._tcp.%n%.256s",
+ srv_service, &prefix_length, host->name);
+ temp_fully_qualified_name = string_from_gstring(g);
+ 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_timerwrap(dnsa, temp_fully_qualified_name, ind_type,
+ CUSS &temp_fully_qualified_name);
+
+ DEBUG(D_dns)
+ if ((dnssec_request || dnssec_require)
+ && !dns_is_secure(dnsa)
+ && dns_is_aa(dnsa))
+ debug_printf("DNS lookup of %.256s (SRV) requested AD, but got AA\n", host->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 != g->s && 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, CUSS &srv_fail_domains, 0,
+ &domainlist_anchor, 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)
+ {
+ ind_type = T_MX;
+ dnssec = DS_UNK;
+ lookup_dnssec_authenticated = NULL;
+ rc = dns_lookup_timerwrap(dnsa, host->name, ind_type, fully_qualified_name);
+
+ DEBUG(D_dns)
+ if ( (dnssec_request || dnssec_require)
+ && !dns_is_secure(dnsa)
+ && dns_is_aa(dnsa))
+ debug_printf("DNS lookup of %.256s (MX) requested AD, but got AA\n", host->name);
+
+ if (dnssec_request)
+ if (dns_is_secure(dnsa))
+ {
+ DEBUG(D_host_lookup) debug_printf("%s (MX resp) DNSSEC\n", host->name);
+ 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;
+ DEBUG(D_host_lookup)
+ debug_printf("dnssec fail on MX for %.256s", host->name);
+#ifndef STAND_ALONE
+ if (match_isinlist(host->name, CUSS &mx_fail_domains, 0,
+ &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL) != OK)
+ { yield = HOST_FIND_SECURITY; goto out; }
+#endif
+ rc = DNS_FAIL;
+ /*FALLTHROUGH*/
+
+ case DNS_FAIL:
+ case DNS_AGAIN:
+#ifndef STAND_ALONE
+ if (match_isinlist(host->name, CUSS &mx_fail_domains, 0,
+ &domainlist_anchor, 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 | HOST_FIND_BY_AAAA)))
+ {
+ 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;
+ host->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, whichrrs);
+
+ /* 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)
+ if (host->address)
+ {
+ if (fully_qualified_name)
+ debug_printf("fully qualified name = %s\n", *fully_qualified_name);
+ for (host_item * h = host; h != last->next; h = h->next)
+ debug_printf("%s %s mx=%d sort=%d %s\n", h->name,
+ h->address ? h->address : US"<null>", 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 (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS);
+ rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)) if (rr->type == ind_type)
+ {
+ int precedence, weight;
+ int port = PORT_NONE;
+ const uschar * s = rr->data; /* MUST be unsigned for GETSHORT */
+ uschar data[256];
+
+ 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);
+ else
+ {
+ /* 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). */
+ 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) /* 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)
+ {
+ 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;
+ }
+ else
+
+ /* Make a new host item and seek the correct insertion place */
+ {
+ int sort_key = precedence * 1000 + weight;
+ host_item * next = store_get(sizeof(host_item), GET_UNTAINTED);
+ 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
+
+ /* Else scan down the items we have inserted as part of this exercise;
+ don't go further. */
+ {
+ 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 (!last) /* No rr of correct type; give up */
+ {
+ yield = HOST_FIND_FAILED;
+ goto out;
+ }
+
+/* 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) 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,
+ whichrrs & HOST_FIND_IPV4_ONLY
+ ? HOST_FIND_BY_A : HOST_FIND_BY_A | HOST_FIND_BY_AAAA);
+ if (rc != HOST_FOUND)
+ {
+ h->status = hstatus_unusable;
+ switch (rc)
+ {
+ case HOST_FIND_AGAIN: yield = rc; h->why = hwhy_deferred; break;
+ case HOST_FIND_SECURITY: yield = rc; h->why = hwhy_insecure; break;
+ case HOST_IGNORED: h->why = hwhy_ignored; break;
+ default: h->why = hwhy_failed; break;
+ }
+ }
+ }
+
+/* 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)
+ {
+ 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 /* OR this one is unset */
+ )
+ continue; /* move on to next */
+
+ if ( whichrrs & HOST_FIND_IPV4_FIRST
+ ? !Ustrchr(h->address, ':') /* OR this one is IPv4 */
+ || next->address
+ && Ustrchr(next->address, ':') /* OR next is IPv6 */
+
+ : Ustrchr(h->address, ':') /* OR this one is IPv6 */
+ || next->address
+ && !Ustrchr(next->address, ':') /* 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)
+ 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_SECURITY ? "HOST_FIND_SECURITY" :
+ 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 %s", h->name,
+ !h->address ? US"<null>" : h->address, h->mx,
+ h->dnssec == DS_YES ? US"DNSSEC " : US"");
+ 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 */
+store_free_dns_answer(dnsa);
+return yield;
+}
+
+
+
+
+#ifdef SUPPORT_DANE
+/* Lookup TLSA record for host/port.
+Return: OK success with dnssec; DANE mode
+ DEFER Do not use this host now, may retry later
+ FAIL_FORCED No TLSA record; DANE not usable
+ FAIL Do not use this connection
+*/
+
+int
+tlsa_lookup(const host_item * host, dns_answer * dnsa, BOOL dane_required)
+{
+uschar buffer[300];
+const uschar * fullname = buffer;
+int rc;
+BOOL sec;
+
+/* TLSA lookup string */
+(void)sprintf(CS buffer, "_%d._tcp.%.256s", host->port, host->name);
+
+rc = dns_lookup_timerwrap(dnsa, buffer, T_TLSA, &fullname);
+sec = dns_is_secure(dnsa);
+DEBUG(D_transport)
+ debug_printf("TLSA lookup ret %s %sDNSSEC\n", dns_rc_names[rc], sec ? "" : "not ");
+
+switch (rc)
+ {
+ case DNS_AGAIN:
+ return DEFER; /* just defer this TLS'd conn */
+
+ case DNS_SUCCEED:
+ if (sec)
+ {
+ DEBUG(D_transport)
+ {
+ dns_scan dnss;
+ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT))
+ if (rr->type == T_TLSA && rr->size > 3)
+ {
+ uint16_t payload_length = rr->size - 3;
+ uschar s[MAX_TLSA_EXPANDED_SIZE], * sp = s, * p = US rr->data;
+
+ sp += sprintf(CS sp, "%d ", *p++); /* usage */
+ sp += sprintf(CS sp, "%d ", *p++); /* selector */
+ sp += sprintf(CS sp, "%d ", *p++); /* matchtype */
+ while (payload_length-- > 0 && sp-s < (MAX_TLSA_EXPANDED_SIZE - 4))
+ sp += sprintf(CS sp, "%02x", *p++);
+
+ debug_printf(" %s\n", s);
+ }
+ }
+ return OK;
+ }
+ log_write(0, LOG_MAIN,
+ "DANE error: TLSA lookup for %s not DNSSEC", host->name);
+ /*FALLTRHOUGH*/
+
+ case DNS_NODATA: /* no TLSA RR for this lookup */
+ case DNS_NOMATCH: /* no records at all for this lookup */
+ return dane_required ? FAIL : FAIL_FORCED;
+
+ default:
+ case DNS_FAIL:
+ return dane_required ? FAIL : DEFER;
+ }
+}
+#endif /*SUPPORT_DANE*/
+
+
+
+/*************************************************
+**************************************************
+* 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 | HOST_FIND_BY_AAAA;
+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_init();
+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 | HOST_FIND_BY_AAAA;
+ 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 | HOST_FIND_BY_AAAA;
+ 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 | HOST_FIND_BY_AAAA;
+ 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_require_dnssec") == 0) require_dnssec = FALSE;
+ else if (Ustrcmp(buffer, "test_harness") == 0)
+ f.running_in_test_harness = !f.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;
+ dnssec_domains d;
+
+ 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;
+
+ d.request = request_dnssec ? &h.name : NULL;
+ d.require = require_dnssec ? &h.name : NULL;
+
+ rc = byname
+ ? host_find_byname(&h, NULL, flags, &fully_qualified_name, TRUE)
+ : host_find_bydns(&h, NULL, flags, US"smtp", NULL, NULL,
+ &d, &fully_qualified_name, NULL);
+
+ switch (rc)
+ {
+ case HOST_FIND_FAILED: printf("Failed\n"); break;
+ case HOST_FIND_AGAIN: printf("Again\n"); break;
+ case HOST_FIND_SECURITY: printf("Security\n"); break;
+ case HOST_FOUND_LOCAL: printf("Local\n"); break;
+ }
+ }
+
+ printf("\n> ");
+ }
+
+printf("Testing host_aton\n");
+printf("> ");
+while (Ufgets(buffer, 256, stdin) != NULL)
+ {
+ 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 (int 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/imap_utf7.c b/src/imap_utf7.c
new file mode 100644
index 0000000..aac0fef
--- /dev/null
+++ b/src/imap_utf7.c
@@ -0,0 +1,211 @@
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "exim.h"
+
+#ifdef SUPPORT_I18N
+
+uschar *
+imap_utf7_encode(uschar *string, const uschar *charset, uschar sep,
+ uschar *specials, uschar **error)
+{
+static uschar encode_base64[64] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
+size_t slen;
+uschar *sptr;
+gstring * yield = NULL;
+int i = 0; /* compiler quietening */
+uschar c = 0; /* compiler quietening */
+BOOL base64mode = FALSE;
+BOOL lastsep = FALSE;
+uschar utf16buf[256];
+uschar *utf16ptr;
+uschar *s;
+uschar outbuf[256];
+uschar *outptr = outbuf;
+#if HAVE_ICONV
+iconv_t icd;
+#endif
+
+if (!specials) specials = US"";
+
+/* Pass over the string. If it consists entirely of "normal" characters
+ (possibly with leading seps), return it as is. */
+for (s = string; *s; s++)
+ {
+ if (s == string && *s == sep)
+ string++;
+ if ( *s >= 0x7f
+ || *s < 0x20
+ || strchr("./&", *s)
+ || *s == sep
+ || Ustrchr(specials, *s)
+ )
+ break;
+ }
+
+if (!*s)
+ return string;
+
+sptr = string;
+slen = Ustrlen(string);
+
+#if HAVE_ICONV
+if ((icd = iconv_open("UTF-16BE", CCS charset)) == (iconv_t)-1)
+ {
+ *error = string_sprintf(
+ "imapfolder: iconv_open(\"UTF-16BE\", \"%s\") failed: %s%s",
+ charset, strerror(errno),
+ errno == EINVAL ? " (maybe unsupported conversion)" : "");
+ return NULL;
+ }
+#endif
+
+while (slen > 0)
+ {
+#if HAVE_ICONV
+ size_t left = sizeof(utf16buf);
+ utf16ptr = utf16buf;
+
+ if ( iconv(icd, (ICONV_ARG2_TYPE)&sptr, &slen, CSS &utf16ptr, &left)
+ == (size_t)-1
+ && errno != E2BIG
+ )
+ {
+ *error = string_sprintf("imapfolder: iconv() failed to convert from %s: %s",
+ charset, strerror(errno));
+ iconv_close(icd);
+ return NULL;
+ }
+#else
+ for (utf16ptr = utf16buf;
+ slen > 0 && (utf16ptr - utf16buf) < sizeof(utf16buf);
+ utf16ptr += 2, slen--, sptr++)
+ {
+ *utf16ptr = *sptr;
+ *(utf16ptr+1) = '\0';
+ }
+#endif
+
+ s = utf16buf;
+ while (s < utf16ptr)
+ {
+ /* Now encode utf16buf as modified UTF-7 */
+ if ( s[0] != 0
+ || s[1] >= 0x7f
+ || s[1] < 0x20
+ || (Ustrchr(specials, s[1]) && s[1] != sep)
+ )
+ {
+ lastsep = FALSE;
+ /* Encode as modified BASE64 */
+ if (!base64mode)
+ {
+ *outptr++ = '&';
+ base64mode = TRUE;
+ i = 0;
+ }
+
+ for (int j = 0; j < 2; j++, s++) switch (i++)
+ {
+ case 0:
+ /* Top 6 bits of the first octet */
+ *outptr++ = encode_base64[(*s >> 2) & 0x3F];
+ c = (*s & 0x03); break;
+ case 1:
+ /* Bottom 2 bits of the first octet, and top 4 bits of the second */
+ *outptr++ = encode_base64[(c << 4) | ((*s >> 4) & 0x0F)];
+ c = (*s & 0x0F); break;
+ case 2:
+ /* Bottom 4 bits of the second octet and top 2 bits of the third */
+ *outptr++ = encode_base64[(c << 2) | ((*s >> 6) & 0x03)];
+ /* Bottom 6 bits of the third octet */
+ *outptr++ = encode_base64[*s & 0x3F];
+ i = 0;
+ }
+ }
+
+ else if ( (s[1] != '.' && s[1] != '/')
+ || s[1] == sep
+ )
+ {
+ /* Encode as self (almost) */
+ if (base64mode)
+ {
+ switch (i)
+ {
+ case 1:
+ /* Remaining bottom 2 bits of the last octet */
+ *outptr++ = encode_base64[c << 4];
+ break;
+ case 2:
+ /* Remaining bottom 4 bits of the last octet */
+ *outptr++ = encode_base64[c << 2];
+ }
+ *outptr++ = '-';
+ base64mode = FALSE;
+ }
+
+ if (*++s == sep)
+ {
+ if (!lastsep)
+ {
+ *outptr++ = '.';
+ lastsep = TRUE;
+ }
+ }
+ else
+ {
+ *outptr++ = *s;
+ if (*s == '&')
+ *outptr++ = '-';
+ lastsep = FALSE;
+ }
+
+ s++;
+ }
+ else
+ {
+ *error = string_sprintf("imapfolder: illegal character '%c'", s[1]);
+ return NULL;
+ }
+
+ if (outptr > outbuf + sizeof(outbuf) - 3)
+ {
+ yield = string_catn(yield, outbuf, outptr - outbuf);
+ outptr = outbuf;
+ }
+
+ }
+ } /* End of input string */
+
+if (base64mode)
+ {
+ switch (i)
+ {
+ case 1:
+ /* Remaining bottom 2 bits of the last octet */
+ *outptr++ = encode_base64[c << 4];
+ break;
+ case 2:
+ /* Remaining bottom 4 bits of the last octet */
+ *outptr++ = encode_base64[c << 2];
+ }
+ *outptr++ = '-';
+ }
+
+#if HAVE_ICONV
+iconv_close(icd);
+#endif
+
+yield = string_catn(yield, outbuf, outptr - outbuf);
+
+if (yield->s[yield->ptr-1] == '.')
+ yield->ptr--;
+
+return string_from_gstring(yield);
+}
+
+#endif /* whole file */
+/* vi: aw ai sw=2
+*/
diff --git a/src/ip.c b/src/ip.c
new file mode 100644
index 0000000..aa42343
--- /dev/null
+++ b/src/ip.c
@@ -0,0 +1,853 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 - 2021 */
+/* 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, especially as there are now several
+different places in the code where sockets are used. */
+
+
+#include "exim.h"
+
+
+#if defined(TCP_FASTOPEN)
+# if defined(MSG_FASTOPEN) || defined(EXIM_TFO_CONNECTX) || defined(EXIM_TFO_FREEBSD)
+# define EXIM_SUPPORT_TFO
+# endif
+#endif
+
+/*************************************************
+* 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(const uschar *address, struct sockaddr_in6 *saddr)
+{
+#ifdef IPV6_USE_INET_PTON
+
+ if (inet_pton(AF_INET6, CCS 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(CCS 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 *
+*************************************************/
+
+int
+ip_addr(void * sin_, int af, const uschar * address, int port)
+{
+union sockaddr_46 * sin = 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);
+ return sizeof(sin->v6);
+ }
+else
+#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);
+ sin->v4.sin_addr.s_addr = address[0] == 0
+ ? (S_ADDR_TYPE)INADDR_ANY
+ : (S_ADDR_TYPE)inet_addr(CS address);
+ return sizeof(sin->v4);
+ }
+}
+
+
+
+/* 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)
+{
+union sockaddr_46 sin;
+int s_len = ip_addr(&sin, af, address, port);
+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)
+ fastopen_blob non-null iff TCP_FASTOPEN can be used; may indicate early-data to
+ be sent in SYN segment. Any such data must be idempotent.
+
+Returns: 0 on success; -1 on failure, with errno set
+*/
+
+int
+ip_connect(int sock, int af, const uschar *address, int port, int timeout,
+ const blob * fastopen_blob)
+{
+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
+#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(CCS 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. */
+
+callout_address = string_sprintf("[%s]:%d", address, port);
+sigalrm_seen = FALSE;
+if (timeout > 0) ALARM(timeout);
+
+#ifdef EXIM_SUPPORT_TFO
+/* TCP Fast Open, if the system has a cookie from a previous call to
+this peer, can send data in the SYN packet. The peer can send data
+before it gets our ACK of its SYN,ACK - the latter is useful for
+the SMTP banner. Other (than SMTP) cases of TCP connections can
+possibly use the data-on-syn, so support that too. */
+
+if (fastopen_blob && f.tcp_fastopen_ok)
+ {
+# ifdef MSG_FASTOPEN
+ /* This is a Linux implementation. */
+
+ if ((rc = sendto(sock, fastopen_blob->data, fastopen_blob->len,
+ MSG_FASTOPEN | MSG_DONTWAIT, s_ptr, s_len)) >= 0)
+ /* seen for with-data, experimental TFO option, with-cookie case */
+ /* seen for with-data, proper TFO opt, with-cookie case */
+ {
+ DEBUG(D_transport|D_v)
+ debug_printf(" TFO mode connection attempt to %s, %lu data\n",
+ address, (unsigned long)fastopen_blob->len);
+ /*XXX also seen on successful TFO, sigh */
+ tcp_out_fastopen = fastopen_blob->len > 0 ? TFO_ATTEMPTED_DATA : TFO_ATTEMPTED_NODATA;
+ }
+ else switch (errno)
+ {
+ case EINPROGRESS: /* expected if we had no cookie for peer */
+ /* seen for no-data, proper TFO option, both cookie-request and with-cookie cases */
+ /* apparently no visibility of the diffference at this point */
+ /* seen for with-data, proper TFO opt, cookie-req */
+ /* with netwk delay, post-conn tcp_info sees unacked 1 for R, 2 for C; code in smtp_out.c */
+ /* ? older Experimental TFO option behaviour ? */
+ DEBUG(D_transport|D_v) debug_printf(" TFO mode sendto, %s data: EINPROGRESS\n",
+ fastopen_blob->len > 0 ? "with" : "no");
+ if (!fastopen_blob->data)
+ {
+ tcp_out_fastopen = TFO_ATTEMPTED_NODATA; /* we tried; unknown if useful yet */
+ rc = 0;
+ }
+ else /* queue unsent data */
+ rc = send(sock, fastopen_blob->data, fastopen_blob->len, 0);
+ break;
+
+ case EOPNOTSUPP:
+ DEBUG(D_transport)
+ debug_printf("Tried TCP Fast Open but apparently not enabled by sysctl\n");
+ goto legacy_connect;
+
+ case EPIPE:
+ DEBUG(D_transport)
+ debug_printf("Tried TCP Fast Open but kernel too old to support it\n");
+ goto legacy_connect;
+ }
+
+# elif defined(EXIM_TFO_FREEBSD)
+ /* Re: https://people.freebsd.org/~pkelsey/tfo-tools/tfo-client.c */
+
+ if (setsockopt(sock, IPPROTO_TCP, TCP_FASTOPEN, &on, sizeof(on)) < 0)
+ {
+ DEBUG(D_transport)
+ debug_printf("Tried TCP Fast Open but apparently not enabled by sysctl\n");
+ goto legacy_connect;
+ }
+ if ((rc = sendto(sock, fastopen_blob->data, fastopen_blob->len, 0,
+ s_ptr, s_len)) >= 0)
+ {
+ DEBUG(D_transport|D_v)
+ debug_printf(" TFO mode connection attempt to %s, %lu data\n",
+ address, (unsigned long)fastopen_blob->len);
+ tcp_out_fastopen = fastopen_blob->len > 0 ? TFO_ATTEMPTED_DATA : TFO_ATTEMPTED_NODATA;
+ }
+
+# elif defined(EXIM_TFO_CONNECTX)
+ /* MacOS */
+ sa_endpoints_t ends = {
+ .sae_srcif = 0, .sae_srcaddr = NULL, .sae_srcaddrlen = 0,
+ .sae_dstaddr = s_ptr, .sae_dstaddrlen = s_len };
+ struct iovec iov = {
+ .iov_base = fastopen_blob->data, .iov_len = fastopen_blob->len };
+ size_t len;
+
+ if ((rc = connectx(sock, &ends, SAE_ASSOCID_ANY,
+ CONNECT_DATA_IDEMPOTENT, &iov, 1, &len, NULL)) == 0)
+ {
+ DEBUG(D_transport|D_v)
+ debug_printf(" TFO mode connection attempt to %s, %lu data\n",
+ address, (unsigned long)fastopen_blob->len);
+ tcp_out_fastopen = fastopen_blob->len > 0 ? TFO_ATTEMPTED_DATA : TFO_ATTEMPTED_NODATA;
+
+ if (len != fastopen_blob->len)
+ DEBUG(D_transport|D_v)
+ debug_printf(" only queued %lu data!\n", (unsigned long)len);
+ }
+ else if (errno == EINPROGRESS)
+ {
+ DEBUG(D_transport|D_v) debug_printf(" TFO mode connectx, %s data: EINPROGRESS\n",
+ fastopen_blob->len > 0 ? "with" : "no");
+ if (!fastopen_blob->data)
+ {
+ tcp_out_fastopen = TFO_ATTEMPTED_NODATA; /* we tried; unknown if useful yet */
+ rc = 0;
+ }
+ else /* assume that no data was queued; block in send */
+ rc = send(sock, fastopen_blob->data, fastopen_blob->len, 0);
+ }
+# endif
+ }
+else
+#endif /*EXIM_SUPPORT_TFO*/
+ {
+#if defined(EXIM_SUPPORT_TFO) && !defined(EXIM_TFO_CONNECTX)
+legacy_connect:
+#endif
+
+ DEBUG(D_transport|D_v) if (fastopen_blob)
+ debug_printf(" non-TFO mode connection attempt to %s, %lu data\n",
+ address, (unsigned long)fastopen_blob->len);
+ if ((rc = connect(sock, s_ptr, s_len)) >= 0)
+ if ( fastopen_blob && fastopen_blob->data && fastopen_blob->len
+ && send(sock, fastopen_blob->data, fastopen_blob->len, 0) < 0)
+ rc = -1;
+ }
+
+save_errno = errno;
+ALARM_CLR(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 (f.running_in_test_harness && 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
+ hostname host name, or ip address (as text)
+ portlo,porthi the remote port range
+ timeout a timeout
+ connhost if not NULL, host_item to be filled in with connection details
+ errstr pointer for allocated string on error
+ fastopen_blob with SOCK_STREAM, if non-null, request TCP Fast Open.
+ Additionally, optional idempotent early-data to send
+
+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, const blob * fastopen_blob)
+{
+int namelen;
+host_item shost;
+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_copyn(hostname+1, namelen-2);
+ 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_copyn(hostname, namelen);
+
+/* Otherwise lookup IP address(es) from the name */
+
+else
+ {
+ shost.name = string_copyn(hostname, namelen);
+ 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 (host_item * h = &shost; h; 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 (int port = portlo; port <= porthi; port++)
+ if (ip_connect(fd, af, h->address, port, timeout, fastopen_blob) == 0)
+ {
+ if (fd6 >= 0 && fd != fd6) close(fd6);
+ if (fd4 >= 0 && fd != fd4) close(fd4);
+ if (connhost)
+ {
+ h->port = port;
+ *connhost = *h;
+ connhost->next = NULL;
+ }
+ return fd;
+ }
+ }
+
+*errstr = string_sprintf("failed to connect to any address for %s: %s",
+ hostname, strerror(errno));
+
+bad:
+ close(fd4); close(fd6); return -1;
+}
+
+
+/*XXX TFO? */
+int
+ip_tcpsocket(const uschar * hostport, uschar ** errstr, int tmo,
+ host_item * connhost)
+{
+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,
+ tmo, connhost, errstr, NULL);
+}
+
+int
+ip_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;
+ }
+
+callout_address = string_copy(path);
+server.sun_family = AF_UNIX;
+Ustrncpy(US 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;
+}
+
+/* spec is either an absolute path (with a leading /), or
+a host (name or IP) and port (whitespace-separated).
+The port can be a range, dash-separated, or a single number.
+
+For a TCP socket, optionally fill in a host_item.
+*/
+int
+ip_streamsocket(const uschar * spec, uschar ** errstr, int tmo,
+ host_item * connhost)
+{
+return *spec == '/'
+ ? ip_unixsocket(spec, errstr) : ip_tcpsocket(spec, errstr, tmo, connhost);
+}
+
+/*************************************************
+* 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, const uschar *address, BOOL torf)
+{
+int fodder = 1;
+if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
+ US (&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 *
+*************************************************/
+
+/*
+Arguments:
+ fd the file descriptor
+ timelimit the timeout endpoint, seconds-since-epoch
+Returns: TRUE => ready for i/o
+ FALSE => timed out, or other error
+*/
+BOOL
+fd_ready(int fd, time_t timelimit)
+{
+int rc, time_left = timelimit - time(NULL);
+
+if (time_left <= 0)
+ {
+ errno = ETIMEDOUT;
+ return FALSE;
+ }
+/* Wait until the socket is ready */
+
+do
+ {
+ /*DEBUG(D_transport) debug_printf("waiting for data on fd\n");*/
+ rc = poll_one_fd(fd, POLLIN, time_left * 1000);
+
+ /* 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 and adjust the timeout
+ accordingly */
+
+ if (rc < 0 && errno == EINTR)
+ {
+ DEBUG(D_transport) debug_printf("EINTR while waiting for socket data\n");
+
+ /* Watch out, 'continue' jumps to the condition, not to the loops top */
+ if ((time_left = timelimit - time(NULL)) > 0) continue;
+ }
+
+ if (rc <= 0)
+ {
+ errno = ETIMEDOUT;
+ return FALSE;
+ }
+
+ /* Checking the FD_ISSET is not enough, if we're interrupted, the
+ select_inset may still contain the 'input'. */
+ }
+while (rc < 0);
+return TRUE;
+}
+
+/* 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:
+ cctx the connection context (socket fd, possibly TLS context)
+ buffer to read into
+ bufsize the buffer size
+ timelimit the timeout endpoint, seconds-since-epoch
+
+Returns: > 0 => that much data read
+ <= 0 on error or EOF; errno set - zero for EOF
+*/
+
+int
+ip_recv(client_conn_ctx * cctx, uschar * buffer, int buffsize, time_t timelimit)
+{
+int rc;
+
+if (!fd_ready(cctx->sock, timelimit))
+ return -1;
+
+/* 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. */
+
+#ifndef DISABLE_TLS
+if (cctx->tls_ctx) /* client TLS */
+ rc = tls_read(cctx->tls_ctx, buffer, buffsize);
+else if (tls_in.active.sock == cctx->sock) /* server TLS */
+ rc = tls_read(NULL, buffer, buffsize);
+else
+#endif
+ rc = recv(cctx->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)
+{
+for (int i = 0; i < dscp_table_size; ++i)
+ fprintf(stream, "%s\n", dscp_table[i].name);
+}
+
+
+/* End of ip.c */
+/* vi: aw ai sw=2
+*/
diff --git a/src/local_scan.c b/src/local_scan.c
new file mode 100644
index 0000000..7a3bae7
--- /dev/null
+++ b/src/local_scan.c
@@ -0,0 +1,64 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* Copyright (c) The Exim Maintainers 2021 */
+/* 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
+
+HAVE_LOCAL_SCAN=yes
+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)
+{
+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
index 0000000..c609a27
--- /dev/null
+++ b/src/local_scan.h
@@ -0,0 +1,239 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2020 */
+/* 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. That source
+should first #define LOCAL_SCAN
+
+Not every definition that becomes available to the compiler by the inclusion
+of this file is part of the local_scan API. The "Adding a local scan function
+to Exim" chapter in the documentation is definitive.
+
+This API is also used for functions called by the ${dlfunc expansion item.
+Source for those should first #define DLFUNC_IMPL and then include this file.
+Coders of dlfunc routines should read the notes on tainting at the start of
+store.c
+*/
+
+
+/* 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"
+
+
+/* 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 6
+#define LOCAL_SCAN_ABI_VERSION_MINOR 0
+#define LOCAL_SCAN_ABI_VERSION \
+ LOCAL_SCAN_ABI_VERSION_MAJOR.LOCAL_SCAN_ABI_VERSION_MINOR
+
+
+
+/* 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)
+
+/* 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; /* should have been uschar but too late now */
+ int type;
+ union {
+ void * value;
+ long offset;
+ void (* fn)();
+ } v;
+} optionlist;
+#define OPT_OFF(s, field) {.offset = offsetof(s, field)}
+
+/* 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 */
+ uschar *orcpt; /* DSN orcpt */
+ int dsn_flags; /* DSN flags */
+#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 const 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 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, const uschar *, int, int *,
+ uschar **);
+extern int smtp_fflush(void);
+extern void smtp_printf(const char *, BOOL, ...) PRINTF_FUNCTION(1,3);
+extern void smtp_vprintf(const char *, BOOL, va_list);
+
+#define string_sprintf(fmt, ...) \
+ string_sprintf_trc(fmt, US __FUNCTION__, __LINE__, __VA_ARGS__)
+extern uschar *string_sprintf_trc(const char *, const uschar *, unsigned, ...) ALMOST_PRINTF(1,4);
+
+#define store_get(size, proto_mem) \
+ store_get_3((size), (proto_mem), __FUNCTION__, __LINE__)
+extern void *store_get_3(int, const void *, const char *, int) ALLOC ALLOC_SIZE(1) WARN_UNUSED_RESULT;
+#define store_get_perm(size, proto_mem) \
+ store_get_perm_3((size), (proto_mem), __FUNCTION__, __LINE__)
+extern void *store_get_perm_3(int, const void *, const char *, int) ALLOC ALLOC_SIZE(1) WARN_UNUSED_RESULT;
+
+
+#if defined(LOCAL_SCAN) || defined(DLFUNC_IMPL)
+/* When compiling a local_scan() file we want to rename a published API, so that
+we can use an inlined implementation in the compiles of the main Exim files,
+with the original name. */
+
+# define string_copy(s) string_copy_function(s)
+# define string_copyn(s, n) string_copyn_function((s), (n))
+# define string_copy_taint(s, t) string_copy_taint_function((s), (t))
+# define child_open_exim(p) child_open_exim_function((p), US"from local_scan")
+# define child_open_exim2(p, s, a) child_open_exim2_function((p), (s), (a), US"from local_scan")
+# define child_open(a,e,u,i,o,l) child_open_function((a),(e),(u),(i),(o),(l),US"from local_scan")
+
+extern uschar * string_copy_function(const uschar *);
+extern uschar * string_copyn_function(const uschar *, int n);
+extern uschar * string_copy_taint_function(const uschar *, const void * proto_mem);
+extern pid_t child_open_exim_function(int *, const uschar *);
+extern pid_t child_open_exim2_function(int *, uschar *, uschar *, const uschar *);
+extern pid_t child_open_function(uschar **, uschar **, int, int *, int *, BOOL, const uschar *);
+#endif
+
+/* End of local_scan.h */
diff --git a/src/log.c b/src/log.c
new file mode 100644
index 0000000..8ca973f
--- /dev/null
+++ b/src/log.c
@@ -0,0 +1,1554 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 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 *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"";
+
+static size_t pid_position[2];
+
+
+/* These should be kept in-step with the private delivery error
+number definitions in macros.h */
+
+static const uschar * exim_errstrings[] = {
+ [0] = US"",
+ [- ERRNO_UNKNOWNERROR] = US"unknown error",
+ [- ERRNO_USERSLASH] = US"user slash",
+ [- ERRNO_EXISTRACE] = US"exist race",
+ [- ERRNO_NOTREGULAR] = US"not regular",
+ [- ERRNO_NOTDIRECTORY] = US"not directory",
+ [- ERRNO_BADUGID] = US"bad ugid",
+ [- ERRNO_BADMODE] = US"bad mode",
+ [- ERRNO_INODECHANGED] = US"inode changed",
+ [- ERRNO_LOCKFAILED] = US"lock failed",
+ [- ERRNO_BADADDRESS2] = US"bad address2",
+ [- ERRNO_FORBIDPIPE] = US"forbid pipe",
+ [- ERRNO_FORBIDFILE] = US"forbid file",
+ [- ERRNO_FORBIDREPLY] = US"forbid reply",
+ [- ERRNO_MISSINGPIPE] = US"missing pipe",
+ [- ERRNO_MISSINGFILE] = US"missing file",
+ [- ERRNO_MISSINGREPLY] = US"missing reply",
+ [- ERRNO_BADREDIRECT] = US"bad redirect",
+ [- ERRNO_SMTPCLOSED] = US"smtp closed",
+ [- ERRNO_SMTPFORMAT] = US"smtp format",
+ [- ERRNO_SPOOLFORMAT] = US"spool format",
+ [- ERRNO_NOTABSOLUTE] = US"not absolute",
+ [- ERRNO_EXIMQUOTA] = US"Exim-imposed quota",
+ [- ERRNO_HELD] = US"held",
+ [- ERRNO_FILTER_FAIL] = US"Delivery filter process failure",
+ [- ERRNO_CHHEADER_FAIL] = US"Delivery add/remove header failure",
+ [- ERRNO_WRITEINCOMPLETE] = US"Delivery write incomplete error",
+ [- ERRNO_EXPANDFAIL] = US"Some expansion failed",
+ [- ERRNO_GIDFAIL] = US"Failed to get gid",
+ [- ERRNO_UIDFAIL] = US"Failed to get uid",
+ [- ERRNO_BADTRANSPORT] = US"Unset or non-existent transport",
+ [- ERRNO_MBXLENGTH] = US"MBX length mismatch",
+ [- ERRNO_UNKNOWNHOST] = US"Lookup failed routing or in smtp tpt",
+ [- ERRNO_FORMATUNKNOWN] = US"Can't match format in appendfile",
+ [- ERRNO_BADCREATE] = US"Creation outside home in appendfile",
+ [- ERRNO_LISTDEFER] = US"Can't check a list; lookup defer",
+ [- ERRNO_DNSDEFER] = US"DNS lookup defer",
+ [- ERRNO_TLSFAILURE] = US"Failed to start TLS session",
+ [- ERRNO_TLSREQUIRED] = US"Mandatory TLS session not started",
+ [- ERRNO_CHOWNFAIL] = US"Failed to chown a file",
+ [- ERRNO_PIPEFAIL] = US"Failed to create a pipe",
+ [- ERRNO_CALLOUTDEFER] = US"When verifying",
+ [- ERRNO_AUTHFAIL] = US"When required by client",
+ [- ERRNO_CONNECTTIMEOUT] = US"Used internally in smtp transport",
+ [- ERRNO_RCPT4XX] = US"RCPT gave 4xx error",
+ [- ERRNO_MAIL4XX] = US"MAIL gave 4xx error",
+ [- ERRNO_DATA4XX] = US"DATA gave 4xx error",
+ [- ERRNO_PROXYFAIL] = US"Negotiation failed for proxy configured host",
+ [- ERRNO_AUTHPROB] = US"Authenticator 'other' failure",
+ [- ERRNO_UTF8_FWD] = US"target not supporting SMTPUTF8",
+ [- ERRNO_HOST_IS_LOCAL] = US"host is local",
+ [- ERRNO_TAINT] = US"tainted filename",
+
+ [- ERRNO_RRETRY] = US"Not time for routing",
+
+ [- ERRNO_LRETRY] = US"Not time for local delivery",
+ [- ERRNO_HRETRY] = US"Not time for any remote host",
+ [- ERRNO_LOCAL_ONLY] = US"Local-only delivery",
+ [- ERRNO_QUEUE_DOMAIN] = US"Domain in queue_domains",
+ [- ERRNO_TRETRY] = US"Transport concurrency limit",
+
+ [- ERRNO_EVENT] = US"Event requests alternate response",
+};
+
+
+/************************************************/
+const uschar *
+exim_errstr(int err)
+{
+return err < 0 ? exim_errstrings[-err] : CUS strerror(err);
+}
+
+/*************************************************
+* 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, const uschar *s)
+{
+int len;
+int linecount = 0;
+
+if (!syslog_pid && LOGGING(pid))
+ s = string_sprintf("%.*s%s", (int)pid_position[0], s, s + pid_position[1]);
+if (!syslog_timestamp)
+ {
+ len = log_timezone ? 26 : 20;
+ if (LOGGING(millisec)) len += 4;
+ s += len;
+ }
+
+len = Ustrlen(s);
+
+#ifndef NO_OPENLOG
+if (!syslog_open && !f.running_in_test_harness)
+ {
+# 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 (int pass = 0; pass < 2; pass++)
+ {
+ const uschar * ss = s;
+ for (int 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 (f.running_in_test_harness)
+ if (linecount == 1)
+ fprintf(stderr, "SYSLOG: '%.*s'\n", plen, ss);
+ else
+ fprintf(stderr, "SYSLOG: '[%d%c%d] %.*s'\n", i,
+ ss[plen] == '\n' && tlen != 0 ? '\\' : '/',
+ linecount, plen, ss);
+ 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)
+ {
+ write_syslog(LOG_CRIT, s1);
+ if (debug_file) debug_printf("%s\n", s1);
+ if (log_stderr && log_stderr != debug_file)
+ fprintf(log_stderr, "%s\n", s1);
+ }
+if (f.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)
+*/
+
+static int
+log_open_already_exim(const uschar * const name)
+{
+int fd = -1;
+const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NONBLOCK;
+
+if (geteuid() != exim_uid)
+ {
+ errno = EACCES;
+ return -1;
+ }
+
+fd = Uopen(name, flags, 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)
+ if (created)
+ debug_printf("created log directory %s\n", name);
+ else
+ debug_printf("failed to create log directory %s: %s\n", name, strerror(errno));
+ *lastslash = '/';
+ if (created) fd = Uopen(name, flags, LOG_MODE);
+ }
+
+return fd;
+}
+
+
+
+/* Inspired by OpenSSH's mm_send_fd(). Thanks!
+Send fd over socketpair.
+Return: true iff good.
+*/
+
+static BOOL
+log_send_fd(const int sock, const int fd)
+{
+struct msghdr msg;
+union {
+ struct cmsghdr hdr;
+ char buf[CMSG_SPACE(sizeof(int))];
+} cmsgbuf;
+struct cmsghdr *cmsg;
+char ch = 'A';
+struct iovec vec = {.iov_base = &ch, .iov_len = 1};
+ssize_t n;
+
+memset(&msg, 0, sizeof(msg));
+memset(&cmsgbuf, 0, sizeof(cmsgbuf));
+msg.msg_control = &cmsgbuf.buf;
+msg.msg_controllen = sizeof(cmsgbuf.buf);
+
+cmsg = CMSG_FIRSTHDR(&msg);
+cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+cmsg->cmsg_level = SOL_SOCKET;
+cmsg->cmsg_type = SCM_RIGHTS;
+*(int *)CMSG_DATA(cmsg) = fd;
+
+msg.msg_iov = &vec;
+msg.msg_iovlen = 1;
+
+while ((n = sendmsg(sock, &msg, 0)) == -1 && errno == EINTR);
+return n == 1;
+}
+
+/* Inspired by OpenSSH's mm_receive_fd(). Thanks!
+Return fd passed over socketpair, or -1 on error.
+*/
+
+static int
+log_recv_fd(const int sock)
+{
+struct msghdr msg;
+union {
+ struct cmsghdr hdr;
+ char buf[CMSG_SPACE(sizeof(int))];
+} cmsgbuf;
+struct cmsghdr *cmsg;
+char ch = '\0';
+struct iovec vec = {.iov_base = &ch, .iov_len = 1};
+ssize_t n;
+int fd;
+
+memset(&msg, 0, sizeof(msg));
+msg.msg_iov = &vec;
+msg.msg_iovlen = 1;
+
+memset(&cmsgbuf, 0, sizeof(cmsgbuf));
+msg.msg_control = &cmsgbuf.buf;
+msg.msg_controllen = sizeof(cmsgbuf.buf);
+
+while ((n = recvmsg(sock, &msg, 0)) == -1 && errno == EINTR) ;
+if (n != 1 || ch != 'A') return -1;
+
+if (!(cmsg = CMSG_FIRSTHDR(&msg))) return -1;
+if (cmsg->cmsg_type != SCM_RIGHTS) return -1;
+if ((fd = *(const int *)CMSG_DATA(cmsg)) < 0) return -1;
+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_open_as_exim(const uschar * const name)
+{
+int fd = -1;
+const uid_t euid = geteuid();
+
+if (euid == exim_uid)
+ fd = log_open_already_exim(name);
+else if (euid == root_uid)
+ {
+ int sock[2];
+ if (socketpair(AF_UNIX, SOCK_STREAM, 0, sock) == 0)
+ {
+ const pid_t pid = fork();
+ if (pid == 0)
+ {
+ (void)close(sock[0]);
+ if ( setgroups(1, &exim_gid) != 0
+ || setgid(exim_gid) != 0
+ || setuid(exim_uid) != 0
+
+ || getuid() != exim_uid || geteuid() != exim_uid
+ || getgid() != exim_gid || getegid() != exim_gid
+
+ || (fd = log_open_already_exim(name)) < 0
+ || !log_send_fd(sock[1], fd)
+ ) _exit(EXIT_FAILURE);
+ (void)close(sock[1]);
+ _exit(EXIT_SUCCESS);
+ }
+
+ (void)close(sock[1]);
+ if (pid > 0)
+ {
+ fd = log_recv_fd(sock[0]);
+ while (waitpid(pid, NULL, 0) == -1 && errno == EINTR);
+ }
+ (void)close(sock[0]);
+ }
+ }
+
+if (fd >= 0)
+ {
+ int flags;
+ flags = fcntl(fd, F_GETFD);
+ if (flags != -1) (void)fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
+ flags = fcntl(fd, F_GETFL);
+ if (flags != -1) (void)fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
+ }
+else
+ errno = EACCES;
+
+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
+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, const 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]);
+
+switch (type)
+ {
+ case lt_main:
+ /* 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. */
+ Ustrcpy(mainlog_name, buffer);
+ if (string_datestamp_offset > 0)
+ mainlog_datestamp = mainlog_name + string_datestamp_offset;
+ break;
+
+ case lt_reject:
+ /* Ditto for the reject log */
+ Ustrcpy(rejectlog_name, buffer);
+ if (string_datestamp_offset > 0)
+ rejectlog_datestamp = rejectlog_name + string_datestamp_offset;
+ break;
+
+ case lt_debug:
+ /* and deal with the debug log (which keeps the datestamp, but does not
+ update it) */
+ Ustrcpy(debuglog_name, buffer);
+ if (tag)
+ {
+ if (is_tainted(tag))
+ die(US"exim: tainted tag for debug log filename",
+ US"Logging failure; please try later");
+
+ /* 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);
+ }
+ break;
+
+ default:
+ /* 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. */
+ 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 copy is ok, because we know that to is a substring of from. But
+ due to overlap we must use memmove() not Ustrcpy(). */
+ memmove(from, to, Ustrlen(to)+1);
+ }
+ break;
+ }
+
+/* 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. After a successful open, return. */
+
+if ((*fd = log_open_as_exim(buffer)) >= 0)
+ return;
+
+euid = geteuid();
+
+/* 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)
+ {
+ *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. Also, when we had to use a subprocess for the create we didn't retrieve
+errno from it, so get the error from the open attempt above (which is often
+meaningful enough, so leave it). */
+
+if (!panic_save_buffer)
+ if ((panic_save_buffer = US malloc(LOG_BUFFER_SIZE)))
+ 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 */
+}
+
+
+static void
+unlink_log(int type)
+{
+if (type == lt_debug) unlink(CS debuglog_name);
+}
+
+
+
+/*************************************************
+* 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 gstring *
+log_config_info(gstring * g, int flags)
+{
+g = string_cat(g, US"Exim configuration error");
+
+if (flags & (LOG_CONFIG_FOR & ~LOG_CONFIG))
+ return string_cat(g, US" for ");
+
+if (flags & (LOG_CONFIG_IN & ~LOG_CONFIG))
+ g = string_fmt_append(g, " in line %d of %s", config_lineno, config_filename);
+
+return string_catn(g, US":\n ", 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)
+ if ((panic_save_buffer = US malloc(LOG_BUFFER_SIZE)))
+ 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;
+}
+
+
+
+static void
+set_file_path(void)
+{
+int sep = ':'; /* Fixed separator - outside use */
+uschar *t;
+const uschar *tt = US LOG_FILE_PATH;
+while ((t = string_nextinlist(&tt, &sep, log_buffer, LOG_BUFFER_SIZE)))
+ {
+ if (Ustrcmp(t, "syslog") == 0 || t[0] == 0) continue;
+ file_path = string_copy(t);
+ break;
+ }
+}
+
+
+/* Close mainlog, unless we do not see a chance to open the file mainlog later
+again. This will happen if we log from a transport process (which has dropped
+privs); something we traditionally avoid, but the introduction of taint-tracking
+and resulting detection of errors is makinng harder. */
+
+void
+mainlog_close(void)
+{
+if (mainlogfd < 0
+ || !(geteuid() == 0 || geteuid() == exim_uid))
+ return;
+(void)close(mainlogfd);
+mainlogfd = -1;
+mainlog_inode = 0;
+}
+
+/*************************************************
+* 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 panicking.
+
+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_selector[0]
+ 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, ...)
+{
+int paniclogfd;
+ssize_t written_len;
+gstring gs = { .size = LOG_BUFFER_SIZE-1, .ptr = 0, .s = log_buffer };
+gstring * g;
+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 ? panic_save_buffer : US"";
+ if (debug_file) debug_printf("%s%s", extra, log_buffer);
+ if (log_stderr && log_stderr != debug_file)
+ fprintf(log_stderr, "%s%s", extra, log_buffer);
+ if (*extra) 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)
+ if (!(log_buffer = US malloc(LOG_BUFFER_SIZE)))
+ {
+ 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)
+ {
+ int sep = ':'; /* Fixed separator - outside use */
+ uschar *s;
+ const uschar *ss = log_file_path;
+
+ logging_mode = 0;
+ while ((s = string_nextinlist(&ss, &sep, log_buffer, LOG_BUFFER_SIZE)))
+ {
+ if (Ustrcmp(s, "syslog") == 0)
+ logging_mode |= LOG_MODE_SYSLOG;
+ else if (logging_mode & LOG_MODE_FILE)
+ multiple = TRUE;
+ else
+ {
+ logging_mode |= LOG_MODE_FILE;
+
+ /* If a non-empty path is given, use it */
+
+ if (*s)
+ 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
+ set_file_path(); /* 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 && !file_path[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);
+ }
+
+/* Optionally trigger debug */
+
+if (flags & LOG_PANIC && dtrigger_selector & BIT(DTi_panictrigger))
+ debug_trigger_fire();
+
+/* 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;
+
+ g = string_catn(&gs, US"LOG:", 4);
+
+ /* Show the selector that was passed into the call. */
+
+ for (i = 0; i < log_options_count; i++)
+ {
+ unsigned int bitnum = log_options[i].bit;
+ if (bitnum < BITWORDSIZE && selector == BIT(bitnum))
+ g = string_fmt_append(g, " %s", log_options[i].name);
+ }
+
+ g = string_fmt_append(g, "%s%s%s%s\n ",
+ flags & LOG_MAIN ? " MAIN" : "",
+ flags & LOG_PANIC ? " PANIC" : "",
+ (flags & LOG_PANIC_DIE) == LOG_PANIC_DIE ? " DIE" : "",
+ flags & LOG_REJECT ? " REJECT" : "");
+
+ if (flags & LOG_CONFIG) g = log_config_info(g, flags);
+
+ /* We want to be able to log tainted info, but log_buffer is directly
+ malloc'd. So use deliberately taint-nonchecking routines to build into
+ it, trusting that we will never expand the results. */
+
+ va_start(ap, format);
+ i = g->ptr;
+ if (!string_vformat(g, SVFMT_TAINT_NOCHK, format, ap))
+ {
+ g->ptr = i;
+ g = string_cat(g, US"**** log string overflowed log buffer ****");
+ }
+ va_end(ap);
+
+ g->size = LOG_BUFFER_SIZE;
+ g = string_catn(g, US"\n", 1);
+ debug_printf("%s", string_from_gstring(g));
+
+ gs.size = LOG_BUFFER_SIZE-1; /* Having used the buffer for debug output, */
+ gs.ptr = 0; /* reset it for the real use. */
+ gs.s = log_buffer;
+ }
+/* If no log file is specified, we are in a mess. */
+
+if (!(flags & (LOG_MAIN|LOG_PANIC|LOG_REJECT)))
+ 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 (f.disable_logging)
+ {
+ DEBUG(D_any) debug_printf("log writing disabled\n");
+ if ((flags & LOG_PANIC_DIE) == LOG_PANIC_DIE) exim_exit(EXIT_FAILURE);
+ 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. */
+
+g = string_fmt_append(&gs, "%s ", tod_stamp(tod_log));
+
+if (LOGGING(pid))
+ {
+ if (!syslog_pid) pid_position[0] = g->ptr; /* remember begin … */
+ g = string_fmt_append(g, "[%d] ", (int)getpid());
+ if (!syslog_pid) pid_position[1] = g->ptr; /* … and end+1 of the PID */
+ }
+
+if (f.really_exim && message_id[0] != 0)
+ g = string_fmt_append(g, "%s ", message_id);
+
+if (flags & LOG_CONFIG)
+ g = log_config_info(g, flags);
+
+va_start(ap, format);
+ {
+ int i = g->ptr;
+
+ /* We want to be able to log tainted info, but log_buffer is directly
+ malloc'd. So use deliberately taint-nonchecking routines to build into
+ it, trusting that we will never expand the results. */
+
+ if (!string_vformat(g, SVFMT_TAINT_NOCHK, format, ap))
+ {
+ g->ptr = i;
+ g = string_cat(g, US"**** log string overflowed log buffer ****\n");
+ }
+ }
+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
+ && g->ptr < LOG_BUFFER_SIZE - 10 - Ustrlen(raw_sender))
+ g = string_fmt_append_f(g, SVFMT_TAINT_NOCHK, " from <%s>", raw_sender);
+
+/* 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
+ && g->ptr < LOG_BUFFER_SIZE - 6
+ && raw_recipients_count > 0)
+ {
+ int i;
+ g = string_fmt_append_f(g, SVFMT_TAINT_NOCHK, " for", NULL);
+ for (i = 0; i < raw_recipients_count; i++)
+ {
+ uschar * s = raw_recipients[i];
+ if (LOG_BUFFER_SIZE - g->ptr < Ustrlen(s) + 3) break;
+ g = string_fmt_append_f(g, SVFMT_TAINT_NOCHK, " %s", s);
+ }
+ }
+
+g = string_catn(g, US"\n", 1);
+string_from_gstring(g);
+
+/* 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 (!f.really_exim || f.log_testing_mode)
+ {
+ if ( !debug_selector
+ && log_stderr
+ && (selector == 0 || (selector & log_selector[0]) != 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
+ && (!selector || selector & log_selector[0]))
+ {
+ if ( logging_mode & LOG_MODE_SYSLOG
+ && (syslog_duplication || !(flags & (LOG_REJECT|LOG_PANIC))))
+ write_syslog(LOG_INFO, log_buffer);
+
+ if (logging_mode & LOG_MODE_FILE)
+ {
+ 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)
+ {
+ 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)
+ mainlog_close();
+
+ /* 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, g->s, g->ptr);
+ if (written_len != g->ptr)
+ {
+ log_write_failed(US"main log", g->ptr, 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)
+ {
+ if (header_list && LOGGING(rejected_header))
+ {
+ gstring * g2;
+ int i;
+
+ if (recipients_count > 0)
+ {
+ /* List the sender */
+
+ g2 = string_fmt_append_f(g, SVFMT_TAINT_NOCHK,
+ "Envelope-from: <%s>\n", sender_address);
+ if (g2) g = g2;
+
+ /* List up to 5 recipients */
+
+ g2 = string_fmt_append_f(g, SVFMT_TAINT_NOCHK,
+ "Envelope-to: <%s>\n", recipients_list[0].address);
+ if (g2) g = g2;
+
+ for (i = 1; i < recipients_count && i < 5; i++)
+ {
+ g2 = string_fmt_append_f(g, SVFMT_TAINT_NOCHK,
+ " <%s>\n", recipients_list[i].address);
+ if (g2) g = g2;
+ }
+
+ if (i < recipients_count)
+ {
+ g2 = string_fmt_append_f(g, SVFMT_TAINT_NOCHK, " ...\n", NULL);
+ if (g2) g = g2;
+ }
+ }
+
+ /* A header with a NULL text is an unfilled in Received: header */
+
+ for (header_line * h = header_list; h; h = h->next) if (h->text)
+ {
+ g2 = string_fmt_append_f(g, SVFMT_TAINT_NOCHK,
+ "%c %s", h->type, h->text);
+ if (g2)
+ g = g2;
+ else /* Buffer is full; truncate */
+ {
+ g->ptr -= 100; /* For message and separator */
+ if (g->s[g->ptr-1] == '\n') g->ptr--;
+ g = string_cat(g, US"\n*** truncated ***\n");
+ break;
+ }
+ }
+ }
+
+ /* Write to syslog or to a log file */
+
+ if ( logging_mode & LOG_MODE_SYSLOG
+ && (syslog_duplication || !(flags & LOG_PANIC)))
+ write_syslog(LOG_NOTICE, string_from_gstring(g));
+
+ /* 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)
+ {
+ struct stat statbuf;
+
+ if (rejectlog_datestamp)
+ {
+ 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, g->s, g->ptr);
+ if (written_len != g->ptr)
+ {
+ log_write_failed(US"reject log", g->ptr, 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)
+ {
+ if (log_stderr && log_stderr != debug_file && !mua_wrapper)
+ fprintf(log_stderr, "%s", CS string_from_gstring(g));
+
+ if (logging_mode & LOG_MODE_SYSLOG)
+ 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)
+ {
+ panic_recurseflag = TRUE;
+ open_log(&paniclogfd, lt_panic, NULL); /* Won't return on failure */
+ panic_recurseflag = FALSE;
+
+ if (panic_save_buffer)
+ (void) write(paniclogfd, panic_save_buffer, Ustrlen(panic_save_buffer));
+
+ written_len = write_to_fd_buf(paniclogfd, g->s, g->ptr);
+ if (written_len != g->ptr)
+ {
+ 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)", g->ptr, (int)written_len, save_errno, strerror(save_errno));
+ write_syslog(LOG_CRIT, string_from_gstring(g));
+ 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;
+}
+
+
+
+/*************************************************
+* Multi-bit set or clear *
+*************************************************/
+
+/* These functions take a list of bit indexes (terminated by -1) and
+clear or set the corresponding bits in the selector.
+
+Arguments:
+ selector address of the bit string
+ selsize number of words in the bit string
+ bits list of bits to set
+*/
+
+void
+bits_clear(unsigned int *selector, size_t selsize, int *bits)
+{
+for(; *bits != -1; ++bits)
+ BIT_CLEAR(selector, selsize, *bits);
+}
+
+void
+bits_set(unsigned int *selector, size_t selsize, int *bits)
+{
+for(; *bits != -1; ++bits)
+ BIT_SET(selector, selsize, *bits);
+}
+
+
+
+/*************************************************
+* 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 option table is a list of names and bit indexes. The index -1
+means "set all bits, except for those listed in notall". The notall
+list is terminated by -1.
+
+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:
+ selector address of the bit string
+ selsize number of words in the bit string
+ notall list of bits to exclude from "all"
+ 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 * selector, size_t selsize, int * notall,
+ const uschar * string, bit_table * options, int count, uschar * which,
+ int flags)
+{
+uschar *errmsg;
+if (!string) return;
+
+if (*string == '=')
+ {
+ char *end; /* Not uschar */
+ memset(selector, 0, sizeof(*selector)*selsize);
+ *selector = strtoul(CCS string+1, &end, 0);
+ if (!*end) return;
+ errmsg = string_sprintf("malformed numeric %s_selector setting: %s", which,
+ string);
+ goto ERROR_RETURN;
+ }
+
+/* Handle symbolic setting */
+
+else for(;;)
+ {
+ BOOL adding;
+ const uschar * s;
+ int len;
+ bit_table * start, * end;
+
+ Uskip_whitespace(&string);
+ if (!*string) 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;
+
+ if (bit == -1)
+ {
+ if (adding)
+ {
+ memset(selector, -1, sizeof(*selector)*selsize);
+ bits_clear(selector, selsize, notall);
+ }
+ else
+ memset(selector, 0, sizeof(*selector)*selsize);
+ }
+ else if (adding)
+ BIT_SET(selector, selsize, bit);
+ else
+ BIT_CLEAR(selector, selsize, 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.
+
+It might be nice, in ACL-initiated pretrigger mode, to not create the file
+immediately but only upon a trigger - but we'd need another cmdline option
+to pass the name through child_exxec_exim(). */
+
+void
+debug_logging_activate(const uschar * tag_name, const uschar * opts)
+{
+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 && (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, 1, debug_notall, opts,
+ debug_options, debug_options_count, US"debug", DEBUG_FROM_CONFIG);
+
+/* When activating from a transport process we may never have logged at all
+resulting in certain setup not having been done. Hack this for now so we
+do not segfault; note that nondefault log locations will not work */
+
+if (!*file_path) set_file_path();
+
+open_log(&debug_fd, lt_debug, tag_name);
+
+if (debug_fd != -1)
+ debug_file = fdopen(debug_fd, "w");
+else
+ log_write(0, LOG_MAIN|LOG_PANIC, "unable to open debug log");
+}
+
+
+void
+debug_logging_from_spool(const uschar * filename)
+{
+if (debug_fd < 0)
+ {
+ Ustrncpy(debuglog_name, filename, sizeof(debuglog_name));
+ if ((debug_fd = log_open_as_exim(filename)) >= 0)
+ debug_file = fdopen(debug_fd, "w");
+ DEBUG(D_deliver) debug_printf("debug enabled by spoolfile\n");
+ }
+/*
+else DEBUG(D_deliver)
+ debug_printf("debug already active; ignoring spoolfile '%s'\n", filename);
+*/
+}
+
+
+void
+debug_logging_stop(BOOL kill)
+{
+debug_pretrigger_discard();
+if (!debug_file || !debuglog_name[0]) return;
+
+debug_selector = 0;
+fclose(debug_file);
+debug_file = NULL;
+debug_fd = -1;
+if (kill) unlink_log(lt_debug);
+}
+
+
+/* End of log.c */
diff --git a/src/lookupapi.h b/src/lookupapi.h
new file mode 100644
index 0000000..41cc239
--- /dev/null
+++ b/src/lookupapi.h
@@ -0,0 +1,65 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* 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 */
+ const uschar *, /* file name for those that have one */
+ uschar **); /* for error message */
+ BOOL (*check)( /* file checking function */
+ void *, /* handle */
+ const 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 */
+ const uschar *, /* file name or NULL */
+ const uschar *, /* key or query */
+ int, /* length of key or query */
+ uschar **, /* for returning answer */
+ uschar **, /* for error message */
+ uint *, /* cache TTL, seconds */
+ const uschar *); /* options */
+ 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 */
+ unsigned); /* lookup type index */
+ gstring * (*version_report)( /* diagnostic function */
+ gstring *); /* string to appand to */
+} lookup_info;
+
+/* This magic number is used by the following lookup_module_info structure
+ for checking API compatibility. It used to be equivalent to the string"LMM3" */
+#define LOOKUP_MODULE_INFO_MAGIC 0x4c4d4935
+/* Version 2 adds: version_report */
+/* Version 3 change: non/cache becomes TTL in seconds */
+/* Version 4 add: index on quoting function */
+/* Version 5 change: version report now adds to a gstring */
+
+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
index 0000000..19585bf
--- /dev/null
+++ b/src/lookups/Makefile
@@ -0,0 +1,76 @@
+# Make file for building Exim's lookup modules.
+# This is called from the main make file, after cd'ing
+# to the lookups subdirectory.
+#
+# Copyright (c) The Exim Maintainers 2021
+
+# 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: $(HDRS) lf_check_file.c lf_functions.h
+lf_quote.o: $(HDRS) lf_quote.c lf_functions.h
+lf_sqlperform.o: $(HDRS) lf_sqlperform.c lf_functions.h
+
+cdb.o: $(HDRS) cdb.c
+dbmdb.o: $(HDRS) dbmdb.c
+dnsdb.o: $(HDRS) dnsdb.c
+dsearch.o: $(HDRS) dsearch.c
+ibase.o: $(HDRS) ibase.c
+ldap.o: $(HDRS) ldap.c
+lmdb.o: $(HDRS) lmdb.c
+json.o: $(HDRS) json.c
+lsearch.o: $(HDRS) lsearch.c
+mysql.o: $(HDRS) mysql.c
+nis.o: $(HDRS) nis.c
+nisplus.o: $(HDRS) nisplus.c
+oracle.o: $(HDRS) oracle.c
+passwd.o: $(HDRS) passwd.c
+pgsql.o: $(HDRS) pgsql.c
+readsock.o: $(HDRS) readsock.c
+redis.o: $(HDRS) redis.c
+spf.o: $(HDRS) spf.c
+sqlite.o: $(HDRS) sqlite.c
+testdb.o: $(HDRS) testdb.c
+whoson.o: $(HDRS) whoson.c
+
+cdb.so: $(HDRS) cdb.c
+dbmdb.so: $(HDRS) dbmdb.c
+dnsdb.so: $(HDRS) dnsdb.c
+dsearch.so: $(HDRS) dsearch.c
+ibase.so: $(HDRS) ibase.c
+json.so: $(HDRS) json.c
+ldap.so: $(HDRS) ldap.c
+lmdb.so: $(HDRS) lmdb.c
+lsearch.so: $(HDRS) lsearch.c
+mysql.so: $(HDRS) mysql.c
+nis.so: $(HDRS) nis.c
+nisplus.so: $(HDRS) nisplus.c
+oracle.so: $(HDRS) oracle.c
+passwd.so: $(HDRS) passwd.c
+pgsql.so: $(HDRS) pgsql.c
+redis.so: $(HDRS) redis.c
+spf.so: $(HDRS) spf.c
+sqlite.so: $(HDRS) sqlite.c
+testdb.so: $(HDRS) testdb.c
+whoson.so: $(HDRS) whoson.c
+
+# End
diff --git a/src/lookups/README b/src/lookups/README
new file mode 100644
index 0000000..2e87eda
--- /dev/null
+++ b/src/lookups/README
@@ -0,0 +1,181 @@
+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. Traditionally, lookup_querystyle does not use this (just returning a
+dummy value, and doing the "open" work in the xxx_find() routine); but this is
+not enforced by the framework.
+
+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
+ uint *do_cache the lookup should set this to 0 when it changes data.
+ This is MAXINT by default. When set to 0 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.
+ If set to a nonzero number of seconds, the cached value
+ becomes unusable after this time. Currently the dnsdb
+ lookup uses this to support the TTL value.
+ uschar *opts options, a comma-separated list of tagged values for
+ modifying the search operation
+
+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_version_report()
+--------------------
+
+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
index 0000000..966078f
--- /dev/null
+++ b/src/lookups/cdb.c
@@ -0,0 +1,490 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/*
+ * Exim - CDB database lookup module
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * Copyright (c) The Exim Maintainers 2020 - 2022
+ * 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
+ * --------------------------------------------------------------
+ * Modified by The Exim Maintainers 2015:
+ * const propagation
+ * --------------------------------------------------------------
+ *
+ * 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 variants 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(const 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 independent) */
+
+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(const uschar * filename, uschar ** errmsg)
+{
+int fileno;
+struct cdb_state *cdbp;
+struct stat statbuf;
+void * mapbuf;
+
+if ((fileno = Uopen(filename, O_RDONLY, 0)) < 0)
+ {
+ *errmsg = string_open_failed("%s for cdb lookup", filename);
+ return NULL;
+ }
+
+if (fstat(fileno, &statbuf) != 0)
+ {
+ *errmsg = string_open_failed("fstat(%s) failed - cannot do cdb lookup",
+ filename);
+ return NULL;
+ }
+
+/* If this is a valid file, then it *must* be at least
+CDB_HASH_TABLE bytes long */
+
+if (statbuf.st_size < CDB_HASH_TABLE)
+ {
+ *errmsg = string_open_failed("%s too short for cdb lookup", filename);
+ return NULL;
+ }
+
+/* Having got a file open we need the structure to put things in */
+cdbp = store_get(sizeof(struct cdb_state), GET_UNTAINTED);
+/* 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
+if ((mapbuf = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fileno, 0))
+ != 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);
+ }
+
+/* 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_indent("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, GET_UNTAINTED);
+
+/* 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("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, const 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, const uschar * filename, const uschar * keystring,
+ int key_len, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+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;
+
+key_hash = cdb_hash(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_indent("%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 (int 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. Assume it is not
+ tainted, lacking any way of telling. */
+
+ *result = store_get(item_dat_len + 1, GET_UNTAINTED);
+ 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 (int 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 */
+ rmark reset_point = store_mark();
+ uschar * item_key = store_get(key_len, GET_TAINTED); /* keys liable to be tainted */
+
+ 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(reset_point);
+
+ /* matches - get data length */
+ item_dat_len = cdb_unpack(packbuf + 4);
+
+ /* then we build a new result string. We know we have enough
+ memory so disable Coverity errors about the tainted item_dat_ken */
+
+ *result = store_get(item_dat_len + 1, GET_UNTAINTED);
+ /* coverity[tainted_data] */
+ if (cdb_bread(cdbp->fileno, *result, item_dat_len) == -1)
+ return DEFER;
+
+ /* coverity[tainted_data] */
+ (*result)[item_dat_len] = 0;
+ return OK;
+ }
+ /* Reclaim some store */
+ store_reset(reset_point);
+ }
+ }
+ 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"
+
+gstring *
+cdb_version_report(gstring * g)
+{
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, "Library version: CDB: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+
+lookup_info cdb_lookup_info = {
+ .name = US"cdb", /* lookup name */
+ .type = lookup_absfile, /* absolute file name */
+ .open = cdb_open, /* open function */
+ .check = cdb_check, /* check function */
+ .find = cdb_find, /* find function */
+ .close = cdb_close, /* close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = 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
index 0000000..32514af
--- /dev/null
+++ b/src/lookups/dbmdb.c
@@ -0,0 +1,284 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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(const uschar * filename, uschar ** errmsg)
+{
+uschar * dirname = string_copy(filename);
+uschar * s;
+EXIM_DB * yield = NULL;
+
+if ((s = Ustrrchr(dirname, '/'))) *s = '\0';
+if (!(yield = exim_dbopen(filename, dirname, O_RDONLY, 0)))
+ *errmsg = string_open_failed("%s as a %s file", filename, EXIM_DBTYPE);
+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, const uschar *filename, int modemask, uid_t *owners,
+ gid_t *owngroups, uschar **errmsg)
+{
+int rc;
+
+#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, const uschar * filename, const uschar * keystring,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+EXIM_DB *d = (EXIM_DB *)handle;
+EXIM_DATUM key, data;
+
+exim_datum_init(&key); /* Some DBM libraries require datums to */
+exim_datum_init(&data); /* be cleared before use. */
+length++;
+exim_datum_data_set(&key,
+ memcpy(store_get(length, keystring), keystring, length)); /* key can have embedded NUL */
+exim_datum_size_set(&key, length);
+
+if (exim_dbget(d, &key, &data))
+ {
+ *result = string_copyn(exim_datum_data_get(&data), exim_datum_size_get(&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 */
+
+static int
+dbmnz_find(void * handle, const uschar * filename, const uschar * keystring,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+return dbmdb_find(handle, filename, keystring, length-1, result, errmsg,
+ do_cache, opts);
+}
+
+
+
+/*************************************************
+* 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, const uschar * filename, const uschar * keystring,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+uschar *key_item, *key_buffer, *key_p;
+const 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, keystring);
+
+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_indent("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, opts);
+}
+
+
+
+/*************************************************
+* 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"
+
+gstring *
+dbm_version_report(gstring * g)
+{
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, "Library version: DBM: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+
+lookup_info dbm_lookup_info = {
+ .name = US"dbm", /* lookup name */
+ .type = lookup_absfile, /* uses absolute file name */
+ .open = dbmdb_open, /* open function */
+ .check = dbmdb_check, /* check function */
+ .find = dbmdb_find, /* find function */
+ .close = dbmdb_close, /* close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = dbm_version_report /* version reporting */
+};
+
+lookup_info dbmz_lookup_info = {
+ .name = US"dbmnz", /* lookup name */
+ .type = lookup_absfile, /* uses absolute file name */
+ .open = dbmdb_open, /* sic */ /* open function */
+ .check = dbmdb_check, /* sic */ /* check function */
+ .find = dbmnz_find, /* find function */
+ .close = dbmdb_close, /* sic */ /* close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = NULL /* no version reporting (redundant) */
+};
+
+lookup_info dbmjz_lookup_info = {
+ .name = US"dbmjz", /* lookup name */
+ .type = lookup_absfile, /* uses absolute file name */
+ .open = dbmdb_open, /* sic */ /* open function */
+ .check = dbmdb_check, /* sic */ /* check function */
+ .find = dbmjz_find, /* find function */
+ .close = dbmdb_close, /* sic */ /* close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = 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
index 0000000..355be1b
--- /dev/null
+++ b/src/lookups/dnsdb.c
@@ -0,0 +1,614 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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",
+#endif
+ "cname",
+ "csa",
+ "mx",
+ "mxh",
+ "ns",
+ "ptr",
+ "soa",
+ "spf",
+ "srv",
+ "tlsa",
+ "txt",
+ "zns"
+};
+
+static int type_values[] = {
+ T_A,
+#if HAVE_IPV6
+ T_ADDRESSES, /* Private type for AAAA + A */
+ T_AAAA,
+#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_SOA,
+ 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(const uschar * filename, uschar **errmsg)
+{
+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) Options, all comma-terminated, in any order. Any unrecognised option
+terminates option processing. Recognised options are:
+
+- 'defer_FOO': set the defer behaviour 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'.
+
+- 'dnssec_FOO', with 'strict', 'lax' (default), and 'never'. The meanings are
+require, try and don't-try dnssec respectively.
+
+- 'retrans_VAL', set the timeout value. VAL is an Exim time specification
+(eg "5s"). The default is set by the main configuration option 'dns_retrans'.
+
+- 'retry_VAL', set the retry count on timeouts. VAL is an integer. The
+default is set by the main configuration option "dns_retry".
+
+(d) 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".
+
+(e) 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, const uschar * filename, const uschar * keystring,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+int rc;
+int sep = 0;
+int defer_mode = PASS;
+int dnssec_mode = PASS;
+int save_retrans = dns_retrans;
+int save_retry = dns_retry;
+int type;
+int failrc = FAIL;
+const uschar *outsep = CUS"\n";
+const uschar *outsep2 = NULL;
+uschar *equals, *domain, *found;
+
+dns_answer * dnsa = store_get_dns_answer();
+dns_scan dnss;
+
+/* Because we're working in the search pool, we try to reclaim as much
+store as possible later, so we preallocate the result here */
+
+gstring * yield = string_get(256);
+
+/* 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. */
+
+for (;;)
+ {
+ 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";
+ rc = DEFER;
+ goto out;
+ }
+ }
+ else if (strncmpic(keystring, US"dnssec_", 7) == 0)
+ {
+ 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";
+ rc = DEFER;
+ goto out;
+ }
+ }
+ else if (strncmpic(keystring, US"retrans_", 8) == 0)
+ {
+ int timeout_sec;
+ if ((timeout_sec = readconf_readtime(keystring += 8, ',', FALSE)) <= 0)
+ {
+ *errmsg = US"unsupported dnsdb timeout value";
+ rc = DEFER;
+ goto out;
+ }
+ dns_retrans = timeout_sec;
+ while (*keystring != ',') keystring++;
+ }
+ else if (strncmpic(keystring, US"retry_", 6) == 0)
+ {
+ int retries;
+ if ((retries = (int)strtol(CCS keystring + 6, CSS &keystring, 0)) < 0)
+ {
+ *errmsg = US"unsupported dnsdb retry count";
+ rc = DEFER;
+ goto out;
+ }
+ dns_retry = retries;
+ }
+ else
+ break;
+
+ while (isspace(*keystring)) keystring++;
+ if (*keystring++ != ',')
+ {
+ *errmsg = US"dnsdb modifier syntax error";
+ rc = DEFER;
+ goto out;
+ }
+ 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 < nelem(type_names); i++)
+ if (len == Ustrlen(type_names[i]) &&
+ strncmpic(keystring, US type_names[i], len) == 0)
+ {
+ type = type_values[i];
+ break;
+ }
+
+ if (i >= nelem(type_names))
+ {
+ *errmsg = US"unsupported DNS record type";
+ rc = DEFER;
+ goto out;
+ }
+
+ 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.
+MX priority and value are space-separated by default.
+SRV and TLSA record parts are space-separated by default. */
+
+if (!outsep2) switch(type)
+ {
+ case T_SPF: outsep2 = US""; break;
+ case T_SRV: case T_MX: case T_TLSA: outsep2 = US" "; break;
+ }
+
+/* Now scan the list and do a lookup for each item */
+
+while ((domain = string_nextinlist(&keystring, &sep, NULL, 0)))
+ {
+ 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)
+ domain = dns_build_reverse(domain);
+
+ do
+ {
+ DEBUG(D_lookup) debug_printf_indent("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) searchtype = T_AAAA;
+ else if (searchtype == T_AAAA) searchtype = T_A;
+ rc = dns_special_lookup(dnsa, domain, searchtype, CUSS &found);
+ }
+ else
+#endif
+ rc = dns_special_lookup(dnsa, domain, type, CUSS &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_retrans = save_retrans;
+ dns_retry = save_retry;
+ dns_init(FALSE, FALSE, FALSE); /* clr dnssec bit */
+ rc = DEFER; /* always defer */
+ goto out;
+ }
+ if (defer_mode == PASS) failrc = DEFER; /* defer only if all do */
+ continue; /* treat defer as fail */
+ }
+
+
+ /* Search the returned records */
+
+ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)) if (rr->type == searchtype)
+ {
+ if (*do_cache > rr->ttl)
+ *do_cache = rr->ttl;
+
+ if (type == T_A || type == T_AAAA || type == T_ADDRESSES)
+ {
+ for (dns_address * da = dns_address_from_rr(dnsa, rr); da; da = da->next)
+ {
+ if (yield->ptr) yield = string_catn(yield, outsep, 1);
+ yield = string_cat(yield, da->address);
+ }
+ continue;
+ }
+
+ /* Other kinds of record just have one piece of data each, but there may be
+ several of them, of course. */
+
+ if (yield->ptr) yield = string_catn(yield, outsep, 1);
+
+ if (type == T_TXT || type == T_SPF)
+ {
+ if (outsep2 == NULL) /* output only the first item of data */
+ yield = string_catn(yield, US (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_catn(yield, outsep2, 1);
+ yield = string_catn(yield, US ((rr->data)+data_offset), chunk_len);
+ data_offset += chunk_len;
+ }
+ }
+ }
+ else if (type == T_TLSA)
+ {
+ uint8_t usage, selector, matching_type;
+ uint16_t payload_length;
+ uschar s[MAX_TLSA_EXPANDED_SIZE];
+ uschar * sp = s;
+ uschar * p = US 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%c%d%c%d%c", usage, *outsep2,
+ selector, *outsep2, matching_type, *outsep2);
+ /* Now append the cert/identifier, one hex char at a time */
+ while (payload_length-- > 0 && sp-s < (MAX_TLSA_EXPANDED_SIZE - 4))
+ sp += sprintf(CS sp, "%02x", *p++);
+
+ yield = string_cat(yield, s);
+ }
+ else /* T_CNAME, T_CSA, T_MX, T_MXH, T_NS, T_PTR, T_SOA, T_SRV */
+ {
+ int priority, weight, port;
+ uschar s[264];
+ uschar * p = US rr->data;
+
+ switch (type)
+ {
+ case T_MXH:
+ /* mxh ignores the priority number and includes only the hostnames */
+ GETSHORT(priority, p);
+ break;
+
+ case T_MX:
+ GETSHORT(priority, p);
+ sprintf(CS s, "%d%c", priority, *outsep2);
+ yield = string_cat(yield, s);
+ break;
+
+ case T_SRV:
+ GETSHORT(priority, p);
+ GETSHORT(weight, p);
+ GETSHORT(port, p);
+ sprintf(CS s, "%d%c%d%c%d%c", priority, *outsep2,
+ weight, *outsep2, port, *outsep2);
+ yield = string_cat(yield, s);
+ break;
+
+ case 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 (Ustrcmp(found, domain) != 0)
+ {
+ 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_catn(yield, s, 2);
+ break;
+
+ default:
+ break;
+ }
+
+ /* 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, s);
+
+ if (type == T_SOA && outsep2 != NULL)
+ {
+ unsigned long serial, refresh, retry, expire, minimum;
+
+ p += rc;
+ yield = string_catn(yield, outsep2, 1);
+
+ rc = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen, p,
+ (DN_EXPAND_ARG4_TYPE)s, sizeof(s));
+ if (rc < 0)
+ {
+ log_write(0, LOG_MAIN, "responsible-mailbox truncated: type=%s "
+ "domain=%s", dns_text_type(type), domain);
+ break;
+ }
+ else yield = string_cat(yield, s);
+
+ p += rc;
+ GETLONG(serial, p); GETLONG(refresh, p);
+ GETLONG(retry, p); GETLONG(expire, p); GETLONG(minimum, p);
+ sprintf(CS s, "%c%lu%c%lu%c%lu%c%lu%c%lu",
+ *outsep2, serial, *outsep2, refresh,
+ *outsep2, retry, *outsep2, expire, *outsep2, minimum);
+ yield = string_cat(yield, s);
+ }
+ }
+ } /* Loop for list of returned records */
+
+ /* Loop for set of A-lookup types */
+ } while (type == T_ADDRESSES && searchtype != T_A);
+
+ } /* Loop for list of domains */
+
+/* Reclaim unused memory */
+
+gstring_release_unused(yield);
+
+/* If yield NULL we have not found anything. Otherwise, insert the terminating
+zero and return the result. */
+
+dns_retrans = save_retrans;
+dns_retry = save_retry;
+dns_init(FALSE, FALSE, FALSE); /* clear the dnssec bit for getaddrbyname */
+
+if (!yield || !yield->ptr)
+ rc = failrc;
+else
+ {
+ *result = string_from_gstring(yield);
+ rc = OK;
+ }
+
+out:
+
+store_free_dns_answer(dnsa);
+return rc;
+}
+
+
+
+/*************************************************
+* Version reporting entry point *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+gstring *
+dnsdb_version_report(gstring * g)
+{
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, "Library version: DNSDB: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+
+static lookup_info _lookup_info = {
+ .name = US"dnsdb", /* lookup name */
+ .type = lookup_querystyle, /* query style */
+ .open = dnsdb_open, /* open function */
+ .check = NULL, /* check function */
+ .find = dnsdb_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = 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
index 0000000..a769102
--- /dev/null
+++ b/src/lookups/dsearch.c
@@ -0,0 +1,190 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* 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(const uschar * dirname, uschar ** errmsg)
+{
+DIR * dp = exim_opendir(dirname);
+if (!dp)
+ {
+ *errmsg = string_open_failed("%s for directory search", dirname);
+ 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. */
+
+static BOOL
+dsearch_check(void * handle, const uschar * filename, int modemask,
+ uid_t * owners, gid_t * owngroups, uschar ** errmsg)
+{
+handle = handle;
+if (*filename == '/')
+ return lf_check_file(-1, filename, S_IFDIR, modemask, owners, owngroups,
+ "dsearch", errmsg) == 0;
+*errmsg = string_sprintf("dirname '%s' for dsearch is not absolute", filename);
+return FALSE;
+}
+
+
+/*************************************************
+* Find entry point *
+*************************************************/
+
+#define RET_FULL BIT(0)
+#define FILTER_TYPE BIT(1)
+#define FILTER_ALL BIT(1)
+#define FILTER_FILE BIT(2)
+#define FILTER_DIR BIT(3)
+#define FILTER_SUBDIR BIT(4)
+
+/* 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. */
+
+static int
+dsearch_find(void * handle, const uschar * dirname, const uschar * keystring,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+struct stat statbuf;
+int save_errno;
+uschar * filename;
+unsigned flags = 0;
+
+if (Ustrchr(keystring, '/') != 0)
+ {
+ *errmsg = string_sprintf("key for dsearch lookup contains a slash: %s",
+ keystring);
+ return DEFER;
+ }
+
+if (opts)
+ {
+ int sep = ',';
+ uschar * ele;
+
+ while ((ele = string_nextinlist(&opts, &sep, NULL, 0)))
+ if (Ustrcmp(ele, "ret=full") == 0)
+ flags |= RET_FULL;
+ else if (Ustrncmp(ele, "filter=", 7) == 0)
+ {
+ ele += 7;
+ if (Ustrcmp(ele, "file") == 0)
+ flags |= FILTER_TYPE | FILTER_FILE;
+ else if (Ustrcmp(ele, "dir") == 0)
+ flags |= FILTER_TYPE | FILTER_DIR;
+ else if (Ustrcmp(ele, "subdir") == 0)
+ flags |= FILTER_TYPE | FILTER_SUBDIR; /* like dir but not "." or ".." */
+ }
+ }
+
+filename = string_sprintf("%s/%s", dirname, keystring);
+if ( Ulstat(filename, &statbuf) >= 0
+ && ( !(flags & FILTER_TYPE)
+ || (flags & FILTER_FILE && S_ISREG(statbuf.st_mode))
+ || ( flags & (FILTER_DIR | FILTER_SUBDIR)
+ && S_ISDIR(statbuf.st_mode)
+ && ( flags & FILTER_DIR
+ || keystring[0] != '.'
+ || keystring[1] && keystring[1] != '.'
+ ) ) ) )
+ {
+ /* Since the filename exists in the filesystem, we can return a
+ non-tainted result. */
+ *result = string_copy_taint(flags & RET_FULL ? filename : keystring, GET_UNTAINTED);
+ return OK;
+ }
+
+if (errno == ENOENT || errno == 0) return FAIL;
+
+save_errno = errno;
+*errmsg = string_sprintf("%s: lstat: %s", filename, strerror(errno));
+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"
+
+gstring *
+dsearch_version_report(gstring * g)
+{
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, "Library version: dsearch: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+
+static lookup_info _lookup_info = {
+ .name = US"dsearch", /* lookup name */
+ .type = lookup_absfile, /* uses absolute file name */
+ .open = dsearch_open, /* open function */
+ .check = dsearch_check, /* check function */
+ .find = dsearch_find, /* find function */
+ .close = dsearch_close, /* close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = 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
index 0000000..c4fff71
--- /dev/null
+++ b/src/lookups/ibase.c
@@ -0,0 +1,561 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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(const 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_indent("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;
+int i;
+rmark reset_point;
+
+char buffer[256];
+ISC_STATUS status[20], *statusp = status;
+
+gstring * result;
+int yield = DEFER;
+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 (int 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)
+ {
+ 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_indent("Interbase using cached connection for %s\n",
+ server_copy);
+ }
+else
+ {
+ cn = store_get(sizeof(ibase_connection), GET_UNTAINTED);
+ 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;
+ 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 (char * p = sdata[1]; *p;)
+ *dpb++ = *p++;
+ *dpb++ = isc_dpb_password;
+ *dpb++ = strlen(sdata[2]);
+ for (char * p = sdata[2]; *p;)
+ *dpb++ = *p++;
+ dpb_length = dpb - buffer;
+
+ DEBUG(D_lookup)
+ debug_printf_indent("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;
+ }
+
+/* Lacking any information, assume that the data is untainted */
+reset_point = store_mark();
+out_sqlda = store_get(XSQLDA_LENGTH(1), GET_UNTAINTED);
+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);
+ reset_point = store_reset(reset_point);
+ 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), GET_UNTAINTED);
+ if (isc_dsql_describe
+ (status, &stmth, out_sqlda->version, new_sqlda))
+ {
+ isc_interprete(buffer, &statusp);
+ isc_dsql_free_statement(status, &stmth, DSQL_drop);
+ reset_point = store_reset(reset_point);
+ 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 = CS store_get(sizeof(char) * var->sqllen + 2, GET_UNTAINTED);
+ break;
+ case SQL_TEXT:
+ var->sqldata = CS store_get(sizeof(char) * var->sqllen, GET_UNTAINTED);
+ break;
+ case SQL_SHORT:
+ var->sqldata = CS store_get(sizeof(short), GET_UNTAINTED);
+ break;
+ case SQL_LONG:
+ var->sqldata = CS store_get(sizeof(ISC_LONG), GET_UNTAINTED);
+ break;
+#ifdef SQL_INT64
+ case SQL_INT64:
+ var->sqldata = CS store_get(sizeof(ISC_INT64), GET_UNTAINTED);
+ break;
+#endif
+ case SQL_FLOAT:
+ var->sqldata = CS store_get(sizeof(float), GET_UNTAINTED);
+ break;
+ case SQL_DOUBLE:
+ var->sqldata = CS store_get(sizeof(double), GET_UNTAINTED);
+ break;
+#ifdef SQL_TIMESTAMP
+ case SQL_DATE:
+ var->sqldata = CS store_get(sizeof(ISC_QUAD), GET_UNTAINTED);
+ break;
+#else
+ case SQL_TIMESTAMP:
+ var->sqldata = CS store_get(sizeof(ISC_TIMESTAMP), GET_UNTAINTED);
+ break;
+ case SQL_TYPE_DATE:
+ var->sqldata = CS store_get(sizeof(ISC_DATE), GET_UNTAINTED);
+ break;
+ case SQL_TYPE_TIME:
+ var->sqldata = CS store_get(sizeof(ISC_TIME), GET_UNTAINTED);
+ break;
+ #endif
+ }
+ if (var->sqltype & 1)
+ var->sqlind = (short *) store_get(sizeof(short), GET_UNTAINTED);
+ }
+
+/* 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)
+ result = string_catn(result, 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_catn(result, US buffer,
+ fetch_field(buffer, sizeof(buffer),
+ &out_sqlda->sqlvar[0]));
+ }
+
+ else
+ for (int i = 0; i < out_sqlda->sqld; i++)
+ {
+ int len = fetch_field(buffer, sizeof(buffer), &out_sqlda->sqlvar[i]);
+
+ result = string_catn(result, US out_sqlda->sqlvar[i].aliasname,
+ out_sqlda->sqlvar[i].aliasname_length);
+ result = string_catn(result, US "=", 1);
+
+ /* Quote the value if it contains spaces or is empty */
+
+ if (*out_sqlda->sqlvar[i].sqlind == -1) /* NULL value */
+ result = string_catn(result, US "\"\"", 2);
+
+ else if (buffer[0] == 0 || Ustrchr(buffer, ' ') != NULL)
+ {
+ result = string_catn(result, US "\"", 1);
+ for (int j = 0; j < len; j++)
+ {
+ if (buffer[j] == '\"' || buffer[j] == '\\')
+ result = string_cat(result, US "\\", 1);
+ result = string_cat(result, US buffer + j, 1);
+ }
+ result = string_catn(result, US "\"", 1);
+ }
+ else
+ result = string_catn(result, US buffer, len);
+ result = string_catn(result, 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)
+ {
+ yield = FAIL;
+ *errmsg = US "Interbase: no data found";
+ }
+else
+ gstring_release_unused(result);
+
+
+/* Get here by goto from various error checks. */
+
+IBASE_EXIT:
+
+if (stmth)
+ isc_dsql_free_statement(status, &stmth, DSQL_drop);
+
+/* Non-NULL result indicates a successful result */
+
+if (result)
+ {
+ *resultptr = string_from_gstring(result);
+ return OK;
+ }
+else
+ {
+ DEBUG(D_lookup) debug_printf_indent("%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, const uschar * filename, uschar * query, int length,
+ uschar ** result, uschar ** errmsg, uint * do_cache, const uschar * opts)
+{
+int sep = 0;
+uschar *server;
+uschar *list = ibase_servers;
+
+DEBUG(D_lookup) debug_printf_indent("Interbase query: %s\n", query);
+
+while ((server = string_nextinlist(&list, &sep, NULL, 0)))
+ {
+ 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)
+ *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
+ idx lookup type index
+
+Returns: the processed string or NULL for a bad option
+*/
+
+static uschar *
+ibase_quote(uschar * s, uschar * opt, unsigned idx)
+{
+int c;
+int count = 0;
+uschar * t = s, * quoted;
+
+if (opt)
+ return NULL; /* No options recognized */
+
+while ((c = *t++))
+ if (c == '\'') count++;
+
+t = quoted = store_get_quoted(Ustrlen(s) + count + 1, s, idx);
+
+while ((c = *s++))
+ if (c == '\'') { *t++ = '\''; *t++ = '\''; }
+ else *t++ = c;
+
+*t = 0;
+return quoted;
+}
+
+
+/*************************************************
+* Version reporting entry point *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+gstring *
+ibase_version_report(gstring * g)
+{
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, "Library version: ibase: Exim version %s\n", EXIM_VERSION_STR));
+#endif
+return g;
+}
+
+
+static lookup_info _lookup_info = {
+ .name = US"ibase", /* lookup name */
+ .type = lookup_querystyle, /* query-style lookup */
+ .open = ibase_open, /* open function */
+ .check NULL, /* no check function */
+ .find = ibase_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = ibase_tidy, /* tidy function */
+ .quote = ibase_quote, /* quoting function */
+ .version_report = 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/json.c b/src/lookups/json.c
new file mode 100644
index 0000000..c9abf8c
--- /dev/null
+++ b/src/lookups/json.c
@@ -0,0 +1,188 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/* Copyright (c) Jeremy Harris 2019 - 2020 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "lf_functions.h"
+#include <jansson.h>
+
+
+
+/* All use of allocations will be done against the POOL_SEARCH memory,
+which is freed once by search_tidyup(). Make the free call a dummy.
+This burns some 300kB in handling a 37kB JSON file, for the benefit of
+a fast free. The alternative of staying with malloc is nearly as bad,
+eyeballing the activity there are 20% the number of free vs. alloc
+calls (before the big bunch at the end).
+
+Assume that the file is trusted, so no tainting */
+
+static void *
+json_malloc(size_t nbytes)
+{
+void * p = store_get((int)nbytes, GET_UNTAINTED);
+/* debug_printf("%s %d: %p\n", __FUNCTION__, (int)nbytes, p); */
+return p;
+}
+static void
+json_free(void * p)
+{
+/* debug_printf("%s: %p\n", __FUNCTION__, p); */
+}
+
+/*************************************************
+* Open entry point *
+*************************************************/
+
+/* See local README for interface description */
+
+static void *
+json_open(const uschar * filename, uschar ** errmsg)
+{
+FILE * f;
+
+json_set_alloc_funcs(json_malloc, json_free);
+
+if (!(f = Ufopen(filename, "rb")))
+ *errmsg = string_open_failed("%s for json search", filename);
+return f;
+}
+
+
+
+/*************************************************
+* Check entry point *
+*************************************************/
+
+static BOOL
+json_check(void *handle, const 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, "json", errmsg) == 0;
+}
+
+
+
+/*************************************************
+* Find entry point for lsearch *
+*************************************************/
+
+/* See local README for interface description */
+
+static int
+json_find(void * handle, const uschar * filename, const uschar * keystring,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+FILE * f = handle;
+json_t * j, * j0;
+json_error_t jerr;
+uschar * key;
+int sep = 0;
+
+rewind(f);
+if (!(j = json_loadf(f, 0, &jerr)))
+ {
+ *errmsg = string_sprintf("json error on open: %.*s\n",
+ JSON_ERROR_TEXT_LENGTH, jerr.text);
+ return FAIL;
+ }
+j0 = j;
+
+for (int k = 1; (key = string_nextinlist(&keystring, &sep, NULL, 0)); k++)
+ {
+ BOOL numeric = TRUE;
+ for (uschar * s = key; *s; s++) if (!isdigit(*s)) { numeric = FALSE; break; }
+
+ if (!(j = numeric
+ ? json_array_get(j, (size_t) strtoul(CS key, NULL, 10))
+ : json_object_get(j, CCS key)
+ ) )
+ {
+ DEBUG(D_lookup) debug_printf_indent("%s, for key %d: '%s'\n",
+ numeric
+ ? US"bad index, or not json array"
+ : US"no such key, or not json object",
+ k, key);
+ json_decref(j0);
+ return FAIL;
+ }
+ }
+
+switch (json_typeof(j))
+ {
+ case JSON_STRING:
+ *result = string_copyn(CUS json_string_value(j), json_string_length(j));
+ break;
+ case JSON_INTEGER:
+ *result = string_sprintf("%" JSON_INTEGER_FORMAT, json_integer_value(j));
+ break;
+ case JSON_REAL:
+ *result = string_sprintf("%f", json_real_value(j));
+ break;
+ case JSON_TRUE: *result = US"true"; break;
+ case JSON_FALSE: *result = US"false"; break;
+ case JSON_NULL: *result = NULL; break;
+ default: *result = US json_dumps(j, 0); break;
+ }
+json_decref(j0);
+return OK;
+}
+
+
+
+/*************************************************
+* Close entry point *
+*************************************************/
+
+/* See local README for interface description */
+
+static void
+json_close(void *handle)
+{
+(void)fclose((FILE *)handle);
+}
+
+
+
+/*************************************************
+* Version reporting entry point *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+gstring *
+json_version_report(gstring * g)
+{
+return string_fmt_append(g, "Library version: json: Jansonn version %s\n", JANSSON_VERSION);
+}
+
+
+static lookup_info json_lookup_info = {
+ .name = US"json", /* lookup name */
+ .type = lookup_absfile, /* uses absolute file name */
+ .open = json_open, /* open function */
+ .check = json_check, /* check function */
+ .find = json_find, /* find function */
+ .close = json_close, /* close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = json_version_report /* version reporting */
+};
+
+
+#ifdef DYNLOOKUP
+#define json_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &json_lookup_info };
+lookup_module_info json_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+/* End of lookups/json.c */
diff --git a/src/lookups/ldap.c b/src/lookups/ldap.c
new file mode 100644
index 0000000..9751fa3
--- /dev/null
+++ b/src/lookups/ldap.c
@@ -0,0 +1,1613 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Many thanks to Stuart Lynne for contributing the original code for this
+driver. Further contributions 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(const 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;
+
+gstring * 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 attrs_requested = 0;
+int error_yield = DEFER;
+int msgid;
+int rc, ldap_rc, ldap_parse_rc;
+int port;
+int rescount = 0;
+BOOL attribute_found = FALSE;
+BOOL ldapi = FALSE;
+
+DEBUG(D_lookup) debug_printf_indent("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 || !ludp->lud_host[0]) && server)
+ {
+ host = server;
+ port = s_port;
+ }
+else
+ {
+ host = US ludp->lud_host;
+ if (host && !host[0]) host = NULL;
+ port = ludp->lud_port;
+ }
+
+DEBUG(D_lookup) debug_printf_indent("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)
+ {
+ 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 (uschar ** attrp = USS ludp->lud_attrs; attrp && *attrp; attrp++)
+ attrs_requested++;
+
+/* 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; 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)
+ {
+ 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 ? host : US"";
+ rmark reset_point = store_mark();
+ gstring * g;
+
+ /* Handle connection via Unix socket ("ldapi"). We build a basic LDAP URI to
+ contain the path name, with slashes escaped as %2F. */
+
+ if (ldapi)
+ {
+ g = string_catn(NULL, US"ldapi://", 8);
+ for (uschar ch; (ch = *shost); shost++)
+ g = ch == '/' ? string_catn(g, US"%2F", 3) : string_catn(g, shost, 1);
+ }
+
+ /* This is not an ldapi call. Just build a URI with the protocol type, host
+ name, and port. */
+
+ else
+ {
+ uschar * init_ptr = Ustrchr(ldap_url, '/');
+ g = string_catn(NULL, ldap_url, init_ptr - ldap_url);
+ g = string_fmt_append(g, "//%s:%d/", shost, port);
+ }
+ string_from_gstring(g);
+
+ /* Call ldap_initialize() and check the result */
+
+ DEBUG(D_lookup) debug_printf_indent("ldap_initialize with URL %s\n", g->s);
+ if ((rc = ldap_initialize(&ld, CS g->s)) != LDAP_SUCCESS)
+ {
+ *errmsg = string_sprintf("ldap_initialize: (error %d) URL \"%s\"\n",
+ rc, g->s);
+ goto RETURN_ERROR;
+ }
+ store_reset(reset_point); /* 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)
+ {
+ *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_indent("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)
+ {
+ tls_option =
+ Ustrcmp(eldap_require_cert, "hard") == 0 ? LDAP_OPT_X_TLS_HARD
+ : Ustrcmp(eldap_require_cert, "demand") == 0 ? LDAP_OPT_X_TLS_DEMAND
+ : Ustrcmp(eldap_require_cert, "allow") == 0 ? LDAP_OPT_X_TLS_ALLOW
+ : Ustrcmp(eldap_require_cert, "try") == 0 ? LDAP_OPT_X_TLS_TRY
+ : LDAP_OPT_X_TLS_NEVER;
+
+ DEBUG(D_lookup) debug_printf_indent(
+ "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_indent("LDAP_OPT_X_TLS_HARD set due to ldaps:// URI\n");
+ }
+ else
+ {
+ tls_option = LDAP_OPT_X_TLS_TRY;
+ DEBUG(D_lookup)
+ debug_printf_indent("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)
+ 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)
+ ldap_set_option(ldsetctx, LDAP_OPT_X_TLS_CACERTDIR, eldap_ca_cert_dir);
+#endif
+#ifdef LDAP_OPT_X_TLS_CERTFILE
+ if (eldap_cert_file)
+ ldap_set_option(ldsetctx, LDAP_OPT_X_TLS_CERTFILE, eldap_cert_file);
+#endif
+#ifdef LDAP_OPT_X_TLS_KEYFILE
+ if (eldap_cert_key)
+ ldap_set_option(ldsetctx, LDAP_OPT_X_TLS_KEYFILE, eldap_cert_key);
+#endif
+#ifdef LDAP_OPT_X_TLS_CIPHER_SUITE
+ if (eldap_cipher_suite)
+ 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)
+ {
+ int cert_option =
+ Ustrcmp(eldap_require_cert, "hard") == 0 ? LDAP_OPT_X_TLS_HARD
+ : Ustrcmp(eldap_require_cert, "demand") == 0 ? LDAP_OPT_X_TLS_DEMAND
+ : Ustrcmp(eldap_require_cert, "allow") == 0 ? LDAP_OPT_X_TLS_ALLOW
+ : Ustrcmp(eldap_require_cert, "try") == 0 ? LDAP_OPT_X_TLS_TRY
+ : LDAP_OPT_X_TLS_NEVER;
+
+ /* 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_indent("Unable to set TLS require cert_option(%d) globally: %s\n",
+ cert_option, ldap_err2string(rc));
+ }
+#endif
+#ifdef LDAP_OPT_X_TLS_NEWCTX
+ if ((rc = ldap_set_option(ldsetctx, LDAP_OPT_X_TLS_NEWCTX, &am_server)))
+ DEBUG(D_lookup)
+ debug_printf_indent("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), GET_UNTAINTED);
+ lcp->host = host ? string_copy(host) : NULL;
+ 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_indent("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 && user
+ || lcp->user && !user
+ || lcp->user && user && Ustrcmp(lcp->user, user) != 0
+ || !lcp->password && password
+ || lcp->password && !password
+ || lcp->password && password && Ustrcmp(lcp->password, password) != 0
+ )
+ {
+ DEBUG(D_lookup) debug_printf_indent("%sbinding with user=%s password=%s\n",
+ lcp->bound ? "re-" : "", user, password);
+
+ if (eldap_start_tls && !lcp->is_start_tls_called && !ldapi)
+ {
+#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_indent("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_indent("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 : string_copy(user);
+ lcp->password = !password ? 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_indent("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_indent("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;
+ int valuecount; /* We can see an attr spread across several
+ entries. If B is derived from A and we request
+ A and the directory contains both, A and B,
+ then we get two entries, one for A and one for B.
+ Here we just count the values per entry */
+
+ DEBUG(D_lookup) debug_printf_indent("LDAP result loop\n");
+
+ for(e = ldap_first_entry(lcp->ld, result), valuecount = 0;
+ e;
+ e = ldap_next_entry(lcp->ld, e))
+ {
+ uschar *new_dn;
+ BOOL insert_space = FALSE;
+
+ DEBUG(D_lookup) debug_printf_indent("LDAP entry loop\n");
+
+ rescount++; /* Count results */
+
+ /* Results for multiple entries values are separated by newlines. */
+
+ if (data) data = string_catn(data, US"\n", 1);
+
+ /* Get the DN from the last result. */
+
+ if ((new_dn = US ldap_get_dn(lcp->ld, e)))
+ {
+ if (dn)
+ {
+#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) /* below, that's for the first only */
+ {
+ data = string_cat(data, new_dn);
+ (void) string_from_gstring(data);
+ 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).
+ If more than one attribute is being retrieved, the data is given as a
+ sequence of name=value pairs, separated by (space), with the value always in quotes.
+ If there are multiple values, they are given within the quotes, comma separated. */
+
+ else for (uschar * attr = US ldap_first_attribute(lcp->ld, e, &ber);
+ attr; attr = US ldap_next_attribute(lcp->ld, e, ber))
+ {
+ DEBUG(D_lookup) debug_printf_indent("LDAP attr loop\n");
+
+ /* In case of attrs_requested == 1 we just count the values, in all other cases
+ (0, >1) we count the values per attribute */
+ if (attrs_requested != 1) valuecount = 0;
+
+ if (attr[0] != 0)
+ {
+ /* Get array of values for this attribute. */
+
+ if ((firstval = values = USS ldap_get_values(lcp->ld, e, CS attr)))
+ {
+ if (attrs_requested != 1)
+ {
+ if (insert_space)
+ data = string_catn(data, US" ", 1);
+ else
+ insert_space = TRUE;
+ data = string_cat(data, attr);
+ data = string_catn(data, US"=\"", 2);
+ }
+
+ while (*values)
+ {
+ uschar *value = *values;
+ int len = Ustrlen(value);
+ ++valuecount;
+
+ DEBUG(D_lookup) debug_printf_indent("LDAP value loop %s:%s\n", attr, value);
+
+ /* In case we requested one attribute only but got several times
+ into that attr loop, we need to append the additional values.
+ (This may happen if you derive attributeTypes B and C from A and
+ then query for A.) In all other cases we detect the different
+ attribute and append only every non first value. */
+
+ if (data && valuecount > 1)
+ data = string_catn(data, US",", 1);
+
+ /* For multiple attributes, the data is in quotes. We must escape
+ internal quotes, backslashes, newlines, and must double commas. */
+
+ if (attrs_requested != 1)
+ for (int j = 0; j < len; j++)
+ {
+ if (value[j] == '\n')
+ data = string_catn(data, US"\\n", 2);
+ else if (value[j] == ',')
+ data = string_catn(data, US",,", 2);
+ else
+ {
+ if (value[j] == '\"' || value[j] == '\\')
+ data = string_catn(data, US"\\", 1);
+ data = string_catn(data, value+j, 1);
+ }
+ }
+
+ /* For single attributes, just double commas */
+
+ else
+ for (int j = 0; j < len; j++)
+ if (value[j] == ',')
+ data = string_catn(data, US",,", 2);
+ else
+ data = string_catn(data, 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 (attrs_requested != 1)
+ data = string_catn(data, 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.
+In the odd case of a single attribute with zero-length value, allocate
+an empty string. */
+
+if (!data) data = string_get(1);
+(void) string_from_gstring(data);
+gstring_release_unused(data);
+
+/* Copy the last dn into eldap_dn */
+
+if (dn)
+ {
+ 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_indent("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)
+ {
+ int err;
+ DEBUG(D_lookup) debug_printf_indent("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_indent("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_indent("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 ? error1 : US"",
+ error2 && error2[0] ? US"/" : US"",
+ error2 ? error2 : US"",
+ matched && matched[0] ? US"/" : US"",
+ matched ? 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_indent("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 = US"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_indent("LDAP search: returning: %s\n", data->s);
+*res = data->s;
+
+RETURN_OK:
+if (result) ldap_msgfree(result);
+ldap_free_urldesc(ludp);
+return OK;
+
+/* Error returns */
+
+RETURN_ERROR_BREAK:
+*defer_break = TRUE;
+
+RETURN_ERROR:
+DEBUG(D_lookup) debug_printf_indent("%s\n", *errmsg);
+
+RETURN_ERROR_NOMSG:
+if (result) ldap_msgfree(result);
+if (ludp) ldap_free_urldesc(ludp);
+
+#if defined LDAP_LIB_OPENLDAP2
+ if (error2) ldap_memfree(error2);
+ if (matched) 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(const 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;
+const uschar *url = ldap_url;
+const uschar *p;
+uschar *user = NULL;
+uschar *password = NULL;
+uschar *local_servers = NULL;
+const uschar *list;
+
+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)
+ {
+ const uschar *name = url;
+ while (*url && *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_indent("%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 = US"LDAP option REFERRALS is not \"follow\" or \"nofollow\"";
+ DEBUG(D_lookup) debug_printf_indent("%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_indent("%s\n", *errmsg);
+ return DEFER;
+ }
+ #endif
+
+ else
+ {
+ *errmsg =
+ string_sprintf("unknown parameter \"%.*s\" precedes LDAP URL",
+ namelen, name);
+ DEBUG(D_lookup) debug_printf_indent("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_indent("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)
+ {
+ uschar *t = user;
+ for (uschar * 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_indent("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 || !password)
+ {
+ *errmsg = US"ldapauth lookups must specify the username and password";
+ return DEFER;
+ }
+ if (!*password)
+ {
+ DEBUG(D_lookup) debug_printf_indent("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_indent("LDAP query error: %s\n", *errmsg);
+ return DEFER;
+ }
+
+/* No default servers, or URL contains a server name: just one attempt */
+
+if (!eldap_default_servers && !local_servers || 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 servers until OK or FAIL. Use local_servers list
+if defined in the lookup, otherwise use the global default list */
+
+list = local_servers ? local_servers : eldap_default_servers;
+for (uschar * server; server = string_nextinlist(&list, &sep, NULL, 0); )
+ {
+ int rc, port = 0;
+ uschar *colon = Ustrchr(server, ':');
+ if (colon)
+ {
+ *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, const uschar * filename, const uschar * ldap_url,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+return(control_ldap_search(ldap_url, SEARCH_LDAP_SINGLE, result, errmsg));
+}
+
+static int
+eldapm_find(void * handle, const uschar * filename, const uschar * ldap_url,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+return(control_ldap_search(ldap_url, SEARCH_LDAP_MULTIPLE, result, errmsg));
+}
+
+static int
+eldapdn_find(void * handle, const uschar * filename, const uschar * ldap_url,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+return(control_ldap_search(ldap_url, SEARCH_LDAP_DN, result, errmsg));
+}
+
+int
+eldapauth_find(void * handle, const uschar * filename, const uschar * ldap_url,
+ int length, uschar ** result, uschar ** errmsg, uint * 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(const 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)
+{
+eldap_dn = NULL;
+
+for (LDAP_CONNECTION *lcp; lcp = ldap_connections; ldap_connections = lcp->next)
+ {
+ DEBUG(D_lookup) debug_printf_indent("unbind LDAP connection to %s:%d\n",
+ lcp->host, lcp->port);
+ if(lcp->bound) ldap_unbind(lcp->ld);
+ }
+}
+
+
+
+/*************************************************
+* 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
+ idx lookup type index
+
+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, unsigned idx)
+{
+int c, count = 0, len = 0;
+BOOL dn = FALSE;
+uschar * t = s, * quoted;
+
+/* Test for a DN quotation. */
+
+if (opt)
+ {
+ 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.
+XXX No longer true; we always copy, to support quoted-enforcement */
+
+while ((c = *t++))
+ {
+ 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_quoted(len + count + 1, s, idx);
+
+/* Handle plain quote_ldap */
+
+if (!dn)
+ {
+ while ((c = *s++))
+ {
+ 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, US"%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, US"%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"
+
+gstring *
+ldap_version_report(gstring * g)
+{
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, "Library version: LDAP: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+
+static lookup_info ldap_lookup_info = {
+ .name = US"ldap", /* lookup name */
+ .type = lookup_querystyle, /* query-style lookup */
+ .open = eldap_open, /* open function */
+ .check = NULL, /* check function */
+ .find = eldap_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = eldap_tidy, /* tidy function */
+ .quote = eldap_quote, /* quoting function */
+ .version_report = ldap_version_report /* version reporting */
+};
+
+static lookup_info ldapdn_lookup_info = {
+ .name = US"ldapdn", /* lookup name */
+ .type = lookup_querystyle, /* query-style lookup */
+ .open = eldap_open, /* sic */ /* open function */
+ .check = NULL, /* check function */
+ .find = eldapdn_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = eldap_tidy, /* sic */ /* tidy function */
+ .quote = eldap_quote, /* sic */ /* quoting function */
+ .version_report = NULL /* no version reporting (redundant) */
+};
+
+static lookup_info ldapm_lookup_info = {
+ .name = US"ldapm", /* lookup name */
+ .type = lookup_querystyle, /* query-style lookup */
+ .open = eldap_open, /* sic */ /* open function */
+ .check = NULL, /* check function */
+ .find = eldapm_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = eldap_tidy, /* sic */ /* tidy function */
+ .quote = eldap_quote, /* sic */ /* quoting function */
+ .version_report = 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
index 0000000..ddfda85
--- /dev/null
+++ b/src/lookups/ldap.h
@@ -0,0 +1,13 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Header for eldapauth_find */
+
+extern int eldapauth_find(void *, uschar *, const 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
index 0000000..7f0f128
--- /dev/null
+++ b/src/lookups/lf_check_file.c
@@ -0,0 +1,113 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* Copyright (c) The Exim Maintainers 2020 */
+/* 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, const uschar * filename, int s_type, int modemask,
+ uid_t * owners, gid_t * owngroups, const char * type, uschar ** errmsg)
+{
+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 (int 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 (int 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
index 0000000..fd9eb30
--- /dev/null
+++ b/src/lookups/lf_functions.h
@@ -0,0 +1,20 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 */
+/* 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, const uschar *, int, int, uid_t *, gid_t *,
+ const char *, uschar **);
+extern gstring *lf_quote(uschar *, uschar *, int, gstring *);
+extern int lf_sqlperform(const uschar *, const uschar *, const uschar *,
+ const uschar *, uschar **,
+ uschar **, uint *, const uschar *,
+ int(*)(const uschar *, uschar *, uschar **,
+ uschar **, BOOL *, uint *, const uschar *));
+
+/* End of lf_functions.h */
diff --git a/src/lookups/lf_quote.c b/src/lookups/lf_quote.c
new file mode 100644
index 0000000..6f4143d
--- /dev/null
+++ b/src/lookups/lf_quote.c
@@ -0,0 +1,63 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 expanding-string
+
+Returns: the result pointer (possibly updated)
+*/
+
+gstring *
+lf_quote(uschar *name, uschar *value, int vlength, gstring * result)
+{
+result = string_append(result, 2, name, US"=");
+
+/* NULL is handled as an empty string */
+
+if (!value)
+ {
+ value = US"";
+ vlength = 0;
+ }
+
+/* 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] == '\"')
+ {
+ result = string_catn(result, US"\"", 1);
+ for (int j = 0; j < vlength; j++)
+ {
+ if (value[j] == '\"' || value[j] == '\\')
+ result = string_catn(result, US"\\", 1);
+ result = string_catn(result, US value+j, 1);
+ }
+ result = string_catn(result, US"\"", 1);
+ }
+else
+ result = string_catn(result, US value, vlength);
+
+return string_catn(result, 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
index 0000000..ce6f163
--- /dev/null
+++ b/src/lookups/lf_sqlperform.c
@@ -0,0 +1,175 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 zero if data is changed
+ func the lookup function to call
+
+Returns: the return from the lookup function, or DEFER
+*/
+
+int
+lf_sqlperform(const uschar *name, const uschar *optionname,
+ const uschar *optserverlist, const uschar *query,
+ uschar **result, uschar **errmsg, uint *do_cache, const uschar * opts,
+ int(*fn)(const uschar *, uschar *, uschar **, uschar **, BOOL *, uint *, const uschar *))
+{
+int rc;
+uschar *server;
+BOOL defer_break = FALSE;
+
+DEBUG(D_lookup) debug_printf_indent("%s query: \"%s\" opts '%s'\n", name, query, opts);
+
+/* Handle queries that do have server information at the start. */
+
+if (Ustrncmp(query, "servers", 7) == 0)
+ {
+ int qsep = 0;
+ const uschar *s, *ss;
+ const uschar *qserverlist;
+ uschar *qserver;
+
+ s = query + 7;
+ skip_whitespace(&s);
+ if (*s++ != '=')
+ {
+ *errmsg = string_sprintf("missing = after \"servers\" in %s lookup", name);
+ return DEFER;
+ }
+ skip_whitespace(&s);
+
+ ss = Ustrchr(s, ';');
+ if (!ss)
+ {
+ *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", (int)(ss - s), s);
+
+ while ((qserver = string_nextinlist(&qserverlist, &qsep, NULL, 0)))
+ {
+ if (Ustrchr(qserver, '/'))
+ server = qserver;
+ else
+ {
+ int len = Ustrlen(qserver);
+ const uschar * serverlist = optserverlist;
+
+ for (int sep = 0; server = string_nextinlist(&serverlist, &sep, NULL, 0);)
+ if (Ustrncmp(server, qserver, len) == 0 && server[len] == '/')
+ break;
+
+ if (!server)
+ {
+ *errmsg = string_sprintf("%s server \"%s\" not found in %s", name,
+ qserver, optionname);
+ return DEFER;
+ }
+ }
+
+ if (is_tainted(server))
+ {
+ *errmsg = string_sprintf("%s server \"%s\" is tainted", name, server);
+ return DEFER;
+ }
+
+ rc = (*fn)(ss+1, server, result, errmsg, &defer_break, do_cache, opts);
+ if (rc != DEFER || defer_break) return rc;
+ }
+ }
+
+/* Handle queries that do not have server information at the start. */
+
+else
+ {
+ const uschar * serverlist = NULL;
+
+ /* If options are present, scan for a server definition. Default to
+ the "optserverlist" srgument. */
+
+ if (opts)
+ {
+ uschar * ele;
+ for (int sep = ','; ele = string_nextinlist(&opts, &sep, NULL, 0); )
+ if (Ustrncmp(ele, "servers=", 8) == 0)
+ { serverlist = ele + 8; break; }
+ }
+
+ if (!serverlist)
+ serverlist = optserverlist;
+ if (!serverlist)
+ *errmsg = string_sprintf("no %s servers defined (%s option)", name,
+ optionname);
+ else
+ for (int d = 0; (server = string_nextinlist(&serverlist, &d, NULL, 0)); )
+ {
+ /* If not a full spec assume from options; scan main list for matching
+ hostname */
+
+ if (!Ustrchr(server, '/'))
+ {
+ int len = Ustrlen(server);
+ const uschar * slist = optserverlist;
+ uschar * ele;
+ for (int sep = 0; ele = string_nextinlist(&slist, &sep, NULL, 0); )
+ if (Ustrncmp(ele, server, len) == 0 && ele[len] == '/')
+ break;
+ if (!ele)
+ {
+ *errmsg = string_sprintf("%s server \"%s\" not found in %s", name,
+ server, optionname);
+ return DEFER;
+ }
+ server = ele;
+ }
+
+ if (is_tainted(server))
+ {
+ *errmsg = string_sprintf("%s server \"%s\" is tainted", name, server);
+ return DEFER;
+ }
+
+ rc = (*fn)(query, server, result, errmsg, &defer_break, do_cache, opts);
+ if (rc != DEFER || defer_break) return rc;
+ }
+ }
+
+return DEFER;
+}
+
+/* End of lf_sqlperform.c */
diff --git a/src/lookups/lmdb.c b/src/lookups/lmdb.c
new file mode 100644
index 0000000..a32c7f7
--- /dev/null
+++ b/src/lookups/lmdb.c
@@ -0,0 +1,162 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 2016 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+
+#ifdef LOOKUP_LMDB
+
+#include <lmdb.h>
+
+typedef struct lmdbstrct
+{
+MDB_txn *txn;
+MDB_dbi db_dbi;
+} Lmdbstrct;
+
+
+/*************************************************
+* Open entry point *
+*************************************************/
+
+static void *
+lmdb_open(const uschar * filename, uschar ** errmsg)
+{
+MDB_env * db_env = NULL;
+Lmdbstrct * lmdb_p;
+int ret, save_errno;
+const uschar * errstr;
+
+lmdb_p = store_get(sizeof(Lmdbstrct), GET_UNTAINTED);
+lmdb_p->txn = NULL;
+
+if ((ret = mdb_env_create(&db_env)))
+ {
+ errstr = US"create environment";
+ goto bad;
+ }
+
+if ((ret = mdb_env_open(db_env, CS filename, MDB_NOSUBDIR|MDB_RDONLY, 0660)))
+ {
+ errstr = string_sprintf("open environment with %s", filename);
+ goto bad;
+ }
+
+if ((ret = mdb_txn_begin(db_env, NULL, MDB_RDONLY, &lmdb_p->txn)))
+ {
+ errstr = US"start transaction";
+ goto bad;
+ }
+
+if ((ret = mdb_open(lmdb_p->txn, NULL, 0, &lmdb_p->db_dbi)))
+ {
+ errstr = US"open database";
+ goto bad;
+ }
+
+return lmdb_p;
+
+bad:
+ save_errno = errno;
+ if (lmdb_p->txn) mdb_txn_abort(lmdb_p->txn);
+ if (db_env) mdb_env_close(db_env);
+ *errmsg = string_sprintf("LMDB: Unable to %s: %s", errstr, mdb_strerror(ret));
+ errno = save_errno;
+ return NULL;
+}
+
+
+/*************************************************
+* Find entry point *
+*************************************************/
+
+static int
+lmdb_find(void * handle, const uschar * filename,
+ const uschar * keystring, int length, uschar ** result, uschar ** errmsg,
+ uint * do_cache, const uschar * opts)
+{
+int ret;
+MDB_val dbkey, data;
+Lmdbstrct * lmdb_p = handle;
+
+dbkey.mv_data = CS keystring;
+dbkey.mv_size = length;
+
+DEBUG(D_lookup) debug_printf_indent("LMDB: lookup key: %s\n", CS keystring);
+
+if ((ret = mdb_get(lmdb_p->txn, lmdb_p->db_dbi, &dbkey, &data)) == 0)
+ {
+ *result = string_copyn(US data.mv_data, data.mv_size);
+ DEBUG(D_lookup) debug_printf_indent("LMDB: lookup result: %s\n", *result);
+ return OK;
+ }
+else if (ret == MDB_NOTFOUND)
+ {
+ *errmsg = US"LMDB: lookup, no data found";
+ DEBUG(D_lookup) debug_printf_indent("%s\n", *errmsg);
+ return FAIL;
+ }
+else
+ {
+ *errmsg = string_sprintf("LMDB: lookup error: %s", mdb_strerror(ret));
+ DEBUG(D_lookup) debug_printf_indent("%s\n", *errmsg);
+ return DEFER;
+ }
+}
+
+
+/*************************************************
+* Close entry point *
+*************************************************/
+
+static void
+lmdb_close(void * handle)
+{
+Lmdbstrct * lmdb_p = handle;
+MDB_env * db_env = mdb_txn_env(lmdb_p->txn);
+mdb_txn_abort(lmdb_p->txn);
+mdb_env_close(db_env);
+}
+
+
+/*************************************************
+* Version reporting entry point *
+*************************************************/
+
+#include "../version.h"
+
+gstring *
+lmdb_version_report(gstring * g)
+{
+g = string_fmt_append(g, "Library version: LMDB: Compile: %d.%d.%d\n",
+ MDB_VERSION_MAJOR, MDB_VERSION_MINOR, MDB_VERSION_PATCH);
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, " Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+static lookup_info lmdb_lookup_info = {
+ .name = US"lmdb", /* lookup name */
+ .type = lookup_absfile, /* query-style lookup */
+ .open = lmdb_open, /* open function */
+ .check = NULL, /* no check function */
+ .find = lmdb_find, /* find function */
+ .close = lmdb_close, /* close function */
+ .tidy = NULL, /* tidy function */
+ .quote = NULL, /* quoting function */
+ .version_report = lmdb_version_report /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+# define lmdb_lookup_module_info _lookup_module_info
+#endif /* DYNLOOKUP */
+
+static lookup_info *_lookup_list[] = { &lmdb_lookup_info };
+lookup_module_info lmdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+#endif /* LOOKUP_LMDB */
diff --git a/src/lookups/lsearch.c b/src/lookups/lsearch.c
new file mode 100644
index 0000000..dcfdec9
--- /dev/null
+++ b/src/lookups/lsearch.c
@@ -0,0 +1,487 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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(const uschar * filename, uschar ** errmsg)
+{
+FILE * f = Ufopen(filename, "rb");
+if (!f)
+ *errmsg = string_open_failed("%s for linear search", filename);
+return f;
+}
+
+
+
+/*************************************************
+* Check entry point *
+*************************************************/
+
+static BOOL
+lsearch_check(void *handle, const 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, const uschar * filename,
+ const uschar * keystring, int length, uschar ** result, uschar ** errmsg,
+ int type, const uschar * opts)
+{
+FILE *f = handle;
+BOOL ret_full = FALSE;
+int old_pool = store_pool;
+rmark reset_point = NULL;
+uschar buffer[4096];
+
+if (opts)
+ {
+ int sep = ',';
+ uschar * ele;
+
+ while ((ele = string_nextinlist(&opts, &sep, NULL, 0)))
+ if (Ustrcmp(ele, "ret=full") == 0)
+ { ret_full = TRUE; break; }
+ }
+
+/* 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_mark();
+ }
+
+rewind(f);
+for (BOOL this_is_eol, last_was_eol = TRUE;
+ Ufgets(buffer, sizeof(buffer), f) != NULL;
+ last_was_eol = this_is_eol)
+ {
+ int p = Ustrlen(buffer);
+ int linekeylength;
+ BOOL this_is_comment;
+ gstring * 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 && *s != '\"')
+ {
+ *t++ = *s == '\\' ? string_interpret_escape(CUSS &s) : *s;
+ s++;
+ }
+ linekeylength = t - buffer;
+ if (*s) s++; /* Past terminating " */
+ if (ret_full)
+ memmove(t, s, Ustrlen(s)+1); /* copy the rest of line also */
+ }
+
+ /* Otherwise it is terminated by a colon or white space */
+
+ else
+ {
+ while (*s && *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];
+ const 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 drop
+ 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. */
+
+ if (!ret_full)
+ if (Uskip_whitespace(&s) == ':')
+ {
+ s++;
+ Uskip_whitespace(&s);
+ }
+
+ /* Reset dynamic store, if we need to, and revert to the search pool */
+
+ if (reset_point)
+ {
+ reset_point = 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;
+ yield = string_get(100);
+ if (ret_full)
+ yield = string_cat(yield, buffer);
+ else if (*s)
+ yield = string_cat(yield, 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, s);
+ }
+
+ gstring_release_unused(yield);
+ *result = string_from_gstring(yield);
+ return OK;
+ }
+
+/* Reset dynamic store, if we need to */
+
+if (reset_point)
+ {
+ 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, const uschar * filename, const uschar * keystring,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+return internal_lsearch_find(handle, filename, keystring, length, result,
+ errmsg, LSEARCH_PLAIN, opts);
+}
+
+
+
+/*************************************************
+* Find entry point for wildlsearch *
+*************************************************/
+
+/* See local README for interface description */
+
+static int
+wildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+return internal_lsearch_find(handle, filename, keystring, length, result,
+ errmsg, LSEARCH_WILD, opts);
+}
+
+
+
+/*************************************************
+* Find entry point for nwildlsearch *
+*************************************************/
+
+/* See local README for interface description */
+
+static int
+nwildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+return internal_lsearch_find(handle, filename, keystring, length, result,
+ errmsg, LSEARCH_NWILD, opts);
+}
+
+
+
+
+/*************************************************
+* Find entry point for iplsearch *
+*************************************************/
+
+/* See local README for interface description */
+
+static int
+iplsearch_find(void * handle, uschar const * filename, const uschar * keystring,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+if ((length == 1 && keystring[0] == '*') ||
+ string_is_ip_address(keystring, NULL) != 0)
+ return internal_lsearch_find(handle, filename, keystring, length, result,
+ errmsg, LSEARCH_IP, opts);
+
+*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"
+
+gstring *
+lsearch_version_report(gstring * g)
+{
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, "Library version: lsearch: Exim version %s\n", EXIM_VERSION_STR));
+#endif
+return g;
+}
+
+
+static lookup_info iplsearch_lookup_info = {
+ .name = US"iplsearch", /* lookup name */
+ .type = lookup_absfile, /* uses absolute file name */
+ .open = lsearch_open, /* open function */
+ .check = lsearch_check, /* check function */
+ .find = iplsearch_find, /* find function */
+ .close = lsearch_close, /* close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = NULL /* no version reporting (redundant) */
+};
+
+static lookup_info lsearch_lookup_info = {
+ .name = US"lsearch", /* lookup name */
+ .type = lookup_absfile, /* uses absolute file name */
+ .open = lsearch_open, /* open function */
+ .check = lsearch_check, /* check function */
+ .find = lsearch_find, /* find function */
+ .close = lsearch_close, /* close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = lsearch_version_report /* version reporting */
+};
+
+static lookup_info nwildlsearch_lookup_info = {
+ .name = US"nwildlsearch", /* lookup name */
+ .type = lookup_absfile, /* uses absolute file name */
+ .open = lsearch_open, /* open function */
+ .check = lsearch_check, /* check function */
+ .find = nwildlsearch_find, /* find function */
+ .close = lsearch_close, /* close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = NULL /* no version reporting (redundant) */
+};
+
+static lookup_info wildlsearch_lookup_info = {
+ .name = US"wildlsearch", /* lookup name */
+ .type = lookup_absfile, /* uses absolute file name */
+ .open = lsearch_open, /* open function */
+ .check = lsearch_check, /* check function */
+ .find = wildlsearch_find, /* find function */
+ .close = lsearch_close, /* close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = 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
index 0000000..78b8c2b
--- /dev/null
+++ b/src/lookups/mysql.c
@@ -0,0 +1,503 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 */
+
+/* We define symbols for *_VERSION_ID (numeric), *_VERSION_STR (char*)
+and *_BASE_STR (char*). It's a bit of guesswork. Especially for mariadb
+with versions before 10.2, as they do not define there there specific symbols.
+*/
+
+/* Newer (>= 10.2) MariaDB */
+#if defined MARIADB_VERSION_ID
+#define EXIM_MxSQL_VERSION_ID MARIADB_VERSION_ID
+
+/* MySQL defines MYSQL_VERSION_ID, and MariaDB does so */
+/* https://dev.mysql.com/doc/refman/5.7/en/c-api-server-client-versions.html */
+#elif defined LIBMYSQL_VERSION_ID
+#define EXIM_MxSQL_VERSION_ID LIBMYSQL_VERSION_ID
+#elif defined MYSQL_VERSION_ID
+#define EXIM_MxSQL_VERSION_ID MYSQL_VERSION_ID
+
+#else
+#define EXIM_MYSQL_VERSION_ID 0
+#endif
+
+/* Newer (>= 10.2) MariaDB */
+#ifdef MARIADB_CLIENT_VERSION_STR
+#define EXIM_MxSQL_VERSION_STR MARIADB_CLIENT_VERSION_STR
+
+/* Mysql uses MYSQL_SERVER_VERSION */
+#elif defined LIBMYSQL_VERSION
+#define EXIM_MxSQL_VERSION_STR LIBMYSQL_VERSION
+#elif defined MYSQL_SERVER_VERSION
+#define EXIM_MxSQL_VERSION_STR MYSQL_SERVER_VERSION
+
+#else
+#define EXIM_MxSQL_VERSION_STR "unknown"
+#endif
+
+#if defined MARIADB_BASE_VERSION
+#define EXIM_MxSQL_BASE_STR MARIADB_BASE_VERSION
+
+#elif defined MARIADB_PACKAGE_VERSION
+#define EXIM_MxSQL_BASE_STR "mariadb"
+
+#elif defined MYSQL_BASE_VERSION
+#define EXIM_MxSQL_BASE_STR MYSQL_BASE_VERSION
+
+#else
+#define EXIM_MxSQL_BASE_STR "n.A."
+#endif
+
+
+/* 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(const 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_indent("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 zero if data is changed
+ opts options
+
+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(const uschar *query, uschar *server, uschar **resultptr,
+ uschar **errmsg, BOOL *defer_break, uint *do_cache, const uschar * opts)
+{
+MYSQL *mysql_handle = NULL; /* Keep compilers happy */
+MYSQL_RES *mysql_result = NULL;
+MYSQL_ROW mysql_row_data;
+MYSQL_FIELD *fields;
+
+int i;
+int yield = DEFER;
+unsigned int num_fields;
+gstring * 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 (int i = 3; i > 0; i--)
+ {
+ uschar *pp = Ustrrchr(server, '/');
+ if (!pp)
+ {
+ *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; 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)[group] where all the parts are optional.
+The "group" parameter specifies an option group from a MySQL option file. */
+
+if (!cn)
+ {
+ uschar *p;
+ uschar *socket = NULL;
+ int port = 0;
+ uschar *group = US"exim";
+
+ if ((p = Ustrchr(sdata[0], '[')))
+ {
+ *p++ = 0;
+ group = p;
+ while (*p && *p != ']') p++;
+ *p = 0;
+ }
+
+ if ((p = Ustrchr(sdata[0], '(')))
+ {
+ *p++ = 0;
+ socket = p;
+ while (*p && *p != ')') p++;
+ *p = 0;
+ }
+
+ if ((p = Ustrchr(sdata[0], ':')))
+ {
+ *p++ = 0;
+ port = Uatoi(p);
+ }
+
+ if (Ustrchr(sdata[0], '/'))
+ {
+ *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_indent("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), GET_UNTAINTED);
+ mysql_init(mysql_handle);
+ mysql_options(mysql_handle, MYSQL_READ_DEFAULT_GROUP, CS group);
+ 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), GET_UNTAINTED);
+ 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_indent("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 zero requests this. */
+
+if (!(mysql_result = mysql_use_result(mysql_handle)))
+ {
+ if (mysql_field_count(mysql_handle) == 0)
+ {
+ DEBUG(D_lookup) debug_printf_indent("MYSQL: query was not one that returns data\n");
+ result = string_cat(result,
+ string_sprintf("%lld", mysql_affected_rows(mysql_handle)));
+ *do_cache = 0;
+ 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)))
+ {
+ unsigned long * lengths = mysql_fetch_lengths(mysql_result);
+
+ if (result)
+ result = string_catn(result, US"\n", 1);
+
+ if (num_fields != 1)
+ for (int i = 0; i < num_fields; i++)
+ result = lf_quote(US fields[i].name, US mysql_row_data[i], lengths[i],
+ result);
+
+ else if (mysql_row_data[0] != NULL) /* NULL value yields nothing */
+ result = lengths[0] == 0 && !result
+ ? string_get(1) /* for 0-len string result ensure non-null gstring */
+ : string_catn(result, US mysql_row_data[0], lengths[0]);
+ }
+
+/* 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)
+ {
+ *errmsg = string_sprintf(
+ "MYSQL: lookup result error when checking for more results: %s\n",
+ mysql_error(mysql_handle));
+ goto MYSQL_EXIT;
+ }
+ else /* just ignore more results */
+ DEBUG(D_lookup) debug_printf_indent("MYSQL: got unexpected more results\n");
+
+/* 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)
+ {
+ yield = FAIL;
+ *errmsg = US"MYSQL: no data found";
+ }
+
+/* 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) mysql_free_result(mysql_result);
+
+/* Non-NULL result indicates a successful result */
+
+if (result)
+ {
+ *resultptr = string_from_gstring(result);
+ gstring_release_unused(result);
+ return OK;
+ }
+else
+ {
+ DEBUG(D_lookup) debug_printf_indent("%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, const uschar * filename, const uschar * query,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+return lf_sqlperform(US"MySQL", US"mysql_servers", mysql_servers, query,
+ result, errmsg, do_cache, opts, 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
+ idx lookup type index
+
+Returns: the processed string or NULL for a bad option
+*/
+
+static uschar *
+mysql_quote(uschar * s, uschar * opt, unsigned idx)
+{
+int c, count = 0;
+uschar * t = s, * quoted;
+
+if (opt) return NULL; /* No options recognized */
+
+while ((c = *t++))
+ if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL) count++;
+
+/* Old code: if (count == 0) return s;
+Now always allocate and copy, to track the quoted status. */
+
+t = quoted = store_get_quoted(Ustrlen(s) + count + 1, s, idx);
+
+while ((c = *s++))
+ {
+ 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"
+
+gstring *
+mysql_version_report(gstring * g)
+{
+g = string_fmt_append(g,
+ "Library version: MySQL: Compile: %lu %s [%s]\n"
+ " Runtime: %lu %s\n",
+ (long)EXIM_MxSQL_VERSION_ID, EXIM_MxSQL_VERSION_STR, EXIM_MxSQL_BASE_STR,
+ mysql_get_client_version(), mysql_get_client_info());
+#ifdef DYNLOOKUP
+g = string_fmt_append(g,
+ " Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+/* These are the lookup_info blocks for this driver */
+
+static lookup_info mysql_lookup_info = {
+ .name = US"mysql", /* lookup name */
+ .type = lookup_querystyle, /* query-style lookup */
+ .open = mysql_open, /* open function */
+ .check = NULL, /* no check function */
+ .find = mysql_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = mysql_tidy, /* tidy function */
+ .quote = mysql_quote, /* quoting function */
+ .version_report = 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
index 0000000..0024f44
--- /dev/null
+++ b/src/lookups/nis.c
@@ -0,0 +1,141 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* 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(const uschar * filename, uschar ** errmsg)
+{
+char *nis_domain;
+if (yp_get_default_domain(&nis_domain) != 0)
+ {
+ *errmsg = US"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, const uschar * filename, const uschar * keystring,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+int rc;
+uschar *nis_data;
+int nis_data_length;
+do_cache = do_cache; /* Placate picky compilers */
+if ((rc = yp_match(CCS handle, CCS filename, CCS 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, const uschar * filename, const uschar * keystring,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+int rc;
+uschar *nis_data;
+int nis_data_length;
+do_cache = do_cache; /* Placate picky compilers */
+if ((rc = yp_match(CCS handle, CCS filename, CCS 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"
+
+gstring *
+nis_version_report(gstring * g)
+{
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, "Library version: NIS: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+
+static lookup_info nis_lookup_info = {
+ .name = US"nis", /* lookup name */
+ .type = 0, /* not abs file, not query style*/
+ .open = nis_open, /* open function */
+ .check = NULL, /* check function */
+ .find = nis_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = nis_version_report /* version reporting */
+};
+
+static lookup_info nis0_lookup_info = {
+ .name = US"nis0", /* lookup name */
+ .type = 0, /* not absfile, not query style */
+ .open = nis_open, /* sic */ /* open function */
+ .check = NULL, /* check function */
+ .find = nis0_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = 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
index 0000000..d9f3f7d
--- /dev/null
+++ b/src/lookups/nisplus.c
@@ -0,0 +1,294 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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(const 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, const uschar * filename, const uschar * query,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+int error_error = FAIL;
+const uschar * field_name = NULL;
+nis_result *nrt = NULL;
+nis_result *nre = NULL;
+nis_object *tno, *eno;
+struct entry_obj *eo;
+struct table_obj *ta;
+const uschar * p = query + length;
+gstring * 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) /* get the query without the result-field */
+ {
+ uint len = p-1 - query;
+ field_name = p;
+ query = string_copyn(query, len);
+ p = query + len;
+ }
+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 (int 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) 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)
+ {
+ yield = string_cat (yield, US tc->tc_name);
+ yield = string_catn(yield, US"=", 1);
+
+ /* Quote the value if it contains spaces or is empty */
+
+ if (value[0] == 0 || Ustrchr(value, ' ') != NULL)
+ {
+ yield = string_catn(yield, US"\"", 1);
+ for (int j = 0; j < len; j++)
+ {
+ if (value[j] == '\"' || value[j] == '\\')
+ yield = string_catn(yield, US"\\", 1);
+ yield = string_catn(yield, value+j, 1);
+ }
+ yield = string_catn(yield, US"\"", 1);
+ }
+ else
+ yield = string_catn(yield, value, len);
+
+ yield = string_catn(yield, US" ", 1);
+ }
+
+ /* When the specified field is found, grab its data and finish */
+
+ else if (Ustrcmp(field_name, tc->tc_name) == 0)
+ {
+ yield = string_catn(yield, 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)
+ *errmsg = string_sprintf("NIS+ field %s not found for %s", field_name,
+ query);
+else
+ gstring_release_unused(yield);
+
+/* Free result store before finishing. */
+
+NISPLUS_EXIT:
+if (nrt) nis_freeresult(nrt);
+if (nre) nis_freeresult(nre);
+
+if (yield)
+ {
+ *result = string_from_gstring(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
+ idx lookup type index
+
+Returns: the processed string or NULL for a bad option
+*/
+
+static uschar *
+nisplus_quote(uschar * s, uschar * opt, unsigned idx)
+{
+int count = 0;
+uschar * quoted, * t = s;
+
+if (opt) return NULL; /* No options recognized */
+
+while (*t) if (*t++ == '\"') count++;
+
+t = quoted = store_get_quoted(Ustrlen(s) + count + 1, s, idx);
+
+while (*s)
+ {
+ *t++ = *s;
+ if (*s++ == '\"') *t++ = '\"';
+ }
+
+*t = 0;
+return quoted;
+}
+
+
+/*************************************************
+* Version reporting entry point *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+gstring *
+nisplus_version_report(gstring * g)
+{
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, "Library version: NIS+: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+
+static lookup_info _lookup_info = {
+ .name = US"nisplus", /* lookup name */
+ .type = lookup_querystyle, /* query-style lookup */
+ .open = nisplus_open, /* open function */
+ .check = NULL, /* check function */
+ .find = nisplus_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = nisplus_quote, /* quoting function */
+ .version_report = 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
index 0000000..d32b5e4
--- /dev/null
+++ b/src/lookups/oracle.c
@@ -0,0 +1,628 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* 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(const 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_indent("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 yield = DEFER;
+unsigned int num_fields = 0;
+gstring * result = NULL;
+oracle_connection *cn = NULL;
+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 (int 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; 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)
+ {
+ DEBUG(D_lookup) debug_printf_indent("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), GET_UNTAINTED);
+ hda = store_get(HDA_SIZE, GET_UNTAINTED);
+ 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), GET_UNTAINTED);
+ 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_indent("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), GET_UNTAINTED);
+
+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, GET_UNTAINTED);
+desc = store_get(sizeof(Ora_Describe)*MAX_SELECT_LIST_SIZE, GET_UNTAINTED);
+
+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) result = string_catn(result, "\n", 1);
+
+ /* Single field - just add on the data */
+
+ if (num_fields == 1)
+ result = string_catn(result, def[0].buf, def[0].col_retlen);
+
+ /* Multiple fields - precede by file name, removing {lead,trail}ing WS */
+
+ else for (int 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_catn(result, s, slen);
+ result = string_catn(result, US"=", 1);
+
+ /* int and float type won't 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))
+ {
+ result = string_catn(result, "\"", 1);
+ for (int j = 0; j < def[i].col_retlen; j++)
+ {
+ if (def[i].buf[j] == '\"' || def[i].buf[j] == '\\')
+ result = string_catn(result, "\\", 1);
+ result = string_catn(result, def[i].buf+j, 1);
+ }
+ result = string_catn(result, "\"", 1);
+ }
+
+ else switch(desc[i].dbtype)
+ {
+ case INT_TYPE:
+ result = string_cat(result, string_sprintf("%d", def[i].int_buf));
+ break;
+
+ case FLOAT_TYPE:
+ result = string_cat(result, string_sprintf("%f", def[i].flt_buf));
+ break;
+
+ case STRING_TYPE:
+ result = string_catn(result, 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_catn(result, " ", 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)
+ {
+ yield = FAIL;
+ *errmsg = "ORACLE: no data found";
+ }
+else
+ gstring_release_unused(result);
+
+/* 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 successful result */
+
+if (result)
+ {
+ *resultptr = string_from_gstring(result);
+ return OK;
+ }
+else
+ {
+ DEBUG(D_lookup) debug_printf_indent("%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, const uschar * filename, uschar * query, int length,
+ uschar ** result, uschar ** errmsg, uint * do_cache, const uschar * opts)
+{
+int sep = 0;
+uschar *server;
+uschar *list = oracle_servers;
+
+do_cache = do_cache; /* Placate picky compilers */
+
+DEBUG(D_lookup) debug_printf_indent("ORACLE query: %s\n", query);
+
+while ((server = string_nextinlist(&list, &sep, NULL, 0)))
+ {
+ BOOL defer_break;
+ int rc = perform_oracle_search(query, server, result, errmsg, &defer_break);
+ if (rc != DEFER || defer_break) return rc;
+ }
+
+if (!oracle_servers)
+ *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
+ idx lookup type index
+
+Returns: the processed string or NULL for a bad option
+*/
+
+static uschar *
+oracle_quote(uschar * s, uschar * opt, unsigned idx)
+{
+int c, count = 0;
+uschar * t = s, * quoted;
+
+if (opt) return NULL; /* No options are recognized */
+
+while ((c = *t++))
+ if (strchr("\n\t\r\b\'\"\\", c) != NULL) count++;
+
+t = quoted = store_get_quoted((int)Ustrlen(s) + count + 1, s, idx);
+
+while ((c = *s++))
+ {
+ 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"
+
+gstring *
+oracle_version_report(gstring * g)
+{
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, "Library version: Oracle: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+
+static lookup_info _lookup_info = {
+ .name = US"oracle", /* lookup name */
+ .type = lookup_querystyle, /* query-style lookup */
+ .open = oracle_open, /* open function */
+ .check = NULL, /* check function */
+ .find = oracle_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = oracle_tidy, /* tidy function */
+ .quote = oracle_quote, /* quoting function */
+ .version_report = 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
index 0000000..eaf78b2
--- /dev/null
+++ b/src/lookups/passwd.c
@@ -0,0 +1,85 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* 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(const uschar * filename, uschar ** 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, const uschar * filename, const uschar * keystring,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+struct passwd *pw;
+
+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"
+
+gstring *
+passwd_version_report(gstring * g)
+{
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, "Library version: passwd: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+static lookup_info _lookup_info = {
+ .name = US"passwd", /* lookup name */
+ .type = lookup_querystyle, /* query-style lookup */
+ .open = passwd_open, /* open function */
+ .check = NULL, /* no check function */
+ .find = passwd_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = 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
index 0000000..4bb693a
--- /dev/null
+++ b/src/lookups/pgsql.c
@@ -0,0 +1,506 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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(const 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_indent("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_indent("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 delimiters 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
+ opts options list
+
+Returns: OK, FAIL, or DEFER
+*/
+
+static int
+perform_pgsql_search(const uschar *query, uschar *server, uschar **resultptr,
+ uschar **errmsg, BOOL *defer_break, uint *do_cache, const uschar * opts)
+{
+PGconn *pg_conn = NULL;
+PGresult *pg_result = NULL;
+
+gstring * result = NULL;
+int yield = DEFER;
+unsigned int num_fields, num_tuples;
+pgsql_connection *cn;
+rmark reset_point = store_mark();
+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 (int i = 2; i >= 0; i--)
+ {
+ uschar *pp = Ustrrchr(server, '/');
+ if (!pp)
+ {
+ *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; 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)
+ {
+ 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 && *p != ')') p++;
+ *p = 0;
+
+ last_slash = Ustrrchr(server, '/');
+ last_dot = Ustrrchr(server, '.');
+
+ DEBUG(D_lookup) debug_printf_indent("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 || !last_dot)
+ {
+ *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, ':')))
+ {
+ *p++ = 0;
+ port = p;
+ }
+
+ if (Ustrchr(server, '/'))
+ {
+ *errmsg = string_sprintf("unexpected slash in pgSQL server hostname: %s",
+ server);
+ *defer_break = TRUE;
+ return DEFER;
+ }
+
+ DEBUG(D_lookup) debug_printf_indent("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)
+ {
+ reset_point = store_reset(reset_point);
+ *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), GET_UNTAINTED);
+ 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_indent("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 zero. */
+
+ result = string_cat(result, US PQcmdTuples(pg_result));
+ *do_cache = 0;
+ DEBUG(D_lookup) debug_printf_indent("PGSQL: command does not return any data "
+ "but was successful. Rows affected: %s\n", string_from_gstring(result));
+ break;
+
+ 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 (int i = 0; i < num_tuples; i++)
+ {
+ if (result)
+ result = string_catn(result, US"\n", 1);
+
+ if (num_fields == 1)
+ result = string_catn(result,
+ US PQgetvalue(pg_result, i, 0), PQgetlength(pg_result, i, 0));
+ else
+ for (int 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);
+ }
+ if (!result) result = string_get(1);
+ }
+
+/* If result is NULL then no data has been found and so we return FAIL. */
+
+if (!result)
+ {
+ yield = FAIL;
+ *errmsg = US"PGSQL: no data found";
+ }
+
+/* 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) PQclear(pg_result);
+
+/* Non-NULL result indicates a successful result */
+
+if (result)
+ {
+ gstring_release_unused(result);
+ *resultptr = string_from_gstring(result);
+ return OK;
+ }
+else
+ {
+ DEBUG(D_lookup) debug_printf_indent("%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, const uschar * filename, const uschar * query,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+return lf_sqlperform(US"PostgreSQL", US"pgsql_servers", pgsql_servers, query,
+ result, errmsg, do_cache, opts, 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.
+
+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
+ idx lookup type index
+
+Returns: the processed string or NULL for a bad option
+*/
+
+static uschar *
+pgsql_quote(uschar * s, uschar * opt, unsigned idx)
+{
+int count = 0, c;
+uschar * t = s, * quoted;
+
+if (opt) return NULL; /* No options recognized */
+
+while ((c = *t++))
+ if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL) count++;
+
+t = quoted = store_get_quoted(Ustrlen(s) + count + 1, s, idx);
+
+while ((c = *s++))
+ {
+ 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"
+
+gstring *
+pgsql_version_report(gstring * g)
+{
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, "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? */
+
+return g;
+}
+
+
+static lookup_info _lookup_info = {
+ .name = US"pgsql", /* lookup name */
+ .type = lookup_querystyle, /* query-style lookup */
+ .open = pgsql_open, /* open function */
+ .check = NULL, /* no check function */
+ .find = pgsql_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = pgsql_tidy, /* tidy function */
+ .quote = pgsql_quote, /* quoting function */
+ .version_report = 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/readsock.c b/src/lookups/readsock.c
new file mode 100644
index 0000000..22179c9
--- /dev/null
+++ b/src/lookups/readsock.c
@@ -0,0 +1,341 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/* Copyright (c) Jeremy Harris 2020 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+
+static int
+internal_readsock_open(client_conn_ctx * cctx, const uschar * sspec,
+ int timeout, BOOL do_tls, uschar ** errmsg)
+{
+const uschar * server_name;
+host_item host;
+
+if (Ustrncmp(sspec, "inet:", 5) == 0)
+ {
+ int port;
+ uschar * port_name;
+
+ DEBUG(D_lookup)
+ debug_printf_indent(" new inet socket needed for readsocket\n");
+
+ server_name = sspec + 5;
+ port_name = Ustrrchr(server_name, ':');
+
+ /* Sort out the port */
+
+ if (!port_name)
+ {
+ /* expand_string_message results in an EXPAND_FAIL, from our
+ only caller. Lack of it gets a SOCK_FAIL; we feed back via errmsg
+ for that, which gets copied to search_error_message. */
+
+ expand_string_message =
+ string_sprintf("missing port for readsocket %s", sspec);
+ return FAIL;
+ }
+ *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);
+ return FAIL;
+ }
+ }
+ else
+ {
+ struct servent *service_info = getservbyname(CS port_name, "tcp");
+ if (!service_info)
+ {
+ expand_string_message = string_sprintf("unknown port \"%s\"",
+ port_name);
+ return FAIL;
+ }
+ port = ntohs(service_info->s_port);
+ }
+
+ /* Not having the request-string here in the open routine means
+ that we cannot do TFO; a pity */
+
+ cctx->sock = ip_connectedsocket(SOCK_STREAM, server_name, port, port,
+ timeout, &host, errmsg, NULL);
+ callout_address = NULL;
+ if (cctx->sock < 0)
+ return FAIL;
+ }
+
+else
+ {
+ struct sockaddr_un sockun; /* don't call this "sun" ! */
+ int rc;
+
+ DEBUG(D_lookup)
+ debug_printf_indent(" new unix socket needed for readsocket\n");
+
+ if ((cctx->sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
+ {
+ *errmsg = string_sprintf("failed to create socket: %s", strerror(errno));
+ return FAIL;
+ }
+
+ sockun.sun_family = AF_UNIX;
+ sprintf(sockun.sun_path, "%.*s", (int)(sizeof(sockun.sun_path)-1),
+ sspec);
+ server_name = US sockun.sun_path;
+
+ sigalrm_seen = FALSE;
+ ALARM(timeout);
+ rc = connect(cctx->sock, (struct sockaddr *) &sockun, sizeof(sockun));
+ ALARM_CLR(0);
+ if (sigalrm_seen)
+ {
+ *errmsg = US "socket connect timed out";
+ goto bad;
+ }
+ if (rc < 0)
+ {
+ *errmsg = string_sprintf("failed to connect to socket "
+ "%s: %s", sspec, strerror(errno));
+ goto bad;
+ }
+ host.name = server_name;
+ host.address = US"";
+ }
+
+#ifndef DISABLE_TLS
+if (do_tls)
+ {
+ union sockaddr_46 interface_sock;
+ EXIM_SOCKLEN_T size = sizeof(interface_sock);
+ smtp_connect_args conn_args = {.host = &host };
+ tls_support tls_dummy = { .sni = NULL };
+ uschar * errstr;
+
+ if (getsockname(cctx->sock, (struct sockaddr *) &interface_sock, &size) == 0)
+ conn_args.sending_ip_address = host_ntoa(-1, &interface_sock, NULL, NULL);
+ else
+ {
+ *errmsg = string_sprintf("getsockname failed: %s", strerror(errno));
+ goto bad;
+ }
+
+ if (!tls_client_start(cctx, &conn_args, NULL, &tls_dummy, &errstr))
+ {
+ *errmsg = string_sprintf("TLS connect failed: %s", errstr);
+ goto bad;
+ }
+ }
+#endif
+
+DEBUG(D_expand|D_lookup) debug_printf_indent(" connected to socket %s\n", sspec);
+return OK;
+
+bad:
+ close(cctx->sock);
+ return FAIL;
+}
+
+/* All use of allocations will be done against the POOL_SEARCH memory,
+which is freed once by search_tidyup(). */
+
+/*************************************************
+* Open entry point *
+*************************************************/
+
+/* See local README for interface description */
+/* We just create a placeholder record with a closed socket, so
+that connection cacheing at the framework layer works. */
+
+static void *
+readsock_open(const uschar * filename, uschar ** errmsg)
+{
+client_conn_ctx * cctx = store_get(sizeof(*cctx), GET_UNTAINTED);
+cctx->sock = -1;
+cctx->tls_ctx = NULL;
+DEBUG(D_lookup) debug_printf_indent("readsock: allocated context\n");
+return cctx;
+}
+
+
+
+
+
+/*************************************************
+* Find entry point for lsearch *
+*************************************************/
+
+/* See local README for interface description */
+
+static int
+readsock_find(void * handle, const uschar * filename, const uschar * keystring,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+client_conn_ctx * cctx = handle;
+int sep = ',';
+struct {
+ BOOL do_shutdown:1;
+ BOOL do_tls:1;
+ BOOL cache:1;
+} lf = {.do_shutdown = TRUE};
+uschar * eol = NULL;
+int timeout = 5;
+gstring * yield;
+int ret = DEFER;
+
+DEBUG(D_lookup)
+ debug_printf_indent("readsock: file=\"%s\" key=\"%s\" len=%d opts=\"%s\"\n",
+ filename, keystring, length, opts);
+
+/* Parse options */
+
+if (opts) for (uschar * s; s = string_nextinlist(&opts, &sep, NULL, 0); )
+ if (Ustrncmp(s, "timeout=", 8) == 0)
+ timeout = readconf_readtime(s + 8, 0, FALSE);
+ else if (Ustrncmp(s, "shutdown=", 9) == 0)
+ lf.do_shutdown = Ustrcmp(s + 9, "no") != 0;
+#ifndef DISABLE_TLS
+ else if (Ustrncmp(s, "tls=", 4) == 0 && Ustrcmp(s + 4, US"no") != 0)
+ lf.do_tls = TRUE;
+#endif
+ else if (Ustrncmp(s, "eol=", 4) == 0)
+ eol = string_unprinting(s + 4);
+ else if (Ustrcmp(s, "cache=yes") == 0)
+ lf.cache = TRUE;
+ else if (Ustrcmp(s, "send=no") == 0)
+ length = 0;
+
+if (!filename) return FAIL; /* Server spec is required */
+
+/* Open the socket, if not cached */
+
+if (cctx->sock == -1)
+ if (internal_readsock_open(cctx, filename, timeout, lf.do_tls, errmsg) != OK)
+ return ret;
+
+testharness_pause_ms(100); /* Allow sequencing of test actions */
+
+/* Write the request string, if not empty or already done */
+
+if (length)
+ {
+ if ((
+#ifndef DISABLE_TLS
+ cctx->tls_ctx ? tls_write(cctx->tls_ctx, keystring, length, FALSE) :
+#endif
+ write(cctx->sock, keystring, length)) != length)
+ {
+ *errmsg = string_sprintf("request write to socket "
+ "failed: %s", strerror(errno));
+ goto out;
+ }
+ }
+
+/* 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
+if (!cctx->tls_ctx && lf.do_shutdown)
+ shutdown(cctx->sock, SHUT_WR);
+#endif
+
+testharness_pause_ms(100);
+
+/* Now we need to read from the socket, under a timeout. The function
+that reads a file can be used. If we're using a stdio buffered read,
+and might need later write ops on the socket, the stdio must be in
+writable mode or the underlying socket goes non-writable. */
+
+sigalrm_seen = FALSE;
+#ifdef DISABLE_TLS
+if (TRUE)
+#else
+if (!cctx->tls_ctx)
+#endif
+ {
+ FILE * fp = fdopen(cctx->sock, "rb");
+ if (!fp)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "readsock fdopen: %s\n", strerror(errno));
+ goto out;
+ }
+ ALARM(timeout);
+ yield = cat_file(fp, NULL, eol);
+ }
+else
+ {
+ ALARM(timeout);
+ yield = cat_file_tls(cctx->tls_ctx, NULL, eol);
+ }
+
+ALARM_CLR(0);
+
+if (sigalrm_seen)
+ { *errmsg = US "socket read timed out"; goto out; }
+
+*result = yield ? string_from_gstring(yield) : US"";
+ret = OK;
+if (!lf.cache) *do_cache = 0;
+
+out:
+
+(void) close(cctx->sock);
+cctx->sock = -1;
+return ret;
+}
+
+
+
+/*************************************************
+* Close entry point *
+*************************************************/
+
+/* See local README for interface description */
+
+static void
+readsock_close(void * handle)
+{
+client_conn_ctx * cctx = handle;
+if (cctx->sock < 0) return;
+#ifndef DISABLE_TLS
+if (cctx->tls_ctx) tls_close(cctx->tls_ctx, TRUE);
+#endif
+close(cctx->sock);
+cctx->sock = -1;
+}
+
+
+
+static lookup_info readsock_lookup_info = {
+ .name = US"readsock", /* lookup name */
+ .type = lookup_querystyle,
+ .open = readsock_open, /* open function */
+ .check = NULL,
+ .find = readsock_find, /* find function */
+ .close = readsock_close,
+ .tidy = NULL,
+ .quote = NULL, /* no quoting function */
+ .version_report = NULL
+};
+
+
+#ifdef DYNLOOKUP
+#define readsock_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &readsock_lookup_info };
+lookup_module_info readsock_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+/* End of lookups/readsock.c */
diff --git a/src/lookups/redis.c b/src/lookups/redis.c
new file mode 100644
index 0000000..9c8559c
--- /dev/null
+++ b/src/lookups/redis.c
@@ -0,0 +1,471 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+
+#ifdef LOOKUP_REDIS
+
+#include "lf_functions.h"
+
+#include <hiredis/hiredis.h>
+
+#ifndef nele
+# define nele(arr) (sizeof(arr) / sizeof(*arr))
+#endif
+
+/* 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(const 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))
+ {
+ redis_connections = cn->next;
+ DEBUG(D_lookup) debug_printf_indent("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
+ opts options
+
+ 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(const uschar *command, uschar *server, uschar **resultptr,
+ uschar **errmsg, BOOL *defer_break, uint *do_cache, const uschar * opts)
+{
+redisContext *redis_handle = NULL; /* Keep compilers happy */
+redisReply *redis_reply = NULL;
+redisReply *entry = NULL;
+redisReply *tentry = NULL;
+redis_connection *cn;
+int yield = DEFER;
+int i, j;
+gstring * result = NULL;
+uschar *server_copy = NULL;
+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 (int i = 2; i > 0; i--)
+ {
+ uschar *pp = Ustrrchr(server, '/');
+
+ if (!pp)
+ {
+ *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; cn = cn->next)
+ if (Ustrcmp(cn->server, server_copy) == 0)
+ {
+ redis_handle = cn->handle;
+ break;
+ }
+
+if (!cn)
+ {
+ uschar *p;
+ uschar *socket = NULL;
+ int port = 0;
+ /* int redis_err = REDIS_OK; */
+
+ if ((p = Ustrchr(sdata[0], '(')))
+ {
+ *p++ = 0;
+ socket = p;
+ while (*p != 0 && *p != ')') p++;
+ *p = 0;
+ }
+
+ if ((p = Ustrchr(sdata[0], ':')))
+ {
+ *p++ = 0;
+ port = Uatoi(p);
+ }
+ else
+ port = Uatoi("6379");
+
+ if (Ustrchr(server, '/'))
+ {
+ *errmsg = string_sprintf("unexpected slash in Redis server hostname: %s",
+ sdata[0]);
+ *defer_break = TRUE;
+ return DEFER;
+ }
+
+ DEBUG(D_lookup)
+ debug_printf_indent("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 ? */
+ redis_handle =
+ socket ? redisConnectUnix(CCS socket) : redisConnect(CCS server, port);
+ if (!redis_handle)
+ {
+ *errmsg = US"REDIS connection failed";
+ *defer_break = FALSE;
+ goto REDIS_EXIT;
+ }
+
+ /* Add the connection to the cache */
+ cn = store_get(sizeof(redis_connection), GET_UNTAINTED);
+ cn->server = server_copy;
+ cn->handle = redis_handle;
+ cn->next = redis_connections;
+ redis_connections = cn;
+ }
+else
+ {
+ DEBUG(D_lookup)
+ debug_printf_indent("REDIS using cached connection for %s\n", server_copy);
+}
+
+/* Authenticate if there is a password */
+if(sdata[2])
+ if (!(redis_reply = redisCommand(redis_handle, "AUTH %s", sdata[2])))
+ {
+ *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])
+ {
+ if (!(redis_reply = redisCommand(redis_handle, "SELECT %s", sdata[1])))
+ {
+ *errmsg = string_sprintf("REDIS: Selecting database=%s failed: %s\n", sdata[1], redis_handle->errstr);
+ *defer_break = FALSE;
+ goto REDIS_EXIT;
+ }
+ DEBUG(D_lookup) debug_printf_indent("REDIS: Selecting database=%s\n", sdata[1]);
+ }
+
+/* split string on whitespace into argv */
+ {
+ uschar * argv[32];
+ const uschar * s = command;
+ int siz, ptr, i;
+ uschar c;
+
+ while (isspace(*s)) s++;
+
+ for (i = 0; *s && i < nele(argv); i++)
+ {
+ gstring * g;
+
+ for (g = NULL; (c = *s) && !isspace(c); s++)
+ if (c != '\\' || *++s) /* backslash protects next char */
+ g = string_catn(g, s, 1);
+ argv[i] = string_from_gstring(g);
+
+ DEBUG(D_lookup) debug_printf_indent("REDIS: argv[%d] '%s'\n", i, argv[i]);
+ while (isspace(*s)) s++;
+ }
+
+ /* Run the command. We use the argv form rather than plain as that parses
+ into args by whitespace yet has no escaping mechanism. */
+
+ if (!(redis_reply = redisCommandArgv(redis_handle, i, CCSS argv, 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);
+
+ /* trap MOVED cluster responses and follow them */
+ if (Ustrncmp(redis_reply->str, "MOVED", 5) == 0)
+ {
+ DEBUG(D_lookup)
+ debug_printf_indent("REDIS: cluster redirect %s\n", redis_reply->str);
+ /* follow redirect
+ This is cheating, we simply set defer_break = FALSE to move on to
+ the next server in the redis_servers list */
+ *defer_break = FALSE;
+ return DEFER;
+ } else {
+ *defer_break = TRUE;
+ }
+ *do_cache = 0;
+ goto REDIS_EXIT;
+ /* NOTREACHED */
+
+ case REDIS_REPLY_NIL:
+ DEBUG(D_lookup)
+ debug_printf_indent("REDIS: query was not one that returned any data\n");
+ result = string_catn(result, US"", 1);
+ *do_cache = 0;
+ goto REDIS_EXIT;
+ /* NOTREACHED */
+
+ case REDIS_REPLY_INTEGER:
+ result = string_cat(result, redis_reply->integer != 0 ? US"true" : US"false");
+ break;
+
+ case REDIS_REPLY_STRING:
+ case REDIS_REPLY_STATUS:
+ result = string_catn(result, 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 (int i = 0; i < redis_reply->elements; i++)
+ {
+ entry = redis_reply->element[i];
+
+ if (result)
+ result = string_catn(result, US"\n", 1);
+
+ switch (entry->type)
+ {
+ case REDIS_REPLY_INTEGER:
+ result = string_fmt_append(result, "%d", entry->integer);
+ break;
+ case REDIS_REPLY_STRING:
+ result = string_catn(result, US entry->str, entry->len);
+ break;
+ case REDIS_REPLY_ARRAY:
+ for (int j = 0; j < entry->elements; j++)
+ {
+ tentry = entry->element[j];
+
+ if (result)
+ result = string_catn(result, US"\n", 1);
+
+ switch (tentry->type)
+ {
+ case REDIS_REPLY_INTEGER:
+ result = string_fmt_append(result, "%d", tentry->integer);
+ break;
+ case REDIS_REPLY_STRING:
+ result = string_catn(result, US tentry->str, tentry->len);
+ break;
+ case REDIS_REPLY_ARRAY:
+ DEBUG(D_lookup)
+ debug_printf_indent("REDIS: result has nesting of arrays which"
+ " is not supported. Ignoring!\n");
+ break;
+ default:
+ DEBUG(D_lookup) debug_printf_indent(
+ "REDIS: result has unsupported type. Ignoring!\n");
+ break;
+ }
+ }
+ break;
+ default:
+ DEBUG(D_lookup) debug_printf_indent("REDIS: query returned unsupported type\n");
+ break;
+ }
+ }
+ break;
+ }
+
+
+if (result)
+ gstring_release_unused(result);
+else
+ {
+ yield = FAIL;
+ *errmsg = US"REDIS: no data found";
+ }
+
+REDIS_EXIT:
+
+/* Free store for any result that was got; don't close the connection,
+as it is cached. */
+
+if (redis_reply) freeReplyObject(redis_reply);
+
+/* Non-NULL result indicates a successful result */
+
+if (result)
+ {
+ *resultptr = string_from_gstring(result);
+ return OK;
+ }
+else
+ {
+ DEBUG(D_lookup) debug_printf_indent("%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)),
+ const uschar * filename __attribute__((unused)),
+ const uschar * command, int length, uschar ** result, uschar ** errmsg,
+ uint * do_cache, const uschar * opts)
+{
+return lf_sqlperform(US"Redis", US"redis_servers", redis_servers, command,
+ result, errmsg, do_cache, opts, perform_redis_search);
+}
+
+
+
+/*************************************************
+* Quote entry point *
+*************************************************/
+
+/* Prefix any whitespace, or backslash, with a backslash.
+This is not a Redis thing but instead to let the argv splitting
+we do to split on whitespace, yet provide means for getting
+whitespace into an argument.
+
+Arguments:
+ s the string to be quoted
+ opt additional option text or NULL if none
+ idx lookup type index
+
+Returns: the processed string or NULL for a bad option
+*/
+
+static uschar *
+redis_quote(uschar * s, uschar * opt, unsigned idx)
+{
+int c, count = 0;
+uschar * t = s, * quoted;
+
+if (opt) return NULL; /* No options recognized */
+
+while ((c = *t++))
+ if (isspace(c) || c == '\\') count++;
+
+t = quoted = store_get_quoted(Ustrlen(s) + count + 1, s, idx);
+
+while ((c = *s++))
+ {
+ if (isspace(c) || c == '\\') *t++ = '\\';
+ *t++ = c;
+ }
+
+*t = 0;
+return quoted;
+}
+
+
+/*************************************************
+* Version reporting entry point *
+*************************************************/
+#include "../version.h"
+
+gstring *
+redis_version_report(gstring * g)
+{
+g = string_fmt_append(g,
+ "Library version: REDIS: Compile: %d [%d]\n", HIREDIS_MAJOR, HIREDIS_MINOR);
+#ifdef DYNLOOKUP
+g = string_fmt_append(g,
+ " Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+
+
+/* These are the lookup_info blocks for this driver */
+static lookup_info redis_lookup_info = {
+ .name = US"redis", /* lookup name */
+ .type = lookup_querystyle, /* query-style lookup */
+ .open = redis_open, /* open function */
+ .check = NULL, /* no check function */
+ .find = redis_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = redis_tidy, /* tidy function */
+ .quote = redis_quote, /* quoting function */
+ .version_report = 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 /* LOOKUP_REDIS */
+/* End of lookups/redis.c */
diff --git a/src/lookups/spf.c b/src/lookups/spf.c
new file mode 100644
index 0000000..78d954c
--- /dev/null
+++ b/src/lookups/spf.c
@@ -0,0 +1,159 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Exim - SPF lookup module using libspf2
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Copyright (c) The Exim Maintainers 2020 - 2022
+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 SUPPORT_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"
+#if !defined(HAVE_NS_TYPE) && defined(NS_INADDRSZ)
+# define HAVE_NS_TYPE
+#endif
+#include <spf2/spf.h>
+#include <spf2/spf_dns_resolv.h>
+#include <spf2/spf_dns_cache.h>
+
+extern SPF_dns_server_t * SPF_dns_exim_new(int);
+
+
+static void *
+spf_open(const uschar * filename, uschar ** errmsg)
+{
+SPF_dns_server_t * dc;
+SPF_server_t *spf_server = NULL;
+int debug = 0;
+
+DEBUG(D_lookup) debug = 1;
+
+if ((dc = SPF_dns_exim_new(debug)))
+ if ((dc = SPF_dns_cache_new(dc, NULL, debug, 8)))
+ spf_server = SPF_server_new_dns(dc, debug);
+
+if (!spf_server)
+ {
+ *errmsg = US"SPF_dns_exim_nnew() 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, const uschar * filename, const uschar * keystring,
+ int key_len, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+SPF_server_t *spf_server = handle;
+SPF_request_t *spf_request;
+SPF_response_t *spf_response = NULL;
+
+if (!(spf_request = SPF_request_new(spf_server)))
+ {
+ *errmsg = US"SPF_request_new() failed";
+ return FAIL;
+ }
+
+#if HAVE_IPV6
+switch (string_is_ip_address(filename, NULL))
+#else
+switch (4)
+#endif
+ {
+ case 4:
+ if (!SPF_request_set_ipv4_str(spf_request, CS filename))
+ break;
+ *errmsg = string_sprintf("invalid IPv4 address '%s'", filename);
+ return FAIL;
+#if HAVE_IPV6
+
+ case 6:
+ if (!SPF_request_set_ipv6_str(spf_request, CS filename))
+ break;
+ *errmsg = string_sprintf("invalid IPv6 address '%s'", filename);
+ return FAIL;
+
+ default:
+ *errmsg = string_sprintf("invalid IP address '%s'", filename);
+ return FAIL;
+#endif
+ }
+
+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)));
+
+DEBUG(D_lookup) spf_response_debug(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"
+
+gstring *
+spf_version_report(gstring * g)
+{
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, "Library version: SPF: Exim version %s\n", EXIM_VERSION_STR));
+#endif
+return g;
+}
+
+
+static lookup_info _lookup_info = {
+ .name = US"spf", /* lookup name */
+ .type = 0, /* not absfile, not query style */
+ .open = spf_open, /* open function */
+ .check = NULL, /* no check function */
+ .find = spf_find, /* find function */
+ .close = spf_close, /* close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = 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 /* SUPPORT_SPF */
diff --git a/src/lookups/sqlite.c b/src/lookups/sqlite.c
new file mode 100644
index 0000000..9080ae7
--- /dev/null
+++ b/src/lookups/sqlite.c
@@ -0,0 +1,200 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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(const uschar * filename, uschar ** errmsg)
+{
+sqlite3 *db = NULL;
+int ret;
+
+if (!filename || !*filename)
+ {
+ DEBUG(D_lookup) debug_printf_indent("Using sqlite_dbfile: %s\n", sqlite_dbfile);
+ filename = sqlite_dbfile;
+ }
+if (!filename || *filename != '/')
+ *errmsg = US"absolute file name expected for \"sqlite\" lookup";
+else if ((ret = sqlite3_open(CCS filename, &db)) != 0)
+ {
+ *errmsg = (void *)sqlite3_errmsg(db);
+ sqlite3_close(db);
+ db = NULL;
+ DEBUG(D_lookup) debug_printf_indent("Error opening database: %s\n", *errmsg);
+ }
+
+if (db)
+ sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout);
+return db;
+}
+
+
+/*************************************************
+* Find entry point *
+*************************************************/
+
+/* See local README for interface description. */
+
+static int
+sqlite_callback(void *arg, int argc, char **argv, char **azColName)
+{
+gstring * res = *(gstring **)arg;
+
+/* For second and subsequent results, insert \n */
+
+if (res)
+ res = string_catn(res, US"\n", 1);
+
+if (argc > 1)
+ {
+ /* For multiple fields, include the field name too */
+ for (int i = 0; i < argc; i++)
+ {
+ uschar * value = US(argv[i] ? argv[i] : "<NULL>");
+ res = lf_quote(US azColName[i], value, Ustrlen(value), res);
+ }
+ }
+
+else
+ res = string_cat(res, argv[0] ? US argv[0] : US "<NULL>");
+
+/* always return a non-null gstring, even for a zero-length string result */
+*(gstring **)arg = res ? res : string_get(1);
+return 0;
+}
+
+
+static int
+sqlite_find(void * handle, const uschar * filename, const uschar * query,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+int ret;
+gstring * res = NULL;
+
+ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, CSS errmsg);
+if (ret != SQLITE_OK)
+ {
+ debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg);
+ return FAIL;
+ }
+
+if (!res) *do_cache = 0; /* on fail, wipe cache */
+
+*result = string_from_gstring(res);
+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
+ idx lookup type index
+
+Returns: the processed string or NULL for a bad option
+*/
+
+static uschar *
+sqlite_quote(uschar * s, uschar * opt, unsigned idx)
+{
+int c, count = 0;
+uschar * t = s, * quoted;
+
+if (opt) return NULL; /* No options recognized */
+
+while ((c = *t++)) if (c == '\'') count++;
+count += t - s;
+
+t = quoted = store_get_quoted(count + 1, s, idx);
+
+while ((c = *s++))
+ {
+ if (c == '\'') *t++ = '\'';
+ *t++ = c;
+ }
+
+*t = 0;
+return quoted;
+}
+
+
+
+/*************************************************
+* Version reporting entry point *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+gstring *
+sqlite_version_report(gstring * g)
+{
+g = string_fmt_append(g,
+ "Library version: SQLite: Compile: %s\n"
+ " Runtime: %s\n",
+ SQLITE_VERSION, sqlite3_libversion());
+#ifdef DYNLOOKUP
+g = string_fmt_append(g,
+ " Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+static lookup_info _lookup_info = {
+ .name = US"sqlite", /* lookup name */
+ .type = lookup_absfilequery, /* query-style lookup, starts with file name */
+ .open = sqlite_open, /* open function */
+ .check = NULL, /* no check function */
+ .find = sqlite_find, /* find function */
+ .close = sqlite_close, /* close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = sqlite_quote, /* quoting function */
+ .version_report = 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
index 0000000..4824161
--- /dev/null
+++ b/src/lookups/testdb.c
@@ -0,0 +1,100 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* 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(const uschar * filename, uschar ** errmsg)
+{
+return (void *)(1); /* Just return something non-null */
+}
+
+
+
+/*************************************************
+* Find entry point *
+*************************************************/
+
+/* See local README for interface description. */
+
+static int
+testdb_find(void * handle, const uschar * filename, const uschar * query,
+ int length, uschar ** result, uschar ** errmsg, uint * do_cache,
+ const uschar * opts)
+{
+if (Ustrcmp(query, "fail") == 0)
+ {
+ *errmsg = US"testdb lookup forced FAIL";
+ DEBUG(D_lookup) debug_printf_indent("%s\n", *errmsg);
+ return FAIL;
+ }
+if (Ustrcmp(query, "defer") == 0)
+ {
+ *errmsg = US"testdb lookup forced DEFER";
+ DEBUG(D_lookup) debug_printf_indent("%s\n", *errmsg);
+ return DEFER;
+ }
+
+if (Ustrcmp(query, "nocache") == 0) *do_cache = 0;
+
+*result = string_copy(query);
+return OK;
+}
+
+
+/*************************************************
+* Version reporting entry point *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+gstring *
+testdb_version_report(gstring * g)
+{
+#ifdef DYNLOOKUP
+g = string_fmt_append(g, "Library version: TestDB: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+
+static lookup_info _lookup_info = {
+ .name = US"testdb", /* lookup name */
+ .type = lookup_querystyle, /* query-style lookup */
+ .open = testdb_open, /* open function */
+ .check = NULL, /* check function */
+ .find = testdb_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = 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
index 0000000..990703f
--- /dev/null
+++ b/src/lookups/whoson.c
@@ -0,0 +1,98 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* 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(const uschar * filename, uschar ** errmsg)
+{
+return (void *)(1); /* Just return something non-null */
+}
+
+
+/*************************************************
+* Find entry point *
+*************************************************/
+
+/* See local README for interface description. */
+
+static int
+whoson_find(void * handle, const uschar * filename, uschar * query, int length,
+ uschar ** result, uschar ** errmsg, uint * do_cache, const uschar * opts)
+{
+uschar buffer[80];
+
+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"
+
+gstring *
+whoson_version_report(gstring * g)
+{
+g = string_fmt_append(g,
+ "Library version: Whoson: Runtime: %s\n", wso_version());
+#ifdef DYNLOOKUP
+g = string_fmt_append(g,
+ " Exim version %s\n", EXIM_VERSION_STR);
+#endif
+return g;
+}
+
+static lookup_info _lookup_info = {
+ .name = US"whoson", /* lookup name */
+ .type = lookup_querystyle, /* query-style lookup */
+ .open = whoson_open, /* open function */
+ .check = NULL, /* check function */
+ .find = whoson_find, /* find function */
+ .close = NULL, /* no close function */
+ .tidy = NULL, /* no tidy function */
+ .quote = NULL, /* no quoting function */
+ .version_report = 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
index 0000000..167522d
--- /dev/null
+++ b/src/lss.c
@@ -0,0 +1,142 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* 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(CUS domain, CUSS &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(CUS local_part, CUSS &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(CUS address, caseless, TRUE, CUSS &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(CUSS &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 b64encode(CUS 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 b64decode(code, ptr);
+}
+
+
+/* End of lss.c */
diff --git a/src/macro_predef.c b/src/macro_predef.c
new file mode 100644
index 0000000..a0c659c
--- /dev/null
+++ b/src/macro_predef.c
@@ -0,0 +1,345 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) Jeremy Harris 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Create a static data structure with the predefined macros, to be
+included in the main Exim build */
+
+#include "exim.h"
+#include "macro_predef.h"
+
+unsigned mp_index = 0;
+
+/* Global dummy variables */
+
+void fn_smtp_receive_timeout(const uschar * name, const uschar * str) {}
+uschar * syslog_facility_str;
+
+/******************************************************************************/
+
+void
+builtin_macro_create_var(const uschar * name, const uschar * val)
+{
+printf ("static macro_item p%d = { ", mp_index);
+if (mp_index == 0)
+ printf(".next=NULL,");
+else
+ printf(".next=&p%d,", mp_index-1);
+
+printf(" .command_line=FALSE, .namelen=%d, .replen=%d,"
+ " .name=US\"%s\", .replacement=US\"%s\" };\n",
+ Ustrlen(name), Ustrlen(val), CS name, CS val);
+mp_index++;
+}
+
+
+void
+builtin_macro_create(const uschar * name)
+{
+builtin_macro_create_var(name, US"y");
+}
+
+
+/* restricted snprintf */
+void
+spf(uschar * buf, int len, const uschar * fmt, ...)
+{
+va_list ap;
+va_start(ap, fmt);
+
+while (*fmt && len > 1)
+ if (*fmt == '%' && fmt[1] == 'T')
+ {
+ uschar * s = va_arg(ap, uschar *);
+ while (*s && len-- > 1)
+ *buf++ = toupper(*s++);
+ fmt += 2;
+ }
+ else
+ {
+ *buf++ = *fmt++; len--;
+ }
+*buf = '\0';
+va_end(ap);
+}
+
+void
+options_from_list(optionlist * opts, unsigned nopt,
+ const uschar * section, uschar * group)
+{
+const uschar * s;
+uschar buf[EXIM_DRIVERNAME_MAX];
+
+/* The 'previously-defined-substring' rule for macros in config file
+lines is done thus for these builtin macros: we know that the table
+we source from is in strict alpha order, hence the builtins portion
+of the macros list is in reverse-alpha (we prepend them) - so longer
+macros that have substrings are always discovered first during
+expansion. */
+
+for (int i = 0; i < nopt; i++) if (*(s = US opts[i].name) && *s != '*')
+ {
+ if (group)
+ spf(buf, sizeof(buf), CUS"_OPT_%T_%T_%T", section, group, s);
+ else
+ spf(buf, sizeof(buf), CUS"_OPT_%T_%T", section, s);
+ builtin_macro_create(buf);
+ }
+}
+
+
+/******************************************************************************/
+
+
+/* Create compile-time feature macros */
+static void
+features(void)
+{
+/* Probably we could work out a static initialiser for wherever
+macros are stored, but this will do for now. Some names are awkward
+due to conflicts with other common macros. */
+
+#ifdef SUPPORT_CRYPTEQ
+ builtin_macro_create(US"_HAVE_CRYPTEQ");
+#endif
+#if HAVE_ICONV
+ builtin_macro_create(US"_HAVE_ICONV");
+#endif
+#if HAVE_IPV6
+ builtin_macro_create(US"_HAVE_IPV6");
+#endif
+#ifdef HAVE_SETCLASSRESOURCES
+ builtin_macro_create(US"_HAVE_SETCLASSRESOURCES");
+#endif
+#ifdef SUPPORT_PAM
+ builtin_macro_create(US"_HAVE_PAM");
+#endif
+#ifdef EXIM_PERL
+ builtin_macro_create(US"_HAVE_PERL");
+#endif
+#ifdef EXPAND_DLFUNC
+ builtin_macro_create(US"_HAVE_DLFUNC");
+#endif
+#ifdef USE_TCP_WRAPPERS
+ builtin_macro_create(US"_HAVE_TCPWRAPPERS");
+#endif
+#ifndef DISABLE_TLS
+ builtin_macro_create(US"_HAVE_TLS");
+# ifdef USE_GNUTLS
+ builtin_macro_create(US"_HAVE_GNUTLS");
+# else
+ builtin_macro_create(US"_HAVE_OPENSSL");
+# endif
+#endif
+#ifdef SUPPORT_TRANSLATE_IP_ADDRESS
+ builtin_macro_create(US"_HAVE_TRANSLATE_IP_ADDRESS");
+#endif
+#ifdef SUPPORT_MOVE_FROZEN_MESSAGES
+ builtin_macro_create(US"_HAVE_MOVE_FROZEN_MESSAGES");
+#endif
+#ifdef WITH_CONTENT_SCAN
+ builtin_macro_create(US"_HAVE_CONTENT_SCANNING");
+#endif
+#ifndef DISABLE_DKIM
+ builtin_macro_create(US"_HAVE_DKIM");
+#endif
+#ifdef SUPPORT_DMARC
+ builtin_macro_create(US"_HAVE_DMARC");
+#endif
+#ifndef DISABLE_DNSSEC
+ builtin_macro_create(US"_HAVE_DNSSEC");
+#endif
+#ifndef DISABLE_EVENT
+ builtin_macro_create(US"_HAVE_EVENT");
+#endif
+#ifdef SUPPORT_I18N
+ builtin_macro_create(US"_HAVE_I18N");
+#endif
+#ifndef DISABLE_OCSP
+ builtin_macro_create(US"_HAVE_OCSP");
+#endif
+#ifndef DISABLE_PIPE_CONNECT
+ builtin_macro_create(US"_HAVE_PIPE_CONNECT");
+#endif
+#ifndef DISABLE_PRDR
+ builtin_macro_create(US"_HAVE_PRDR");
+#endif
+#ifdef SUPPORT_PROXY
+ builtin_macro_create(US"_HAVE_PROXY");
+#endif
+#ifdef SUPPORT_SOCKS
+ builtin_macro_create(US"_HAVE_SOCKS");
+#endif
+#if defined(SUPPORT_SRS)
+ builtin_macro_create(US"_HAVE_NATIVE_SRS"); /* beware clash with _HAVE_SRS */
+#endif
+#ifdef TCP_FASTOPEN
+ builtin_macro_create(US"_HAVE_TCP_FASTOPEN");
+#endif
+#ifdef SUPPORT_SPF
+ builtin_macro_create(US"_HAVE_SPF");
+#endif
+#ifdef SUPPORT_SRS
+ builtin_macro_create(US"_HAVE_SRS");
+#endif
+#ifdef EXPERIMENTAL_ARC
+ builtin_macro_create(US"_HAVE_ARC");
+#endif
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+ builtin_macro_create(US"_HAVE_BRIGHTMAIL");
+#endif
+#ifdef SUPPORT_DANE
+ builtin_macro_create(US"_HAVE_DANE");
+#endif
+#ifdef EXPERIMENTAL_DCC
+ builtin_macro_create(US"_HAVE_DCC");
+#endif
+#ifdef EXPERIMENTAL_DSN_INFO
+ builtin_macro_create(US"_HAVE_DSN_INFO");
+#endif
+#ifndef DISABLE_TLS_RESUME
+ builtin_macro_create(US"_HAVE_TLS_RESUME");
+#endif
+
+#ifdef LOOKUP_LSEARCH
+ builtin_macro_create(US"_HAVE_LOOKUP_LSEARCH");
+#endif
+#ifdef LOOKUP_CDB
+ builtin_macro_create(US"_HAVE_LOOKUP_CDB");
+#endif
+#ifdef LOOKUP_DBM
+ builtin_macro_create(US"_HAVE_LOOKUP_DBM");
+#endif
+#ifdef LOOKUP_DNSDB
+ builtin_macro_create(US"_HAVE_LOOKUP_DNSDB");
+#endif
+#ifdef LOOKUP_DSEARCH
+ builtin_macro_create(US"_HAVE_LOOKUP_DSEARCH");
+#endif
+#ifdef LOOKUP_IBASE
+ builtin_macro_create(US"_HAVE_LOOKUP_IBASE");
+#endif
+#ifdef LOOKUP_LMDB
+ builtin_macro_create(US"_HAVE_LMDB");
+ builtin_macro_create(US"_HAVE_LOOKUP_LMDB");
+#endif
+#ifdef LOOKUP_LDAP
+ builtin_macro_create(US"_HAVE_LOOKUP_JSON");
+#endif
+#ifdef LOOKUP_LDAP
+ builtin_macro_create(US"_HAVE_LOOKUP_LDAP");
+#endif
+#ifdef LOOKUP_MYSQL
+ builtin_macro_create(US"_HAVE_LOOKUP_MYSQL");
+#endif
+#ifdef LOOKUP_NIS
+ builtin_macro_create(US"_HAVE_LOOKUP_NIS");
+#endif
+#ifdef LOOKUP_NISPLUS
+ builtin_macro_create(US"_HAVE_LOOKUP_NISPLUS");
+#endif
+#ifdef LOOKUP_ORACLE
+ builtin_macro_create(US"_HAVE_LOOKUP_ORACLE");
+#endif
+#ifdef LOOKUP_PASSWD
+ builtin_macro_create(US"_HAVE_LOOKUP_PASSWD");
+#endif
+#ifdef LOOKUP_PGSQL
+ builtin_macro_create(US"_HAVE_LOOKUP_PGSQL");
+#endif
+#ifdef LOOKUP_REDIS
+ builtin_macro_create(US"_HAVE_LOOKUP_REDIS");
+#endif
+#ifdef LOOKUP_SQLITE
+ builtin_macro_create(US"_HAVE_LOOKUP_SQLITE");
+#endif
+#ifdef LOOKUP_TESTDB
+ builtin_macro_create(US"_HAVE_LOOKUP_TESTDB");
+#endif
+#ifdef LOOKUP_WHOSON
+ builtin_macro_create(US"_HAVE_LOOKUP_WHOSON");
+#endif
+
+#ifdef TRANSPORT_APPENDFILE
+# ifdef SUPPORT_MAILDIR
+ builtin_macro_create(US"_HAVE_TRANSPORT_APPEND_MAILDIR");
+# endif
+# ifdef SUPPORT_MAILSTORE
+ builtin_macro_create(US"_HAVE_TRANSPORT_APPEND_MAILSTORE");
+# endif
+# ifdef SUPPORT_MBX
+ builtin_macro_create(US"_HAVE_TRANSPORT_APPEND_MBX");
+# endif
+#endif
+
+features_acl();
+features_crypto();
+
+#ifdef WITH_CONTENT_SCAN
+features_malware();
+#endif
+}
+
+static void
+exp_features(void)
+{
+#ifdef EXPERIMENTAL_ARC
+ builtin_macro_create(US"_EXP_ARC");
+#endif
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+ builtin_macro_create(US"_EXP_BMI");
+#endif
+#ifdef EXPERIMENTAL_DCC
+ builtin_macro_create(US"_EXP_DCC");
+#endif
+#ifdef EXPERIMENTAL_DSN_INFO
+ builtin_macro_create(US"_EXP_DSNI");
+#endif
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ builtin_macro_create(US"_EXP_LIMITS");
+#endif
+#ifdef EXPERIMENTAL_QUEUEFILE
+ builtin_macro_create(US"_EXP_QUEUEFILE");
+#endif
+}
+
+
+static void
+options(void)
+{
+options_main();
+options_routers();
+options_transports();
+options_auths();
+options_logging();
+#ifndef DISABLE_TLS
+options_tls();
+#endif
+}
+
+static void
+params(void)
+{
+#ifndef DISABLE_DKIM
+params_dkim();
+#endif
+}
+
+
+int
+main(void)
+{
+printf("#include \"exim.h\"\n");
+features();
+exp_features();
+options();
+params();
+
+printf("macro_item * macros = &p%d;\n", mp_index-1);
+printf("macro_item * mlast = &p0;\n");
+exit(0);
+}
diff --git a/src/macro_predef.h b/src/macro_predef.h
new file mode 100644
index 0000000..59b1bbe
--- /dev/null
+++ b/src/macro_predef.h
@@ -0,0 +1,28 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Jeremy Harris 2017 - 2018 */
+/* Copyright (c) The Exim Maintainers 2021 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Global functions */
+
+extern void spf(uschar *, int, const uschar *, ...);
+extern void builtin_macro_create(const uschar *);
+extern void builtin_macro_create_var(const uschar *, const uschar *);
+extern void options_from_list(optionlist *, unsigned, const uschar *, uschar *);
+
+extern void features_acl(void);
+extern void features_malware(void);
+extern void features_crypto(void);
+extern void options_main(void);
+extern void options_routers(void);
+extern void options_transports(void);
+extern void options_auths(void);
+extern void options_logging(void);
+extern void params_dkim(void);
+#ifndef DISABLE_TLS
+extern void options_tls(void);
+#endif
+
diff --git a/src/macros.h b/src/macros.h
new file mode 100644
index 0000000..fa89de1
--- /dev/null
+++ b/src/macros.h
@@ -0,0 +1,1117 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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)
+
+/* Number of elements of an array */
+#define nelem(arr) (sizeof(arr) / sizeof(*arr))
+
+/* Maximum of two items */
+#ifndef MAX
+# define MAX(a,b) ((a) > (b) ? (a) : (b))
+#endif
+
+
+/* When running in the test harness, the load average is fudged. */
+
+#define OS_GETLOADAVG() \
+ (f.running_in_test_harness? (test_harness_load_avg += 10) : os_getloadavg())
+
+
+/* The address_item structure has a struct full of 1-bit flags. These macros
+manipulate them. */
+
+#define setflag(addr, flagname) addr->flags.flagname = TRUE
+#define clearflag(addr, flagname) addr->flags.flagname = FALSE
+
+#define testflag(addr, flagname) (addr->flags.flagname)
+
+#define copyflag(addrnew, addrold, flagname) \
+ addrnew->flags.flagname = addrold->flags.flagname
+
+
+/* For almost all calls to convert things to printing characters, we want to
+allow tabs & spaces. A macro just makes life a bit easier. */
+
+#define string_printing(s) string_printing2((s), 0)
+#define SP_TAB BIT(0)
+#define SP_SPACE BIT(1)
+
+
+/* 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))
+
+
+/* When built with TLS support, the act of flushing SMTP output becomes
+a no-op once an SSL session is in progress. */
+
+#ifndef DISABLE_TLS
+#define mac_smtp_fflush() if (tls_in.active.sock < 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 LOG_NAME_SIZE 256
+#define DEBUG(x) if (debug_selector & (x))
+#define HDEBUG(x) if (host_checking || debug_selector & (x))
+
+/* 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 19
+
+/* 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 384
+
+/* 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
+#define SMTP_HBUFF_PREV(n) ((n) ? (n)-1 : SMTP_HBUFF_SIZE-1)
+
+/* 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 PLUS room for string additions.
+Let's go with "at least twice as large as maximum path length".
+*/
+
+#ifdef AUTH_HEIMDAL_GSSAPI
+ /* RFC 4121 section 5.2, SHOULD support 64K input buffers */
+# define __BIG_BUFFER_SIZE 65536
+#else
+# define __BIG_BUFFER_SIZE 16384
+#endif
+
+#ifndef PATH_MAX
+/* exim.h will have ensured this exists before including us. */
+# error headers confusion, PATH_MAX missing in macros.h
+#endif
+#if (PATH_MAX*2) > __BIG_BUFFER_SIZE
+# define BIG_BUFFER_SIZE (PATH_MAX*2)
+#else
+# define BIG_BUFFER_SIZE __BIG_BUFFER_SIZE
+#endif
+
+/* header size of pipe content
+ currently: char id, char subid, char[5] length */
+#define PIPE_HEADER_SIZE 7
+
+/* 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)
+
+/* 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, and the max number of continuation records allowed. */
+
+#define WAIT_NAME_MAX 50
+#define WAIT_CONT_MAX 1000
+
+/* 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) (regex_match(regex_ismsgid, (s), -1, NULL))
+
+
+/* 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 {
+ EXIM_DTYPE_NONE,
+ EXIM_DTYPE_ROUTER,
+ EXIM_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 SUPPORT_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.
+
+Use rc_names[] for debug strings. */
+
+#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) */
+#define DANE 16 /* Deferred for domain mismatch (used in transport) */
+
+/* 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. Use dns_rc_names[] for debug strings */
+
+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 */
+#define END_PROTOCOL 6 /* Protocol error in CHUNKING sequence */
+
+/* result codes for bdat_getc() (which can also return EOF) */
+
+#define EOD (-2)
+#define ERR (-3)
+
+
+/* Bit masks for debug and log selectors */
+
+/* Assume words are 32 bits wide. Tiny waste of space on 64 bit
+platforms, but this ensures bit vectors always work the same way. */
+#define BITWORDSIZE 32
+
+/* This macro is for single-word bit vectors: the debug selector,
+and the first word of the log selector. */
+#define BIT(n) (1UL << (n))
+
+/* And these are for multi-word vectors. */
+#define BITWORD(n) ( (n) / BITWORDSIZE)
+#define BITMASK(n) (1U << (n) % BITWORDSIZE)
+
+#define BIT_CLEAR(s,z,n) ((s)[BITWORD(n)] &= ~BITMASK(n))
+#define BIT_SET(s,z,n) ((s)[BITWORD(n)] |= BITMASK(n))
+#define BIT_TEST(s,z,n) (((s)[BITWORD(n)] & BITMASK(n)) != 0)
+
+/* Used in globals.c for initializing bit_table structures. T will be either
+D or L corresponding to the debug and log selector bits declared below. */
+
+#define BIT_TABLE(T,name) { US #name, T##i_##name }
+
+/* IOTA allows us to keep an implicit sequential count, like a simple enum,
+but we can have sequentially numbered identifiers which are not declared
+sequentially. We use this for more compact declarations of bit indexes and
+masks, alternating between sequential bit index and corresponding mask. */
+
+#define IOTA(iota) (__LINE__ - iota)
+#define IOTA_INIT(zero) (__LINE__ - zero + 1)
+
+/* Options bits for debugging. DEBUG_BIT() declares both a bit index and the
+corresponding mask. Di_all is a special value recognized by decode_bits().
+These must match the debug_options table in globals.c .
+
+Exim's code assumes in a number of places that the debug_selector is one
+word, and this is exposed in the local_scan ABI. The D_v and D_local_scan bit
+masks are part of the local_scan API so are #defined in local_scan.h */
+
+#define DEBUG_BIT(name) Di_##name = IOTA(Di_iota), D_##name = (int)BIT(Di_##name)
+
+enum {
+ Di_all = -1,
+ Di_v = 0,
+ Di_local_scan = 1,
+
+ Di_iota = IOTA_INIT(2),
+ DEBUG_BIT(acl), /* 2 */
+ DEBUG_BIT(auth),
+ DEBUG_BIT(deliver),
+ DEBUG_BIT(dns),
+ DEBUG_BIT(dnsbl),
+ DEBUG_BIT(exec), /* 7 */
+ DEBUG_BIT(expand),
+ DEBUG_BIT(filter),
+ DEBUG_BIT(hints_lookup),
+ DEBUG_BIT(host_lookup),
+ DEBUG_BIT(ident),
+ DEBUG_BIT(interface),
+ DEBUG_BIT(lists),
+ DEBUG_BIT(load), /* 15 */
+ DEBUG_BIT(lookup),
+ DEBUG_BIT(memory),
+ DEBUG_BIT(noutf8),
+ DEBUG_BIT(pid),
+ DEBUG_BIT(process_info),
+ DEBUG_BIT(queue_run),
+ DEBUG_BIT(receive),
+ DEBUG_BIT(resolver), /* 23 */
+ DEBUG_BIT(retry),
+ DEBUG_BIT(rewrite),
+ DEBUG_BIT(route),
+ DEBUG_BIT(timestamp),
+ DEBUG_BIT(tls),
+ DEBUG_BIT(transport),
+ DEBUG_BIT(uid),
+ DEBUG_BIT(verify), /* 31 */
+};
+
+/* Multi-bit debug masks */
+
+#define D_all 0xffffffff
+
+#define D_any (D_all & \
+ ~(D_v | \
+ D_noutf8 | \
+ D_pid | \
+ D_timestamp) )
+
+#define D_default (0xffffffff & \
+ ~(D_expand | \
+ D_filter | \
+ D_interface | \
+ D_load | \
+ D_local_scan | \
+ D_memory | \
+ D_noutf8 | \
+ D_pid | \
+ D_timestamp | \
+ D_resolver))
+
+/* Bits for debug triggers */
+
+enum {
+ DTi_panictrigger,
+ DTi_pretrigger,
+};
+
+/* Options bits for logging. Those that have values < BITWORDSIZE can be used
+in calls to log_write(). The others are put into later words in log_selector
+and are only ever tested independently, so they do not need bit mask
+declarations. The Li_all value is recognized specially by decode_bits().
+Add also to log_options[] when creating new ones. */
+
+#define LOG_BIT(name) Li_##name = IOTA(Li_iota), L_##name = BIT(Li_##name)
+
+enum logbit {
+ Li_all = -1,
+
+ Li_iota = IOTA_INIT(0),
+ LOG_BIT(address_rewrite),
+ LOG_BIT(all_parents),
+ LOG_BIT(connection_reject),
+ LOG_BIT(delay_delivery),
+ LOG_BIT(dnslist_defer),
+ LOG_BIT(etrn),
+ LOG_BIT(host_lookup_failed),
+ LOG_BIT(lost_incoming_connection),
+ LOG_BIT(queue_run),
+ LOG_BIT(retry_defer),
+ LOG_BIT(size_reject),
+ LOG_BIT(skip_delivery),
+ LOG_BIT(smtp_connection),
+ LOG_BIT(smtp_incomplete_transaction),
+ LOG_BIT(smtp_protocol_error),
+ LOG_BIT(smtp_syntax_error),
+
+ Li_8bitmime = BITWORDSIZE,
+ Li_acl_warn_skipped,
+ Li_arguments,
+ Li_deliver_time,
+ Li_delivery_size,
+ Li_dkim,
+ Li_dkim_verbose,
+ Li_dnssec,
+ Li_ident_timeout,
+ Li_incoming_interface,
+ Li_incoming_port,
+ Li_millisec,
+ Li_msg_id,
+ Li_msg_id_created,
+ Li_outgoing_interface,
+ Li_outgoing_port,
+ Li_pid,
+ Li_pipelining,
+ Li_protocol_detail,
+ Li_proxy,
+ Li_queue_time,
+ Li_queue_time_exclusive,
+ Li_queue_time_overall,
+ Li_receive_time,
+ Li_received_sender,
+ Li_received_recipients,
+ Li_rejected_header,
+ Li_return_path_on_delivery,
+ Li_sender_on_delivery,
+ Li_sender_verify_fail,
+ Li_smtp_confirmation,
+ Li_smtp_mailauth,
+ Li_smtp_no_mail,
+ Li_subject,
+ Li_tls_certificate_verified,
+ Li_tls_cipher,
+ Li_tls_peerdn,
+ Li_tls_resumption,
+ Li_tls_sni,
+ Li_unknown_in_list,
+
+ log_selector_size = BITWORD(Li_unknown_in_list) + 1
+};
+
+#define LOGGING(opt) BIT_TEST(log_selector, log_selector_size, Li_##opt)
+
+/* Private error numbers for delivery failures, set negative so as not
+to conflict with system errno values. Take care to maintain the string
+table exim_errstrings[] in log.c */
+
+#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 routing or in smtp tpt */
+#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 */
+#define ERRNO_AUTHPROB (-48) /* Authenticator "other" failure */
+#define ERRNO_UTF8_FWD (-49) /* target not supporting SMTPUTF8 */
+#define ERRNO_HOST_IS_LOCAL (-50) /* Transport refuses to talk to localhost */
+#define ERRNO_TAINT (-51) /* Transport refuses to talk use tainted filename */
+
+/* These must be last, so all retry deferments can easily be identified */
+
+#define ERRNO_RETRY_BASE (-52) /* Base to test against */
+#define ERRNO_RRETRY (-52) /* Not time for routing */
+
+#define ERRNO_WARN_BASE (-53) /* Base to test against */
+#define ERRNO_LRETRY (-53) /* Not time for local delivery */
+#define ERRNO_HRETRY (-54) /* Not time for any remote host */
+#define ERRNO_LOCAL_ONLY (-55) /* Local-only delivery */
+#define ERRNO_QUEUE_DOMAIN (-56) /* Domain in queue_domains */
+#define ERRNO_TRETRY (-57) /* Transport concurrency limit */
+#define ERRNO_EVENT (-58) /* Event processing request alternate response */
+
+
+
+/* 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_func, 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_callout_hold 0x0200 /* lazy close connection */
+#define vopt_success_on_redirect 0x0400
+#define vopt_quota 0x0800 /* quota check, to local/appendfile */
+
+/* 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_insecure, hwhy_failed, hwhy_deferred,
+ hwhy_ignored };
+
+/* Domain lookup types for routers */
+
+#define LK_DEFAULT BIT(0)
+#define LK_BYNAME BIT(1)
+#define LK_BYDNS BIT(2) /* those 3 should be mutually exclusive */
+
+#define LK_IPV4_ONLY BIT(3)
+#define LK_IPV4_PREFER BIT(4)
+
+/* 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_BDAT,
+ 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_FIND_SECURITY, /* dnssec required but not acheived */
+ 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 BIT(0)
+#define HOST_FIND_BY_MX BIT(1)
+#define HOST_FIND_BY_A BIT(2)
+#define HOST_FIND_BY_AAAA BIT(3)
+#define HOST_FIND_QUALIFY_SINGLE BIT(4)
+#define HOST_FIND_SEARCH_PARENTS BIT(5)
+#define HOST_FIND_IPV4_FIRST BIT(6)
+#define HOST_FIND_IPV4_ONLY BIT(7)
+
+/* 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, MSG_SETQUEUE,
+ /* 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 0x0001
+#define topt_add_delivery_date 0x0002
+#define topt_add_envelope_to 0x0004
+#define topt_escape_headers 0x0008 /* Apply escape check to headers */
+#define topt_use_crlf 0x0010 /* Terminate lines with CRLF */
+#define topt_no_headers 0x0020 /* Omit headers */
+#define topt_no_body 0x0040 /* Omit body */
+#define topt_end_dot 0x0080 /* Send terminating dot line */
+#define topt_no_flush 0x0100 /* more data expected after message (eg QUIT) */
+#define topt_use_bdat 0x0200 /* prepend chunks with RFC3030 BDAT header */
+#define topt_output_string 0x0400 /* create string rather than write to fd */
+#define topt_continuation 0x0800 /* do not reset buffer */
+#define topt_not_socket 0x1000 /* cannot do socket-only syscalls */
+
+/* Options for smtp_write_command */
+
+enum {
+ SCMD_FLUSH = 0, /* write to kernel */
+ SCMD_MORE, /* write to kernel, but likely more soon */
+ SCMD_BUFFER /* stash in application cmd output buffer */
+};
+
+/* 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
+
+
+/* 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 */
+ };
+
+#define ACL_BIT_RCPT BIT(ACL_WHERE_RCPT)
+#define ACL_BIT_MAIL BIT(ACL_WHERE_MAIL)
+#define ACL_BIT_PREDATA BIT(ACL_WHERE_PREDATA)
+#define ACL_BIT_MIME BIT(ACL_WHERE_MIME)
+#define ACL_BIT_DKIM BIT(ACL_WHERE_DKIM)
+#define ACL_BIT_DATA BIT(ACL_WHERE_DATA)
+#ifdef DISABLE_PRDR
+# define ACL_BIT_PRDR 0
+#else
+# define ACL_BIT_PRDR BIT(ACL_WHERE_PRDR)
+#endif
+#define ACL_BIT_NOTSMTP BIT(ACL_WHERE_NOTSMTP)
+#define ACL_BIT_AUTH BIT(ACL_WHERE_AUTH)
+#define ACL_BIT_CONNECT BIT(ACL_WHERE_CONNECT)
+#define ACL_BIT_ETRN BIT(ACL_WHERE_ETRN)
+#define ACL_BIT_EXPN BIT(ACL_WHERE_EXPN)
+#define ACL_BIT_HELO BIT(ACL_WHERE_HELO)
+#define ACL_BIT_MAILAUTH BIT(ACL_WHERE_MAILAUTH)
+#define ACL_BIT_NOTSMTP_START BIT(ACL_WHERE_NOTSMTP_START)
+#define ACL_BIT_NOTQUIT BIT(ACL_WHERE_NOTQUIT)
+#define ACL_BIT_QUIT BIT(ACL_WHERE_QUIT)
+#define ACL_BIT_STARTTLS BIT(ACL_WHERE_STARTTLS)
+#define ACL_BIT_VRFY BIT(ACL_WHERE_VRFY)
+#define ACL_BIT_DELIVERY BIT(ACL_WHERE_DELIVERY)
+#define ACL_BIT_UNKNOWN BIT(ACL_WHERE_UNKNOWN)
+
+#define ACL_BITS_HAVEDATA (ACL_BIT_MIME | ACL_BIT_DKIM | ACL_BIT_DATA \
+ | ACL_BIT_PRDR \
+ | ACL_BIT_NOTSMTP | ACL_BIT_QUIT | ACL_BIT_NOTQUIT)
+
+
+/* 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 };
+
+/* Codes for ESMTP facilities offered by peer */
+
+#define OPTION_TLS BIT(0)
+#define OPTION_IGNQ BIT(1)
+#define OPTION_PRDR BIT(2)
+#define OPTION_UTF8 BIT(3)
+#define OPTION_DSN BIT(4)
+#define OPTION_PIPE BIT(5)
+#define OPTION_SIZE BIT(6)
+#define OPTION_CHUNKING BIT(7)
+#define OPTION_EARLY_PIPE BIT(8)
+
+/* Argument for *_getc */
+
+#define GETC_BUFFER_UNLIMITED UINT_MAX
+
+/* UTF-8 chars for line-drawing */
+
+#define UTF8_DOWN_RIGHT "\xE2\x95\xAD"
+#define UTF8_VERT "\xE2\x94\x82"
+#define UTF8_HORIZ "\xE2\x94\x80"
+#define UTF8_VERT_RIGHT "\xE2\x94\x9C"
+#define UTF8_UP_RIGHT "\xE2\x95\xB0"
+#define UTF8_VERT_2DASH "\xE2\x95\x8E"
+
+
+/* Options on tls_close */
+#define TLS_NO_SHUTDOWN 0 /* Just forget the context */
+#define TLS_SHUTDOWN_NOWAIT 1 /* Send alert; do not wait */
+#define TLS_SHUTDOWN_WAIT 2 /* Send alert & wait for peer's alert */
+#define TLS_SHUTDOWN_WONLY 3 /* only wait for peer's alert */
+
+
+#ifdef COMPILE_UTILITY
+# define ALARM(seconds) alarm(seconds);
+# define ALARM_CLR(seconds) alarm(seconds);
+#else
+/* For debugging of odd alarm-signal problems, stash caller info while the
+alarm is active. Clear it down on cancelling the alarm so we can tell there
+should not be one active. */
+
+# define ALARM(seconds) \
+ debug_selector & D_any \
+ ? (sigalarm_setter = CUS __FUNCTION__, alarm(seconds)) : alarm(seconds);
+# define ALARM_CLR(seconds) \
+ debug_selector & D_any \
+ ? (sigalarm_setter = NULL, alarm(seconds)) : alarm(seconds);
+#endif
+
+#define AUTHS_REGEX US"\\n250[\\s\\-]AUTH\\s+([\\-\\w \\t]+)(?:\\n|$)"
+
+#define EARLY_PIPE_FEATURE_NAME "PIPECONNECT"
+#define EARLY_PIPE_FEATURE_LEN 11
+
+
+/* Flags for auth_client_item() */
+
+#define AUTH_ITEM_FIRST BIT(0)
+#define AUTH_ITEM_LAST BIT(1)
+#define AUTH_ITEM_IGN64 BIT(2)
+
+
+/* Flags for tls_{in,out}_resumption */
+#define RESUME_SUPPORTED BIT(0)
+#define RESUME_CLIENT_REQUESTED BIT(1)
+#define RESUME_CLIENT_SUGGESTED BIT(2)
+#define RESUME_SERVER_TICKET BIT(3)
+#define RESUME_USED BIT(4)
+
+#define RESUME_DECODE_STRING \
+ US"not requested or offered : 0x02 :client requested, no server ticket" \
+ ": 0x04 : 0x05 : 0x06 :client offered session, no server action" \
+ ": 0x08 :no client request: 0x0A :client requested new ticket, server provided" \
+ ": 0x0C :client offered session, not used: 0x0E :client offered session, server only provided new ticket" \
+ ": 0x10 :session resumed unasked: 0x12 :session resumed unasked" \
+ ": 0x14 : 0x15 : 0x16 :session resumed" \
+ ": 0x18 :session resumed unasked: 0x1A :session resumed unasked" \
+ ": 0x1C :session resumed: 0x1E :session resumed, also new ticket"
+
+/* Flags for string_vformat */
+#define SVFMT_EXTEND BIT(0)
+#define SVFMT_REBUFFER BIT(1)
+#define SVFMT_TAINT_NOCHK BIT(2)
+
+
+#define NOTIFIER_SOCKET_NAME "exim_daemon_notify"
+#define NOTIFY_MSG_QRUN 1 /* Notify message types */
+#define NOTIFY_QUEUE_SIZE_REQ 2
+
+/* End of macros.h */
diff --git a/src/malware.c b/src/malware.c
new file mode 100644
index 0000000..4719a5d
--- /dev/null
+++ b/src/malware.c
@@ -0,0 +1,2328 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/*
+ * Copyright (c) The Exim Maintainers 2015 - 2022
+ * Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003 - 2015
+ * License: GPL
+ */
+
+/* Code for calling virus (malware) scanners. Called from acl.c. */
+
+#include "exim.h"
+#ifdef WITH_CONTENT_SCAN /* entire file */
+
+typedef enum {
+#ifndef DISABLE_MAL_FFROTD
+ M_FPROTD,
+#endif
+#ifndef DISABLE_MAL_FFROT6D
+ M_FPROT6D,
+#endif
+#ifndef DISABLE_MAL_DRWEB
+ M_DRWEB,
+#endif
+#ifndef DISABLE_MAL_AVE
+ M_AVES,
+#endif
+#ifndef DISABLE_MAL_FSECURE
+ M_FSEC,
+#endif
+#ifndef DISABLE_MAL_KAV
+ M_KAVD,
+#endif
+#ifndef DISABLE_MAL_SOPHIE
+ M_SOPHIE,
+#endif
+#ifndef DISABLE_MAL_CLAM
+ M_CLAMD,
+#endif
+#ifndef DISABLE_MAL_MKS
+ M_MKSD,
+#endif
+#ifndef DISABLE_MAL_AVAST
+ M_AVAST,
+#endif
+#ifndef DISABLE_MAL_SOCK
+ M_SOCK,
+#endif
+#ifndef DISABLE_MAL_CMDLINE
+ M_CMDL,
+#endif
+ M_DUMMY
+ } 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[] =
+{
+#ifndef DISABLE_MAL_FFROTD
+ { M_FPROTD, US"f-protd", US"localhost 10200-10204", MC_TCP },
+#endif
+#ifndef DISABLE_MAL_FFROT6D
+ { M_FPROT6D, US"f-prot6d", US"localhost 10200", MC_TCP },
+#endif
+#ifndef DISABLE_MAL_DRWEB
+ { M_DRWEB, US"drweb", US"/usr/local/drweb/run/drwebd.sock", MC_STRM },
+#endif
+#ifndef DISABLE_MAL_AVE
+ { M_AVES, US"aveserver", US"/var/run/aveserver", MC_UNIX },
+#endif
+#ifndef DISABLE_MAL_FSECURE
+ { M_FSEC, US"fsecure", US"/var/run/.fsav", MC_UNIX },
+#endif
+#ifndef DISABLE_MAL_KAV
+ { M_KAVD, US"kavdaemon", US"/var/run/AvpCtl", MC_UNIX },
+#endif
+#ifndef DISABLE_MAL_SOPHIE
+ { M_SOPHIE, US"sophie", US"/var/run/sophie", MC_UNIX },
+#endif
+#ifndef DISABLE_MAL_CLAM
+ { M_CLAMD, US"clamd", US"/tmp/clamd", MC_NONE },
+#endif
+#ifndef DISABLE_MAL_MKS
+ { M_MKSD, US"mksd", NULL, MC_NONE },
+#endif
+#ifndef DISABLE_MAL_AVAST
+ { M_AVAST, US"avast", US"/var/run/avast/scan.sock", MC_STRM },
+#endif
+#ifndef DISABLE_MAL_SOCK
+ { M_SOCK, US"sock", US"/tmp/malware.sock", MC_STRM },
+#endif
+#ifndef DISABLE_MAL_CMDLINE
+ { M_CMDL, US"cmdline", NULL, MC_NONE },
+#endif
+ { -1, NULL, NULL, MC_NONE } /* end-marker */
+};
+
+/******************************************************************************/
+# ifdef MACRO_PREDEF /* build solely to predefine macros */
+
+# include "macro_predef.h"
+
+void
+features_malware(void)
+{
+const uschar * s;
+uschar * t;
+uschar buf[EXIM_DRIVERNAME_MAX];
+
+spf(buf, sizeof(buf), US"_HAVE_MALWARE_");
+
+for (const struct scan * sc = m_scans; sc->scancode != -1; sc++)
+ {
+ for (s = sc->name, t = buf+14; *s; s++) if (*s != '-')
+ *t++ = toupper(*s);
+ *t = '\0';
+ builtin_macro_create(buf);
+ }
+}
+
+/******************************************************************************/
+# else /*!MACRO_PREDEF, main build*/
+
+
+#define MALWARE_TIMEOUT 120 /* default timeout, seconds */
+
+static const uschar * malware_regex_default = US ".+";
+static const pcre2_code * malware_default_re = NULL;
+
+
+#ifndef DISABLE_MAL_CLAM
+/* The maximum number of clamd servers that are supported in the configuration */
+# define MAX_CLAMD_SERVERS 32
+# define MAX_CLAMD_SERVERS_S "32"
+
+typedef struct clamd_address {
+ uschar * hostspec;
+ unsigned tcp_port;
+ unsigned retry;
+} clamd_address;
+#endif
+
+
+#ifndef DISABLE_MAL_DRWEB
+# 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 */
+
+static const uschar * drweb_re_str = US "infected\\swith\\s*(.+?)$";
+static const pcre2_code * drweb_re = NULL;
+#endif
+
+#ifndef DISABLE_MAL_FSECURE
+static const uschar * fsec_re_str = US "\\S{0,5}INFECTED\\t[^\\t]*\\t([^\\t]+)\\t\\S*$";
+static const pcre2_code * fsec_re = NULL;
+#endif
+
+#ifndef DISABLE_MAL_KAV
+static const uschar * kav_re_sus_str = US "suspicion:\\s*(.+?)\\s*$";
+static const uschar * kav_re_inf_str = US "infected:\\s*(.+?)\\s*$";
+static const pcre2_code * kav_re_sus = NULL;
+static const pcre2_code * kav_re_inf = NULL;
+#endif
+
+#ifndef DISABLE_MAL_AVAST
+static const uschar * ava_re_clean_str = US "(?!\\\\)\\t\\[\\+\\]";
+static const uschar * ava_re_virus_str = US "(?!\\\\)\\t\\[L\\]\\d+\\.0\\t0\\s(.*)";
+static const uschar * ava_re_error_str = US "(?!\\\\)\\t\\[E\\]\\d+\\.0\\tError\\s\\d+\\s(.*)";
+static const pcre2_code * ava_re_clean = NULL;
+static const pcre2_code * ava_re_virus = NULL;
+static const pcre2_code * ava_re_error = NULL;
+#endif
+
+#ifndef DISABLE_MAL_FFROT6D
+static const uschar * fprot6d_re_error_str = US "^\\d+\\s<(.+?)>$";
+static const uschar * fprot6d_re_virus_str = US "^\\d+\\s<infected:\\s+(.+?)>\\s+.+$";
+static const pcre2_code * fprot6d_re_error = NULL;
+static const pcre2_code * fprot6d_re_virus = NULL;
+#endif
+
+
+
+/******************************************************************************/
+
+#ifndef DISABLE_MAL_KAV
+/* 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 = CS &word;
+ return(byte[0] ? LITTLE_MY_ENDIAN : BIG_MY_ENDIAN);
+}
+#endif
+
+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[MESSAGE_ID_LENGTH+1];
+
+
+/* Some (currently avast only) use backslash escaped whitespace,
+this function undoes these escapes */
+
+#ifndef DISABLE_MAL_AVAST
+static inline void
+unescape(uschar *p)
+{
+uschar *p0;
+for (; *p; ++p)
+ if (*p == '\\' && (isspace(p[1]) || p[1] == '\\'))
+ for (p0 = p; *p0; ++p0) *p0 = p0[1];
+}
+#endif
+
+/* --- malware_*_defer --- */
+static inline int
+malware_panic_defer(const uschar * str)
+{
+log_write(0, LOG_MAIN|LOG_PANIC, "malware acl condition: %s", str);
+return DEFER;
+}
+static inline int
+malware_log_defer(const uschar * str)
+{
+log_write(0, LOG_MAIN, "malware acl condition: %s", str);
+return DEFER;
+}
+/* --- m_*_defer --- */
+static inline int
+m_panic_defer(struct scan * scanent, const uschar * hostport,
+ const uschar * str)
+{
+return malware_panic_defer(string_sprintf("%s %s : %s",
+ scanent->name, hostport ? hostport : CUS"", str));
+}
+/* --- m_*_defer_3 */
+static inline int
+m_panic_defer_3(struct scan * scanent, const uschar * hostport,
+ const uschar * str, int fd_to_close)
+{
+DEBUG(D_acl) debug_print_socket(fd_to_close);
+(void) close(fd_to_close);
+return m_panic_defer(scanent, hostport, str);
+}
+
+/*************************************************/
+
+#ifndef DISABLE_MAL_CLAM
+/* 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, const blob * fastopen_blob)
+{
+int fd = ip_connectedsocket(SOCK_STREAM, hostname, port, port, 5,
+ host, errstr, fastopen_blob);
+#ifdef EXIM_TFO_FREEBSD
+/* Under some fault conditions, FreeBSD 12.2 seen to send a (non-TFO) SYN
+and, getting no response, wait for a long time. Impose a 5s max. */
+if (fd >= 0)
+ (void) poll_one_fd(fd, POLLOUT, 5 * 1000);
+#endif
+return fd;
+}
+#endif
+
+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 pcre2_code *
+m_pcre_compile(const uschar * re, uschar ** errstr)
+{
+int err;
+PCRE2_SIZE roffset;
+const pcre2_code * cre;
+
+if (!(cre = pcre2_compile((PCRE2_SPTR)re, PCRE2_ZERO_TERMINATED,
+ PCRE_COPT, &err, &roffset, pcre_cmp_ctx)))
+ {
+ uschar errbuf[128];
+ pcre2_get_error_message(err, errbuf, sizeof(errbuf));
+ *errstr= string_sprintf("regular expression error in '%s': %s at offset %ld",
+ re, errbuf, (long)roffset);
+ }
+return cre;
+}
+
+uschar *
+m_pcre_exec(const pcre2_code * cre, uschar * text)
+{
+pcre2_match_data * md = pcre2_match_data_create(2, pcre_gen_ctx);
+int i = pcre2_match(cre, text, PCRE2_ZERO_TERMINATED, 0, 0, md, pcre_mtc_ctx);
+PCRE2_UCHAR * substr = NULL;
+PCRE2_SIZE slen;
+
+if (i >= 2) /* Got it */
+ pcre2_substring_get_bynumber(md, 1, &substr, &slen);
+return US substr;
+}
+
+static const pcre2_code *
+m_pcre_nextinlist(const uschar ** list, int * sep,
+ char * listerr, uschar ** errstr)
+{
+const uschar * list_ele;
+const pcre2_code * cre = NULL;
+
+if (!(list_ele = string_nextinlist(list, sep, NULL, 0)))
+ *errstr = US listerr;
+else
+ {
+ DEBUG(D_acl) debug_printf_indent("%15s%10s'%s'\n", "", "RE: ",
+ string_printing(list_ele));
+ cre = m_pcre_compile(CUS list_ele, errstr);
+ }
+return cre;
+}
+
+
+/*
+ Simple though inefficient wrapper for reading a line. Drop CRs and the
+ trailing newline. Can return early on buffer full. Null-terminate.
+ Apply initial timeout if no data ready.
+
+ Return: number of chars - zero for an empty line
+ -1 on EOF
+ -2 on timeout or error
+*/
+static int
+recv_line(int fd, uschar * buffer, int bsize, time_t tmo)
+{
+uschar * p = buffer;
+ssize_t rcv;
+BOOL ok = FALSE;
+
+if (!fd_ready(fd, tmo))
+ return -2;
+
+/*XXX tmo handling assumes we always get a whole line */
+/* read until \n */
+errno = 0;
+while ((rcv = read(fd, p, 1)) > 0)
+ {
+ ok = TRUE;
+ if (p-buffer > bsize-2) break;
+ if (*p == '\n') break;
+ if (*p != '\r') p++;
+ }
+if (!ok)
+ {
+ DEBUG(D_acl)
+ {
+ debug_printf_indent("Malware scan: read %s (%s)\n",
+ rcv==0 ? "EOF" : "error", strerror(errno));
+ debug_print_socket(fd);
+ }
+ return rcv==0 ? -1 : -2;
+ }
+*p = '\0';
+
+DEBUG(D_acl) debug_printf_indent("Malware scan: read '%s'\n", buffer);
+return p - buffer;
+}
+
+/* return TRUE iff size as requested */
+#ifndef DISABLE_MAL_DRWEB
+static BOOL
+recv_len(int sock, void * buf, int size, time_t tmo)
+{
+return fd_ready(sock, tmo)
+ ? recv(sock, buf, size, 0) == size
+ : FALSE;
+}
+#endif
+
+
+
+#ifndef DISABLE_MAL_MKS
+/* ============= 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_panic_defer(
+ US"unable to write to mksd UNIX socket (/var/run/mksd/socket)");
+ return -1;
+ }
+ for (;;) /* check for short write */
+ 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, time_t tmo)
+{
+client_conn_ctx cctx = {.sock = sock};
+int offset = 0;
+int i;
+
+do
+ {
+ i = ip_recv(&cctx, av_buffer+offset, av_buffer_size-offset, tmo);
+ if (i <= 0)
+ {
+ (void) malware_panic_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_panic_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_panic_defer(scanent, NULL,
+ string_sprintf("scanner failed: %s", line));
+
+ default: /* VIR */
+ if ((p = strchr (line, '\n')) != NULL)
+ {
+ *p = '\0';
+ if ( p-line > 5
+ && line[3] == ' '
+ && (p = strchr(line+4, ' ')) != NULL
+ && p-line > 4
+ )
+ {
+ *p = '\0';
+ malware_name = string_copy(US line+4);
+ return OK;
+ }
+ }
+ return m_panic_defer(scanent, NULL,
+ string_sprintf("malformed reply received: %s", line));
+ }
+}
+
+static int
+mksd_scan_packed(struct scan * scanent, int sock, const uschar * scan_filename,
+ time_t tmo)
+{
+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 = (void *) 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), tmo) < 0)
+ return DEFER;
+
+return mksd_parse_line (scanent, CS av_buffer);
+}
+#endif /* MKSD */
+
+
+#ifndef DISABLE_MAL_CLAM
+static int
+clamd_option(clamd_address * cd, const uschar * optstr, int * subsep)
+{
+uschar * s;
+
+cd->retry = 0;
+while ((s = string_nextinlist(&optstr, subsep, NULL, 0)))
+ if (Ustrncmp(s, "retry=", 6) == 0)
+ {
+ int sec = readconf_readtime((s += 6), '\0', FALSE);
+ if (sec < 0)
+ return FAIL;
+ cd->retry = sec;
+ }
+ else
+ return FAIL;
+return OK;
+}
+#endif
+
+
+
+/*************************************************
+* 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:
+ malware_re match condition for "malware="
+ scan_filename the file holding the email to be scanned, if we're faking
+ this up for the -bmalware test, else NULL
+ timeout if nonzero, non-default timeoutl
+
+Returns: Exim message processing code (OK, FAIL, DEFER, ...)
+ where true means malware was found (condition applies)
+*/
+static int
+malware_internal(const uschar * malware_re, const uschar * scan_filename,
+ int timeout)
+{
+int sep = 0;
+const uschar *av_scanner_work = av_scanner;
+uschar *scanner_name;
+unsigned long mbox_size;
+FILE *mbox_file;
+const pcre2_code *re;
+uschar * errstr;
+struct scan * scanent;
+const uschar * scanner_options;
+client_conn_ctx malware_daemon_ctx = {.sock = -1};
+time_t tmo;
+uschar * eml_filename, * eml_dir;
+
+if (!malware_re)
+ return FAIL; /* empty means "don't match anything" */
+
+/* Ensure the eml mbox file is spooled up */
+
+if (!(mbox_file = spool_mbox(&mbox_size, scan_filename, &eml_filename)))
+ return malware_panic_defer(US"error while creating mbox spool file");
+
+/* None of our current scanners need the mbox file as a stream (they use
+the name), so we can close it right away. Get the directory too. */
+
+(void) fclose(mbox_file);
+eml_dir = string_copyn(eml_filename, Ustrrchr(eml_filename, '/') - eml_filename);
+
+/* parse 1st option */
+if (strcmpic(malware_re, US"false") == 0 || Ustrcmp(malware_re,"0") == 0)
+ return FAIL; /* explicitly no matching */
+
+/* special cases (match anything except empty) */
+if ( strcmpic(malware_re,US"true") == 0
+ || Ustrcmp(malware_re,"*") == 0
+ || Ustrcmp(malware_re,"1") == 0
+ )
+ {
+ if ( !malware_default_re
+ && !(malware_default_re = m_pcre_compile(malware_regex_default, &errstr)))
+ return malware_panic_defer(errstr);
+ malware_re = malware_regex_default;
+ re = malware_default_re;
+ }
+
+/* compile the regex, see if it works */
+else if (!(re = m_pcre_compile(malware_re, &errstr)))
+ return malware_panic_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_panic_defer(
+ string_sprintf("av_scanner starts with $, but expansion failed: %s",
+ expand_string_message));
+
+ DEBUG(D_acl)
+ debug_printf_indent("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_panic_defer(US"av_scanner configuration variable is empty");
+ if (!timeout) timeout = MALWARE_TIMEOUT;
+ tmo = time(NULL) + timeout;
+
+ for (scanent = m_scans; ; scanent++)
+ {
+ if (!scanent->name)
+ return malware_panic_defer(string_sprintf("unknown scanner type '%s'",
+ scanner_name));
+ if (strcmpic(scanner_name, US scanent->name) != 0)
+ continue;
+ DEBUG(D_acl) debug_printf_indent("Malware scan: %s tmo=%s\n",
+ scanner_name, readconf_printtime(timeout));
+
+ if (!(scanner_options = string_nextinlist(&av_scanner_work, &sep, NULL, 0)))
+ scanner_options = scanent->options_default;
+ if (scanent->conn == MC_NONE)
+ break;
+
+ DEBUG(D_acl) debug_printf_indent("%15s%10s%s\n", "", "socket: ", scanner_options);
+ switch(scanent->conn)
+ {
+ case MC_TCP:
+ malware_daemon_ctx.sock = ip_tcpsocket(scanner_options, &errstr, 5, NULL); break;
+ case MC_UNIX:
+ malware_daemon_ctx.sock = ip_unixsocket(scanner_options, &errstr); break;
+ case MC_STRM:
+ malware_daemon_ctx.sock = ip_streamsocket(scanner_options, &errstr, 5, NULL); break;
+ default:
+ /* compiler quietening */ break;
+ }
+ if (malware_daemon_ctx.sock < 0)
+ return m_panic_defer(scanent, CUS callout_address, errstr);
+ break;
+ }
+
+ switch (scanent->scancode)
+ {
+#ifndef DISABLE_MAL_FFROTD
+ 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;
+ int len;
+
+ 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);
+ DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s: %s\n",
+ scanner_name, scanrequest);
+
+ /* send scan request */
+ if (m_sock_send(malware_daemon_ctx.sock, scanrequest, Ustrlen(scanrequest)+1, &errstr) < 0)
+ return m_panic_defer(scanent, CUS callout_address, errstr);
+
+ while ((len = recv_line(malware_daemon_ctx.sock, buf, sizeof(buf), tmo)) >= 0)
+ if (len > 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;
+ }
+ }
+ if (len < -1)
+ {
+ (void)close(malware_daemon_ctx.sock);
+ return DEFER;
+ }
+ break;
+ } /* f-protd */
+#endif
+
+#ifndef DISABLE_MAL_FFROT6D
+ case M_FPROT6D: /* "f-prot6d" scanner type ----------------------------------- */
+ {
+ int bread;
+ uschar * e;
+ uschar * linebuffer;
+ uschar * scanrequest;
+ uschar av_buffer[1024];
+
+ if ((!fprot6d_re_virus && !(fprot6d_re_virus = m_pcre_compile(fprot6d_re_virus_str, &errstr)))
+ || (!fprot6d_re_error && !(fprot6d_re_error = m_pcre_compile(fprot6d_re_error_str, &errstr))))
+ return malware_panic_defer(errstr);
+
+ scanrequest = string_sprintf("SCAN FILE %s\n", eml_filename);
+ DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s: %s\n",
+ scanner_name, scanrequest);
+
+ if (m_sock_send(malware_daemon_ctx.sock, scanrequest, Ustrlen(scanrequest), &errstr) < 0)
+ return m_panic_defer(scanent, CUS callout_address, errstr);
+
+ bread = ip_recv(&malware_daemon_ctx, av_buffer, sizeof(av_buffer), tmo);
+
+ if (bread <= 0)
+ return m_panic_defer_3(scanent, CUS callout_address,
+ string_sprintf("unable to read from socket (%s)", strerror(errno)),
+ malware_daemon_ctx.sock);
+
+ if (bread == sizeof(av_buffer))
+ return m_panic_defer_3(scanent, CUS callout_address,
+ US"buffer too small", malware_daemon_ctx.sock);
+
+ av_buffer[bread] = '\0';
+ linebuffer = string_copy(av_buffer);
+
+ m_sock_send(malware_daemon_ctx.sock, US"QUIT\n", 5, 0);
+
+ if ((e = m_pcre_exec(fprot6d_re_error, linebuffer)))
+ return m_panic_defer_3(scanent, CUS callout_address,
+ string_sprintf("scanner reported error (%s)", e), malware_daemon_ctx.sock);
+
+ if (!(malware_name = m_pcre_exec(fprot6d_re_virus, linebuffer)))
+ malware_name = NULL;
+
+ break;
+ } /* f-prot6d */
+#endif
+
+#ifndef DISABLE_MAL_DRWEB
+ 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;
+
+ /* 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 = exim_open2(CCS eml_filename, O_RDONLY)) == -1)
+ return m_panic_defer_3(scanent, NULL,
+ string_sprintf("can't open spool file %s: %s",
+ eml_filename, strerror(errno)),
+ malware_daemon_ctx.sock);
+
+ if ((fsize = lseek(drweb_fd, 0, SEEK_END)) == -1)
+ {
+ int err;
+badseek: err = errno;
+ (void)close(drweb_fd);
+ return m_panic_defer_3(scanent, NULL,
+ string_sprintf("can't seek spool file %s: %s",
+ eml_filename, strerror(err)),
+ malware_daemon_ctx.sock);
+ }
+ fsize_uint = (unsigned int) fsize;
+ if ((off_t)fsize_uint != fsize)
+ {
+ (void)close(drweb_fd);
+ return m_panic_defer_3(scanent, NULL,
+ string_sprintf("seeking spool file %s, size overflow",
+ eml_filename),
+ malware_daemon_ctx.sock);
+ }
+ drweb_slen = htonl(fsize);
+ if (lseek(drweb_fd, 0, SEEK_SET) < 0)
+ goto badseek;
+
+ DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s remote scan [%s]\n",
+ scanner_name, scanner_options);
+
+ /* send scan request */
+ if ((send(malware_daemon_ctx.sock, &drweb_cmd, sizeof(drweb_cmd), 0) < 0) ||
+ (send(malware_daemon_ctx.sock, &drweb_flags, sizeof(drweb_flags), 0) < 0) ||
+ (send(malware_daemon_ctx.sock, &drweb_fin, sizeof(drweb_fin), 0) < 0) ||
+ (send(malware_daemon_ctx.sock, &drweb_slen, sizeof(drweb_slen), 0) < 0))
+ {
+ (void)close(drweb_fd);
+ return m_panic_defer_3(scanent, CUS callout_address, string_sprintf(
+ "unable to send commands to socket (%s)", scanner_options),
+ malware_daemon_ctx.sock);
+ }
+
+ if (!(drweb_fbuf = store_malloc(fsize_uint)))
+ {
+ (void)close(drweb_fd);
+ return m_panic_defer_3(scanent, NULL,
+ string_sprintf("unable to allocate memory %u for file (%s)",
+ fsize_uint, eml_filename),
+ malware_daemon_ctx.sock);
+ }
+
+ if ((result = read (drweb_fd, drweb_fbuf, fsize)) == -1)
+ {
+ int err = errno;
+ (void)close(drweb_fd);
+ store_free(drweb_fbuf);
+ return m_panic_defer_3(scanent, NULL,
+ string_sprintf("can't read spool file %s: %s",
+ eml_filename, strerror(err)),
+ malware_daemon_ctx.sock);
+ }
+ (void)close(drweb_fd);
+
+ /* send file body to socket */
+ if (send(malware_daemon_ctx.sock, drweb_fbuf, fsize, 0) < 0)
+ {
+ store_free(drweb_fbuf);
+ return m_panic_defer_3(scanent, CUS callout_address, string_sprintf(
+ "unable to send file body to socket (%s)", scanner_options),
+ malware_daemon_ctx.sock);
+ }
+ store_free(drweb_fbuf);
+ }
+ else
+ {
+ drweb_slen = htonl(Ustrlen(eml_filename));
+
+ DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s local scan [%s]\n",
+ scanner_name, scanner_options);
+
+ /* send scan request */
+ if ((send(malware_daemon_ctx.sock, &drweb_cmd, sizeof(drweb_cmd), 0) < 0) ||
+ (send(malware_daemon_ctx.sock, &drweb_flags, sizeof(drweb_flags), 0) < 0) ||
+ (send(malware_daemon_ctx.sock, &drweb_slen, sizeof(drweb_slen), 0) < 0) ||
+ (send(malware_daemon_ctx.sock, eml_filename, Ustrlen(eml_filename), 0) < 0) ||
+ (send(malware_daemon_ctx.sock, &drweb_fin, sizeof(drweb_fin), 0) < 0))
+ return m_panic_defer_3(scanent, CUS callout_address, string_sprintf(
+ "unable to send commands to socket (%s)", scanner_options),
+ malware_daemon_ctx.sock);
+ }
+
+ /* wait for result */
+ if (!recv_len(malware_daemon_ctx.sock, &drweb_rc, sizeof(drweb_rc), tmo))
+ return m_panic_defer_3(scanent, CUS callout_address,
+ US"unable to read return code", malware_daemon_ctx.sock);
+ drweb_rc = ntohl(drweb_rc);
+
+ if (!recv_len(malware_daemon_ctx.sock, &drweb_vnum, sizeof(drweb_vnum), tmo))
+ return m_panic_defer_3(scanent, CUS callout_address,
+ US"unable to read the number of viruses", malware_daemon_ctx.sock);
+ drweb_vnum = ntohl(drweb_vnum);
+
+ /* "virus(es) found" if virus number is > 0 */
+ if (drweb_vnum)
+ {
+ gstring * g = NULL;
+
+ /* setup default virus name */
+ malware_name = US"unknown";
+
+ /* set up match regex */
+ if (!drweb_re)
+ drweb_re = m_pcre_compile(drweb_re_str, &errstr);
+
+ /* read and concatenate virus names into one string */
+ for (int i = 0; i < drweb_vnum; i++)
+ {
+ pcre2_match_data * md = pcre2_match_data_create(2, pcre_gen_ctx);
+
+ /* read the size of report */
+ if (!recv_len(malware_daemon_ctx.sock, &drweb_slen, sizeof(drweb_slen), tmo))
+ return m_panic_defer_3(scanent, CUS callout_address,
+ US"cannot read report size", malware_daemon_ctx.sock);
+ drweb_slen = ntohl(drweb_slen);
+
+ /* assume tainted, since it is external input */
+ tmpbuf = store_get(drweb_slen, GET_TAINTED);
+
+ /* read report body */
+ if (!recv_len(malware_daemon_ctx.sock, tmpbuf, drweb_slen, tmo))
+ return m_panic_defer_3(scanent, CUS callout_address,
+ US"cannot read report string", malware_daemon_ctx.sock);
+ tmpbuf[drweb_slen] = '\0';
+
+ /* try matcher on the line, grab substring */
+ result = pcre2_match(drweb_re, (PCRE2_SPTR)tmpbuf, PCRE2_ZERO_TERMINATED,
+ 0, 0, md, pcre_mtc_ctx);
+ if (result >= 2)
+ {
+ PCRE2_SIZE * ovec = pcre2_get_ovector_pointer(md);
+
+ if (i==0) /* the first name we just copy to malware_name */
+ g = string_catn(NULL, US ovec[2], ovec[3] - ovec[2]);
+
+ else /* concatenate each new virus name to previous */
+ {
+ g = string_catn(g, US"/", 1);
+ g = string_catn(g, US ovec[2], ovec[3] - ovec[2]);
+ }
+ }
+ }
+ malware_name = string_from_gstring(g);
+ }
+ 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_panic_defer_3(scanent, CUS callout_address,
+ string_sprintf("drweb daemon retcode 0x%x (%s)", drweb_rc, drweb_s),
+ malware_daemon_ctx.sock);
+
+ /* no virus found */
+ malware_name = NULL;
+ }
+ break;
+ } /* drweb */
+#endif
+
+#ifndef DISABLE_MAL_AVE
+ case M_AVES: /* "aveserver" scanner type -------------------------------- */
+ {
+ uschar buf[32768];
+ int result;
+
+ /* read aveserver's greeting and see if it is ready (2xx greeting) */
+ buf[0] = 0;
+ recv_line(malware_daemon_ctx.sock, buf, sizeof(buf), tmo);
+
+ if (buf[0] != '2') /* aveserver is having problems */
+ return m_panic_defer_3(scanent, CUS callout_address,
+ string_sprintf("unavailable (Responded: %s).",
+ ((buf[0] != 0) ? buf : US "nothing") ),
+ malware_daemon_ctx.sock);
+
+ /* prepare our command */
+ (void)string_format(buf, sizeof(buf), "SCAN bPQRSTUW %s\r\n",
+ eml_filename);
+
+ /* and send it */
+ DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s %s\n",
+ scanner_name, buf);
+ if (m_sock_send(malware_daemon_ctx.sock, buf, Ustrlen(buf), &errstr) < 0)
+ return m_panic_defer(scanent, CUS callout_address, errstr);
+
+ malware_name = NULL;
+ result = 0;
+ /* read response lines, find malware name and final response */
+ while (recv_line(malware_daemon_ctx.sock, buf, sizeof(buf), tmo) > 0)
+ {
+ if (buf[0] == '2')
+ break;
+ if (buf[0] == '5') /* aveserver is having problems */
+ {
+ result = m_panic_defer(scanent, CUS callout_address,
+ string_sprintf("unable to scan file %s (Responded: %s).",
+ eml_filename, buf));
+ break;
+ }
+ if (Ustrncmp(buf,"322",3) == 0)
+ {
+ uschar *p = Ustrchr(&buf[4], ' ');
+ *p = '\0';
+ malware_name = string_copy(&buf[4]);
+ }
+ }
+
+ if (m_sock_send(malware_daemon_ctx.sock, US"quit\r\n", 6, &errstr) < 0)
+ return m_panic_defer(scanent, CUS callout_address, errstr);
+
+ /* read aveserver's greeting and see if it is ready (2xx greeting) */
+ buf[0] = 0;
+ recv_line(malware_daemon_ctx.sock, buf, sizeof(buf), tmo);
+
+ if (buf[0] != '2') /* aveserver is having problems */
+ return m_panic_defer_3(scanent, CUS callout_address,
+ string_sprintf("unable to quit dialogue (Responded: %s).",
+ ((buf[0] != 0) ? buf : US "nothing") ),
+ malware_daemon_ctx.sock);
+
+ if (result == DEFER)
+ {
+ (void)close(malware_daemon_ctx.sock);
+ return DEFER;
+ }
+ break;
+ } /* aveserver */
+#endif
+
+#ifndef DISABLE_MAL_FSECURE
+ case M_FSEC: /* "fsecure" scanner type ---------------------------------- */
+ {
+ int i, bread = 0;
+ uschar * file_name;
+ uschar av_buffer[1024];
+ 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_indent("Malware scan: issuing %s scan [%s]\n",
+ scanner_name, scanner_options);
+ /* pass options */
+ memset(av_buffer, 0, sizeof(av_buffer));
+ for (i = 0; i != nelem(cmdopt); i++)
+ {
+
+ if (m_sock_send(malware_daemon_ctx.sock, cmdopt[i], Ustrlen(cmdopt[i]), &errstr) < 0)
+ return m_panic_defer(scanent, CUS callout_address, errstr);
+
+ bread = ip_recv(&malware_daemon_ctx, av_buffer, sizeof(av_buffer), tmo);
+ if (bread > 0) av_buffer[bread]='\0';
+ if (bread < 0)
+ return m_panic_defer_3(scanent, CUS callout_address,
+ string_sprintf("unable to read answer %d (%s)", i, strerror(errno)),
+ malware_daemon_ctx.sock);
+ for (int 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(malware_daemon_ctx.sock, file_name, Ustrlen(file_name), &errstr) < 0)
+ return m_panic_defer(scanent, CUS callout_address, errstr);
+
+ /* set up match */
+ /* todo also SUSPICION\t */
+ if (!fsec_re)
+ fsec_re = m_pcre_compile(fsec_re_str, &errstr);
+
+ /* read report, linewise. Apply a timeout as the Fsecure daemon
+ sometimes wants an answer to "PING" but they won't tell us what */
+ {
+ uschar * p = av_buffer;
+ uschar * q;
+
+ for (;;)
+ {
+ errno = ETIMEDOUT;
+ i = av_buffer+sizeof(av_buffer)-p;
+ if ((bread= ip_recv(&malware_daemon_ctx, p, i-1, tmo)) < 0)
+ return m_panic_defer_3(scanent, CUS callout_address,
+ string_sprintf("unable to read result (%s)", strerror(errno)),
+ malware_daemon_ctx.sock);
+
+ for (p[bread] = '\0'; (q = Ustrchr(p, '\n')); p = q+1)
+ {
+ *q = '\0';
+
+ /* Really search for virus again? */
+ if (!malware_name)
+ /* try matcher on the line, grab substring */
+ malware_name = m_pcre_exec(fsec_re, p);
+
+ if (Ustrstr(p, "OK\tScan ok."))
+ goto fsec_found;
+ }
+
+ /* copy down the trailing partial line then read another chunk */
+ i = av_buffer+sizeof(av_buffer)-p;
+ memmove(av_buffer, p, i);
+ p = av_buffer+i;
+ }
+ }
+
+ fsec_found:
+ break;
+ } /* fsecure */
+#endif
+
+#ifndef DISABLE_MAL_KAV
+ case M_KAVD: /* "kavdaemon" scanner type -------------------------------- */
+ {
+ time_t t;
+ uschar tmpbuf[1024];
+ uschar * scanrequest;
+ int kav_rc;
+ unsigned long kav_reportlen;
+ int bread;
+ const pcre2_code *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_indent("Malware scan: issuing %s scan [%s]\n",
+ scanner_name, scanner_options);
+
+ /* send scan request */
+ if (m_sock_send(malware_daemon_ctx.sock, scanrequest, Ustrlen(scanrequest)+1, &errstr) < 0)
+ return m_panic_defer(scanent, CUS callout_address, errstr);
+
+ /* wait for result */
+ if (!recv_len(malware_daemon_ctx.sock, tmpbuf, 2, tmo))
+ return m_panic_defer_3(scanent, CUS callout_address,
+ US"unable to read 2 bytes from socket.", malware_daemon_ctx.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_panic_defer_3(scanent, CUS callout_address,
+ US"please reconfigure kavdaemon to NOT disinfect or remove infected files.",
+ malware_daemon_ctx.sock);
+ case 1:
+ return m_panic_defer_3(scanent, CUS callout_address,
+ US"reported 'scanning not completed' (code 1).", malware_daemon_ctx.sock);
+ case 7:
+ return m_panic_defer_3(scanent, CUS callout_address,
+ US"reported 'kavdaemon damaged' (code 7).", malware_daemon_ctx.sock);
+ }
+
+ /* code 8 is not handled, since it is ambiguous. 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 (!recv_len(malware_daemon_ctx.sock, &kav_reportlen, 4, tmo))
+ return m_panic_defer_3(scanent, CUS callout_address,
+ US"cannot read report size", malware_daemon_ctx.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 */
+ if (kav_rc == 3)
+ {
+ if (!kav_re_sus) kav_re_sus = m_pcre_compile(kav_re_sus_str, &errstr);
+ kav_re = kav_re_sus;
+ }
+ else
+ {
+ if (!kav_re_inf) kav_re_inf = m_pcre_compile(kav_re_inf_str, &errstr);
+ kav_re = kav_re_inf;
+ }
+
+ /* read report, linewise. Using size from stream to read amount of data
+ from same stream is safe enough. */
+ /* coverity[tainted_data] */
+ while (kav_reportlen > 0)
+ {
+ if ((bread = recv_line(malware_daemon_ctx.sock, tmpbuf, sizeof(tmpbuf), tmo)) < 0)
+ break;
+ kav_reportlen -= bread+1;
+
+ /* 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;
+ }
+#endif
+
+#ifndef DISABLE_MAL_CMDLINE
+ case M_CMDL: /* "cmdline" scanner type ---------------------------------- */
+ {
+ const uschar *cmdline_scanner = scanner_options;
+ const pcre2_code *cmdline_trigger_re;
+ const pcre2_code *cmdline_regex_re;
+ uschar * file_name;
+ uschar * commandline;
+ void (*eximsigchld)(int);
+ void (*eximsigpipe)(int);
+ FILE *scanner_out = NULL;
+ int scanner_fd;
+ FILE *scanner_record = NULL;
+ uschar linebuffer[32767];
+ int rcnt;
+ int trigger = 0;
+ uschar *p;
+
+ if (!cmdline_scanner)
+ return m_panic_defer(scanent, NULL, 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_panic_defer(scanent, NULL, 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_panic_defer(scanent, NULL, 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_indent("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_panic_defer(scanent, NULL,
+ string_sprintf("call (%s) failed: %s.", commandline, strerror(err)));
+ }
+ scanner_fd = fileno(scanner_out);
+
+ file_name = string_sprintf("%s/%s_scanner_output", eml_dir, message_id);
+
+ if (!(scanner_record = modefopen(file_name, "wb", SPOOL_MODE)))
+ {
+ int err = errno;
+ (void) pclose(scanner_out);
+ signal(SIGCHLD,eximsigchld); signal(SIGPIPE,eximsigpipe);
+ return m_panic_defer(scanent, NULL, string_sprintf(
+ "opening scanner output file (%s) failed: %s.",
+ file_name, strerror(err)));
+ }
+
+ /* look for trigger while recording output */
+ while ((rcnt = recv_line(scanner_fd, linebuffer,
+ sizeof(linebuffer), tmo)))
+ {
+ if (rcnt < 0)
+ {
+ int err = errno;
+ if (rcnt == -1)
+ break;
+ (void) pclose(scanner_out);
+ signal(SIGCHLD,eximsigchld); signal(SIGPIPE,eximsigpipe);
+ return m_panic_defer(scanent, NULL, string_sprintf(
+ "unable to read from scanner (%s): %s",
+ commandline, strerror(err)));
+ }
+
+ if (Ustrlen(linebuffer) > fwrite(linebuffer, 1, Ustrlen(linebuffer), scanner_record))
+ {
+ /* short write */
+ (void) pclose(scanner_out);
+ signal(SIGCHLD,eximsigchld); signal(SIGPIPE,eximsigpipe);
+ return m_panic_defer(scanent, NULL, string_sprintf(
+ "short write on scanner output file (%s).", file_name));
+ }
+ putc('\n', scanner_record);
+ /* 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_panic_defer(scanent, NULL,
+ 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 = Ufopen(file_name, "rb");
+ while (Ufgets(linebuffer, sizeof(linebuffer), scanner_record))
+ if ((s = m_pcre_exec(cmdline_regex_re, linebuffer))) /* try match */
+ malware_name = s;
+ (void)fclose(scanner_record);
+ }
+ else /* no virus found */
+ malware_name = NULL;
+ break;
+ } /* cmdline */
+#endif
+
+#ifndef DISABLE_MAL_SOPHIE
+ 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_indent("Malware scan: issuing %s scan [%s]\n",
+ scanner_name, scanner_options);
+
+ if ( write(malware_daemon_ctx.sock, file_name, Ustrlen(file_name)) < 0
+ || write(malware_daemon_ctx.sock, "\n", 1) != 1
+ )
+ return m_panic_defer_3(scanent, CUS callout_address,
+ string_sprintf("unable to write to UNIX socket (%s)", scanner_options),
+ malware_daemon_ctx.sock);
+
+ /* wait for result */
+ memset(av_buffer, 0, sizeof(av_buffer));
+ if ((bread = ip_recv(&malware_daemon_ctx, av_buffer, sizeof(av_buffer), tmo)) <= 0)
+ return m_panic_defer_3(scanent, CUS callout_address,
+ string_sprintf("unable to read from UNIX socket (%s)", scanner_options),
+ malware_daemon_ctx.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_panic_defer_3(scanent, CUS callout_address,
+ US"scanner reported error", malware_daemon_ctx.sock);
+ else /* all ok, no virus */
+ malware_name = NULL;
+
+ break;
+ }
+#endif
+
+#ifndef DISABLE_MAL_CLAM
+ 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
+* See Exim bug 926 for details. */
+
+ uschar *p, *vname, *result_tag;
+ int bread=0;
+ uschar av_buffer[1024];
+ uschar *hostname = US"";
+ host_item connhost;
+ int clam_fd;
+ unsigned int fsize_uint;
+ BOOL use_scan_command = FALSE;
+ clamd_address * cv[MAX_CLAMD_SERVERS];
+ int num_servers = 0;
+ uint32_t send_size, send_final_zeroblock;
+ blob cmd_str;
+
+ /*XXX if unixdomain socket, only one server supported. Needs fixing;
+ there's no reason we should not mix local and remote servers */
+
+ if (*scanner_options == '/')
+ {
+ clamd_address * cd;
+ const uschar * sublist;
+ int subsep = ' ';
+
+ /* Local file; so we def want to use_scan_command and don't want to try
+ passing IP/port combinations */
+ use_scan_command = TRUE;
+ cd = (clamd_address *) store_get(sizeof(clamd_address), GET_UNTAINTED);
+
+ /* extract socket-path part */
+ sublist = scanner_options;
+ cd->hostspec = string_nextinlist(&sublist, &subsep, NULL, 0);
+
+ /* parse options */
+ if (clamd_option(cd, sublist, &subsep) != OK)
+ return m_panic_defer(scanent, NULL,
+ string_sprintf("bad option '%s'", scanner_options));
+ cv[0] = cd;
+ }
+ else
+ {
+ /* 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 * cd;
+ const uschar * sublist;
+ int subsep = ' ';
+ uschar * s;
+
+ /* The 'local' option means use the SCAN command over the network
+ * socket (ie common file storage in use) */
+ /*XXX we could accept this also as a local option? */
+ if (strcmpic(scanner_options, US"local") == 0)
+ {
+ use_scan_command = TRUE;
+ continue;
+ }
+
+ cd = (clamd_address *) store_get(sizeof(clamd_address), GET_UNTAINTED);
+
+ /* extract host and port part */
+ sublist = scanner_options;
+ if (!(cd->hostspec = string_nextinlist(&sublist, &subsep, NULL, 0)))
+ {
+ (void) m_panic_defer(scanent, NULL,
+ string_sprintf("missing address: '%s'", scanner_options));
+ continue;
+ }
+ if (!(s = string_nextinlist(&sublist, &subsep, NULL, 0)))
+ {
+ (void) m_panic_defer(scanent, NULL,
+ string_sprintf("missing port: '%s'", scanner_options));
+ continue;
+ }
+ cd->tcp_port = atoi(CS s);
+
+ /* parse options */
+ /*XXX should these options be common over scanner types? */
+ if (clamd_option(cd, sublist, &subsep) != OK)
+ return m_panic_defer(scanent, NULL,
+ string_sprintf("bad option '%s'", scanner_options));
+
+ cv[num_servers++] = cd;
+ if (num_servers >= MAX_CLAMD_SERVERS)
+ {
+ (void) m_panic_defer(scanent, NULL,
+ US"More than " MAX_CLAMD_SERVERS_S " clamd servers "
+ "specified; only using the first " MAX_CLAMD_SERVERS_S );
+ break;
+ }
+ } while ((scanner_options = string_nextinlist(&av_scanner_work, &sep,
+ NULL, 0)));
+
+ /* check if we have at least one server */
+ if (!num_servers)
+ return m_panic_defer(scanent, NULL,
+ 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_panic_defer(scanent, NULL,
+ string_sprintf("local/SCAN mode incompatible with" \
+ " : in path to email filename [%s]", eml_filename));
+
+ /* Set up the very first data we will be sending */
+ if (!use_scan_command)
+ { cmd_str.data = US"zINSTREAM"; cmd_str.len = 10; }
+ else
+ {
+ int n;
+ cmd_str.data = string_sprintf("SCAN %s\n%n", eml_filename, &n);
+ cmd_str.len = n; /* .len is a size_t */
+ }
+
+ /* 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)
+ {
+ int i = random_number(num_servers);
+ clamd_address * cd = cv[i];
+
+ DEBUG(D_acl) debug_printf_indent("trying server name %s, port %u\n",
+ cd->hostspec, cd->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) */
+ for (;;)
+ {
+ /*XXX we trust that the cmd_str is idempotent */
+ if ((malware_daemon_ctx.sock = m_tcpsocket(cd->hostspec, cd->tcp_port,
+ &connhost, &errstr,
+ use_scan_command ? &cmd_str : NULL)) >= 0)
+ {
+ /* Connection successfully established with a server */
+ hostname = cd->hostspec;
+ if (use_scan_command) cmd_str.len = 0;
+ break;
+ }
+ if (cd->retry <= 0) break;
+ while (cd->retry > 0) cd->retry = sleep(cd->retry);
+ }
+ if (malware_daemon_ctx.sock >= 0)
+ break;
+
+ (void) m_panic_defer(scanent, CUS callout_address, errstr);
+
+ /* Remove the server from the list. XXX We should free the memory */
+ num_servers--;
+ for (; i < num_servers; i++)
+ cv[i] = cv[i+1];
+ }
+
+ if (num_servers == 0)
+ return m_panic_defer(scanent, NULL, US"all servers failed");
+ }
+ else
+ for (;;)
+ {
+ if ((malware_daemon_ctx.sock = ip_unixsocket(cv[0]->hostspec, &errstr)) >= 0)
+ {
+ hostname = cv[0]->hostspec;
+ break;
+ }
+ if (cv[0]->retry <= 0)
+ return m_panic_defer(scanent, CUS callout_address, errstr);
+ while (cv[0]->retry > 0) cv[0]->retry = sleep(cv[0]->retry);
+ }
+
+ /* 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)
+ {
+ struct stat st;
+#if defined(EXIM_TCP_CORK) && !defined(OS_SENDFILE)
+ BOOL corked = TRUE;
+#endif
+ /* 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. We only send one chunk. */
+
+ DEBUG(D_acl) debug_printf_indent(
+ "Malware scan: issuing %s new-style remote scan (zINSTREAM)\n",
+ scanner_name);
+
+#if defined(EXIM_TCP_CORK)
+ (void) setsockopt(malware_daemon_ctx.sock, IPPROTO_TCP, EXIM_TCP_CORK,
+ US &on, sizeof(on));
+#endif
+ /* Pass the string to ClamAV (10 = "zINSTREAM\0"), if not already sent */
+ if (cmd_str.len)
+ if (send(malware_daemon_ctx.sock, cmd_str.data, cmd_str.len, 0) < 0)
+ return m_panic_defer_3(scanent, CUS hostname,
+ string_sprintf("unable to send zINSTREAM to socket (%s)",
+ strerror(errno)),
+ malware_daemon_ctx.sock);
+
+ if ((clam_fd = exim_open2(CS eml_filename, O_RDONLY)) < 0)
+ {
+ int err = errno;
+ return m_panic_defer_3(scanent, NULL,
+ string_sprintf("can't open spool file %s: %s",
+ eml_filename, strerror(err)),
+ malware_daemon_ctx.sock);
+ }
+ if (fstat(clam_fd, &st) < 0)
+ {
+ int err = errno;
+ (void)close(clam_fd);
+ return m_panic_defer_3(scanent, NULL,
+ string_sprintf("can't stat spool file %s: %s",
+ eml_filename, strerror(err)),
+ malware_daemon_ctx.sock);
+ }
+ fsize_uint = (unsigned int) st.st_size;
+ if ((off_t)fsize_uint != st.st_size)
+ {
+ (void)close(clam_fd);
+ return m_panic_defer_3(scanent, NULL,
+ string_sprintf("stat spool file %s, size overflow", eml_filename),
+ malware_daemon_ctx.sock);
+ }
+
+ /* send file size */
+ send_size = htonl(fsize_uint);
+ if (send(malware_daemon_ctx.sock, &send_size, sizeof(send_size), 0) < 0)
+ return m_panic_defer_3(scanent, NULL,
+ string_sprintf("unable to send file size to socket (%s)", hostname),
+ malware_daemon_ctx.sock);
+
+ /* send file body */
+ while (fsize_uint)
+ {
+#ifdef OS_SENDFILE
+ int n = os_sendfile(malware_daemon_ctx.sock, clam_fd, NULL, (size_t)fsize_uint);
+ if (n < 0)
+ return m_panic_defer_3(scanent, NULL,
+ string_sprintf("unable to send file body to socket (%s): %s", hostname, strerror(errno)),
+ malware_daemon_ctx.sock);
+ fsize_uint -= n;
+#else
+ int n = MIN(fsize_uint, big_buffer_size);
+ if ((n = read(clam_fd, big_buffer, n)) < 0)
+ return m_panic_defer_3(scanent, NULL,
+ string_sprintf("can't read spool file %s: %s",
+ eml_filename, strerror(errno)),
+ malware_daemon_ctx.sock);
+ if (send(malware_daemon_ctx.sock, big_buffer, (size_t)n, 0) < 0)
+ return m_panic_defer_3(scanent, NULL,
+ string_sprintf("unable to send file body to socket (%s): %s", hostname, strerror(errno)),
+ malware_daemon_ctx.sock);
+ fsize_uint -= n;
+# ifdef EXIM_TCP_CORK
+ if (corked)
+ {
+ corked = FALSE;
+ (void) setsockopt(malware_daemon_ctx.sock, IPPROTO_TCP, EXIM_TCP_CORK,
+ US &off, sizeof(off));
+ }
+# endif
+#endif /*!OS_SENDFILE*/
+
+ }
+
+ send_final_zeroblock = 0;
+ if (send(malware_daemon_ctx.sock, &send_final_zeroblock, sizeof(send_final_zeroblock), 0) < 0)
+ return m_panic_defer_3(scanent, NULL,
+ string_sprintf("unable to send file terminator to socket (%s)", hostname),
+ malware_daemon_ctx.sock);
+#ifdef OS_SENDFILE
+ (void) setsockopt(malware_daemon_ctx.sock, IPPROTO_TCP, EXIM_TCP_CORK,
+ US &off, sizeof(off));
+#endif
+ }
+ 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), if not already sent */
+
+ DEBUG(D_acl) debug_printf_indent(
+ "Malware scan: issuing %s local-path scan [%s]\n",
+ scanner_name, scanner_options);
+
+ if (cmd_str.len)
+ if (send(malware_daemon_ctx.sock, cmd_str.data, cmd_str.len, 0) < 0)
+ return m_panic_defer_3(scanent, CUS callout_address,
+ string_sprintf("unable to write to socket (%s)", strerror(errno)),
+ malware_daemon_ctx.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(&malware_daemon_ctx, av_buffer, sizeof(av_buffer), tmo);
+ (void)close(malware_daemon_ctx.sock);
+ malware_daemon_ctx.sock = -1;
+ malware_daemon_ctx.tls_ctx = NULL;
+
+ if (bread <= 0)
+ return m_panic_defer(scanent, CUS callout_address,
+ string_sprintf("unable to read from socket (%s)",
+ errno == 0 ? "EOF" : strerror(errno)));
+
+ if (bread == sizeof(av_buffer))
+ return m_panic_defer(scanent, CUS callout_address,
+ 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_panic_defer(scanent, CUS callout_address,
+ 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_indent("Malware response: %s\n", av_buffer);
+
+ while (isspace(*--p) && (p > av_buffer))
+ *p = '\0';
+ if (*p) ++p;
+
+ /* colon in returned output? */
+ if (!(p = Ustrchr(av_buffer,':')))
+ return m_panic_defer(scanent, CUS callout_address, 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_indent("Malware found, name \"%s\"\n", malware_name);
+
+ }
+ else if (Ustrcmp(result_tag, "ERROR") == 0)
+ return m_panic_defer(scanent, CUS callout_address,
+ 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_indent("Malware not found\n");
+
+ }
+ else
+ return m_panic_defer(scanent, CUS callout_address,
+ string_sprintf("unparseable response from ClamAV: {%s}", av_buffer));
+
+ break;
+ } /* clamd */
+#endif
+
+#ifndef DISABLE_MAL_SOCK
+ 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 pcre2_code *sockline_trig_re;
+ const pcre2_code *sockline_name_re;
+
+ /* find scanner command line */
+ if ( (sockline_scanner = string_nextinlist(&av_scanner_work, &sep,
+ NULL, 0))
+ && *sockline_scanner
+ )
+ { /* check for no expansions apart from one %s */
+ uschar * s = Ustrchr(sockline_scanner, '%');
+ if (s++)
+ if ((*s != 's' && *s != '%') || Ustrchr(s+1, '%'))
+ return m_panic_defer_3(scanent, NULL,
+ US"unsafe sock scanner call spec", malware_daemon_ctx.sock);
+ }
+ else
+ sockline_scanner = sockline_scanner_default;
+ DEBUG(D_acl) debug_printf_indent("%15s%10s'%s'\n", "", "cmdline: ",
+ string_printing(sockline_scanner));
+
+ /* find scanner output trigger */
+ sockline_trig_re = m_pcre_nextinlist(&av_scanner_work, &sep,
+ "missing trigger specification", &errstr);
+ if (!sockline_trig_re)
+ return m_panic_defer_3(scanent, NULL, errstr, malware_daemon_ctx.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_panic_defer_3(scanent, NULL, errstr, malware_daemon_ctx.sock);
+
+ /* prepare scanner call - security depends on expansions check above */
+ commandline = string_sprintf( CS sockline_scanner, CS eml_filename);
+ DEBUG(D_acl) debug_printf_indent("%15s%10s'%s'\n", "", "expanded: ",
+ string_printing(commandline));
+
+ /* Pass the command string to the socket */
+ if (m_sock_send(malware_daemon_ctx.sock, commandline, Ustrlen(commandline), &errstr) < 0)
+ return m_panic_defer(scanent, CUS callout_address, errstr);
+
+ /* Read the result */
+ bread = ip_recv(&malware_daemon_ctx, av_buffer, sizeof(av_buffer), tmo);
+
+ if (bread <= 0)
+ return m_panic_defer_3(scanent, CUS callout_address,
+ string_sprintf("unable to read from socket (%s)", strerror(errno)),
+ malware_daemon_ctx.sock);
+
+ if (bread == sizeof(av_buffer))
+ return m_panic_defer_3(scanent, CUS callout_address,
+ US"buffer too small", malware_daemon_ctx.sock);
+ av_buffer[bread] = '\0';
+ linebuffer = string_copy(av_buffer);
+ DEBUG(D_acl) debug_printf_indent("%15s%10s'%s'\n", "", "answer: ",
+ string_printing(linebuffer));
+
+ /* 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";
+ DEBUG(D_acl) debug_printf_indent("%15s%10s'%s'\n", "", "name: ",
+ string_printing(malware_name));
+ }
+ else /* no virus found */
+ malware_name = NULL;
+ break;
+ }
+#endif
+
+#ifndef DISABLE_MAL_MKS
+ case M_MKSD: /* "mksd" scanner type ------------------------------------- */
+ {
+ char *mksd_options_end;
+ int mksd_maxproc = 1; /* default, if no option supplied */
+ 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_panic_defer(scanent, CUS callout_address,
+ string_sprintf("invalid option '%s'", scanner_options));
+ }
+
+ if((malware_daemon_ctx.sock = ip_unixsocket(US "/var/run/mksd/socket", &errstr)) < 0)
+ return m_panic_defer(scanent, CUS callout_address, errstr);
+
+ malware_name = NULL;
+
+ DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s scan\n", scanner_name);
+
+ if ((retval = mksd_scan_packed(scanent, malware_daemon_ctx.sock, eml_filename, tmo)) != OK)
+ {
+ close (malware_daemon_ctx.sock);
+ return retval;
+ }
+ break;
+ }
+#endif
+
+#ifndef DISABLE_MAL_AVAST
+ case M_AVAST: /* "avast" scanner type ----------------------------------- */
+ {
+ uschar buf[1024];
+ uschar * scanrequest;
+ enum {AVA_HELO, AVA_OPT, AVA_RSP, AVA_DONE} avast_stage;
+ int nread;
+ uschar * error_message = NULL;
+ BOOL more_data = FALSE;
+ BOOL strict = TRUE;
+
+ /* According to Martin Tuma @avast the protocol uses "escaped
+ whitespace", that is, every embedded whitespace is backslash
+ escaped, as well as backslash is protected by backslash.
+ The returned lines contain the name of the scanned file, a tab
+ and the [ ] marker.
+ [+] - not infected
+ [L] - infected
+ [E] - some error occurred
+ Such marker follows the first non-escaped TAB. For more information
+ see avast-protocol(5)
+
+ We observed two cases:
+ -> SCAN /file
+ <- /file [E]0.0 Error 13 Permission denied
+ <- 451 SCAN Engine error 13 permission denied
+
+ -> SCAN /file
+ <- /file… [E]3.0 Error 41120 The file is a decompression bomb
+ <- /file… [+]2.0
+ <- /file… [+]2.0 0 Eicar Test Virus!!!
+ <- 200 SCAN OK
+
+ If the scanner returns 4xx, DEFER is a good decision, combined
+ with a panic log entry, to get the admin's attention.
+
+ If the scanner returns 200, we reject it as malware, if found any,
+ or, in case of an error, we set the malware message to the error
+ string.
+
+ Some of the >= 42000 errors are message related - usually some
+ broken archives etc, but some of them are e.g. license related.
+ Once the license expires the engine starts returning errors for
+ every scanning attempt. I¹ have the full list of the error codes
+ but it is not a public API and is subject to change. It is hard
+ for me to say what you should do in case of an engine error. You
+ can have a “Treat * unscanned file as infection” policy or “Treat
+ unscanned file as clean” policy. ¹) Jakub Bednar
+
+ */
+
+ if ( ( !ava_re_clean
+ && !(ava_re_clean = m_pcre_compile(ava_re_clean_str, &errstr)))
+ || ( !ava_re_virus
+ && !(ava_re_virus = m_pcre_compile(ava_re_virus_str, &errstr)))
+ || ( !ava_re_error
+ && !(ava_re_error = m_pcre_compile(ava_re_error_str, &errstr)))
+ )
+ return malware_panic_defer(errstr);
+
+ /* wait for result */
+ for (avast_stage = AVA_HELO;
+ (nread = recv_line(malware_daemon_ctx.sock, buf, sizeof(buf), tmo)) > 0;
+ )
+ {
+ int slen = Ustrlen(buf);
+ if (slen >= 1)
+ {
+
+ /* Multi line responses are bracketed between 210 … and nnn … */
+ if (Ustrncmp(buf, "210", 3) == 0)
+ {
+ more_data = 1;
+ continue;
+ }
+ else if (more_data && isdigit(buf[0])) more_data = 0;
+
+ switch (avast_stage)
+ {
+ case AVA_HELO:
+ if (more_data) continue;
+ if (Ustrncmp(buf, "220", 3) != 0)
+ goto endloop; /* require a 220 */
+ goto sendreq;
+
+ case AVA_OPT:
+ if (more_data) continue;
+ if (Ustrncmp(buf, "200", 3) != 0)
+ goto endloop; /* require a 200 */
+
+ sendreq:
+ {
+ int len;
+ /* Check for another option to send. Newline-terminate it. */
+ if ((scanrequest = string_nextinlist(&av_scanner_work, &sep,
+ NULL, 0)))
+ {
+ if (Ustrcmp(scanrequest, "pass_unscanned") == 0)
+ {
+ DEBUG(D_acl) debug_printf_indent("pass unscanned files as clean\n");
+ strict = FALSE;
+ goto sendreq;
+ }
+ scanrequest = string_sprintf("%s\n", scanrequest);
+ avast_stage = AVA_OPT; /* just sent option */
+ DEBUG(D_acl) debug_printf_indent("send to avast OPTION: %s", scanrequest);
+ }
+ else
+ {
+ scanrequest = string_sprintf("SCAN %s\n", eml_dir);
+ avast_stage = AVA_RSP; /* just sent command */
+ DEBUG(D_acl) debug_printf_indent("send to avast REQUEST: SCAN %s\n", eml_dir);
+ }
+
+ /* send config-cmd or scan-request to socket */
+ len = Ustrlen(scanrequest);
+ if (send(malware_daemon_ctx.sock, scanrequest, len, 0) == -1)
+ {
+ scanrequest[len-1] = '\0';
+ return m_panic_defer_3(scanent, CUS callout_address, string_sprintf(
+ "unable to send request '%s' to socket (%s): %s",
+ scanrequest, scanner_options, strerror(errno)), malware_daemon_ctx.sock);
+ }
+ break;
+ }
+
+ case AVA_RSP:
+
+ if (isdigit(buf[0])) /* We're done */
+ goto endloop;
+
+ if (malware_name) /* Nothing else matters, just read on */
+ break;
+
+ if (regex_match(ava_re_clean, buf, slen, NULL))
+ break;
+
+ if ((malware_name = m_pcre_exec(ava_re_virus, buf)))
+ {
+ unescape(malware_name);
+ DEBUG(D_acl)
+ debug_printf_indent("unescaped malware name: '%s'\n", malware_name);
+ break;
+ }
+
+ if (strict) /* treat scanner errors as malware */
+ {
+ if ((malware_name = m_pcre_exec(ava_re_error, buf)))
+ {
+ unescape(malware_name);
+ DEBUG(D_acl)
+ debug_printf_indent("unescaped error message: '%s'\n", malware_name);
+ break;
+ }
+ }
+ else if (regex_match(ava_re_error, buf, slen, NULL))
+ {
+ log_write(0, LOG_MAIN, "internal scanner error (ignored): %s", buf);
+ break;
+ }
+
+ /* here also for any unexpected response from the scanner */
+ DEBUG(D_acl) debug_printf("avast response not handled: '%s'\n", buf);
+
+ goto endloop;
+
+ default: log_write(0, LOG_PANIC, "%s:%d:%s: should not happen",
+ __FILE__, __LINE__, __FUNCTION__);
+ }
+ }
+ }
+
+ endloop:
+
+ if (nread == -1) error_message = US"EOF from scanner";
+ else if (nread < 0) error_message = US"timeout from scanner";
+ else if (nread == 0) error_message = US"got nothing from scanner";
+ else if (buf[0] != '2') error_message = buf;
+
+ DEBUG(D_acl) debug_printf_indent("sent to avast QUIT\n");
+ if (send(malware_daemon_ctx.sock, "QUIT\n", 5, 0) == -1)
+ return m_panic_defer_3(scanent, CUS callout_address,
+ string_sprintf("unable to send quit request to socket (%s): %s",
+ scanner_options, strerror(errno)), malware_daemon_ctx.sock);
+
+ if (error_message)
+ return m_panic_defer_3(scanent, CUS callout_address, error_message, malware_daemon_ctx.sock);
+
+ }
+#endif
+ } /* scanner type switch */
+
+ if (malware_daemon_ctx.sock >= 0)
+ (void) close (malware_daemon_ctx.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_indent(
+ "Matched regex to malware [%s] [%s]\n", malware_re, malware_name);
+ return OK;
+ }
+else
+ return FAIL;
+}
+
+
+/*************************************************
+* 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:
+ malware_re match condition for "malware="
+ timeout if nonzero, timeout in seconds
+
+Returns: Exim message processing code (OK, FAIL, DEFER, ...)
+ where true means malware was found (condition applies)
+*/
+int
+malware(const uschar * malware_re, int timeout)
+{
+int ret = malware_internal(malware_re, NULL, timeout);
+
+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 message_id_buf[64];
+int ret;
+
+/* 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);
+f.enable_dollar_recipients = TRUE;
+
+ret = malware_internal(US"*", eml_filename, 0);
+
+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;
+}
+
+
+void
+malware_init(void)
+{
+if (!malware_default_re)
+ malware_default_re = regex_must_compile(malware_regex_default, FALSE, TRUE);
+
+#ifndef DISABLE_MAL_DRWEB
+if (!drweb_re)
+ drweb_re = regex_must_compile(drweb_re_str, FALSE, TRUE);
+#endif
+#ifndef DISABLE_MAL_FSECURE
+if (!fsec_re)
+ fsec_re = regex_must_compile(fsec_re_str, FALSE, TRUE);
+#endif
+#ifndef DISABLE_MAL_KAV
+if (!kav_re_sus)
+ kav_re_sus = regex_must_compile(kav_re_sus_str, FALSE, TRUE);
+if (!kav_re_inf)
+ kav_re_inf = regex_must_compile(kav_re_inf_str, FALSE, TRUE);
+#endif
+#ifndef DISABLE_MAL_AVAST
+if (!ava_re_clean)
+ ava_re_clean = regex_must_compile(ava_re_clean_str, FALSE, TRUE);
+if (!ava_re_virus)
+ ava_re_virus = regex_must_compile(ava_re_virus_str, FALSE, TRUE);
+if (!ava_re_error)
+ ava_re_error = regex_must_compile(ava_re_error_str, FALSE, TRUE);
+#endif
+#ifndef DISABLE_MAL_FFROT6D
+if (!fprot6d_re_error)
+ fprot6d_re_error = regex_must_compile(fprot6d_re_error_str, FALSE, TRUE);
+if (!fprot6d_re_virus)
+ fprot6d_re_virus = regex_must_compile(fprot6d_re_virus_str, FALSE, TRUE);
+#endif
+}
+
+
+gstring *
+malware_show_supported(gstring * g)
+{
+g = string_cat(g, US"Malware:");
+for (struct scan * sc = m_scans; sc->scancode != (scanner_t)-1; sc++)
+ g = string_fmt_append(g, " %s", sc->name);
+return string_cat(g, US"\n");
+}
+
+
+# endif /*!MACRO_PREDEF*/
+#endif /*WITH_CONTENT_SCAN*/
+/*
+ * vi: aw ai sw=2
+ */
diff --git a/src/match.c b/src/match.c
new file mode 100644
index 0000000..2e4bff0
--- /dev/null
+++ b/src/match.c
@@ -0,0 +1,1352 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 {
+ const uschar *origsubject; /* caseful; keep these two first, in */
+ const 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 {
+ const 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, const uschar *pattern, const uschar **valueptr, uschar **error)
+{
+const check_string_block *cb = arg;
+int search_type, partial, affixlen, starflags;
+int expand_setup = cb->expand_setup;
+const uschar * affix, * opts;
+uschar *s;
+uschar *filename = NULL;
+uschar *keyquery, *result, *semicolon;
+void *handle;
+
+if (valueptr) *valueptr = NULL;
+
+/* 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 = string_copy(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; /* $0 (might be) the matched subject in full */
+ 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 pcre2_code * re = regex_must_compile(pattern, cb->caseless, FALSE);
+ if (expand_setup < 0
+ ? !regex_match(re, s, -1, NULL)
+ : !regex_match_and_setup(re, s, 0, expand_setup)
+ )
+ return FAIL;
+ if (valueptr) *valueptr = pattern; /* "value" gets the RE */
+ return OK;
+ }
+
+/* Tail match */
+
+if (pattern[0] == '*')
+ {
+ int slen = Ustrlen(s);
+ int patlen; /* Sun compiler doesn't like non-constant initializer */
+
+ patlen = Ustrlen(++pattern);
+ if (patlen > slen) return FAIL;
+ if (cb->caseless
+ ? strncmpic(s + slen - patlen, pattern, patlen) != 0
+ : Ustrncmp(s + slen - patlen, pattern, patlen) != 0)
+ return FAIL;
+ if (expand_setup >= 0)
+ {
+ expand_nstring[++expand_setup] = s; /* write a $n, the matched subject variable-part */
+ expand_nlength[expand_setup] = slen - patlen;
+ expand_nmax = expand_setup; /* commit also $0, the matched subject */
+ }
+ if (valueptr) *valueptr = pattern - 1; /* "value" gets the (original) pattern */
+ return OK;
+ }
+
+/* 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)
+ {
+ int slen = Ustrlen(s);
+ if (s[0] != '[' && s[slen-1] != ']') return FAIL; /*XXX should this be || ? */
+ for (ip_address_item * ip = host_find_interfaces(); ip; ip = ip->next)
+ if (Ustrncmp(ip->address, s+1, slen - 2) == 0
+ && ip->address[slen - 2] == 0)
+ {
+ if (expand_setup >= 0) expand_nmax = expand_setup; /* commit $0, the IP addr */
+ if (valueptr) *valueptr = pattern; /* "value" gets the pattern */
+ 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;
+ const uschar *ss = pattern + 4;
+ const 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) 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/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) && (prim || !removed))
+ return FAIL;
+
+ if (expand_setup >= 0) expand_nmax = expand_setup; /* commit $0, the matched subject */
+ if (valueptr) *valueptr = pattern; /* "value" gets the patterm */
+ return OK;
+
+ /*** 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)
+ {
+ if (cb->caseless ? strcmpic(s, pattern) != 0 : Ustrcmp(s, pattern) != 0)
+ return FAIL;
+ if (expand_setup >= 0) expand_nmax = expand_setup; /* Original code! $0 gets the matched subject */
+ if (valueptr) *valueptr = pattern; /* "value" gets the pattern */
+ return OK;
+ }
+
+/* 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, &opts);
+*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 = search_args(search_type, s, semicolon+1, &filename, opts);
+
+/* 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. */
+
+if (!(handle = search_open(filename, search_type, 0, NULL, 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, opts);
+
+if (!result) return f.search_find_defer ? DEFER : FAIL;
+if (valueptr) *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(const uschar *s, const uschar *pattern, int expand_setup,
+ BOOL use_partial, BOOL caseless, BOOL at_is_special, const 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 const 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(const uschar **listptr, int sep, tree_node **anchorptr,
+ unsigned int **cache_ptr, int (*func)(void *,const uschar *,const uschar **,uschar **),
+ void *arg, int type, const uschar *name, const uschar **valueptr)
+{
+int yield = OK;
+unsigned int * original_cache_bits = *cache_ptr;
+BOOL include_unknown = FALSE, ignore_unknown = FALSE,
+ include_defer = FALSE, ignore_defer = FALSE;
+const uschar *list;
+uschar *sss;
+uschar *ot = NULL;
+
+/* 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) 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)
+ {
+ HDEBUG(D_lists) if (ot) debug_printf_indent("%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)
+ {
+ check_string_block *cb = (check_string_block *)arg;
+ deliver_domain = string_copy(cb->subject);
+ list = expand_cstring(*listptr);
+ deliver_domain = NULL;
+ }
+ else
+ list = expand_cstring(*listptr);
+
+ if (!list)
+ {
+ if (f.expand_string_forcedfail)
+ {
+ HDEBUG(D_lists) debug_printf_indent("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 */
+#define LIST_LIMIT_PR 2048
+
+HDEBUG(D_any) if (!ot)
+ {
+ int n, m;
+ gstring * g = string_fmt_append(NULL, "%s in \"%n%.*s%n\"",
+ name, &n, LIST_LIMIT_PR, list, &m);
+ if (m - n >= LIST_LIMIT_PR) g = string_catn(g, US"...", 3);
+ g = string_catn(g, US"?", 1);
+ gstring_release_unused(g);
+ ot = string_from_gstring(g);
+ }
+
+/* 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, NULL, 0)))
+ {
+ 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)
+ 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(US 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)
+ {
+ 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;
+
+ if (!(t = tree_search(*anchorptr, ss+1)))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "unknown named%s list \"%s\"",
+ type == MCL_DOMAIN ? " domain" :
+ type == MCL_HOST ? " host" :
+ type == MCL_ADDRESS ? " address" :
+ type == MCL_LOCALPART ? " local part" : "",
+ ss);
+ return DEFER;
+ }
+ 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)
+ {
+ 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)
+ *cache_ptr = NULL;
+ else
+ {
+ use_cache_bits[offset] |= bits << shift;
+
+ if (valueptr)
+ {
+ 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), GET_UNTAINTED);
+ p->key = string_copy(get_check_key(arg, type));
+
+
+ p->data = *valueptr ? string_copy(*valueptr) : NULL;
+ store_pool = old_pool;
+
+ p->next = nb->cache_data;
+ nb->cache_data = p;
+ if (*valueptr)
+ DEBUG(D_lists) debug_printf_indent("data from lookup saved for "
+ "cache for %s: key '%s' value '%s'\n", ss, p->key, *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_indent("cached %s match for %s\n",
+ (bits & (-bits)) == bits ? "yes" : "no", ss);
+
+ cached = US" - cached";
+ if (valueptr)
+ {
+ const uschar *key = get_check_key(arg, type);
+
+ for (namedlist_cacheblock * p = nb->cache_data; p; p = p->next)
+ if (Ustrcmp(key, p->key) == 0)
+ {
+ *valueptr = p->data;
+ break;
+ }
+ DEBUG(D_lists) debug_printf_indent("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_indent("%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_indent("%s %s (matched \"%s\")\n", ot,
+ (yield == OK)? "yes" : "no", sss);
+ return yield;
+
+ case DEFER:
+ if (!error)
+ error = string_sprintf("DNS lookup of \"%s\" deferred", ss);
+ if (ignore_defer)
+ {
+ HDEBUG(D_lists) debug_printf_indent("%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;
+ }
+ if (!search_error_message) search_error_message = error;
+ 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_indent("%s: item ignored by +ignore_unknown\n",
+ error);
+ }
+ else
+ {
+ HDEBUG(D_lists) debug_printf_indent("%s %s (%s)\n", ot,
+ include_unknown? "yes":"no", error);
+ if (!include_unknown)
+ {
+ if (LOGGING(unknown_in_list))
+ 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)
+ {
+ 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("%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) 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_indent("%s %s (matched \"%s\" in %s)\n", ot,
+ yield == OK ? "yes" : "no", sss, filename);
+
+ /* The "pattern" being matched came from the file; we use a stack-local.
+ Copy it to allocated memory now we know it matched. */
+
+ if (valueptr) *valueptr = string_copy(ss);
+ return file_yield;
+
+ case DEFER:
+ if (!error)
+ error = string_sprintf("DNS lookup of %s deferred", ss);
+ if (ignore_defer)
+ {
+ HDEBUG(D_lists) debug_printf_indent("%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_indent("%s: item ignored by +ignore_unknown\n",
+ error);
+ }
+ else
+ {
+ HDEBUG(D_lists) debug_printf_indent("%s %s (%s)\n", ot,
+ include_unknown? "yes":"no", error);
+ (void)fclose(f);
+ if (!include_unknown)
+ {
+ if (LOGGING(unknown_in_list))
+ 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_indent("%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(const uschar *s, const uschar **listptr, int sep,
+ tree_node **anchorptr,
+ unsigned int *cache_bits, int type, BOOL caseless, const 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.at_is_special = FALSE;
+switch (type & ~MCL_NOEXPAND)
+ {
+ case MCL_DOMAIN: cb.at_is_special = TRUE; /*FALLTHROUGH*/
+ case MCL_LOCALPART: cb.expand_setup = 0; break;
+ default: cb.expand_setup = sep > UCHAR_MAX ? 0 : -1; break;
+ }
+cb.use_partial = TRUE;
+cb.caseless = caseless;
+if (valueptr) *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, const uschar *pattern, const 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;
+const uschar * listptr;
+uschar * subject = cb->address;
+const uschar * s;
+uschar * pdomain, * sdomain;
+uschar * value = NULL;
+
+DEBUG(D_lists) debug_printf_indent("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) return *pattern ? FAIL : OK;
+
+/* 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, *ss;
+
+ 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 (const uschar * key = sdomain + 1; key && watchdog-- > 0; )
+ {
+ int sep = 0;
+
+ if ((rc = match_check_string(key, pattern + 2, -1, TRUE, FALSE, FALSE,
+ CUSS &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(CUSS &list, &sep, NULL, 0)))
+ {
+ 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
+ ? strncmpic(subject+sllen-cllen, pattern + 1, cllen) != 0
+ : 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;
+ }
+ value = string_copyn(pattern + 1, cllen);
+ }
+ else
+ {
+ if (sllen != pllen) return FAIL;
+ if (cb->caseless
+ ? strncmpic(subject, pattern, sllen) != 0
+ : Ustrncmp(subject, pattern, sllen) != 0) return FAIL;
+ }
+ value = string_copyn(pattern, sllen);
+ }
+
+/* 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 ? pdomain + 1 : pattern,
+ 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 ? pdomain + 1 : pattern;
+if (valueptr) *valueptr = NULL;
+
+ {
+ const uschar * dvalue = NULL;
+ rc = 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 */
+ &dvalue); /* where to pass back lookup data */
+ if (valueptr && (value || dvalue))
+ *valueptr = string_sprintf("%s@%s",
+ value ? value : US"", dvalue ? dvalue : US"");
+ }
+return rc;
+}
+
+
+
+
+/*************************************************
+* 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(const uschar *address, BOOL caseless, BOOL expand,
+ const uschar **listptr, unsigned int *cache_bits, int expand_setup, int sep,
+ const uschar **valueptr)
+{
+check_address_block ab;
+unsigned int *local_cache_bits = cache_bits;
+int len;
+
+/* 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.
+Limit the subject address size to avoid mem-exhaustion attacks. The size chosen
+is historical (we used to use big_buffer here). */
+
+if ((len = Ustrlen(address)) > BIG_BUFFER_SIZE) len = BIG_BUFFER_SIZE;
+ab.address = string_copyn(address, len);
+
+for (uschar * p = ab.address + len - 1; p >= ab.address; 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 is above */
+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);
+}
+
+/* Simpler version of match_address_list; always caseless, expanding,
+no cache bits, no value-return.
+
+Arguments:
+ address address to test
+ listptr list to check against
+ sep separator character for the list;
+ may be 0 to get separator from the list;
+ may be UCHAR_MAX+1 for one-item list
+
+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_basic(const uschar *address, const uschar **listptr, int sep)
+{
+return match_address_list(address, TRUE, TRUE, listptr, NULL, -1, sep, NULL);
+}
+
+/* End of match.c */
diff --git a/src/md5.c b/src/md5.c
new file mode 100644
index 0000000..fdb144e
--- /dev/null
+++ b/src/md5.c
@@ -0,0 +1,355 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 */
+/* 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];
+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 (int 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)
+{
+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 (int 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 = US (&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)
+ {
+ 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 (int 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/memcheck.h b/src/memcheck.h
new file mode 100644
index 0000000..6f97b09
--- /dev/null
+++ b/src/memcheck.h
@@ -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
index 0000000..c192199
--- /dev/null
+++ b/src/mime.c
@@ -0,0 +1,803 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/*
+ * Copyright (c) The Exim Maintainers 2015 - 2022
+ * Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004 - 2015
+ * 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;
+
+static mime_header mime_header_list[] = {
+ /* name namelen value */
+ { 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 = nelem(mime_header_list);
+
+static mime_parameter mime_parameter_list[] = {
+ /* name namelen value */
+ { US"name=", 5, &mime_filename },
+ { US"filename=", 9, &mime_filename },
+ { US"charset=", 8, &mime_charset },
+ { US"boundary=", 9, &mime_boundary }
+};
+
+
+/*************************************************
+* set MIME anomaly level + text *
+*************************************************/
+
+/* Small wrapper to set the two expandables which
+ give info on detected "problems" in MIME
+ encodings. Indexes are defined in mime.h. */
+
+void
+mime_set_anomaly(int idx)
+{
+struct anom {
+ int level;
+ const uschar * text;
+} anom[] = { {1, CUS"Broken Quoted-Printable encoding detected"},
+ {2, CUS"Broken BASE64 encoding detected"} };
+
+mime_anomaly_level = anom[idx].level;
+mime_anomaly_text = anom[idx].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
+*/
+
+static 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 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++ = '=';
+ ++ipos;
+ }
+ else if (decode_qp_result == -1)
+ break;
+ else if (decode_qp_result >= 0)
+ *opos++ = decode_qp_result;
+ }
+ else
+ *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;
+}
+
+
+/*
+ * Return open filehandle for combo of path and file.
+ * Side-effect: set mime_decoded_filename, to copy in allocated mem
+ */
+static FILE *
+mime_get_decode_file(uschar *pname, uschar *fname)
+{
+if (pname && fname)
+ mime_decoded_filename = string_sprintf("%s/%s", pname, fname);
+else if (!pname)
+ mime_decoded_filename = string_copy(fname);
+else if (!fname)
+ {
+ int file_nr = 0;
+ int result = 0;
+
+ /* must find first free sequential filename */
+ do
+ {
+ struct stat mystat;
+ mime_decoded_filename = string_sprintf("%s/%s-%05u", pname, message_id, file_nr++);
+ /* security break */
+ if (file_nr >= 1024)
+ break;
+ result = stat(CS mime_decoded_filename, &mystat);
+ } while(result != -1);
+ }
+
+return modefopen(mime_decoded_filename, "wb+", SPOOL_MODE);
+}
+
+
+int
+mime_decode(const uschar **listptr)
+{
+int sep = 0;
+const uschar *list = *listptr;
+uschar * option;
+uschar * decode_path;
+FILE *decode_file = NULL;
+long f_pos = 0;
+ssize_t size_counter = 0;
+ssize_t (*decode_function)(FILE*, FILE*, uschar*);
+
+if (!mime_stream || (f_pos = ftell(mime_stream)) < 0)
+ return FAIL;
+
+/* build default decode path (will exist since MBOX must be spooled up) */
+decode_path = string_sprintf("%s/scan/%s", spool_directory, message_id);
+
+/* try to find 1st option */
+if ((option = string_nextinlist(&list, &sep, NULL, 0)))
+ {
+ /* 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);
+if (fseek(mime_stream, f_pos, SEEK_SET))
+ return DEFER;
+
+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;
+}
+
+
+static 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 */
+return c == EOF || num_copied == 1 ? 0 : 1;
+}
+
+
+/* reset all per-part mime variables */
+static void
+mime_vars_reset(void)
+{
+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;
+}
+
+
+/* Grab a parameter value, dealing with quoting.
+
+Arguments:
+ str Input string. Updated on return to point to terminating ; or NUL
+
+Return:
+ Allocated string with parameter value
+*/
+static uschar *
+mime_param_val(uschar ** sp)
+{
+uschar * s = *sp;
+gstring * val = NULL;
+
+/* debug_printf_indent(" considering paramval '%s'\n", s); */
+
+while (*s && *s != ';') /* ; terminates */
+ if (*s == '"')
+ {
+ s++; /* skip opening " */
+ while (*s && *s != '"') /* " protects ; */
+ val = string_catn(val, s++, 1);
+ if (*s) s++; /* skip closing " */
+ }
+ else
+ val = string_catn(val, s++, 1);
+*sp = s;
+return string_from_gstring(val);
+}
+
+static uschar *
+mime_next_semicolon(uschar * s)
+{
+while (*s && *s != ';') /* ; terminates */
+ if (*s == '"')
+ {
+ s++; /* skip opening " */
+ while (*s && *s != '"') /* " protects ; */
+ s++;
+ if (*s) s++; /* skip closing " */
+ }
+ else
+ s++;
+return s;
+}
+
+
+static uschar *
+rfc2231_to_2047(const uschar * fname, const uschar * charset, int * len)
+{
+gstring * val = string_catn(NULL, US"=?", 2);
+uschar c;
+
+if (charset)
+ val = string_cat(val, charset);
+val = string_catn(val, US"?Q?", 3);
+
+while ((c = *fname))
+ if (c == '%' && isxdigit(fname[1]) && isxdigit(fname[2]))
+ {
+ val = string_catn(val, US"=", 1);
+ val = string_catn(val, ++fname, 2);
+ fname += 2;
+ }
+ else
+ val = string_catn(val, fname++, 1);
+
+val = string_catn(val, US"?=", 2);
+*len = val->ptr;
+return string_from_gstring(val);
+}
+
+
+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. Assume tainted data. */
+header = store_get(MIME_MAX_HEADER_SIZE+1, GET_TAINTED);
+
+/* 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_vars_reset();
+
+ /* 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) for (;;)
+ {
+ if (!fgets(CS header, MIME_MAX_HEADER_SIZE, f))
+ {
+ /* Hit EOF or read error. Ugh. */
+ DEBUG(D_acl) debug_printf_indent("MIME: Hit EOF ...\n");
+ return rc;
+ }
+
+ /* boundary line must start with 2 dashes */
+ if ( Ustrncmp(header, "--", 2) == 0
+ && Ustrncmp(header+2, context->boundary, Ustrlen(context->boundary)) == 0
+ )
+ { /* found boundary */
+ if (Ustrncmp((header+2+Ustrlen(context->boundary)), "--", 2) == 0)
+ {
+ /* END boundary found */
+ DEBUG(D_acl) debug_printf_indent("MIME: End boundary found %s\n",
+ context->boundary);
+ return rc;
+ }
+
+ DEBUG(D_acl) debug_printf_indent("MIME: Next part with boundary %s\n",
+ context->boundary);
+ break;
+ }
+ }
+
+ /* parse headers, set up expansion variables */
+ while (mime_get_header(f, header))
+
+ /* look for interesting headers */
+ for (struct mime_header * mh = mime_header_list;
+ mh < mime_header_list + mime_header_list_size;
+ mh++) if (strncmpic(mh->name, header, mh->namelen) == 0)
+ {
+ uschar * p = header + mh->namelen;
+ uschar * q;
+
+ /* grab the value (normalize to lower case)
+ and copy to its corresponding expansion variable */
+
+ for (q = p; *q != ';' && *q; q++) ;
+ *mh->value = string_copynlc(p, q-p);
+ DEBUG(D_acl) debug_printf_indent("MIME: found %s header, value is '%s'\n",
+ mh->name, *mh->value);
+
+ if (*(p = q)) p++; /* jump past the ; */
+
+ {
+ uschar * mime_fname = NULL;
+ uschar * mime_fname_rfc2231 = NULL;
+ uschar * mime_filename_charset = NULL;
+ BOOL decoding_failed = FALSE;
+
+ /* grab all param=value tags on the remaining line,
+ check if they are interesting */
+
+ while (*p)
+ {
+ DEBUG(D_acl) debug_printf_indent("MIME: considering paramlist '%s'\n", p);
+
+ if ( !mime_filename
+ && strncmpic(CUS"content-disposition:", header, 20) == 0
+ && strncmpic(CUS"filename*", p, 9) == 0
+ )
+ { /* RFC 2231 filename */
+ uschar * q;
+
+ /* find value of the filename */
+ p += 9;
+ while(*p != '=' && *p) p++;
+ if (*p) p++; /* p is filename or NUL */
+ q = mime_param_val(&p); /* p now trailing ; or NUL */
+
+ if (q && *q)
+ {
+ uschar * temp_string, * err_msg;
+ int slen;
+
+ /* build up an un-decoded filename over successive
+ filename*= parameters (for use when 2047 decode fails) */
+
+ mime_fname_rfc2231 = string_sprintf("%#s%s",
+ mime_fname_rfc2231, q);
+
+ if (!decoding_failed)
+ {
+ int size;
+ if (!mime_filename_charset)
+ {
+ uschar * s = q;
+
+ /* look for a ' in the "filename" */
+ while(*s != '\'' && *s) s++; /* s is 1st ' or NUL */
+
+ if ((size = s-q) > 0)
+ mime_filename_charset = string_copyn(q, size);
+
+ if (*(p = s)) p++;
+ while(*p == '\'') p++; /* p is after 2nd ' */
+ }
+ else
+ p = q;
+
+ DEBUG(D_acl) debug_printf_indent("MIME: charset %s fname '%s'\n",
+ mime_filename_charset ? mime_filename_charset : US"<NULL>", p);
+
+ temp_string = rfc2231_to_2047(p, mime_filename_charset, &slen);
+ DEBUG(D_acl) debug_printf_indent("MIME: 2047-name %s\n", temp_string);
+
+ temp_string = rfc2047_decode(temp_string, FALSE, NULL, ' ',
+ NULL, &err_msg);
+ DEBUG(D_acl) debug_printf_indent("MIME: plain-name %s\n", temp_string);
+
+ if (!temp_string || (size = Ustrlen(temp_string)) == slen)
+ decoding_failed = TRUE;
+ else
+ /* build up a decoded filename over successive
+ filename*= parameters */
+
+ mime_filename = mime_fname = mime_fname
+ ? string_sprintf("%s%s", mime_fname, temp_string)
+ : temp_string;
+ }
+ }
+ }
+
+ else
+ /* look for interesting parameters */
+ for (mime_parameter * mp = mime_parameter_list;
+ mp < mime_parameter_list + nelem(mime_parameter_list);
+ mp++
+ ) if (strncmpic(mp->name, p, mp->namelen) == 0)
+ {
+ uschar * q;
+ uschar * dummy_errstr;
+
+ /* grab the value and copy to its expansion variable */
+ p += mp->namelen;
+ q = mime_param_val(&p); /* p now trailing ; or NUL */
+
+ *mp->value = q && *q
+ ? rfc2047_decode(q, check_rfc2047_length, NULL, 32, NULL,
+ &dummy_errstr)
+ : NULL;
+ DEBUG(D_acl) debug_printf_indent(
+ "MIME: found %s parameter in %s header, value '%s'\n",
+ mp->name, mh->name, *mp->value);
+
+ break; /* done matching param names */
+ }
+
+
+ /* There is something, but not one of our interesting parameters.
+ Advance past the next semicolon */
+ p = mime_next_semicolon(p);
+ if (*p) p++;
+ } /* param scan on line */
+
+ if (strncmpic(CUS"content-disposition:", header, 20) == 0)
+ {
+ if (decoding_failed) mime_filename = mime_fname_rfc2231;
+
+ DEBUG(D_acl) debug_printf_indent(
+ "MIME: found %s parameter in %s header, value is '%s'\n",
+ "filename", mh->name, mime_filename);
+ }
+ }
+ }
+
+ /* set additional flag variables (easier access) */
+ if ( mime_content_type
+ && 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 && nested_context.boundary
+ && Ustrncmp(mime_content_type,"multipart",9) == 0)
+ {
+ DEBUG(D_acl)
+ debug_printf_indent("MIME: 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
+ && Ustrncmp(mime_content_type,"message/rfc822",14) == 0)
+ {
+ const uschar * rfc822name = NULL;
+ uschar * filename;
+ int file_nr = 0;
+ int result = 0;
+
+ /* must find first free sequential filename */
+ for (gstring * g = string_get(64); result != -1; g->ptr = 0)
+ {
+ struct stat mystat;
+ g = string_fmt_append(g,
+ "%s/scan/%s/__rfc822_%05u", spool_directory, message_id, file_nr++);
+ /* security break */
+ if (file_nr >= 128)
+ goto NO_RFC822;
+ result = stat(CS (filename = string_from_gstring(g)), &mystat);
+ }
+
+ 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 acl condition warning - could not decode RFC822 MIME part to file.");
+ rc = DEFER;
+ goto out;
+ }
+ mime_decoded_filename = NULL;
+ }
+
+NO_RFC822:
+ /* If the boundary of this instance is NULL, we are finished here */
+ if (!context) break;
+
+ if (context->context == MBC_COVERLETTER_ONESHOT)
+ context->context = MBC_ATTACHMENT;
+ }
+
+out:
+mime_vars_reset();
+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
index 0000000..5fd4392
--- /dev/null
+++ b/src/mime.h
@@ -0,0 +1,44 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004, 2015
+ * License: GPL
+ * Copyright (c) The Exim Maintainers 2016
+ */
+
+#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;
+ uschar ** value;
+} mime_header;
+
+
+typedef struct mime_parameter {
+ uschar * name;
+ int namelen;
+ uschar ** value;
+} mime_parameter;
+
+/* MIME Anomaly list */
+#define MIME_ANOMALY_BROKEN_BASE64 1
+#define MIME_ANOMALY_BROKEN_QP 0
+
+
+#endif
diff --git a/src/moan.c b/src/moan.c
new file mode 100644
index 0000000..4f2550d
--- /dev/null
+++ b/src/moan.c
@@ -0,0 +1,874 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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)
+ {
+ 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);
+}
+
+
+
+/*************************************************
+* Write References: line for DSN *
+*************************************************/
+
+/* Generate a References: header if there is in the header_list
+at least one of Message-ID:, References:, or In-Reply-To: (see RFC 2822).
+
+Arguments: f the FILE to write to
+ message_id optional already-found message-id, or NULL
+
+Returns: nothing
+*/
+
+void
+moan_write_references(FILE * fp, uschar * message_id)
+{
+header_line * h;
+
+if (!message_id)
+ for (h = header_list; h; h = h->next)
+ if (h->type == htype_id)
+ {
+ message_id = Ustrchr(h->text, ':') + 1;
+ Uskip_whitespace(&message_id);
+ }
+
+for (h = header_list; h; h = h->next)
+ if (h->type != htype_old && strncmpic(US"References:", h->text, 11) == 0)
+ break;
+
+if (!h)
+ for (h = header_list; h; 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 || message_id)
+ {
+ fprintf(fp, "References:");
+ if (h)
+ {
+ const uschar * s;
+ uschar * id, * error;
+ uschar * referenced_ids[12];
+ int reference_count = 0;
+
+ s = Ustrchr(h->text, ':') + 1;
+ f.parse_allow_group = FALSE;
+ while (*s && (s = parse_message_id(s, &id, &error)))
+ if (reference_count == nelem(referenced_ids))
+ {
+ 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 (int i = 0; i < reference_count; ++i)
+ fprintf(fp, " %s", referenced_ids[i]);
+ }
+
+ /* The message id will have a newline on the end of it. */
+
+ if (message_id) fprintf(fp, " %s", message_id);
+ else fprintf(fp, "\n");
+ }
+}
+
+
+
+/*************************************************
+* 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
+*/
+
+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 * fp;
+int pid;
+
+#ifdef SUPPORT_DMARC
+uschar * s, * s2;
+
+/* For DMARC if there is a specific sender set, expand the variable for the
+header From: and grab the address from that for the envelope FROM. */
+
+if ( ident == ERRMESS_DMARC_FORENSIC
+ && dmarc_forensic_sender
+ && (s = expand_string(dmarc_forensic_sender))
+ && *s
+ && (s2 = expand_string(string_sprintf("${address:%s}", s)))
+ && *s2
+ )
+ pid = child_open_exim2(&fd, s2, bounce_sender_authentication,
+ US"moan_send_message");
+else
+ {
+ s = NULL;
+ pid = child_open_exim(&fd, US"moan_send_message");
+ }
+
+#else
+pid = child_open_exim(&fd, US"moan_send_message");
+#endif
+
+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 */
+
+fp = fdopen(fd, "wb");
+if (errors_reply_to) fprintf(fp, "Reply-To: %s\n", errors_reply_to);
+fprintf(fp, "Auto-Submitted: auto-replied\n");
+
+#ifdef SUPPORT_DMARC
+if (s)
+ fprintf(fp, "From: %s\n", s);
+else
+#endif
+ moan_write_from(fp);
+
+fprintf(fp, "To: %s\n", recipient);
+moan_write_references(fp, NULL);
+
+switch(ident)
+ {
+ case ERRMESS_BADARGADDRESS:
+ fprintf(fp,
+ "Subject: Mail failure - malformed recipient address\n\n");
+ fprintf(fp,
+ "A message that you sent contained a recipient address that was incorrectly\n"
+ "constructed:\n\n");
+ fprintf(fp, " %s %s\n", eblock->text1, eblock->text2);
+ count = Ustrlen(eblock->text1);
+ if (count > 0 && eblock->text1[count-1] == '.')
+ fprintf(fp,
+ "\nRecipient addresses must not end with a '.' character.\n");
+ fprintf(fp,
+ "\nThe message has not been delivered to any recipients.\n");
+ break;
+
+ case ERRMESS_BADNOADDRESS:
+ case ERRMESS_BADADDRESS:
+ fprintf(fp,
+ "Subject: Mail failure - malformed recipient address\n\n");
+ fprintf(fp,
+ "A message that you sent contained one or more recipient addresses that were\n"
+ "incorrectly constructed:\n\n");
+
+ while (eblock)
+ {
+ fprintf(fp, " %s: %s\n", eblock->text1, eblock->text2);
+ count++;
+ eblock = eblock->next;
+ }
+
+ fprintf(fp, (count == 1)? "\nThis address has been ignored. " :
+ "\nThese addresses have been ignored. ");
+
+ fprintf(fp, (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(fp, "Subject: Mail failure - no recipient addresses\n\n");
+ fprintf(fp,
+ "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(fp, "Subject: Mail failure - no recipient addresses\n\n");
+ fprintf(fp,
+ "A message that you sent contained no recipient addresses, and therefore no\n"
+ "delivery could be attempted.\n");
+ break;
+
+ case ERRMESS_IOERR:
+ fprintf(fp, "Subject: Mail failure - system failure\n\n");
+ fprintf(fp,
+ "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(fp, "Subject: Mail failure - overlong header section\n\n");
+ fprintf(fp,
+ "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(fp, "Subject: Mail failure - overlong header line\n\n");
+ fprintf(fp,
+ "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(fp, "Subject: Mail failure - message too big\n\n");
+ fprintf(fp,
+ "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(fp, "Subject: Mail failure - too many recipients\n\n");
+ fprintf(fp,
+ "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(fp, "Subject: Mail failure - rejected by local scanning code\n\n");
+ fprintf(fp,
+ "A message that you sent was rejected by the local scanning code that\n"
+ "checks incoming messages on this system.");
+ if (eblock->text1)
+ fprintf(fp, " The following error was given:\n\n %s", eblock->text1);
+ fprintf(fp, "\n");
+ break;
+
+#ifdef SUPPORT_DMARC
+ case ERRMESS_DMARC_FORENSIC:
+ bounce_return_message = TRUE;
+ bounce_return_body = FALSE;
+ fprintf(fp, "Subject: DMARC Forensic Report for %s from IP %s\n\n",
+ eblock ? eblock->text2 : US"Unknown",
+ sender_host_address);
+ fprintf(fp,
+ "A message claiming to be from you has failed the published DMARC\n"
+ "policy for your domain.\n\n");
+ while (eblock)
+ {
+ fprintf(fp, " %s: %s\n", eblock->text1, eblock->text2);
+ count++;
+ eblock = eblock->next;
+ }
+ break;
+#endif
+
+ default:
+ fprintf(fp, "Subject: Mail failure\n\n");
+ fprintf(fp,
+ "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(fp, "\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(fp, "\n"
+ "------ No more than %d%s characters of the body are included.\n\n",
+ x, k);
+ }
+ else fprintf(fp, " ------\n\n");
+ }
+ else
+ {
+ fprintf(fp, "\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)
+ {
+ if (headers->text != NULL) fprintf(fp, "%s", CS headers->text);
+ headers = headers->next;
+ }
+
+ if (ident != ERRMESS_VLONGHEADER && ident != ERRMESS_VLONGHDRLINE)
+ fputc('\n', fp);
+
+ /* 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)
+ {
+ BOOL enddot = f.dot_ends && message_file == stdin;
+ uschar * buf = store_get(bounce_return_linesize_limit+2, GET_TAINTED);
+
+ if (firstline) fprintf(fp, "%s", CS firstline);
+
+ while (fgets(CS buf, bounce_return_linesize_limit+2, message_file))
+ {
+ int len;
+
+ if (enddot && *buf == '.' && buf[1] == '\n')
+ {
+ fputc('.', fp);
+ break;
+ }
+
+ len = Ustrlen(buf);
+ if (buf[len-1] != '\n')
+ { /* eat rest of partial line */
+ int ch;
+ while ((ch = fgetc(message_file)) != EOF && ch != '\n') ;
+ }
+
+ if (size_limit > 0 && len > size_limit - written)
+ {
+ buf[size_limit - written] = '\0';
+ fputs(CS buf, fp);
+ break;
+ }
+
+ fputs(CS buf, fp);
+ }
+ }
+#ifdef SUPPORT_DMARC
+ /* Overkill, but use exact test in case future code gets inserted */
+ else if (bounce_return_body && message_file == NULL)
+ {
+ /*XXX limit line length here? */
+ /* This doesn't print newlines, disable until can parse and fix
+ * output to be legible. */
+ fprintf(fp, "%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(fp);
+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)
+ 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 && f.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) sender_address = new_sender;
+ else firstline = big_buffer;
+ }
+
+/* If viable sender address, send a message */
+
+if (sender_address && sender_address[0] && !f.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, US"moan_tell_someone");
+
+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);
+moan_write_references(f, NULL);
+fprintf(f, "Subject: %s\n\n", subject);
+va_start(ap, format);
+vfprintf(f, format, ap);
+va_end(ap);
+
+if (addr)
+ {
+ fprintf(f, "\nThe following address(es) have yet to be delivered:\n");
+ for (; addr; addr = addr->next)
+ {
+ uschar * parent = addr->parent ? addr->parent->address : NULL;
+ fprintf(f, " %s", addr->address);
+ if (parent) fprintf(f, " <%s>", parent);
+ if (addr->basic_errno > 0) fprintf(f, ": %s", strerror(addr->basic_errno));
+ if (addr->message) 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;
+const uschar *listptr = errors_copy;
+uschar *yield = NULL;
+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, NULL, 0)))
+ {
+ const uschar *newaddress = item;
+ const 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;
+
+for (error_block * e = eblock; e; 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) return TRUE;
+
+if (!(s = expand_string(syntax_errors_to)))
+ {
+ 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, US"moan_skipped_syntax_errors");
+
+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");
+moan_write_references(f, NULL);
+
+if (custom)
+ {
+ if (!(t = expand_string(custom)))
+ {
+ 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 (error_block * e = eblock; e; 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
index 0000000..49fed0a
--- /dev/null
+++ b/src/mytypes.h
@@ -0,0 +1,150 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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
+
+# include <string.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.
+OpenBSD has unfortunately taken to objecting to use of %n in printf
+so we have to give up on all of the available parameter checking. */
+
+#if defined(__GNUC__) || defined(__clang__)
+# ifndef __OpenBSD__
+# define PRINTF_FUNCTION(A,B) __attribute__((format(printf,A,B)))
+# endif
+# define ARG_UNUSED __attribute__((__unused__))
+# define FUNC_MAYBE_UNUSED __attribute__((__unused__))
+# define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
+# define ALLOC __attribute__((malloc))
+# define ALLOC_SIZE(A) __attribute__((alloc_size(A)))
+# define NORETURN __attribute__((noreturn))
+#else
+# define ARG_UNUSED /**/
+# define FUNC_MAYBE_UNUSED /**/
+# define WARN_UNUSED_RESULT /**/
+# define ALLOC /**/
+# define ALLOC_SIZE(A) /**/
+# define NORETURN /**/
+#endif
+
+#ifndef PRINTF_FUNCTION
+# define PRINTF_FUNCTION(A,B) /**/
+#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 unsigned 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 **)
+#define CUSS (const unsigned char **)
+#define CCSS (const 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 Ufgets(b,n,f) fgets(CS(b),n,f)
+#define Ufopen(s,t) exim_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) exim_open(CCS(s),(n)|O_BINARY,m) /* where all files must */
+# define Uopen2(s,n) exim_open2(CCS(s),(n)|O_BINARY)
+#else /* be opened as binary */
+# define Uopen(s,n,m) exim_open(CCS(s),n,m) /* to avoid problems */
+# define Uopen2(s,n) exim_open2(CCS(s),n)
+#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 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_nt(s,t) strcpy(CS s, CCS t) /* no taint check */
+#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 Ustrncmp(s,t,n) strncmp(CCS(s),CCS(t),n)
+#define Ustrncpy_nt(s,t,n) strncpy(CS s, CCS t, n) /* no taint check */
+#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))
+
+#if defined(EM_VERSION_C) || defined(LOCAL_SCAN) || defined(DLFUNC_IMPL)
+# define Ustrcat(s,t) strcat(CS(s), CCS(t))
+# define Ustrcpy(s,t) strcpy(CS(s), CCS(t))
+# define Ustrncat(s,t,n) strncat(CS(s), CCS(t), n)
+# define Ustrncpy(s,t,n) strncpy(CS(s), CCS(t), n)
+#else
+# define Ustrcat(s,t) __Ustrcat(s, CUS(t), __FUNCTION__, __LINE__)
+# define Ustrcpy(s,t) __Ustrcpy(s, CUS(t), __FUNCTION__, __LINE__)
+# define Ustrncat(s,t,n) __Ustrncat(s, CUS(t), n, __FUNCTION__, __LINE__)
+# define Ustrncpy(s,t,n) __Ustrncpy(s, CUS(t), n, __FUNCTION__, __LINE__)
+#endif
+
+#endif
+/* End of mytypes.h */
diff --git a/src/os.c b/src/os.c
new file mode 100644
index 0000000..ac5f61b
--- /dev/null
+++ b/src/os.c
@@ -0,0 +1,971 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#ifdef STAND_ALONE
+# include <signal.h>
+# include <stdio.h>
+# include <time.h>
+#endif
+
+#ifndef CS
+# define CS (char *)
+# define US (unsigned char *)
+#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, CS (&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));
+
+for (struct ifaddrs * ifa = ifalist; ifa; ifa = ifa->ifa_next)
+ {
+ struct sockaddr * ifa_addr = ifa->ifa_addr;
+ if (!ifa_addr) continue;
+ if (ifa_addr->sa_family != AF_INET
+#if HAVE_IPV6
+ && 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), GET_UNTAINTED);
+ next->next = NULL;
+ next->port = 0;
+ (void)host_ntoa(-1, ifa_addr, next->address, NULL);
+
+ if (!yield)
+ 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 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, CS &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 (char * cp = buf; cp < buf + ifc.V_ifc_len; cp += len)
+ {
+ memcpy(CS &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, CS &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, CS &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), GET_UNTAINTED);
+ 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), GET_UNTAINTED);
+yield->address = US"127.0.0.1";
+yield->port = 0;
+yield->next = NULL;
+
+#if HAVE_IPV6
+yield->next = store_get(sizeof(address_item), GET_UNTAINTED);
+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 */
+
+/* ----------------------------------------------------------------------- */
+
+/***********************************************************
+* unsetenv() *
+***********************************************************/
+
+/* Most modern systems define int unsetenv(const char*),
+* some don't. */
+
+#if !defined(OS_UNSETENV)
+int
+os_unsetenv(const unsigned char * name)
+{
+return unsetenv(CS name);
+}
+#endif
+
+/* ----------------------------------------------------------------------- */
+
+/***********************************************************
+* getcwd() *
+***********************************************************/
+
+/* Glibc allows getcwd(NULL, 0) to do auto-allocation. Some systems
+do auto-allocation, but need the size of the buffer, and others
+may not even do this. If the OS supports getcwd(NULL, 0) we'll use
+this, for all other systems we provide our own getcwd() */
+
+#if !defined(OS_GETCWD)
+unsigned char *
+os_getcwd(unsigned char * buffer, size_t size)
+{
+return US getcwd(CS buffer, size);
+}
+#else
+#ifndef PATH_MAX
+# define PATH_MAX 4096
+#endif
+unsigned char *
+os_getcwd(unsigned char * buffer, size_t size)
+{
+char * b = CS buffer;
+
+if (!size) size = PATH_MAX;
+if (!b && !(b = malloc(size))) return NULL;
+if (!(b = getcwd(b, size))) return NULL;
+return buffer ? buffer : realloc(b, strlen(b) + 1);
+}
+#endif
+
+/* ----------------------------------------------------------------------- */
+
+
+
+
+/*************************************************
+**************************************************
+* 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_CLR(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_CLR(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
index 0000000..547cf13
--- /dev/null
+++ b/src/osfunctions.h
@@ -0,0 +1,43 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2016 */
+/* Copyright (c) The Exim Maintainers 2020 */
+/* 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
+#ifndef os_unsetenv
+extern int os_unsetenv(const uschar *);
+#endif
+#ifndef os_getcwd
+extern uschar *os_getcwd(uschar *, size_t);
+#endif
+
+/* End of osfunctions.h */
diff --git a/src/parse.c b/src/parse.c
new file mode 100644
index 0000000..bdba3ec
--- /dev/null
+++ b/src/parse.c
@@ -0,0 +1,2243 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for parsing addresses */
+
+
+#include "exim.h"
+
+
+static const 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), GET_UNTAINTED);
+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(const 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 US 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
+*/
+
+const uschar *
+parse_find_at(const uschar *s)
+{
+const uschar * t = s + Ustrlen(s);
+while (--t >= s)
+ if (*t == '@')
+ {
+ int backslash_count = 0;
+ const 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
+Returns: new character pointer
+*/
+
+static const uschar *
+skip_comment(const uschar *s)
+{
+last_comment_position = s;
+while (*s)
+ {
+ int c, level;
+
+ if (Uskip_whitespace(&s) != '(') break;
+ level = 1;
+ while((c = *(++s)))
+ {
+ 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.
+
+Optionally, msg_id domain literals ( printable-ascii enclosed in [] )
+are permitted.
+
+Arguments:
+ s current character pointer
+ t where to put the domain
+ msg_id_literals flag for relaxed domain-literal processing
+ errorptr put error message here on failure (*t will be 0 on exit)
+
+Returns: new character pointer
+*/
+
+static const uschar *
+read_domain(const uschar *s, uschar *t, BOOL msg_id_literals, 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;
+ }
+
+ if (msg_id_literals)
+ while (*s >= 33 && *s <= 90 || *s >= 94 && *s <= 126) *t++ = *s++;
+ else
+ while (*s == '.' || *s == ':' || isxdigit(*s)) *t++ = *s++;
+
+ if (*s == ']') *t++ = *s++; else
+ {
+ *errorptr = US"malformed domain literal";
+ *tt = 0;
+ }
+
+ if (!allow_domain_literals && !msg_id_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 const uschar *
+read_local_part(const 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) && c != '\"')
+ {
+ *t++ = c;
+ if (c == '\\' && s[1]) *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) *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 const uschar *
+read_route(const uschar *s, uschar *t, uschar **errorptr)
+{
+BOOL commas = FALSE;
+*errorptr = NULL;
+
+while (*s == '@')
+ {
+ *t++ = '@';
+ s = read_domain(s+1, t, FALSE, 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 const uschar *
+read_addr_spec(const 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, FALSE, 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(const uschar *mailbox, uschar **errorptr, int *start, int *end,
+ int *domain, BOOL allow_null)
+{
+uschar * yield = store_get(Ustrlen(mailbox) + 1, mailbox);
+const uschar *startptr, *endptr;
+const uschar *s = US mailbox;
+uschar *t = US 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) 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 || *s == ';')
+ {
+ if (!*t) 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 != '<' && (!f.parse_allow_group || *s != ':'))
+ {
+ s = read_local_part(s, t, errorptr, FALSE);
+ if (*errorptr)
+ {
+ *errorptr = string_sprintf("%s (expected word or \"<\")", *errorptr);
+ goto PARSE_FAILED;
+ }
+ }
+
+ if (*s == ':')
+ {
+ f.parse_found_group = TRUE;
+ f.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 a limited
+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 == '<')
+ {
+ if(bracket_count++ > 5) FAILED(US"angle-brackets nested too deep");
+ 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) 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) goto PARSE_FAILED;
+ *domain = domainptr - yield;
+ if (source_routed && *domain == 0)
+ FAILED(US"domain missing in source-routed address");
+ }
+
+ endptr = s;
+ if (*errorptr) 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, (int)(s - US 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)
+ {
+ uschar *domainptr = yield;
+ s = read_route(s, t, errorptr);
+ if (*errorptr) goto PARSE_FAILED;
+ *t = 0; /* Ensure route is ignored - probably overkill */
+ s = read_addr_spec(s, t, 0, errorptr, &domainptr);
+ if (*errorptr) 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, TRUE, errorptr);
+ if (!*t) 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)
+ {
+ if (f.parse_found_group && *s == ';')
+ {
+ f.parse_found_group = FALSE;
+ f.parse_allow_group = TRUE;
+ }
+ else
+ {
+ *errorptr = string_sprintf("malformed address: %.32s may not follow %.*s",
+ s, (int)(s - US mailbox), mailbox);
+ goto PARSE_FAILED;
+ }
+ }
+*start = startptr - US mailbox; /* Return offsets */
+while (isspace(endptr[-1])) endptr--;
+*end = endptr - US 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 5321 limits email
+addresses to 256, so we do a check here, giving an error if the address is
+ridiculously long. */
+
+if (*end - *start > EXIM_EMAILADDR_MAX)
+ {
+ *errorptr = string_sprintf("address is ridiculously long: %.64s...", yield);
+ return NULL;
+ }
+
+return 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 (f.parse_found_group && *s == ';')
+ {
+ f.parse_found_group = FALSE;
+ f.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.
+
+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
+ 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 allocated memory containing the quoted string
+*/
+
+const uschar *
+parse_quote_2047(const uschar *string, int len, const uschar *charset,
+ BOOL fold)
+{
+const uschar * s = string;
+int hlen, l;
+BOOL coded = FALSE;
+BOOL first_byte = FALSE;
+gstring * g =
+ string_fmt_append(NULL, "=?%s?Q?", charset ? charset : US"iso-8859-1");
+
+hlen = l = g->ptr;
+
+for (s = string; len > 0; s++, len--)
+ {
+ int ch = *s;
+
+ if (g->ptr - l > 67 && !first_byte)
+ {
+ g = fold ? string_catn(g, US"?=\n ", 4) : string_catn(g, US"?= ", 3);
+ l = g->ptr;
+ g = string_catn(g, g->s, hlen);
+ }
+
+ if ( ch < 33 || ch > 126
+ || Ustrchr("?=()<>@,;:\\\".[]_", ch) != NULL)
+ {
+ if (ch == ' ')
+ {
+ g = string_catn(g, US"_", 1);
+ first_byte = FALSE;
+ }
+ else
+ {
+ g = string_fmt_append(g, "=%02X", ch);
+ coded = TRUE;
+ first_byte = !first_byte;
+ }
+ }
+ else
+ { g = string_catn(g, s, 1); first_byte = FALSE; }
+ }
+
+if (coded)
+ string = string_from_gstring(g = string_catn(g, US"?=", 2));
+else
+ g->ptr = -1;
+
+gstring_release_unused(g);
+return 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 allocated memory.
+
+Arguments:
+ phrase an RFC822 phrase
+ len the length of the phrase
+
+Returns: the fixed RFC822 phrase
+*/
+
+const uschar *
+parse_fix_phrase(const uschar *phrase, int len)
+{
+int ch, i;
+BOOL quoted = FALSE;
+const uschar *s, *end;
+uschar * buffer;
+uschar *t, *yield;
+
+while (len > 0 && isspace(*phrase)) { phrase++; len--; }
+
+/* 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, FALSE);
+
+/* No non-printers; use the RFC 822 quoting rules */
+
+if (len <= 0 || len >= INT_MAX/4)
+ return string_copy_taint(CUS"", phrase);
+
+buffer = store_get((len+1)*4, phrase);
+
+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 == '(')
+ {
+ const 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++ = '(';
+ if (ss > s)
+ {
+ 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;
+store_release_above(t+1);
+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(const uschar *s, int options, address_item **anchor,
+ uschar **error, const uschar *incoming_domain, const uschar *directory,
+ error_block **syntax_errors)
+{
+int count = 0;
+
+DEBUG(D_route) debug_printf("parse_forward_list: %s\n", s);
+
+for (;;)
+ {
+ int len, special = 0, specopt = 0, specbit = 0;
+ const uschar * ss, * nexts;
+ address_item * addr;
+ BOOL inquote = FALSE;
+
+ for (;;)
+ {
+ while (isspace(*s) || *s == ',') s++;
+ if (*s == '#') { while (*s && *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)
+ {
+ return (count > 0 || (syntax_errors && *syntax_errors))
+ ? 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 || /* Not skipping syntax errors, or */
+ !*syntax_errors) /* 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) debug_printf("extract item: %.*s\n", len, s);
+
+ /* 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)
+ {
+ uschar * ss = Ustrchr(s+1, ':') + 1; /* line after the special... */
+ if ((options & specopt) == specbit)
+ {
+ *error = string_sprintf("\"%.*s\" is not permitted", len, s);
+ return FF_ERROR;
+ }
+ while (*ss && isspace(*ss)) ss++; /* skip leading whitespace */
+ if ((len = Ustrlen(ss)) > 0) /* ignore trailing newlines */
+ for (const uschar * t = ss + len - 1; t >= ss && *t == '\n'; t--) len--;
+ *error = string_copyn(ss, len); /* becomes the error */
+ 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];
+ const 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 = US"file name missing after :include:";
+ return FF_ERROR;
+ }
+
+ if (flen > sizeof(filename)-1)
+ {
+ *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)
+ {
+ *error = US"included files not permitted";
+ return FF_ERROR;
+ }
+
+ if (is_tainted(filename))
+ {
+ *error = string_sprintf("Tainted name '%s' for included file not permitted\n",
+ filename);
+ return FF_ERROR;
+ }
+
+ /* Check file name if required */
+
+ if (directory)
+ {
+ int len = Ustrlen(directory);
+ uschar * p;
+
+ while (len > 0 && directory[len-1] == '/') len--; /* ignore trailing '/' */
+ 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;
+ }
+
+#ifdef EXIM_HAVE_OPENAT
+ /* 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. We open the directory and openat every component,
+ with a flag that fails symlinks. */
+
+ {
+ int fd = exim_open2(CCS directory, O_RDONLY);
+ if (fd < 0)
+ {
+ *error = string_sprintf("failed to open directory %s", directory);
+ return FF_ERROR;
+ }
+ while (*p)
+ {
+ uschar temp;
+ int fd2;
+ uschar * q = p + 1; /* skip dividing '/' */
+
+ while (*q == '/') q++; /* skip extra '/' */
+ while (*++p && *p != '/') ; /* end of component */
+ temp = *p;
+ *p = '\0';
+
+ fd2 = exim_openat(fd, CS q, O_RDONLY|O_NOFOLLOW);
+ close(fd);
+ *p = temp;
+ if (fd2 < 0)
+ {
+ *error = string_sprintf("failed to open %s (component of included "
+ "file); could be symbolic link", filename);
+ return FF_ERROR;
+ }
+ fd = fd2;
+ }
+ f = fdopen(fd, "rb");
+ }
+#else
+ /* 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)
+ {
+ int temp;
+ while (*++p && *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;
+ }
+ }
+#endif
+ }
+
+#ifdef EXIM_HAVE_OPENAT
+ else
+#endif
+ /* Open and stat the file */
+ f = Ufopen(filename, "rb");
+
+ if (!f)
+ {
+ *error = string_open_failed("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 && (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, filename);
+ 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)
+ {
+ for (last = addr; last->next; last = last->next) count++;
+ 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;
+ const uschar *recipient = NULL;
+ uschar * s_ltd = string_copyn(s, len);
+
+ /* 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_ltd == '\\')
+ {
+ recipient =
+ parse_extract_address(s_ltd+1, error, &start, &end, &domain, FALSE);
+ if (recipient)
+ recipient = domain != 0 ? NULL :
+ string_sprintf("%s@%s", recipient, incoming_domain);
+ }
+
+ /* Try parsing the item as an address. */
+
+ if (!recipient) recipient =
+ parse_extract_address(s_ltd, 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_ltd == '|' || *s_ltd == '/') && (!recipient || domain == 0))
+ {
+ uschar * t = store_get(Ustrlen(s_ltd) + 1, s_ltd);
+ uschar * p = t, * q = s_ltd;
+
+ while (*q)
+ {
+ 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_ltd != '|') 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)
+ {
+ if (Ustrcmp(*error, "empty address") == 0)
+ {
+ *error = NULL;
+ s = nexts;
+ continue;
+ }
+
+ if (syntax_errors)
+ {
+ error_block * e = store_get(sizeof(error_block), GET_UNTAINTED);
+ error_block * last = *syntax_errors;
+ if (last)
+ {
+ while (last->next) last = last->next;
+ last->next = e;
+ }
+ else
+ *syntax_errors = e;
+ e->next = NULL;
+ e->text1 = *error;
+ e->text2 = s_ltd;
+ s = nexts;
+ continue;
+ }
+ else
+ {
+ *error = string_sprintf("%s in \"%s\"", *error, s_ltd);
+ return FF_ERROR;
+ }
+ }
+
+ /* Address was successfully parsed. Rewrite, and then make an address
+ block. */
+
+ recipient = options & RDO_REWRITE
+ ? rewrite_address(recipient, TRUE, FALSE, global_rewrite_rules,
+ rewrite_existflags)
+ : rewrite_address_qualify(recipient, TRUE); /*XXX loses track of const */
+ addr = deliver_make_addr(US recipient, TRUE); /* TRUE => copy recipient, so deconst ok */
+ }
+
+ /* Add the original data to the output chain. */
+
+ 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
+*/
+
+const uschar *
+parse_message_id(const uschar *str, uschar **yield, uschar **error)
+{
+uschar *domain = NULL;
+uschar *id;
+rmark reset_point;
+
+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. */
+
+reset_point = store_mark();
+id = *yield = store_get(Ustrlen(str) + 1, str);
+*id++ = *str++;
+
+str = read_addr_spec(str, id, '>', error, &domain);
+
+if (!*error)
+ {
+ if (*str != '>') *error = US"Missing '>' after message-id";
+ else if (domain == NULL) *error = US"domain missing in message-id";
+ }
+
+if (*error)
+ {
+ store_reset(reset_point);
+ return NULL;
+ }
+
+while (*id) id++;
+*id++ = *str++;
+*id++ = 0;
+store_release_above(id);
+
+return skip_comment(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 const uschar *
+parse_number(const 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 const uschar *
+parse_day_of_week(const 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;
+return skip_comment(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 const uschar *
+parse_date(const 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]
+*/
+
+const uschar * s, * 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');
+s = skip_comment(str);
+if (s == str) return NULL;
+str = s;
+
+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;
+s = skip_comment(str);
+if (s == str) return NULL;
+str=s;
+
+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 const uschar *
+parse_time(const 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
+*/
+
+const 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
+*/
+
+const uschar *
+parse_date_time(const 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 };
+const 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;
+return skip_comment(str);
+}
+
+
+
+
+/*************************************************
+**************************************************
+* Stand-alone test program *
+**************************************************
+*************************************************/
+
+#if defined STAND_ALONE
+int main(void)
+{
+int start, end, domain;
+uschar buffer[1024];
+
+store_init();
+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)));
+ }
+
+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)
+ 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)
+ 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");
+
+f.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)
+ {
+ 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)
+ 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);
+ Uskip_whitespace(&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
index 0000000..47f92ee
--- /dev/null
+++ b/src/pdkim/Makefile
@@ -0,0 +1,19 @@
+# Make file for building the pdkim library.
+# Copyright (c) The Exim Maintainers 1995 - 2018
+
+OBJ = pdkim.o signing.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) -I. $*.c
+
+pdkim.o: $(HDRS) crypt_ver.h pdkim.h pdkim.c
+signing.o: $(HDRS) crypt_ver.h signing.h signing.c
+
+# End
diff --git a/src/pdkim/README b/src/pdkim/README
new file mode 100644
index 0000000..953e86e
--- /dev/null
+++ b/src/pdkim/README
@@ -0,0 +1,9 @@
+PDKIM - a RFC4871 (DKIM) implementation
+http://duncanthrax.net/pdkim/
+Copyright (C) 2009 Tom Kistner <tom@duncanthrax.net>
+
+No longer includes code from the PolarSSL project.
+Copyright (C) 2016 Jeremy Harris <jgh@exim.org>
+
+This copy of PDKIM is included with Exim. For a standalone distribution,
+visit http://duncanthrax.net/pdkim/.
diff --git a/src/pdkim/config.h b/src/pdkim/config.h
new file mode 100644
index 0000000..fdd4cfe
--- /dev/null
+++ b/src/pdkim/config.h
@@ -0,0 +1,4 @@
+#define POLARSSL_BASE64_C
+
+
+
diff --git a/src/pdkim/crypt_ver.h b/src/pdkim/crypt_ver.h
new file mode 100644
index 0000000..a6d7e36
--- /dev/null
+++ b/src/pdkim/crypt_ver.h
@@ -0,0 +1,33 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Jeremy Harris 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Signing and hashing routine selection for PDKIM */
+
+#include "../exim.h"
+#include "../sha_ver.h"
+
+
+#ifdef USE_GNUTLS
+# include <gnutls/gnutls.h>
+
+# if GNUTLS_VERSION_NUMBER >= 0x030000
+# define SIGN_GNUTLS
+# if GNUTLS_VERSION_NUMBER >= 0x030600
+# define SIGN_HAVE_ED25519
+# endif
+# else
+# define SIGN_GCRYPT
+# endif
+#endif
+
+#ifdef USE_OPENSSL
+# define SIGN_OPENSSL
+# if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10101000L
+# define SIGN_HAVE_ED25519
+# endif
+#endif
+
diff --git a/src/pdkim/pdkim.c b/src/pdkim/pdkim.c
new file mode 100644
index 0000000..e47bfc5
--- /dev/null
+++ b/src/pdkim/pdkim.c
@@ -0,0 +1,2097 @@
+/*
+ * PDKIM - a RFC4871 (DKIM) implementation
+ *
+ * Copyright (c) The Exim Maintainers 2021 - 2022
+ * Copyright (C) 2009 - 2016 Tom Kistner <tom@duncanthrax.net>
+ * Copyright (C) 2016 - 2020 Jeremy Harris <jgh@exim.org>
+ *
+ * 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 "../exim.h"
+
+
+#ifndef DISABLE_DKIM /* entire file */
+
+#ifdef DISABLE_TLS
+# error Must not DISABLE_TLS, for DKIM
+#endif
+
+#include "crypt_ver.h"
+
+#ifdef SIGN_OPENSSL
+# include <openssl/rsa.h>
+# include <openssl/ssl.h>
+# include <openssl/err.h>
+#elif defined(SIGN_GNUTLS)
+# include <gnutls/gnutls.h>
+# include <gnutls/x509.h>
+#endif
+
+#include "pdkim.h"
+#include "signing.h"
+
+#define PDKIM_SIGNATURE_VERSION "1"
+#define PDKIM_PUB_RECORD_VERSION US "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
+
+/* -------------------------------------------------------------------------- */
+struct pdkim_stringlist {
+ uschar * value;
+ int tag;
+ void * next;
+};
+
+/* -------------------------------------------------------------------------- */
+/* A bunch of list constants */
+const uschar * pdkim_querymethods[] = {
+ US"dns/txt",
+ NULL
+};
+const uschar * pdkim_canons[] = {
+ US"simple",
+ US"relaxed",
+ NULL
+};
+
+const pdkim_hashtype pdkim_hashes[] = {
+ { US"sha1", HASH_SHA1 },
+ { US"sha256", HASH_SHA2_256 },
+ { US"sha512", HASH_SHA2_512 }
+};
+
+const uschar * pdkim_keytypes[] = {
+ [KEYTYPE_RSA] = US"rsa",
+#ifdef SIGN_HAVE_ED25519
+ [KEYTYPE_ED25519] = US"ed25519", /* Works for 3.6.0 GnuTLS, OpenSSL 1.1.1 */
+#endif
+
+#ifdef notyet_EC_dkim_extensions /* https://tools.ietf.org/html/draft-srose-dkim-ecc-00 */
+ US"eccp256",
+ US"eccp348",
+ US"ed448",
+#endif
+};
+
+typedef struct pdkim_combined_canon_entry {
+ const uschar * str;
+ int canon_headers;
+ int canon_body;
+} pdkim_combined_canon_entry;
+
+pdkim_combined_canon_entry pdkim_combined_canons[] = {
+ { US"simple/simple", PDKIM_CANON_SIMPLE, PDKIM_CANON_SIMPLE },
+ { US"simple/relaxed", PDKIM_CANON_SIMPLE, PDKIM_CANON_RELAXED },
+ { US"relaxed/simple", PDKIM_CANON_RELAXED, PDKIM_CANON_SIMPLE },
+ { US"relaxed/relaxed", PDKIM_CANON_RELAXED, PDKIM_CANON_RELAXED },
+ { US"simple", PDKIM_CANON_SIMPLE, PDKIM_CANON_SIMPLE },
+ { US"relaxed", PDKIM_CANON_RELAXED, PDKIM_CANON_SIMPLE },
+ { NULL, 0, 0 }
+};
+
+
+static const blob lineending = {.data = US"\r\n", .len = 2};
+
+/* -------------------------------------------------------------------------- */
+uschar *
+dkim_sig_to_a_tag(const pdkim_signature * sig)
+{
+if ( sig->keytype < 0 || sig->keytype > nelem(pdkim_keytypes)
+ || sig->hashtype < 0 || sig->hashtype > nelem(pdkim_hashes))
+ return US"err";
+return string_sprintf("%s-%s",
+ pdkim_keytypes[sig->keytype], pdkim_hashes[sig->hashtype].dkim_hashname);
+}
+
+
+static int
+pdkim_keyname_to_keytype(const uschar * s)
+{
+for (int i = 0; i < nelem(pdkim_keytypes); i++)
+ if (Ustrcmp(s, pdkim_keytypes[i]) == 0) return i;
+return -1;
+}
+
+int
+pdkim_hashname_to_hashtype(const uschar * s, unsigned len)
+{
+if (!len) len = Ustrlen(s);
+for (int i = 0; i < nelem(pdkim_hashes); i++)
+ if (Ustrncmp(s, pdkim_hashes[i].dkim_hashname, len) == 0)
+ return i;
+return -1;
+}
+
+void
+pdkim_cstring_to_canons(const uschar * s, unsigned len,
+ int * canon_head, int * canon_body)
+{
+if (!len) len = Ustrlen(s);
+for (int i = 0; pdkim_combined_canons[i].str; i++)
+ if ( Ustrncmp(s, pdkim_combined_canons[i].str, len) == 0
+ && len == Ustrlen(pdkim_combined_canons[i].str))
+ {
+ *canon_head = pdkim_combined_canons[i].canon_headers;
+ *canon_body = pdkim_combined_canons[i].canon_body;
+ break;
+ }
+}
+
+
+
+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_FAIL_SIG_ALGO_MISMATCH: return "PDKIM_VERIFY_FAIL_SIG_ALGO_MISMATCH";
+ 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_DNSRECORD: return "PDKIM_VERIFY_INVALID_PUBKEY_DNSRECORD";
+ case PDKIM_VERIFY_INVALID_PUBKEY_IMPORT: return "PDKIM_VERIFY_INVALID_PUBKEY_IMPORT";
+ case PDKIM_VERIFY_INVALID_PUBKEY_KEYSIZE: return "PDKIM_VERIFY_INVALID_PUBKEY_KEYSIZE";
+ case PDKIM_VERIFY_INVALID_SIGNATURE_ERROR: return "PDKIM_VERIFY_INVALID_SIGNATURE_ERROR";
+ case PDKIM_VERIFY_INVALID_DKIM_VERSION: return "PDKIM_VERIFY_INVALID_DKIM_VERSION";
+ default: return "PDKIM_VERIFY_UNKNOWN";
+ }
+}
+
+const uschar *
+pdkim_errstr(int status)
+{
+switch(status)
+ {
+ case PDKIM_OK: return US"OK";
+ case PDKIM_FAIL: return US"FAIL";
+ case PDKIM_ERR_RSA_PRIVKEY: return US"PRIVKEY";
+ case PDKIM_ERR_RSA_SIGNING: return US"SIGNING";
+ case PDKIM_ERR_LONG_LINE: return US"LONG_LINE";
+ case PDKIM_ERR_BUFFER_TOO_SMALL: return US"BUFFER_TOO_SMALL";
+ case PDKIM_ERR_EXCESS_SIGS: return US"EXCESS_SIGS";
+ case PDKIM_SIGN_PRIVKEY_WRAP: return US"PRIVKEY_WRAP";
+ case PDKIM_SIGN_PRIVKEY_B64D: return US"PRIVKEY_B64D";
+ default: return US"(unknown)";
+ }
+}
+
+
+/* -------------------------------------------------------------------------- */
+/* Print debugging functions */
+void
+pdkim_quoteprint(const uschar *data, int len)
+{
+for (int i = 0; i < len; i++)
+ {
+ const int c = data[i];
+ switch (c)
+ {
+ case ' ' : debug_printf("{SP}"); break;
+ case '\t': debug_printf("{TB}"); break;
+ case '\r': debug_printf("{CR}"); break;
+ case '\n': debug_printf("{LF}"); break;
+ case '{' : debug_printf("{BO}"); break;
+ case '}' : debug_printf("{BC}"); break;
+ default:
+ if ( (c < 32) || (c > 127) )
+ debug_printf("{%02x}", c);
+ else
+ debug_printf("%c", c);
+ break;
+ }
+ }
+debug_printf("\n");
+}
+
+void
+pdkim_hexprint(const uschar *data, int len)
+{
+if (data) for (int i = 0 ; i < len; i++) debug_printf("%02x", data[i]);
+else debug_printf("<NULL>");
+debug_printf("\n");
+}
+
+
+
+static pdkim_stringlist *
+pdkim_prepend_stringlist(pdkim_stringlist * base, const uschar * str)
+{
+pdkim_stringlist * new_entry = store_get(sizeof(pdkim_stringlist), GET_UNTAINTED);
+
+memset(new_entry, 0, sizeof(pdkim_stringlist));
+new_entry->value = string_copy(str);
+if (base) new_entry->next = base;
+return new_entry;
+}
+
+
+
+/* Trim whitespace fore & aft */
+
+static void
+pdkim_strtrim(gstring * str)
+{
+uschar * p = str->s;
+uschar * q;
+
+while (*p == '\t' || *p == ' ') /* dump the leading whitespace */
+ { str->size--; str->ptr--; str->s++; }
+
+while ( str->ptr > 0
+ && ((q = str->s + str->ptr - 1), (*q == '\t' || *q == ' '))
+ )
+ str->ptr--; /* dump trailing whitespace */
+
+(void) string_from_gstring(str);
+}
+
+
+
+/* -------------------------------------------------------------------------- */
+
+DLLEXPORT void
+pdkim_free_ctx(pdkim_ctx *ctx)
+{
+}
+
+
+/* -------------------------------------------------------------------------- */
+/* Matches the name of the passed raw "header" against
+ the passed colon-separated "tick", and invalidates
+ the entry in tick. Entries can be prefixed for multi- or over-signing,
+ in which case do not invalidate.
+
+ Returns OK for a match, or fail-code
+*/
+
+static int
+header_name_match(const uschar * header, uschar * tick)
+{
+const uschar * ticklist = tick;
+int sep = ':';
+BOOL multisign;
+uschar * hname, * p, * ele;
+uschar * hcolon = Ustrchr(header, ':'); /* Get header name */
+
+if (!hcolon)
+ return PDKIM_FAIL; /* This isn't a header */
+
+/* if we had strncmpic() we wouldn't need this copy */
+hname = string_copyn(header, hcolon-header);
+
+while (p = US ticklist, ele = string_nextinlist(&ticklist, &sep, NULL, 0))
+ {
+ switch (*ele)
+ {
+ case '=': case '+': multisign = TRUE; ele++; break;
+ default: multisign = FALSE; break;
+ }
+
+ if (strcmpic(ele, hname) == 0)
+ {
+ if (!multisign)
+ *p = '_'; /* Invalidate this header name instance in tick-off list */
+ return PDKIM_OK;
+ }
+ }
+return PDKIM_FAIL;
+}
+
+
+/* -------------------------------------------------------------------------- */
+/* Performs "relaxed" canonicalization of a header. */
+
+uschar *
+pdkim_relax_header_n(const uschar * header, int len, BOOL append_crlf)
+{
+BOOL past_field_name = FALSE;
+BOOL seen_wsp = FALSE;
+uschar * relaxed = store_get(len+3, GET_TAINTED);
+uschar * q = relaxed;
+
+for (const uschar * p = header; p - header < len; p++)
+ {
+ uschar c = *p;
+
+ if (c == '\r' || c == '\n') /* Ignore CR & LF */
+ continue;
+ if (c == '\t' || c == ' ')
+ {
+ if (seen_wsp)
+ continue;
+ c = ' '; /* Turns WSP into SP */
+ seen_wsp = TRUE;
+ }
+ else
+ if (!past_field_name && c == ':')
+ {
+ if (seen_wsp) q--; /* This removes WSP immediately before the colon */
+ seen_wsp = TRUE; /* This removes WSP immediately after the colon */
+ past_field_name = TRUE;
+ }
+ else
+ seen_wsp = FALSE;
+
+ /* Lowercase header name */
+ if (!past_field_name) c = tolower(c);
+ *q++ = c;
+ }
+
+if (q > relaxed && q[-1] == ' ') q--; /* Squash eventual trailing SP */
+
+if (append_crlf) { *q++ = '\r'; *q++ = '\n'; }
+*q = '\0';
+return relaxed;
+}
+
+
+uschar *
+pdkim_relax_header(const uschar * header, BOOL append_crlf)
+{
+return pdkim_relax_header_n(header, Ustrlen(header), append_crlf);
+}
+
+
+/* -------------------------------------------------------------------------- */
+#define PDKIM_QP_ERROR_DECODE -1
+
+static const uschar *
+pdkim_decode_qp_char(const uschar *qp_p, int *c)
+{
+const 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;
+ *c |= isdigit(qp_p[1]) ? qp_p[1] - '0' : toupper(qp_p[1]) - 'A' + 10;
+ return qp_p + 2;
+ }
+
+/* Illegal char here */
+*c = PDKIM_QP_ERROR_DECODE;
+return initial_pos;
+}
+
+
+/* -------------------------------------------------------------------------- */
+
+static uschar *
+pdkim_decode_qp(const uschar * str)
+{
+int nchar = 0;
+uschar * q;
+const uschar * p = str;
+uschar * n = store_get(Ustrlen(str)+1, GET_TAINTED);
+
+*n = '\0';
+q = n;
+while (*p)
+ {
+ if (*p == '=')
+ {
+ p = pdkim_decode_qp_char(p, &nchar);
+ if (nchar >= 0)
+ {
+ *q++ = nchar;
+ continue;
+ }
+ }
+ else
+ *q++ = *p;
+ p++;
+ }
+*q = '\0';
+return n;
+}
+
+
+/* -------------------------------------------------------------------------- */
+
+void
+pdkim_decode_base64(const uschar * str, blob * b)
+{
+int dlen = b64decode(str, &b->data);
+if (dlen < 0) b->data = NULL;
+b->len = dlen;
+}
+
+uschar *
+pdkim_encode_base64(blob * b)
+{
+return b64encode(CUS b->data, b->len);
+}
+
+
+/* -------------------------------------------------------------------------- */
+#define PDKIM_HDR_LIMBO 0
+#define PDKIM_HDR_TAG 1
+#define PDKIM_HDR_VALUE 2
+
+static pdkim_signature *
+pdkim_parse_sig_header(pdkim_ctx * ctx, uschar * raw_hdr)
+{
+pdkim_signature * sig;
+uschar *q;
+gstring * cur_tag = NULL;
+gstring * cur_val = NULL;
+BOOL past_hname = FALSE;
+BOOL in_b_val = FALSE;
+int where = PDKIM_HDR_LIMBO;
+
+sig = store_get(sizeof(pdkim_signature), GET_UNTAINTED);
+memset(sig, 0, sizeof(pdkim_signature));
+sig->bodylength = -1;
+
+/* Set so invalid/missing data error display is accurate */
+sig->version = 0;
+sig->keytype = -1;
+sig->hashtype = -1;
+
+q = sig->rawsig_no_b_val = store_get(Ustrlen(raw_hdr)+1, GET_TAINTED);
+
+for (uschar * p = raw_hdr; ; p++)
+ {
+ char c = *p;
+
+ /* Ignore FWS */
+ if (c == '\r' || c == '\n')
+ goto NEXT_CHAR;
+
+ /* Fast-forward through header name */
+ if (!past_hname)
+ {
+ if (c == ':') past_hname = TRUE;
+ goto NEXT_CHAR;
+ }
+
+ if (where == PDKIM_HDR_LIMBO)
+ {
+ /* In limbo, just wait for a tag-char to appear */
+ if (!(c >= 'a' && c <= 'z'))
+ goto NEXT_CHAR;
+
+ where = PDKIM_HDR_TAG;
+ }
+
+ if (where == PDKIM_HDR_TAG)
+ {
+ if (c >= 'a' && c <= 'z')
+ cur_tag = string_catn(cur_tag, p, 1);
+
+ if (c == '=')
+ {
+ if (Ustrcmp(string_from_gstring(cur_tag), "b") == 0)
+ {
+ *q++ = '=';
+ in_b_val = TRUE;
+ }
+ where = PDKIM_HDR_VALUE;
+ goto NEXT_CHAR;
+ }
+ }
+
+ if (where == PDKIM_HDR_VALUE)
+ {
+ if (c == '\r' || c == '\n' || c == ' ' || c == '\t')
+ goto NEXT_CHAR;
+
+ if (c == ';' || c == '\0')
+ {
+ /* We must have both tag and value, and tags must be one char except
+ for the possibility of "bh". */
+
+ if ( cur_tag && cur_val
+ && (cur_tag->ptr == 1 || *cur_tag->s == 'b')
+ )
+ {
+ (void) string_from_gstring(cur_val);
+ pdkim_strtrim(cur_val);
+
+ DEBUG(D_acl) debug_printf(" %s=%s\n", cur_tag->s, cur_val->s);
+
+ switch (*cur_tag->s)
+ {
+ case 'b': /* sig-data or body-hash */
+ switch (cur_tag->s[1])
+ {
+ case '\0': pdkim_decode_base64(cur_val->s, &sig->sighash); break;
+ case 'h': if (cur_tag->ptr == 2)
+ pdkim_decode_base64(cur_val->s, &sig->bodyhash);
+ break;
+ default: break;
+ }
+ break;
+ case 'v': /* version */
+ /* We only support version 1, and that is currently the
+ only version there is. */
+ sig->version =
+ Ustrcmp(cur_val->s, PDKIM_SIGNATURE_VERSION) == 0 ? 1 : -1;
+ break;
+ case 'a': /* algorithm */
+ {
+ const uschar * list = cur_val->s;
+ int sep = '-';
+ uschar * elem;
+
+ if ((elem = string_nextinlist(&list, &sep, NULL, 0)))
+ sig->keytype = pdkim_keyname_to_keytype(elem);
+ if ((elem = string_nextinlist(&list, &sep, NULL, 0)))
+ for (int i = 0; i < nelem(pdkim_hashes); i++)
+ if (Ustrcmp(elem, pdkim_hashes[i].dkim_hashname) == 0)
+ { sig->hashtype = i; break; }
+ }
+
+ case 'c': /* canonicalization */
+ pdkim_cstring_to_canons(cur_val->s, 0,
+ &sig->canon_headers, &sig->canon_body);
+ break;
+ case 'q': /* Query method (for pubkey)*/
+ for (int i = 0; pdkim_querymethods[i]; i++)
+ if (Ustrcmp(cur_val->s, pdkim_querymethods[i]) == 0)
+ {
+ sig->querymethod = i; /* we never actually use this */
+ break;
+ }
+ break;
+ case 's': /* Selector */
+ sig->selector = string_copyn(cur_val->s, cur_val->ptr); break;
+ case 'd': /* SDID */
+ sig->domain = string_copyn(cur_val->s, cur_val->ptr); break;
+ case 'i': /* AUID */
+ sig->identity = pdkim_decode_qp(cur_val->s); break;
+ case 't': /* Timestamp */
+ sig->created = strtoul(CS cur_val->s, NULL, 10); break;
+ case 'x': /* Expiration */
+ sig->expires = strtoul(CS cur_val->s, NULL, 10); break;
+ case 'l': /* Body length count */
+ sig->bodylength = strtol(CS cur_val->s, NULL, 10); break;
+ case 'h': /* signed header fields */
+ sig->headernames = string_copyn(cur_val->s, cur_val->ptr); break;
+ case 'z': /* Copied headfields */
+ sig->copiedheaders = pdkim_decode_qp(cur_val->s); break;
+/*XXX draft-ietf-dcrup-dkim-crypto-05 would need 'p' tag support
+for rsafp signatures. But later discussion is dropping those. */
+ default:
+ DEBUG(D_acl) debug_printf(" Unknown tag encountered\n");
+ break;
+ }
+ }
+ cur_tag = cur_val = NULL;
+ in_b_val = FALSE;
+ where = PDKIM_HDR_LIMBO;
+ }
+ else
+ cur_val = string_catn(cur_val, p, 1);
+ }
+
+NEXT_CHAR:
+ if (c == '\0')
+ break;
+
+ if (!in_b_val)
+ *q++ = c;
+ }
+
+if (sig->keytype < 0 || sig->hashtype < 0) /* Cannot verify this signature */
+ return NULL;
+
+*q = '\0';
+/* Chomp raw header. The final newline must not be added to the signature. */
+while (--q > sig->rawsig_no_b_val && (*q == '\r' || *q == '\n'))
+ *q = '\0';
+
+DEBUG(D_acl)
+ {
+ debug_printf(
+ "DKIM >> Raw signature w/o b= tag value >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+ pdkim_quoteprint(US sig->rawsig_no_b_val, Ustrlen(sig->rawsig_no_b_val));
+ debug_printf(
+ "DKIM >> Sig size: %4u bits\n", (unsigned) sig->sighash.len*8);
+ debug_printf(
+ "DKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+ }
+
+if (!pdkim_set_sig_bodyhash(ctx, sig))
+ return NULL;
+
+return sig;
+}
+
+
+/* -------------------------------------------------------------------------- */
+
+pdkim_pubkey *
+pdkim_parse_pubkey_record(const uschar *raw_record)
+{
+const uschar * ele;
+int sep = ';';
+pdkim_pubkey * pub;
+
+pub = store_get(sizeof(pdkim_pubkey), GET_TAINTED);
+memset(pub, 0, sizeof(pdkim_pubkey));
+
+while ((ele = string_nextinlist(&raw_record, &sep, NULL, 0)))
+ {
+ const uschar * val;
+
+ if ((val = Ustrchr(ele, '=')))
+ {
+ int taglen = val++ - ele;
+
+ DEBUG(D_acl) debug_printf(" %.*s=%s\n", taglen, ele, val);
+ switch (ele[0])
+ {
+ case 'v': pub->version = val; break;
+ case 'h': pub->hashes = val; break;
+ case 'k': pub->keytype = val; break;
+ case 'g': pub->granularity = val; break;
+ case 'n': pub->notes = pdkim_decode_qp(val); break;
+ case 'p': pdkim_decode_base64(val, &pub->key); break;
+ case 's': pub->srvtype = val; break;
+ case 't': if (Ustrchr(val, 'y')) pub->testing = 1;
+ if (Ustrchr(val, 's')) pub->no_subdomaining = 1;
+ break;
+ default: DEBUG(D_acl) debug_printf(" Unknown tag encountered\n"); break;
+ }
+ }
+ }
+
+/* Set fallback defaults */
+if (!pub->version)
+ pub->version = string_copy(PDKIM_PUB_RECORD_VERSION);
+else if (Ustrcmp(pub->version, PDKIM_PUB_RECORD_VERSION) != 0)
+ {
+ DEBUG(D_acl) debug_printf(" Bad v= field\n");
+ return NULL;
+ }
+
+if (!pub->granularity) pub->granularity = US"*";
+if (!pub->keytype ) pub->keytype = US"rsa";
+if (!pub->srvtype ) pub->srvtype = US"*";
+
+/* p= is required */
+if (pub->key.data)
+ return pub;
+
+DEBUG(D_acl) debug_printf(" Missing p= field\n");
+return NULL;
+}
+
+
+/* -------------------------------------------------------------------------- */
+
+/* Update one bodyhash with some additional data.
+If we have to relax the data for this sig, return our copy of it. */
+
+static blob *
+pdkim_update_ctx_bodyhash(pdkim_bodyhash * b, const blob * orig_data, blob * relaxed_data)
+{
+const blob * canon_data = orig_data;
+size_t left;
+
+/* Defaults to simple canon (no further treatment necessary) */
+
+if (b->canon_method == PDKIM_CANON_RELAXED)
+ {
+ /* Relax the line if not done already */
+ if (!relaxed_data)
+ {
+ BOOL seen_wsp = FALSE;
+ int q = 0;
+
+ /* We want to be able to free this else we allocate
+ for the entire message which could be many MB. Since
+ we don't know what allocations the SHA routines might
+ do, not safe to use store_get()/store_reset(). */
+
+ relaxed_data = store_malloc(sizeof(blob) + orig_data->len+1);
+ relaxed_data->data = US (relaxed_data+1);
+
+ for (const uschar * p = orig_data->data, * r = p + orig_data->len; p < r; p++)
+ {
+ char c = *p;
+ if (c == '\r')
+ {
+ if (q > 0 && relaxed_data->data[q-1] == ' ')
+ q--;
+ }
+ else if (c == '\t' || c == ' ')
+ {
+ c = ' '; /* Turns WSP into SP */
+ if (seen_wsp)
+ continue;
+ seen_wsp = TRUE;
+ }
+ else
+ seen_wsp = FALSE;
+ relaxed_data->data[q++] = c;
+ }
+ relaxed_data->data[q] = '\0';
+ relaxed_data->len = q;
+ }
+ canon_data = relaxed_data;
+ }
+
+/* Make sure we don't exceed the to-be-signed body length */
+left = canon_data->len;
+if ( b->bodylength >= 0
+ && left > (unsigned long)b->bodylength - b->signed_body_bytes
+ )
+ left = (unsigned long)b->bodylength - b->signed_body_bytes;
+
+if (left > 0)
+ {
+ exim_sha_update(&b->body_hash_ctx, CUS canon_data->data, left);
+ b->signed_body_bytes += left;
+ DEBUG(D_acl) pdkim_quoteprint(canon_data->data, left);
+ }
+
+return relaxed_data;
+}
+
+
+/* -------------------------------------------------------------------------- */
+
+static void
+pdkim_finish_bodyhash(pdkim_ctx * ctx)
+{
+for (pdkim_bodyhash * b = ctx->bodyhash; b; b = b->next) /* Finish hashes */
+ {
+ DEBUG(D_acl) debug_printf("DKIM: finish bodyhash %s/%s/%ld len %ld\n",
+ pdkim_hashes[b->hashtype].dkim_hashname, pdkim_canons[b->canon_method],
+ b->bodylength, b->signed_body_bytes);
+ exim_sha_finish(&b->body_hash_ctx, &b->bh);
+ }
+
+/* Traverse all signatures */
+for (pdkim_signature * sig = ctx->sig; sig; sig = sig->next)
+ {
+ pdkim_bodyhash * b = sig->calc_body_hash;
+
+ DEBUG(D_acl)
+ {
+ debug_printf("DKIM [%s]%s Body bytes (%s) hashed: %lu\n"
+ "DKIM [%s]%s Body %s computed: ",
+ sig->domain, sig->selector, pdkim_canons[b->canon_method], b->signed_body_bytes,
+ sig->domain, sig->selector, pdkim_hashes[b->hashtype].dkim_hashname);
+ pdkim_hexprint(CUS b->bh.data, b->bh.len);
+ }
+
+ /* SIGNING -------------------------------------------------------------- */
+ if (ctx->flags & PDKIM_MODE_SIGN)
+ {
+ /* If bodylength limit is set, and we have received less bytes
+ than the requested amount, effectively remove the limit tag. */
+ if (b->signed_body_bytes < sig->bodylength)
+ sig->bodylength = -1;
+ }
+
+ else
+ /* VERIFICATION --------------------------------------------------------- */
+ /* Be careful that the header sig included a bodyash */
+
+ if (sig->bodyhash.data && sig->bodyhash.len == b->bh.len
+ && memcmp(b->bh.data, sig->bodyhash.data, b->bh.len) == 0)
+ {
+ DEBUG(D_acl) debug_printf("DKIM [%s] Body hash compared OK\n", sig->domain);
+ }
+ else
+ {
+ DEBUG(D_acl)
+ {
+ debug_printf("DKIM [%s] Body hash signature from headers: ", sig->domain);
+ pdkim_hexprint(sig->bodyhash.data, sig->bodyhash.len);
+ debug_printf("DKIM [%s] Body hash did NOT verify\n", sig->domain);
+ }
+ sig->verify_status = PDKIM_VERIFY_FAIL;
+ sig->verify_ext_status = PDKIM_VERIFY_FAIL_BODY;
+ }
+ }
+}
+
+
+
+static void
+pdkim_body_complete(pdkim_ctx * ctx)
+{
+/* In simple body mode, if any empty lines were buffered,
+replace with one. rfc 4871 3.4.3 */
+/*XXX checking the signed-body-bytes is a gross hack; I think
+it indicates that all linebreaks should be buffered, including
+the one terminating a text line */
+
+for (pdkim_bodyhash * b = ctx->bodyhash; b; b = b->next)
+ if ( b->canon_method == PDKIM_CANON_SIMPLE
+ && b->signed_body_bytes == 0
+ && b->num_buffered_blanklines > 0
+ )
+ (void) pdkim_update_ctx_bodyhash(b, &lineending, NULL);
+
+ctx->flags |= PDKIM_SEEN_EOD;
+ctx->linebuf_offset = 0;
+}
+
+
+
+/* -------------------------------------------------------------------------- */
+/* Call from pdkim_feed below for processing complete body lines */
+/* NOTE: the line is not NUL-terminated; but we have a count */
+
+static void
+pdkim_bodyline_complete(pdkim_ctx * ctx)
+{
+blob line = {.data = ctx->linebuf, .len = ctx->linebuf_offset};
+blob * rnl = NULL;
+blob * rline = NULL;
+
+/* Ignore extra data if we've seen the end-of-data marker */
+if (ctx->flags & PDKIM_SEEN_EOD) goto all_skip;
+
+/* We've always got one extra byte to stuff a zero ... */
+ctx->linebuf[line.len] = '\0';
+
+/* Terminate on EOD marker */
+if (ctx->flags & PDKIM_DOT_TERM)
+ {
+ if (memcmp(line.data, ".\r\n", 3) == 0)
+ { pdkim_body_complete(ctx); return; }
+
+ /* Unstuff dots */
+ if (memcmp(line.data, "..", 2) == 0)
+ { line.data++; line.len--; }
+ }
+
+/* Empty lines need to be buffered until we find a non-empty line */
+if (memcmp(line.data, "\r\n", 2) == 0)
+ {
+ for (pdkim_bodyhash * b = ctx->bodyhash; b; b = b->next)
+ b->num_buffered_blanklines++;
+ goto all_skip;
+ }
+
+/* Process line for each bodyhash separately */
+for (pdkim_bodyhash * b = ctx->bodyhash; b; b = b->next)
+ {
+ if (b->canon_method == PDKIM_CANON_RELAXED)
+ {
+ /* Lines with just spaces need to be buffered too */
+ uschar * cp = line.data;
+ char c;
+
+ while ((c = *cp))
+ {
+ if (c == '\r' && cp[1] == '\n') break;
+ if (c != ' ' && c != '\t') goto hash_process;
+ cp++;
+ }
+
+ b->num_buffered_blanklines++;
+ goto hash_skip;
+ }
+
+hash_process:
+ /* At this point, we have a non-empty line, so release the buffered ones. */
+
+ while (b->num_buffered_blanklines)
+ {
+ rnl = pdkim_update_ctx_bodyhash(b, &lineending, rnl);
+ b->num_buffered_blanklines--;
+ }
+
+ rline = pdkim_update_ctx_bodyhash(b, &line, rline);
+hash_skip: ;
+ }
+
+if (rnl) store_free(rnl);
+if (rline) store_free(rline);
+
+all_skip:
+
+ctx->linebuf_offset = 0;
+return;
+}
+
+
+/* -------------------------------------------------------------------------- */
+/* Callback from pdkim_feed below for processing complete headers */
+#define DKIM_SIGNATURE_HEADERNAME "DKIM-Signature:"
+
+static int
+pdkim_header_complete(pdkim_ctx * ctx)
+{
+if ( (ctx->cur_header->ptr > 1) &&
+ (ctx->cur_header->s[ctx->cur_header->ptr-1] == '\r') )
+ --ctx->cur_header->ptr;
+(void) string_from_gstring(ctx->cur_header);
+
+#ifdef EXPERIMENTAL_ARC
+/* Feed the header line to ARC processing */
+(void) arc_header_feed(ctx->cur_header, !(ctx->flags & PDKIM_MODE_SIGN));
+#endif
+
+if (++ctx->num_headers > PDKIM_MAX_HEADERS) goto BAIL;
+
+/* SIGNING -------------------------------------------------------------- */
+if (ctx->flags & PDKIM_MODE_SIGN)
+ for (pdkim_signature * sig = ctx->sig; sig; sig = sig->next) /* Traverse all signatures */
+
+ /* Add header to the signed headers list (in reverse order) */
+ sig->headers = pdkim_prepend_stringlist(sig->headers, ctx->cur_header->s);
+
+/* VERIFICATION ----------------------------------------------------------- */
+/* DKIM-Signature: headers are added to the verification list */
+else
+ {
+#ifdef notdef
+ DEBUG(D_acl)
+ {
+ debug_printf("DKIM >> raw hdr: ");
+ pdkim_quoteprint(CUS ctx->cur_header->s, ctx->cur_header->ptr);
+ }
+#endif
+ if (strncasecmp(CCS ctx->cur_header->s,
+ DKIM_SIGNATURE_HEADERNAME,
+ Ustrlen(DKIM_SIGNATURE_HEADERNAME)) == 0)
+ {
+ pdkim_signature * sig, * last_sig;
+ /* Create and chain new signature block. We could error-check for all
+ required tags here, but prefer to create the internal sig and expicitly
+ fail verification of it later. */
+
+ DEBUG(D_acl) debug_printf(
+ "DKIM >> Found sig, trying to parse >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+
+ sig = pdkim_parse_sig_header(ctx, ctx->cur_header->s);
+
+ if (!(last_sig = ctx->sig))
+ ctx->sig = sig;
+ else
+ {
+ while (last_sig->next) last_sig = last_sig->next;
+ last_sig->next = sig;
+ }
+
+ if (dkim_collect_input && --dkim_collect_input == 0)
+ {
+ ctx->headers = pdkim_prepend_stringlist(ctx->headers, ctx->cur_header->s);
+ ctx->cur_header->s[ctx->cur_header->ptr = 0] = '\0';
+ return PDKIM_ERR_EXCESS_SIGS;
+ }
+ }
+
+ /* all headers are stored for signature verification */
+ ctx->headers = pdkim_prepend_stringlist(ctx->headers, ctx->cur_header->s);
+ }
+
+BAIL:
+ctx->cur_header->s[ctx->cur_header->ptr = 0] = '\0'; /* leave buffer for reuse */
+return PDKIM_OK;
+}
+
+
+
+/* -------------------------------------------------------------------------- */
+#define HEADER_BUFFER_FRAG_SIZE 256
+
+DLLEXPORT int
+pdkim_feed(pdkim_ctx * ctx, uschar * data, int len)
+{
+/* Alternate EOD signal, used in non-dotstuffing mode */
+if (!data)
+ pdkim_body_complete(ctx);
+
+else for (int p = 0; p < len; p++)
+ {
+ uschar c = data[p];
+ int rc;
+
+ if (ctx->flags & PDKIM_PAST_HDRS)
+ {
+ if (c == '\n' && !(ctx->flags & PDKIM_SEEN_CR)) /* emulate the CR */
+ {
+ ctx->linebuf[ctx->linebuf_offset++] = '\r';
+ if (ctx->linebuf_offset == PDKIM_MAX_BODY_LINE_LEN-1)
+ return PDKIM_ERR_LONG_LINE;
+ }
+
+ /* Processing body byte */
+ ctx->linebuf[ctx->linebuf_offset++] = c;
+ if (c == '\r')
+ ctx->flags |= PDKIM_SEEN_CR;
+ else if (c == '\n')
+ {
+ ctx->flags &= ~PDKIM_SEEN_CR;
+ pdkim_bodyline_complete(ctx);
+ }
+
+ if (ctx->linebuf_offset == PDKIM_MAX_BODY_LINE_LEN-1)
+ return PDKIM_ERR_LONG_LINE;
+ }
+ else
+ {
+ /* Processing header byte */
+ if (c == '\r')
+ ctx->flags |= PDKIM_SEEN_CR;
+ else if (c == '\n')
+ {
+ if (!(ctx->flags & PDKIM_SEEN_CR)) /* emulate the CR */
+ ctx->cur_header = string_catn(ctx->cur_header, CUS "\r", 1);
+
+ if (ctx->flags & PDKIM_SEEN_LF) /* Seen last header line */
+ {
+ if ((rc = pdkim_header_complete(ctx)) != PDKIM_OK)
+ return rc;
+
+ ctx->flags = (ctx->flags & ~(PDKIM_SEEN_LF|PDKIM_SEEN_CR)) | PDKIM_PAST_HDRS;
+ DEBUG(D_acl) debug_printf(
+ "DKIM >> Body data for hash, canonicalized >>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+ continue;
+ }
+ else
+ ctx->flags = (ctx->flags & ~PDKIM_SEEN_CR) | PDKIM_SEEN_LF;
+ }
+ else if (ctx->flags & PDKIM_SEEN_LF)
+ {
+ if (!(c == '\t' || c == ' ')) /* End of header */
+ if ((rc = pdkim_header_complete(ctx)) != PDKIM_OK)
+ return rc;
+ ctx->flags &= ~PDKIM_SEEN_LF;
+ }
+
+ if (!ctx->cur_header || ctx->cur_header->ptr < PDKIM_MAX_HEADER_LEN)
+ ctx->cur_header = string_catn(ctx->cur_header, CUS &data[p], 1);
+ }
+ }
+return PDKIM_OK;
+}
+
+
+
+/* Extend a growing header with a continuation-linebreak */
+static gstring *
+pdkim_hdr_cont(gstring * str, int * col)
+{
+*col = 1;
+return string_catn(str, US"\r\n\t", 3);
+}
+
+
+
+/*
+ * RFC 5322 specifies that header line length SHOULD be no more than 78
+ * pdkim_headcat
+ *
+ * Returns gstring (not nul-terminated) appending to one supplied
+ *
+ * col: this int holds and receives column number (octets since last '\n')
+ * str: partial string to append to
+ * pad: padding, split line or space after before or after eg: ";".
+ * Only the initial charater is used.
+ * intro: - must join to payload eg "h=", usually the tag name
+ * payload: eg base64 data - long data can be split arbitrarily.
+ *
+ * this code doesn't fold the header in some of the places that RFC4871
+ * allows: As per RFC5322(2.2.3) it only folds before or after tag-value
+ * pairs and inside long values. it also always spaces or breaks after the
+ * "pad"
+ *
+ * No guarantees are made for output given out-of range input. like tag
+ * names longer than 78, or bogus col. Input is assumed to be free of line breaks.
+ */
+
+static gstring *
+pdkim_headcat(int * col, gstring * str,
+ const uschar * pad, const uschar * intro, const uschar * payload)
+{
+int len, chomp, padded = 0;
+
+/* If we can fit at least the pad at the end of current line, do it now.
+Otherwise, wrap if there is a pad. */
+
+if (pad)
+ if (*col + 1 <= 78)
+ {
+ str = string_catn(str, pad, 1);
+ (*col)++;
+ pad = NULL;
+ padded = 1;
+ }
+ else
+ str = pdkim_hdr_cont(str, col);
+
+/* Special case: if the whole addition does not fit at the end of the current
+line, but could fit on a new line, wrap to give it its full, dedicated line. */
+
+len = (pad ? 2 : padded)
+ + (intro ? Ustrlen(intro) : 0)
+ + (payload ? Ustrlen(payload) : 0);
+if (len <= 77 && *col+len > 78)
+ {
+ str = pdkim_hdr_cont(str, col);
+ padded = 0;
+ }
+
+/* Either we already dealt with the pad or we know there is room */
+
+if (pad)
+ {
+ str = string_catn(str, pad, 1);
+ str = string_catn(str, US" ", 1);
+ *col += 2;
+ }
+else if (padded && *col < 78)
+ {
+ str = string_catn(str, US" ", 1);
+ (*col)++;
+ }
+
+/* Call recursively with intro as payload: it gets the same, special treatment
+(that is, not split if < 78). */
+
+if (intro)
+ str = pdkim_headcat(col, str, NULL, NULL, intro);
+
+if (payload)
+ for (len = Ustrlen(payload); len; len -= chomp)
+ {
+ if (*col >= 78)
+ str = pdkim_hdr_cont(str, col);
+ chomp = *col+len > 78 ? 78 - *col : len;
+ str = string_catn(str, payload, chomp);
+ *col += chomp;
+ payload += chomp;
+ }
+
+return str;
+}
+
+
+/* -------------------------------------------------------------------------- */
+
+/* Signing: create signature header
+*/
+static uschar *
+pdkim_create_header(pdkim_signature * sig, BOOL final)
+{
+uschar * base64_bh;
+uschar * base64_b;
+int col = 0;
+gstring * hdr;
+gstring * canon_all;
+
+canon_all = string_cat (NULL, pdkim_canons[sig->canon_headers]);
+canon_all = string_catn(canon_all, US"/", 1);
+canon_all = string_cat (canon_all, pdkim_canons[sig->canon_body]);
+(void) string_from_gstring(canon_all);
+
+hdr = string_cat(NULL, US"DKIM-Signature: v="PDKIM_SIGNATURE_VERSION);
+col = hdr->ptr;
+
+/* Required and static bits */
+hdr = pdkim_headcat(&col, hdr, US";", US"a=", dkim_sig_to_a_tag(sig));
+hdr = pdkim_headcat(&col, hdr, US";", US"q=", pdkim_querymethods[sig->querymethod]);
+hdr = pdkim_headcat(&col, hdr, US";", US"c=", canon_all->s);
+hdr = pdkim_headcat(&col, hdr, US";", US"d=", sig->domain);
+hdr = pdkim_headcat(&col, hdr, US";", US"s=", sig->selector);
+
+/* list of header names can be split between items. */
+ {
+ uschar * n = string_copy(sig->headernames);
+ uschar * i = US"h=";
+ uschar * s = US";";
+
+ while (*n)
+ {
+ uschar * c = Ustrchr(n, ':');
+
+ if (c) *c ='\0';
+
+ if (!i)
+ hdr = pdkim_headcat(&col, hdr, NULL, NULL, US":");
+
+ hdr = pdkim_headcat(&col, hdr, s, i, n);
+
+ if (!c)
+ break;
+
+ n = c+1;
+ s = NULL;
+ i = NULL;
+ }
+ }
+
+base64_bh = pdkim_encode_base64(&sig->calc_body_hash->bh);
+hdr = pdkim_headcat(&col, hdr, US";", US"bh=", base64_bh);
+
+/* Optional bits */
+if (sig->identity)
+ hdr = pdkim_headcat(&col, hdr, US";", US"i=", sig->identity);
+
+if (sig->created > 0)
+ {
+ uschar minibuf[21];
+
+ snprintf(CS minibuf, sizeof(minibuf), "%lu", sig->created);
+ hdr = pdkim_headcat(&col, hdr, US";", US"t=", minibuf);
+}
+
+if (sig->expires > 0)
+ {
+ uschar minibuf[21];
+
+ snprintf(CS minibuf, sizeof(minibuf), "%lu", sig->expires);
+ hdr = pdkim_headcat(&col, hdr, US";", US"x=", minibuf);
+ }
+
+if (sig->bodylength >= 0)
+ {
+ uschar minibuf[21];
+
+ snprintf(CS minibuf, sizeof(minibuf), "%lu", sig->bodylength);
+ hdr = pdkim_headcat(&col, hdr, US";", US"l=", minibuf);
+ }
+
+/* Preliminary or final version? */
+if (final)
+ {
+ base64_b = pdkim_encode_base64(&sig->sighash);
+ hdr = pdkim_headcat(&col, hdr, US";", US"b=", base64_b);
+
+ /* add trailing semicolon: I'm not sure if this is actually needed */
+ hdr = pdkim_headcat(&col, hdr, NULL, US";", US"");
+ }
+else
+ {
+ /* To satisfy the rule "all surrounding whitespace [...] deleted"
+ ( RFC 6376 section 3.7 ) we ensure there is no whitespace here. Otherwise
+ the headcat routine could insert a linebreak which the relaxer would reduce
+ to a single space preceding the terminating semicolon, resulting in an
+ incorrect header-hash. */
+ hdr = pdkim_headcat(&col, hdr, US";", US"b=;", US"");
+ }
+
+return string_from_gstring(hdr);
+}
+
+
+/* -------------------------------------------------------------------------- */
+
+/* According to draft-ietf-dcrup-dkim-crypto-07 "keys are 256 bits" (referring
+to DNS, hence the pubkey). Check for more than 32 bytes; if so assume the
+alternate possible representation (still) being discussed: a
+SubjectPublickeyInfo wrapped key - and drop all but the trailing 32-bytes (it
+should be a DER, with exactly 12 leading bytes - but we could accept a BER also,
+which could be any size). We still rely on the crypto library for checking for
+undersize.
+
+When the RFC is published this should be re-addressed. */
+
+static void
+check_bare_ed25519_pubkey(pdkim_pubkey * p)
+{
+int excess = p->key.len - 32;
+if (excess > 0)
+ {
+ DEBUG(D_acl)
+ debug_printf("DKIM: unexpected pubkey len %lu\n", (unsigned long) p->key.len);
+ p->key.data += excess; p->key.len = 32;
+ }
+}
+
+
+static pdkim_pubkey *
+pdkim_key_from_dns(pdkim_ctx * ctx, pdkim_signature * sig, ev_ctx * vctx,
+ const uschar ** errstr)
+{
+uschar * dns_txt_name, * dns_txt_reply;
+pdkim_pubkey * p;
+
+/* Fetch public key for signing domain, from DNS */
+
+dns_txt_name = string_sprintf("%s._domainkey.%s.", sig->selector, sig->domain);
+
+if ( !(dns_txt_reply = ctx->dns_txt_callback(dns_txt_name))
+ || dns_txt_reply[0] == '\0'
+ )
+ {
+ sig->verify_status = PDKIM_VERIFY_INVALID;
+ sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE;
+ return NULL;
+ }
+
+DEBUG(D_acl)
+ {
+ debug_printf(
+ "DKIM >> Parsing public key record >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
+ " %s\n"
+ " Raw record: ",
+ dns_txt_name);
+ pdkim_quoteprint(CUS dns_txt_reply, Ustrlen(dns_txt_reply));
+ }
+
+if ( !(p = pdkim_parse_pubkey_record(CUS dns_txt_reply))
+ || (Ustrcmp(p->srvtype, "*") != 0 && Ustrcmp(p->srvtype, "email") != 0)
+ )
+ {
+ sig->verify_status = PDKIM_VERIFY_INVALID;
+ sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_DNSRECORD;
+
+ DEBUG(D_acl)
+ {
+ if (p)
+ debug_printf(" Invalid public key service type '%s'\n", p->srvtype);
+ else
+ debug_printf(" Error while parsing public key record\n");
+ debug_printf(
+ "DKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+ }
+ return NULL;
+ }
+
+DEBUG(D_acl) debug_printf(
+ "DKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+
+/* Import public key */
+
+/* Normally we use the signature a= tag to tell us the pubkey format.
+When signing under debug we do a test-import of the pubkey, and at that
+time we do not have a signature so we must interpret the pubkey k= tag
+instead. Assume writing on the sig is ok in that case. */
+
+if (sig->keytype < 0)
+ if ((sig->keytype = pdkim_keyname_to_keytype(p->keytype)) < 0)
+ {
+ DEBUG(D_acl) debug_printf("verify_init: unhandled keytype %s\n", p->keytype);
+ sig->verify_status = PDKIM_VERIFY_INVALID;
+ sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_IMPORT;
+ return NULL;
+ }
+
+if (sig->keytype == KEYTYPE_ED25519)
+ check_bare_ed25519_pubkey(p);
+
+if ((*errstr = exim_dkim_verify_init(&p->key,
+ sig->keytype == KEYTYPE_ED25519 ? KEYFMT_ED25519_BARE : KEYFMT_DER,
+ vctx, &sig->keybits)))
+ {
+ DEBUG(D_acl) debug_printf("verify_init: %s\n", *errstr);
+ sig->verify_status = PDKIM_VERIFY_INVALID;
+ sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_IMPORT;
+ return NULL;
+ }
+
+vctx->keytype = sig->keytype;
+return p;
+}
+
+
+/* -------------------------------------------------------------------------- */
+/* Sort and filter the sigs developed from the message */
+
+static pdkim_signature *
+sort_sig_methods(pdkim_signature * siglist)
+{
+pdkim_signature * yield, ** ss;
+const uschar * prefs;
+uschar * ele;
+int sep;
+
+if (!siglist) return NULL;
+
+/* first select in order of hashtypes */
+DEBUG(D_acl) debug_printf("DKIM: dkim_verify_hashes '%s'\n", dkim_verify_hashes);
+for (prefs = dkim_verify_hashes, sep = 0, yield = NULL, ss = &yield;
+ ele = string_nextinlist(&prefs, &sep, NULL, 0); )
+ {
+ int i = pdkim_hashname_to_hashtype(CUS ele, 0);
+ for (pdkim_signature * s = siglist, * next, ** prev = &siglist; s;
+ s = next)
+ {
+ next = s->next;
+ if (s->hashtype == i)
+ { *prev = next; s->next = NULL; *ss = s; ss = &s->next; }
+ else
+ prev = &s->next;
+ }
+ }
+
+/* then in order of keytypes */
+siglist = yield;
+DEBUG(D_acl) debug_printf("DKIM: dkim_verify_keytypes '%s'\n", dkim_verify_keytypes);
+for (prefs = dkim_verify_keytypes, sep = 0, yield = NULL, ss = &yield;
+ ele = string_nextinlist(&prefs, &sep, NULL, 0); )
+ {
+ int i = pdkim_keyname_to_keytype(CUS ele);
+ for (pdkim_signature * s = siglist, * next, ** prev = &siglist; s;
+ s = next)
+ {
+ next = s->next;
+ if (s->keytype == i)
+ { *prev = next; s->next = NULL; *ss = s; ss = &s->next; }
+ else
+ prev = &s->next;
+ }
+ }
+
+DEBUG(D_acl) for (pdkim_signature * s = yield; s; s = s->next)
+ debug_printf(" retain d=%s s=%s a=%s\n",
+ s->domain, s->selector, dkim_sig_to_a_tag(s));
+return yield;
+}
+
+
+/* -------------------------------------------------------------------------- */
+
+DLLEXPORT int
+pdkim_feed_finish(pdkim_ctx * ctx, pdkim_signature ** return_signatures,
+ const uschar ** err)
+{
+BOOL verify_pass = FALSE;
+
+/* 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->ptr > 0)
+ {
+ blob * rnl = NULL;
+ int rc;
+
+ if ((rc = pdkim_header_complete(ctx)) != PDKIM_OK)
+ return rc;
+
+ for (pdkim_bodyhash * b = ctx->bodyhash; b; b = b->next)
+ rnl = pdkim_update_ctx_bodyhash(b, &lineending, rnl);
+ if (rnl) store_free(rnl);
+ }
+else
+ DEBUG(D_acl) debug_printf(
+ "DKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+
+/* Build (and/or evaluate) body hash. Do this even if no DKIM sigs, in case we
+have a hash to do for ARC. */
+
+pdkim_finish_bodyhash(ctx);
+
+/* Sort and filter the recived signatures */
+
+if (!(ctx->flags & PDKIM_MODE_SIGN))
+ ctx->sig = sort_sig_methods(ctx->sig);
+
+if (!ctx->sig)
+ {
+ DEBUG(D_acl) debug_printf("DKIM: no signatures\n");
+ *return_signatures = NULL;
+ return PDKIM_OK;
+ }
+
+for (pdkim_signature * sig = ctx->sig; sig; sig = sig->next)
+ {
+ hctx hhash_ctx;
+ uschar * sig_hdr = US"";
+ blob hhash;
+ gstring * hdata = NULL;
+ es_ctx sctx;
+
+ if ( !(ctx->flags & PDKIM_MODE_SIGN)
+ && sig->verify_status == PDKIM_VERIFY_FAIL)
+ {
+ DEBUG(D_acl)
+ debug_printf("DKIM: [%s] abandoning this signature\n", sig->domain);
+ continue;
+ }
+
+ /*XXX The hash of the headers is needed for GCrypt (for which we can do RSA
+ signing only, as it happens) and for either GnuTLS and OpenSSL when we are
+ signing with EC (specifically, Ed25519). The former is because the GCrypt
+ signing operation is pure (does not do its own hash) so we must hash. The
+ latter is because we (stupidly, but this is what the IETF draft is saying)
+ must hash with the declared hash method, then pass the result to the library
+ hash-and-sign routine (because that's all the libraries are providing. And
+ we're stuck with whatever that hidden hash method is, too). We may as well
+ do this hash incrementally.
+ We don't need the hash we're calculating here for the GnuTLS and OpenSSL
+ cases of RSA signing, since those library routines can do hash-and-sign.
+
+ Some time in the future we could easily avoid doing the hash here for those
+ cases (which will be common for a long while. We could also change from
+ the current copy-all-the-headers-into-one-block, then call the hash-and-sign
+ implementation - to a proper incremental one. Unfortunately, GnuTLS just
+ cannot do incremental - either signing or verification. Unsure about GCrypt.
+ */
+
+ /*XXX The header hash is also used (so far) by the verify operation */
+
+ if (!exim_sha_init(&hhash_ctx, pdkim_hashes[sig->hashtype].exim_hashmethod))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "DKIM: hash setup error, possibly nonhandled hashtype");
+ break;
+ }
+
+ if (ctx->flags & PDKIM_MODE_SIGN)
+ DEBUG(D_acl) debug_printf(
+ "DKIM >> Headers to be signed: >>>>>>>>>>>>\n"
+ " %s\n",
+ sig->sign_headers);
+
+ DEBUG(D_acl) debug_printf(
+ "DKIM >> Header data for hash, canonicalized (%-7s), in sequence >>\n",
+ pdkim_canons[sig->canon_headers]);
+
+
+ /* 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.
+ Then append to that list any remaining header names for which there was no
+ header to sign. */
+
+ if (ctx->flags & PDKIM_MODE_SIGN)
+ {
+ gstring * g = NULL;
+ const uschar * l;
+ uschar * s;
+ int sep = 0;
+
+ /* Import private key, including the keytype which we need for building
+ the signature header */
+
+ if ((*err = exim_dkim_signing_init(CUS sig->privkey, &sctx)))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "signing_init: %s", *err);
+ return PDKIM_ERR_RSA_PRIVKEY;
+ }
+ sig->keytype = sctx.keytype;
+
+ sig->headernames = NULL; /* Collected signed header names */
+ for (pdkim_stringlist * p = sig->headers; p; p = p->next)
+ {
+ uschar * rh = p->value;
+
+ if (header_name_match(rh, sig->sign_headers) == PDKIM_OK)
+ {
+ /* Collect header names (Note: colon presence is guaranteed here) */
+ g = string_append_listele_n(g, ':', rh, Ustrchr(rh, ':') - rh);
+
+ if (sig->canon_headers == PDKIM_CANON_RELAXED)
+ rh = pdkim_relax_header(rh, TRUE); /* cook header for relaxed canon */
+
+ /* Feed header to the hash algorithm */
+ exim_sha_update_string(&hhash_ctx, CUS rh);
+
+ /* Remember headers block for signing (when the library cannot do incremental) */
+ /*XXX we could avoid doing this for all but the GnuTLS/RSA case */
+ hdata = exim_dkim_data_append(hdata, rh);
+
+ DEBUG(D_acl) pdkim_quoteprint(rh, Ustrlen(rh));
+ }
+ }
+
+ /* Any headers we wanted to sign but were not present must also be listed.
+ Ignore elements that have been ticked-off or are marked as never-oversign. */
+
+ l = sig->sign_headers;
+ while((s = string_nextinlist(&l, &sep, NULL, 0)))
+ {
+ if (*s == '+') /* skip oversigning marker */
+ s++;
+ if (*s != '_' && *s != '=')
+ g = string_append_listele(g, ':', s);
+ }
+ sig->headernames = string_from_gstring(g);
+
+ /* Create signature header with b= omitted */
+ sig_hdr = pdkim_create_header(sig, FALSE);
+ }
+
+ /* VERIFICATION ----------------------------------------------------------- */
+ /* When verifying, walk through the header name list in the h= parameter and
+ add the headers to the hash in that order. */
+ else
+ {
+ uschar * p = sig->headernames;
+ uschar * q;
+
+ if (p)
+ {
+ /* clear tags */
+ for (pdkim_stringlist * hdrs = ctx->headers; hdrs; hdrs = hdrs->next)
+ hdrs->tag = 0;
+
+ p = string_copy(p);
+ while(1)
+ {
+ if ((q = Ustrchr(p, ':')))
+ *q = '\0';
+
+ /*XXX walk the list of headers in same order as received. */
+ for (pdkim_stringlist * hdrs = ctx->headers; hdrs; hdrs = hdrs->next)
+ if ( hdrs->tag == 0
+ && strncasecmp(CCS hdrs->value, CCS p, Ustrlen(p)) == 0
+ && (hdrs->value)[Ustrlen(p)] == ':'
+ )
+ {
+ /* cook header for relaxed canon, or just copy it for simple */
+
+ uschar * rh = sig->canon_headers == PDKIM_CANON_RELAXED
+ ? pdkim_relax_header(hdrs->value, TRUE)
+ : string_copy(CUS hdrs->value);
+
+ /* Feed header to the hash algorithm */
+ exim_sha_update_string(&hhash_ctx, CUS rh);
+
+ DEBUG(D_acl) pdkim_quoteprint(rh, Ustrlen(rh));
+ hdrs->tag = 1;
+ break;
+ }
+
+ if (!q) break;
+ p = q+1;
+ }
+
+ sig_hdr = string_copy(sig->rawsig_no_b_val);
+ }
+ }
+
+ DEBUG(D_acl) debug_printf(
+ "DKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+
+ DEBUG(D_acl)
+ {
+ debug_printf(
+ "DKIM >> Signed DKIM-Signature header, pre-canonicalized >>>>>>>>>>>>>\n");
+ pdkim_quoteprint(CUS sig_hdr, Ustrlen(sig_hdr));
+ debug_printf(
+ "DKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+ }
+
+ /* Relax header if necessary */
+ if (sig->canon_headers == PDKIM_CANON_RELAXED)
+ sig_hdr = pdkim_relax_header(sig_hdr, FALSE);
+
+ DEBUG(D_acl)
+ {
+ debug_printf("DKIM >> Signed DKIM-Signature header, canonicalized (%-7s) >>>>>>>\n",
+ pdkim_canons[sig->canon_headers]);
+ pdkim_quoteprint(CUS sig_hdr, Ustrlen(sig_hdr));
+ debug_printf(
+ "DKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+ }
+
+ /* Finalize header hash */
+ exim_sha_update_string(&hhash_ctx, CUS sig_hdr);
+ exim_sha_finish(&hhash_ctx, &hhash);
+
+ DEBUG(D_acl)
+ {
+ debug_printf("DKIM [%s] Header %s computed: ",
+ sig->domain, pdkim_hashes[sig->hashtype].dkim_hashname);
+ pdkim_hexprint(hhash.data, hhash.len);
+ }
+
+ /* Remember headers block for signing (when the signing library cannot do
+ incremental) */
+ if (ctx->flags & PDKIM_MODE_SIGN)
+ hdata = exim_dkim_data_append(hdata, US sig_hdr);
+
+ /* SIGNING ---------------------------------------------------------------- */
+ if (ctx->flags & PDKIM_MODE_SIGN)
+ {
+ hashmethod hm = sig->keytype == KEYTYPE_ED25519
+#if defined(SIGN_OPENSSL)
+ ? HASH_NULL
+#else
+ ? HASH_SHA2_512
+#endif
+ : pdkim_hashes[sig->hashtype].exim_hashmethod;
+
+#ifdef SIGN_HAVE_ED25519
+ /* For GCrypt, and for EC, we pass the hash-of-headers to the signing
+ routine. For anything else we just pass the headers. */
+
+ if (sig->keytype != KEYTYPE_ED25519)
+#endif
+ {
+ hhash.data = hdata->s;
+ hhash.len = hdata->ptr;
+ }
+
+ if ((*err = exim_dkim_sign(&sctx, hm, &hhash, &sig->sighash)))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "signing: %s", *err);
+ return PDKIM_ERR_RSA_SIGNING;
+ }
+
+ DEBUG(D_acl)
+ {
+ debug_printf( "DKIM [%s] b computed: ", sig->domain);
+ pdkim_hexprint(sig->sighash.data, sig->sighash.len);
+ }
+
+ sig->signature_header = pdkim_create_header(sig, TRUE);
+ }
+
+ /* VERIFICATION ----------------------------------------------------------- */
+ else
+ {
+ ev_ctx vctx;
+ hashmethod hm;
+
+ /* Make sure we have all required signature tags */
+ if (!( sig->domain && *sig->domain
+ && sig->selector && *sig->selector
+ && sig->headernames && *sig->headernames
+ && sig->bodyhash.data
+ && sig->sighash.data
+ && sig->keytype >= 0
+ && sig->hashtype >= 0
+ && sig->version
+ ) )
+ {
+ sig->verify_status = PDKIM_VERIFY_INVALID;
+ sig->verify_ext_status = PDKIM_VERIFY_INVALID_SIGNATURE_ERROR;
+
+ DEBUG(D_acl) debug_printf(
+ " Error in DKIM-Signature header: tags missing or invalid (%s)\n"
+ "DKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n",
+ !(sig->domain && *sig->domain) ? "d="
+ : !(sig->selector && *sig->selector) ? "s="
+ : !(sig->headernames && *sig->headernames) ? "h="
+ : !sig->bodyhash.data ? "bh="
+ : !sig->sighash.data ? "b="
+ : sig->keytype < 0 || sig->hashtype < 0 ? "a="
+ : "v="
+ );
+ goto NEXT_VERIFY;
+ }
+
+ /* Make sure sig uses supported DKIM version (only v1) */
+ if (sig->version != 1)
+ {
+ sig->verify_status = PDKIM_VERIFY_INVALID;
+ sig->verify_ext_status = PDKIM_VERIFY_INVALID_DKIM_VERSION;
+
+ DEBUG(D_acl) debug_printf(
+ " Error in DKIM-Signature header: unsupported DKIM version\n"
+ "DKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+ goto NEXT_VERIFY;
+ }
+
+ DEBUG(D_acl)
+ {
+ debug_printf( "DKIM [%s] b from mail: ", sig->domain);
+ pdkim_hexprint(sig->sighash.data, sig->sighash.len);
+ }
+
+ if (!(sig->pubkey = pdkim_key_from_dns(ctx, sig, &vctx, err)))
+ {
+ log_write(0, LOG_MAIN, "DKIM: %s%s %s%s [failed key import]",
+ sig->domain ? "d=" : "", sig->domain ? sig->domain : US"",
+ sig->selector ? "s=" : "", sig->selector ? sig->selector : US"");
+ goto NEXT_VERIFY;
+ }
+
+ /* If the pubkey limits to a list of specific hashes, ignore sigs that
+ do not have the hash part of the sig algorithm matching */
+
+ if (sig->pubkey->hashes)
+ {
+ const uschar * list = sig->pubkey->hashes, * ele;
+ int sep = ':';
+ while ((ele = string_nextinlist(&list, &sep, NULL, 0)))
+ if (Ustrcmp(ele, pdkim_hashes[sig->hashtype].dkim_hashname) == 0) break;
+ if (!ele)
+ {
+ DEBUG(D_acl) debug_printf("pubkey h=%s vs. sig a=%s_%s\n",
+ sig->pubkey->hashes,
+ pdkim_keytypes[sig->keytype],
+ pdkim_hashes[sig->hashtype].dkim_hashname);
+ sig->verify_status = PDKIM_VERIFY_FAIL;
+ sig->verify_ext_status = PDKIM_VERIFY_FAIL_SIG_ALGO_MISMATCH;
+ goto NEXT_VERIFY;
+ }
+ }
+
+ hm = sig->keytype == KEYTYPE_ED25519
+#if defined(SIGN_OPENSSL)
+ ? HASH_NULL
+#else
+ ? HASH_SHA2_512
+#endif
+ : pdkim_hashes[sig->hashtype].exim_hashmethod;
+
+ /* Check the signature */
+
+ if ((*err = exim_dkim_verify(&vctx, hm, &hhash, &sig->sighash)))
+ {
+ DEBUG(D_acl) debug_printf("headers verify: %s\n", *err);
+ sig->verify_status = PDKIM_VERIFY_FAIL;
+ sig->verify_ext_status = PDKIM_VERIFY_FAIL_MESSAGE;
+ goto NEXT_VERIFY;
+ }
+ if (*dkim_verify_min_keysizes)
+ {
+ unsigned minbits;
+ uschar * ss = expand_getkeyed(US pdkim_keytypes[sig->keytype],
+ dkim_verify_min_keysizes);
+ if (ss && (minbits = atoi(CS ss)) > sig->keybits)
+ {
+ DEBUG(D_acl) debug_printf("Key too short: Actual: %s %u Minima '%s'\n",
+ pdkim_keytypes[sig->keytype], sig->keybits, dkim_verify_min_keysizes);
+ sig->verify_status = PDKIM_VERIFY_FAIL;
+ sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_KEYSIZE;
+ }
+ }
+
+
+ /* We have a winner! (if bodyhash was correct earlier) */
+ if (sig->verify_status == PDKIM_VERIFY_NONE)
+ {
+ sig->verify_status = PDKIM_VERIFY_PASS;
+ verify_pass = TRUE;
+ if (dkim_verify_minimal) break;
+ }
+
+NEXT_VERIFY:
+
+ DEBUG(D_acl)
+ {
+ debug_printf("DKIM [%s] %s signature status: %s",
+ sig->domain, dkim_sig_to_a_tag(sig),
+ pdkim_verify_status_str(sig->verify_status));
+ if (sig->verify_ext_status > 0)
+ debug_printf(" (%s)\n",
+ pdkim_verify_ext_status_str(sig->verify_ext_status));
+ else
+ debug_printf("\n");
+ }
+ }
+ }
+
+/* If requested, set return pointer to signature(s) */
+if (return_signatures)
+ *return_signatures = ctx->sig;
+
+return ctx->flags & PDKIM_MODE_SIGN || verify_pass
+ ? PDKIM_OK : PDKIM_FAIL;
+}
+
+
+/* -------------------------------------------------------------------------- */
+
+DLLEXPORT pdkim_ctx *
+pdkim_init_verify(uschar * (*dns_txt_callback)(const uschar *), BOOL dot_stuffing)
+{
+pdkim_ctx * ctx;
+
+ctx = store_get(sizeof(pdkim_ctx), GET_UNTAINTED);
+memset(ctx, 0, sizeof(pdkim_ctx));
+
+if (dot_stuffing) ctx->flags = PDKIM_DOT_TERM;
+/* The line-buffer is for message data, hence tainted */
+ctx->linebuf = store_get(PDKIM_MAX_BODY_LINE_LEN, GET_TAINTED);
+ctx->dns_txt_callback = dns_txt_callback;
+ctx->cur_header = string_get_tainted(36, GET_TAINTED);
+
+return ctx;
+}
+
+
+/* -------------------------------------------------------------------------- */
+
+DLLEXPORT pdkim_signature *
+pdkim_init_sign(pdkim_ctx * ctx,
+ uschar * domain, uschar * selector, uschar * privkey,
+ uschar * hashname, const uschar ** errstr)
+{
+int hashtype;
+pdkim_signature * sig;
+
+if (!domain || !selector || !privkey)
+ return NULL;
+
+/* Allocate & init one signature struct */
+
+sig = store_get(sizeof(pdkim_signature), GET_UNTAINTED);
+memset(sig, 0, sizeof(pdkim_signature));
+
+sig->bodylength = -1;
+
+sig->domain = string_copy(US domain);
+sig->selector = string_copy(US selector);
+sig->privkey = string_copy(US privkey);
+sig->keytype = -1;
+
+for (hashtype = 0; hashtype < nelem(pdkim_hashes); hashtype++)
+ if (Ustrcmp(hashname, pdkim_hashes[hashtype].dkim_hashname) == 0)
+ { sig->hashtype = hashtype; break; }
+if (hashtype >= nelem(pdkim_hashes))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "DKIM: unrecognised hashname '%s'", hashname);
+ return NULL;
+ }
+
+DEBUG(D_acl)
+ {
+ pdkim_signature s = *sig;
+ ev_ctx vctx;
+
+ debug_printf("DKIM (checking verify key)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+ if (!pdkim_key_from_dns(ctx, &s, &vctx, errstr))
+ debug_printf("WARNING: bad dkim key in dns\n");
+ debug_printf("DKIM (finished checking verify key)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+ }
+return sig;
+}
+
+
+/* -------------------------------------------------------------------------- */
+
+DLLEXPORT void
+pdkim_set_optional(pdkim_signature * sig,
+ char * sign_headers,
+ char * identity,
+ int canon_headers,
+ int canon_body,
+ long bodylength,
+ unsigned long created,
+ unsigned long expires)
+{
+if (identity)
+ sig->identity = string_copy(US identity);
+
+sig->sign_headers = string_copy(sign_headers
+ ? US sign_headers : US PDKIM_DEFAULT_SIGN_HEADERS);
+
+sig->canon_headers = canon_headers;
+sig->canon_body = canon_body;
+sig->bodylength = bodylength;
+sig->created = created;
+sig->expires = expires;
+
+return;
+}
+
+
+
+/* Set up a blob for calculating the bodyhash according to the
+given needs. Use an existing one if possible, or create a new one.
+
+Return: hashblob pointer, or NULL on error
+*/
+pdkim_bodyhash *
+pdkim_set_bodyhash(pdkim_ctx * ctx, int hashtype, int canon_method,
+ long bodylength)
+{
+pdkim_bodyhash * b;
+
+if (hashtype == -1 || canon_method == -1) return NULL;
+
+for (b = ctx->bodyhash; b; b = b->next)
+ if ( hashtype == b->hashtype
+ && canon_method == b->canon_method
+ && bodylength == b->bodylength)
+ {
+ DEBUG(D_receive) debug_printf("DKIM: using existing bodyhash %s/%s/%ld\n",
+ pdkim_hashes[hashtype].dkim_hashname, pdkim_canons[canon_method], bodylength);
+ return b;
+ }
+
+DEBUG(D_receive) debug_printf("DKIM: new bodyhash %s/%s/%ld\n",
+ pdkim_hashes[hashtype].dkim_hashname, pdkim_canons[canon_method], bodylength);
+b = store_get(sizeof(pdkim_bodyhash), GET_UNTAINTED);
+b->next = ctx->bodyhash;
+b->hashtype = hashtype;
+b->canon_method = canon_method;
+b->bodylength = bodylength;
+if (!exim_sha_init(&b->body_hash_ctx, /*XXX hash method: extend for sha512 */
+ pdkim_hashes[hashtype].exim_hashmethod))
+ {
+ DEBUG(D_acl)
+ debug_printf("DKIM: hash init error, possibly nonhandled hashtype\n");
+ return NULL;
+ }
+b->signed_body_bytes = 0;
+b->num_buffered_blanklines = 0;
+ctx->bodyhash = b;
+return b;
+}
+
+
+/* Set up a blob for calculating the bodyhash according to the
+needs of this signature. Use an existing one if possible, or
+create a new one.
+
+Return: hashblob pointer, or NULL on error (only used as a boolean).
+*/
+pdkim_bodyhash *
+pdkim_set_sig_bodyhash(pdkim_ctx * ctx, pdkim_signature * sig)
+{
+pdkim_bodyhash * b = pdkim_set_bodyhash(ctx,
+ sig->hashtype, sig->canon_body, sig->bodylength);
+sig->calc_body_hash = b;
+return b;
+}
+
+
+/* -------------------------------------------------------------------------- */
+
+
+void
+pdkim_init_context(pdkim_ctx * ctx, BOOL dot_stuffed,
+ uschar * (*dns_txt_callback)(const uschar *))
+{
+memset(ctx, 0, sizeof(pdkim_ctx));
+ctx->flags = dot_stuffed ? PDKIM_MODE_SIGN | PDKIM_DOT_TERM : PDKIM_MODE_SIGN;
+/* The line buffer is for message data, hence tainted */
+ctx->linebuf = store_get(PDKIM_MAX_BODY_LINE_LEN, GET_TAINTED);
+DEBUG(D_acl) ctx->dns_txt_callback = dns_txt_callback;
+}
+
+
+void
+pdkim_init(void)
+{
+exim_dkim_init();
+}
+
+
+
+#endif /*DISABLE_DKIM*/
diff --git a/src/pdkim/pdkim.h b/src/pdkim/pdkim.h
new file mode 100644
index 0000000..f6ff782
--- /dev/null
+++ b/src/pdkim/pdkim.h
@@ -0,0 +1,369 @@
+/*
+ * PDKIM - a RFC4871 (DKIM) implementation
+ *
+ * Copyright (C) 2009 - 2012 Tom Kistner <tom@duncanthrax.net>
+ * Copyright (c) 2016 - 2020 Jeremy Harris
+ *
+ * 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.
+ */
+#ifndef PDKIM_H
+#define PDKIM_H
+
+#include "../blob.h"
+#include "../hash.h"
+
+#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"
+
+#define PDKIM_OVERSIGN_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"
+
+/* -------------------------------------------------------------------------- */
+/* 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_RSA_PRIVKEY -101
+#define PDKIM_ERR_RSA_SIGNING -102
+#define PDKIM_ERR_LONG_LINE -103
+#define PDKIM_ERR_BUFFER_TOO_SMALL -104
+#define PDKIM_ERR_EXCESS_SIGS -105
+#define PDKIM_SIGN_PRIVKEY_WRAP -106
+#define PDKIM_SIGN_PRIVKEY_B64D -107
+
+/* -------------------------------------------------------------------------- */
+/* 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_POLICY BIT(31)
+
+#define PDKIM_VERIFY_FAIL_BODY 1
+#define PDKIM_VERIFY_FAIL_MESSAGE 2
+#define PDKIM_VERIFY_FAIL_SIG_ALGO_MISMATCH 3
+#define PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE 4
+#define PDKIM_VERIFY_INVALID_BUFFER_SIZE 5
+#define PDKIM_VERIFY_INVALID_PUBKEY_DNSRECORD 6
+#define PDKIM_VERIFY_INVALID_PUBKEY_IMPORT 7
+#define PDKIM_VERIFY_INVALID_PUBKEY_KEYSIZE 8
+#define PDKIM_VERIFY_INVALID_SIGNATURE_ERROR 9
+#define PDKIM_VERIFY_INVALID_DKIM_VERSION 10
+
+/* -------------------------------------------------------------------------- */
+/* Some parameter values */
+#define PDKIM_QUERYMETHOD_DNS_TXT 0
+
+#define PDKIM_CANON_SIMPLE 0
+#define PDKIM_CANON_RELAXED 1
+
+/* -------------------------------------------------------------------------- */
+/* 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 {
+ const uschar * version; /* v= */
+ const uschar *granularity; /* g= */
+
+ const uschar * hashes; /* h= */
+ const uschar * keytype; /* k= */
+ const uschar * srvtype; /* s= */
+ uschar *notes; /* n= */
+
+ blob key; /* p= */
+ int testing; /* t=y */
+ int no_subdomaining; /* t=s */
+} pdkim_pubkey;
+
+/* -------------------------------------------------------------------------- */
+/* Body-hash to be calculated */
+typedef struct pdkim_bodyhash {
+ struct pdkim_bodyhash * next;
+ int hashtype;
+ int canon_method;
+ long bodylength;
+
+ hctx body_hash_ctx;
+ unsigned long signed_body_bytes; /* done so far */
+ int num_buffered_blanklines;
+
+ blob bh; /* completed hash */
+} pdkim_bodyhash;
+
+/* -------------------------------------------------------------------------- */
+/* Signature as it appears in a DKIM-Signature header */
+typedef struct pdkim_signature {
+ struct pdkim_signature * next;
+
+ /* Bits stored in a DKIM signature header --------------------------- */
+
+ /* (v=) The version, as an integer. Currently, always "1" */
+ int version;
+
+ /* (a=) The signature algorithm. */
+ int keytype; /* pdkim_keytypes index */
+ unsigned keybits; /* size of the key */
+ int hashtype; /* pdkim_hashes index */
+
+ /* (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 */
+ uschar *selector;
+
+ /* (d=) The domain as given in the signature */
+ uschar *domain;
+
+ /* (i=) The identity as given in the signature */
+ uschar *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 */
+ uschar *headernames;
+
+ /* (z=) */
+ uschar *copiedheaders;
+
+ /* (b=) Raw signature data, along with its length in bytes */
+ blob sighash;
+
+ /* (bh=) Raw body hash data, along with its length in bytes */
+ blob bodyhash;
+
+ /* Folded DKIM-Signature: header. Signing 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(). */
+ uschar *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;
+
+ /* Properties below this point are used internally only ------------- */
+
+ /* Per-signature helper variables ----------------------------------- */
+ pdkim_bodyhash *calc_body_hash; /* hash to be / being calculated */
+
+ pdkim_stringlist *headers; /* Raw headers included in the sig */
+
+ /* Signing specific ------------------------------------------------- */
+ uschar * privkey; /* Private key */
+ uschar * sign_headers; /* To-be-signed header names */
+ uschar * rawsig_no_b_val; /* Original signature header w/o b= tag value. */
+} pdkim_signature;
+
+
+/* -------------------------------------------------------------------------- */
+/* Context to keep state between all operations. */
+typedef struct pdkim_ctx {
+
+#define PDKIM_MODE_SIGN BIT(0) /* if unset, mode==verify */
+#define PDKIM_DOT_TERM BIT(1) /* dot termination and unstuffing */
+#define PDKIM_SEEN_CR BIT(2)
+#define PDKIM_SEEN_LF BIT(3)
+#define PDKIM_PAST_HDRS BIT(4)
+#define PDKIM_SEEN_EOD BIT(5)
+ unsigned flags;
+
+ /* One (signing) or several chained (verification) signatures */
+ pdkim_signature *sig;
+
+ /* One (signing) or several chained (verification) bodyhashes */
+ pdkim_bodyhash *bodyhash;
+
+ /* Callback for dns/txt query method (verification only) */
+ uschar * (*dns_txt_callback)(const uschar *);
+
+ /* Coder's little helpers */
+ gstring *cur_header;
+ uschar *linebuf;
+ int linebuf_offset;
+ int num_headers;
+ pdkim_stringlist *headers; /* Raw headers for verification */
+} pdkim_ctx;
+
+
+/******************************************************************************/
+
+typedef struct {
+ const uschar * dkim_hashname;
+ hashmethod exim_hashmethod;
+} pdkim_hashtype;
+extern const pdkim_hashtype pdkim_hashes[];
+
+/******************************************************************************/
+
+
+/* -------------------------------------------------------------------------- */
+/* API functions. Please see the sample code in sample/test_sign.c and
+ sample/test_verify.c for documentation.
+*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void pdkim_init (void);
+
+void pdkim_init_context (pdkim_ctx *, BOOL, uschar * (*)(const uschar *));
+
+DLLEXPORT
+pdkim_signature *pdkim_init_sign (pdkim_ctx *,
+ uschar *, uschar *, uschar *, uschar *,
+ const uschar **);
+
+DLLEXPORT
+pdkim_ctx *pdkim_init_verify (uschar * (*)(const uschar *), BOOL);
+
+DLLEXPORT
+void pdkim_set_optional (pdkim_signature *, char *, char *,int, int,
+ long,
+ unsigned long,
+ unsigned long);
+
+int pdkim_hashname_to_hashtype(const uschar *, unsigned);
+void pdkim_cstring_to_canons(const uschar *, unsigned, int *, int *);
+pdkim_bodyhash *pdkim_set_bodyhash(pdkim_ctx *, int, int, long);
+pdkim_bodyhash *pdkim_set_sig_bodyhash(pdkim_ctx *, pdkim_signature *);
+
+DLLEXPORT
+int pdkim_feed (pdkim_ctx *, uschar *, int);
+DLLEXPORT
+int pdkim_feed_finish (pdkim_ctx *, pdkim_signature **, const uschar **);
+
+DLLEXPORT
+void pdkim_free_ctx (pdkim_ctx *);
+
+
+const uschar * pdkim_errstr(int);
+
+extern uschar * pdkim_encode_base64(blob *);
+extern void pdkim_decode_base64(const uschar *, blob *);
+extern void pdkim_hexprint(const uschar *, int);
+extern void pdkim_quoteprint(const uschar *, int);
+extern pdkim_pubkey * pdkim_parse_pubkey_record(const uschar *);
+extern uschar * pdkim_relax_header_n(const uschar *, int, BOOL);
+extern uschar * pdkim_relax_header(const uschar *, BOOL);
+extern uschar * dkim_sig_to_a_tag(const pdkim_signature *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/src/pdkim/pdkim_hash.h b/src/pdkim/pdkim_hash.h
new file mode 100644
index 0000000..8f9a126
--- /dev/null
+++ b/src/pdkim/pdkim_hash.h
@@ -0,0 +1,38 @@
+/*
+ * PDKIM - a RFC4871 (DKIM) implementation
+ *
+ * Copyright (C) 1995 - 2018 Exim maintainers
+ *
+ * Hash interface functions
+ */
+
+#include "../exim.h"
+
+#if !defined(HASH_H) /* entire file */
+#define HASH_H
+
+#ifdef DISABLE_TLS
+# error Must not DISABLE_TLS, for DKIM
+#endif
+
+#include "crypt_ver.h"
+#include "../blob.h"
+#include "../hash.h"
+
+#ifdef SIGN_OPENSSL
+# include <openssl/rsa.h>
+# include <openssl/ssl.h>
+# include <openssl/err.h>
+#elif defined(SIGN_GNUTLS)
+# include <gnutls/gnutls.h>
+# include <gnutls/x509.h>
+#endif
+
+#if defined(SHA_OPENSSL)
+# include "pdkim.h"
+#elif defined(SHA_GCRYPT)
+# include "pdkim.h"
+#endif
+
+#endif
+/* End of File */
diff --git a/src/pdkim/signing.c b/src/pdkim/signing.c
new file mode 100644
index 0000000..d78f31a
--- /dev/null
+++ b/src/pdkim/signing.c
@@ -0,0 +1,903 @@
+/*
+ * PDKIM - a RFC4871 (DKIM) implementation
+ * Copyright (c) The Exim Maintainers 1995 - 2022
+ *
+ * signing/verification interface
+ */
+
+#include "../exim.h"
+#include "crypt_ver.h"
+#include "signing.h"
+
+
+#ifdef MACRO_PREDEF
+# include "../macro_predef.h"
+
+void
+features_crypto(void)
+{
+# ifdef SIGN_HAVE_ED25519
+ builtin_macro_create(US"_CRYPTO_SIGN_ED25519");
+# endif
+# ifdef EXIM_HAVE_SHA3
+ builtin_macro_create(US"_CRYPTO_HASH_SHA3");
+# endif
+}
+#else
+
+#ifndef DISABLE_DKIM /* rest of file */
+
+#ifdef DISABLE_TLS
+# error Must no DISABLE_TLS, for DKIM
+#endif
+
+
+/******************************************************************************/
+#ifdef SIGN_GNUTLS
+# define EXIM_GNUTLS_LIBRARY_LOG_LEVEL 3
+
+# ifndef GNUTLS_VERIFY_ALLOW_BROKEN
+# define GNUTLS_VERIFY_ALLOW_BROKEN 0
+# endif
+
+
+/* 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
+
+
+
+void
+exim_dkim_init(void)
+{
+#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
+}
+
+
+/* accumulate data (gnutls-only). String to be appended must be nul-terminated. */
+gstring *
+exim_dkim_data_append(gstring * g, uschar * s)
+{
+return string_cat(g, s);
+}
+
+
+
+/* import private key from PEM string in memory.
+Return: NULL for success, or an error string */
+
+const uschar *
+exim_dkim_signing_init(const uschar * privkey_pem, es_ctx * sign_ctx)
+{
+gnutls_datum_t k = { .data = (void *)privkey_pem, .size = Ustrlen(privkey_pem) };
+gnutls_x509_privkey_t x509_key;
+const uschar * where;
+int rc;
+
+if ( (where = US"internal init", rc = gnutls_x509_privkey_init(&x509_key))
+ || (rc = gnutls_privkey_init(&sign_ctx->key))
+ || (where = US"privkey PEM-block import",
+ rc = gnutls_x509_privkey_import(x509_key, &k, GNUTLS_X509_FMT_PEM))
+ || (where = US"internal privkey transfer",
+ rc = gnutls_privkey_import_x509(sign_ctx->key, x509_key, 0))
+ )
+ return string_sprintf("%s: %s", where, gnutls_strerror(rc));
+
+switch (rc = gnutls_privkey_get_pk_algorithm(sign_ctx->key, NULL))
+ {
+ case GNUTLS_PK_RSA: sign_ctx->keytype = KEYTYPE_RSA; break;
+#ifdef SIGN_HAVE_ED25519
+ case GNUTLS_PK_EDDSA_ED25519: sign_ctx->keytype = KEYTYPE_ED25519; break;
+#endif
+ default: return rc < 0
+ ? CUS gnutls_strerror(rc)
+ : string_sprintf("Unhandled key type: %d '%s'", rc, gnutls_pk_get_name(rc));
+ }
+
+return NULL;
+}
+
+
+
+/* allocate mem for signature (when signing) */
+/* hash & sign data. No way to do incremental.
+
+Return: NULL for success, or an error string */
+
+const uschar *
+exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, blob * data, blob * sig)
+{
+gnutls_datum_t k_data = { .data = data->data, .size = data->len };
+gnutls_digest_algorithm_t dig;
+gnutls_datum_t k_sig;
+int rc;
+
+switch (hash)
+ {
+ case HASH_SHA1: dig = GNUTLS_DIG_SHA1; break;
+ case HASH_SHA2_256: dig = GNUTLS_DIG_SHA256; break;
+ case HASH_SHA2_512: dig = GNUTLS_DIG_SHA512; break;
+ default: return US"nonhandled hash type";
+ }
+
+if ((rc = gnutls_privkey_sign_data(sign_ctx->key, dig, 0, &k_data, &k_sig)))
+ return CUS gnutls_strerror(rc);
+
+/* Don't care about deinit for the key; shortlived process */
+
+sig->data = k_sig.data;
+sig->len = k_sig.size;
+return NULL;
+}
+
+
+
+/* import public key (from blob in memory)
+Return: NULL for success, or an error string */
+
+const uschar *
+exim_dkim_verify_init(blob * pubkey, keyformat fmt, ev_ctx * verify_ctx,
+ unsigned * bits)
+{
+gnutls_datum_t k;
+int rc;
+const uschar * ret = NULL;
+
+gnutls_pubkey_init(&verify_ctx->key);
+k.data = pubkey->data;
+k.size = pubkey->len;
+
+switch(fmt)
+ {
+ case KEYFMT_DER:
+ if ((rc = gnutls_pubkey_import(verify_ctx->key, &k, GNUTLS_X509_FMT_DER)))
+ ret = US gnutls_strerror(rc);
+ break;
+#ifdef SIGN_HAVE_ED25519
+ case KEYFMT_ED25519_BARE:
+ if ((rc = gnutls_pubkey_import_ecc_raw(verify_ctx->key,
+ GNUTLS_ECC_CURVE_ED25519, &k, NULL)))
+ ret = US gnutls_strerror(rc);
+ break;
+#endif
+ default:
+ ret = US"pubkey format not handled";
+ break;
+ }
+if (!ret && bits) gnutls_pubkey_get_pk_algorithm(verify_ctx->key, bits);
+return ret;
+}
+
+
+/* verify signature (of hash if RSA sig, of data if EC sig. No way to do incremental)
+(given pubkey & alleged sig)
+Return: NULL for success, or an error string */
+
+const uschar *
+exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, blob * data_hash, blob * sig)
+{
+gnutls_datum_t k = { .data = data_hash->data, .size = data_hash->len };
+gnutls_datum_t s = { .data = sig->data, .size = sig->len };
+int rc;
+const uschar * ret = NULL;
+
+#ifdef SIGN_HAVE_ED25519
+if (verify_ctx->keytype == KEYTYPE_ED25519)
+ {
+ if ((rc = gnutls_pubkey_verify_data2(verify_ctx->key,
+ GNUTLS_SIGN_EDDSA_ED25519, 0, &k, &s)) < 0)
+ ret = US gnutls_strerror(rc);
+ }
+else
+#endif
+ {
+ gnutls_sign_algorithm_t algo;
+ switch (hash)
+ {
+ case HASH_SHA1: algo = GNUTLS_SIGN_RSA_SHA1; break;
+ case HASH_SHA2_256: algo = GNUTLS_SIGN_RSA_SHA256; break;
+ case HASH_SHA2_512: algo = GNUTLS_SIGN_RSA_SHA512; break;
+ default: return US"nonhandled hash type";
+ }
+
+ if ((rc = gnutls_pubkey_verify_hash2(verify_ctx->key, algo,
+ GNUTLS_VERIFY_ALLOW_BROKEN, &k, &s)) < 0)
+ ret = US gnutls_strerror(rc);
+ }
+
+gnutls_pubkey_deinit(verify_ctx->key);
+return ret;
+}
+
+
+
+
+#elif defined(SIGN_GCRYPT)
+/******************************************************************************/
+/* This variant is used under pre-3.0.0 GnuTLS. Only rsa-sha1 and rsa-sha256 */
+
+
+/* Internal service routine:
+Read and move past an asn.1 header, checking class & tag,
+optionally returning the data-length */
+
+static int
+as_tag(blob * der, uschar req_cls, long req_tag, long * alen)
+{
+int rc;
+uschar tag_class;
+int taglen;
+long tag, len;
+
+debug_printf_indent("as_tag: %02x %02x %02x %02x\n",
+ der->data[0], der->data[1], der->data[2], der->data[3]);
+
+if ((rc = asn1_get_tag_der(der->data++, der->len--, &tag_class, &taglen, &tag))
+ != ASN1_SUCCESS)
+ return rc;
+
+if (tag_class != req_cls || tag != req_tag) return ASN1_ELEMENT_NOT_FOUND;
+
+if ((len = asn1_get_length_der(der->data, der->len, &taglen)) < 0)
+ return ASN1_DER_ERROR;
+if (alen) *alen = len;
+
+/* debug_printf_indent("as_tag: tlen %d dlen %d\n", taglen, (int)len); */
+
+der->data += taglen;
+der->len -= taglen;
+return rc;
+}
+
+/* Internal service routine:
+Read and move over an asn.1 integer, setting an MPI to the value
+*/
+
+static uschar *
+as_mpi(blob * der, gcry_mpi_t * mpi)
+{
+long alen;
+int rc;
+gcry_error_t gerr;
+
+debug_printf_indent("%s\n", __FUNCTION__);
+
+/* integer; move past the header */
+if ((rc = as_tag(der, 0, ASN1_TAG_INTEGER, &alen)) != ASN1_SUCCESS)
+ return US asn1_strerror(rc);
+
+/* read to an MPI */
+if ((gerr = gcry_mpi_scan(mpi, GCRYMPI_FMT_STD, der->data, alen, NULL)))
+ return US gcry_strerror(gerr);
+
+/* move over the data */
+der->data += alen; der->len -= alen;
+return NULL;
+}
+
+
+
+void
+exim_dkim_init(void)
+{
+/* Version check should be the very first call because it
+makes sure that important subsystems are initialized. */
+if (!gcry_check_version (GCRYPT_VERSION))
+ {
+ fputs ("libgcrypt version mismatch\n", stderr);
+ exit (2);
+ }
+
+/* We don't want to see any warnings, e.g. because we have not yet
+parsed program options which might be used to suppress such
+warnings. */
+gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
+
+/* ... If required, other initialization goes here. Note that the
+process might still be running with increased privileges and that
+the secure memory has not been initialized. */
+
+/* Allocate a pool of 16k secure memory. This make the secure memory
+available and also drops privileges where needed. */
+gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
+
+/* It is now okay to let Libgcrypt complain when there was/is
+a problem with the secure memory. */
+gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
+
+/* ... If required, other initialization goes here. */
+
+/* Tell Libgcrypt that initialization has completed. */
+gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
+
+return;
+}
+
+
+
+
+/* Accumulate data (gnutls-only).
+String to be appended must be nul-terminated. */
+
+gstring *
+exim_dkim_data_append(gstring * g, uschar * s)
+{
+return g; /*dummy*/
+}
+
+
+
+/* import private key from PEM string in memory.
+Only handles RSA keys.
+Return: NULL for success, or an error string */
+
+const uschar *
+exim_dkim_signing_init(const uschar * privkey_pem, es_ctx * sign_ctx)
+{
+uschar * s1, * s2;
+blob der;
+long alen;
+int rc;
+
+/*XXX will need extension to _spot_ as well as handle a
+non-RSA key? I think...
+So... this is not a PrivateKeyInfo - which would have a field
+identifying the keytype - PrivateKeyAlgorithmIdentifier -
+but a plain RSAPrivateKey (wrapped in PEM-headers. Can we
+use those as a type tag? What forms are there? "BEGIN EC PRIVATE KEY" (cf. ec(1ssl))
+
+How does OpenSSL PEM_read_bio_PrivateKey() deal with it?
+gnutls_x509_privkey_import() ?
+*/
+
+/*
+ * 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
+
+ * ECPrivateKey ::= SEQUENCE {
+ * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
+ * privateKey OCTET STRING,
+ * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
+ * publicKey [1] BIT STRING OPTIONAL
+ * }
+ * Hmm, only 1 useful item, and not even an integer? Wonder how we might use it...
+
+- actually, gnutls_x509_privkey_import() appears to require a curve name parameter
+ value for that is an OID? a local-only integer (it's an enum in GnuTLS)?
+
+
+Useful cmds:
+ ssh-keygen -t ecdsa -f foo.privkey
+ ssh-keygen -t ecdsa -b384 -f foo.privkey
+ ssh-keygen -t ecdsa -b521 -f foo.privkey
+ ssh-keygen -t ed25519 -f foo.privkey
+
+ < foo openssl pkcs8 -in /dev/stdin -inform PEM -nocrypt -topk8 -outform DER | od -x
+
+ openssl asn1parse -in foo -inform PEM -dump
+ openssl asn1parse -in foo -inform PEM -dump -stroffset 24 (??)
+(not good for ed25519)
+
+ */
+
+if ( !(s1 = Ustrstr(CS privkey_pem, "-----BEGIN RSA PRIVATE KEY-----"))
+ || !(s2 = Ustrstr(CS (s1+=31), "-----END RSA PRIVATE KEY-----" ))
+ )
+ return US"Bad PEM wrapper";
+
+*s2 = '\0';
+
+if ((rc = b64decode(s1, &der.data) < 0))
+ return US"Bad PEM-DER b64 decode";
+der.len = rc;
+
+/* untangle asn.1 */
+
+/* sequence; just move past the header */
+if ((rc = as_tag(&der, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, NULL))
+ != ASN1_SUCCESS) goto asn_err;
+
+/* integer version; move past the header, check is zero */
+if ((rc = as_tag(&der, 0, ASN1_TAG_INTEGER, &alen)) != ASN1_SUCCESS)
+ goto asn_err;
+if (alen != 1 || *der.data != 0)
+ return US"Bad version number";
+der.data++; der.len--;
+
+if ( (s1 = as_mpi(&der, &sign_ctx->n))
+ || (s1 = as_mpi(&der, &sign_ctx->e))
+ || (s1 = as_mpi(&der, &sign_ctx->d))
+ || (s1 = as_mpi(&der, &sign_ctx->p))
+ || (s1 = as_mpi(&der, &sign_ctx->q))
+ || (s1 = as_mpi(&der, &sign_ctx->dp))
+ || (s1 = as_mpi(&der, &sign_ctx->dq))
+ || (s1 = as_mpi(&der, &sign_ctx->qp))
+ )
+ return s1;
+
+#ifdef extreme_debug
+DEBUG(D_acl) debug_printf_indent("rsa_signing_init:\n");
+ {
+ uschar * s;
+ gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->n);
+ debug_printf_indent(" N : %s\n", s);
+ gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->e);
+ debug_printf_indent(" E : %s\n", s);
+ gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->d);
+ debug_printf_indent(" D : %s\n", s);
+ gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->p);
+ debug_printf_indent(" P : %s\n", s);
+ gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->q);
+ debug_printf_indent(" Q : %s\n", s);
+ gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->dp);
+ debug_printf_indent(" DP: %s\n", s);
+ gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->dq);
+ debug_printf_indent(" DQ: %s\n", s);
+ gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->qp);
+ debug_printf_indent(" QP: %s\n", s);
+ }
+#endif
+
+sign_ctx->keytype = KEYTYPE_RSA;
+return NULL;
+
+asn_err: return US asn1_strerror(rc);
+}
+
+
+
+/* allocate mem for signature (when signing) */
+/* sign already-hashed data.
+
+Return: NULL for success, or an error string */
+
+const uschar *
+exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, blob * data, blob * sig)
+{
+char * sexp_hash;
+gcry_sexp_t s_hash = NULL, s_key = NULL, s_sig = NULL;
+gcry_mpi_t m_sig;
+uschar * errstr;
+gcry_error_t gerr;
+
+/*XXX will need extension for hash types (though, possibly, should
+be re-specced to not rehash but take an already-hashed value? Actually
+current impl looks WRONG - it _is_ given a hash so should not be
+re-hashing. Has this been tested?
+
+Will need extension for non-RSA sugning algos. */
+
+switch (hash)
+ {
+ case HASH_SHA1: sexp_hash = "(data(flags pkcs1)(hash sha1 %b))"; break;
+ case HASH_SHA2_256: sexp_hash = "(data(flags pkcs1)(hash sha256 %b))"; break;
+ default: return US"nonhandled hash type";
+ }
+
+#define SIGSPACE 128
+sig->data = store_get(SIGSPACE, GET_UNTAINTED);
+
+if (gcry_mpi_cmp (sign_ctx->p, sign_ctx->q) > 0)
+ {
+ gcry_mpi_swap (sign_ctx->p, sign_ctx->q);
+ gcry_mpi_invm (sign_ctx->qp, sign_ctx->p, sign_ctx->q);
+ }
+
+if ( (gerr = gcry_sexp_build (&s_key, NULL,
+ "(private-key (rsa (n%m)(e%m)(d%m)(p%m)(q%m)(u%m)))",
+ sign_ctx->n, sign_ctx->e,
+ sign_ctx->d, sign_ctx->p,
+ sign_ctx->q, sign_ctx->qp))
+ || (gerr = gcry_sexp_build (&s_hash, NULL, sexp_hash,
+ (int) data->len, CS data->data))
+ || (gerr = gcry_pk_sign (&s_sig, s_hash, s_key))
+ )
+ return US gcry_strerror(gerr);
+
+/* gcry_sexp_dump(s_sig); */
+
+if ( !(s_sig = gcry_sexp_find_token(s_sig, "s", 0))
+ )
+ return US"no sig result";
+
+m_sig = gcry_sexp_nth_mpi(s_sig, 1, GCRYMPI_FMT_USG);
+
+#ifdef extreme_debug
+DEBUG(D_acl)
+ {
+ uschar * s;
+ gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, m_sig);
+ debug_printf_indent(" SG: %s\n", s);
+ }
+#endif
+
+gerr = gcry_mpi_print(GCRYMPI_FMT_USG, sig->data, SIGSPACE, &sig->len, m_sig);
+if (gerr)
+ {
+ debug_printf_indent("signature conversion from MPI to buffer failed\n");
+ return US gcry_strerror(gerr);
+ }
+#undef SIGSPACE
+
+return NULL;
+}
+
+
+/* import public key (from blob in memory)
+Return: NULL for success, or an error string */
+
+const uschar *
+exim_dkim_verify_init(blob * pubkey, keyformat fmt, ev_ctx * verify_ctx,
+ unsigned * bits)
+{
+/*
+in code sequence per b81207d2bfa92 rsa_parse_public_key() and asn1_get_mpi()
+*/
+uschar tag_class;
+int taglen;
+long alen;
+unsigned nbits;
+int rc;
+uschar * errstr;
+gcry_error_t gerr;
+uschar * stage = US"S1";
+
+if (fmt != KEYFMT_DER) return US"pubkey format not handled";
+
+/*
+sequence
+ sequence
+ OBJECT:rsaEncryption
+ NULL
+ BIT STRING:RSAPublicKey
+ sequence
+ INTEGER:Public modulus
+ INTEGER:Public exponent
+
+openssl rsa -in aux-fixed/dkim/dkim.private -pubout -outform DER | od -t x1 | head;
+openssl rsa -in aux-fixed/dkim/dkim.private -pubout | openssl asn1parse -dump;
+openssl rsa -in aux-fixed/dkim/dkim.private -pubout | openssl asn1parse -dump -offset 22;
+*/
+
+/* sequence; just move past the header */
+if ((rc = as_tag(pubkey, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, NULL))
+ != ASN1_SUCCESS) goto asn_err;
+
+/* sequence; skip the entire thing */
+DEBUG(D_acl) stage = US"S2";
+if ((rc = as_tag(pubkey, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, &alen))
+ != ASN1_SUCCESS) goto asn_err;
+pubkey->data += alen; pubkey->len -= alen;
+
+
+/* bitstring: limit range to size of bitstring;
+move over header + content wrapper */
+DEBUG(D_acl) stage = US"BS";
+if ((rc = as_tag(pubkey, 0, ASN1_TAG_BIT_STRING, &alen)) != ASN1_SUCCESS)
+ goto asn_err;
+pubkey->len = alen;
+pubkey->data++; pubkey->len--;
+
+/* sequence; just move past the header */
+DEBUG(D_acl) stage = US"S3";
+if ((rc = as_tag(pubkey, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, NULL))
+ != ASN1_SUCCESS) goto asn_err;
+
+/* read two integers */
+DEBUG(D_acl) stage = US"MPI";
+nbits = pubkey->len;
+if ((errstr = as_mpi(pubkey, &verify_ctx->n))) return errstr;
+nbits = (nbits - pubkey->len) * 8;
+if ((errstr = as_mpi(pubkey, &verify_ctx->e))) return errstr;
+
+#ifdef extreme_debug
+DEBUG(D_acl) debug_printf_indent("rsa_verify_init:\n");
+ {
+ uschar * s;
+ gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, verify_ctx->n);
+ debug_printf_indent(" N : %s\n", s);
+ gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, verify_ctx->e);
+ debug_printf_indent(" E : %s\n", s);
+ }
+
+#endif
+if (bits) *bits = nbits;
+return NULL;
+
+asn_err:
+DEBUG(D_acl) return string_sprintf("%s: %s", stage, asn1_strerror(rc));
+ return US asn1_strerror(rc);
+}
+
+
+/* verify signature (of hash)
+XXX though we appear to be doing a hash, too!
+(given pubkey & alleged sig)
+Return: NULL for success, or an error string */
+
+const uschar *
+exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, blob * data_hash, blob * sig)
+{
+/*
+cf. libgnutls 2.8.5 _wrap_gcry_pk_verify()
+*/
+char * sexp_hash;
+gcry_mpi_t m_sig;
+gcry_sexp_t s_sig = NULL, s_hash = NULL, s_pkey = NULL;
+gcry_error_t gerr;
+uschar * stage;
+
+/*XXX needs extension for SHA512 */
+switch (hash)
+ {
+ case HASH_SHA1: sexp_hash = "(data(flags pkcs1)(hash sha1 %b))"; break;
+ case HASH_SHA2_256: sexp_hash = "(data(flags pkcs1)(hash sha256 %b))"; break;
+ default: return US"nonhandled hash type";
+ }
+
+if ( (stage = US"pkey sexp build",
+ gerr = gcry_sexp_build (&s_pkey, NULL, "(public-key(rsa(n%m)(e%m)))",
+ verify_ctx->n, verify_ctx->e))
+ || (stage = US"data sexp build",
+ gerr = gcry_sexp_build (&s_hash, NULL, sexp_hash,
+ (int) data_hash->len, CS data_hash->data))
+ || (stage = US"sig mpi scan",
+ gerr = gcry_mpi_scan(&m_sig, GCRYMPI_FMT_USG, sig->data, sig->len, NULL))
+ || (stage = US"sig sexp build",
+ gerr = gcry_sexp_build (&s_sig, NULL, "(sig-val(rsa(s%m)))", m_sig))
+ || (stage = US"verify",
+ gerr = gcry_pk_verify (s_sig, s_hash, s_pkey))
+ )
+ {
+ DEBUG(D_acl) debug_printf_indent("verify: error in stage '%s'\n", stage);
+ return US gcry_strerror(gerr);
+ }
+
+if (s_sig) gcry_sexp_release (s_sig);
+if (s_hash) gcry_sexp_release (s_hash);
+if (s_pkey) gcry_sexp_release (s_pkey);
+gcry_mpi_release (m_sig);
+gcry_mpi_release (verify_ctx->n);
+gcry_mpi_release (verify_ctx->e);
+
+return NULL;
+}
+
+
+
+
+#elif defined(SIGN_OPENSSL)
+/******************************************************************************/
+
+void
+exim_dkim_init(void)
+{
+ERR_load_crypto_strings();
+}
+
+
+/* accumulate data (was gnutls-only but now needed for OpenSSL non-EC too
+because now using hash-and-sign interface) */
+gstring *
+exim_dkim_data_append(gstring * g, uschar * s)
+{
+return string_cat(g, s);
+}
+
+
+/* import private key from PEM string in memory.
+Return: NULL for success, or an error string */
+
+const uschar *
+exim_dkim_signing_init(const uschar * privkey_pem, es_ctx * sign_ctx)
+{
+BIO * bp = BIO_new_mem_buf((void *)privkey_pem, -1);
+
+if (!(sign_ctx->key = PEM_read_bio_PrivateKey(bp, NULL, NULL, NULL)))
+ return string_sprintf("privkey PEM-block import: %s",
+ ERR_error_string(ERR_get_error(), NULL));
+
+sign_ctx->keytype =
+#ifdef SIGN_HAVE_ED25519
+ EVP_PKEY_type(EVP_PKEY_id(sign_ctx->key)) == EVP_PKEY_ED25519
+ ? KEYTYPE_ED25519 : KEYTYPE_RSA;
+#else
+ KEYTYPE_RSA;
+#endif
+return NULL;
+}
+
+
+
+/* allocate mem for signature (when signing) */
+/* hash & sign data. Incremental not supported.
+
+Return: NULL for success with the signaature in the sig blob, or an error string */
+
+const uschar *
+exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, blob * data, blob * sig)
+{
+const EVP_MD * md;
+EVP_MD_CTX * ctx;
+size_t siglen;
+
+switch (hash)
+ {
+ case HASH_NULL: md = NULL; break; /* Ed25519 signing */
+ case HASH_SHA1: md = EVP_sha1(); break;
+ case HASH_SHA2_256: md = EVP_sha256(); break;
+ case HASH_SHA2_512: md = EVP_sha512(); break;
+ default: return US"nonhandled hash type";
+ }
+
+#ifdef SIGN_HAVE_ED25519
+if ( (ctx = EVP_MD_CTX_new())
+ && EVP_DigestSignInit(ctx, NULL, md, NULL, sign_ctx->key) > 0
+ && EVP_DigestSign(ctx, NULL, &siglen, NULL, 0) > 0
+ && (sig->data = store_get(siglen, GET_UNTAINTED))
+
+ /* Obtain the signature (slen could change here!) */
+ && EVP_DigestSign(ctx, sig->data, &siglen, data->data, data->len) > 0
+ )
+ {
+ EVP_MD_CTX_destroy(ctx);
+ sig->len = siglen;
+ return NULL;
+ }
+#else
+/*XXX renamed to EVP_MD_CTX_new() in 1.1.0 */
+if ( (ctx = EVP_MD_CTX_create())
+ && EVP_DigestSignInit(ctx, NULL, md, NULL, sign_ctx->key) > 0
+ && EVP_DigestSignUpdate(ctx, data->data, data->len) > 0
+ && EVP_DigestSignFinal(ctx, NULL, &siglen) > 0
+ && (sig->data = store_get(siglen, GET_UNTAINTED))
+
+ /* Obtain the signature (slen could change here!) */
+ && EVP_DigestSignFinal(ctx, sig->data, &siglen) > 0
+ )
+ {
+ EVP_MD_CTX_destroy(ctx);
+ sig->len = siglen;
+ return NULL;
+ }
+#endif
+
+if (ctx) EVP_MD_CTX_destroy(ctx);
+return US ERR_error_string(ERR_get_error(), NULL);
+}
+
+
+
+/* import public key (from blob in memory)
+Return: NULL for success, or an error string */
+
+const uschar *
+exim_dkim_verify_init(blob * pubkey, keyformat fmt, ev_ctx * verify_ctx,
+ unsigned * bits)
+{
+const uschar * s = pubkey->data;
+uschar * ret = NULL;
+
+switch(fmt)
+ {
+ case KEYFMT_DER:
+ /*XXX hmm, we never free this */
+ if (!(verify_ctx->key = d2i_PUBKEY(NULL, &s, pubkey->len)))
+ ret = US ERR_error_string(ERR_get_error(), NULL);
+ break;
+#ifdef SIGN_HAVE_ED25519
+ case KEYFMT_ED25519_BARE:
+ if (!(verify_ctx->key = EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL,
+ s, pubkey->len)))
+ ret = US ERR_error_string(ERR_get_error(), NULL);
+ break;
+#endif
+ default:
+ ret = US"pubkey format not handled";
+ break;
+ }
+
+if (!ret && bits) *bits = EVP_PKEY_bits(verify_ctx->key);
+return ret;
+}
+
+
+
+
+/* verify signature (of hash, except Ed25519 where of-data)
+(given pubkey & alleged sig)
+Return: NULL for success, or an error string */
+
+const uschar *
+exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, blob * data, blob * sig)
+{
+const EVP_MD * md;
+
+switch (hash)
+ {
+ case HASH_NULL: md = NULL; break;
+ case HASH_SHA1: md = EVP_sha1(); break;
+ case HASH_SHA2_256: md = EVP_sha256(); break;
+ case HASH_SHA2_512: md = EVP_sha512(); break;
+ default: return US"nonhandled hash type";
+ }
+
+#ifdef SIGN_HAVE_ED25519
+if (!md)
+ {
+ EVP_MD_CTX * ctx;
+
+ if ((ctx = EVP_MD_CTX_new()))
+ {
+ if ( EVP_DigestVerifyInit(ctx, NULL, md, NULL, verify_ctx->key) > 0
+ && EVP_DigestVerify(ctx, sig->data, sig->len, data->data, data->len) > 0
+ )
+ { EVP_MD_CTX_free(ctx); return NULL; }
+ EVP_MD_CTX_free(ctx);
+ }
+ }
+else
+#endif
+ {
+ EVP_PKEY_CTX * ctx;
+
+ if ((ctx = EVP_PKEY_CTX_new(verify_ctx->key, NULL)))
+ {
+ if ( EVP_PKEY_verify_init(ctx) > 0
+ && EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) > 0
+ && EVP_PKEY_CTX_set_signature_md(ctx, md) > 0
+ && EVP_PKEY_verify(ctx, sig->data, sig->len,
+ data->data, data->len) == 1
+ )
+ { EVP_PKEY_CTX_free(ctx); return NULL; }
+ EVP_PKEY_CTX_free(ctx);
+
+ DEBUG(D_tls)
+ if (Ustrcmp(ERR_reason_error_string(ERR_peek_error()), "wrong signature length") == 0)
+ debug_printf("sig len (from msg hdr): %d, expected (from dns pubkey) %d\n",
+ (int) sig->len, EVP_PKEY_size(verify_ctx->key));
+ }
+ }
+
+return US ERR_error_string(ERR_get_error(), NULL);
+}
+
+
+
+#endif
+/******************************************************************************/
+
+#endif /*DISABLE_DKIM*/
+#endif /*MACRO_PREDEF*/
+/* End of File */
diff --git a/src/pdkim/signing.h b/src/pdkim/signing.h
new file mode 100644
index 0000000..ed6f397
--- /dev/null
+++ b/src/pdkim/signing.h
@@ -0,0 +1,97 @@
+/*
+ * PDKIM - a RFC4871 (DKIM) implementation
+ *
+ * Copyright (C) 1995 - 2020 Exim maintainers
+ *
+ * RSA signing/verification interface
+ */
+
+#include "../exim.h"
+
+#ifndef DISABLE_DKIM /* entire file */
+
+#include "crypt_ver.h"
+
+#ifdef SIGN_OPENSSL
+# include <openssl/rsa.h>
+# include <openssl/ssl.h>
+# include <openssl/err.h>
+#elif defined(SIGN_GNUTLS)
+# include <gnutls/gnutls.h>
+# include <gnutls/x509.h>
+# include <gnutls/abstract.h>
+#elif defined(SIGN_GCRYPT)
+# include <gcrypt.h>
+# include <libtasn1.h>
+#endif
+
+#include "../blob.h"
+
+typedef enum {
+ KEYTYPE_RSA,
+ KEYTYPE_ED25519
+} keytype;
+
+typedef enum {
+ KEYFMT_DER, /* an asn.1 structure */
+ KEYFMT_ED25519_BARE /* just the key */
+} keyformat;
+
+
+#ifdef SIGN_OPENSSL
+
+typedef struct {
+ keytype keytype;
+ EVP_PKEY * key;
+} es_ctx;
+
+typedef struct {
+ keytype keytype;
+ EVP_PKEY * key;
+} ev_ctx;
+
+#elif defined(SIGN_GNUTLS)
+
+typedef struct {
+ keytype keytype;
+ gnutls_privkey_t key;
+} es_ctx;
+
+typedef struct {
+ keytype keytype;
+ gnutls_pubkey_t key;
+} ev_ctx;
+
+#elif defined(SIGN_GCRYPT)
+
+typedef struct {
+ keytype keytype;
+ gcry_mpi_t n;
+ gcry_mpi_t e;
+ gcry_mpi_t d;
+ gcry_mpi_t p;
+ gcry_mpi_t q;
+ gcry_mpi_t dp;
+ gcry_mpi_t dq;
+ gcry_mpi_t qp;
+} es_ctx;
+
+typedef struct {
+ keytype keytype;
+ gcry_mpi_t n;
+ gcry_mpi_t e;
+} ev_ctx;
+
+#endif
+
+
+extern void exim_dkim_init(void);
+extern gstring * exim_dkim_data_append(gstring *, uschar *);
+
+extern const uschar * exim_dkim_signing_init(const uschar *, es_ctx *);
+extern const uschar * exim_dkim_sign(es_ctx *, hashmethod, blob *, blob *);
+extern const uschar * exim_dkim_verify_init(blob *, keyformat, ev_ctx *, unsigned *);
+extern const uschar * exim_dkim_verify(ev_ctx *, hashmethod, blob *, blob *);
+
+#endif /*DISABLE_DKIM*/
+/* End of File */
diff --git a/src/perl.c b/src/perl.c
new file mode 100644
index 0000000..f07ee2e
--- /dev/null
+++ b/src/perl.c
@@ -0,0 +1,201 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 1999 - 2022 */
+/* 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 <assert.h>
+
+#define HINTSDB_H
+#define DBFUNCTIONS_H
+
+#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), CCS str);
+ else if (!f.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 = 1;
+ static char *argv[4] = { "exim-perl" };
+ SV *sv;
+ STRLEN len;
+
+ if (opt_perl_taintmode) argv[argc++] = "-T";
+ argv[argc++] = "/dev/null";
+ argv[argc] = 0;
+
+ assert(sizeof(argv)/sizeof(argv[0]) > argc);
+
+ 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;
+}
+
+gstring *
+call_perl_cat(gstring * yield, 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_catn(yield, str, (int)len);
+ FREETMPS;
+ LEAVE;
+
+ setlocale(LC_ALL, "C"); /* In case it got changed */
+ return yield;
+}
+
+/* End of perl.c */
diff --git a/src/priv.c b/src/priv.c
new file mode 100644
index 0000000..94d4254
--- /dev/null
+++ b/src/priv.c
@@ -0,0 +1,76 @@
+#include "exim.h"
+#include <sys/types.h>
+#include <unistd.h>
+#include <string.h>
+
+static enum {
+ PRIV_DROPPING, PRIV_DROPPED,
+ PRIV_RESTORING, PRIV_RESTORED
+} priv_state = PRIV_RESTORED;
+
+
+static uid_t priv_euid;
+static gid_t priv_egid;
+static gid_t priv_groups[EXIM_GROUPLIST_SIZE + 1];
+static int priv_ngroups;
+
+/* Inspired by OpenSSH's temporarily_use_uid(). Thanks! */
+
+void
+priv_drop_temp(const uid_t temp_uid, const gid_t temp_gid)
+{
+if (priv_state != PRIV_RESTORED)
+ log_write(0, LOG_PANIC_DIE, "priv_drop_temp: unexpected priv_state %d != %d", priv_state, PRIV_RESTORED);
+
+priv_state = PRIV_DROPPING;
+
+priv_euid = geteuid();
+if (priv_euid == root_uid)
+ {
+ priv_egid = getegid();
+ priv_ngroups = getgroups(nelem(priv_groups), priv_groups);
+ if (priv_ngroups < 0)
+ log_write(0, LOG_PANIC_DIE, "getgroups: %s", strerror(errno));
+
+ if (priv_ngroups > 0 && setgroups(1, &temp_gid) != 0)
+ log_write(0, LOG_PANIC_DIE, "setgroups: %s", strerror(errno));
+ if (setegid(temp_gid) != 0)
+ log_write(0, LOG_PANIC_DIE, "setegid(%d): %s", temp_gid, strerror(errno));
+ if (seteuid(temp_uid) != 0)
+ log_write(0, LOG_PANIC_DIE, "seteuid(%d): %s", temp_uid, strerror(errno));
+
+ if (geteuid() != temp_uid)
+ log_write(0, LOG_PANIC_DIE, "getdeuid() != %d", temp_uid);
+ if (getegid() != temp_gid)
+ log_write(0, LOG_PANIC_DIE, "getegid() != %d", temp_gid);
+ }
+
+priv_state = PRIV_DROPPED;
+}
+
+/* Inspired by OpenSSH's restore_uid(). Thanks! */
+
+void
+priv_restore(void)
+{
+if (priv_state != PRIV_DROPPED)
+ log_write(0, LOG_PANIC_DIE, "priv_restore: unexpected priv_state %d != %d", priv_state, PRIV_DROPPED);
+priv_state = PRIV_RESTORING;
+
+if (priv_euid == root_uid)
+ {
+ if (seteuid(priv_euid) != 0)
+ log_write(0, LOG_PANIC_DIE, "seteuid(%d): %s", priv_euid, strerror(errno));
+ if (setegid(priv_egid) != 0)
+ log_write(0, LOG_PANIC_DIE, "setegid(%d): %s", priv_egid, strerror(errno));
+ if (priv_ngroups > 0 && setgroups(priv_ngroups, priv_groups) != 0)
+ log_write(0, LOG_PANIC_DIE, "setgroups: %s", strerror(errno));
+
+ if (geteuid() != priv_euid)
+ log_write(0, LOG_PANIC_DIE, "getdeuid() != %d", priv_euid);
+ if (getegid() != priv_egid)
+ log_write(0, LOG_PANIC_DIE, "getdegid() != %d", priv_egid);
+ }
+
+priv_state = PRIV_RESTORED;
+}
diff --git a/src/queue.c b/src/queue.c
new file mode 100644
index 0000000..4bdd6fb
--- /dev/null
+++ b/src/queue.c
@@ -0,0 +1,1588 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions that operate on the input queue. */
+
+
+#include "exim.h"
+
+
+
+
+
+
+
+#ifndef COMPILE_UTILITY
+
+/* 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
+
+
+#ifndef DISABLE_TLS
+static BOOL queue_tls_init = FALSE;
+#endif
+
+/*************************************************
+* 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 && b)
+ {
+ int d;
+ if ((d = Ustrncmp(a->text, b->text, 6)) == 0)
+ d = Ustrcmp(a->text + 14, b->text + 14);
+ if (d < 0)
+ {
+ *append = a;
+ append= &a->next;
+ a = a->next;
+ }
+ else
+ {
+ *append = b;
+ append= &b->next;
+ b = b->next;
+ }
+ }
+
+*append = a ? 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
+ pcount If not NULL, fill in with count of files and do not return list
+
+Returns: pointer to a chain of queue name items
+*/
+
+static queue_filename *
+queue_get_spool_list(int subdiroffset, uschar *subdirs, int *subcount,
+ BOOL randomize, unsigned * pcount)
+{
+int i;
+int flags = 0;
+int resetflags = -1;
+int subptr;
+queue_filename *yield = NULL;
+queue_filename *last = NULL;
+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 (pcount)
+ *pcount = 0;
+else 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. */
+
+spool_pname_buf(buffer, sizeof(buffer));
+buffer[sizeof(buffer) - 3] = 0;
+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 */
+ DIR *dd;
+
+ if (subdirchar != 0)
+ {
+ buffer[subptr] = '/';
+ buffer[subptr+1] = subdirchar;
+ }
+
+ DEBUG(D_queue_run) debug_printf("looking in %s\n", buffer);
+ if (!(dd = exim_opendir(buffer)))
+ continue;
+
+ /* Now scan the directory. */
+
+ for (struct dirent *ent; ent = readdir(dd); )
+ {
+ 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)
+ if (pcount)
+ (*pcount)++;
+ else
+ {
+ queue_filename * next =
+ store_get(sizeof(queue_filename) + Ustrlen(name), 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)
+ {
+ 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
+ {
+ next->next = NULL;
+ for (int j = 0; j < LOG2_MAXNODES; j++)
+ if (root[j])
+ {
+ 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)
+ {
+ uschar subdir[2];
+
+ rmdir(CS buffer);
+ subdir[0] = subdirchar; subdir[1] = 0;
+ rmdir(CS spool_dname(US"msglog", subdir));
+ }
+ 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 (!pcount && !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 = f.queue_run_force || deliver_selectstring != NULL ||
+ deliver_selectstring_sender != NULL;
+const pcre2_code *selectstring_regex = NULL;
+const pcre2_code *selectstring_regex_sender = NULL;
+uschar *log_detail = NULL;
+int subcount = 0;
+uschar subdirs[64];
+pid_t qpid[4] = {0}; /* Parallelism factor for q2stage 1st phase */
+BOOL single_id = FALSE;
+
+#ifdef MEASURE_TIMING
+report_time_since(&timestamp_startup, US"queue_run start");
+#endif
+
+/* 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;
+f.queue_smtp = f.queue_2stage;
+
+queue_run_pid = getpid();
+f.queue_running = TRUE;
+
+/* Log the true start of a queue run, and fancy options */
+
+if (!recurse)
+ {
+ uschar extras[8];
+ uschar *p = extras;
+
+ if (f.queue_2stage) *p++ = 'q';
+ if (f.queue_run_first_delivery) *p++ = 'i';
+ if (f.queue_run_force) *p++ = 'f';
+ if (f.deliver_force_thaw) *p++ = 'f';
+ if (f.queue_run_local) *p++ = 'l';
+ *p = 0;
+
+ p = big_buffer;
+ p += sprintf(CS p, "pid=%d", (int)queue_run_pid);
+
+ if (extras[0] != 0)
+ p += sprintf(CS p, " -q%s", extras);
+
+ if (deliver_selectstring)
+ {
+ snprintf(CS p, big_buffer_size - (p - big_buffer), " -R%s %s",
+ f.deliver_selectstring_regex? "r" : "", deliver_selectstring);
+ p += Ustrlen(CCS p);
+ }
+
+ if (deliver_selectstring_sender)
+ {
+ snprintf(CS p, big_buffer_size - (p - big_buffer), " -S%s %s",
+ f.deliver_selectstring_sender_regex? "r" : "", deliver_selectstring_sender);
+ p += Ustrlen(CCS p);
+ }
+
+ log_detail = string_copy(big_buffer);
+ if (*queue_name)
+ log_write(L_queue_run, LOG_MAIN, "Start '%s' queue run: %s",
+ queue_name, log_detail);
+ else
+ log_write(L_queue_run, LOG_MAIN, "Start queue run: %s", log_detail);
+
+ single_id = start_id && stop_id && !f.queue_2stage
+ && Ustrcmp(start_id, stop_id) == 0;
+ }
+
+/* If deliver_selectstring is a regex, compile it. */
+
+if (deliver_selectstring && f.deliver_selectstring_regex)
+ selectstring_regex = regex_must_compile(deliver_selectstring, TRUE, FALSE);
+
+if (deliver_selectstring_sender && f.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
+subsequent 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 (int i = queue_run_in_order ? -1 : 0;
+ i <= (queue_run_in_order ? -1 : subcount);
+ i++)
+ {
+ rmark reset_point1 = store_mark();
+
+ 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 (queue_filename * fq = queue_get_spool_list(i, subdirs, &subcount,
+ !queue_run_in_order, NULL);
+ fq; fq = fq->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 (!f.queue_run_force && deliver_queue_load_max >= 0)
+ if ((load_average = os_getloadavg()) > 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);
+
+ /* If initial of a 2-phase run, maintain a set of child procs
+ to get disk parallelism */
+
+ if (f.queue_2stage && !queue_run_in_order)
+ {
+ int i;
+ if (qpid[f.running_in_test_harness ? 0 : nelem(qpid) - 1])
+ {
+ DEBUG(D_queue_run) debug_printf("q2stage waiting for child %d\n", (int)qpid[0]);
+ waitpid(qpid[0], NULL, 0);
+ DEBUG(D_queue_run) debug_printf("q2stage reaped child %d\n", (int)qpid[0]);
+ if (f.running_in_test_harness) i = 0;
+ else for (i = 0; i < nelem(qpid) - 1; i++) qpid[i] = qpid[i+1];
+ qpid[i] = 0;
+ }
+ else
+ for (i = 0; qpid[i]; ) i++;
+ if ((qpid[i] = exim_fork(US"qrun-phase-one")))
+ continue; /* parent loops around */
+ }
+
+ /* Skip this message unless it's within the ID limits */
+
+ if (stop_id && Ustrncmp(fq->text, stop_id, MESSAGE_ID_LENGTH) > 0)
+ goto go_around;
+ if (start_id && Ustrncmp(fq->text, start_id, MESSAGE_ID_LENGTH) < 0)
+ goto go_around;
+
+ /* Check that the message still exists */
+
+ message_subdir[0] = fq->dir_uschar;
+ if (Ustat(spool_fname(US"input", message_subdir, fq->text, US""), &statbuf) < 0)
+ goto go_around;
+
+ /* 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 || deliver_selectstring_sender ||
+ f.queue_run_first_delivery)
+ {
+ BOOL wanted = TRUE;
+ BOOL orig_dont_deliver = f.dont_deliver;
+ rmark reset_point2 = store_mark();
+
+ /* 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(fq->text, FALSE, TRUE) != spool_read_OK) goto go_around;
+ f.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 (f.deliver_freeze && !f.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 (f.queue_run_first_delivery && !f.deliver_firsttime)
+ {
+ DEBUG(D_queue_run) debug_printf("%s: not first delivery\n", fq->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
+ && !(f.deliver_selectstring_sender_regex
+ ? regex_match(selectstring_regex_sender, sender_address, -1, NULL)
+ : (strstric(sender_address, deliver_selectstring_sender, FALSE)
+ != NULL)
+ ) )
+ {
+ DEBUG(D_queue_run) debug_printf("%s: sender address did not match %s\n",
+ fq->text, deliver_selectstring_sender);
+ wanted = FALSE;
+ }
+
+ /* Recipient matching */
+
+ else if (deliver_selectstring)
+ {
+ int i;
+ for (i = 0; i < recipients_count; i++)
+ {
+ uschar *address = recipients_list[i].address;
+ if ( (f.deliver_selectstring_regex
+ ? regex_match(selectstring_regex, address, -1, NULL)
+ : (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",
+ fq->text, deliver_selectstring);
+ wanted = FALSE;
+ }
+ }
+
+ /* Recover store used when reading the header */
+
+ spool_clear_header_globals();
+ store_reset(reset_point2);
+ if (!wanted) goto go_around; /* 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", fq->text);
+ fq->text[SPOOL_NAME_LENGTH-2] = 0;
+#ifdef MEASURE_TIMING
+ report_time_since(&timestamp_startup, US"queue msg selected");
+#endif
+
+#ifndef DISABLE_TLS
+ if (!queue_tls_init)
+ {
+ queue_tls_init = TRUE;
+ /* Preload TLS library info for smtp transports. Once, and only if we
+ have a delivery to do. */
+ tls_client_creds_reload(FALSE);
+ }
+#endif
+
+single_item_retry:
+ if ((pid = exim_fork(US"qrun-delivery")) == 0)
+ {
+ int rc;
+ (void)close(pfd[pipe_read]);
+ rc = deliver_message(fq->text, force_delivery, FALSE);
+ exim_underbar_exit(rc == DELIVER_NOT_ATTEMPTED
+ ? EXIT_FAILURE : EXIT_SUCCESS);
+ }
+ 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)", fq->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)) force_delivery = f.queue_run_force;
+
+ /* If the process crashed, tell somebody */
+
+ else if (status & 0x00ff)
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "queue run: process %d crashed with signal %d while delivering %s",
+ (int)pid, status & 0x00ff, fq->text);
+
+ /* If single-item delivery was untried (likely due to locking)
+ retry once after a delay */
+
+ if (status & 0xff00 && single_id)
+ {
+ single_id = FALSE;
+ DEBUG(D_queue_run) debug_printf("qrun single-item pause before retry\n");
+ millisleep(500);
+ DEBUG(D_queue_run) debug_printf("qrun single-item retry after pause\n");
+ goto single_item_retry;
+ }
+
+ /* 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 ((status = read(pfd[pipe_read], buffer, sizeof(buffer))) != 0)
+ log_write(0, LOG_MAIN|LOG_PANIC, status > 0 ?
+ "queue run: unexpected data on pipe" : "queue run: error on pipe: %s",
+ strerror(errno));
+ (void)close(pfd[pipe_read]);
+ set_process_info("running queue");
+
+ /* If initial of a 2-phase run, we are a child - so just exit */
+ if (f.queue_2stage && !queue_run_in_order)
+ exim_exit(EXIT_SUCCESS);
+
+ /* If we are in the test harness, and this is not the first of a 2-stage
+ queue run, update fudged queue times. */
+
+ if (f.running_in_test_harness && !f.queue_2stage)
+ {
+ uschar * fqtnext = Ustrchr(fudged_queue_times, '/');
+ if (fqtnext) fudged_queue_times = fqtnext + 1;
+ }
+
+
+ continue;
+
+ go_around:
+ /* If initial of a 2-phase run, we are a child - so just exit */
+ if (f.queue_2stage && !queue_run_in_order)
+ exim_exit(EXIT_SUCCESS);
+ } /* End loop for list of messages */
+
+ tree_nonrecipients = NULL;
+ 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)
+ for (int j = 1; j <= subcount; j++)
+ {
+ int r;
+ if ((r = random_number(100)) >= 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 (f.queue_2stage)
+ {
+
+ /* wait for last children */
+ for (int i = 0; i < nelem(qpid); i++)
+ if (qpid[i])
+ {
+ DEBUG(D_queue_run) debug_printf("q2stage reaped child %d\n", (int)qpid[i]);
+ waitpid(qpid[i], NULL, 0);
+ }
+ else break;
+
+#ifdef MEASURE_TIMING
+ report_time_since(&timestamp_startup, US"queue_run 1st phase done");
+#endif
+ f.queue_2stage = FALSE;
+ queue_run(start_id, stop_id, TRUE);
+ }
+
+/* At top level, log the end of the run. */
+
+if (!recurse)
+ if (*queue_name)
+ log_write(L_queue_run, LOG_MAIN, "End '%s' queue run: %s",
+ queue_name, log_detail);
+ else
+ 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: count
+*/
+
+unsigned
+queue_count(void)
+{
+int subcount;
+unsigned count = 0;
+uschar subdirs[64];
+
+(void) queue_get_spool_list(-1, /* entire queue */
+ subdirs, /* for holding sub list */
+ &subcount, /* for subcount */
+ FALSE, /* not random */
+ &count); /* just get the count */
+return count;
+}
+
+
+#define QUEUE_SIZE_AGE 60 /* update rate for queue_size */
+
+unsigned
+queue_count_cached(void)
+{
+time_t now;
+if ((now = time(NULL)) >= queue_size_next)
+ {
+ queue_size = queue_count();
+ queue_size_next = now + (f.running_in_test_harness ? 3 : QUEUE_SIZE_AGE);
+ }
+return queue_size;
+}
+
+/************************************************
+* 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) queue_list_extras(p->left);
+if (!p->data.val) printf(" +D %s\n", p->name);
+if (p->right) 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 subcount;
+int now = (int)time(NULL);
+rmark reset_point;
+queue_filename * qf = NULL;
+uschar subdirs[64];
+
+/* If given a list of messages, build a chain containing their ids. */
+
+if (count > 0)
+ {
+ queue_filename *last = NULL;
+ for (int i = 0; i < count; i++)
+ {
+ queue_filename * next =
+ store_get(sizeof(queue_filename) + Ustrlen(list[i]) + 2, list[i]);
+ sprintf(CS next->text, "%s-H", list[i]);
+ next->dir_uschar = '*';
+ next->next = NULL;
+ if (i == 0) qf = next; else last->next = next;
+ last = next;
+ }
+ }
+
+/* Otherwise get a list of the entire queue, in order if necessary. */
+
+else
+ qf = queue_get_spool_list(
+ -1, /* entire queue */
+ subdirs, /* for holding sub list */
+ &subcount, /* for subcount */
+ option >= 8, /* randomize if required */
+ NULL); /* don't just count */
+
+if (option >= 8) option -= 8;
+
+/* Now scan the chain and print information, resetting store used
+each time. */
+
+for (;
+ qf && (reset_point = store_mark());
+ spool_clear_header_globals(), store_reset(reset_point), qf = qf->next
+ )
+ {
+ int rc, save_errno;
+ int size = 0;
+ BOOL env_read;
+
+ message_size = 0;
+ message_subdir[0] = qf->dir_uschar;
+ rc = spool_read_header(qf->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 i, ptr;
+ FILE *jread;
+ struct stat statbuf;
+ uschar * fname = spool_fname(US"input", message_subdir, qf->text, US"");
+
+ ptr = Ustrlen(fname)-1;
+ fname[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(fname, &statbuf) == 0)
+ size = message_size + statbuf.st_size - SPOOL_DATA_START_OFFSET + 1;
+ i = (now - received_time.tv_sec)/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 */
+
+ fname[ptr] = 'J';
+ if ((jread = Ufopen(fname, "rb")))
+ {
+ 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 (int i = 0; i < 16; i++) fputc(qf->text[i], stdout);
+
+ if (env_read && sender_address)
+ {
+ printf(" <%s>", sender_address);
+ if (f.sender_set_untrusted) printf(" (%s)", originator_login);
+ }
+
+ if (rc != spool_read_OK)
+ {
+ printf("\n ");
+ if (save_errno == ERRNO_SPOOLFORMAT)
+ {
+ struct stat statbuf;
+ uschar * fname = spool_fname(US"input", message_subdir, qf->text, US"");
+
+ if (Ustat(fname, &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 (f.deliver_freeze) printf(" *** frozen ***");
+
+ printf("\n");
+
+ if (recipients_list)
+ {
+ for (int 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 ? "D" : " ", recipients_list[i].address);
+ if (delivered) delivered->data.val = TRUE;
+ }
+ if (option == 2 && tree_nonrecipients)
+ 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)
+{
+BOOL yield = TRUE;
+BOOL removed = FALSE;
+struct passwd *pw;
+uschar *doing = NULL;
+uschar *username;
+uschar *errmsg;
+uschar spoolname[32];
+
+/* 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, rc;
+ uschar *subdirectory, *suffix;
+
+ if (!f.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 (int i = 0; i < 2; i++)
+ {
+ set_subdir_str(message_subdir, id, i);
+ if ((fd = Uopen(spool_fname(subdirectory, message_subdir, id, suffix),
+ O_RDONLY, 0)) >= 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 ((deliver_datafile = spool_open_datafile(id)) < 0)
+ if (errno == ENOENT)
+ {
+ yield = FALSE;
+ printf("Spool data file for %s does not exist\n", id);
+ if (action != MSG_REMOVE || !f.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 || !f.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 (!f.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:
+ {
+ transport_ctx tctx = {{0}};
+ deliver_in_buffer = store_malloc(DELIVER_IN_BUFFER_SIZE);
+ deliver_out_buffer = store_malloc(DELIVER_OUT_BUFFER_SIZE);
+ tctx.u.fd = 1;
+ (void) transport_write_message(&tctx, 0);
+ break;
+ }
+
+
+ case MSG_FREEZE:
+ if (f.deliver_freeze)
+ {
+ yield = FALSE;
+ printf("is already frozen\n");
+ }
+ else
+ {
+ f.deliver_freeze = TRUE;
+ f.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 (!f.deliver_freeze)
+ {
+ yield = FALSE;
+ printf("is not frozen\n");
+ }
+ else
+ {
+ f.deliver_freeze = FALSE;
+ f.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:
+ {
+ uschar suffix[3];
+
+ suffix[0] = '-';
+ suffix[2] = 0;
+ message_subdir[0] = id[5];
+
+ for (int j = 0; j < 2; message_subdir[0] = 0, j++)
+ {
+ uschar * fname = spool_fname(US"msglog", message_subdir, id, US"");
+
+ DEBUG(D_any) debug_printf(" removing %s", fname);
+ if (Uunlink(fname) < 0)
+ {
+ if (errno != ENOENT)
+ {
+ yield = FALSE;
+ printf("Error while removing %s: %s\n", fname, strerror(errno));
+ }
+ else DEBUG(D_any) debug_printf(" (no file)\n");
+ }
+ else
+ {
+ removed = TRUE;
+ DEBUG(D_any) debug_printf(" (ok)\n");
+ }
+
+ for (int i = 0; i < 3; i++)
+ {
+ uschar * fname;
+
+ suffix[1] = (US"DHJ")[i];
+ fname = spool_fname(US"input", message_subdir, id, suffix);
+
+ DEBUG(D_any) debug_printf(" removing %s", fname);
+ if (Uunlink(fname) < 0)
+ {
+ if (errno != ENOENT)
+ {
+ yield = FALSE;
+ printf("Error while removing %s: %s\n", fname, strerror(errno));
+ }
+ else DEBUG(D_any) debug_printf(" (no file)\n");
+ }
+ else
+ {
+ removed = TRUE;
+ DEBUG(D_any) debug_printf(" (done)\n");
+ }
+ }
+ }
+
+ /* 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)
+ {
+#ifndef DISABLE_EVENT
+ if (event_action) for (int i = 0; i < recipients_count; i++)
+ {
+ tree_node *delivered =
+ tree_search(tree_nonrecipients, recipients_list[i].address);
+ if (!delivered)
+ {
+ uschar * save_local = deliver_localpart;
+ const uschar * save_domain = deliver_domain;
+ uschar * addr = recipients_list[i].address, * errmsg = NULL;
+ int start, end, dom;
+
+ if (!parse_extract_address(addr, &errmsg, &start, &end, &dom, TRUE))
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "failed to parse address '%.100s'\n: %s", addr, errmsg);
+ else
+ {
+ deliver_localpart =
+ string_copyn(addr+start, dom ? (dom-1) - start : end - start);
+ deliver_domain = dom
+ ? CUS string_copyn(addr+dom, end - dom) : CUS"";
+
+ (void) event_raise(event_action, US"msg:fail:internal",
+ string_sprintf("message removed by %s", username), NULL);
+
+ deliver_localpart = save_local;
+ deliver_domain = save_domain;
+ }
+ }
+ }
+ (void) event_raise(event_action, US"msg:complete", NULL, NULL);
+#endif
+ log_write(0, LOG_MAIN, "removed by %s", username);
+ log_write(0, LOG_MAIN, "Completed");
+ }
+ break;
+ }
+
+
+ case MSG_SETQUEUE:
+ /* The global "queue_name_dest" is used as destination, "queue_name"
+ as source */
+
+ spool_move_message(id, message_subdir, US"", US"");
+ break;
+
+
+ case MSG_MARK_ALL_DELIVERED:
+ for (int 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 (int 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)
+ {
+ yield = FALSE;
+ printf("- error while %s:\n bad address %s: %s\n",
+ doing, argv[recipients_arg], errmess);
+ }
+ else if (*recipient && 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)
+ {
+#ifdef SUPPORT_I18N
+ if (string_is_utf8(recipient)) allow_utf8_domains = message_smtputf8 = TRUE;
+#endif
+ receive_add_recipient(recipient, -1);
+ log_write(0, LOG_MAIN, "recipient <%s> added by %s",
+ recipient, username);
+ }
+ else if (action == MSG_MARK_DELIVERED)
+ {
+ int i;
+ 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 */
+ {
+#ifdef SUPPORT_I18N
+ if (string_is_utf8(recipient)) allow_utf8_domains = message_smtputf8 = TRUE;
+#endif
+ 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). */
+
+if (deliver_datafile >= 0)
+ {
+ (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)
+{
+int sep = 0;
+struct stat statbuf;
+const uschar * s = queue_only_file;
+uschar * ss;
+
+if (s)
+ while ((ss = string_nextinlist(&s, &sep, NULL, 0)))
+ if (Ustrncmp(ss, "smtp", 4) == 0)
+ {
+ ss += 4;
+ if (Ustat(ss, &statbuf) == 0)
+ {
+ f.queue_smtp = TRUE;
+ DEBUG(D_receive) debug_printf("queue_smtp set because %s exists\n", ss);
+ }
+ }
+ else
+ if (Ustat(ss, &statbuf) == 0)
+ {
+ queue_only = TRUE;
+ DEBUG(D_receive) debug_printf("queue_only set because %s exists\n", ss);
+ }
+}
+
+
+
+/******************************************************************************/
+/******************************************************************************/
+
+#ifndef DISABLE_QUEUE_RAMP
+void
+queue_notify_daemon(const uschar * msgid)
+{
+uschar buf[MESSAGE_ID_LENGTH + 2];
+int fd;
+
+DEBUG(D_queue_run) debug_printf("%s: %s\n", __FUNCTION__, msgid);
+
+buf[0] = NOTIFY_MSG_QRUN;
+memcpy(buf+1, msgid, MESSAGE_ID_LENGTH+1);
+
+if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) >= 0)
+ {
+ struct sockaddr_un sa_un = {.sun_family = AF_UNIX};
+
+#ifdef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+ int len = offsetof(struct sockaddr_un, sun_path) + 1
+ + snprintf(sa_un.sun_path+1, sizeof(sa_un.sun_path)-1, "%s",
+ expand_string(notifier_socket));
+ sa_un.sun_path[0] = 0;
+#else
+ int len = offsetof(struct sockaddr_un, sun_path)
+ + snprintf(sa_un.sun_path, sizeof(sa_un.sun_path), "%s",
+ expand_string(notifier_socket));
+#endif
+
+ if (sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&sa_un, len) < 0)
+ DEBUG(D_queue_run)
+ debug_printf("%s: sendto %s\n", __FUNCTION__, strerror(errno));
+ close(fd);
+ }
+else DEBUG(D_queue_run) debug_printf(" socket: %s\n", strerror(errno));
+}
+#endif
+
+#endif /*!COMPILE_UTILITY*/
+
+/* End of queue.c */
diff --git a/src/rda.c b/src/rda.c
new file mode 100644
index 0000000..b635ebf
--- /dev/null
+++ b/src/rda.c
@@ -0,0 +1,997 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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; 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)
+{
+Uskip_whitespace(&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;
+struct stat statbuf;
+uschar * s;
+
+if ((rc = Ustat(filename, &statbuf)) >= 0) return FILE_EXIST;
+saved_errno = errno;
+
+s = string_copy(filename);
+sigalrm_seen = FALSE;
+
+if (saved_errno == ENOENT)
+ {
+ uschar * slash = Ustrrchr(s, '/');
+ Ustrcpy(slash+1, US".");
+
+ ALARM(30);
+ rc = Ustat(s, &statbuf);
+ if (rc != 0 && errno == EACCES && !sigalrm_seen)
+ {
+ *slash = 0;
+ rc = Ustat(s, &statbuf);
+ }
+ saved_errno = errno;
+ ALARM_CLR(0);
+
+ DEBUG(D_route) debug_printf("stat(%s)=%d\n", s, rc);
+ }
+
+if (sigalrm_seen || rc != 0)
+ {
+ *error = string_sprintf("failed to stat %s (%s)", s,
+ 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(const 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;
+
+/* Reading a file is a form of expansion; we wish to deny attackers the
+capability to specify the file name. */
+
+if (is_tainted(filename))
+ {
+ *error = string_sprintf("Tainted name '%s' for file read not permitted\n",
+ filename);
+ *yield = FF_ERROR;
+ return NULL;
+ }
+
+/* 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. */
+
+if (!(fwd = Ufopen(filename, "rb"))) 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 ? "ignore_enotdir set => skip " : "");
+ *yield =
+ options & RDO_ENOTDIR || 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)) 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)) 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("%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 && statbuf.st_uid == rdata->pw->pw_uid)
+ uid_ok = TRUE;
+ else if (rdata->owners)
+ for (int 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 && statbuf.st_gid == rdata->pw->pw_gid)
+ gid_ok = TRUE;
+ else if (rdata->owngroups)
+ for (int 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, filename);
+
+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 " %sbytes read from %s\n",
+ statbuf.st_size, is_tainted(filename) ? "(tainted) " : "", 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(const redirect_block * rdata, int options,
+ const uschar * include_directory, const uschar * sieve_vacation_directory,
+ const uschar * sieve_enotify_mailto_owner, const uschar * sieve_useraddress,
+ const uschar * sieve_subaddress, address_item ** generated, uschar ** error,
+ error_block ** eblockp, int * filtertype)
+{
+const uschar * data;
+
+if (rdata->isfile)
+ {
+ int yield = 0;
+ if (!(data = rda_get_file_contents(rdata, options, error, &yield)))
+ return yield;
+ }
+else data = rdata->string;
+
+*filtertype = f.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))
+ {
+ *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)
+ {
+ *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, const uschar *s)
+{
+int len = s ? Ustrlen(s) + 1 : 0;
+return ( write(fd, &len, sizeof(int)) != sizeof(int)
+ || (s && 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
+ /* We know we have enough memory so disable the error on "len" */
+ /* coverity[tainted_data] */
+ /* We trust the data source, so untainted */
+ if (read(fd, *sp = store_get(len, GET_UNTAINTED), 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 initial 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,
+ const uschar * include_directory, const uschar * sieve_vacation_directory,
+ const uschar * sieve_enotify_mailto_owner, const uschar * sieve_useraddress,
+ const uschar * sieve_subaddress, const ugid_block * ugid, address_item ** generated,
+ uschar ** error, error_block ** eblockp, int * filtertype, const 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", string_printing(rdata->string));
+
+/* Do the expansions of the file name or data first, while still privileged. */
+
+if (!(data = expand_string(rdata->string)))
+ {
+ if (f.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'%s\n", data, is_tainted(data) ? " (tainted)":"");
+
+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 = exim_fork(US"router-interpret")) == 0)
+ {
+ header_line *waslast = header_last; /* Save last header */
+ int fd_flags = -1;
+
+ fd = pfd[pipe_write];
+ (void)close(pfd[pipe_read]);
+
+ if ((fd_flags = fcntl(fd, F_GETFD)) == -1) goto bad;
+ if (fcntl(fd, F_SETFD, fd_flags | FD_CLOEXEC) == -1) goto bad;
+
+ 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");
+ BIT_CLEAR(log_selector, log_selector_size, Li_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)
+ {
+ for (error_block * ep = *eblockp; ep; 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 (f.system_filtering)
+ {
+ int i = 0;
+ for (header_line * 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)
+ {
+ for (address_item * addr = *generated; addr; addr = addr->next)
+ {
+ int reply_options = 0;
+ int ig_err = addr->prop.ignore_error ? 1 : 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->prop.errors_address) != 0
+ || write(fd, &ig_err, sizeof(ig_err)) != sizeof(ig_err)
+ )
+ goto bad;
+
+ if (addr->pipe_expandn)
+ for (uschar ** pp = addr->pipe_expandn; *pp; pp++)
+ if (rda_write_string(fd, *pp) != 0)
+ goto bad;
+ if (rda_write_string(fd, NULL) != 0)
+ goto bad;
+
+ if (!addr->reply)
+ {
+ 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();
+ exim_underbar_exit(EXIT_SUCCESS);
+
+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)
+ {
+ error_block *e;
+ for (error_block ** p = eblockp; ; p = &e->next)
+ {
+ uschar *s;
+ if (!rda_read_string(fd, &s)) goto DISASTER;
+ if (!s) break;
+ e = store_get(sizeof(error_block), GET_UNTAINTED);
+ e->next = NULL;
+ e->text1 = s;
+ if (!rda_read_string(fd, &s)) goto DISASTER;
+ e->text2 = s;
+ *p = e;
+ }
+ }
+
+/* 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 (f.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++;
+ if (!(h = h->next)) goto DISASTER_NO_HEADER;
+ }
+ h->type = htype_old;
+ }
+
+ for (;;)
+ {
+ uschar *s;
+ int type;
+ if (!rda_read_string(fd, &s)) goto DISASTER;
+ if (!s) 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) 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->prop.errors_address)
+ || read(fd, &i, sizeof(i)) != sizeof(i)
+ )
+ goto DISASTER;
+ addr->prop.ignore_error = (i != 0);
+
+ /* 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 *), GET_UNTAINTED);
+ 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), GET_UNTAINTED);
+
+ 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 ? US": error=" : US"",
+ *error ? *error : US"");
+ 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
index 0000000..06bc50f
--- /dev/null
+++ b/src/readconf.c
@@ -0,0 +1,4510 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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"
+
+#ifdef MACRO_PREDEF
+# include "macro_predef.h"
+#endif
+
+#define READCONF_DEBUG if (FALSE) /* Change to TRUE to enable */
+
+
+static uschar * syslog_facility_str;
+static void fn_smtp_receive_timeout(const uschar *, const uschar *, unsigned);
+
+/*************************************************
+* 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} },
+#ifndef DISABLE_TLS
+ { "acl_smtp_starttls", opt_stringptr, {&acl_smtp_starttls} },
+#endif
+ { "acl_smtp_vrfy", opt_stringptr, {&acl_smtp_vrfy} },
+ { "add_environment", opt_stringptr, {&add_environment} },
+ { "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_linesize_limit", opt_mkint, {&bounce_return_linesize_limit} },
+ { "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} },
+ { "chunking_advertise_hosts", opt_stringptr, {&chunking_advertise_hosts} },
+ { "commandline_checks_require_admin", opt_bool,{&commandline_checks_require_admin} },
+ { "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
+ { "debug_store", opt_bool, {&debug_store} },
+ { "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_hashes", opt_stringptr, {&dkim_verify_hashes} },
+ { "dkim_verify_keytypes", opt_stringptr, {&dkim_verify_keytypes} },
+ { "dkim_verify_min_keysizes", opt_stringptr, {&dkim_verify_min_keysizes} },
+ { "dkim_verify_minimal", opt_bool, {&dkim_verify_minimal} },
+ { "dkim_verify_signers", opt_stringptr, {&dkim_verify_signers} },
+#endif
+#ifdef SUPPORT_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_cname_loops", opt_int, {&dns_cname_loops} },
+ { "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_trust_aa", opt_stringptr, {&dns_trust_aa} },
+ { "dns_use_edns0", opt_int, {&dns_use_edns0} },
+ /* This option is now a no-op, retained for compatibility */
+ { "drop_cr", opt_bool, {&drop_cr} },
+/*********************************************************/
+ { "dsn_advertise_hosts", opt_stringptr, {&dsn_advertise_hosts} },
+ { "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} },
+#ifndef DISABLE_EVENT
+ { "event_action", opt_stringptr, {&event_action} },
+#endif
+ { "exim_group", opt_gid, {&exim_gid} },
+ { "exim_path", opt_stringptr, {&exim_path} },
+ { "exim_user", opt_uid, {&exim_uid} },
+ { "exim_version", opt_stringptr, {&version_string} },
+ { "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} },
+#ifndef DISABLE_TLS
+ { "gnutls_allow_auto_pkcs11", opt_bool, {&gnutls_allow_auto_pkcs11} },
+ { "gnutls_compat_mode", opt_bool, {&gnutls_compat_mode} },
+#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} },
+#ifdef SUPPORT_PROXY
+ { "hosts_proxy", opt_stringptr, {&hosts_proxy} },
+#endif
+#ifndef DISABLE_TLS
+ { "hosts_require_alpn", opt_stringptr, {&hosts_require_alpn} },
+#endif
+ { "hosts_require_helo", opt_stringptr, {&hosts_require_helo} },
+ { "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_environment", opt_stringptr, {&keep_environment} },
+ { "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
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ { "limits_advertise_hosts", opt_stringptr, {&limits_advertise_hosts} },
+#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} },
+#ifdef HAVE_LOCAL_SCAN
+ { "local_scan_timeout", opt_time, {&local_scan_timeout} },
+#endif
+ { "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} },
+ { "notifier_socket", opt_stringptr, {&notifier_socket} },
+#ifndef DISABLE_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} },
+ { "perl_taintmode", opt_bool, {&opt_perl_taintmode} },
+#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_PIPE_CONNECT
+ { "pipelining_connect_advertise_hosts", opt_stringptr,
+ {&pipe_connect_advertise_hosts} },
+#endif
+#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 SUPPORT_PROXY
+ { "proxy_protocol_timeout", opt_time, {&proxy_protocol_timeout} },
+#endif
+ { "qualify_domain", opt_stringptr, {&qualify_domain_sender} },
+ { "qualify_recipient", opt_stringptr, {&qualify_domain_recipient} },
+ { "queue_domains", opt_stringptr, {&queue_domains} },
+#ifndef DISABLE_QUEUE_RAMP
+ { "queue_fast_ramp", opt_bool, {&queue_fast_ramp} },
+#endif
+ { "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_stringptr, {&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 LOOKUP_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} },
+ { "slow_lookup_log", opt_int, {&slow_lookup_log} },
+ { "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_stringptr, {&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_backlog_monitor", opt_int, {&smtp_backlog_monitor} },
+ { "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_func, {.fn = &fn_smtp_receive_timeout} },
+ { "smtp_reserve_hosts", opt_stringptr, {&smtp_reserve_hosts} },
+ { "smtp_return_error_details",opt_bool, {&smtp_return_error_details} },
+#ifdef SUPPORT_I18N
+ { "smtputf8_advertise_hosts", opt_stringptr, {&smtputf8_advertise_hosts} },
+#endif
+#ifdef WITH_CONTENT_SCAN
+ { "spamd_address", opt_stringptr, {&spamd_address} },
+#endif
+#ifdef SUPPORT_SPF
+ { "spf_guess", opt_stringptr, {&spf_guess} },
+ { "spf_smtp_comment_template",opt_stringptr, {&spf_smtp_comment_template} },
+#endif
+ { "split_spool_directory", opt_bool, {&split_spool_directory} },
+ { "spool_directory", opt_stringptr, {&spool_directory} },
+ { "spool_wireformat", opt_bool, {&spool_wireformat} },
+#ifdef LOOKUP_SQLITE
+ { "sqlite_dbfile", opt_stringptr, {&sqlite_dbfile} },
+ { "sqlite_lock_timeout", opt_int, {&sqlite_lock_timeout} },
+#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_pid", opt_bool, {&syslog_pid} },
+ { "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} },
+ { "tls_advertise_hosts", opt_stringptr, {&tls_advertise_hosts} },
+#ifndef DISABLE_TLS
+ { "tls_alpn", opt_stringptr, {&tls_alpn} },
+ { "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} },
+ { "tls_eccurve", opt_stringptr, {&tls_eccurve} },
+# 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} },
+# ifndef DISABLE_TLS_RESUME
+ { "tls_resumption_hosts", opt_stringptr, {&tls_resumption_hosts} },
+# endif
+ { "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} }
+};
+
+#ifndef MACRO_PREDEF
+static int optionlist_config_size = nelem(optionlist_config);
+#endif
+
+
+#ifdef MACRO_PREDEF
+
+static void
+fn_smtp_receive_timeout(const uschar * name, const uschar * str, unsigned flags) {/*Dummy*/}
+
+void
+options_main(void)
+{
+options_from_list(optionlist_config, nelem(optionlist_config), US"MAIN", NULL);
+}
+
+void
+options_auths(void)
+{
+uschar buf[EXIM_DRIVERNAME_MAX];
+
+options_from_list(optionlist_auths, optionlist_auths_size, US"AUTHENTICATORS", NULL);
+
+for (struct auth_info * ai = auths_available; ai->driver_name[0]; ai++)
+ {
+ spf(buf, sizeof(buf), US"_DRIVER_AUTHENTICATOR_%T", ai->driver_name);
+ builtin_macro_create(buf);
+ options_from_list(ai->options, (unsigned)*ai->options_count, US"AUTHENTICATOR", ai->driver_name);
+
+ if (ai->macros_create) (ai->macros_create)();
+ }
+}
+
+void
+options_logging(void)
+{
+uschar buf[EXIM_DRIVERNAME_MAX];
+
+for (bit_table * bp = log_options; bp < log_options + log_options_count; bp++)
+ {
+ spf(buf, sizeof(buf), US"_LOG_%T", bp->name);
+ builtin_macro_create(buf);
+ }
+}
+
+
+#else /*!MACRO_PREDEF*/
+
+extern char **environ;
+
+static void save_config_line(const uschar* line);
+static void save_config_position(const uschar *file, int line);
+static void print_config(BOOL admin, BOOL terse);
+
+
+#define CSTATE_STACK_SIZE 10
+
+const uschar *config_directory = NULL;
+
+
+/* Structure for chain (stack) of .included files */
+
+typedef struct config_file_item {
+ struct config_file_item *next;
+ const uschar *filename;
+ const uschar *directory;
+ FILE *file;
+ int lineno;
+} config_file_item;
+
+/* Structure for chain of configuration lines (-bP config) */
+
+typedef struct config_line_item {
+ struct config_line_item *next;
+ uschar *line;
+} config_line_item;
+
+static config_line_item* config_lines;
+
+/* 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;
+
+/* constants */
+static const uschar * const hidden = US"<value not displayable>";
+
+/* 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);
+
+
+#define opt_fn_print BIT(0)
+#define opt_fn_print_label BIT(1)
+
+
+/*************************************************
+* 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)
+{
+for (int i = 0; i < nelem(optionlist_config); i++)
+ if (p == optionlist_config[i].v.value) return US optionlist_config[i].name;
+
+for (router_instance * r = routers; r; r = r->next)
+ {
+ router_info *ri = r->info;
+ for (int i = 0; i < *ri->options_count; i++)
+ {
+ if ((ri->options[i].type & opt_mask) != opt_stringptr) continue;
+ if (p == CS (r->options_block) + ri->options[i].v.offset)
+ return US ri->options[i].name;
+ }
+ }
+
+for (transport_instance * t = transports; t; t = t->next)
+ {
+ transport_info *ti = t->info;
+ for (int i = 0; i < *ti->options_count; i++)
+ {
+ optionlist * op = &ti->options[i];
+ if ((op->type & opt_mask) != opt_stringptr) continue;
+ if (p == ( op->type & opt_public
+ ? CS t
+ : CS t->options_block
+ )
+ + op->v.offset)
+ return US op->name;
+ }
+ }
+
+return US"";
+}
+
+
+
+
+/*************************************************
+* Deal with an assignment to a macro *
+*************************************************/
+
+/* We have a new definition; append to the list.
+
+Args:
+ name Name of the macro; will be copied
+ val Expansion result for the macro; will be copied
+*/
+
+macro_item *
+macro_create(const uschar * name, const uschar * val, BOOL command_line)
+{
+macro_item * m = store_get(sizeof(macro_item), GET_UNTAINTED);
+
+READCONF_DEBUG fprintf(stderr, "%s: '%s' '%s'\n", __FUNCTION__, name, val);
+m->next = NULL;
+m->command_line = command_line;
+m->namelen = Ustrlen(name);
+m->replen = Ustrlen(val);
+m->name = string_copy(name);
+m->replacement = string_copy(val);
+if (mlast)
+ mlast->next = m;
+else
+ macros = m;
+mlast = m;
+if (!macros_user)
+ macros_user = m;
+return m;
+}
+
+
+/* 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: FALSE iff fatal error
+*/
+
+BOOL
+macro_read_assignment(uschar *s)
+{
+uschar name[EXIM_DRIVERNAME_MAX];
+int namelen = 0;
+BOOL redef = FALSE;
+macro_item *m;
+
+while (isalnum(*s) || *s == '_')
+ {
+ if (namelen >= sizeof(name) - 1)
+ {
+ log_write(0, LOG_PANIC|LOG_CONFIG_IN,
+ "macro name too long (maximum is " SIZE_T_FMT " characters)", sizeof(name) - 1);
+ return FALSE;
+ }
+ name[namelen++] = *s++;
+ }
+name[namelen] = 0;
+
+Uskip_whitespace(&s);
+if (*s++ != '=')
+ {
+ log_write(0, LOG_PANIC|LOG_CONFIG_IN, "malformed macro definition");
+ return FALSE;
+ }
+
+if (*s == '=')
+ {
+ redef = TRUE;
+ s++;
+ }
+Uskip_whitespace(&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. This is the requirement that make using a tree
+for macros hard; we must check all macros for the substring. Perhaps a
+sorted list, and a bsearch, would work?
+Note: it is documented that the other way round works. */
+
+for (m = macros; m; m = m->next)
+ {
+ if (Ustrcmp(m->name, name) == 0)
+ {
+ if (!m->command_line && !redef)
+ {
+ log_write(0, LOG_CONFIG|LOG_PANIC, "macro \"%s\" is already "
+ "defined (use \"==\" if you want to redefine it)", name);
+ return FALSE;
+ }
+ break;
+ }
+
+ if (m->namelen < namelen && Ustrstr(name, m->name) != NULL)
+ {
+ log_write(0, LOG_CONFIG|LOG_PANIC, "\"%s\" cannot be defined as "
+ "a macro because previously defined macro \"%s\" is a substring",
+ name, m->name);
+ return FALSE;
+ }
+
+ /* We cannot have this test, because it is documented that a substring
+ macro is permitted (there is even an example).
+ *
+ * if (m->namelen > 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);
+ */
+ }
+
+/* Check for an overriding command-line definition. */
+
+if (m && m->command_line) return TRUE;
+
+/* Redefinition must refer to an existing macro. */
+
+if (redef)
+ if (m)
+ {
+ m->replen = Ustrlen(s);
+ m->replacement = string_copy(s);
+ }
+ else
+ {
+ log_write(0, LOG_CONFIG|LOG_PANIC, "can't redefine an undefined macro "
+ "\"%s\"", name);
+ return FALSE;
+ }
+
+/* We have a new definition. */
+else
+ (void) macro_create(name, s, FALSE);
+return TRUE;
+}
+
+
+
+
+
+/* Process line for macros. The line is in big_buffer starting at offset len.
+Expand big_buffer if needed. Handle definitions of new macros, and
+macro expansions, rewriting the line in the buffer.
+
+Arguments:
+ len Offset in buffer of start of line
+ newlen Pointer to offset of end of line, updated on return
+ macro_found Pointer to return that a macro was expanded
+
+Return: pointer to first nonblank char in line
+*/
+
+uschar *
+macros_expand(int len, int * newlen, BOOL * macro_found)
+{
+uschar * ss = big_buffer + len;
+uschar * s;
+
+/* Find the true start of the physical line - leading spaces are always
+ignored. */
+
+Uskip_whitespace(&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++;
+ if (Uskip_whitespace(&s) != '=') s = ss; /* Not a macro definition */
+ }
+
+/* Skip leading chars which cannot start a macro name, to avoid multiple
+pointless rescans in Ustrstr calls. */
+
+while (*s && !isupper(*s) && !(*s == '_' && isupper(s[1]))) s++;
+
+/* For each defined macro, scan the line (from after XXX= if present),
+replacing all occurrences of the macro. */
+
+*macro_found = FALSE;
+if (*s) for (macro_item * m = *s == '_' ? macros : macros_user; m; m = m->next)
+ {
+ uschar * p, *pp;
+ uschar * t;
+
+ while (*s && !isupper(*s) && !(*s == '_' && isupper(s[1]))) s++;
+ if (!*s) break;
+
+ t = s;
+ while ((p = Ustrstr(t, m->name)) != NULL)
+ {
+ int moveby;
+
+ READCONF_DEBUG fprintf(stderr, "%s: matched '%s' in '%.*s'\n", __FUNCTION__,
+ m->name, (int) Ustrlen(ss)-1, ss);
+ /* Expand the buffer if necessary */
+
+ while (*newlen - m->namelen + m->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 + m->namelen;
+ if ((moveby = m->replen - m->namelen) != 0)
+ {
+ memmove(p + m->replen, pp, (big_buffer + *newlen) - pp + 1);
+ *newlen += moveby;
+ }
+ Ustrncpy(p, m->replacement, m->replen);
+ t = p + m->replen;
+ while (*t && !isupper(*t) && !(*t == '_' && isupper(t[1]))) t++;
+ *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. */
+
+Uskip_whitespace(&ss);
+return ss;
+}
+
+/*************************************************
+* 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;
+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_directory = config_file_stack->directory;
+ config_lineno = config_file_stack->lineno;
+ config_file_stack = config_file_stack->next;
+ if (config_lines)
+ save_config_position(config_filename, config_lineno);
+ 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);
+
+ if (config_lines && config_lineno == 1)
+ save_config_position(config_filename, config_lineno);
+
+ /* 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);
+ }
+
+ ss = macros_expand(len, &newlen, &macro_found);
+
+ /* 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 occurred 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;
+ Uskip_whitespace(&ss);
+ t = ss + Ustrlen(ss);
+ while (t > ss && isspace(t[-1])) t--;
+ if (*ss == '\"' && t[-1] == '\"')
+ {
+ ss++;
+ t--;
+ }
+ *t = 0;
+
+ /* We allow relative file names. For security reasons currently
+ relative names not allowed with .include_if_exists. For .include_if_exists
+ we need to check the permissions/ownership of the containing folder */
+ if (*ss != '/')
+ if (include_if_exists) log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, ".include specifies a non-"
+ "absolute path \"%s\"", ss);
+ else
+ {
+ gstring * g = string_append(NULL, 3, config_directory, "/", ss);
+ ss = string_from_gstring(g);
+ }
+
+ if (include_if_exists != 0 && (Ustat(ss, &statbuf) != 0)) continue;
+
+ if (config_lines)
+ save_config_position(config_filename, config_lineno);
+ save = store_get(sizeof(config_file_item), GET_UNTAINTED);
+ save->next = config_file_stack;
+ config_file_stack = save;
+ save->file = config_file;
+ save->filename = config_filename;
+ save->directory = config_directory;
+ save->lineno = config_lineno;
+
+ if (!(config_file = Ufopen(ss, "rb")))
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "failed to open included "
+ "configuration file %s", ss);
+
+ config_filename = string_copy(ss);
+ config_directory = string_copyn(ss, CUstrrchr(ss, '/') - 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 (config_lines)
+ save_config_line(s);
+
+if (strncmpic(s, US"begin ", 6) == 0)
+ {
+ s += 6;
+ Uskip_whitespace(&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;
+BOOL broken = FALSE;
+
+if (isalpha(Uskip_whitespace(&s)))
+ while (isalnum(*s) || *s == '_')
+ {
+ if (p < len-1) name[p++] = *s;
+ else {
+ broken = TRUE;
+ break;
+ }
+ s++;
+ }
+
+name[p] = 0;
+if (broken) {
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+ "exim item name too long (>%d), unable to use \"%s\" (truncated)",
+ len, name);
+}
+Uskip_whitespace(&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(const uschar *s, int terminator, BOOL return_msec)
+{
+int yield = 0;
+for (;;)
+ {
+ int value, count;
+ double fraction;
+
+ if (!isdigit(*s)) return -1;
+ (void)sscanf(CCS 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(CCS 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(const 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(const 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(const uschar *name, optionlist *oltop, int last, void *data_block)
+{
+optionlist *ol;
+uschar name2[EXIM_DRIVERNAME_MAX];
+sprintf(CS name2, "*set_%.50s", name);
+if (!(ol = find_option(name2, oltop, last)))
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "Exim internal error: missing set flag for %s", name);
+return data_block
+ ? (BOOL *)(US data_block + ol->v.offset) : (BOOL *)ol->v.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(const uschar *s, const uschar *t1, const uschar *t2, const 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(const uschar *p, int *existflags, BOOL isglobal)
+{
+rewrite_rule * next = store_get(sizeof(rewrite_rule), GET_UNTAINTED);
+
+next->next = NULL;
+next->key = string_dequote(&p);
+
+Uskip_whitespace(&p);
+if (!*p)
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+ "missing rewrite replacement string");
+
+next->flags = 0;
+next->replacement = string_dequote(&p);
+
+while (*p) 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(const uschar *s, const uschar *name)
+{
+uschar *yield;
+const 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;
+}
+
+
+/*************************************************
+* Custom-handler options *
+*************************************************/
+static void
+fn_smtp_receive_timeout(const uschar * name, const uschar * str, unsigned flags)
+{
+if (flags & opt_fn_print)
+ {
+ if (flags & opt_fn_print_label) printf("%s = ", name);
+ printf("%s\n", smtp_receive_timeout_s
+ ? string_printing2(smtp_receive_timeout_s, SP_TAB)
+ : readconf_printtime(smtp_receive_timeout));
+ }
+else if (*str == '$')
+ smtp_receive_timeout_s = string_copy(str);
+else
+ {
+ /* "smtp_receive_timeout", opt_time, &smtp_receive_timeout */
+ smtp_receive_timeout = readconf_readtime(str, 0, FALSE);
+ if (smtp_receive_timeout < 0)
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "invalid time value for %s",
+ name);
+ }
+}
+
+/*************************************************
+* 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 count, type, value;
+int issecure = 0;
+uid_t uid;
+gid_t gid;
+BOOL boolvalue = TRUE;
+BOOL freesptr = TRUE;
+optionlist *ol, *ol2;
+struct passwd *pw;
+rmark reset_point;
+int intbase = 0;
+uschar *inttype = US"";
+uschar *sptr;
+uschar *s = buffer;
+uschar **str_target;
+uschar name[EXIM_DRIVERNAME_MAX];
+uschar name2[EXIM_DRIVERNAME_MAX];
+
+/* 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 (int 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. */
+
+if (!(ol = find_option(name + offset, oltop, last)))
+ {
+ if (!unknown_txt) 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)
+ 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_expand_bool, a general string that
+ultimately expands to one of those values. */
+
+else if (*s && (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 && !(ol->type & opt_public))
+ 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 = store_mark();
+ 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:
+ str_target = data_block ? USS (US data_block + ol->v.offset)
+ : USS ol->v.value;
+ if (ol->type & opt_rep_con)
+ {
+ uschar * saved_condition;
+ /* 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. */
+ *str_target = string_copy_perm( (saved_condition = *str_target)
+ ? string_sprintf("${if and{{bool_lax{%s}}{bool_lax{%s}}}}",
+ saved_condition, sptr)
+ : sptr,
+ FALSE);
+ /* 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_o =
+ Ustrncmp(name, "headers_add", 11) == 0 ? '\n'
+ : Ustrncmp(name, "set", 3) == 0 ? ';'
+ : ':';
+ int sep_i = -(int)sep_o;
+ const uschar * list = sptr;
+ uschar * s;
+ gstring * list_o = NULL;
+
+ if (*str_target)
+ {
+ list_o = string_get(Ustrlen(*str_target) + Ustrlen(sptr));
+ list_o = string_cat(list_o, *str_target);
+ }
+
+ while ((s = string_nextinlist(&list, &sep_i, NULL, 0)))
+ list_o = string_append_listele(list_o, sep_o, s);
+
+ if (list_o)
+ *str_target = string_copy_perm(string_from_gstring(list_o), FALSE);
+ }
+ else
+ {
+ *str_target = sptr;
+ freesptr = FALSE;
+ }
+ break;
+
+ case opt_rewrite:
+ if (data_block)
+ *USS (US data_block + ol->v.offset) = sptr;
+ else
+ *USS ol->v.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 || !ol3)
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+ "rewrite rules not available for driver");
+
+ if (data_block)
+ {
+ chain = (rewrite_rule **)(US data_block + ol2->v.offset);
+ flagptr = (int *)(US data_block + ol3->v.offset);
+ }
+ else
+ {
+ chain = (rewrite_rule **)ol2->v.value;
+ flagptr = (int *)ol3->v.value;
+ }
+
+ /* This will trap if sptr is tainted. Not sure if that can happen */
+ while ((p = string_nextinlist(CUSS &sptr, &sep, big_buffer, BIG_BUFFER_SIZE)))
+ {
+ 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);
+ if ((ol2 = find_option(name2, oltop, last)))
+ {
+ uschar *ss = (Ustrchr(sptr, '$') != NULL) ? sptr : NULL;
+
+ if (data_block)
+ *(USS(US data_block + ol2->v.offset)) = ss;
+ else
+ *(USS ol2->v.value) = ss;
+
+ if (ss)
+ {
+ *(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)
+ *(uid_t *)(US data_block + ol->v.offset) = uid;
+ else
+ *(uid_t *)ol->v.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, US"group");
+ ol2 = find_option(name, oltop, last);
+ if (ol2 && ((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)
+ *((gid_t *)(US data_block + ol2->v.offset)) = pw->pw_gid;
+ else
+ *((gid_t *)ol2->v.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);
+ if ((ol2 = find_option(name2, oltop, last)))
+ {
+ uschar *ss = (Ustrchr(sptr, '$') != NULL) ? sptr : NULL;
+
+ if (data_block)
+ *(USS(US data_block + ol2->v.offset)) = ss;
+ else
+ *(USS ol2->v.value) = ss;
+
+ if (ss)
+ {
+ *(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)
+ *((gid_t *)(US data_block + ol->v.offset)) = gid;
+ else
+ *((gid_t *)ol->v.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;
+ const uschar *p;
+ const 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)
+ *((uid_t **)(US data_block + ol->v.offset)) = list;
+ else
+ *((uid_t **)ol->v.value) = list;
+
+ p = op;
+ while (count-- > 1)
+ {
+ int sep = 0;
+ /* If p is tainted we trap. Not sure that can happen */
+ (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;
+ const uschar *p;
+ const uschar *op = expand_string (sptr);
+
+ if (!op)
+ 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)
+ *((gid_t **)(US data_block + ol->v.offset)) = list;
+ else
+ *((gid_t **)ol->v.value) = list;
+
+ p = op;
+ while (count-- > 1)
+ {
+ int sep = 0;
+ /* If p is tainted we trap. Not sure that can happen */
+ (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) reset_point = 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 && Ustrchr(s, '$') != 0)
+ {
+ sprintf(CS name2, "*expand_%.50s", name);
+ if ((ol2 = find_option(name2, oltop, last)))
+ {
+ reset_point = store_mark();
+ sptr = read_string(s, name);
+ if (data_block)
+ *(USS(US data_block + ol2->v.offset)) = sptr;
+ else
+ *(USS ol2->v.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, EXIM_DRIVERNAME_MAX, 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
+ ? (int *)(US data_block + ol->v.offset)
+ : (int *)ol->v.value;
+ if (boolvalue) *ptr |= bit; else *ptr &= ~bit;
+ break;
+ }
+
+ /* Handle full BOOL types */
+
+ if (data_block)
+ *((BOOL *)(US data_block + ol->v.offset)) = boolvalue;
+ else
+ *((BOOL *)ol->v.value) = boolvalue;
+
+ /* Verify fudge */
+
+ if (type == opt_bool_verify)
+ {
+ sprintf(CS name2, "%.50s_recipient", name + offset);
+ if ((ol2 = find_option(name2, oltop, last)))
+ if (data_block)
+ *((BOOL *)(US data_block + ol2->v.offset)) = boolvalue;
+ else
+ *((BOOL *)ol2->v.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);
+ if ((ol2 = find_option(name2, oltop, last)))
+ if (data_block)
+ *((BOOL *)(US data_block + ol2->v.offset)) = TRUE;
+ else
+ *((BOOL *)ol2->v.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, M, G, and T. 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 && *endptr)
+ {
+ uschar * mp = US"TtGgMmKk\0"; /* YyZzEePpTtGgMmKk */
+
+ if ((mp = Ustrchr(mp, *endptr)))
+ {
+ endptr++;
+ do
+ {
+ if (lvalue > INT_MAX/1024 || lvalue < INT_MIN/1024)
+ {
+ errno = ERANGE;
+ break;
+ }
+ lvalue *= 1024;
+ }
+ while (*(mp += 2));
+ }
+ }
+
+ 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)
+ extra_chars_error(endptr, inttype, US"integer value for ", name);
+
+ value = (int)lvalue;
+ }
+
+ if (data_block)
+ *(int *)(US data_block + ol->v.offset) = value;
+ else
+ *(int *)ol->v.value = value;
+ break;
+
+ /* Integer held in K: again, allow formats and suffixes as above. */
+
+ case opt_Kint:
+ {
+ uschar *endptr;
+ errno = 0;
+ int_eximarith_t 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 && *endptr)
+ {
+ uschar * mp = US"ZzEePpTtGgMmKk\0"; /* YyZzEePpTtGgMmKk */
+
+ if ((mp = Ustrchr(mp, *endptr)))
+ {
+ endptr++;
+ while (*(mp += 2))
+ {
+ if (lvalue > EXIM_ARITH_MAX/1024 || lvalue < EXIM_ARITH_MIN/1024)
+ {
+ errno = ERANGE;
+ break;
+ }
+ lvalue *= 1024;
+ }
+ }
+ else
+ lvalue = (lvalue + 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)
+ *(int_eximarith_t *)(US data_block + ol->v.offset) = lvalue;
+ else
+ *(int_eximarith_t *)ol->v.value = lvalue;
+ 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);
+
+ /* We get a coverity error here for using count, as it derived
+ from the tainted buffer pointed to by s, as parsed by sscanf().
+ By the definition of sscanf we must be accessing between start
+ and end of s (assuming it is nul-terminated...) so ignore the error. */
+ /* coverity[tainted_data] */
+ 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)
+ *((int *)(US data_block + ol->v.offset)) = value;
+ else
+ *((int *)ol->v.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)
+ *((int *)(US data_block + ol->v.offset)) = value;
+ else
+ *((int *)ol->v.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
+ ? (int *)(US data_block + ol->v.offset)
+ : (int *)ol->v.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;
+ }
+
+ case opt_func:
+ {
+ void (*fn)() = ol->v.fn;
+ fn(name, s, 0);
+ 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) p += sprintf(CS p, "%dw", w);
+if (d > 0) p += sprintf(CS p, "%dd", d);
+if (h > 0) p += sprintf(CS p, "%dh", h);
+if (m > 0) p += sprintf(CS p, "%dm", m);
+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: boolean success
+*/
+
+static BOOL
+print_ol(optionlist *ol, const 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[EXIM_DRIVERNAME_MAX];
+
+if (!ol)
+ {
+ printf("%s is not a known option\n", name);
+ return FALSE;
+ }
+
+/* Non-admin callers cannot see options that have been flagged secure by the
+"hide" prefix. */
+
+if (!f.admin_user && ol->type & opt_secure)
+ {
+ if (no_labels)
+ printf("%s\n", CCS hidden);
+ else
+ printf("%s = %s\n", name, CCS hidden);
+ return TRUE;
+ }
+
+/* Else show the value of the option */
+
+value = ol->v.value;
+if (options_block)
+ {
+ if (!(ol->type & opt_public))
+ options_block = (void *)(((driver_instance *)options_block)->options_block);
+ value = (void *)(US options_block + (long int)value);
+ }
+
+switch(ol->type & opt_mask)
+ {
+ case opt_stringptr:
+ case opt_rewrite: /* Show the text value */
+ s = *(USS value);
+ if (!no_labels) printf("%s = ", name);
+ printf("%s\n", s ? string_printing2(s, SP_TAB) : US"");
+ 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_eximarith_t x = *((int_eximarith_t *)value);
+ if (!no_labels) printf("%s = ", name);
+ if (x == 0) printf("0\n");
+ else if ((x & ((1<<30)-1)) == 0) printf(PR_EXIM_ARITH "T\n", x >> 30);
+ else if ((x & ((1<<20)-1)) == 0) printf(PR_EXIM_ARITH "G\n", x >> 20);
+ else if ((x & ((1<<10)-1)) == 0) printf(PR_EXIM_ARITH "M\n", x >> 10);
+ else printf(PR_EXIM_ARITH "K\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);
+ if ((ol2 = find_option(name2, oltop, last)))
+ {
+ if (options_block)
+ s = *USS (US options_block + ol2->v.offset);
+ else
+ s = *USS ol2->v.value;
+ if (!no_labels) printf("%s = ", name);
+ printf("%s\n", s ? string_printing(s) : US"");
+ 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
+ if ((pw = getpwuid(*((uid_t *)value))))
+ printf("%s\n", pw->pw_name);
+ else
+ printf("%ld\n", (long int)(*((uid_t *)value)));
+ 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);
+ if ( (ol2 = find_option(name2, oltop, last))
+ && (ol2->type & opt_mask) == opt_stringptr)
+ {
+ if (options_block)
+ s = *USS (US options_block + ol2->v.offset);
+ else
+ s = *USS ol2->v.value;
+ if (!no_labels) printf("%s = ", name);
+ printf("%s\n", s ? string_printing(s) : US"");
+ 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
+ if ((gr = getgrgid(*((int *)value))))
+ printf("%s\n", gr->gr_name);
+ else
+ printf("%ld\n", (long int)(*((int *)value)));
+ break;
+
+ case opt_uidlist:
+ uidlist = *((uid_t **)value);
+ if (!no_labels) printf("%s =", name);
+ if (uidlist)
+ {
+ uschar sep = no_labels ? '\0' : ' ';
+ for (int i = 1; i <= (int)(uidlist[0]); i++)
+ {
+ uschar *name = NULL;
+ if ((pw = getpwuid(uidlist[i]))) name = US pw->pw_name;
+ if (sep != '\0') printf("%c", sep);
+ if (name) 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)
+ {
+ uschar sep = no_labels ? '\0' : ' ';
+ for (int i = 1; i <= (int)(gidlist[0]); i++)
+ {
+ uschar *name = NULL;
+ if ((gr = getgrgid(gidlist[i]))) name = US gr->gr_name;
+ if (sep != '\0') printf("%c", sep);
+ if (name) 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 *list = (int *)value;
+ if (!no_labels) printf("%s = ", name);
+ for (int 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);
+ if ((ol2 = find_option(name2, oltop, last)) && ol2->v.value)
+ {
+ if (options_block)
+ s = *USS (US options_block + ol2->v.offset);
+ else
+ s = *USS ol2->v.value;
+ if (s)
+ {
+ 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;
+
+ case opt_func:
+ ol->v.fn(name, NULL, no_labels ? opt_fn_print : opt_fn_print|opt_fn_print_label);
+ break;
+ }
+return TRUE;
+}
+
+
+
+/*************************************************
+* 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
+ config_file print the name of the configuration file
+ (configure_file will still work, for backward
+ compatibility)
+ 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
+ config print the configuration as it is parsed
+ environment print the used execution environment
+
+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: Boolean success
+*/
+
+BOOL
+readconf_print(const uschar *name, uschar *type, BOOL no_labels)
+{
+BOOL names_only = FALSE;
+optionlist *ol2 = NULL;
+driver_instance *d = NULL;
+int size = 0;
+
+if (!type)
+ {
+ if (*name == '+')
+ {
+ 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 (int i = 0; i < 4; i++)
+ if ((t = tree_search(*(anchors[i]), name+1)))
+ {
+ namedlist_block * nb = t->data.ptr;
+ const uschar * s = nb->hide ? hidden : nb->string;
+ found = TRUE;
+ if (no_labels)
+ printf("%s\n", CCS s);
+ else
+ printf("%slist %s = %s\n", types[i], name+1, CCS s);
+ }
+
+ if (!found)
+ printf("no address, domain, host, or local part list called \"%s\" "
+ "exists\n", name+1);
+
+ return found;
+ }
+
+ if ( Ustrcmp(name, "configure_file") == 0
+ || Ustrcmp(name, "config_file") == 0)
+ {
+ printf("%s\n", CS config_main_filename);
+ return TRUE;
+ }
+
+ if (Ustrcmp(name, "all") == 0)
+ {
+ for (optionlist * ol = optionlist_config;
+ ol < optionlist_config + nelem(optionlist_config); ol++)
+ if (!(ol->type & opt_hidden))
+ (void) print_ol(ol, US ol->name, NULL,
+ optionlist_config, nelem(optionlist_config),
+ no_labels);
+ return TRUE;
+ }
+
+ if (Ustrcmp(name, "local_scan") == 0)
+ {
+#ifndef LOCAL_SCAN_HAS_OPTIONS
+ printf("local_scan() options are not supported\n");
+ return FALSE;
+#else
+ for (optionlist * ol = local_scan_options;
+ ol < local_scan_options + local_scan_options_count; ol++)
+ (void) print_ol(ol, US ol->name, NULL, local_scan_options,
+ local_scan_options_count, no_labels);
+ return TRUE;
+#endif
+ }
+
+ if (Ustrcmp(name, "config") == 0)
+ {
+ print_config(f.admin_user, no_labels);
+ return TRUE;
+ }
+
+ 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 if (Ustrcmp(name, "environment") == 0)
+ {
+ if (environ)
+ {
+ uschar ** p;
+ for (p = USS environ; *p; p++) ;
+ qsort(environ, p - USS environ, sizeof(*p), string_compare_by_pointer);
+
+ for (p = USS environ; *p; p++)
+ {
+ uschar * q;
+ if (no_labels && (q = Ustrchr(*p, '='))) *q = '\0';
+ puts(CS *p);
+ }
+ }
+ return TRUE;
+ }
+
+ else
+ return print_ol(find_option(name,
+ optionlist_config, nelem(optionlist_config)),
+ name, NULL, optionlist_config, nelem(optionlist_config), no_labels);
+ }
+
+/* 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 (!f.admin_user)
+ {
+ fprintf(stderr, "exim: permission denied\n");
+ return FALSE;
+ }
+ for (macro_item * m = macros; m; m = m->next)
+ if (!name || Ustrcmp(name, m->name) == 0)
+ {
+ if (names_only)
+ printf("%s\n", CS m->name);
+ else if (no_labels)
+ printf("%s\n", CS m->replacement);
+ else
+ printf("%s=%s\n", CS m->name, CS m->replacement);
+ if (name)
+ return TRUE;
+ }
+ if (!name) return TRUE;
+
+ printf("%s %s not found\n", type, name);
+ return FALSE;
+ }
+
+if (names_only)
+ {
+ for (; d; d = d->next) printf("%s\n", CS d->name);
+ return TRUE;
+ }
+
+/* Either search for a given driver, or print all of them */
+
+for (; d; d = d->next)
+ {
+ BOOL rc = FALSE;
+ if (!name)
+ printf("\n%s %s:\n", d->name, type);
+ else if (Ustrcmp(d->name, name) != 0) continue;
+
+ for (optionlist * ol = ol2; ol < ol2 + size; ol++)
+ if (!(ol->type & opt_hidden))
+ rc |= print_ol(ol, US ol->name, d, ol2, size, no_labels);
+
+ for (optionlist * ol = d->info->options;
+ ol < d->info->options + *(d->info->options_count); ol++)
+ if (!(ol->type & opt_hidden))
+ rc |= print_ol(ol, US ol->name, d, d->info->options,
+ *d->info->options_count, no_labels);
+
+ if (name) return rc;
+ }
+if (!name) return TRUE;
+
+printf("%s %s not found\n", type, name);
+return FALSE;
+}
+
+
+
+/*************************************************
+* 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
+ hide do not output value on "-bP"
+
+Returns: nothing
+*/
+
+static void
+read_named_list(tree_node **anchorp, int *numberp, int max, uschar *s,
+ uschar *tname, BOOL hide)
+{
+BOOL forcecache = FALSE;
+uschar *ss;
+tree_node *t;
+namedlist_block * nb = store_get_perm(sizeof(namedlist_block), FALSE);
+
+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);
+
+Uskip_whitespace(&s);
+ss = s;
+while (isalnum(*s) || *s == '_') s++;
+t = store_get(sizeof(tree_node) + s-ss, ss);
+Ustrncpy(t->name, ss, s-ss);
+t->name[s-ss] = 0;
+Uskip_whitespace(&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;
+nb->hide = hide;
+
+if (*s++ != '=') log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+ "missing '=' after \"%s\"", t->name);
+Uskip_whitespace(&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);
+}
+
+
+
+
+/*************************************************
+* 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(BOOL nowarn)
+{
+int sep = 0;
+struct stat statbuf;
+uschar *s, *filename;
+const uschar *list = config_main_filelist;
+
+/* Loop through the possible file names */
+
+/* Should never be a tainted list */
+while((filename = string_nextinlist(&list, &sep, big_buffer, big_buffer_size)))
+ {
+
+ /* 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);
+ if (!(config_file = Ufopen(filename, "rb")))
+# 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)
+ {
+ 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)
+ {
+ *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 || 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)
+ {
+ uschar *last_slash = Ustrrchr(filename, '/');
+ config_filename = config_main_filename = string_copy(filename);
+
+ /* The config_main_directory we need for the $config_dir expansion.
+ config_main_filename we need for $config_file expansion.
+ And config_dir is the directory of the current configuration, used for
+ relative .includes. We do need to know it's name, as we change our working
+ directory later. */
+
+ if (filename[0] == '/')
+ config_main_directory = last_slash == filename ? US"/" : string_copyn(filename, last_slash - filename);
+ else
+ {
+ /* relative configuration file name: working dir + / + basename(filename) */
+
+ uschar buf[PATH_MAX];
+ gstring * g;
+
+ if (os_getcwd(buf, PATH_MAX) == NULL)
+ {
+ perror("exim: getcwd");
+ exit(EXIT_FAILURE);
+ }
+ g = string_cat(NULL, buf);
+
+ /* If the dir does not end with a "/", append one */
+ if (g->s[g->ptr-1] != '/')
+ g = string_catn(g, US"/", 1);
+
+ /* If the config file contains a "/", extract the directory part */
+ if (last_slash)
+ g = string_catn(g, filename, last_slash - filename);
+
+ config_main_directory = string_from_gstring(g);
+ }
+ config_directory = config_main_directory;
+ }
+else
+ if (!filename)
+ 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("configuration file %s", filename));
+
+/* Now, once we found and opened our configuration file, we change the directory
+to a safe place. Later we change to $spool_directory. */
+
+if (Uchdir("/") < 0)
+ {
+ perror("exim: chdir `/': ");
+ exit(EXIT_FAILURE);
+ }
+
+/* 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 (f.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);
+
+ /* Do a dummy store-allocation of a size related to the (toplevel) file size.
+ This assumes we will need this much storage to handle all the allocations
+ during startup; it won't help when .include is being used. When it does, it
+ will cut down on the number of store blocks (and malloc calls, and sbrk
+ syscalls). It also assume we're on the relevant pool. */
+
+ if (statbuf.st_size > 8192)
+ {
+ rmark r = store_mark();
+ void * dummy = store_get((int)statbuf.st_size, GET_UNTAINTED);
+ store_reset(r);
+ }
+ }
+
+/* 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()))
+ {
+ BOOL hide;
+ uschar * t;
+
+ if (config_lineno == 1 && Ustrstr(s, "\xef\xbb\xbf") == s)
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+ "found unexpected BOM (Byte Order Mark)");
+
+ if (isupper(*s))
+ {
+ if (!macro_read_assignment(s)) exim_exit(EXIT_FAILURE);
+ continue;
+ }
+
+ t = (hide = Ustrncmp(s, "hide", 4) == 0 && isspace(s[4])) ? s + 5 : s;
+
+ if (Ustrncmp(t, "domainlist", 10) == 0)
+ read_named_list(&domainlist_anchor, &domainlist_count,
+ MAX_NAMED_LIST, t+10, US"domain list", hide);
+
+ else if (Ustrncmp(t, "hostlist", 8) == 0)
+ read_named_list(&hostlist_anchor, &hostlist_count,
+ MAX_NAMED_LIST, t+8, US"host list", hide);
+
+ else if (Ustrncmp(t, "addresslist", 11) == 0)
+ read_named_list(&addresslist_anchor, &addresslist_count,
+ MAX_NAMED_LIST, t+11, US"address list", hide);
+
+ else if (Ustrncmp(t, "localpartlist", 13) == 0)
+ read_named_list(&localpartlist_anchor, &localpartlist_count,
+ MAX_NAMED_LIST, t+13, US"local part list", hide);
+
+ 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 && !*timezone_string) 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)
+ {
+ const 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
+ || match_isinlist(hostname, CUSS &dns_ipv4_lookup, 0, NULL, NULL,
+ MCL_DOMAIN, TRUE, NULL) != OK))
+ af = AF_INET6;
+#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)
+ { 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)
+ 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. */
+
+if (!(s = expand_string(spool_directory)))
+ 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)
+ {
+ const uschar *ss, *sss;
+ int sep = ':'; /* Fixed for log file path */
+ if (!(s = expand_string(log_file_path)))
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to expand log_file_path "
+ "\"%s\": %s", log_file_path, expand_string_message);
+
+ ss = s;
+ /* should never be a tainted list */
+ while ((sss = string_nextinlist(&ss, &sep, big_buffer, big_buffer_size)))
+ {
+ uschar *t;
+ if (sss[0] == 0 || Ustrcmp(sss, "syslog") == 0) continue;
+ if (!(t = Ustrstr(sss, "%s")))
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "log_file_path \"%s\" does not "
+ "contain \"%%s\"", sss);
+ *t = 'X';
+ if ((t = Ustrchr(sss, '%')))
+ 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)
+ {
+ 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)
+ {
+ if (!(s = expand_string(pid_file_path)))
+ 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 || !*process_log_path)
+ 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)
+ unpick_ratelimit(smtp_ratelimit_mail, &smtp_rlm_threshold,
+ &smtp_rlm_base, &smtp_rlm_factor, &smtp_rlm_limit);
+
+if (smtp_ratelimit_rcpt)
+ 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)
+ qualify_domain_sender = primary_hostname;
+if (!qualify_domain_recipient)
+ 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)
+ 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)
+ {
+ uschar *errmess;
+ int start, end, domain;
+ uschar *recipient = parse_extract_address(errors_reply_to, &errmess,
+ &start, &end, &domain, FALSE);
+
+ if (!recipient)
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+ "error in errors_reply_to (%s): %s", errors_reply_to, errmess);
+
+ if (!domain)
+ 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))
+ 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)
+ {
+ long int n;
+ uschar *end;
+ uschar *s = expand_string(host_number_string);
+
+ if (!s)
+ 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)
+ 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;
+ }
+
+#ifndef DISABLE_TLS
+/* If tls_verify_hosts is set, tls_verify_certificates must also be set */
+
+if ((tls_verify_hosts || tls_try_verify_hosts) && !tls_verify_certificates)
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+ "tls_%sverify_hosts is set, but tls_verify_certificates is not set",
+ tls_verify_hosts ? "" : "try_");
+
+/* 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)
+ {
+# 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
+ }
+#endif /*DISABLE_TLS*/
+
+if (!nowarn && !keep_environment && environ && *environ)
+ log_write(0, LOG_MAIN,
+ "Warning: purging the environment.\n"
+ " Suggested action: use keep_environment.");
+}
+
+
+
+/*************************************************
+* 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)
+{
+for (driver_info * dd = drivers_available; dd->driver_name[0] != 0;
+ dd = (driver_info *)((US dd) + size_of_info))
+ if (Ustrcmp(d->driver_name, dd->driver_name) == 0)
+ {
+ int len = dd->options_len;
+ d->info = dd;
+ d->options_block = store_get_perm(len, FALSE);
+ memcpy(d->options_block, dd->options_block, len);
+ for (int 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 */
+}
+
+
+
+
+static void
+driver_init_fini(driver_instance * d, const uschar * class)
+{
+if (!d->driver_name)
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+ "no driver defined for %s \"%s\"", class, d->name);
+(d->info->init)(d);
+}
+
+
+/*************************************************
+* 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()))
+ {
+ uschar name[EXIM_DRIVERNAME_MAX];
+ 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)
+ {
+ /* s is using big_buffer, so this call had better not */
+ driver_init_fini(d, class);
+ d = NULL;
+ }
+ if (!macro_read_assignment(buffer)) exim_exit(EXIT_FAILURE);
+ 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++ == ':')
+ {
+ /* Finish off initializing the previous driver. */
+
+ if (d)
+ driver_init_fini(d, class);
+
+ /* Check that we haven't already got a driver of this name */
+
+ for (d = *anchor; d; 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_perm(instance_size, FALSE);
+ memcpy(d, instance_default, instance_size);
+ *p = d;
+ p = &d->next;
+ d->name = string_copy(name);
+ d->srcfile = config_filename;
+ d->srcline = config_lineno;
+
+ /* Clear out the "set" bits in the generic options */
+
+ for (int 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. */
+
+ Uskip_whitespace(&s);
+ if (*s) 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)
+ 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 && d->driver_name)
+ 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)
+ 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)
+ driver_init_fini(d, class);
+}
+
+
+
+/*************************************************
+* 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);
+uschar *ss;
+
+for (optionlist * ol = d->info->options; ol < d->info->options + count; ol++)
+ if ((ol->type & opt_mask) == opt_stringptr)
+ {
+ void * options_block = ol->type & opt_public ? (void *)d : d->options_block;
+ uschar * value = *USS(US options_block + ol->v.offset);
+
+ if (value && (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(const uschar *pp, const uschar *p,
+ int *basic_errno, int *more_errno)
+{
+int len;
+const 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;
+ const 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 < nelem(extras); i++)
+ if (strncmpic(x, extras[i], xlen) == 0)
+ {
+ *more_errno = values[i];
+ break;
+ }
+
+ if (i >= nelem(extras))
+ 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 (strncmpic(pp, US"lookup", p - pp) == 0)
+ *basic_errno = ERRNO_UNKNOWNHOST;
+
+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(const uschar **paddr, int type)
+{
+const uschar *p = *paddr;
+const uschar *pp;
+
+if (*p++ != ',') log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "comma expected");
+
+Uskip_whitespace(&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;
+const uschar *p;
+
+while ((p = get_config_line()))
+ {
+ retry_rule **rchain;
+ const uschar *pp;
+ uschar *error;
+
+ next = store_get(sizeof(retry_config), GET_UNTAINTED);
+ 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);
+ Uskip_whitespace(&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)))
+ 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. */
+
+ Uskip_whitespace(&p);
+ if (Ustrncmp(p, "senders", 7) == 0)
+ {
+ p += 7;
+ Uskip_whitespace(&p);
+ if (*p++ != '=') log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+ "\"=\" expected after \"senders\" in retry rule");
+ Uskip_whitespace(&p);
+ next->senders = string_dequote(&p);
+ }
+
+ /* Now the retry rules. Keep the maximum timeout encountered. */
+
+ Uskip_whitespace(&p);
+
+ while (*p)
+ {
+ retry_rule * rule = store_get(sizeof(retry_rule), GET_UNTAINTED);
+ *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");
+
+ if (Uskip_whitespace(&p) == ';')
+ {
+ p++;
+ Uskip_whitespace(&p);
+ }
+ else if (*p)
+ 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)
+{
+#ifndef DISABLE_PIPE_CONNECT
+int nauths = 0;
+#endif
+
+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 (auth_instance * au = auths; au; au = au->next)
+ {
+ if (!au->public_name)
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "no public name specified for "
+ "the %s authenticator", au->name);
+
+ for (auth_instance * bu = au->next; bu; 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 && bu->client ? US"client" : US"server",
+ au->name, bu->name, au->public_name);
+#ifndef DISABLE_PIPE_CONNECT
+ nauths++;
+#endif
+ }
+#ifndef DISABLE_PIPE_CONNECT
+f.smtp_in_early_pipe_no_auth = nauths > 16;
+#endif
+}
+
+
+/* For error messages, a string describing the config location associated
+with current processing. NULL if we are not in an authenticator. */
+
+uschar *
+authenticator_current_name(void)
+{
+if (!authenticator_name) return NULL;
+return string_sprintf(" (authenticator %s, %s %d)", authenticator_name, driver_srcfile, driver_srcline);
+}
+
+
+
+
+
+/*************************************************
+* 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)
+ {
+ uschar name[EXIM_DRIVERNAME_MAX];
+ tree_node *node;
+ uschar *error;
+
+ p = readconf_readname(name, sizeof(name), acl_line);
+ if (isupper(*name) && *p == '=')
+ {
+ if (!macro_read_assignment(acl_line)) exim_exit(EXIT_FAILURE);
+ 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_perm(sizeof(tree_node) + Ustrlen(name), 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()))
+ (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 = nelem(section_list);
+ int mid = last/2;
+ int n = Ustrlen(next_section);
+
+ if (tolower(next_section[n-1]) != 's') Ustrcpy(next_section+n, US"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);
+}
+
+/* Init the storage for the pre-parsed config lines */
+void
+readconf_save_config(const uschar *s)
+{
+save_config_line(string_sprintf("# Exim Configuration (%s)",
+ f.running_in_test_harness ? US"X" : s));
+}
+
+static void
+save_config_position(const uschar *file, int line)
+{
+save_config_line(string_sprintf("# %d \"%s\"", line, file));
+}
+
+/* Append a pre-parsed logical line to the config lines store,
+this operates on a global (static) list that holds all the pre-parsed
+config lines, we do no further processing here, output formatting and
+honouring of <hide> or macros will be done during output */
+
+static void
+save_config_line(const uschar* line)
+{
+static config_line_item *current;
+config_line_item *next;
+
+next = (config_line_item*) store_get(sizeof(config_line_item), GET_UNTAINTED);
+next->line = string_copy(line);
+next->next = NULL;
+
+if (!config_lines) config_lines = next;
+else current->next = next;
+
+current = next;
+}
+
+/* List the parsed config lines, care about nice formatting and
+hide the <hide> values unless we're the admin user */
+void
+print_config(BOOL admin, BOOL terse)
+{
+const int TS = terse ? 0 : 2;
+int indent = 0;
+rmark r = NULL;
+
+for (const config_line_item * i = config_lines; i; i = i->next)
+ {
+ uschar * current, * p;
+
+ if (r) store_reset(r);
+ r = store_mark();
+
+ /* skip over to the first non-space */
+ for (current = string_copy(i->line); *current && isspace(*current); ++current)
+ ;
+
+ if (!*current)
+ continue;
+
+ /* Collapse runs of spaces. We stop this if we encounter one of the
+ following characters: "'$, as this may indicate careful formatting */
+
+ for (p = current; *p; p++) if (isspace(*p))
+ {
+ uschar *next;
+ if (*p != ' ') *p = ' ';
+
+ for (next = p; isspace(*next); ++next)
+ ;
+
+ if (next - p > 1)
+ memmove(p+1, next, Ustrlen(next)+1);
+
+ if (*next == '"' || *next == '\'' || *next == '$')
+ break;
+ }
+
+ /* # lines */
+ if (current[0] == '#')
+ puts(CCS current);
+
+ /* begin lines are left aligned */
+ else if (Ustrncmp(current, "begin", 5) == 0 && isspace(current[5]))
+ {
+ if (!terse) puts("");
+ puts(CCS current);
+ indent = TS;
+ }
+
+ /* router/acl/transport block names */
+ else if (current[Ustrlen(current)-1] == ':' && !Ustrchr(current, '='))
+ {
+ if (!terse) puts("");
+ printf("%*s%s\n", TS, "", current);
+ indent = 2 * TS;
+ }
+
+ /* hidden lines (all MACROS or lines prefixed with "hide") */
+ else if ( !admin
+ && ( isupper(*current)
+ || Ustrncmp(current, "hide", 4) == 0 && isspace(current[4])
+ )
+ )
+ {
+ if ((p = Ustrchr(current, '=')))
+ {
+ *p = '\0';
+ printf("%*s%s= %s\n", indent, "", current, CCS hidden);
+ }
+ /* e.g.: hide split_spool_directory */
+ else
+ printf("%*s\n", indent, CCS hidden);
+ }
+
+ else
+ /* rest is public */
+ printf("%*s%s\n", indent, "", current);
+ }
+if (r) store_reset(r);
+}
+
+#endif /*!MACRO_PREDEF*/
+/* vi: aw ai sw=2
+*/
+/* End of readconf.c */
diff --git a/src/receive.c b/src/receive.c
new file mode 100644
index 0000000..0a27c79
--- /dev/null
+++ b/src/receive.c
@@ -0,0 +1,4528 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Code for receiving a message and setting up spool files. */
+
+#include "exim.h"
+#include <setjmp.h>
+
+#ifdef EXPERIMENTAL_DCC
+extern int dcc_ok;
+#endif
+
+#ifdef SUPPORT_DMARC
+# include "dmarc.h"
+#endif
+
+/*************************************************
+* Local static variables *
+*************************************************/
+
+static int data_fd = -1;
+static uschar *spool_name = US"";
+
+enum CH_STATE {LF_SEEN, MID_LINE, CR_SEEN};
+
+#ifdef HAVE_LOCAL_SCAN
+jmp_buf local_scan_env; /* error-handling context for local_scan */
+unsigned had_local_scan_crash;
+unsigned had_local_scan_timeout;
+#endif
+
+
+/*************************************************
+* 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.) */
+
+uschar stdin_buf[4096];
+uschar * stdin_inptr = stdin_buf;
+uschar * stdin_inend = stdin_buf;
+
+static BOOL
+stdin_refill(void)
+{
+size_t rc = fread(stdin_buf, 1, sizeof(stdin_buf), stdin);
+if (rc <= 0)
+ {
+ if (had_data_timeout)
+ {
+ 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", NULL); /* Does not return */
+ }
+ if (had_data_sigint)
+ {
+ if (filter_test == FTEST_NONE)
+ {
+ fprintf(stderr, "\nexim: %s received - message abandoned\n",
+ had_data_sigint == SIGTERM ? "SIGTERM" : "SIGINT");
+ log_write(0, LOG_MAIN, "%s received while reading local message",
+ had_data_sigint == SIGTERM ? "SIGTERM" : "SIGINT");
+ }
+ receive_bomb_out(US"signal-exit", NULL); /* Does not return */
+ }
+ return FALSE;
+ }
+stdin_inend = stdin_buf + rc;
+stdin_inptr = stdin_buf;
+return TRUE;
+}
+
+int
+stdin_getc(unsigned lim)
+{
+if (stdin_inptr >= stdin_inend)
+ if (!stdin_refill())
+ return EOF;
+return *stdin_inptr++;
+}
+
+
+BOOL
+stdin_hasc(void)
+{
+return stdin_inptr < stdin_inend;
+}
+
+int
+stdin_ungetc(int c)
+{
+if (stdin_inptr <= stdin_buf)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "buffer underflow in stdin_ungetc");
+
+*--stdin_inptr = c;
+return c;
+}
+
+int
+stdin_feof(void)
+{
+return stdin_hasc() ? FALSE : 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 (f.trusted_caller) return TRUE;
+if (!newsender || !untrusted_set_sender) return FALSE;
+qnewsender = Ustrchr(newsender, '@')
+ ? newsender : string_sprintf("%s@%s", newsender, qualify_domain_sender);
+return match_address_list_basic(qnewsender, CUSS &untrusted_set_sender, 0) == 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_eximarith_t
+receive_statvfs(BOOL isspool, int *inodeptr)
+{
+#ifdef HAVE_STATFS
+struct STATVFS statbuf;
+struct stat dummy;
+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 */
+ const 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":";
+ /* should never be a tainted list */
+ while ((path = string_nextinlist(&p, &sep, buffer, sizeof(buffer))))
+ 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)
+ if (stat(CS path, &dummy) == -1 && errno == ENOENT)
+ { /* Can happen on first run after installation */
+ *inodeptr = -1;
+ return -1;
+ }
+ else
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "cannot accept message: failed to stat "
+ "%s directory %s: %s", name, path, 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_eximarith_t)(((double)statbuf.F_BAVAIL * (double)statbuf.F_FRSIZE)/1024.0);
+
+#else
+/* Unable to find partition sizes in this environment. */
+
+*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_eximarith_t space;
+int 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 = " PR_EXIM_ARITH "K inodes = %d "
+ "check_space = " PR_EXIM_ARITH "K inodes = %d msg_size = %d\n",
+ space, inodes, check_spool_space, check_spool_inodes, msg_size);
+
+ if ( space >= 0 && space + msg_size / 1024 < check_spool_space
+ || inodes >= 0 && inodes < check_spool_inodes)
+ {
+ log_write(0, LOG_MAIN, "spool directory space check failed: space="
+ PR_EXIM_ARITH " 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 = " PR_EXIM_ARITH "K inodes = %d "
+ "check_space = " PR_EXIM_ARITH "K 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=" PR_EXIM_ARITH
+ " 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 (spool_data_file)
+ {
+ (void)fclose(spool_data_file);
+ spool_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)
+{
+had_data_timeout = sig;
+}
+
+
+
+#ifdef HAVE_LOCAL_SCAN
+/*************************************************
+* local_scan() timeout *
+*************************************************/
+
+/* Handler function for timeouts that occur while running a local_scan()
+function. Posix recommends against calling longjmp() from a signal-handler,
+but the GCC manual says you can so we will, and trust that it's better than
+calling probably non-signal-safe funxtions during logging from within the
+handler, even with other compilers.
+
+See also https://cwe.mitre.org/data/definitions/745.html which also lists
+it as unsafe.
+
+This is all because we have no control over what might be written for a
+local-scan function, so cannot sprinkle had-signal checks after each
+call-site. At least with the default "do-nothing" function we won't
+ever get here.
+
+Argument: the signal number
+Returns: nothing
+*/
+
+static void
+local_scan_timeout_handler(int sig)
+{
+had_local_scan_timeout = sig;
+siglongjmp(local_scan_env, 1);
+}
+
+
+
+/*************************************************
+* 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)
+{
+had_local_scan_crash = sig;
+siglongjmp(local_scan_env, 1);
+}
+
+#endif /*HAVE_LOCAL_SCAN*/
+
+
+/*************************************************
+* 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)
+{
+had_data_sigint = sig;
+}
+
+
+
+/*************************************************
+* 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;
+
+ const int safe_recipients_limit = INT_MAX / 2 / sizeof(recipient_item);
+ if (recipients_list_max < 0 || recipients_list_max >= safe_recipients_limit)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Too many recipients: %d", recipients_list_max);
+ }
+
+ recipients_list_max = recipients_list_max ? 2*recipients_list_max : 50;
+ recipients_list = store_get(recipients_list_max * sizeof(recipient_item), GET_UNTAINTED);
+ if (oldlist)
+ 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
+recipients_list[recipients_count].orcpt = NULL;
+recipients_list[recipients_count].dsn_flags = 0;
+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, TRUE);
+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)
+{
+DEBUG(D_receive) debug_printf("receive_remove_recipient(\"%s\") called\n",
+ recipient);
+for (int 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;
+}
+
+
+
+
+
+/* Pause for a while waiting for input. If none received in that time,
+close the logfile, if we had one open; then if we wait for a long-running
+datasource (months, in one use-case) log rotation will not leave us holding
+the file copy. */
+
+static void
+log_close_chk(void)
+{
+if (!receive_timeout && !receive_hasc())
+ {
+ struct timeval t;
+ timesince(&t, &received_time);
+ if (t.tv_sec > 30*60)
+ mainlog_close();
+ else
+ if (poll_one_fd(0, POLLIN, (30*60 - t.tv_sec) * 1000) == 0)
+ mainlog_close();
+ }
+}
+
+/*************************************************
+* 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 (!f.dot_ends)
+ {
+ int last_ch = '\n';
+
+ for ( ;
+ log_close_chk(), (ch = (receive_getc)(GETC_BUFFER_UNLIMITED)) != 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 (log_close_chk(), (ch = (receive_getc)(GETC_BUFFER_UNLIMITED)) != 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') { body_linecount++; linelength = -1; }
+ else ch_state = 0;
+ 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;
+int linelength = 0;
+
+while ((ch = (receive_getc)(GETC_BUFFER_UNLIMITED)) != 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;
+ cutthrough_data_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;
+ }
+ /* The dot was removed at state 3. For a doubled dot, here, reinstate
+ it to cutthrough. The current ch, dot or not, is passed both to cutthrough
+ and to file below. */
+ if (ch == '.')
+ {
+ uschar c= ch;
+ cutthrough_data_puts(&c, 1);
+ }
+ ch_state = 1;
+ 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;
+ cutthrough_data_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)
+ {
+ if (fputc(ch, fout) == EOF) return END_WERROR;
+ if (message_size > thismessage_size_limit) return END_SIZE;
+ }
+ if(ch == '\n')
+ cutthrough_data_put_nl();
+ else
+ {
+ uschar c = ch;
+ cutthrough_data_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;
+}
+
+
+
+
+/* Variant of the above read_message_data_smtp() specialised for RFC 3030
+CHUNKING. Accept input lines separated by either CRLF or CR or LF and write
+LF-delimited spoolfile. Until we have wireformat spoolfiles, we need the
+body_linecount accounting for proper re-expansion for the wire, so use
+a cut-down version of the state-machine above; we don't need to do leading-dot
+detection and unstuffing.
+
+Arguments:
+ fout a FILE to which to write the message; NULL if skipping;
+ must be open for both writing and reading.
+
+Returns: One of the END_xxx values indicating why it stopped reading
+*/
+
+static int
+read_message_bdat_smtp(FILE * fout)
+{
+int linelength = 0, ch;
+enum CH_STATE ch_state = LF_SEEN;
+BOOL fix_nl = FALSE;
+
+for(;;)
+ {
+ switch ((ch = bdat_getc(GETC_BUFFER_UNLIMITED)))
+ {
+ case EOF: return END_EOF;
+ case ERR: return END_PROTOCOL;
+ case EOD:
+ /* Nothing to get from the sender anymore. We check the last
+ character written to the spool.
+
+ RFC 3030 states, that BDAT chunks are normal text, terminated by CRLF.
+ If we would be strict, we would refuse such broken messages.
+ But we are liberal, so we fix it. It would be easy just to append
+ the "\n" to the spool.
+
+ But there are some more things (line counting, message size calculation and such),
+ that would need to be duplicated here. So we simply do some ungetc
+ trickery.
+ */
+ if (fout)
+ {
+ if (fseek(fout, -1, SEEK_CUR) < 0) return END_PROTOCOL;
+ if (fgetc(fout) == '\n') return END_DOT;
+ }
+
+ if (linelength == -1) /* \r already seen (see below) */
+ {
+ DEBUG(D_receive) debug_printf("Add missing LF\n");
+ bdat_ungetc('\n');
+ continue;
+ }
+ DEBUG(D_receive) debug_printf("Add missing CRLF\n");
+ bdat_ungetc('\r'); /* not even \r was seen */
+ fix_nl = TRUE;
+
+ continue;
+ case '\0': body_zerocount++; break;
+ }
+ switch (ch_state)
+ {
+ case LF_SEEN: /* After LF or CRLF */
+ ch_state = MID_LINE;
+ /* fall through to handle as normal uschar. */
+
+ case MID_LINE: /* Mid-line state */
+ if (ch == '\n')
+ {
+ ch_state = LF_SEEN;
+ body_linecount++;
+ if (linelength > max_received_linelength)
+ max_received_linelength = linelength;
+ linelength = -1;
+ }
+ else if (ch == '\r')
+ {
+ ch_state = CR_SEEN;
+ if (fix_nl) bdat_ungetc('\n');
+ continue; /* don't write CR */
+ }
+ break;
+
+ case CR_SEEN: /* After (unwritten) CR */
+ body_linecount++;
+ if (linelength > max_received_linelength)
+ max_received_linelength = linelength;
+ linelength = -1;
+ if (ch == '\n')
+ ch_state = LF_SEEN;
+ else
+ {
+ message_size++;
+ if (fout && fputc('\n', fout) == EOF) return END_WERROR;
+ cutthrough_data_put_nl();
+ if (ch == '\r') continue; /* don't write CR */
+ ch_state = MID_LINE;
+ }
+ break;
+ }
+
+ /* Add the character to the spool file, unless skipping */
+
+ message_size++;
+ linelength++;
+ if (fout)
+ {
+ if (fputc(ch, fout) == EOF) return END_WERROR;
+ if (message_size > thismessage_size_limit) return END_SIZE;
+ }
+ if(ch == '\n')
+ cutthrough_data_put_nl();
+ else
+ {
+ uschar c = ch;
+ cutthrough_data_puts(&c, 1);
+ }
+ }
+/*NOTREACHED*/
+}
+
+static int
+read_message_bdat_smtp_wire(FILE * fout)
+{
+int ch;
+
+/* Remember that this message uses wireformat. */
+
+DEBUG(D_receive) debug_printf("CHUNKING: %s\n",
+ fout ? "writing spoolfile in wire format" : "flushing input");
+f.spool_file_wireformat = TRUE;
+
+for (;;)
+ {
+ if (chunking_data_left > 0)
+ {
+ unsigned len = MAX(chunking_data_left, thismessage_size_limit - message_size + 1);
+ uschar * buf = bdat_getbuf(&len);
+
+ if (!buf) return END_EOF;
+ message_size += len;
+ if (fout && fwrite(buf, len, 1, fout) != 1) return END_WERROR;
+ }
+ else switch (ch = bdat_getc(GETC_BUFFER_UNLIMITED))
+ {
+ case EOF: return END_EOF;
+ case EOD: return END_DOT;
+ case ERR: return END_PROTOCOL;
+
+ default:
+ message_size++;
+ /*XXX not done:
+ linelength
+ max_received_linelength
+ body_linecount
+ body_zerocount
+ */
+ if (fout && fputc(ch, fout) == EOF) return END_WERROR;
+ break;
+ }
+ if (message_size > thismessage_size_limit) return END_SIZE;
+ }
+/*NOTREACHED*/
+}
+
+
+
+
+/*************************************************
+* 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 = chunking_state <= CHUNKING_OFFERED
+ ? read_message_data_smtp(NULL)
+ : read_message_bdat_smtp_wire(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);
+smtp_notquit_exit(US"connection-lost", NULL, NULL);
+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;
+ eblock.text2 = US"";
+ 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 *last_received = NULL;
+
+switch(where)
+ {
+ case ACL_WHERE_DKIM:
+ case ACL_WHERE_MIME:
+ case ACL_WHERE_DATA:
+ if ( cutthrough.cctx.sock >= 0 && cutthrough.delivery
+ && (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)
+ {
+ DEBUG(D_receive|D_acl) debug_printf_indent(">>Headers removed by %s ACL:\n", acl_name);
+
+ for (header_line * h = header_list; h; h = h->next) if (h->type != htype_old)
+ {
+ const uschar * list = acl_removed_headers;
+ int sep = ':'; /* This is specified as a colon-separated list */
+ uschar *s;
+
+ while ((s = string_nextinlist(&list, &sep, NULL, 0)))
+ if (header_testname(h, s, Ustrlen(s), FALSE))
+ {
+ h->type = htype_old;
+ DEBUG(D_receive|D_acl) debug_printf_indent(" %s", h->text);
+ }
+ }
+ acl_removed_headers = NULL;
+ DEBUG(D_receive|D_acl) debug_printf_indent(">>\n");
+ }
+
+if (!acl_added_headers) return;
+DEBUG(D_receive|D_acl) debug_printf_indent(">>Headers added by %s ACL:\n", acl_name);
+
+for (header_line * h = acl_added_headers, * next; h; 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_indent(" (at top)");
+ break;
+
+ case htype_add_rec:
+ if (!last_received)
+ {
+ last_received = header_list;
+ while (!header_testname(last_received, US"Received", 8, FALSE))
+ last_received = last_received->next;
+ while (last_received->next &&
+ 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_indent(" (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 &&
+ ( (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_indent(" (before any non-Received: or Resent-*: header)");
+ break;
+
+ default:
+ h->next = NULL;
+ header_last->next = h;
+ DEBUG(D_receive|D_acl) debug_printf_indent(" ");
+ break;
+ }
+
+ if (!h->next) 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", h->text);
+ }
+
+acl_added_headers = NULL;
+DEBUG(D_receive|D_acl) debug_printf_indent(">>\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
+
+Returns: the extended string
+*/
+
+static gstring *
+add_host_info_for_log(gstring * g)
+{
+if (sender_fullhost)
+ {
+ if (LOGGING(dnssec) && sender_host_dnssec) /*XXX sender_helo_dnssec? */
+ g = string_catn(g, US" DS", 3);
+ g = string_append(g, 2, US" H=", sender_fullhost);
+ if (LOGGING(incoming_interface) && interface_address)
+ g = string_fmt_append(g, " I=[%s]:%d", interface_address, interface_port);
+ }
+if (f.tcp_in_fastopen && !f.tcp_in_fastopen_logged)
+ {
+ g = string_catn(g, US" TFO*", f.tcp_in_fastopen_data ? 5 : 4);
+ f.tcp_in_fastopen_logged = TRUE;
+ }
+if (sender_ident)
+ g = string_append(g, 2, US" U=", sender_ident);
+if (received_protocol)
+ g = string_append(g, 2, US" P=", received_protocol);
+if (LOGGING(pipelining) && f.smtp_in_pipelining_advertised)
+ {
+ g = string_catn(g, US" L", 2);
+#ifndef DISABLE_PIPE_CONNECT
+ if (f.smtp_in_early_pipe_used)
+ g = string_catn(g, US"*", 1);
+ else if (f.smtp_in_early_pipe_advertised)
+ g = string_catn(g, US".", 1);
+#endif
+ if (!f.smtp_in_pipelining_used)
+ g = string_catn(g, US"-", 1);
+ }
+return g;
+}
+
+
+
+#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 = NULL;
+unsigned long mbox_size;
+uschar *user_msg, *log_msg;
+int mime_part_count_buffer = -1;
+uschar * mbox_filename;
+int rc = OK;
+
+/* check if it is a MIME message */
+
+for (header_line * my_headerlist = header_list; my_headerlist;
+ my_headerlist = my_headerlist->next)
+ if ( my_headerlist->type != '*' /* skip deleted headers */
+ && 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;
+ }
+
+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 */
+if (!(mbox_file = spool_mbox(&mbox_size, NULL, &mbox_filename)))
+ { /* 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 (rfc822_file_path)
+ {
+ 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;
+ }
+ rfc822_file_path = NULL;
+ }
+
+/* check if we must check any message/rfc822 attachments */
+if (rc == OK)
+ {
+ uschar * scandir = string_copyn(mbox_filename,
+ Ustrrchr(mbox_filename, '/') - mbox_filename);
+ struct dirent * entry;
+ DIR * tempdir;
+
+ for (tempdir = exim_opendir(scandir); entry = readdir(tempdir); )
+ if (strncmpic(US entry->d_name, US"__rfc822_", 9) == 0)
+ {
+ rfc822_file_path = string_sprintf("%s/%s", scandir, entry->d_name);
+ DEBUG(D_receive)
+ debug_printf("RFC822 attachment detected: running MIME ACL for '%s'\n",
+ rfc822_file_path);
+ break;
+ }
+ closedir(tempdir);
+
+ if (rfc822_file_path)
+ {
+ if ((mbox_file = Ufopen(rfc822_file_path, "rb")))
+ {
+ /* set RFC822 expansion variable */
+ mime_is_rfc822 = 1;
+ mime_part_count_buffer = mime_part_count;
+ goto MIME_ACL_CHECK;
+ }
+ log_write(0, LOG_PANIC,
+ "acl_smtp_mime: can't open RFC822 spool file, skipping.");
+ unlink(CS rfc822_file_path);
+ }
+ }
+
+END_MIME_ACL:
+add_acl_headers(ACL_WHERE_MIME, US"MIME");
+if (rc == DISCARD)
+ {
+ recipients_count = 0;
+ *blackholed_by_ptr = US"MIME ACL";
+ cancel_cutthrough_connection(TRUE, US"mime acl discard");
+ }
+else if (rc != OK)
+ {
+ Uunlink(spool_name);
+ cancel_cutthrough_connection(TRUE, US"mime acl not ok");
+ unspool_mbox();
+#ifdef EXPERIMENTAL_DCC
+ dcc_ok = 0;
+#endif
+ if (smtp_input)
+ {
+ if (smtp_handle_acl_fail(ACL_WHERE_MIME, rc, user_msg, log_msg) != 0)
+ *smtp_yield_ptr = FALSE; /* No more messages 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 = expand_string(US"${tod_full}");
+header_line * received_header= header_list;
+
+if (recipients_count == 1) received_for = recipients_list[0].address;
+received = expand_string(received_header_text);
+received_for = NULL;
+
+if (!received)
+ {
+ 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])
+ {
+ received_header->text = string_sprintf("Received: ; %s\n", timestamp);
+ received_header->type = htype_old;
+ }
+else
+ {
+ received_header->text = string_sprintf("%s;\n\t%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 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 had_zero = 0;
+int prevlines_length = 0;
+const int id_resolution = BASE_62 == 62 ? 5000 : 10000;
+
+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;
+rmark rcvd_log_reset_point;
+gstring * g;
+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 */
+
+rmark reset_point;
+header_line *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;
+BOOL msgid_header_newly_created = FALSE;
+
+/* Variables for use when building the Received: header. */
+
+uschar *timestamp;
+int tslen;
+
+/* Time of creation of message_id */
+
+static struct timeval message_id_tv = { 0, 0 };
+
+
+/* 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(TRUE, US"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), GET_UNTAINTED);
+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.
+The data comes from the message, so is tainted. */
+
+reset_point = store_mark();
+next = store_get(sizeof(header_line), GET_UNTAINTED);
+next->text = store_get(header_size, GET_TAINTED);
+
+/* 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;
+spool_data_file = NULL;
+data_fd = -1;
+spool_name = US"";
+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;
+
+#ifdef WITH_CONTENT_SCAN
+/* reset non-per-part mime variables */
+mime_is_coverletter = 0;
+mime_is_rfc822 = 0;
+mime_part_count = -1;
+#endif
+
+#ifndef DISABLE_DKIM
+/* Call into DKIM to set up the context. In CHUNKING mode
+we clear the dot-stuffing flag */
+if (smtp_input && !smtp_batched_input && !f.dkim_disable_verify)
+ dkim_exim_verify_init(chunking_state <= CHUNKING_OFFERED);
+#endif
+
+#ifdef SUPPORT_DMARC
+if (sender_host_address) dmarc_init(); /* initialize libopendmarc */
+#endif
+
+/* In SMTP sessions we may receive several messages in one connection. Before
+each subsequent 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.
+Do this wait any time we have previously created a message-id, even if we
+rejected the message. This gives unique IDs for logging done by ACLs.
+The initial timestamp must have been obtained via exim_gettime() to avoid
+issues on Linux with suspend/resume. */
+
+if (message_id_tv.tv_sec)
+ {
+ message_id_tv.tv_usec = (message_id_tv.tv_usec/id_resolution) * id_resolution;
+ exim_wait_tick(&message_id_tv, id_resolution);
+ }
+
+/* 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.
+We use a routine that if possible uses a monotonic clock, and can be used again
+after reception for the tick-wait even under the Linux non-Posix behaviour. */
+
+else
+ exim_gettime(&message_id_tv);
+
+/* 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.
+For this we do not care about the Linux suspend/resume problem, so rather than
+use exim_gettime() everywhere we use a plain gettimeofday() here. */
+
+gettimeofday(&received_time, NULL);
+
+/* If SMTP input, set the special handler for timeouts. The alarm() calls
+happen in the smtp_getc() function when it refills its buffer. */
+
+had_data_timeout = 0;
+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. */
+
+had_data_sigint = 0;
+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)(GETC_BUFFER_UNLIMITED);
+
+ /* If we hit EOF on a SMTP connection, it's an error, since incoming
+ SMTP must have a correct "." terminator. */
+
+ if (smtp_input /* && !smtp_batched_input */)
+ if (ch == EOF)
+ {
+ smtp_reply = handle_lost_connection(US" (header)");
+ smtp_yield = FALSE;
+ goto TIDYUP; /* Skip to end of function */
+ }
+ else if (ch == ERR)
+ goto TIDYUP;
+
+ /* 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 release if
+ there were no allocations since the once that we want to free. */
+
+ if (ptr >= header_size - 4)
+ {
+ int oldsize = header_size;
+
+ if (header_size >= INT_MAX/2)
+ goto OVERSIZE;
+ header_size *= 2;
+
+ if (!store_extend(next->text, oldsize, header_size))
+ next->text = store_newblock(next->text, header_size, ptr);
+ }
+
+ /* 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 < 0) 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 (f.dot_ends && ptr == 0 && ch == '.')
+ {
+ ch = (receive_getc)(GETC_BUFFER_UNLIMITED);
+ if (ch == '\r')
+ {
+ ch = (receive_getc)(GETC_BUFFER_UNLIMITED);
+ if (ch != '\n')
+ {
+ if (ch >= 0) receive_ungetc(ch);
+ ch = '\r'; /* Revert to CR */
+ }
+ }
+ if (ch == '\n')
+ {
+ message_ended = END_DOT;
+ reset_point = store_reset(reset_point);
+ 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)(GETC_BUFFER_UNLIMITED);
+ 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. */
+
+ if (ch >= 0) (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)
+ {
+OVERSIZE:
+ 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",
+ f.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)
+ {
+ reset_point = store_reset(reset_point);
+ 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 >= 0)
+ {
+ int nextch = (receive_getc)(GETC_BUFFER_UNLIMITED);
+ if (nextch == ' ' || nextch == '\t')
+ {
+ next->text[ptr++] = nextch;
+ if (++message_size >= header_maxsize)
+ goto OVERSIZE;
+ continue; /* Iterate the loop */
+ }
+ else if (nextch >= 0) /* not EOF, ERR etc */
+ (receive_ungetc)(nextch); /* For next time */
+ else ch = nextch; /* 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_release_above(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
+ && verify_check_host(&ignore_fromline_hosts) == OK
+ )
+ || (!sender_host_address && ignore_fromline_local)
+ )
+ && regex_match_and_setup(regex_From, next->text, 0, -1)
+ )
+ {
+ if (!f.sender_address_forced)
+ {
+ uschar *uucp_sender = expand_string(uucp_from_sender);
+ if (!uucp_sender)
+ 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)
+ {
+ if (domain == 0 && newsender[0] != 0)
+ /* deconst ok as newsender was not const */
+ newsender = US rewrite_address_qualify(newsender, FALSE);
+
+ if (filter_test != FTEST_NONE || receive_check_set_sender(newsender))
+ {
+ sender_address = newsender;
+
+ if (f.trusted_caller || filter_test != FTEST_NONE)
+ {
+ authenticated_sender = NULL;
+ originator_name = US"";
+ f.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 (uschar * 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",
+ f.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-";
+ }
+ }
+
+ /* Reject CHUNKING messages that do not CRLF their first header line */
+
+ if (!first_line_ended_crlf && chunking_state > CHUNKING_OFFERED)
+ {
+ log_write(L_size_reject, LOG_MAIN|LOG_REJECT, "rejected from <%s>%s%s%s%s: "
+ "Non-CRLF-terminated header, under CHUNKING: message abandoned",
+ sender_address,
+ sender_fullhost ? " H=" : "", sender_fullhost ? sender_fullhost : US"",
+ sender_ident ? " U=" : "", sender_ident ? sender_ident : US"");
+ smtp_printf("552 Message header not CRLF terminated\r\n", FALSE);
+ bdat_flush_data();
+ smtp_reply = US"";
+ goto TIDYUP; /* Skip to end of function */
+ }
+
+ /* The line has been handled. If we have hit EOF, break out of the loop,
+ indicating no pending data line and no more data for the message */
+
+ if (ch < 0)
+ {
+ next = NULL;
+ if (ch == EOF) message_ended = END_DOT;
+ else if (ch == ERR) message_ended = END_PROTOCOL;
+ break;
+ }
+
+ /* Set up for the next header */
+
+ reset_point = store_mark();
+ header_size = 256;
+ next = store_get(sizeof(header_line), GET_UNTAINTED);
+ next->text = store_get(header_size, GET_TAINTED);
+ 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 (header_line * h = header_list->next; h; 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.
+In CHUNKING mode, a protocol error makes us give up on the message. */
+
+if (smtp_input)
+ if ((receive_feof)())
+ {
+ smtp_reply = handle_lost_connection(US" (after header)");
+ smtp_yield = FALSE;
+ goto TIDYUP; /* Skip to end of function */
+ }
+ else if (message_ended == END_PROTOCOL)
+ {
+ smtp_reply = US""; /* no reply needed */
+ goto TIDYUP;
+ }
+
+/* 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 (header_line * h = header_list->next; h; 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 && (!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 = !f.active_local_sender_retain
+ && ( f.sender_local && !f.trusted_caller && !f.suppress_local_fixups
+ || f.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)
+ {
+ const 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 (header_line * h = header_list->next; h; 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++;
+
+ f.parse_allow_group = TRUE; /* Allow address group syntax */
+
+ while (*s != 0)
+ {
+ uschar *ss = parse_find_address_end(s, FALSE);
+ uschar *recipient, *errmess, *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, s);
+ for (uschar * p = s; p < ss; p++) if (*p != '\n') *pp++ = *p;
+ *pp = 0;
+
+#ifdef SUPPORT_I18N
+ {
+ BOOL b = allow_utf8_domains;
+ allow_utf8_domains = TRUE;
+#endif
+ recipient = parse_extract_address(recipient, &errmess, &start, &end,
+ &domain, FALSE);
+
+#ifdef SUPPORT_I18N
+ if (recipient)
+ if (string_is_utf8(recipient)) message_smtputf8 = TRUE;
+ else allow_utf8_domains = b;
+ }
+#else
+ ;
+#endif
+
+ /* 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 && Ustrcmp(errmess, "empty address") != 0)
+ {
+ int len = Ustrlen(s);
+ error_block * b = store_get(sizeof(error_block), GET_UNTAINTED);
+ 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 */
+
+ f.parse_allow_group = FALSE; /* Reset group syntax flags */
+ f.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. The queue-sort code
+needs to know the layout. 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. */
+
+if (host_number_string)
+ 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
+ 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. */
+
+set_subdir_str(message_subdir, message_id, 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
+ && ((!sender_host_address && !f.suppress_local_fixups) || f.submission_mode))
+ {
+ uschar *id_text = US"";
+ uschar *id_domain = primary_hostname;
+ header_line * h;
+
+ /* Permit only letters, digits, dots, and hyphens in the domain */
+
+ if (message_id_domain)
+ {
+ uschar *new_id_domain = expand_string(message_id_domain);
+ if (!new_id_domain)
+ {
+ if (!f.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)
+ {
+ id_domain = new_id_domain;
+ for (uschar * p = id_domain; *p; 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)
+ {
+ uschar *new_id_text = expand_string(message_id_text);
+ if (!new_id_text)
+ {
+ if (!f.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)
+ {
+ id_text = new_id_text;
+ for (uschar * p = id_text; *p; 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. */
+
+ h = header_add_at_position_internal(!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);
+
+ /* Arrange for newly-created Message-Id to be logged */
+
+ if (!resents_exist)
+ {
+ msgid_header_newly_created = TRUE;
+ msgid_header = h;
+ }
+ }
+
+/* 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 (LOGGING(received_recipients))
+ {
+ raw_recipients = store_get(recipients_count * sizeof(uschar *), GET_UNTAINTED);
+ for (int 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). */
+
+DEBUG(D_rewrite)
+ { debug_printf_indent("qualify & rewrite recipients list\n"); acl_level++; }
+for (int i = 0; i < recipients_count; i++)
+ recipients_list[i].address = /* deconst ok as src was not cont */
+ US rewrite_address(recipients_list[i].address, TRUE, TRUE,
+ global_rewrite_rules, rewrite_existflags);
+DEBUG(D_rewrite) acl_level--;
+
+/* 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
+ && ((!sender_host_address && !f.suppress_local_fixups) || f.submission_mode))
+ {
+ const 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)
+ {
+ if (!f.trusted_caller || f.sender_name_forced ||
+ (!smtp_input && !f.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) oname = submission_name;
+
+ /* Envelope sender is empty */
+
+ if (!*sender_address)
+ {
+ uschar *fromstart, *fromend;
+
+ fromstart = string_sprintf("%sFrom: %s%s",
+ resent_prefix, oname, *oname ? " <" : "");
+ fromend = *oname ? US">" : US"";
+
+ if (f.sender_local || f.local_error_message)
+ header_add(htype_from, "%s%s@%s%s\n", fromstart,
+ local_part_quote(originator_login), qualify_domain_sender,
+ fromend);
+
+ else if (f.submission_mode && authenticated_id)
+ {
+ if (!submission_domain)
+ header_add(htype_from, "%s%s@%s%s\n", fromstart,
+ local_part_quote(authenticated_id), qualify_domain_sender,
+ fromend);
+
+ else if (!*submission_domain) /* 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 ? " <" : "",
+ sender_address_unrewritten ? sender_address_unrewritten : sender_address,
+ *oname ? ">" : "");
+
+ 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
+ && ( f.active_local_from_check
+ && ( f.sender_local && !f.trusted_caller && !f.suppress_local_fixups
+ || f.submission_mode && authenticated_id
+ ) ) )
+ {
+ 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;
+
+ generated_sender_address = f.submission_mode
+ ? !submission_domain
+ ? string_sprintf("%s@%s",
+ local_part_quote(authenticated_id), qualify_domain_sender)
+ : !*submission_domain /* empty => full address */
+ ? string_sprintf("%s", authenticated_id)
+ : string_sprintf("%s@%s",
+ local_part_quote(authenticated_id), submission_domain)
+ : 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)
+ {
+ int slen;
+ uschar *at = domain ? from_address + domain - 1 : NULL;
+
+ if (at) *at = 0;
+ from_address += route_check_prefix(from_address, local_from_prefix, NULL);
+ if ((slen = route_check_suffix(from_address, local_from_suffix, NULL)) > 0)
+ {
+ memmove(from_address+slen, from_address, Ustrlen(from_address)-slen);
+ from_address += slen;
+ }
+ if (at) *at = '@';
+
+ if ( strcmpic(generated_sender_address, from_address) == 0
+ || (!domain && 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 (f.submission_mode && !submission_name)
+ header_add(htype_sender, "%sSender: %s\n", resent_prefix,
+ generated_sender_address);
+ else
+ header_add(htype_sender, "%sSender: %s <%s>\n",
+ resent_prefix,
+ f.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 (f.submission_mode && *sender_address)
+ {
+ if (!sender_address_unrewritten)
+ 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. */
+
+DEBUG(D_rewrite)
+ { debug_printf("global rewrite rules\n"); acl_level++; }
+if (global_rewrite_rules && !sender_address_unrewritten && *sender_address)
+ {
+ /* deconst ok as src was not const */
+ sender_address = US rewrite_address(sender_address, FALSE, TRUE,
+ global_rewrite_rules, rewrite_existflags);
+ DEBUG(D_receive|D_rewrite)
+ debug_printf("rewritten sender = %s\n", sender_address);
+ }
+DEBUG(D_rewrite) acl_level--;
+
+
+/* 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. */
+
+DEBUG(D_rewrite)
+ { debug_printf("rewrite headers\n"); acl_level++; }
+for (header_line * h = header_list->next, * newh; h; h = h->next)
+ if ((newh = rewrite_header(h, NULL, NULL, global_rewrite_rules,
+ rewrite_existflags, TRUE)))
+ h = newh;
+DEBUG(D_rewrite) acl_level--;
+
+
+/* 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 && !f.suppress_local_fixups) || f.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 (header_line * h = header_list->next; h; 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;
+ }
+
+/*XXX CHUNKING: need to cancel cutthrough under BDAT, for now. In future,
+think more if it could be handled. Cannot do onward CHUNKING unless
+inbound is, but inbound chunking ought to be ok with outbound plain.
+Could we do onward CHUNKING given inbound CHUNKING?
+*/
+if (chunking_state > CHUNKING_OFFERED)
+ cancel_cutthrough_connection(FALSE, US"chunking active");
+
+/* 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.
+Having created it, send the headers to the destination. */
+
+if (cutthrough.cctx.sock >= 0 && cutthrough.delivery)
+ {
+ if (received_count > received_headers_max)
+ {
+ cancel_cutthrough_connection(TRUE, US"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 ? "H=" : "", sender_fullhost ? sender_fullhost : US"",
+ sender_ident ? "U=" : "", sender_ident ? sender_ident : US"");
+ 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. */
+
+spool_name = spool_fname(US"input", message_subdir, message_id, US"-D");
+DEBUG(D_receive) debug_printf("Data file name: %s\n", spool_name);
+
+if ((data_fd = Uopen(spool_name, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE)) < 0)
+ {
+ if (errno == ENOENT)
+ {
+ (void) directory_make(spool_directory,
+ spool_sname(US"input", message_subdir),
+ 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 (0 != exim_fchown(data_fd, exim_uid, exim_gid, spool_name))
+ 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. */
+
+spool_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(spool_data_file, "%s-D\n", message_id);
+if (next)
+ {
+ uschar *s = next->text;
+ int len = next->slen;
+ if (fwrite(s, 1, len, spool_data_file) == len) /* "if" for 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(spool_data_file) && !(receive_feof)() && message_ended != END_DOT)
+ {
+ if (smtp_input)
+ {
+ message_ended = chunking_state <= CHUNKING_OFFERED
+ ? read_message_data_smtp(spool_data_file)
+ : spool_wireformat
+ ? read_message_bdat_smtp_wire(spool_data_file)
+ : read_message_bdat_smtp(spool_data_file);
+ receive_linecount++; /* The terminating "." line */
+ }
+ else
+ message_ended = read_message_data(spool_data_file);
+
+ receive_linecount += body_linecount; /* For BSMTP errors mainly */
+ message_linecount += body_linecount;
+
+ switch (message_ended)
+ {
+ /* Handle premature termination of SMTP */
+
+ case END_EOF:
+ if (smtp_input)
+ {
+ Uunlink(spool_name); /* Lose data file when closed */
+ cancel_cutthrough_connection(TRUE, US"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 */
+ }
+ break;
+
+ /* 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. */
+
+ case END_SIZE:
+ Uunlink(spool_name); /* Lose the data file when closed */
+ cancel_cutthrough_connection(TRUE, US"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 ? " H=" : "",
+ sender_fullhost ? sender_fullhost : US"",
+ sender_ident ? " U=" : "",
+ sender_ident ? sender_ident : US"",
+ 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(spool_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, spool_data_file, header_list);
+ /* Does not return */
+ }
+ break;
+
+ /* Handle bad BDAT protocol sequence */
+
+ case END_PROTOCOL:
+ Uunlink(spool_name); /* Lose the data file when closed */
+ cancel_cutthrough_connection(TRUE, US"sender protocol error");
+ smtp_reply = US""; /* Response already sent */
+ message_id[0] = 0; /* Indicate no message accepted */
+ goto TIDYUP; /* Skip to end of function */
+ }
+ }
+
+/* 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(spool_data_file) == EOF || ferror(spool_data_file) ||
+ EXIMfsync(fileno(spool_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 ? sender_fullhost : sender_ident);
+
+ log_write(0, LOG_MAIN, "Message abandoned: %s", msg);
+ Uunlink(spool_name); /* Lose the data file */
+ cancel_cutthrough_connection(TRUE, US"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(spool_data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET);
+ give_local_error(ERRMESS_IOERR, msg, US"", error_rc, spool_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);
+gettimeofday(&received_time_complete, NULL);
+
+
+/* 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 || recipients_count == 0))
+ {
+ DEBUG(D_receive)
+ {
+ if (recipients_count == 0) debug_printf("*** No recipients\n");
+ if (bad_addresses)
+ {
+ debug_printf("*** Bad address(es)\n");
+ for (error_block * eblock = bad_addresses; eblock; eblock = eblock->next)
+ debug_printf(" %s: %s\n", eblock->text1, eblock->text2);
+ }
+ }
+
+ log_write(0, LOG_MAIN|LOG_PANIC, "%s %s found in headers",
+ message_id, bad_addresses ? "bad addresses" : "no recipients");
+
+ fseek(spool_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
+ ? recipients_list ? ERRMESS_BADADDRESS : ERRMESS_BADNOADDRESS
+ : extracted_ignored ? ERRMESS_IGADDRESS : ERRMESS_NOADDRESS,
+ bad_addresses, header_list, spool_data_file, FALSE
+ ) )
+ error_rc = bad_addresses ? EXIT_FAILURE : EXIT_NORECIPIENTS;
+ }
+ else
+ {
+ if (!bad_addresses)
+ 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 ? "es:\n" : ":");
+ for ( ; bad_addresses; bad_addresses = bad_addresses->next)
+ fprintf(stderr, " %s: %s\n", bad_addresses->text1,
+ bad_addresses->text2);
+ }
+ }
+
+ if (recipients_count == 0 || error_handling == ERRORS_STDERR)
+ {
+ Uunlink(spool_name);
+ (void)fclose(spool_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) /* 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;
+
+f.enable_dollar_recipients = TRUE;
+
+if (recipients_count == 0)
+ blackholed_by = f.recipients_discarded ? US"MAIL ACL" : US"RCPT ACL";
+
+else
+ {
+ /* Handle interactive SMTP messages */
+
+ if (smtp_input && !smtp_batched_input)
+ {
+
+#ifndef DISABLE_DKIM
+ if (!f.dkim_disable_verify)
+ {
+ /* Finish verification */
+ dkim_exim_verify_finish();
+
+ /* Check if we must run the DKIM ACL */
+ if (acl_smtp_dkim && dkim_verify_signers && *dkim_verify_signers)
+ {
+ uschar * dkim_verify_signers_expanded =
+ expand_string(dkim_verify_signers);
+ gstring * results = NULL;
+ int signer_sep = 0;
+ const uschar * ptr;
+ uschar * item;
+ gstring * seen_items = NULL;
+ int old_pool = store_pool;
+
+ store_pool = POOL_PERM; /* Allow created variables to live to data ACL */
+
+ if (!(ptr = dkim_verify_signers_expanded))
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "expansion of dkim_verify_signers option failed: %s",
+ expand_string_message);
+
+ /* Default to OK when no items are present */
+ rc = OK;
+ while ((item = string_nextinlist(&ptr, &signer_sep, NULL, 0)))
+ {
+ /* Prevent running ACL for an empty item */
+ if (!item || !*item) continue;
+
+ /* Only run ACL once for each domain or identity,
+ no matter how often it appears in the expanded list. */
+ if (seen_items)
+ {
+ uschar * seen_item;
+ const uschar * seen_items_list = string_from_gstring(seen_items);
+ int seen_sep = ':';
+ BOOL seen_this_item = FALSE;
+
+ while ((seen_item = string_nextinlist(&seen_items_list, &seen_sep,
+ NULL, 0)))
+ if (Ustrcmp(seen_item,item) == 0)
+ {
+ seen_this_item = TRUE;
+ break;
+ }
+
+ if (seen_this_item)
+ {
+ DEBUG(D_receive)
+ debug_printf("acl_smtp_dkim: skipping signer %s, "
+ "already seen\n", item);
+ continue;
+ }
+
+ seen_items = string_catn(seen_items, US":", 1);
+ }
+ seen_items = string_cat(seen_items, item);
+
+ rc = dkim_exim_acl_run(item, &results, &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(TRUE, US"dkim acl not ok");
+ break;
+ }
+ }
+ dkim_verify_status = string_from_gstring(results);
+ store_pool = old_pool;
+ add_acl_headers(ACL_WHERE_DKIM, US"DKIM");
+ if (rc == DISCARD)
+ {
+ recipients_count = 0;
+ blackholed_by = US"DKIM ACL";
+ if (log_msg)
+ 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 messages 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 */
+ }
+ }
+ else
+ dkim_exim_verify_log_all();
+ }
+#endif /* DISABLE_DKIM */
+
+#ifdef WITH_CONTENT_SCAN
+ if ( recipients_count > 0
+ && acl_smtp_mime
+ && !run_mime_acl(acl_smtp_mime, &smtp_yield, &smtp_reply, &blackholed_by)
+ )
+ goto TIDYUP;
+#endif /* WITH_CONTENT_SCAN */
+
+#ifdef SUPPORT_DMARC
+ dmarc_store_data(from_header);
+#endif
+
+#ifndef DISABLE_PRDR
+ if (prdr_requested && recipients_count > 1 && acl_smtp_data_prdr)
+ {
+ int all_pass = OK;
+ int all_fail = FAIL;
+
+ smtp_printf("353 PRDR content analysis beginning\r\n", TRUE);
+ /* Loop through recipients, responses must be in same order received */
+ for (unsigned int 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)
+ blackhole_log_msg = string_sprintf(": %s", log_msg);
+ cancel_cutthrough_connection(TRUE, US"data acl discard");
+ }
+ else if (rc != OK)
+ {
+ Uunlink(spool_name);
+ cancel_cutthrough_connection(TRUE, US"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 messages 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
+ && !run_mime_acl(acl_not_smtp_mime, &smtp_yield, &smtp_reply,
+ &blackholed_by)
+ )
+ goto TIDYUP;
+#endif /* WITH_CONTENT_SCAN */
+
+ if (acl_not_smtp)
+ {
+ uschar *user_msg, *log_msg;
+ f.authentication_local = TRUE;
+ 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)
+ 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)
+ log_write(0, log_reject_target, "F=<%s> rejected by non-SMTP ACL: %s",
+ sender_address, log_msg);
+
+ if (!user_msg) user_msg = US"local configuration problem";
+ if (smtp_batched_input)
+ moan_smtp_batch(NULL, "%d %s", 550, user_msg);
+ /* Does not return */
+ else
+ {
+ fseek(spool_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, spool_data_file,
+ header_list);
+ /* Does not return */
+ }
+ }
+ add_acl_headers(ACL_WHERE_NOTSMTP, US"non-SMTP");
+ }
+ }
+
+ /* The applicable ACLs have been run */
+
+ if (f.deliver_freeze) frozen_by = US"ACL"; /* for later logging */
+ if (f.queue_only_policy) queued_by = US"ACL";
+ }
+
+#ifdef WITH_CONTENT_SCAN
+unspool_mbox();
+#endif
+
+#ifdef EXPERIMENTAL_DCC
+dcc_ok = 0;
+#endif
+
+
+#ifdef HAVE_LOCAL_SCAN
+/* 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. */
+
+if (sigsetjmp(local_scan_env, 1) == 0)
+ {
+ had_local_scan_crash = 0;
+ 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;
+
+ had_local_scan_timeout = 0;
+ 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_CLR(0);
+ os_non_restarting_signal(SIGALRM, sigalrm_handler);
+
+ f.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);
+ }
+else
+ {
+ if (had_local_scan_crash)
+ {
+ log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() function crashed with "
+ "signal %d - message temporarily rejected (size %d)",
+ had_local_scan_crash, message_size);
+ receive_bomb_out(US"local-scan-error", US"local verification problem");
+ /* Does not return */
+ }
+ if (had_local_scan_timeout)
+ {
+ log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() function timed out - "
+ "message temporarily rejected (size %d)", message_size);
+ receive_bomb_out(US"local-scan-timeout", US"local verification problem");
+ /* Does not return */
+ }
+ }
+
+/* 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)
+ {
+ 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 (!f.deliver_freeze) /* ACL might have already frozen */
+ {
+ f.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 (!f.queue_only_policy) /* ACL might have already queued */
+ {
+ f.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)
+ for (uschar * s = local_scan_data; *s != 0; s++) if (*s == '\n') *s = ' ';
+ for (int i = 0; i < recipients_count; i++)
+ {
+ recipient_item *r = recipients_list + i;
+ r->address = rewrite_address_qualify(r->address, TRUE);
+ if (r->errors_to)
+ r->errors_to = rewrite_address_qualify(r->errors_to, TRUE);
+ }
+ if (recipients_count == 0 && !blackholed_by)
+ blackholed_by = US"local_scan";
+ }
+
+/* Message rejected: newlines permitted in local_scan_data to generate
+multiline SMTP responses. */
+
+else
+ {
+ uschar *istemp = US"";
+ uschar *smtp_code;
+ gstring * g;
+
+ 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:
+ BIT_CLEAR(log_selector, log_selector_size, Li_rejected_header);
+ /* Fall through */
+
+ case LOCAL_SCAN_REJECT:
+ smtp_code = US"550";
+ if (!errmsg) errmsg = US"Administrative prohibition";
+ break;
+
+ case LOCAL_SCAN_TEMPREJECT_NOLOGHDR:
+ BIT_CLEAR(log_selector, log_selector_size, Li_rejected_header);
+ /* Fall through */
+
+ case LOCAL_SCAN_TEMPREJECT:
+ TEMPREJECT:
+ smtp_code = US"451";
+ if (!errmsg) errmsg = US"Temporary local problem";
+ istemp = US"temporarily ";
+ break;
+ }
+
+ g = string_append(NULL, 2, US"F=",
+ sender_address[0] == 0 ? US"<>" : sender_address);
+ g = add_host_info_for_log(g);
+
+ log_write(0, LOG_MAIN|LOG_REJECT, "%s %srejected by local_scan(): %.256s",
+ string_from_gstring(g), 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(spool_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, spool_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);
+#endif /* HAVE_LOCAL_SCAN */
+
+
+/* Ensure the first time flag is set in the newly-received message. */
+
+f.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 timestamp 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)
+ {
+ f.deliver_freeze = FALSE;
+ f.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)
+ {
+ Uunlink(spool_name);
+ msg_size = 0; /* Compute size for log line */
+ for (header_line * h = header_list; h; 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(spool_data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET);
+ give_local_error(ERRMESS_IOERR, errmsg, US"", error_rc, spool_data_file,
+ header_list);
+ /* Does not return */
+ }
+ }
+
+
+/* The message has now been successfully received. */
+
+receive_messagecount++;
+
+/* 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. */
+
+if (fflush(spool_data_file))
+ {
+ errmsg = string_sprintf("Spool write error: %s", strerror(errno));
+ log_write(0, LOG_MAIN, "%s\n", 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(spool_data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET);
+ give_local_error(ERRMESS_IOERR, errmsg, US"", error_rc, spool_data_file,
+ header_list);
+ /* Does not return */
+ }
+ }
+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. 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 canonicalize
+it. */
+
+rcvd_log_reset_point = store_mark();
+g = string_get(256);
+
+g = string_append(g, 2,
+ fake_response == FAIL ? US"(= " : US"<= ",
+ sender_address[0] == 0 ? US"<>" : sender_address);
+if (message_reference)
+ g = string_append(g, 2, US" R=", message_reference);
+
+g = add_host_info_for_log(g);
+
+#ifndef DISABLE_TLS
+if (LOGGING(tls_cipher) && tls_in.cipher)
+ {
+ g = string_append(g, 2, US" X=", tls_in.cipher);
+# ifndef DISABLE_TLS_RESUME
+ if (LOGGING(tls_resumption) && tls_in.resumption & RESUME_USED)
+ g = string_catn(g, US"*", 1);
+# endif
+ }
+if (LOGGING(tls_certificate_verified) && tls_in.cipher)
+ g = string_append(g, 2, US" CV=", tls_in.certificate_verified ? "yes":"no");
+if (LOGGING(tls_peerdn) && tls_in.peerdn)
+ g = string_append(g, 3, US" DN=\"", string_printing(tls_in.peerdn), US"\"");
+if (LOGGING(tls_sni) && tls_in.sni)
+ g = string_append(g, 2, US" SNI=", string_printing2(tls_in.sni, SP_TAB|SP_SPACE));
+#endif
+
+if (sender_host_authenticated)
+ {
+ g = string_append(g, 2, US" A=", sender_host_authenticated);
+ if (authenticated_id)
+ {
+ g = string_append(g, 2, US":", authenticated_id);
+ if (LOGGING(smtp_mailauth) && authenticated_sender)
+ g = string_append(g, 2, US":", authenticated_sender);
+ }
+ }
+
+#ifndef DISABLE_PRDR
+if (prdr_requested)
+ g = string_catn(g, US" PRDR", 5);
+#endif
+
+#ifdef SUPPORT_PROXY
+if (proxy_session && LOGGING(proxy))
+ g = string_append(g, 2, US" PRX=", proxy_local_address);
+#endif
+
+if (chunking_state > CHUNKING_OFFERED)
+ g = string_catn(g, US" K", 2);
+
+g = string_fmt_append(g, " S=%d", msg_size);
+
+/* log 8BITMIME mode announced in MAIL_FROM
+ 0 ... no BODY= used
+ 7 ... 7BIT
+ 8 ... 8BITMIME */
+if (LOGGING(8bitmime))
+ g = string_fmt_append(g, " M8S=%d", body_8bitmime);
+
+#ifndef DISABLE_DKIM
+if (LOGGING(dkim) && dkim_verify_overall)
+ g = string_append(g, 2, US" DKIM=", dkim_verify_overall);
+# ifdef EXPERIMENTAL_ARC
+if (LOGGING(dkim) && arc_state && Ustrcmp(arc_state, "pass") == 0)
+ g = string_catn(g, US" ARC", 4);
+# endif
+#endif
+
+if (LOGGING(receive_time))
+ {
+ struct timeval diff = received_time_complete;
+ timediff(&diff, &received_time);
+ g = string_append(g, 2, US" RT=", string_timediff(&diff));
+ }
+
+if (*queue_name)
+ g = string_append(g, 2, US" Q=", queue_name);
+
+/* 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 ( LOGGING(msg_id) && msgid_header
+ && (LOGGING(msg_id_created) || !msgid_header_newly_created)
+ )
+ {
+ uschar * old_id;
+ BOOL save_allow_domain_literals = allow_domain_literals;
+ allow_domain_literals = TRUE;
+ int start, end, domain;
+
+ 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)
+ g = string_append(g, 2,
+ msgid_header_newly_created ? US" id*=" : 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 (LOGGING(subject) && subject_header)
+ {
+ 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 (int i = 0; i < 100 && ss[i] != 0; i++)
+ {
+ if (ss[i] == '\"' || ss[i] == '\\') *p++ = '\\';
+ *p++ = ss[i];
+ }
+ *p++ = '\"';
+ *p = 0;
+ g = string_append(g, 2, US" T=", string_printing(big_buffer));
+ }
+
+/* Terminate the string: string_cat() and string_append() leave room, but do
+not put the zero in. */
+
+(void) string_from_gstring(g);
+
+/* 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)
+ {
+ int fd;
+ uschar * m_name = spool_fname(US"msglog", message_subdir, message_id, US"");
+
+ if ( (fd = Uopen(m_name, O_WRONLY|O_APPEND|O_CREAT, SPOOL_MODE)) < 0
+ && errno == ENOENT
+ )
+ {
+ (void)directory_make(spool_directory,
+ spool_sname(US"msglog", message_subdir),
+ MSGLOG_DIRECTORY_MODE, TRUE);
+ fd = Uopen(m_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",
+ m_name, strerror(errno));
+ else
+ {
+ FILE *message_log = fdopen(fd, "a");
+ if (!message_log)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't fdopen message log %s: %s",
+ m_name, strerror(errno));
+ (void)close(fd);
+ }
+ else
+ {
+ uschar *now = tod_stamp(tod_log);
+ fprintf(message_log, "%s Received from %s\n", now, g->s+3);
+ if (f.deliver_freeze) fprintf(message_log, "%s frozen by %s\n", now,
+ frozen_by);
+ if (f.queue_only_policy) fprintf(message_log,
+ "%s no immediate delivery: queued%s%s by %s\n", now,
+ *queue_name ? " in " : "", *queue_name ? CS queue_name : "",
+ 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. */
+
+f.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_hasc(). 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 && !f.sender_host_notsocket
+ && !receive_hasc())
+ {
+ if (poll_one_fd(fileno(smtp_in), POLLIN, 0) != 0)
+ {
+ int c = (receive_getc)(GETC_BUFFER_UNLIMITED);
+ if (c != EOF) (receive_ungetc)(c);
+ else
+ {
+ smtp_notquit_exit(US"connection-lost", NULL, NULL);
+ smtp_reply = US""; /* No attempt to send a response */
+ smtp_yield = FALSE; /* Nothing more on this connection */
+
+ /* Re-use the log line workspace */
+
+ g->ptr = 0;
+ g = string_cat(g, US"SMTP connection lost after final dot");
+ g = add_host_info_for_log(g);
+ log_write(0, LOG_MAIN, "%s", string_from_gstring(g));
+
+ /* Delete the files for this aborted message. */
+
+ Uunlink(spool_name);
+ Uunlink(spool_fname(US"input", message_subdir, message_id, US"-H"));
+ Uunlink(spool_fname(US"msglog", message_subdir, message_id, US""));
+
+ 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 appropriately.
+ If temp-reject: normally accept to sender, keep the spooled file - unless defer=pass
+ in which case pass temp-reject back to initiator and dump the 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.cctx.sock >= 0 && cutthrough.delivery)
+ {
+ uschar * msg = cutthrough_finaldot(); /* Ask the target system to accept the message */
+ /* 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 */
+
+ case '4': /* Temp-reject. Keep spoolfiles and accept, unless defer-pass mode.
+ ... for which, pass back the exact error */
+ if (cutthrough.defer_pass) smtp_reply = string_copy_perm(msg, TRUE);
+ cutthrough_done = TMP_REJ; /* Avoid the usual immediate delivery attempt */
+ break; /* message_id needed for SMTP accept below */
+
+ default: /* Unknown response, or error. Treat as temp-reject. */
+ if (cutthrough.defer_pass) smtp_reply = US"450 Onward transmission not accepted";
+ 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 = string_copy_perm(msg, TRUE); /* 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 |
+ (LOGGING(received_recipients) ? LOG_RECIPIENTS : 0) |
+ (LOGGING(received_sender) ? LOG_SENDER : 0),
+ "%s", g->s);
+
+ /* Log any control actions taken by an ACL or local_scan(). */
+
+ if (f.deliver_freeze) log_write(0, LOG_MAIN, "frozen by %s", frozen_by);
+ if (f.queue_only_policy) log_write(L_delay_delivery, LOG_MAIN,
+ "no immediate delivery: queued%s%s by %s",
+ *queue_name ? " in " : "", *queue_name ? CS queue_name : "",
+ queued_by);
+ }
+f.receive_call_bombout = FALSE;
+
+/* The store for the main log message can be reused */
+rcvd_log_reset_point = store_reset(rcvd_log_reset_point);
+
+/* If the message is frozen, and freeze_tell is set, do the telling. */
+
+if (f.deliver_freeze && freeze_tell && freeze_tell[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, or a message was passed on
+by cutthrough delivery. (For a non-SMTP message we will have already given up
+because there's no point in carrying on!) For non-cutthrough 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.
+
+For cutthrough we hold the data file locked until we have deleted it, otherwise
+a queue-runner could grab it in the window.
+
+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? We can at least log it; if it is observed on some platform
+then we can think about properly declaring the message not-received. */
+
+
+TIDYUP:
+process_info[process_info_len] = 0; /* Remove message id */
+if (spool_data_file && cutthrough_done == NOT_TRIED)
+ {
+ if (fclose(spool_data_file)) /* Frees the lock */
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "spoolfile error on close: %s", strerror(errno));
+ spool_data_file = NULL;
+ }
+
+/* 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)
+ {
+ 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)
+ {
+ uschar *code = US"250";
+ int len = 3;
+ smtp_message_code(&code, &len, &user_msg, NULL, TRUE);
+ smtp_respond(code, len, TRUE, user_msg);
+ }
+
+ /* Default OK response */
+
+ else if (chunking_state > CHUNKING_OFFERED)
+ {
+ /* If there is more input waiting, no need to flush (probably the client
+ pipelined QUIT after data). We check only the in-process buffer, not
+ the socket. */
+
+ smtp_printf("250- %u byte chunk, total %d\r\n250 OK id=%s\r\n",
+ receive_hasc(),
+ chunking_datasize, message_size+message_linecount, message_id);
+ chunking_state = CHUNKING_OFFERED;
+ }
+ else
+ smtp_printf("250 OK id=%s\r\n", receive_hasc(), 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", FALSE, smtp_reply);
+
+ switch (cutthrough_done)
+ {
+ case ACCEPTED:
+ log_write(0, LOG_MAIN, "Completed");/* Delivery was done */
+ case PERM_REJ:
+ /* Delete spool files */
+ Uunlink(spool_name);
+ Uunlink(spool_fname(US"input", message_subdir, message_id, US"-H"));
+ Uunlink(spool_fname(US"msglog", message_subdir, message_id, US""));
+ break;
+
+ case TMP_REJ:
+ if (cutthrough.defer_pass)
+ {
+ Uunlink(spool_name);
+ Uunlink(spool_fname(US"input", message_subdir, message_id, US"-H"));
+ Uunlink(spool_fname(US"msglog", message_subdir, message_id, US""));
+ }
+ default:
+ break;
+ }
+ if (cutthrough_done != NOT_TRIED)
+ {
+ if (spool_data_file)
+ {
+ (void) fclose(spool_data_file); /* Frees the lock; do not care if error */
+ spool_data_file = NULL;
+ }
+ message_id[0] = 0; /* Prevent a delivery from starting */
+ cutthrough.delivery = cutthrough.callout_hold_only = FALSE;
+ cutthrough.defer_pass = 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)
+ 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)
+ {
+ const uschar *detail =
+#ifdef HAVE_LOCAL_SCAN
+ local_scan_data ? string_printing(local_scan_data) :
+#endif
+ 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
index 0000000..5c0f7c4
--- /dev/null
+++ b/src/regex.c
@@ -0,0 +1,217 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/*
+ * Copyright (c) The Exim Maintainers 2016 - 2022
+ * Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-2015
+ * 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 {
+ pcre2_code *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;
+
+static pcre_list *
+compile(const uschar * list)
+{
+int sep = 0;
+uschar *regex_string;
+pcre_list *re_list_head = NULL;
+pcre_list *ri;
+
+/* precompile our regexes */
+while ((regex_string = string_nextinlist(&list, &sep, NULL, 0)))
+ if (strcmpic(regex_string, US"false") != 0 && Ustrcmp(regex_string, "0") != 0)
+ {
+ pcre2_code * re;
+ int err;
+ PCRE2_SIZE pcre_erroffset;
+
+ /* compile our regular expression */
+ if (!(re = pcre2_compile( (PCRE2_SPTR) regex_string, PCRE2_ZERO_TERMINATED,
+ 0, &err, &pcre_erroffset, pcre_cmp_ctx)))
+ {
+ uschar errbuf[128];
+ pcre2_get_error_message(err, errbuf, sizeof(errbuf));
+ log_write(0, LOG_MAIN,
+ "regex acl condition warning - error in regex '%s': %s at offset %ld, skipped.",
+ regex_string, errbuf, (long)pcre_erroffset);
+ continue;
+ }
+
+ ri = store_get(sizeof(pcre_list), GET_UNTAINTED);
+ ri->re = re;
+ ri->pcre_text = regex_string;
+ ri->next = re_list_head;
+ re_list_head = ri;
+ }
+return re_list_head;
+}
+
+static int
+matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
+{
+pcre2_match_data * md = pcre2_match_data_create(REGEX_VARS + 1, pcre_gen_ctx);
+
+for (pcre_list * ri = re_list_head; ri; ri = ri->next)
+ {
+ int n;
+
+ /* try matcher on the line */
+ if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_mtc_ctx)) > 0)
+ {
+ Ustrncpy(regex_match_string_buffer, ri->pcre_text,
+ sizeof(regex_match_string_buffer)-1);
+ regex_match_string = regex_match_string_buffer;
+
+ for (int nn = 1; nn < n; nn++)
+ {
+ PCRE2_UCHAR * cstr;
+ PCRE2_SIZE cslen;
+ pcre2_substring_get_bynumber(md, nn, &cstr, &cslen);
+ regex_vars[nn-1] = CUS cstr;
+ }
+
+ return OK;
+ }
+ }
+pcre2_match_data_free(md);
+return FAIL;
+}
+
+int
+regex(const uschar **listptr)
+{
+unsigned long mbox_size;
+FILE *mbox_file;
+pcre_list *re_list_head;
+uschar *linebuffer;
+long f_pos = 0;
+int ret = FAIL;
+
+/* reset expansion variable */
+regex_match_string = NULL;
+
+if (!mime_stream) /* We are in the DATA ACL */
+ {
+ if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
+ { /* error while spooling */
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "regex acl condition: error while creating mbox spool file");
+ return DEFER;
+ }
+ }
+else
+ {
+ if ((f_pos = ftell(mime_stream)) < 0)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "regex acl condition: mime_stream: %s", strerror(errno));
+ return DEFER;
+ }
+ mbox_file = mime_stream;
+ }
+
+/* precompile our regexes */
+if (!(re_list_head = compile(*listptr)))
+ return FAIL; /* no regexes -> nothing to do */
+
+/* match each line against all regexes */
+linebuffer = store_get(32767, GET_TAINTED);
+while (fgets(CS linebuffer, 32767, mbox_file))
+ {
+ if ( mime_stream && mime_current_boundary /* check boundary */
+ && Ustrncmp(linebuffer, "--", 2) == 0
+ && Ustrncmp((linebuffer+2), mime_current_boundary,
+ Ustrlen(mime_current_boundary)) == 0)
+ break; /* found boundary */
+
+ if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
+ goto done;
+ }
+/* no matches ... */
+
+done:
+if (!mime_stream)
+ (void)fclose(mbox_file);
+else
+ {
+ clearerr(mime_stream);
+ if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "regex acl condition: mime_stream: %s", strerror(errno));
+ clearerr(mime_stream);
+ }
+ }
+
+return ret;
+}
+
+
+int
+mime_regex(const uschar **listptr)
+{
+pcre_list *re_list_head = NULL;
+FILE *f;
+uschar *mime_subject = NULL;
+int mime_subject_len = 0;
+int ret;
+
+/* reset expansion variable */
+regex_match_string = NULL;
+
+/* precompile our regexes */
+if (!(re_list_head = compile(*listptr)))
+ return FAIL; /* no regexes -> nothing to do */
+
+/* check if the file is already decoded */
+if (!mime_decoded_filename)
+ { /* no, decode it first */
+ const uschar *empty = US"";
+ mime_decode(&empty);
+ if (!mime_decoded_filename)
+ { /* decoding failed */
+ log_write(0, LOG_MAIN,
+ "mime_regex acl condition warning - could not decode MIME part to file");
+ return DEFER;
+ }
+ }
+
+/* open file */
+if (!(f = fopen(CS mime_decoded_filename, "rb")))
+ {
+ log_write(0, LOG_MAIN,
+ "mime_regex acl condition warning - can't open '%s' for reading",
+ mime_decoded_filename);
+ return DEFER;
+ }
+
+/* get 32k memory, tainted */
+mime_subject = store_get(32767, GET_TAINTED);
+
+mime_subject_len = fread(mime_subject, 1, 32766, f);
+
+ret = matcher(re_list_head, mime_subject, mime_subject_len);
+(void)fclose(f);
+return ret;
+}
+
+#endif /* WITH_CONTENT_SCAN */
diff --git a/src/retry.c b/src/retry.c
new file mode 100644
index 0000000..033afb4
--- /dev/null
+++ b/src/retry.c
@@ -0,0 +1,934 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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, const uschar *domain,
+ dbdata_retry *retry_record, time_t now)
+{
+BOOL address_timeout;
+retry_config * retry;
+
+DEBUG(D_retry)
+ {
+ debug_printf("retry time not reached: checking ultimate address timeout\n");
+ debug_printf(" now=" TIME_T_FMT " first_failed=" TIME_T_FMT
+ " next_try=" TIME_T_FMT " expired=%c\n",
+ now, retry_record->first_failed,
+ retry_record->next_try, retry_record->expired ? 'T' : 'F');
+ }
+
+retry = retry_find_config(retry_key+2, domain,
+ retry_record->basic_errno, retry_record->more_errno);
+
+if (retry && retry->rules)
+ {
+ retry_rule *last_rule;
+ for (last_rule = retry->rules; last_rule->next; last_rule = last_rule->next) ;
+ DEBUG(D_retry)
+ debug_printf(" received_time=" TIME_T_FMT " diff=%d timeout=%d\n",
+ received_time.tv_sec, (int)(now - received_time.tv_sec), last_rule->timeout);
+ address_timeout = (now - received_time.tv_sec > 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(const 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, * 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). */
+
+if ((node = tree_search(tree_unusable, host_key)))
+ {
+ 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, TRUE)))
+ {
+ 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)
+ {
+ 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)
+ {
+ 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)
+ {
+ *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 && !f.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)
+ {
+ *retry_message_key = message_key;
+ if (now < message_retry_record->next_try && !f.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), GET_UNTAINTED);
+host_item * host = addr->host_used;
+
+rti->next = addr->retries;
+addr->retries = rti;
+rti->key = key;
+rti->basic_errno = addr->basic_errno;
+rti->more_errno = addr->more_errno;
+rti->message = host
+ ? string_sprintf("H=%s [%s]: %s", host->name, host->address, addr->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(const uschar *key, const uschar *alternate, int basic_errno,
+ int more_errno)
+{
+const 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 copy 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)
+ key = isalnum(*key)
+ ? string_copyn(key, colon-key) /* the hostname */
+ : Ustrrchr(key, ':') + 1; /* Take from the last colon */
+
+/* Sort out the keys */
+
+if (!Ustrchr(key, '@')) key = string_sprintf("*@%s", key);
+if (alternate) alternate = string_sprintf("*@%s", alternate);
+
+/* Scan the configured retry items. */
+
+for (yield = retries; yield; yield = yield->next)
+ {
+ const uschar *plist = yield->pattern;
+ const 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
+ && ( !sender_address
+ || match_address_list_basic(sender_address, &slist, 0) != 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_basic(key, &plist, UCHAR_MAX+1) == OK
+ || ( alternate
+ && match_address_list_basic(alternate, &plist, UCHAR_MAX+1) == OK
+ ) )
+ break;
+ }
+
+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);
+
+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 (int 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))
+ {
+ BOOL timed_out = FALSE;
+
+ for (addr = endaddr; addr; addr = addr->parent)
+ {
+ int update_count = 0;
+ int timedout_count = 0;
+
+ DEBUG(D_retry) debug_printf(" %s%s\n", addr->address,
+ addr->retries ? "" : ": no retry items");
+
+ /* Loop for each retry item. */
+
+ for (retry_item * rti = addr->retries; rti; 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)
+ dbm_file = dbfn_open(US"retry", O_RDWR, &dbblock, TRUE, TRUE);
+
+ if (!dbm_file)
+ {
+ 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 && rti->flags & rf_message)
+ rti->flags |= rf_delete;
+
+ /* Handle the case of a request to delete the retry info for this
+ destination. */
+
+ if (rti->flags & rf_delete)
+ {
+ (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 ? addr->domain : NULL,
+ rti->basic_errno, rti->more_errno)))
+ {
+ DEBUG(D_retry) debug_printf("No configured retry item for %s%s%s\n",
+ rti->key,
+ rti->flags & rf_host ? US" or " : US"",
+ rti->flags & rf_host ? addr->domain : US"");
+ if (addr == endaddr) timedout_count++;
+ continue;
+ }
+
+ DEBUG(D_retry)
+ if (rti->flags & rf_host)
+ 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
+ ? US string_printing(rti->message)
+ : US"unknown error";
+ message_length = Ustrlen(message);
+ if (message_length > EXIM_DB_RLIMIT) message_length = EXIM_DB_RLIMIT;
+
+ /* 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_with_length(dbm_file, rti->key,
+ &message_space);
+ if ( retry_record
+ && now - retry_record->time_stamp > retry_data_expire)
+ retry_record = NULL;
+
+ if (!retry_record)
+ {
+ retry_record = store_get(sizeof(dbdata_retry) + message_length,
+ message);
+ 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 -= sizeof(dbdata_retry);
+
+ /* 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) && 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; 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)
+ 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.tv_sec <= retry_record->first_failed
+ && addr == endaddr
+ && !retry_record->expired
+ && rule)
+ {
+ retry_rule *last_rule;
+ for (last_rule = rule; last_rule->next; last_rule = last_rule->next)
+ ;
+ if (now - received_time.tv_sec > 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)
+ 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 we're using an old one, the read used
+ tainted memory so we're ok to write into it. */
+
+ if (message_length > message_space)
+ {
+ dbdata_retry * newr =
+ store_get(sizeof(dbdata_retry) + message_length, message);
+ 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 && 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
+ ? string_sprintf("%s: retry timeout exceeded", addr->message)
+ : US"retry timeout exceeded";
+ addr->user_message = addr->user_message
+ ? string_sprintf("%s: retry timeout exceeded", addr->user_message)
+ : US"retry timeout exceeded";
+ log_write(0, LOG_MAIN, "** %s%s%s%s: retry timeout exceeded",
+ addr->address,
+ addr->parent ? US" <" : US"",
+ addr->parent ? addr->parent->address : US"",
+ addr->parent ? 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) 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
index 0000000..005dc51
--- /dev/null
+++ b/src/rewrite.c
@@ -0,0 +1,825 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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
+*/
+
+const uschar *
+rewrite_address_qualify(const uschar *s, BOOL is_recipient)
+{
+return parse_find_at(s)
+ ? 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.
+*/
+
+const uschar *
+rewrite_one(const uschar *s, int flag, BOOL *whole, BOOL add_header, uschar *name,
+ rewrite_rule *rewrite_rules)
+{
+const uschar *yield = s;
+const uschar *subject = s;
+uschar *domain = NULL;
+BOOL done = FALSE;
+int rule_number = 1;
+int yield_start = 0, yield_end = 0;
+
+if (whole) *whole = FALSE;
+
+/* Scan the rewriting rules, ignoring any without matching flag */
+
+for (rewrite_rule * rule = rewrite_rules;
+ rule && !done;
+ rule_number++, rule = rule->next) if (rule->flags & flag)
+ {
+ int start, end, pdomain;
+ int count = 0;
+ uschar *save_localpart;
+ const uschar *save_domain;
+ uschar *error, *new;
+ const uschar * newparsed;
+
+ /* 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)
+ {
+ uschar *key = expand_string(rule->key);
+ if (!key)
+ {
+ if (!f.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) 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, CUSS &(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 = US 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)
+ {
+ if (f.expand_string_forcedfail)
+ { if (rule->flags & rewrite_quit) break; else continue; }
+
+ expand_string_message = expand_hide_passwords(expand_string_message);
+
+ 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)
+ {
+ 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)
+ {
+ 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 (LOGGING(address_rewrite) || (debug_selector & D_rewrite) != 0)
+ {
+ const uschar *where = CUS"?";
+
+ for (int 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 && (flag & rewrite_all_headers))
+ {
+ /* 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)
+ {
+ if (start > 0 && new[start-1] == '<')
+ {
+ uschar *p1 = new + start - 1;
+ uschar *p2 = new + end + 1;
+ const uschar *pf1, *pf2;
+
+ while (p1 > new && p1[-1] == ' ') p1--;
+ pf1 = parse_fix_phrase(new, p1 - new);
+ while (*p2 == ' ') p2++;
+ pf2 = parse_fix_phrase(p2, Ustrlen(p2));
+
+ start = Ustrlen(pf1) + start + new - p1;
+ end = start + Ustrlen(newparsed);
+ new = string_sprintf("%s%.*s%s", pf1, (int)(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) done = TRUE;
+
+ /* Allow the current rule to be applied up to 10 times if
+ requested. */
+
+ if (rule->flags & rewrite_repeat)
+ {
+ 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
+*/
+
+const uschar *
+rewrite_address(const 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)
+ {
+ const 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,
+ const uschar *routed_old, const uschar *routed_new,
+ rewrite_rule *rewrite_rules, int existflags, BOOL replace)
+{
+int lastnewline = 0;
+header_line *newh = NULL;
+rmark function_reset_point = store_mark();
+uschar *s = Ustrchr(h->text, ':') + 1;
+
+while (isspace(*s)) s++;
+
+DEBUG(D_rewrite)
+ debug_printf_indent("rewrite_one_header: type=%c:\n %s", h->type, h->text);
+
+f.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)
+ {
+ uschar *sprev;
+ uschar *ss = parse_find_address_end(s, FALSE);
+ uschar *recipient, *new;
+ rmark loop_reset_point = store_mark();
+ uschar *errmess = NULL;
+ 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 we got nothing, then there was any sort of error: non-parsable address,
+ empty address, overlong addres. Sometimes the result matters, sometimes not.
+ It seems this function is called for *any* header we see. */
+
+ if (!recipient)
+ {
+ /* Handle unparesable addresses in the header. Slightly ugly because a
+ null output from the extract can also result from a header without an
+ address, "To: undisclosed recpients:;" being the classic case. */
+
+ if ((rewrite_rules || routed_old) && Ustrcmp(errmess, "empty address") != 0)
+ {
+ log_write(0, LOG_MAIN, "rewrite: %s", errmess);
+ exim_exit(EXIT_FAILURE);
+ }
+ loop_reset_point = 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)
+ {
+ 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;
+ /* deconst ok as recipient was notconst */
+ new = US rewrite_address_qualify(recipient, is_recipient);
+ changed = (new != recipient);
+ recipient = new;
+
+ /* Can only qualify if permitted; if not, no rewrite. */
+
+ if (changed && ((is_recipient && !f.allow_unqualified_recipient) ||
+ (!is_recipient && !f.allow_unqualified_sender)))
+ {
+ loop_reset_point = 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)
+ {
+ BOOL whole;
+ /* deconst ok as recipient was notconst */
+ new = US 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) loop_reset_point = 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 ? newh : h;
+ uschar * newt = store_get_perm(prev->slen - oldlen + newlen + 4, GET_TAINTED);
+ 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, US"\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);
+ function_reset_point = store_mark();
+ newh = store_get(sizeof(header_line), GET_UNTAINTED);
+ newh->type = type;
+ newh->slen = slen;
+ newh->text = string_copyn(newtstart, slen);
+
+ /* Set up for scanning the rest of the header */
+
+ s = newh->text + remlen;
+ DEBUG(D_rewrite) debug_printf("remainder: %s", *s ? s : US"\n");
+ }
+ }
+
+f.parse_allow_group = FALSE; /* Reset group flags */
+f.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 && replace)
+ {
+ newh->next = h->next;
+ if (!newh->next) 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,
+ const uschar *routed_old, const uschar *routed_new,
+ rewrite_rule *rewrite_rules, int existflags, BOOL replace)
+{
+int flag;
+switch (h->type)
+ {
+ case htype_sender: flag = rewrite_sender; break;
+ case htype_from: flag = rewrite_from; break;
+ case htype_to: flag = rewrite_to; break;
+ case htype_cc: flag = rewrite_cc; break;
+ case htype_bcc: flag = rewrite_bcc; break;
+ case htype_reply_to: flag = rewrite_replyto; break;
+ default: return NULL;
+ }
+return rewrite_one_header(h, flag, routed_old, routed_new,
+ rewrite_rules, existflags, replace);
+}
+
+
+
+/************************************************
+* 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(const uschar *s)
+{
+uschar *recipient, *error;
+int 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)
+ {
+ const 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)
+ {
+ if (!done_smtp)
+ printf("Syntax error in %s\n%c%s\n", s, toupper(error[0]), error+1);
+ return;
+ }
+
+for (int i = 0; i < 8; i++)
+ {
+ BOOL whole = FALSE;
+ int flag = 1 << i;
+ const 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
index 0000000..1ed1dd8
--- /dev/null
+++ b/src/rfc2047.c
@@ -0,0 +1,345 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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, string); /* No longer than this */
+
+while (*string != 0)
+ {
+ 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 = 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, const uschar *target,
+ int zeroval, int *lenptr, int *sizeptr, uschar **error)
+{
+int size = Ustrlen(string);
+size_t dlen;
+uschar *dptr;
+gstring *yield;
+uschar *mimeword, *q1, *q2, *endword;
+
+*error = NULL;
+mimeword = decode_mimeword(string, lencheck, &q1, &q2, &endword, &dlen, &dptr);
+
+if (!mimeword)
+ {
+ if (lenptr) *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(sizeof(gstring) + ++size, string);
+yield->size = size;
+yield->ptr = 0;
+yield->s = US(yield + 1);
+
+while (mimeword)
+ {
+
+ #if HAVE_ICONV
+ iconv_t icd = (iconv_t)(-1);
+ #endif
+
+ if (mimeword != string)
+ yield = string_catn(yield, string, mimeword - string);
+/*XXX that might have to convert an untainted string to a tainted one */
+
+ /* 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 && strcmpic(target, mimeword+2) != 0)
+ if ((icd = iconv_open(CS target, CS(mimeword+2))) == (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)
+ for (int i = 0; i < tlen; i++)
+ if (tptr[i] == 0) tptr[i] = zeroval;
+
+ /* Add the new string onto the result */
+
+ yield = string_catn(yield, 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)
+ {
+ 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, string);
+
+if (lenptr) *lenptr = yield->ptr;
+if (sizeptr) *sizeptr = yield->size;
+return string_from_gstring(yield);
+}
+
+
+/* This is the stub that provides the original interface without the sizeptr
+argument. */
+
+uschar *
+rfc2047_decode(uschar *string, BOOL lencheck, const 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
index 0000000..fa69b8b
--- /dev/null
+++ b/src/route.c
@@ -0,0 +1,2072 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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. */
+#define LOFF(field) OPT_OFF(router_instance, field)
+
+optionlist optionlist_routers[] = {
+ { "*expand_group", opt_stringptr | opt_hidden | opt_public,
+ LOFF(expand_gid) },
+ { "*expand_more", opt_stringptr | opt_hidden | opt_public,
+ LOFF(expand_more) },
+ { "*expand_unseen", opt_stringptr | opt_hidden | opt_public,
+ LOFF(expand_unseen) },
+ { "*expand_user", opt_stringptr | opt_hidden | opt_public,
+ LOFF(expand_uid) },
+ { "*set_group", opt_bool | opt_hidden | opt_public,
+ LOFF(gid_set) },
+ { "*set_user", opt_bool | opt_hidden | opt_public,
+ LOFF(uid_set) },
+ { "address_data", opt_stringptr|opt_public,
+ LOFF(address_data) },
+ { "address_test", opt_bool|opt_public,
+ LOFF(address_test) },
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+ { "bmi_deliver_alternate", opt_bool | opt_public,
+ LOFF(bmi_deliver_alternate) },
+ { "bmi_deliver_default", opt_bool | opt_public,
+ LOFF(bmi_deliver_default) },
+ { "bmi_dont_deliver", opt_bool | opt_public,
+ LOFF(bmi_dont_deliver) },
+ { "bmi_rule", opt_stringptr|opt_public,
+ LOFF(bmi_rule) },
+#endif
+ { "cannot_route_message", opt_stringptr | opt_public,
+ LOFF(cannot_route_message) },
+ { "caseful_local_part", opt_bool | opt_public,
+ LOFF(caseful_local_part) },
+ { "check_local_user", opt_bool | opt_public,
+ LOFF(check_local_user) },
+ { "condition", opt_stringptr|opt_public|opt_rep_con,
+ LOFF(condition) },
+ { "debug_print", opt_stringptr | opt_public,
+ LOFF(debug_string) },
+ { "disable_logging", opt_bool | opt_public,
+ LOFF(disable_logging) },
+ { "dnssec_request_domains", opt_stringptr|opt_public,
+ LOFF(dnssec.request) },
+ { "dnssec_require_domains", opt_stringptr|opt_public,
+ LOFF(dnssec.require) },
+ { "domains", opt_stringptr|opt_public,
+ LOFF(domains) },
+ { "driver", opt_stringptr|opt_public,
+ LOFF(driver_name) },
+ { "dsn_lasthop", opt_bool|opt_public,
+ LOFF(dsn_lasthop) },
+ { "errors_to", opt_stringptr|opt_public,
+ LOFF(errors_to) },
+ { "expn", opt_bool|opt_public,
+ LOFF(expn) },
+ { "fail_verify", opt_bool_verify|opt_hidden|opt_public,
+ LOFF(fail_verify_sender) },
+ { "fail_verify_recipient", opt_bool|opt_public,
+ LOFF(fail_verify_recipient) },
+ { "fail_verify_sender", opt_bool|opt_public,
+ LOFF(fail_verify_sender) },
+ { "fallback_hosts", opt_stringptr|opt_public,
+ LOFF(fallback_hosts) },
+ { "group", opt_expand_gid | opt_public,
+ LOFF(gid) },
+ { "headers_add", opt_stringptr|opt_public|opt_rep_str,
+ LOFF(extra_headers) },
+ { "headers_remove", opt_stringptr|opt_public|opt_rep_str,
+ LOFF(remove_headers) },
+ { "ignore_target_hosts",opt_stringptr|opt_public,
+ LOFF(ignore_target_hosts) },
+ { "initgroups", opt_bool | opt_public,
+ LOFF(initgroups) },
+ { "local_part_prefix", opt_stringptr|opt_public,
+ LOFF(prefix) },
+ { "local_part_prefix_optional",opt_bool|opt_public,
+ LOFF(prefix_optional) },
+ { "local_part_suffix", opt_stringptr|opt_public,
+ LOFF(suffix) },
+ { "local_part_suffix_optional",opt_bool|opt_public,
+ LOFF(suffix_optional) },
+ { "local_parts", opt_stringptr|opt_public,
+ LOFF(local_parts) },
+ { "log_as_local", opt_bool|opt_public,
+ LOFF(log_as_local) },
+ { "more", opt_expand_bool|opt_public,
+ LOFF(more) },
+ { "pass_on_timeout", opt_bool|opt_public,
+ LOFF(pass_on_timeout) },
+ { "pass_router", opt_stringptr|opt_public,
+ LOFF(pass_router_name) },
+ { "redirect_router", opt_stringptr|opt_public,
+ LOFF(redirect_router_name) },
+ { "require_files", opt_stringptr|opt_public,
+ LOFF(require_files) },
+ { "retry_use_local_part", opt_bool|opt_public,
+ LOFF(retry_use_local_part) },
+ { "router_home_directory", opt_stringptr|opt_public,
+ LOFF(router_home_directory) },
+ { "self", opt_stringptr|opt_public,
+ LOFF(self) },
+ { "senders", opt_stringptr|opt_public,
+ LOFF(senders) },
+ { "set", opt_stringptr|opt_public|opt_rep_str,
+ LOFF(set) },
+ #ifdef SUPPORT_TRANSLATE_IP_ADDRESS
+ { "translate_ip_address", opt_stringptr|opt_public,
+ LOFF(translate_ip_address) },
+ #endif
+ { "transport", opt_stringptr|opt_public,
+ LOFF(transport_name) },
+ { "transport_current_directory", opt_stringptr|opt_public,
+ LOFF(current_directory) },
+ { "transport_home_directory", opt_stringptr|opt_public,
+ LOFF(home_directory) },
+ { "unseen", opt_expand_bool|opt_public,
+ LOFF(unseen) },
+ { "user", opt_expand_uid | opt_public,
+ LOFF(uid) },
+ { "verify", opt_bool_verify|opt_hidden|opt_public,
+ LOFF(verify_sender) },
+ { "verify_only", opt_bool|opt_public,
+ LOFF(verify_only) },
+ { "verify_recipient", opt_bool|opt_public,
+ LOFF(verify_recipient) },
+ { "verify_sender", opt_bool|opt_public,
+ LOFF(verify_sender) }
+};
+
+int optionlist_routers_size = nelem(optionlist_routers);
+
+
+#ifdef MACRO_PREDEF
+
+# include "macro_predef.h"
+
+void
+options_routers(void)
+{
+uschar buf[64];
+
+options_from_list(optionlist_routers, nelem(optionlist_routers), US"ROUTERS", NULL);
+
+for (router_info * ri = routers_available; ri->driver_name[0]; ri++)
+ {
+ spf(buf, sizeof(buf), US"_DRIVER_ROUTER_%T", ri->driver_name);
+ builtin_macro_create(buf);
+ options_from_list(ri->options, (unsigned)*ri->options_count, US"ROUTER", ri->driver_name);
+ }
+}
+
+#else /*!MACRO_PREDEF*/
+
+/*************************************************
+* 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; rr = rr->next)
+ {
+ if (Ustrcmp(name, rr->name) == 0)
+ {
+ *ptr = rr;
+ break;
+ }
+ if (rr == r) afterthis = TRUE;
+ }
+
+if (!rr)
+ 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)
+{
+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 (router_instance * r = routers; r; 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)
+ && !r->transport_name && !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) && r->transport_name)
+ 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 || r->local_parts || r->condition || r->prefix || r->suffix || r->senders || r->require_files;
+
+ /* Build a host list if fallback hosts is set. */
+
+ {
+ int old_pool = store_pool;
+ store_pool = POOL_PERM;
+ host_build_hostlist(&r->fallback_hostlist, r->fallback_hosts, FALSE);
+ store_pool = old_pool;
+ }
+
+ /* Check redirect_router and pass_router are valid */
+
+ if (r->redirect_router_name)
+ set_router(r, r->redirect_router_name, &(r->redirect_router), FALSE);
+
+ if (r->pass_router_name)
+ set_router(r, r->pass_router_name, &(r->pass_router), TRUE);
+
+#ifdef notdef
+ DEBUG(D_route) debug_printf("DSN: %s %s\n", r->name,
+ r->dsn_lasthop ? "lasthop set" : "propagating DSN");
+#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)
+{
+for (router_instance * r = routers; r; r = r->next)
+ if (r->info->tidyup) (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
+ vp if set, pointer to place for size of wildcard portion
+
+Returns: length of matching prefix or zero
+*/
+
+int
+route_check_prefix(const uschar * local_part, const uschar * prefixes,
+ unsigned * vp)
+{
+int sep = 0;
+uschar *prefix;
+const uschar *listptr = prefixes;
+
+while ((prefix = string_nextinlist(&listptr, &sep, NULL, 0)))
+ {
+ int plen = Ustrlen(prefix);
+ if (prefix[0] == '*')
+ {
+ prefix++;
+ for (const uschar * p = local_part + Ustrlen(local_part) - (--plen);
+ p >= local_part; p--)
+ if (strncmpic(prefix, p, plen) == 0)
+ {
+ unsigned vlen = p - local_part;
+ if (vp) *vp = vlen;
+ return plen + vlen;
+ }
+ }
+ else
+ if (strncmpic(prefix, local_part, plen) == 0)
+ {
+ if (vp) *vp = 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
+ vp if set, pointer to place for size of wildcard portion
+
+Returns: length of matching suffix or zero
+*/
+
+int
+route_check_suffix(const uschar * local_part, const uschar * suffixes,
+ unsigned * vp)
+{
+int sep = 0;
+int alen = Ustrlen(local_part);
+uschar *suffix;
+const uschar *listptr = suffixes;
+
+while ((suffix = string_nextinlist(&listptr, &sep, NULL, 0)))
+ {
+ int slen = Ustrlen(suffix);
+ if (suffix[slen-1] == '*')
+ {
+ const uschar * pend = local_part + alen - (--slen) + 1;
+ for (const uschar * p = local_part; p < pend; p++)
+ if (strncmpic(suffix, p, slen) == 0)
+ {
+ int tlen = alen - (p - local_part);
+ if (vp) *vp = tlen - slen;
+ return tlen;
+ }
+ }
+ else
+ if (alen > slen && strncmpic(suffix, local_part + alen - slen, slen) == 0)
+ {
+ if (vp) *vp = 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, const uschar *list,
+ tree_node **anchorptr, unsigned int *cache_bits, int listtype,
+ const uschar *domloc, const uschar **ldata, BOOL caseless, uschar **perror)
+{
+if (!list) 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. */
+
+switch(domloc
+ ? match_isinlist(domloc, &list, 0, anchorptr, cache_bits, listtype,
+ caseless, ldata)
+ : match_address_list(sender_address ? sender_address : US"",
+ TRUE, TRUE, &list, cache_bits, -1, 0, CUSS &sender_data)
+ )
+ {
+ 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) return FALSE;
+
+while ((slash = Ustrchr(sp, '/')))
+ {
+ *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
+*/
+
+static int
+check_files(const 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;
+const uschar *listptr;
+uschar *check;
+
+if (!s) return OK;
+
+DEBUG(D_route) debug_printf("checking require_files\n");
+
+listptr = s;
+while ((check = string_nextinlist(&listptr, &sep, NULL, 0)))
+ {
+ int rc;
+ int eacces_code = 0;
+ BOOL invert = FALSE;
+ struct stat statbuf;
+ uschar *ss = expand_string(check);
+
+ if (!ss)
+ {
+ if (f.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 = exim_fork(US"require-files");
+
+ /* If fork() fails, reinstate the original error and behave as if
+ this block of code were not present. This is the same behaviour 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))
+ exim_underbar_exit(EXIT_SUCCESS);
+ DEBUG(D_route) debug_printf("route_check_access() failed\n");
+ exim_underbar_exit(EXIT_FAILURE);
+ }
+
+ /* 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 EACCES 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);
+f.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 (f.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, CUSS &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 && !addr->suffix)
+ {
+ 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)
+ check_local_part += Ustrlen(addr->prefix);
+ if (addr->suffix)
+ 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, CUSS &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} and local_part_data. */
+
+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;
+ }
+ addr->prop.localpart_data =
+ deliver_localpart_data = string_copy(US (*pw)->pw_name);
+ 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)
+ {
+ uschar * router_home = expand_string(r->router_home_directory);
+ if (router_home)
+ {
+ setflag(addr, af_home_expanded); /* Note set from router_home_directory */
+ deliver_home = router_home;
+ }
+ else if (!f.expand_string_forcedfail)
+ {
+ *perror = string_sprintf("failed to expand \"%s\" for "
+ "router_home_directory: %s", r->router_home_directory,
+ expand_string_message);
+ return DEFER;
+ }
+ }
+
+/* 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 possibility 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)
+ {
+ DEBUG(D_route) debug_printf("checking \"condition\" \"%.80s\"...\n", r->condition);
+ if (!expand_check_condition(r->condition, r->name, US"router"))
+ {
+ if (f.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)
+ {
+ 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 && 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
+ && (bmi_deliver == 0 || !bmi_alt_location)
+ )
+ {
+ 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
+ && (bmi_deliver == 0 || bmi_alt_location)
+ )
+ {
+ 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(const 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 && (isdigit(*s) || *s == '-') &&
+ s[Ustrspn(s+1, "0123456789")+1] == 0)
+ {
+ *return_uid = (uid_t)Uatoi(s);
+ if (pw) *pw = NULL;
+ return TRUE;
+ }
+
+ string_format_nt(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))) break;
+ if (++i > finduser_retries) break;
+ sleep(1);
+ }
+
+ if (lastpw)
+ {
+ 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)
+ {
+ DEBUG(D_uid) debug_printf("getpwnam() returned NULL (user not found)\n");
+ return FALSE;
+ }
+
+DEBUG(D_uid) debug_printf("getpwnam() succeeded uid=%d gid=%d\n",
+ lastpw->pw_uid, lastpw->pw_gid);
+
+if (return_uid) *return_uid = lastpw->pw_uid;
+if (pw) *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 name 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)))
+ {
+ *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)
+ {
+ *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)
+ {
+ *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;
+}
+
+
+
+/*************************************************
+* 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->prop.errors_address =
+ addr->parent ? addr->parent->prop.errors_address : NULL;
+
+/* 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->prop.errors_address = parent->prop.errors_address;
+
+/* Copy the propagated flags and address_data from the original. */
+
+new->prop.ignore_error = addr->prop.ignore_error;
+new->prop.address_data = addr->prop.address_data;
+new->prop.variables = NULL;
+tree_dup((tree_node **)&new->prop.variables, addr->prop.variables);
+new->dsn_flags = addr->dsn_flags;
+new->dsn_orcpt = addr->dsn_orcpt;
+
+
+/* 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 && tree_search(tree_nonrecipients, addr->unique))
+ {
+ 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;
+ }
+}
+
+
+
+/************************************************/
+/* Add router-assigned variables
+Return OK/DEFER/FAIL/PASS */
+
+static int
+set_router_vars(address_item * addr, const router_instance * r)
+{
+const uschar * varlist = r->set;
+tree_node ** root = (tree_node **) &addr->prop.variables;
+int sep = ';';
+
+if (!varlist) return OK;
+
+/* Walk the varlist, creating variables */
+
+for (uschar * ele; (ele = string_nextinlist(&varlist, &sep, NULL, 0)); )
+ {
+ const uschar * assignment = ele;
+ int esep = '=';
+ uschar * name = string_nextinlist(&assignment, &esep, NULL, 0);
+ uschar * val;
+ tree_node * node;
+
+ /* Variable name must exist and start "r_". */
+
+ if (!name || name[0] != 'r' || name[1] != '_' || !name[2])
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "bad router variable name '%s' in router '%s'\n", name, r->name);
+ return FAIL;
+ }
+ name += 2;
+
+ while (isspace(*assignment)) assignment++;
+
+ if (!(val = expand_string(US assignment)))
+ if (f.expand_string_forcedfail)
+ {
+ int yield;
+ BOOL more;
+ DEBUG(D_route) debug_printf("forced failure in expansion of \"%s\" "
+ "(router variable): decline action taken\n", ele);
+
+ /* Expand "more" if necessary; DEFER => an expansion failed */
+
+ yield = exp_bool(addr, US"router", r->name, D_route,
+ US"more", r->more, r->expand_more, &more);
+ if (yield != OK) return yield;
+
+ if (!more)
+ {
+ DEBUG(D_route)
+ debug_printf("\"more\"=false: skipping remaining routers\n");
+ router_name = NULL;
+ r = NULL;
+ return FAIL;
+ }
+ return PASS;
+ }
+ else
+ {
+ addr->message = string_sprintf("expansion of \"%s\" failed "
+ "in %s router: %s", ele, r->name, expand_string_message);
+ return DEFER;
+ }
+
+ if (!(node = tree_search(*root, name)))
+ { /* name should never be tainted */
+ node = store_get(sizeof(tree_node) + Ustrlen(name), GET_UNTAINTED);
+ Ustrcpy(node->name, name);
+ (void)tree_insertnode(root, node);
+ }
+ node->data.ptr = US val;
+ DEBUG(D_route) debug_printf("set r_%s%s = '%s'%s\n",
+ name, is_tainted(name)?" (tainted)":"",
+ val, is_tainted(val)?" (tainted)":"");
+
+ /* All expansions after this point need visibility of that variable */
+ router_var = *root;
+ }
+return OK;
+}
+
+
+/*************************************************
+* 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;
+const 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 ? addr->start_router : routers; r; r = nextr)
+ {
+ uschar *error;
+ struct passwd *pw = NULL;
+ struct passwd pwcopy;
+ 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 */
+
+ f.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
+ continually adding to an address, for example), put a long stop counter on
+ the number of parents. */
+
+ for (address_item * parent = addr->parent; parent; 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->prefix_v = addr->suffix = addr->suffix_v = 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)
+ {
+ unsigned vlen;
+ int plen = route_check_prefix(addr->local_part, r->prefix, &vlen);
+ if (plen > 0)
+ {
+ /* If the variable-part is zero-length then the prefix was not
+ wildcarded and we can detaint-copy it since it matches the
+ (non-expandable) router option. Otherwise copy the (likely) tainted match
+ and the variable-part of the match from the local_part. */
+
+ if (vlen)
+ {
+ addr->prefix = string_copyn(addr->local_part, plen);
+ addr->prefix_v = string_copyn(addr->local_part, vlen);
+ }
+ else
+ addr->prefix = string_copyn_taint(addr->local_part, plen, GET_UNTAINTED);
+ 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)
+ {
+ unsigned vlen;
+ int slen = route_check_suffix(addr->local_part, r->suffix, &vlen);
+ if (slen > 0)
+ {
+ int lplen = Ustrlen(addr->local_part) - slen;
+ addr->suffix = vlen
+ ? addr->local_part + lplen
+ : string_copy_taint(addr->local_part + lplen, GET_UNTAINTED);
+ addr->suffix_v = addr->suffix + Ustrlen(addr->suffix) - vlen;
+ 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;
+ driver_srcfile = r->srcfile;
+ driver_srcline = r->srcline;
+ 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)
+ {
+ driver_srcfile = router_name = NULL; driver_srcline = 0;
+ 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;
+
+ /* Add any variable-settings that are on the router, to the set on the
+ addr. Expansion is done here and not later when the addr is used. There may
+ be multiple settings, gathered during readconf; this code gathers them during
+ router traversal. On the addr string they are held as a variable tree, so
+ as to maintain the post-expansion taints separate. */
+
+ switch (set_router_vars(addr, r))
+ {
+ case OK: break;
+ case PASS: continue; /* with next router */
+ default: goto ROUTE_EXIT;
+ }
+
+ /* 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)
+ {
+ DEBUG(D_route) debug_printf("processing address_data\n");
+ if (!(deliver_address_data = expand_string(r->address_data)))
+ {
+ if (f.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, US"router", r->name, D_route,
+ 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");
+ driver_srcfile = router_name = NULL; driver_srcline = 0;
+ 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->prop.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)
+ {
+ 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;
+ }
+
+ /* If this should be the last hop for DSN flag the addr. */
+
+ if (r->dsn_lasthop && !(addr->dsn_flags & rf_dsnlasthop))
+ {
+ addr->dsn_flags |= rf_dsnlasthop;
+ HDEBUG(D_route) debug_printf("DSN: last hop for %s\n", addr->address);
+ }
+
+ /* Run the router, and handle the consequences. */
+
+ 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);
+
+ driver_srcfile = router_name = NULL; driver_srcline = 0;
+
+ 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, US"router", r->name, D_route,
+ 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)
+ {
+ HDEBUG(D_route) debug_printf("no more routers\n");
+ if (!addr->message)
+ {
+ uschar *message = US"Unrouteable address";
+ if (addr->router && addr->router->cannot_route_message)
+ {
+ uschar *expmessage = expand_string(addr->router->cannot_route_message);
+ if (!expmessage)
+ {
+ if (!f.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 message: %s\n",
+ r->name, addr->address, addr->message ? addr->message : US"<none>");
+ 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)
+ {
+ int rc;
+ int old_pool = store_pool;
+ for (host_item * h = addr->host_list; h; h = h->next)
+ {
+ uschar *newaddress;
+ uschar *oldaddress, *oldname;
+
+ if (!h->address) continue;
+
+ deliver_host_address = h->address;
+ newaddress = expand_string(r->translate_ip_address);
+ deliver_host_address = NULL;
+
+ if (!newaddress)
+ {
+ if (f.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, US"router", r->name, D_route,
+ 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)
+ {
+ debug_printf(" envelope to: %s\n", addr->address);
+ debug_printf(" transport: %s\n", addr->transport
+ ? addr->transport->name : US"<none>");
+
+ if (addr->prop.errors_address)
+ debug_printf(" errors to %s\n", addr->prop.errors_address);
+
+ for (host_item * h = addr->host_list; h; h = h->next)
+ {
+ debug_printf(" host %s", h->name);
+ if (h->address) 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)
+ 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 && addr->message)
+ addr->message = expand_hide_passwords(addr->message);
+
+deliver_set_expansions(NULL);
+driver_srcfile = router_name = NULL; driver_srcline = 0;
+f.disable_logging = FALSE;
+return yield;
+}
+
+
+
+/* For error messages, a string describing the config location associated
+with current processing. NULL if we are not in a router. */
+/* Name only, for now */
+
+uschar *
+router_current_name(void)
+{
+if (!router_name) return NULL;
+return string_sprintf(" (router %s, %s %d)", router_name, driver_srcfile, driver_srcline);
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of route.c */
diff --git a/src/routers/Makefile b/src/routers/Makefile
new file mode 100644
index 0000000..8f2432d
--- /dev/null
+++ b/src/routers/Makefile
@@ -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
index 0000000..a674e8b
--- /dev/null
+++ b/src/routers/README
@@ -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 subsequent
+ 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
index 0000000..110a1ef
--- /dev/null
+++ b/src/routers/accept.c
@@ -0,0 +1,139 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 - 2021 */
+/* 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 */
+};
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy entries */
+void accept_router_init(router_instance *rblock) {}
+int accept_router_entry(router_instance *rblock, address_item *addr,
+ struct passwd *pw, int verify, address_item **addr_local,
+ address_item **addr_remote, address_item **addr_new,
+ address_item **addr_succeed) {return 0;}
+
+#else /*!MACRO_PREDEF*/
+
+
+
+/*************************************************
+* 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;
+
+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 removable 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->prop.errors_address = errors_to;
+addr->prop.extra_headers = extra_headers;
+addr->prop.remove_headers = remove_headers;
+
+return rf_queue_add(addr, addr_local, addr_remote, rblock, pw)? OK : DEFER;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of routers/accept.c */
diff --git a/src/routers/accept.h b/src/routers/accept.h
new file mode 100644
index 0000000..43494fb
--- /dev/null
+++ b/src/routers/accept.h
@@ -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
index 0000000..a845b4e
--- /dev/null
+++ b/src/routers/dnslookup.c
@@ -0,0 +1,477 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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. */
+#define LOFF(field) OPT_OFF(dnslookup_router_options_block, field)
+
+optionlist dnslookup_router_options[] = {
+ { "check_secondary_mx", opt_bool, LOFF(check_secondary_mx) },
+ { "check_srv", opt_stringptr, LOFF(check_srv) },
+ { "fail_defer_domains", opt_stringptr, LOFF(fail_defer_domains) },
+ { "ipv4_only", opt_stringptr, LOFF(ipv4_only) },
+ { "ipv4_prefer", opt_stringptr, LOFF(ipv4_prefer) },
+ { "mx_domains", opt_stringptr, LOFF(mx_domains) },
+ { "mx_fail_domains", opt_stringptr, LOFF(mx_fail_domains) },
+ { "qualify_single", opt_bool, LOFF(qualify_single) },
+ { "rewrite_headers", opt_bool, LOFF(rewrite_headers) },
+ { "same_domain_copy_routing", opt_bool|opt_public, OPT_OFF(router_instance, same_domain_copy_routing) },
+ { "search_parents", opt_bool, LOFF(search_parents) },
+ { "srv_fail_domains", opt_stringptr, LOFF(srv_fail_domains) },
+ { "widen_domains", opt_stringptr, LOFF(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);
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy entries */
+dnslookup_router_options_block dnslookup_router_option_defaults = {0};
+void dnslookup_router_init(router_instance *rblock) {}
+int dnslookup_router_entry(router_instance *rblock, address_item *addr,
+ struct passwd *pw, int verify, address_item **addr_local,
+ address_item **addr_remote, address_item **addr_new,
+ address_item **addr_succeed) {return 0;}
+
+#else /*!MACRO_PREDEF*/
+
+
+
+
+/* Default private options block for the dnslookup router. */
+
+dnslookup_router_options_block dnslookup_router_option_defaults = {
+ .check_secondary_mx = FALSE,
+ .qualify_single = TRUE,
+ .search_parents = FALSE,
+ .rewrite_headers = TRUE,
+ .widen_domains = NULL,
+ .mx_domains = NULL,
+ .mx_fail_domains = NULL,
+ .srv_fail_domains = NULL,
+ .check_srv = NULL,
+ .fail_defer_domains = NULL,
+ .ipv4_only = NULL,
+ .ipv4_prefer = NULL,
+};
+
+
+
+/*************************************************
+* 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 | HOST_FIND_BY_AAAA;
+dnslookup_router_options_block *ob =
+ (dnslookup_router_options_block *)(rblock->options_block);
+uschar *srv_service = NULL;
+uschar *widen = NULL;
+const uschar *pre_widen = addr->domain;
+const uschar *post_widen = NULL;
+const uschar *fully_qualified_name;
+const uschar *listptr;
+uschar widen_buffer[256];
+
+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)
+ {
+ if ( !(srv_service = expand_string(ob->check_srv))
+ && !f.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
+ && (verify != v_sender || !ob->rewrite_headers || addr->parent))
+ {
+ listptr = ob->widen_domains;
+ /* not expanded so should never be tainted */
+ 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)
+ {
+ h.name = pre_widen;
+ pre_widen = NULL;
+ }
+ else if (widen)
+ {
+ h.name = string_sprintf("%s.%s", addr->domain, widen);
+ /* not expanded so should never be tainted */
+ 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)
+ {
+ 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;
+
+ /* Check if we must request only. or prefer, ipv4 */
+
+ if ( ob->ipv4_only
+ && expand_check_condition(ob->ipv4_only, rblock->name, US"router"))
+ flags = flags & ~HOST_FIND_BY_AAAA | HOST_FIND_IPV4_ONLY;
+ else if (f.search_find_defer)
+ return DEFER;
+ if ( ob->ipv4_prefer
+ && expand_check_condition(ob->ipv4_prefer, rblock->name, US"router"))
+ flags |= HOST_FIND_IPV4_FIRST;
+ else if (f.search_find_defer)
+ return DEFER;
+
+ /* 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)
+ {
+ if (ob->qualify_single) flags |= HOST_FIND_QUALIFY_SINGLE;
+ if (ob->search_parents) flags |= HOST_FIND_SEARCH_PARENTS;
+ }
+
+ rc = host_find_bydns(&h, CUS rblock->ignore_target_hosts, flags,
+ srv_service, ob->srv_fail_domains, ob->mx_fail_domains,
+ &rblock->dnssec,
+ &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)
+ switch(match_isinlist(fully_qualified_name,
+ CUSS &(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_SECURITY)
+ {
+ addr->message = US"host lookup done insecurely";
+ return DEFER;
+ }
+ 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;
+
+ if (ob->fail_defer_domains)
+ switch(match_isinlist(fully_qualified_name,
+ CUSS &ob->fail_defer_domains, 0,
+ &domainlist_anchor, addr->domain_cache, MCL_DOMAIN, TRUE, NULL))
+ {
+ case DEFER:
+ addr->message = US"lookup defer for fail_defer_domains option";
+ return DEFER;
+
+ case OK:
+ DEBUG(D_route) debug_printf("%s router: matched fail_defer_domains\n",
+ rblock->name);
+ addr->message = US"missing MX, or all MXs point to missing A records,"
+ " and defer requested";
+ return DEFER;
+ }
+ /* 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->basic_errno = ERRNO_UNKNOWNHOST;
+ 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 (f.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->prop.errors_address);
+if (rc != OK) return rc;
+
+/* Set up the additional and removable headers for this address. */
+
+rc = rf_get_munge_headers(addr, rblock, &addr->prop.extra_headers,
+ &addr->prop.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), GET_UNTAINTED);
+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;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of routers/dnslookup.c */
+/* vi: aw ai sw=2
+*/
diff --git a/src/routers/dnslookup.h b/src/routers/dnslookup.h
new file mode 100644
index 0000000..b7e0915
--- /dev/null
+++ b/src/routers/dnslookup.h
@@ -0,0 +1,42 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 *fail_defer_domains;
+ uschar *ipv4_only;
+ uschar *ipv4_prefer;
+} 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
index 0000000..3d68642
--- /dev/null
+++ b/src/routers/ipliteral.c
@@ -0,0 +1,202 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 };
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy entries */
+void ipliteral_router_init(router_instance *rblock) {}
+int ipliteral_router_entry(router_instance *rblock, address_item *addr,
+ struct passwd *pw, int verify, address_item **addr_local,
+ address_item **addr_remote, address_item **addr_new,
+ address_item **addr_succeed) {return 0;}
+
+#else /*!MACRO_PREDEF*/
+
+
+/*************************************************
+* 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);
+*/
+}
+
+
+
+/*************************************************
+* 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;
+const uschar *domain = addr->domain;
+const uschar *ip;
+int len = Ustrlen(domain);
+int rc, ipv;
+
+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;
+ip = string_copyn(domain+1, len-2);
+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))
+ 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(CUSS&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";
+ return DECLINE;
+ }
+
+/* Set up a host item */
+
+h = store_get(sizeof(host_item), GET_UNTAINTED);
+
+h->next = NULL;
+h->address = string_copy(ip);
+h->port = PORT_NONE;
+h->name = 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->prop.errors_address);
+if (rc != OK) return rc;
+
+/* Set up the additional and removable headers for this address. */
+
+rc = rf_get_munge_headers(addr, rblock, &addr->prop.extra_headers,
+ &addr->prop.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;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of routers/ipliteral.c */
diff --git a/src/routers/ipliteral.h b/src/routers/ipliteral.h
new file mode 100644
index 0000000..1ddb38b
--- /dev/null
+++ b/src/routers/ipliteral.h
@@ -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
index 0000000..94cde4e
--- /dev/null
+++ b/src/routers/iplookup.c
@@ -0,0 +1,418 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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,
+ OPT_OFF(iplookup_router_options_block, hosts) },
+ { "optional", opt_bool,
+ OPT_OFF(iplookup_router_options_block, optional) },
+ { "port", opt_int,
+ OPT_OFF(iplookup_router_options_block, port) },
+ { "protocol", opt_stringptr,
+ OPT_OFF(iplookup_router_options_block, protocol_name) },
+ { "query", opt_stringptr,
+ OPT_OFF(iplookup_router_options_block, query) },
+ { "reroute", opt_stringptr,
+ OPT_OFF(iplookup_router_options_block, reroute) },
+ { "response_pattern", opt_stringptr,
+ OPT_OFF(iplookup_router_options_block, response_pattern) },
+ { "timeout", opt_time,
+ OPT_OFF(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);
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy entries */
+iplookup_router_options_block iplookup_router_option_defaults = {0};
+void iplookup_router_init(router_instance *rblock) {}
+int iplookup_router_entry(router_instance *rblock, address_item *addr,
+ struct passwd *pw, int verify, address_item **addr_local,
+ address_item **addr_remote, address_item **addr_new,
+ address_item **addr_succeed) {return 0;}
+
+#else /*!MACRO_PREDEF*/
+
+
+/* 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;
+const uschar *listptr;
+uschar host_buffer[256];
+host_item *host = store_get(sizeof(host_item), GET_UNTAINTED);
+address_item *new_addr;
+iplookup_router_options_block *ob =
+ (iplookup_router_options_block *)(rblock->options_block);
+const pcre2_code *re = ob->re_response_pattern;
+int count, query_len, rc;
+int sep = 0;
+
+DEBUG(D_route) debug_printf("%s router called for %s: domain = %s\n",
+ rblock->name, addr->address, addr->domain);
+
+reply = store_get(256, GET_TAINTED);
+
+/* 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;
+/* not expanded so should never be tainted */
+while ((hostname = string_nextinlist(&listptr, &sep, host_buffer,
+ sizeof(host_buffer))))
+ {
+ 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
+ {
+/*XXX might want dnssec request/require on an iplookup router? */
+ 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; h = h->next)
+ {
+ int host_af;
+ client_conn_ctx query_cctx = {0};
+
+ /* Skip any hosts for which we have no address */
+
+ if (!h->address) 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_cctx.sock = ip_socket(ob->protocol == ip_udp ? SOCK_DGRAM:SOCK_STREAM,
+ host_af);
+ if (query_cctx.sock < 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). */
+/*XXX could take advantage of TFO */
+
+ if (ip_connect(query_cctx.sock, host_af, h->address,ob->port, ob->timeout,
+ ob->protocol == ip_udp ? NULL : &tcp_fastopen_nodata) < 0)
+ {
+ close(query_cctx.sock);
+ 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_cctx.sock, query, query_len, 0) < 0)
+ {
+ DEBUG(D_route) debug_printf("send to %s failed\n", h->address);
+ (void)close(query_cctx.sock);
+ continue;
+ }
+
+ /* Read the response and close the socket. If the read fails, try the
+ next IP address. */
+
+ count = ip_recv(&query_cctx, reply, sizeof(reply) - 1, time(NULL) + ob->timeout);
+ (void)close(query_cctx.sock);
+ 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) 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;
+
+new_addr->prop = addr->prop;
+
+if (addr->child_count == USHRT_MAX)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s router generated more than %d "
+ "child addresses for <%s>", rblock->name, USHRT_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 removable headers
+for this new address. */
+
+rc = rf_get_errors_address(addr, rblock, verify, &new_addr->prop.errors_address);
+if (rc != OK) return rc;
+
+rc = rf_get_munge_headers(addr, rblock, &new_addr->prop.extra_headers,
+ &new_addr->prop.remove_headers);
+if (rc != OK) return rc;
+
+return OK;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of routers/iplookup.c */
diff --git a/src/routers/iplookup.h b/src/routers/iplookup.h
new file mode 100644
index 0000000..dbcb03c
--- /dev/null
+++ b/src/routers/iplookup.h
@@ -0,0 +1,42 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* Copyright (c) The Exim Maintainers 2021 */
+/* 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 pcre2_code *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
index 0000000..974ad0c
--- /dev/null
+++ b/src/routers/manualroute.c
@@ -0,0 +1,490 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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,
+ OPT_OFF(manualroute_router_options_block, host_all_ignored) },
+ { "host_find_failed", opt_stringptr,
+ OPT_OFF(manualroute_router_options_block, host_find_failed) },
+ { "hosts_randomize", opt_bool,
+ OPT_OFF(manualroute_router_options_block, hosts_randomize) },
+ { "route_data", opt_stringptr,
+ OPT_OFF(manualroute_router_options_block, route_data) },
+ { "route_list", opt_stringptr,
+ OPT_OFF(manualroute_router_options_block, route_list) },
+ { "same_domain_copy_routing", opt_bool|opt_public,
+ OPT_OFF(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);
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy entries */
+manualroute_router_options_block manualroute_router_option_defaults = {0};
+void manualroute_router_init(router_instance *rblock) {}
+int manualroute_router_entry(router_instance *rblock, address_item *addr,
+ struct passwd *pw, int verify, address_item **addr_local,
+ address_item **addr_remote, address_item **addr_new,
+ address_item **addr_succeed) {return 0;}
+
+#else /*!MACRO_PREDEF*/
+
+
+
+/* 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);
+
+/* Host_find_failed must be a recognized word */
+
+for (int 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 (int 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(const uschar *s, const uschar **domain, const uschar **hostlist,
+ const uschar **options)
+{
+while (*s != 0 && isspace(*s)) s++;
+
+if (domain)
+ {
+ if (!*s) return FALSE; /* missing data */
+ *domain = string_dequote(&s);
+ while (*s && isspace(*s)) s++;
+ }
+
+*hostlist = string_dequote(&s);
+while (*s && 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;
+const uschar *options = NULL;
+const uschar *hostlist = NULL;
+const uschar *domain;
+uschar *newhostlist;
+const uschar *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;
+
+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)
+ {
+ int sep = -(';'); /* Default is semicolon */
+ listptr = ob->route_list;
+
+ while ((route_item = string_nextinlist(&listptr, &sep, NULL, 0)))
+ {
+ 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, CUSS &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) 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
+ {
+ if (!(route_item = rf_expand_data(addr, ob->route_data, &rc)))
+ return rc;
+ (void) parse_route_item(route_item, NULL, &hostlist, &options);
+ if (!hostlist[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)
+ {
+ if (f.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)
+ {
+ unsigned n;
+ const uschar *s = options;
+ while (*options != 0 && !isspace(*options)) options++;
+ n = options-s;
+
+ if (Ustrncmp(s, "randomize", n) == 0) randomize = TRUE;
+ else if (Ustrncmp(s, "no_randomize", n) == 0) randomize = FALSE;
+ else if (Ustrncmp(s, "byname", n) == 0)
+ lookup_type = lookup_type & ~(LK_DEFAULT | LK_BYDNS) | LK_BYNAME;
+ else if (Ustrncmp(s, "bydns", n) == 0)
+ lookup_type = lookup_type & ~(LK_DEFAULT | LK_BYNAME) & LK_BYDNS;
+ else if (Ustrncmp(s, "ipv4_prefer", n) == 0) lookup_type |= LK_IPV4_PREFER;
+ else if (Ustrncmp(s, "ipv4_only", n) == 0) lookup_type |= LK_IPV4_ONLY;
+ else
+ {
+ transport_instance *t;
+ for (t = transports; t; t = t->next)
+ if (Ustrncmp(t->name, s, n) == 0)
+ {
+ transport = t;
+ individual_transport_set = TRUE;
+ break;
+ }
+
+ if (!t)
+ {
+ 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 (*options)
+ {
+ options++;
+ while (*options != 0 && isspace(*options)) options++;
+ }
+ }
+
+/* Set up the errors address, if any. */
+
+rc = rf_get_errors_address(addr, rblock, verify, &addr->prop.errors_address);
+if (rc != OK) return rc;
+
+/* Set up the additional and removable headers for this address. */
+
+rc = rf_get_munge_headers(addr, rblock, &addr->prop.extra_headers,
+ &addr->prop.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 && transport->info->local)
+ {
+ if (hostlist[0])
+ {
+ host_item *h;
+ addr->host_list = h = store_get(sizeof(host_item), GET_UNTAINTED);
+ 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])
+ {
+ if (verify != v_none) goto ROUTED;
+ addr->message = string_sprintf("error in %s router: no host(s) specified "
+ "for domain %s", rblock->name, addr->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)
+ {
+ 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 && 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;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of routers/manualroute.c */
diff --git a/src/routers/manualroute.h b/src/routers/manualroute.h
new file mode 100644
index 0000000..9c20b6f
--- /dev/null
+++ b/src/routers/manualroute.h
@@ -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
index 0000000..55f03a4
--- /dev/null
+++ b/src/routers/queryprogram.c
@@ -0,0 +1,538 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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,
+ OPT_OFF(queryprogram_router_options_block, expand_cmd_gid) },
+ { "*expand_command_user", opt_bool | opt_hidden,
+ OPT_OFF(queryprogram_router_options_block, expand_cmd_uid) },
+ { "*set_command_group", opt_bool | opt_hidden,
+ OPT_OFF(queryprogram_router_options_block, cmd_gid_set) },
+ { "*set_command_user", opt_bool | opt_hidden,
+ OPT_OFF(queryprogram_router_options_block, cmd_uid_set) },
+ { "command", opt_stringptr,
+ OPT_OFF(queryprogram_router_options_block, command) },
+ { "command_group",opt_expand_gid,
+ OPT_OFF(queryprogram_router_options_block, cmd_gid) },
+ { "command_user", opt_expand_uid,
+ OPT_OFF(queryprogram_router_options_block, cmd_uid) },
+ { "current_directory", opt_stringptr,
+ OPT_OFF(queryprogram_router_options_block, current_directory) },
+ { "timeout", opt_time,
+ OPT_OFF(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);
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy entries */
+queryprogram_router_options_block queryprogram_router_option_defaults = {0};
+void queryprogram_router_init(router_instance *rblock) {}
+int queryprogram_router_entry(router_instance *rblock, address_item *addr,
+ struct passwd *pw, int verify, address_item **addr_local,
+ address_item **addr_remote, address_item **addr_new,
+ address_item **addr_succeed) {return 0;}
+
+#else /*!MACRO_PREDEF*/
+
+
+/* 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)
+ 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)
+ 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)
+ {
+ BOOL ignore_error = addr->prop.ignore_error;
+ address_item *next = generated;
+
+ generated = next->next;
+
+ next->parent = addr;
+ next->prop = *addr_prop;
+ next->prop.ignore_error = next->prop.ignore_error || ignore_error;
+ next->start_router = rblock->redirect_router;
+
+ next->next = *addr_new;
+ *addr_new = next;
+
+ if (addr->child_count == USHRT_MAX)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s router generated more than %d "
+ "child addresses for <%s>", rblock->name, USHRT_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];
+const 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. */
+
+bzero(&addr_prop, sizeof(addr_prop));
+addr_prop.address_data = deliver_address_data;
+tree_dup((tree_node **)&addr_prop.variables, addr->prop.variables);
+
+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
+ && !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)
+ {
+ if (route_find_expanded_group(ob->expand_cmd_gid, rblock->name,
+ US"router", &gid, &(addr->message)))
+ return DEFER;
+ }
+ else if (upw)
+ 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 */
+ FALSE, /* args must be untainted */
+ US"queryprogram router", /* for error messages */
+ &addr->message)) /* where to put error message */
+ return DEFER;
+
+/* Create the child process, making it a group leader. */
+
+if ((pid = child_open_uid(argvptr, NULL, 0077, puid, pgid, &fd_in, &fd_out,
+ current_directory, TRUE, US"queryprogram-cmd")) < 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 && !isspace(*rdata)) rdata++;
+if (*rdata) *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) 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) 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 equivalent of the "extract"
+expansion function. */
+
+if ((s = expand_getkeyed(US"data", rdata)) && *s)
+ addr_prop.address_data = string_copy(s);
+
+/* If we found a transport name, find the actual transport */
+
+if ((s = expand_getkeyed(US"transport", rdata)) && *s)
+ {
+ transport_instance *transport;
+ for (transport = transports; transport; transport = transport->next)
+ if (Ustrcmp(transport->name, s) == 0) break;
+ if (!transport)
+ {
+ 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. */
+
+if ((s = expand_getkeyed(US"hosts", rdata)) && *s)
+ {
+ int lookup_type = LK_DEFAULT;
+ uschar * ss = expand_getkeyed(US"lookup", rdata);
+
+ if (ss && *ss)
+ {
+ 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->prop = addr_prop;
+
+/* Queue the address for local or remote delivery. */
+
+return rf_queue_add(addr, addr_local, addr_remote, rblock, pw) ? OK : DEFER;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of routers/queryprogram.c */
diff --git a/src/routers/queryprogram.h b/src/routers/queryprogram.h
new file mode 100644
index 0000000..93046bd
--- /dev/null
+++ b/src/routers/queryprogram.h
@@ -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
index 0000000..31c07f5
--- /dev/null
+++ b/src/routers/redirect.c
@@ -0,0 +1,801 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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. */
+#define LOFF(field) OPT_OFF(redirect_router_options_block, field)
+
+optionlist redirect_router_options[] = {
+ { "allow_defer", opt_bit | (RDON_DEFER << 16),
+ LOFF(bit_options) },
+ { "allow_fail", opt_bit | (RDON_FAIL << 16),
+ LOFF(bit_options) },
+ { "allow_filter", opt_bit | (RDON_FILTER << 16),
+ LOFF(bit_options) },
+ { "allow_freeze", opt_bit | (RDON_FREEZE << 16),
+ LOFF(bit_options) },
+ { "check_ancestor", opt_bool, LOFF(check_ancestor) },
+ { "check_group", opt_bool, LOFF(check_group) },
+ { "check_owner", opt_bool, LOFF(check_owner) },
+ { "data", opt_stringptr, LOFF(data) },
+ { "directory_transport",opt_stringptr, LOFF(directory_transport_name) },
+ { "file", opt_stringptr, LOFF(file) },
+ { "file_transport", opt_stringptr, LOFF(file_transport_name) },
+
+ { "filter_prepend_home",opt_bit | (RDON_PREPEND_HOME << 16),
+ LOFF(bit_options) },
+ { "forbid_blackhole", opt_bit | (RDON_BLACKHOLE << 16),
+ LOFF(bit_options) },
+ { "forbid_exim_filter", opt_bit | (RDON_EXIM_FILTER << 16),
+ LOFF(bit_options) },
+ { "forbid_file", opt_bool,
+ LOFF(forbid_file) },
+ { "forbid_filter_dlfunc", opt_bit | (RDON_DLFUNC << 16),
+ LOFF(bit_options) },
+ { "forbid_filter_existstest", opt_bit | (RDON_EXISTS << 16),
+ LOFF(bit_options) },
+ { "forbid_filter_logwrite",opt_bit | (RDON_LOG << 16),
+ LOFF(bit_options) },
+ { "forbid_filter_lookup", opt_bit | (RDON_LOOKUP << 16),
+ LOFF(bit_options) },
+ { "forbid_filter_perl", opt_bit | (RDON_PERL << 16),
+ LOFF(bit_options) },
+ { "forbid_filter_readfile", opt_bit | (RDON_READFILE << 16),
+ LOFF(bit_options) },
+ { "forbid_filter_readsocket", opt_bit | (RDON_READSOCK << 16),
+ LOFF(bit_options) },
+ { "forbid_filter_reply",opt_bool,
+ LOFF(forbid_filter_reply) },
+ { "forbid_filter_run", opt_bit | (RDON_RUN << 16),
+ LOFF(bit_options) },
+ { "forbid_include", opt_bit | (RDON_INCLUDE << 16),
+ LOFF(bit_options) },
+ { "forbid_pipe", opt_bool,
+ LOFF(forbid_pipe) },
+ { "forbid_sieve_filter",opt_bit | (RDON_SIEVE_FILTER << 16),
+ LOFF(bit_options) },
+ { "forbid_smtp_code", opt_bool,
+ LOFF(forbid_smtp_code) },
+ { "hide_child_in_errmsg", opt_bool,
+ LOFF( hide_child_in_errmsg) },
+ { "ignore_eacces", opt_bit | (RDON_EACCES << 16),
+ LOFF(bit_options) },
+ { "ignore_enotdir", opt_bit | (RDON_ENOTDIR << 16),
+ LOFF(bit_options) },
+
+ { "include_directory", opt_stringptr, LOFF( include_directory) },
+ { "modemask", opt_octint, LOFF(modemask) },
+ { "one_time", opt_bool, LOFF(one_time) },
+ { "owners", opt_uidlist, LOFF(owners) },
+ { "owngroups", opt_gidlist, LOFF(owngroups) },
+ { "pipe_transport", opt_stringptr, LOFF(pipe_transport_name) },
+ { "qualify_domain", opt_stringptr, LOFF(qualify_domain) },
+ { "qualify_preserve_domain", opt_bool, LOFF(qualify_preserve_domain) },
+ { "repeat_use", opt_bool | opt_public, OPT_OFF(router_instance, repeat_use) },
+ { "reply_transport", opt_stringptr, LOFF(reply_transport_name) },
+
+ { "rewrite", opt_bit | (RDON_REWRITE << 16),
+ LOFF(bit_options) },
+
+ { "sieve_enotify_mailto_owner", opt_stringptr, LOFF(sieve_enotify_mailto_owner) },
+ { "sieve_subaddress", opt_stringptr, LOFF(sieve_subaddress) },
+ { "sieve_useraddress", opt_stringptr, LOFF(sieve_useraddress) },
+ { "sieve_vacation_directory", opt_stringptr, LOFF(sieve_vacation_directory) },
+ { "skip_syntax_errors", opt_bool, LOFF(skip_syntax_errors) },
+ { "syntax_errors_text", opt_stringptr, LOFF(syntax_errors_text) },
+ { "syntax_errors_to", opt_stringptr, LOFF(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);
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy entries */
+redirect_router_options_block redirect_router_option_defaults = {0};
+void redirect_router_init(router_instance *rblock) {}
+int redirect_router_entry(router_instance *rblock, address_item *addr,
+ struct passwd *pw, int verify, address_item **addr_local,
+ address_item **addr_remote, address_item **addr_new,
+ address_item **addr_succeed) {return 0;}
+
+#else /*!MACRO_PREDEF*/
+
+
+
+/* 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 */
+ 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 || rblock->remove_headers)
+ 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)
+ 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 && 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 && 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->prop.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)
+ {
+ address_item *parent;
+ address_item *next = generated;
+ uschar *errors_address = next->prop.errors_address;
+
+ generated = next->next;
+ next->parent = addr;
+ next->start_router = rblock->redirect_router;
+ if (addr->child_count == USHRT_MAX)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s router generated more than %d "
+ "child addresses for <%s>", rblock->name, USHRT_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 && !f.queue_2stage)
+ {
+ for (parent = addr; parent->parent; 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; parent = parent->parent)
+ if ((parent->router && 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. */
+
+ {
+ BOOL ignore_error = next->prop.ignore_error;
+ next->prop = *addr_prop;
+ next->prop.ignore_error = ignore_error || addr->prop.ignore_error;
+ }
+ if (errors_address) next->prop.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;
+ }
+ }
+
+#ifdef SUPPORT_I18N
+ if (!next->prop.utf8_msg)
+ next->prop.utf8_msg = string_is_utf8(next->address)
+ || (sender_address && string_is_utf8(sender_address));
+#endif
+
+ 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->prop.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 ");
+
+#ifdef SUPPORT_I18N
+ if (next->prop.utf8_msg) debug_printf("utf8 ");
+#endif
+
+ 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;
+const 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;
+
+/* 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;
+addr_prop.variables = NULL;
+tree_dup((tree_node **)&addr_prop.variables, addr->prop.variables);
+
+#ifdef SUPPORT_I18N
+addr_prop.utf8_msg = addr->prop.utf8_msg;
+addr_prop.utf8_downcvt = addr->prop.utf8_downcvt;
+addr_prop.utf8_downcvt_maybe = addr->prop.utf8_downcvt_maybe;
+#endif
+
+
+/* When verifying and testing addresses, the "logwrite" command in filters
+must be bypassed. */
+
+if (verify == v_none && !f.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;
+ }
+
+/* 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)
+ {
+ uschar *new_qdr = rf_expand_data(addr, ob->qualify_domain, &xrc);
+ if (!new_qdr) 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;
+
+redirect.string = (redirect.isfile = (ob->file != NULL))
+ ? ob->file : ob->data;
+
+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)
+ addr->message = yield == FAIL ? US"forced rejection" : US"forced defer";
+ else
+ {
+ uschar * matched;
+ if ( ob->forbid_smtp_code
+ && regex_match(regex_smtp_code, addr->message, -1, &matched))
+ {
+ DEBUG(D_route) debug_printf("SMTP code at start of error message "
+ "is ignored because forbid_smtp_code is set\n");
+ addr->message += Ustrlen(matched);
+ }
+ 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 (!f.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), GET_UNTAINTED);
+ 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 ? "redirect" : "filter",
+ ob->data ? "data" : "file",
+ 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 || f.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 && !f.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;
+
+ /* Set the data that propagates. */
+
+ next->prop = 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;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of routers/redirect.c */
diff --git a/src/routers/redirect.h b/src/routers/redirect.h
new file mode 100644
index 0000000..4c0399a
--- /dev/null
+++ b/src/routers/redirect.h
@@ -0,0 +1,70 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/* 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;
+
+ 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
index 0000000..d7c9c1c
--- /dev/null
+++ b/src/routers/rf_change_domain.c
@@ -0,0 +1,85 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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, const uschar *domain, BOOL rewrite,
+ address_item **addr_new)
+{
+address_item * parent = store_get(sizeof(address_item), GET_UNTAINTED);
+uschar * at = Ustrrchr(addr->address, '@');
+uschar * address = string_sprintf("%.*s@%s",
+ (int)(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->prop = parent->prop;
+
+addr->address = address;
+addr->unique = string_copy(address);
+addr->parent = parent;
+parent->child_count = 1;
+
+addr->next = *addr_new;
+*addr_new = addr;
+
+/* Rewrite header lines if requested */
+
+if (rewrite)
+ {
+ DEBUG(D_route|D_rewrite) debug_printf("rewriting header lines\n");
+ for (header_line * 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)
+ {
+ h = newh;
+ f.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
index 0000000..6a8ad17
--- /dev/null
+++ b/src/routers/rf_expand_data.c
@@ -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 (f.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
index 0000000..f310d5a
--- /dev/null
+++ b/src/routers/rf_functions.h
@@ -0,0 +1,31 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* 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 *, const uschar *, BOOL, address_item **);
+extern uschar *rf_expand_data(address_item *, uschar *, int *);
+extern int rf_get_errors_address(address_item *, router_instance *,
+ int, 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
index 0000000..b9cf781
--- /dev/null
+++ b/src/routers/rf_get_errors_address.c
@@ -0,0 +1,132 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 */
+/* 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->prop.errors_address;
+if (rblock->errors_to == NULL) return OK;
+
+s = expand_string(rblock->errors_to);
+
+if (s == NULL)
+ {
+ if (f.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)
+ {
+ addr->prop.ignore_error = TRUE; /* 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 = f.address_test_mode;
+ int save1 = 0;
+ int i;
+ const uschar ***p;
+ const uschar *address_expansions_save[ADDRESS_EXPANSIONS_COUNT];
+ address_item *snew = deliver_make_addr(s, FALSE);
+
+ if (sender_address)
+ {
+ save1 = sender_address[0];
+ sender_address[0] = 0;
+ }
+
+ for (i = 0, p = address_expansions; *p;)
+ address_expansions_save[i++] = **p++;
+ f.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);
+
+ f.address_test_mode = save_address_test_mode;
+ for (i = 0, p = address_expansions; *p; )
+ **p++ = address_expansions_save[i++];
+
+ if (sender_address) 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
index 0000000..d304d11
--- /dev/null
+++ b/src/routers/rf_get_munge_headers.c
@@ -0,0 +1,123 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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->prop.extra_headers;
+
+if (rblock->extra_headers)
+ {
+ const uschar * list = rblock->extra_headers;
+ int sep = '\n';
+ uschar * s, * t;
+ int slen;
+
+ while ((s = string_nextinlist(&list, &sep, NULL, 0)))
+ if (!(s = expand_string(t = s)))
+ {
+ if (!f.expand_string_forcedfail)
+ {
+ addr->message = string_sprintf(
+ "%s router failed to expand add_headers item \"%s\": %s",
+ rblock->name, t, 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), GET_UNTAINTED);
+
+ /* 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, s);
+ 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->prop.remove_headers;
+
+/* Expand items from colon-sep list separately, then build new list */
+if (rblock->remove_headers)
+ {
+ const uschar * list = rblock->remove_headers;
+ int sep = ':';
+ uschar * s, * t;
+ gstring * g = NULL;
+
+ if (*remove_headers)
+ g = string_cat(NULL, *remove_headers);
+
+ while ((s = string_nextinlist(&list, &sep, NULL, 0)))
+ if (!(s = expand_string(t = s)))
+ {
+ if (!f.expand_string_forcedfail)
+ {
+ addr->message = string_sprintf(
+ "%s router failed to expand remove_headers item \"%s\": %s",
+ rblock->name, t, expand_string_message);
+ return DEFER;
+ }
+ }
+ else if (*s)
+ g = string_append_listele(g, ':', s);
+
+ if (g)
+ *remove_headers = g->s;
+ }
+
+return OK;
+}
+
+/* vi: aw ai sw=2
+*/
+/* 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
index 0000000..2f639e0
--- /dev/null
+++ b/src/routers/rf_get_transport.c
@@ -0,0 +1,97 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/* 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;
+
+if (!tpname)
+ {
+ if (!require_name) 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)
+ {
+ if (!(ss = expand_string(tpname)))
+ {
+ 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;
+ }
+ if (is_tainted(ss))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "attempt to use tainted value '%s' from '%s' for transport", ss, tpname);
+ addr->basic_errno = ERRNO_BADTRANSPORT;
+ /* Avoid leaking info to an attacker */
+ addr->message = US"internal configuration error";
+ return FALSE;
+ }
+ }
+else
+ ss = tpname;
+
+for (transport_instance * tp = transports; tp; 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
index 0000000..1735e59
--- /dev/null
+++ b/src/routers/rf_get_ugid.c
@@ -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
index 0000000..79a7799
--- /dev/null
+++ b/src/routers/rf_lookup_hostlist.c
@@ -0,0 +1,262 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* Copyright (c) The Exim Maintainers 2020 */
+/* 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,
+ plus LK_IPV4_{ONLY,PREFER}
+ 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;
+
+/* 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. */
+
+for (host_item * prev = NULL, * h = addr->host_list, *next_h; h; h = next_h)
+ {
+ const uschar *canonical_name;
+ int rc, len, port, mx, sort_key;
+
+ next_h = h->next;
+ if (h->address) { 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);
+
+ /* Store the previous mx and sort_key values, which were assigned in
+ host_build_hostlist and will be overwritten by host_find_bydns. */
+
+ mx = h->mx;
+ sort_key = h->sort_key;
+
+ /* If the name ends with "/MX", we interpret it to mean "the list of hosts
+ pointed to by MX records with this name", and the MX record values override
+ the ordering from host_build_hostlist. */
+
+ len = Ustrlen(h->name);
+ if (len > 3 && strcmpic(h->name + len - 3, US"/mx") == 0)
+ {
+ int whichrrs = lookup_type & LK_IPV4_ONLY
+ ? HOST_FIND_BY_MX | HOST_FIND_IPV4_ONLY
+ : lookup_type & LK_IPV4_PREFER
+ ? HOST_FIND_BY_MX | HOST_FIND_IPV4_FIRST
+ : HOST_FIND_BY_MX;
+
+ DEBUG(D_route|D_host_lookup)
+ debug_printf("doing DNS MX lookup for %s\n", h->name);
+
+ mx = MX_NONE;
+ h->name = string_copyn(h->name, len - 3);
+ rc = host_find_bydns(h,
+ ignore_target_hosts,
+ whichrrs, /* look only for MX records */
+ NULL, /* SRV service not relevant */
+ NULL, /* failing srv domains not relevant */
+ NULL, /* no special mx failing domains */
+ &rblock->dnssec, /* dnssec request/require */
+ 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;
+ int whichrrs = lookup_type & LK_IPV4_ONLY
+ ? HOST_FIND_BY_A
+ : lookup_type & LK_IPV4_PREFER
+ ? HOST_FIND_BY_A | HOST_FIND_BY_AAAA | HOST_FIND_IPV4_FIRST
+ : HOST_FIND_BY_A | HOST_FIND_BY_AAAA;
+
+ DEBUG(D_route|D_host_lookup) debug_printf("doing DNS lookup\n");
+ switch (rc = host_find_bydns(h, ignore_target_hosts, whichrrs, NULL,
+ NULL, NULL,
+ &rblock->dnssec, /* domains for request/require */
+ &canonical_name, &removed))
+ {
+ case HOST_FOUND:
+ if (removed) setflag(addr, af_local_host_removed);
+ break;
+ case HOST_FIND_FAILED:
+ if (lookup_type & LK_DEFAULT)
+ {
+ DEBUG(D_route|D_host_lookup)
+ debug_printf("DNS lookup failed: trying %s\n",
+ f.running_in_test_harness
+ ? "host_fake_gethostbyname" : "getipnodebyname");
+ rc = host_find_byname(h, ignore_target_hosts, HOST_FIND_QUALIFY_SINGLE,
+ &canonical_name, TRUE);
+ }
+ break;
+ }
+ }
+
+ /* Temporary failure defers, unless pass_on_timeout is set */
+
+ if (rc == HOST_FIND_SECURITY)
+ {
+ addr->message = string_sprintf("host lookup for %s done insecurely" , h->name);
+ addr->basic_errno = ERRNO_DNSDEFER;
+ return DEFER;
+ }
+ 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->basic_errno = ERRNO_UNKNOWNHOST;
+ addr->message =
+ string_sprintf("lookup of host \"%s\" failed in %s router%s",
+ h->name, rblock->name,
+ f.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 the settings that were previously cleared:
+ port, mx and sort_key. */
+
+ if (port != PORT_NONE)
+ for (host_item * hh = h; hh != next_h; hh = hh->next)
+ hh->port = port;
+
+ if (mx != MX_NONE)
+ for (host_item * hh = h; hh != next_h; hh = hh->next)
+ {
+ hh->mx = mx;
+ hh->sort_key = sort_key;
+ }
+
+ /* 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)
+ {
+ DEBUG(D_route)
+ {
+ debug_printf("Removed from host list:\n");
+ for (; h; 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
index 0000000..0693c8c
--- /dev/null
+++ b/src/routers/rf_queue_add.c
@@ -0,0 +1,109 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2021 */
+/* 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->prop.domain_data = deliver_domain_data; /* Save these values for */
+addr->prop.localpart_data = deliver_localpart_data; /* use in the transport */
+
+/* Handle a local transport */
+
+if (addr->transport && 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)
+ {
+ addr->uid = pw->pw_uid;
+ addr->gid = pw->pw_gid;
+ setflag(addr, af_uid_set);
+ setflag(addr, af_gid_set);
+ setflag(addr, 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)
+ {
+ addr->home_dir = rblock->home_directory;
+ clearflag(addr, af_home_expanded);
+ }
+ else if (!addr->home_dir && 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 ? addr->transport->name : US"<unset>",
+ addr->local_part, addr->domain, addr->prop.errors_address);
+ debug_printf(" domain_data=%s local_part_data=%s\n", addr->prop.domain_data,
+ addr->prop.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
index 0000000..9a4dc3c
--- /dev/null
+++ b/src/routers/rf_self_action.c
@@ -0,0 +1,123 @@
+/*************************************************
+* 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)
+ 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 (while routing <%s>)", msg,
+ addr->domain, addr->address);
+ 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: domain changed to %s\n", msg, addr->domain, new);
+ rf_change_domain(addr, new, rewrite, addr_new);
+ return REROUTED;
+
+ case self_send:
+ DEBUG(D_route)
+ debug_printf("%s: %s: configured to try delivery anyway\n", msg, addr->domain);
+ return OK;
+
+ case self_pass: /* This is soft failure; pass to next router */
+ DEBUG(D_route)
+ debug_printf("%s: %s: passed to next router (self = pass)\n", msg, addr->domain);
+ addr->message = msg;
+ addr->self_hostname = string_copy(host->name);
+ return PASS;
+
+ case self_fail:
+ DEBUG(D_route)
+ debug_printf("%s: %s: address failed (self = fail)\n", msg, addr->domain);
+ 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
index 0000000..e1346b4
--- /dev/null
+++ b/src/routers/rf_set_ugid.c
@@ -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
index 0000000..eec5437
--- /dev/null
+++ b/src/search.c
@@ -0,0 +1,967 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* 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 rmark 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(const uschar * name, int len)
+{
+for (int bot = 0, top = lookup_list_count; 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
+ opts where to put the options
+
+Returns: +ve => valid lookup name; value is offset in lookup_list
+ -ve => invalid name; message in search_error_message.
+*/
+
+int
+search_findtype_partial(const uschar *name, int *ptypeptr, const uschar **ptypeaff,
+ int *afflen, int *starflags, const uschar ** opts)
+{
+int len, stype;
+int pv = -1;
+const uschar *ss = name;
+const uschar * t;
+
+*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 *@,
+and then by options starting with a "," */
+
+len = Ustrlen(ss);
+if ((t = Ustrchr(ss, '*')))
+ {
+ len = t - ss;
+ *starflags |= (t[1] == '@' ? SEARCH_STARAT : SEARCH_STAR);
+ }
+else
+ t = ss;
+
+if ((t = Ustrchr(t, ',')))
+ {
+ int l = t - ss;
+ if (l < len) len = l;
+ *opts = string_copy(t+1);
+ }
+else
+ *opts = NULL;
+
+/* 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;
+}
+
+
+/* Set the parameters for the three different kinds of lookup.
+Arguments:
+ search_type the search-type code
+ search the search-type string
+ query argument for the search; filename or query
+ fnamep pointer to return filename
+ opts options
+
+Return: keyquery the search-type (for single-key) or query (for query-type)
+ */
+uschar *
+search_args(int search_type, uschar * search, uschar * query, uschar ** fnamep,
+ const uschar * opts)
+{
+Uskip_whitespace(&query);
+if (mac_islookup(search_type, lookup_absfilequery))
+ { /* query-style but with file (sqlite) */
+ int sep = ',';
+
+ /* Check options first for new-style file spec */
+ if (opts) for (uschar * s; s = string_nextinlist(&opts, &sep, NULL, 0); )
+ if (Ustrncmp(s, "file=", 5) == 0)
+ {
+ *fnamep = s+5;
+ return query;
+ }
+
+ /* If no filename from options, use old-tyle space-sep prefix on query */
+ if (*query == '/')
+ {
+ uschar * s = query;
+ while (*query && !isspace(*query)) query++;
+ *fnamep = string_copyn(s, query - s);
+ Uskip_whitespace(&query);
+ }
+ else
+ *fnamep = NULL;
+ return query; /* remainder after file skipped */
+ }
+if (!mac_islookup(search_type, lookup_querystyle))
+ { /* single-key */
+ *fnamep = query;
+ return search; /* modifiers important so use "keyquery" for them */
+ }
+*fnamep = NULL; /* else query-style */
+return query;
+}
+
+
+
+/*************************************************
+* 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) tidyup_subtree(t->left);
+if (t->right) tidyup_subtree(t->right);
+if (c && c->handle && lookup_list[c->search_type]->close)
+ lookup_list[c->search_type]->close(c->handle);
+}
+
+
+/* The external entry point
+
+Argument: none
+Returns: nothing
+*/
+
+void
+search_tidyup(void)
+{
+int old_pool = store_pool;
+
+DEBUG(D_lookup) debug_printf_indent("search_tidyup called\n");
+
+/* Close individually each cached open file. */
+
+store_pool = POOL_SEARCH;
+if (search_tree)
+ {
+ 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 (int i = 0; i < lookup_list_count; i++) if (lookup_list[i]->tidy)
+ (lookup_list[i]->tidy)();
+
+if (search_reset_point) search_reset_point = store_reset(search_reset_point);
+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(const 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;
+
+if (filename && is_tainted(filename))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "Tainted filename for search: '%s'", filename);
+ return NULL;
+ }
+
+/* Change to the search store pool and remember our reset point */
+
+store_pool = POOL_SEARCH;
+if (!search_reset_point) search_reset_point = store_mark();
+
+DEBUG(D_lookup) debug_printf_indent("search_open: %s \"%s\"\n", lk->name,
+ filename ? filename : US"NULL");
+
+/* 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 ? filename : US"");
+
+if ((t = tree_search(search_tree, keybuffer)))
+ {
+ if ((c = (search_cache *)t->data.ptr)->handle)
+ {
+ DEBUG(D_lookup) debug_printf_indent(" cached open\n");
+ store_pool = old_pool;
+ return t;
+ }
+ DEBUG(D_lookup) debug_printf_indent(" 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)
+ 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_indent("Too many lookup files open\n closing %s\n",
+ open_bot->name);
+ if ((open_bot = c->up))
+ ((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. */
+
+if (!(handle = (lk->open)(filename, &search_error_message)))
+ {
+ store_pool = old_pool;
+ return NULL;
+ }
+
+if ( lk->check
+ && !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)
+ {
+ t = store_get(sizeof(tree_node) + Ustrlen(keybuffer), GET_UNTAINTED);
+ t->data.ptr = c = store_get(sizeof(search_cache), GET_UNTAINTED);
+ 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
+ cache_rd FALSE to avoid lookup in cache layer
+ opts type-specific options
+
+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, const uschar * filename, uschar * keystring,
+ BOOL cache_rd, const uschar * opts)
+{
+tree_node * t = (tree_node *)handle;
+search_cache * c = (search_cache *)(t->data.ptr);
+expiring_data * e = NULL; /* compiler quietening */
+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"";
+f.search_find_defer = FALSE;
+
+DEBUG(D_lookup) debug_printf_indent("internal_search_find: file=\"%s\"\n "
+ "type=%s key=\"%s\" opts=%s%s%s\n", filename,
+ lookup_list[search_type]->name, keystring,
+ opts ? "\"" : "", opts, opts ? "\"" : "");
+
+/* 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. Check
+whether we want to use the cache entry last so that we can always replace it. */
+
+if ( (t = tree_search(c->item_cache, keystring))
+ && (!(e = t->data.ptr)->expiry || e->expiry > time(NULL))
+ && (!opts && !e->opts || opts && e->opts && Ustrcmp(opts, e->opts) == 0)
+ && cache_rd
+ )
+ { /* Data was in the cache already; set the pointer from the tree node */
+ data = e->data.ptr;
+ DEBUG(D_lookup) debug_printf_indent("cached data used for lookup of %s%s%s\n",
+ keystring,
+ filename ? US"\n in " : US"", filename ? filename : US"");
+ }
+else
+ {
+ uint do_cache = UINT_MAX;
+ int keylength = Ustrlen(keystring);
+
+ DEBUG(D_lookup)
+ {
+ if (t)
+ debug_printf_indent("cached data found but %s; ",
+ e->expiry && e->expiry <= time(NULL) ? "out-of-date"
+ : cache_rd ? "wrong opts" : "no_rd option set");
+ debug_printf_indent("%s lookup required for %s%s%s\n",
+ filename ? US"file" : US"database",
+ keystring,
+ filename ? US"\n in " : US"", filename ? filename : US"");
+ if (!filename && is_tainted(keystring))
+ {
+ debug_printf_indent(" ");
+ debug_print_taint(keystring);
+ }
+ }
+
+ /* Check that the query, for query-style lookups,
+ is either untainted or properly quoted for the lookup type.
+
+ XXX Should we this move into lf_sqlperform() ? The server-taint check is there.
+ */
+
+ if ( !filename && lookup_list[search_type]->quote
+ && is_tainted(keystring) && !is_quoted_like(keystring, search_type))
+ {
+ uschar * s = acl_current_verb();
+ if (!s) s = authenticator_current_name(); /* must be before transport */
+ if (!s) s = transport_current_name(); /* must be before router */
+ if (!s) s = router_current_name(); /* GCC ?: would be good, but not in clang */
+ if (!s) s = US"";
+#ifdef enforce_quote_protection_notyet
+ search_error_message = string_sprintf(
+ "tainted search query is not properly quoted%s: %s%s",
+ s, keystring);
+ f.search_find_defer = TRUE;
+#else
+ {
+ int q = quoter_for_address(keystring);
+ /* If we're called from a transport, no privs to open the paniclog;
+ the logging punts to using stderr - and that seems to stop the debug
+ stream. */
+ log_write(0,
+ transport_name ? LOG_MAIN : LOG_MAIN|LOG_PANIC,
+ "tainted search query is not properly quoted%s: %s", s, keystring);
+
+ DEBUG(D_lookup) debug_printf_indent("search_type %d (%s) quoting %d (%s)\n",
+ search_type, lookup_list[search_type]->name,
+ q, is_real_quoter(q) ? lookup_list[q]->name : US"none");
+ }
+#endif
+ }
+
+ /* 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, opts) == DEFER)
+ f.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. Lookups can also set a TTL for the
+ cache entry; the dnsdb lookup does.
+ Finally, the caller can request no caching by setting an option. */
+
+ else if (do_cache)
+ {
+ DEBUG(D_lookup) debug_printf_indent("%s cache entry\n",
+ t ? "replacing old" : "creating new");
+ if (!t) /* No existing entry. Create new one. */
+ {
+ int len = keylength + 1;
+ /* The cache node value should never be expanded so use tainted mem */
+ e = store_get(sizeof(expiring_data) + sizeof(tree_node) + len, GET_TAINTED);
+ t = (tree_node *)(e+1);
+ memcpy(t->name, keystring, len);
+ t->data.ptr = e;
+ tree_insertnode(&c->item_cache, t);
+ }
+ /* Else previous, out-of-date cache entry. Update with the */
+ /* new result and forget the old one */
+ e->expiry = do_cache == UINT_MAX ? 0 : time(NULL)+do_cache;
+ e->opts = opts ? string_copy(opts) : NULL;
+ e->data.ptr = data;
+ }
+
+/* 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_indent("lookup forced cache cleanup\n");
+ c->item_cache = NULL; /* forget all lookups on this connection */
+ }
+ }
+
+DEBUG(D_lookup)
+ {
+ if (data)
+ debug_printf_indent("lookup yielded: %s\n", data);
+ else if (f.search_find_defer)
+ debug_printf_indent("lookup deferred: %s\n", search_error_message);
+ else debug_printf_indent("lookup failed\n");
+ }
+
+/* Return it in new dynamic store in the regular pool */
+
+store_pool = old_pool;
+return data ? string_copy(data) : NULL;
+}
+
+
+
+
+/*************************************************
+* 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
+ opts type-specific options
+
+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, const uschar * filename, uschar * keystring,
+ int partial, const uschar * affix, int affixlen, int starflags,
+ int * expand_setup, const uschar * opts)
+{
+tree_node * t = (tree_node *)handle;
+BOOL set_null_wild = FALSE, cache_rd = TRUE, ret_key = FALSE;
+uschar * yield;
+
+DEBUG(D_lookup)
+ {
+ if (partial < 0) affixlen = 99; /* So that "NULL" prints */
+ debug_printf_indent("search_find: file=\"%s\"\n key=\"%s\" "
+ "partial=%d affix=%.*s starflags=%x opts=%s%s%s\n",
+ filename ? filename : US"NULL",
+ keystring, partial, affixlen, affix, starflags,
+ opts ? "\"" : "", opts, opts ? "\"" : "");
+
+ }
+
+/* Parse global lookup options. Also, create a new options list with
+the global options dropped so that the cache-modifiers are not
+used in the cache key. */
+
+if (opts)
+ {
+ int sep = ',';
+ gstring * g = NULL;
+
+ for (uschar * ele; ele = string_nextinlist(&opts, &sep, NULL, 0); )
+ if (Ustrcmp(ele, "ret=key") == 0) ret_key = TRUE;
+ else if (Ustrcmp(ele, "cache=no_rd") == 0) cache_rd = FALSE;
+ else g = string_append_listele(g, ',', ele);
+
+ opts = string_from_gstring(g);
+ }
+
+/* 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)
+ {
+ ((search_cache *)(up->data.ptr))->down = down;
+ if (down)
+ ((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) open_bot = t;
+ else ((search_cache *)(open_top->data.ptr))->up = t;
+ open_top = t;
+ }
+
+DEBUG(D_lookup)
+ {
+ debug_printf_indent("LRU list:\n");
+ for (tree_node *t = open_top; t; )
+ {
+ search_cache *c = (search_cache *)(t->data.ptr);
+ debug_printf_indent(" %s\n", t->name);
+ if (t == open_bot) debug_printf_indent(" 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, cache_rd, opts);
+if (f.search_find_defer) return NULL;
+
+if (yield) { 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,
+ is_tainted(keystring) || is_tainted(affix) ? GET_TAINTED : GET_UNTAINTED);
+ Ustrncpy(keystring2, affix, affixlen);
+ Ustrcpy(keystring2 + affixlen, keystring);
+ DEBUG(D_lookup) debug_printf_indent("trying partial match %s\n", keystring2);
+ yield = internal_search_find(handle, filename, keystring2, cache_rd, opts);
+ if (f.search_find_defer) return NULL;
+ }
+
+ /* The key in its entirety did not match a wild entry; try chopping off
+ leading components. */
+
+ if (!yield)
+ {
+ 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_indent("trying partial match %s\n", keystring3);
+ yield = internal_search_find(handle, filename, keystring3,
+ cache_rd, opts);
+ if (f.search_find_defer) return NULL;
+ if (yield)
+ {
+ /* First variable is the wild part; second is the fixed part. Take care
+ to get it right when keystring3 is just "*". */
+
+ if (expand_setup && *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 everything to the left of @ by *. After a match, the wild part
+is set to the string to the left of the @. */
+
+if (!yield && starflags & SEARCH_STARAT)
+ {
+ uschar *atat = Ustrrchr(keystring, '@');
+ if (atat != NULL && atat > keystring)
+ {
+ int savechar;
+ savechar = *(--atat);
+ *atat = '*';
+
+ DEBUG(D_lookup) debug_printf_indent("trying default match %s\n", atat);
+ yield = internal_search_find(handle, filename, atat, cache_rd, opts);
+ *atat = savechar;
+ if (f.search_find_defer) return NULL;
+
+ if (yield && expand_setup && *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 && starflags & (SEARCH_STAR|SEARCH_STARAT))
+ {
+ DEBUG(D_lookup) debug_printf_indent("trying to match *\n");
+ yield = internal_search_find(handle, filename, US"*", cache_rd, opts);
+ if (yield && expand_setup && *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 && *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);
+ }
+
+/* If we have a result, check the options to see if the key was wanted rather
+than the result. Return a de-tainted version of the key on the grounds that
+it have been validated by the lookup. */
+
+if (yield && ret_key)
+ yield = string_copy_taint(keystring, GET_UNTAINTED);
+
+return yield;
+}
+
+/* End of search.c */
diff --git a/src/setenv.c b/src/setenv.c
new file mode 100644
index 0000000..90e6793
--- /dev/null
+++ b/src/setenv.c
@@ -0,0 +1,58 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Michael Haardt 2015
+ * Copyright (c) Jeremy Harris 2015 - 2016
+ * Copyright (c) The Exim Maintainers 2016 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This module provides (un)setenv routines for those environments
+lacking them in libraries. It is #include'd by OS/os.c-foo files. */
+
+
+int
+setenv(const char * name, const char * val, int overwrite)
+{
+uschar * s;
+if (Ustrchr(name, '=')) return -1;
+if (overwrite || !getenv(name))
+ putenv(CS string_copy_perm(string_sprintf("%s=%s", name, val), FALSE));
+return 0;
+}
+
+int
+unsetenv(const char *name)
+{
+size_t len;
+const char * end;
+extern char ** environ;
+
+if (!name)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+if (!environ)
+ return 0;
+
+for (end = name; *end != '=' && *end; ) end++;
+len = end - name;
+
+/* Find name in environment and move remaining variables down.
+Do not early-out in case there are duplicate names. */
+
+for (char ** e = environ; *e; e++)
+ if (strncmp(*e, name, len) == 0 && (*e)[len] == '=')
+ {
+ char ** sp = e;
+ do *sp = sp[1]; while (*++sp);
+ }
+
+return 0;
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of setenv.c */
diff --git a/src/sha_ver.h b/src/sha_ver.h
new file mode 100644
index 0000000..bc2b2f8
--- /dev/null
+++ b/src/sha_ver.h
@@ -0,0 +1,47 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Jeremy Harris 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* SHA routine selection */
+
+#include "exim.h"
+
+/* Please be aware that pulling in extra headers which are not in the system
+ * includes may require careful juggling of CFLAGS in
+ * scripts/Configure-Makefile -- that logic should be kept in sync with this.
+ * In particular, building with just something like USE_OPENSSL_PC=openssl
+ * and not massaging CFLAGS in Local/Makefile is fully supported.
+ */
+
+#ifndef DISABLE_TLS
+
+# define EXIM_HAVE_SHA2
+
+# ifdef USE_GNUTLS
+# include <gnutls/gnutls.h>
+
+# if GNUTLS_VERSION_NUMBER >= 0x020a00
+# define SHA_GNUTLS
+# if GNUTLS_VERSION_NUMBER >= 0x030500
+# define EXIM_HAVE_SHA3 /*MMMM*/
+# endif
+# else
+# define SHA_GCRYPT
+# endif
+# endif
+
+# ifdef USE_OPENSSL
+# define SHA_OPENSSL
+# include <openssl/ssl.h>
+# if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER)
+# define EXIM_HAVE_SHA3
+# endif
+# endif
+
+#else
+# define SHA_NATIVE
+#endif
+
diff --git a/src/sieve.c b/src/sieve.c
new file mode 100644
index 0000000..af3bc9d
--- /dev/null
+++ b/src/sieve.c
@@ -0,0 +1,3629 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/*
+ * Copyright (c) The Exim Maintainers 2016 - 2022
+ * Copyright (c) Michael Haardt 2003 - 2015
+ * 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
+ {
+ const 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
+ const uschar *enotify_mailto_owner;
+#ifdef SUBADDRESS
+ int require_subaddress;
+#endif
+#ifdef VACATION
+ int require_vacation;
+ int vacation_ran;
+#endif
+ const 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 };
+#ifdef ENVELOPE_AUTH
+static uschar str_auth_c[]="auth";
+static const struct String str_auth={ str_auth_c, 4 };
+#endif
+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)
+{
+uschar *new = NULL;
+uschar ch;
+size_t line;
+
+/* Two passes: one to count output allocation size, second
+to do the encoding */
+
+for (int pass = 0; pass <= 1; pass++)
+ {
+ line=0;
+ if (pass==0)
+ dst->length=0;
+ else
+ {
+ dst->character = store_get(dst->length+1, src->character); /* plus one for \0 */
+ new=dst->character;
+ }
+ for (const uschar * start = src->character, * end = start + src->length;
+ start < end; ++start)
+ {
+ ch=*start;
+ if (line>=73) /* line length limit */
+ {
+ if (pass==0)
+ dst->length+=2;
+ else
+ {
+ *new++='='; /* line split */
+ *new++='\n';
+ }
+ line=0;
+ }
+ if ( (ch>='!' && ch<='<')
+ || (ch>='>' && ch<='~')
+ || ( (ch=='\t' || ch==' ')
+ && start+2<end
+ && (*(start+1)!='\r' || *(start+2)!='\n') /* CRLF */
+ )
+ )
+ {
+ if (pass==0)
+ ++dst->length;
+ else
+ *new++=*start; /* copy char */
+ ++line;
+ }
+ else if (ch=='\r' && start+1<end && *(start+1)=='\n') /* CRLF */
+ {
+ if (pass==0)
+ ++dst->length;
+ else
+ *new++='\n'; /* NL */
+ line=0;
+ ++start; /* consume extra input char */
+ }
+ else
+ {
+ if (pass==0)
+ dst->length+=3;
+ else
+ { /* encoded char */
+ new += sprintf(CS new,"=%02X",ch);
+ }
+ 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)
+ {
+ 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;
+struct String hvalue = {.character = NULL, .length = 0};
+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)
+ {
+ gstring * g = string_catn(NULL, start, uri-start);
+
+ to.character = string_from_gstring(g);
+ to.length = g->ptr;
+ if (uri_decode(&to)==-1)
+ {
+ filter->errmsg=US"Invalid URI encoding";
+ return -1;
+ }
+ new = store_get(sizeof(string_item), GET_UNTAINTED);
+ new->text = store_get(to.length+1, to.character);
+ 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)
+ {
+ gstring * g = string_catn(NULL, start, uri-start);
+
+ hname.character = string_from_gstring(g);
+ hname.length = g->ptr;
+ 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)
+ {
+ gstring * g = string_catn(NULL, start, uri-start);
+
+ hname.character = string_from_gstring(g);
+ hname.length = g->ptr;
+ 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), GET_UNTAINTED);
+ new->text = store_get(hvalue.length+1, hvalue.character);
+ 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)
+ {
+ gstring * g;
+
+ if (header->length==-1) header->length = 0;
+
+ g = string_catn(NULL, header->character, header->length);
+ g = string_catn(g, hname.character, hname.length);
+ g = string_catn(g, CUS ": ", 2);
+ g = string_catn(g, hvalue.character, hvalue.length);
+ g = string_catn(g, CUS "\n", 1);
+
+ header->character = string_from_gstring(g);
+ header->length = g->ptr;
+ }
+ }
+ 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)
+{
+gstring * quoted = NULL;
+size_t l;
+const uschar *h;
+
+l=header->length;
+h=header->character;
+while (l)
+ {
+ switch (*h)
+ {
+ case '\0':
+ quoted = string_catn(quoted, CUS "\\0", 2);
+ break;
+ case '$':
+ case '{':
+ case '}':
+ quoted = string_catn(quoted, CUS "\\", 1);
+ default:
+ quoted = string_catn(quoted, h, 1);
+ }
+ ++h;
+ --l;
+ }
+quoted = string_catn(quoted, CUS "", 1);
+return string_from_gstring(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)
+ || testflag(new_addr, af_file)
+ )
+ )
+ {
+ 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);
+ setflag(new_addr, af_file);
+ new_addr->mode = 0;
+ }
+new_addr->prop.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)));
+if (!t) return;
+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)
+{
+gstring * g = NULL;
+
+data->length = 0;
+data->character = NULL;
+
+if (*filter->pc=='"') /* quoted string */
+ {
+ ++filter->pc;
+ while (*filter->pc)
+ {
+ if (*filter->pc=='"') /* end of string */
+ {
+ ++filter->pc;
+
+ if (g)
+ {
+ data->character = string_from_gstring(g);
+ data->length = g->ptr;
+ }
+ else
+ data->character = US"\0";
+ /* that way, there will be at least one character allocated */
+
+#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 */
+ {
+ g = string_catn(g, 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')
+ {
+ g = string_catn(g, US"\r", 1);
+ ++filter->line;
+ }
+#endif
+ g = string_catn(g, 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
+ {
+ g = string_catn(g, 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
+ {
+ if (g)
+ {
+ data->character = string_from_gstring(g);
+ data->length = g->ptr;
+ }
+ else
+ data->character = US"\0";
+ /* that way, there will be at least one character allocated */
+
+#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 */
+ {
+ g = string_catn(g, CUS ".", 1);
+ filter->pc+=2;
+ }
+ }
+ else /* regular character */
+ {
+ g = string_catn(g, 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 = NULL;
+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;
+
+ dataCapacity = dataCapacity ? dataCapacity * 2 : 4;
+ new = store_get(sizeof(struct String) * dataCapacity, GET_UNTAINTED);
+
+ if (d) memcpy(new,d,sizeof(struct String)*dataLength);
+ d = new;
+ }
+
+ 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, GET_UNTAINTED)))
+ 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,*key;
+ 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 (struct String * 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 */
+ if (!(header_value = expand_string(string_sprintf("$rheader_%s",quote(h)))))
+ {
+ filter->errmsg=CUS "header string expansion failed";
+ return -1;
+ }
+ f.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 (struct String * k = key; k->length !=- 1; ++k)
+ {
+ struct String partStr = {.character = part, .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;
+ }
+ f.parse_allow_group = FALSE;
+ f.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;
+ 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 (struct String * 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)
+ {
+ 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,*key;
+ 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 (struct String * 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 || !header_def)
+ {
+ filter->errmsg=CUS "header string expansion failed";
+ return -1;
+ }
+ for (struct String * 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,*key;
+ 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 (struct String * 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)))
+ {
+ filter->errmsg=CUS "header string expansion failed";
+ return -1;
+ }
+ for (struct String * k = key; k->length != -1; ++k)
+ {
+ struct String envelopeStr = {.character = envelope, .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;
+ 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 (struct String * 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;
+
+ 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 (struct String * 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 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;
+ 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 "";
+ if (!envelope_from)
+ {
+ filter->errmsg=CUS "expansion failure for envelope from";
+ return -1;
+ }
+ 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(US"${if def:header_auto-submitted {true}{false}}");
+ if (!auto_submitted_value.character || !auto_submitted_def)
+ {
+ 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)
+ /* New notification, process it */
+ {
+ struct Notification * sent = store_get(sizeof(struct Notification), GET_UNTAINTED);
+ 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)
+ {
+ int pid, fd;
+
+ if ((pid = child_open_exim2(&fd, envelope_from, envelope_from,
+ US"sieve-notify")) >= 1)
+ {
+ FILE * 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 (string_item * 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);
+ }
+ if (message.length != -1)
+ fprintf(f, "Subject: %s\n", parse_quote_2047(message.character,
+ message.length, US"utf-8", 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)
+ 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)
+ debug_printf("Repeated notification to `%s' ignored.\n",method.character);
+ }
+ else
+ if ((filter_test != FTEST_NONE && debug_selector != 0) || debug_selector & D_filter)
+ 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)
+ {
+ 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 (struct String * a = addresses; a->length != -1; ++a)
+ {
+ string_item * new = store_get(sizeof(string_item), GET_UNTAINTED);
+
+ new->text = store_get(a->length+1, a->character);
+ 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;
+ md5 base;
+ uschar digest[16];
+ uschar hexdigest[33];
+ gstring * 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 */
+
+ md5_start(&base);
+
+ if (handle.length==-1)
+ {
+ gstring * key = NULL;
+ if (subject.length!=-1) key =string_catn(key, subject.character, subject.length);
+ if (from.length!=-1) key = string_catn(key, from.character, from.length);
+ key = string_catn(key, reason_is_mime?US"1":US"0", 1);
+ key = string_catn(key, reason.character, reason.length);
+ md5_end(&base, key->s, key->ptr, digest);
+ }
+ else
+ md5_end(&base, handle.character, handle.length, digest);
+
+ for (int 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)
+ {
+ once = string_cat (NULL, filter->vacation_directory);
+ once = string_catn(once, US"/", 1);
+ once = string_catn(once, hexdigest, 33);
+
+ /* process subject */
+
+ if (subject.length==-1)
+ {
+ uschar *subject_def;
+
+ subject_def = expand_string(US"${if def:header_subject {true}{false}}");
+ if (subject_def && Ustrcmp(subject_def,"true")==0)
+ {
+ gstring * g = string_catn(NULL, US"Auto: ", 6);
+
+ expand_header(&subject,&str_subject);
+ g = string_catn(g, subject.character, subject.length);
+ subject.character = string_from_gstring(g);
+ subject.length = g->ptr;
+ }
+ 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);
+ addr->prop.ignore_error = TRUE;
+ addr->next = *generated;
+ *generated = addr;
+ addr->reply = store_get(sizeof(reply_item), GET_UNTAINTED);
+ 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;
+ /* deconst cast safe as we pass in a non-const item */
+ addr->reply->subject = US parse_quote_2047(subject.character, subject.length, US"utf-8", TRUE);
+ addr->reply->oncelog = string_from_gstring(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++;
+
+ addr->reply->headers = string_copyn(reason.character, mime_body-reason.character);
+
+ if (mime_body+(sizeof(nlnl)-1)<reason_end) mime_body+=(sizeof(nlnl)-1);
+ else mime_body=reason_end-1;
+ addr->reply->text = string_copyn(mime_body, reason_end-mime_body);
+ }
+ else
+ {
+ struct String qp = { .character = NULL, .length = 0 }; /* Keep compiler happy (PH) */
+
+ 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 && filter_test == FTEST_NONE)
+ {
+ DIR *oncelogdir;
+ struct dirent *oncelog;
+ struct stat properties;
+ time_t now;
+
+ /* clean up old vacation log databases */
+
+ if ( !(oncelogdir = exim_opendir(filter->vacation_directory))
+ && errno != ENOENT)
+ {
+ filter->errmsg = CUS "unable to open vacation directory";
+ return -1;
+ }
+
+ if (oncelogdir)
+ {
+ time(&now);
+
+ while ((oncelog = readdir(oncelogdir)))
+ 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;
+ 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 (struct String * 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)
+ {
+ 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)
+ {
+ 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(const uschar * filter, int options,
+ const uschar * vacation_directory, const uschar * enotify_mailto_owner,
+ const uschar * useraddress, const uschar * subaddress,
+ address_item ** generated, uschar ** error)
+{
+struct Sieve sieve;
+int r;
+uschar * msg;
+
+DEBUG(D_route) debug_printf("Sieve: start of processing\n");
+sieve.filter = filter;
+
+if (!vacation_directory)
+ sieve.vacation_directory = NULL;
+else if (!(sieve.vacation_directory = expand_cstring(vacation_directory)))
+ {
+ *error = string_sprintf("failed to expand \"%s\" "
+ "(sieve_vacation_directory): %s", vacation_directory,
+ expand_string_message);
+ return FF_ERROR;
+ }
+
+if (!enotify_mailto_owner)
+ sieve.enotify_mailto_owner = NULL;
+else if (!(sieve.enotify_mailto_owner = expand_cstring(enotify_mailto_owner)))
+ {
+ *error = string_sprintf("failed to expand \"%s\" "
+ "(sieve_enotify_mailto_owner): %s", enotify_mailto_owner,
+ expand_string_message);
+ return FF_ERROR;
+ }
+
+sieve.useraddress = useraddress
+ ? useraddress : CUS "$local_part_prefix$local_part$local_part_suffix";
+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 = US"Implicit keep";
+ r = FF_DELIVERED;
+ }
+ else
+ {
+ msg = US"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
index 0000000..edb0adf
--- /dev/null
+++ b/src/smtp_in.c
@@ -0,0 +1,6068 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for handling an incoming SMTP call. */
+
+
+#include "exim.h"
+#include <assert.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 */
+ TLS_AUTH_CMD, /* auto-command at start of SSL */
+
+ /* 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,
+
+ /* RFC3030 section 2: "After all MAIL and RCPT responses are collected and
+ processed the message is sent using a series of BDAT commands"
+ implies that BDAT should be synchronized. However, we see Google, at least,
+ sending MAIL,RCPT,BDAT-LAST in a single packet, clearly not waiting for
+ processing of the RCPT response(s). We shall do the same, and not require
+ synch for BDAT. Worse, as the chunk may (very likely will) follow the
+ command-header in the same packet we cannot do the usual "is there any
+ follow-on data after the command line" even for non-pipeline mode.
+ So we'll need an explicit check after reading the expected chunk amount
+ when non-pipe, before sending the ACK. */
+
+ BDAT_CMD,
+
+ /* 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 SUPPORT_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 struct {
+ BOOL auth_advertised :1;
+#ifndef DISABLE_TLS
+ BOOL tls_advertised :1;
+#endif
+ BOOL dsn_advertised :1;
+ BOOL esmtp :1;
+ BOOL helo_verify_required :1;
+ BOOL helo_verify :1;
+ BOOL helo_seen :1;
+ BOOL helo_accept_junk :1;
+#ifndef DISABLE_PIPE_CONNECT
+ BOOL pipe_connect_acceptable :1;
+#endif
+ BOOL rcpt_smtp_response_same :1;
+ BOOL rcpt_in_progress :1;
+ BOOL smtp_exit_function_called :1;
+#ifdef SUPPORT_I18N
+ BOOL smtputf8_advertised :1;
+#endif
+} fl = {
+ .helo_verify_required = FALSE,
+ .helo_verify = FALSE,
+ .smtp_exit_function_called = FALSE,
+};
+
+static auth_instance *authenticated_by;
+static int count_nonmail;
+static int nonmail_command_count;
+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.
+
+tls_auth is a pseudo-command, never expected in input. It is activated
+on TLS startup and looks for a tls authenticator. */
+
+static smtp_cmd_list cmd_list[] = {
+ /* name len cmd has_arg is_mail_cmd */
+
+ { "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 },
+#ifndef DISABLE_TLS
+ { "starttls", sizeof("starttls")-1, STARTTLS_CMD, FALSE, FALSE },
+ { "tls_auth", 0, TLS_AUTH_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 },
+ { "bdat", sizeof("bdat")-1, BDAT_CMD, TRUE, 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
+#define CMD_LIST_TLS_AUTH 5
+
+/* 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. */
+
+uschar * smtp_names[] =
+ {
+ US"NONE", US"AUTH", US"DATA", US"BDAT", 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_local[] = {
+ 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 */
+ };
+static uschar *protocols[] = {
+ US"smtp", /* HELO */
+ US"smtps", /* The rare case EHLO->STARTTLS->HELO */
+ US"esmtp", /* EHLO */
+ US"esmtps", /* EHLO->STARTTLS->EHLO */
+ US"esmtpa", /* EHLO->AUTH */
+ US"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 */
+
+/* Sanity check and validate optional args to MAIL FROM: envelope */
+enum {
+ ENV_MAIL_OPT_NULL,
+ ENV_MAIL_OPT_SIZE, ENV_MAIL_OPT_BODY, ENV_MAIL_OPT_AUTH,
+#ifndef DISABLE_PRDR
+ ENV_MAIL_OPT_PRDR,
+#endif
+ ENV_MAIL_OPT_RET, ENV_MAIL_OPT_ENVID,
+#ifdef SUPPORT_I18N
+ ENV_MAIL_OPT_UTF8,
+#endif
+ };
+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
+ { US"RET", ENV_MAIL_OPT_RET, TRUE },
+ { US"ENVID", ENV_MAIL_OPT_ENVID, TRUE },
+#ifdef SUPPORT_I18N
+ { US"SMTPUTF8",ENV_MAIL_OPT_UTF8, FALSE }, /* rfc6531 */
+#endif
+ /* keep this the last entry */
+ { 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;
+
+
+/* forward declarations */
+static int smtp_read_command(BOOL check_sync, unsigned buffer_lim);
+static int synprot_error(int type, int code, uschar *data, uschar *errmess);
+static void smtp_quit_handler(uschar **, uschar **);
+static void smtp_rset_handler(void);
+
+/*************************************************
+* 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 /* No transaction in progress */
+ || !LOGGING(smtp_incomplete_transaction))
+ return;
+
+/* Build list of recipients for logging */
+
+if (recipients_count > 0)
+ {
+ raw_recipients = store_get(recipients_count * sizeof(uschar *), GET_UNTAINTED);
+ for (int 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);
+}
+
+
+
+
+void
+smtp_command_timeout_exit(void)
+{
+log_write(L_lost_incoming_connection,
+ LOG_MAIN, "SMTP command timeout on%s connection from %s",
+ tls_in.active.sock >= 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);
+}
+
+void
+smtp_command_sigterm_exit(void)
+{
+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);
+}
+
+void
+smtp_data_timeout_exit(void)
+{
+log_write(L_lost_incoming_connection,
+ LOG_MAIN, "SMTP data timeout (message abandoned) on connection from %s F=<%s>",
+ sender_fullhost ? sender_fullhost : US"local process", sender_address);
+receive_bomb_out(US"data-timeout", US"SMTP incoming data timeout");
+/* Does not return */
+}
+
+void
+smtp_data_sigint_exit(void)
+{
+log_write(0, LOG_MAIN, "%s closed after %s",
+ smtp_get_connection_info(), had_data_sigint == SIGTERM ? "SIGTERM":"SIGINT");
+receive_bomb_out(US"signal-exit",
+ US"Service not available - SIGTERM or SIGINT received");
+/* Does not return */
+}
+
+
+/******************************************************************************/
+/* SMTP input buffer handling. Most of these are similar to stdio routines. */
+
+static void
+smtp_buf_init(void)
+{
+/* Set up the buffer for inputting using direct read() calls, and arrange to
+call the local functions instead of the standard C ones. Place a NUL at the
+end of the buffer to safety-stop C-string reads from it. */
+
+if (!(smtp_inbuffer = US malloc(IN_BUFFER_SIZE)))
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "malloc() failed for SMTP input buffer");
+smtp_inbuffer[IN_BUFFER_SIZE-1] = '\0';
+
+smtp_inptr = smtp_inend = smtp_inbuffer;
+smtp_had_eof = smtp_had_error = 0;
+}
+
+
+
+/* Refill the buffer, and notify DKIM verification code.
+Return false for error or EOF.
+*/
+
+static BOOL
+smtp_refill(unsigned lim)
+{
+int rc, save_errno;
+
+if (!smtp_out) return FALSE;
+fflush(smtp_out);
+if (smtp_receive_timeout > 0) ALARM(smtp_receive_timeout);
+
+/* Limit amount read, so non-message data is not fed to DKIM.
+Take care to not touch the safety NUL at the end of the buffer. */
+
+rc = read(fileno(smtp_in), smtp_inbuffer, MIN(IN_BUFFER_SIZE-1, lim));
+save_errno = errno;
+if (smtp_receive_timeout > 0) ALARM_CLR(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)
+ {
+ if (had_command_timeout) /* set by signal handler */
+ smtp_command_timeout_exit(); /* does not return */
+ if (had_command_sigterm)
+ smtp_command_sigterm_exit();
+ if (had_data_timeout)
+ smtp_data_timeout_exit();
+ if (had_data_sigint)
+ smtp_data_sigint_exit();
+
+ smtp_had_error = save_errno;
+ smtp_read_error = string_copy_perm(
+ string_sprintf(" (error: %s)", strerror(save_errno)), FALSE);
+ }
+ else
+ smtp_had_eof = 1;
+ return FALSE;
+ }
+#ifndef DISABLE_DKIM
+dkim_exim_verify_feed(smtp_inbuffer, rc);
+#endif
+smtp_inend = smtp_inbuffer + rc;
+smtp_inptr = smtp_inbuffer;
+return TRUE;
+}
+
+
+/* Check if there is buffered data */
+
+BOOL
+smtp_hasc(void)
+{
+return smtp_inptr < smtp_inend;
+}
+
+/* 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 negotiated itself into an TLS/SSL state.
+
+Arguments: lim Maximum amount to read/buffer
+Returns: the next character or EOF
+*/
+
+int
+smtp_getc(unsigned lim)
+{
+if (!smtp_hasc() && !smtp_refill(lim)) return EOF;
+return *smtp_inptr++;
+}
+
+/* Get many bytes, refilling buffer if needed */
+
+uschar *
+smtp_getbuf(unsigned * len)
+{
+unsigned size;
+uschar * buf;
+
+if (!smtp_hasc() && !smtp_refill(*len))
+ { *len = 0; return NULL; }
+
+if ((size = smtp_inend - smtp_inptr) > *len) size = *len;
+buf = smtp_inptr;
+smtp_inptr += size;
+*len = size;
+return buf;
+}
+
+/* Copy buffered data to the dkim feed.
+Called, unless TLS, just before starting to read message headers. */
+
+void
+smtp_get_cache(unsigned lim)
+{
+#ifndef DISABLE_DKIM
+int n = smtp_inend - smtp_inptr;
+if (n > lim)
+ n = lim;
+if (n > 0)
+ dkim_exim_verify_feed(smtp_inptr, n);
+#endif
+}
+
+
+/* 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)
+{
+if (smtp_inptr <= smtp_inbuffer) /* NB: NOT smtp_hasc() ! */
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "buffer underflow in smtp_ungetc");
+
+*--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;
+}
+
+
+/* Check if a getc will block or not */
+
+static BOOL
+smtp_could_getc(void)
+{
+int fd, rc;
+fd_set fds;
+struct timeval tzero = {.tv_sec = 0, .tv_usec = 0};
+
+if (smtp_inptr < smtp_inend)
+ return TRUE;
+
+fd = fileno(smtp_in);
+FD_ZERO(&fds);
+FD_SET(fd, &fds);
+rc = select(fd + 1, (SELECT_ARG2_TYPE *)&fds, NULL, NULL, &tzero);
+
+if (rc <= 0) return FALSE; /* Not ready to read */
+rc = smtp_getc(GETC_BUFFER_UNLIMITED);
+if (rc < 0) return FALSE; /* End of file or error */
+
+smtp_ungetc(rc);
+return TRUE;
+}
+
+
+/******************************************************************************/
+/*************************************************
+* Recheck synchronization *
+*************************************************/
+
+/* Synchronization checks can never be perfect because a packet may be on its
+way but not arrived when the check is done. 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
+wouldblock_reading(void)
+{
+#ifndef DISABLE_TLS
+if (tls_in.active.sock >= 0)
+ return !tls_could_getc();
+#endif
+
+return !smtp_could_getc();
+}
+
+static BOOL
+check_sync(void)
+{
+if (!smtp_enforce_sync || !sender_host_address || f.sender_host_notsocket)
+ return TRUE;
+
+return wouldblock_reading();
+}
+
+
+/******************************************************************************/
+/* Variants of the smtp_* input handling functions for use in CHUNKING mode */
+
+/* Forward declarations */
+static inline void bdat_push_receive_functions(void);
+static inline void bdat_pop_receive_functions(void);
+
+
+/* Get a byte from the smtp input, in CHUNKING mode. Handle ack of the
+previous BDAT chunk and getting new ones when we run out. Uses the
+underlying smtp_getc or tls_getc both for that and for getting the
+(buffered) data byte. EOD signals (an expected) no further data.
+ERR signals a protocol error, and EOF a closed input stream.
+
+Called from read_bdat_smtp() in receive.c for the message body, but also
+by the headers read loop in receive_msg(); manipulates chunking_state
+to handle the BDAT command/response.
+Placed here due to the correlation with the above smtp_getc(), which it wraps,
+and also by the need to do smtp command/response handling.
+
+Arguments: lim (ignored)
+Returns: the next character or ERR, EOD or EOF
+*/
+
+int
+bdat_getc(unsigned lim)
+{
+uschar * user_msg = NULL;
+uschar * log_msg;
+
+for(;;)
+ {
+#ifndef DISABLE_DKIM
+ unsigned dkim_save;
+#endif
+
+ if (chunking_data_left > 0)
+ return lwr_receive_getc(chunking_data_left--);
+
+ bdat_pop_receive_functions();
+#ifndef DISABLE_DKIM
+ dkim_save = dkim_collect_input;
+ dkim_collect_input = 0;
+#endif
+
+ /* Unless PIPELINING was offered, there should be no next command
+ until after we ack that chunk */
+
+ if (!f.smtp_in_pipelining_advertised && !check_sync())
+ {
+ unsigned n = smtp_inend - smtp_inptr;
+ if (n > 32) n = 32;
+
+ incomplete_transaction_log(US"sync failure");
+ log_write(0, LOG_MAIN|LOG_REJECT, "SMTP protocol synchronization error "
+ "(next input sent too soon: pipelining was not advertised): "
+ "rejected \"%s\" %s next input=\"%s\"%s",
+ smtp_cmd_buffer, host_and_ident(TRUE),
+ string_printing(string_copyn(smtp_inptr, n)),
+ smtp_inend - smtp_inptr > n ? "..." : "");
+ (void) synprot_error(L_smtp_protocol_error, 554, NULL,
+ US"SMTP synchronization error");
+ goto repeat_until_rset;
+ }
+
+ /* If not the last, ack the received chunk. The last response is delayed
+ until after the data ACL decides on it */
+
+ if (chunking_state == CHUNKING_LAST)
+ {
+#ifndef DISABLE_DKIM
+ dkim_collect_input = dkim_save;
+ dkim_exim_verify_feed(NULL, 0); /* notify EOD */
+ dkim_collect_input = 0;
+#endif
+ return EOD;
+ }
+
+ smtp_printf("250 %u byte chunk received\r\n", FALSE, chunking_datasize);
+ chunking_state = CHUNKING_OFFERED;
+ DEBUG(D_receive) debug_printf("chunking state %d\n", (int)chunking_state);
+
+ /* Expect another BDAT cmd from input. RFC 3030 says nothing about
+ QUIT, RSET or NOOP but handling them seems obvious */
+
+next_cmd:
+ switch(smtp_read_command(TRUE, 1))
+ {
+ default:
+ (void) synprot_error(L_smtp_protocol_error, 503, NULL,
+ US"only BDAT permissible after non-LAST BDAT");
+
+ repeat_until_rset:
+ switch(smtp_read_command(TRUE, 1))
+ {
+ case QUIT_CMD: smtp_quit_handler(&user_msg, &log_msg); /*FALLTHROUGH */
+ case EOF_CMD: return EOF;
+ case RSET_CMD: smtp_rset_handler(); return ERR;
+ default: if (synprot_error(L_smtp_protocol_error, 503, NULL,
+ US"only RSET accepted now") > 0)
+ return EOF;
+ goto repeat_until_rset;
+ }
+
+ case QUIT_CMD:
+ smtp_quit_handler(&user_msg, &log_msg);
+ /*FALLTHROUGH*/
+ case EOF_CMD:
+ return EOF;
+
+ case RSET_CMD:
+ smtp_rset_handler();
+ return ERR;
+
+ case NOOP_CMD:
+ HAD(SCH_NOOP);
+ smtp_printf("250 OK\r\n", FALSE);
+ goto next_cmd;
+
+ case BDAT_CMD:
+ {
+ int n;
+
+ if (sscanf(CS smtp_cmd_data, "%u %n", &chunking_datasize, &n) < 1)
+ {
+ (void) synprot_error(L_smtp_protocol_error, 501, NULL,
+ US"missing size for BDAT command");
+ return ERR;
+ }
+ chunking_state = strcmpic(smtp_cmd_data+n, US"LAST") == 0
+ ? CHUNKING_LAST : CHUNKING_ACTIVE;
+ chunking_data_left = chunking_datasize;
+ DEBUG(D_receive) debug_printf("chunking state %d, %d bytes\n",
+ (int)chunking_state, chunking_data_left);
+
+ if (chunking_datasize == 0)
+ if (chunking_state == CHUNKING_LAST)
+ return EOD;
+ else
+ {
+ (void) synprot_error(L_smtp_protocol_error, 504, NULL,
+ US"zero size for BDAT command");
+ goto repeat_until_rset;
+ }
+
+ bdat_push_receive_functions();
+#ifndef DISABLE_DKIM
+ dkim_collect_input = dkim_save;
+#endif
+ break; /* to top of main loop */
+ }
+ }
+ }
+}
+
+BOOL
+bdat_hasc(void)
+{
+if (chunking_data_left > 0)
+ return lwr_receive_hasc();
+return TRUE;
+}
+
+uschar *
+bdat_getbuf(unsigned * len)
+{
+uschar * buf;
+
+if (chunking_data_left <= 0)
+ { *len = 0; return NULL; }
+
+if (*len > chunking_data_left) *len = chunking_data_left;
+buf = lwr_receive_getbuf(len); /* Either smtp_getbuf or tls_getbuf */
+chunking_data_left -= *len;
+return buf;
+}
+
+void
+bdat_flush_data(void)
+{
+while (chunking_data_left)
+ {
+ unsigned n = chunking_data_left;
+ if (!bdat_getbuf(&n)) break;
+ }
+
+bdat_pop_receive_functions();
+chunking_state = CHUNKING_OFFERED;
+DEBUG(D_receive) debug_printf("chunking state %d\n", (int)chunking_state);
+}
+
+
+static inline void
+bdat_push_receive_functions(void)
+{
+/* push the current receive_* function on the "stack", and
+replace them by bdat_getc(), which in turn will use the lwr_receive_*
+functions to do the dirty work. */
+if (!lwr_receive_getc)
+ {
+ lwr_receive_getc = receive_getc;
+ lwr_receive_getbuf = receive_getbuf;
+ lwr_receive_hasc = receive_hasc;
+ lwr_receive_ungetc = receive_ungetc;
+ }
+else
+ {
+ DEBUG(D_receive) debug_printf("chunking double-push receive functions\n");
+ }
+
+receive_getc = bdat_getc;
+receive_getbuf = bdat_getbuf;
+receive_hasc = bdat_hasc;
+receive_ungetc = bdat_ungetc;
+}
+
+static inline void
+bdat_pop_receive_functions(void)
+{
+if (!lwr_receive_getc)
+ {
+ DEBUG(D_receive) debug_printf("chunking double-pop receive functions\n");
+ return;
+ }
+receive_getc = lwr_receive_getc;
+receive_getbuf = lwr_receive_getbuf;
+receive_hasc = lwr_receive_hasc;
+receive_ungetc = lwr_receive_ungetc;
+
+lwr_receive_getc = NULL;
+lwr_receive_getbuf = NULL;
+lwr_receive_hasc = NULL;
+lwr_receive_ungetc = NULL;
+}
+
+int
+bdat_ungetc(int ch)
+{
+chunking_data_left++;
+bdat_push_receive_functions(); /* we're not done yet, calling push is safe, because it checks the state before pushing anything */
+return lwr_receive_ungetc(ch);
+}
+
+
+
+/******************************************************************************/
+
+/*************************************************
+* 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().
+
+This function is exposed to the local_scan API; do not change the signature.
+
+Arguments:
+ format format string
+ more further data expected
+ ... optional arguments
+
+Returns: nothing
+*/
+
+void
+smtp_printf(const char *format, BOOL more, ...)
+{
+va_list ap;
+
+va_start(ap, more);
+smtp_vprintf(format, more, 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.
+
+This function is exposed to the local_scan API; do not change the signature.
+*/
+/*XXX consider passing caller-info in, for string_vformat-onward */
+
+void
+smtp_vprintf(const char *format, BOOL more, va_list ap)
+{
+gstring gs = { .size = big_buffer_size, .ptr = 0, .s = big_buffer };
+BOOL yield;
+
+/* Use taint-unchecked routines for writing into big_buffer, trusting
+that we'll never expand it. */
+
+yield = !! string_vformat(&gs, SVFMT_TAINT_NOCHK, format, ap);
+string_from_gstring(&gs);
+
+DEBUG(D_receive) for (const uschar * t, * s = gs.s;
+ s && (t = Ustrchr(s, '\r'));
+ s = t + 2) /* \r\n */
+ debug_printf("%s %.*s\n",
+ s == gs.s ? "SMTP>>" : " ",
+ (int)(t - s), s);
+
+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 (fl.rcpt_in_progress)
+ {
+ if (!rcpt_smtp_response)
+ rcpt_smtp_response = string_copy(big_buffer);
+ else if (fl.rcpt_smtp_response_same &&
+ Ustrcmp(rcpt_smtp_response, big_buffer) != 0)
+ fl.rcpt_smtp_response_same = FALSE;
+ fl.rcpt_in_progress = FALSE;
+ }
+
+/* Now write the string */
+
+if (
+#ifndef DISABLE_TLS
+ tls_in.active.sock >= 0 ? (tls_write(NULL, gs.s, gs.ptr, more) < 0) :
+#endif
+ (fwrite(gs.s, gs.ptr, 1, smtp_out) == 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().
+It flushes the output and checks for errors.
+
+Arguments: none
+Returns: 0 for no error; -1 after an error
+*/
+
+int
+smtp_fflush(void)
+{
+if (tls_in.active.sock < 0 && fflush(smtp_out) != 0) smtp_write_error = -1;
+
+if (
+#ifndef DISABLE_TLS
+ tls_in.active.sock >= 0 ? (tls_write(NULL, NULL, 0, FALSE) < 0) :
+#endif
+ (fflush(smtp_out) != 0)
+ )
+ smtp_write_error = -1;
+
+return smtp_write_error;
+}
+
+
+
+/* If there's input waiting (and we're doing pipelineing) then we can pipeline
+a reponse with the one following. */
+
+static BOOL
+pipeline_response(void)
+{
+if ( !smtp_enforce_sync || !sender_host_address
+ || f.sender_host_notsocket || !f.smtp_in_pipelining_advertised)
+ return FALSE;
+
+if (wouldblock_reading()) return FALSE;
+f.smtp_in_pipelining_used = TRUE;
+return TRUE;
+}
+
+
+#ifndef DISABLE_PIPE_CONNECT
+static BOOL
+pipeline_connect_sends(void)
+{
+if (!sender_host_address || f.sender_host_notsocket || !fl.pipe_connect_acceptable)
+ return FALSE;
+
+if (wouldblock_reading()) return FALSE;
+f.smtp_in_early_pipe_used = TRUE;
+return TRUE;
+}
+#endif
+
+/*************************************************
+* 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)
+{
+had_command_timeout = sig;
+}
+
+
+
+/*************************************************
+* 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)
+{
+had_command_sigterm = sig;
+}
+
+
+
+
+#ifdef SUPPORT_PROXY
+/*************************************************
+* 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. A local
+connection cannot.
+
+Arguments: none
+Returns: bool
+*/
+
+static BOOL
+check_proxy_protocol_host()
+{
+int rc;
+
+if ( sender_host_address
+ && (rc = verify_check_this_host(CUSS &hosts_proxy, NULL, NULL,
+ sender_host_address, NULL)) == OK)
+ {
+ DEBUG(D_receive)
+ debug_printf("Detected proxy protocol configured host\n");
+ proxy_session = TRUE;
+ }
+return proxy_session;
+}
+
+
+/*************************************************
+* Read data until newline or end of buffer *
+*************************************************/
+/* While SMTP is server-speaks-first, TLS is client-speaks-first, so we can't
+read an entire buffer and assume there will be nothing past a proxy protocol
+header. Our approach normally is to use stdio, but again that relies upon
+"STARTTLS\r\n" and a server response before the client starts TLS handshake, or
+reading _nothing_ before client TLS handshake. So we don't want to use the
+usual buffering reads which may read enough to block TLS starting.
+
+So unfortunately we're down to "read one byte at a time, with a syscall each,
+and expect a little overhead", for all proxy-opened connections which are v1,
+just to handle the TLS-on-connect case. Since SSL functions wrap the
+underlying fd, we can't assume that we can feed them any already-read content.
+
+We need to know where to read to, the max capacity, and we'll read until we
+get a CR and one more character. Let the caller scream if it's CR+!LF.
+
+Return the amount read.
+*/
+
+static int
+swallow_until_crlf(int fd, uschar *base, int already, int capacity)
+{
+uschar *to = base + already;
+uschar *cr;
+int have = 0;
+int ret;
+int last = 0;
+
+/* For "PROXY UNKNOWN\r\n" we, at time of writing, expect to have read
+up through the \r; for the _normal_ case, we haven't yet seen the \r. */
+
+cr = memchr(base, '\r', already);
+if (cr != NULL)
+ {
+ if ((cr - base) < already - 1)
+ {
+ /* \r and presumed \n already within what we have; probably not
+ actually proxy protocol, but abort cleanly. */
+ return 0;
+ }
+ /* \r is last character read, just need one more. */
+ last = 1;
+ }
+
+while (capacity > 0)
+ {
+ do { ret = read(fd, to, 1); } while (ret == -1 && errno == EINTR && !had_command_timeout);
+ if (ret == -1)
+ return -1;
+ have++;
+ if (last)
+ return have;
+ if (*to == '\r')
+ last = 1;
+ capacity--;
+ to++;
+ }
+
+/* reached end without having room for a final newline, abort */
+errno = EOVERFLOW;
+return -1;
+}
+
+/*************************************************
+* 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: Boolean success
+*/
+
+static void
+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;
+
+/* We can't read "all data until end" because while SMTP is
+server-speaks-first, the TLS handshake is client-speaks-first, so for
+TLS-on-connect ports the proxy protocol header will usually be immediately
+followed by a TLS handshake, and with N TLS libraries, we can't reliably
+reinject data for reading by those. So instead we first read "enough to be
+safely read within the header, and figure out how much more to read".
+For v1 we will later read to the end-of-line, for v2 we will read based upon
+the stated length.
+
+The v2 sig is 12 octets, and another 4 gets us the length, so we know how much
+data is needed total. For v1, where the line looks like:
+PROXY TCPn L3src L3dest SrcPort DestPort \r\n
+
+However, for v1 there's also `PROXY UNKNOWN\r\n` which is only 15 octets.
+We seem to support that. So, if we read 14 octets then we can tell if we're
+v2 or v1. If we're v1, we can continue reading as normal.
+
+If we're v2, we can't slurp up the entire header. We need the length in the
+15th & 16th octets, then to read everything after that.
+
+So to safely handle v1 and v2, with client-sent-first supported correctly,
+we have to do a minimum of 3 read calls, not 1. Eww.
+*/
+
+#define PROXY_INITIAL_READ 14
+#define PROXY_V2_HEADER_SIZE 16
+#if PROXY_INITIAL_READ > PROXY_V2_HEADER_SIZE
+# error Code bug in sizes of data to read for proxy usage
+#endif
+
+int get_ok = 0;
+int size, ret;
+int fd = fileno(smtp_in);
+const char v2sig[12] = "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A";
+uschar * iptype; /* To display debug info */
+socklen_t vslen = sizeof(struct timeval);
+BOOL yield = FALSE;
+
+os_non_restarting_signal(SIGALRM, command_timeout_handler);
+ALARM(proxy_protocol_timeout);
+
+do
+ {
+ /* The inbound host was declared to be a Proxy Protocol host, so
+ don't do a PEEK into the data, actually slurp up enough to be
+ "safe". Can't take it all because TLS-on-connect clients follow
+ immediately with TLS handshake. */
+ ret = read(fd, &hdr, PROXY_INITIAL_READ);
+ }
+ while (ret == -1 && errno == EINTR && !had_command_timeout);
+
+if (ret == -1)
+ goto proxyfail;
+
+/* For v2, handle reading the length, and then the rest. */
+if ((ret == PROXY_INITIAL_READ) && (memcmp(&hdr.v2, v2sig, sizeof(v2sig)) == 0))
+ {
+ int retmore;
+ uint8_t ver;
+
+ /* First get the length fields. */
+ do
+ {
+ retmore = read(fd, (uschar*)&hdr + ret, PROXY_V2_HEADER_SIZE - PROXY_INITIAL_READ);
+ } while (retmore == -1 && errno == EINTR && !had_command_timeout);
+ if (retmore == -1)
+ goto proxyfail;
+ ret += retmore;
+
+ ver = (hdr.v2.ver_cmd & 0xf0) >> 4;
+
+ /* 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 separate values here. */
+
+ if (ver != 0x02)
+ {
+ DEBUG(D_receive) debug_printf("Invalid Proxy Protocol version: %d\n", ver);
+ goto proxyfail;
+ }
+
+ /* The v2 header will always be 16 bytes per the spec. */
+ size = 16 + ntohs(hdr.v2.len);
+ DEBUG(D_receive) debug_printf("Detected PROXYv2 header, size %d (limit %d)\n",
+ size, (int)sizeof(hdr));
+
+ /* We should now have 16 octets (PROXY_V2_HEADER_SIZE), and we know the total
+ amount that we need. Double-check that the size is not unreasonable, then
+ get the rest. */
+ if (size > sizeof(hdr))
+ {
+ DEBUG(D_receive) debug_printf("PROXYv2 header size unreasonably large; security attack?\n");
+ goto proxyfail;
+ }
+
+ do
+ {
+ do
+ {
+ retmore = read(fd, (uschar*)&hdr + ret, size-ret);
+ } while (retmore == -1 && errno == EINTR && !had_command_timeout);
+ if (retmore == -1)
+ goto proxyfail;
+ ret += retmore;
+ DEBUG(D_receive) debug_printf("PROXYv2: have %d/%d required octets\n", ret, size);
+ } while (ret < size);
+
+ } /* end scope for getting rest of data for v2 */
+
+/* At this point: if PROXYv2, we've read the exact size required for all data;
+if PROXYv1 then we've read "less than required for any valid line" and should
+read the rest". */
+
+if (ret >= 16 && memcmp(&hdr.v2, v2sig, 12) == 0)
+ {
+ uint8_t cmd = (hdr.v2.ver_cmd & 0x0f);
+
+ 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, CS &tmpip, sizeof(tmpip));
+ if (!string_is_ip_address(US tmpip, NULL))
+ {
+ DEBUG(D_receive) debug_printf("Invalid %s source IP\n", iptype);
+ goto proxyfail;
+ }
+ proxy_local_address = sender_host_address;
+ sender_host_address = string_copy(US tmpip);
+ tmpport = ntohs(hdr.v2.addr.ip4.src_port);
+ proxy_local_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, CS &tmpip, sizeof(tmpip));
+ if (!string_is_ip_address(US tmpip, NULL))
+ {
+ DEBUG(D_receive) debug_printf("Invalid %s dest port\n", iptype);
+ goto proxyfail;
+ }
+ proxy_external_address = string_copy(US tmpip);
+ tmpport = ntohs(hdr.v2.addr.ip4.dst_port);
+ proxy_external_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, CS &tmpip6, sizeof(tmpip6));
+ if (!string_is_ip_address(US tmpip6, NULL))
+ {
+ DEBUG(D_receive) debug_printf("Invalid %s source IP\n", iptype);
+ goto proxyfail;
+ }
+ proxy_local_address = sender_host_address;
+ sender_host_address = string_copy(US tmpip6);
+ tmpport = ntohs(hdr.v2.addr.ip6.src_port);
+ proxy_local_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, CS &tmpip6, sizeof(tmpip6));
+ if (!string_is_ip_address(US tmpip6, NULL))
+ {
+ DEBUG(D_receive) debug_printf("Invalid %s dest port\n", iptype);
+ goto proxyfail;
+ }
+ proxy_external_address = string_copy(US tmpip6);
+ tmpport = ntohs(hdr.v2.addr.ip6.dst_port);
+ proxy_external_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 */
+ iptype = US"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;
+ uschar *end;
+ uschar *sp; /* Utility variables follow */
+ int tmp_port;
+ int r2;
+ char *endc;
+
+ /* get the rest of the line */
+ r2 = swallow_until_crlf(fd, (uschar*)&hdr, ret, sizeof(hdr)-ret);
+ if (r2 == -1)
+ goto proxyfail;
+ ret += r2;
+
+ p = string_copy(hdr.v1.line);
+ end = memchr(p, '\r', ret - 1);
+
+ if (!end || (end == (uschar*)&hdr + ret) || end[1] != '\n')
+ {
+ DEBUG(D_receive) debug_printf("Partial or invalid PROXY header\n");
+ goto proxyfail;
+ }
+ *end = '\0'; /* Terminate the string */
+ size = end + 2 - p; /* Skip header + CRLF */
+ DEBUG(D_receive) debug_printf("Detected PROXYv1 header\n");
+ DEBUG(D_receive) debug_printf("Bytes read not within PROXY header: %d\n", ret - size);
+ /* Step through the string looking for the required fields. Ensure
+ strict adherence 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_local_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_external_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_local_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_external_port = tmp_port;
+ /* Already checked for /r /n above. Good V1 header received. */
+ }
+else
+ {
+ /* Wrong protocol */
+ DEBUG(D_receive) debug_printf("Invalid proxy protocol version negotiation\n");
+ (void) swallow_until_crlf(fd, (uschar*)&hdr, ret, sizeof(hdr)-ret);
+ goto proxyfail;
+ }
+
+done:
+ DEBUG(D_receive)
+ debug_printf("Valid %s sender from Proxy Protocol header\n", iptype);
+ yield = proxy_session;
+
+/* Don't flush any potential buffer contents. Any input on proxyfail
+should cause a synchronization failure */
+
+proxyfail:
+ DEBUG(D_receive) if (had_command_timeout)
+ debug_printf("Timeout while reading proxy header\n");
+
+bad:
+ if (yield)
+ {
+ sender_host_name = NULL;
+ (void) host_name_lookup();
+ host_build_sender_fullhost();
+ }
+ else
+ {
+ f.proxy_session_failed = TRUE;
+ DEBUG(D_receive)
+ debug_printf("Failure to extract proxied host, only QUIT allowed\n");
+ }
+
+ALARM(0);
+return;
+}
+#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
+ buffer_lim maximum to buffer in lower layer
+
+Returns: a code identifying the command (enumerated above)
+*/
+
+static int
+smtp_read_command(BOOL check_sync, unsigned buffer_lim)
+{
+int c;
+int ptr = 0;
+BOOL hadnull = FALSE;
+
+had_command_timeout = 0;
+os_non_restarting_signal(SIGALRM, command_timeout_handler);
+
+while ((c = (receive_getc)(buffer_lim)) != '\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 (smtp_cmd_list * p = cmd_list; p < cmd_list_end; p++)
+ {
+#ifdef SUPPORT_PROXY
+ /* Only allow QUIT command if Proxy Protocol parsing failed */
+ if (proxy_session && f.proxy_session_failed && p->cmd != QUIT_CMD)
+ continue;
+#endif
+ if ( p->len
+ && 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 */
+ && !f.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 SUPPORT_PROXY
+/* Only allow QUIT command if Proxy Protocol parsing failed */
+if (proxy_session && f.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 /* Not local input */
+ && !f.sender_host_notsocket /* Really is a socket */
+ )
+ return BADSYN_CMD;
+
+return OTHER_CMD;
+}
+
+
+
+/*************************************************
+* 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 || smtp_batched_input) return;
+receive_swallow_smtp();
+smtp_printf("421 %s\r\n", FALSE, message);
+
+for (;;) switch(smtp_read_command(FALSE, GETC_BUFFER_UNLIMITED))
+ {
+ case EOF_CMD:
+ return;
+
+ case QUIT_CMD:
+ f.smtp_in_quit = TRUE;
+ smtp_printf("221 %s closing connection\r\n", FALSE, smtp_active_hostname);
+ mac_smtp_fflush();
+ return;
+
+ case RSET_CMD:
+ smtp_printf("250 Reset OK\r\n", FALSE);
+ break;
+
+ default:
+ smtp_printf("421 %s\r\n", FALSE, 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)
+{
+const uschar * hostname = sender_fullhost
+ ? sender_fullhost : sender_host_address;
+
+if (host_checking)
+ return string_sprintf("SMTP connection from %s", hostname);
+
+if (f.sender_host_unknown || f.sender_host_notsocket)
+ return string_sprintf("SMTP connection from %s", sender_ident);
+
+if (f.is_inetd)
+ return string_sprintf("SMTP connection from %s (via inetd)", hostname);
+
+if (LOGGING(incoming_interface) && interface_address)
+ return string_sprintf("SMTP connection from %s I=[%s]:%d", hostname,
+ interface_address, interface_port);
+
+return string_sprintf("SMTP connection from %s", hostname);
+}
+
+
+
+#ifndef DISABLE_TLS
+/* Append TLS-related information to a log line
+
+Arguments:
+ g String under construction: allocated string to extend, or NULL
+
+Returns: Allocated string or NULL
+*/
+static gstring *
+s_tlslog(gstring * g)
+{
+if (LOGGING(tls_cipher) && tls_in.cipher)
+ {
+ g = string_append(g, 2, US" X=", tls_in.cipher);
+#ifndef DISABLE_TLS_RESUME
+ if (LOGGING(tls_resumption) && tls_in.resumption & RESUME_USED)
+ g = string_catn(g, US"*", 1);
+#endif
+ }
+if (LOGGING(tls_certificate_verified) && tls_in.cipher)
+ g = string_append(g, 2, US" CV=", tls_in.certificate_verified? "yes":"no");
+if (LOGGING(tls_peerdn) && tls_in.peerdn)
+ g = string_append(g, 3, US" DN=\"", string_printing(tls_in.peerdn), US"\"");
+if (LOGGING(tls_sni) && tls_in.sni)
+ g = string_append(g, 2, US" SNI=", string_printing2(tls_in.sni, SP_TAB|SP_SPACE));
+return g;
+}
+#endif
+
+
+
+static gstring *
+s_connhad_log(gstring * g)
+{
+const uschar * sep = smtp_connection_had[SMTP_HBUFF_SIZE-1] != SCH_NONE
+ ? US" C=..." : US" C=";
+
+for (int i = smtp_ch_index; i < SMTP_HBUFF_SIZE; i++)
+ if (smtp_connection_had[i] != SCH_NONE)
+ {
+ g = string_append(g, 2, sep, smtp_names[smtp_connection_had[i]]);
+ sep = US",";
+ }
+for (int i = 0; i < smtp_ch_index; i++, sep = US",")
+ g = string_append(g, 2, sep, smtp_names[smtp_connection_had[i]]);
+return g;
+}
+
+
+/*************************************************
+* 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)
+{
+uschar * s;
+gstring * g = NULL;
+
+if (smtp_mailcmd_count > 0 || !LOGGING(smtp_no_mail))
+ return;
+
+if (sender_host_authenticated)
+ {
+ g = string_append(g, 2, US" A=", sender_host_authenticated);
+ if (authenticated_id) g = string_append(g, 2, US":", authenticated_id);
+ }
+
+#ifndef DISABLE_TLS
+g = s_tlslog(g);
+#endif
+
+g = s_connhad_log(g);
+
+if (!(s = string_from_gstring(g))) s = US"";
+
+log_write(0, LOG_MAIN, "no MAIL in %sSMTP connection from %s D=%s%s",
+ f.tcp_in_fastopen ? f.tcp_in_fastopen_data ? US"TFO* " : US"TFO " : US"",
+ host_and_ident(FALSE), string_timesince(&smtp_connection_start), s);
+}
+
+
+/* Return list of recent smtp commands */
+
+uschar *
+smtp_cmd_hist(void)
+{
+gstring * list = NULL;
+uschar * s;
+
+for (int i = smtp_ch_index; i < SMTP_HBUFF_SIZE; i++)
+ if (smtp_connection_had[i] != SCH_NONE)
+ list = string_append_listele(list, ',', smtp_names[smtp_connection_had[i]]);
+
+for (int i = 0; i < smtp_ch_index; i++)
+ list = string_append_listele(list, ',', smtp_names[smtp_connection_had[i]]);
+
+s = string_from_gstring(list);
+return s ? s : US"";
+}
+
+
+
+
+/*************************************************
+* 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
+argument 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 = fl.helo_accept_junk;
+
+/* Discard any previous 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)
+ for (yield = TRUE; *s; s++)
+ if (!isalnum(*s) && *s != '.' && *s != '-' &&
+ Ustrchr(helo_allow_chars, *s) == NULL)
+ {
+ yield = FALSE;
+ break;
+ }
+
+/* Save argument if OK */
+
+if (yield) sender_helo_name = string_copy_perm(start, TRUE);
+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;
+if (Ustrlen(smtp_cmd_data) <= 0) return FALSE;
+v = smtp_cmd_data + Ustrlen(smtp_cmd_data) - 1;
+while (v > smtp_cmd_data && isspace(*v)) v--;
+v[1] = 0;
+
+while (v > smtp_cmd_data && *v != '=' && !isspace(*v))
+ {
+ /* Take care to not stop at a space embedded in a quoted local-part */
+ if (*v == '"')
+ {
+ do v--; while (v > smtp_cmd_data && *v != '"');
+ if (v <= smtp_cmd_data) return FALSE;
+ }
+ v--;
+ }
+if (v <= smtp_cmd_data) return FALSE;
+
+n = v;
+if (*v == '=')
+ {
+ while (n > smtp_cmd_data && isalpha(n[-1])) n--;
+ /* RFC says SP, but TAB seen in wild and other major MTAs accept it */
+ if (n <= smtp_cmd_data || !isspace(n[-1])) return FALSE;
+ n[-1] = 0;
+ }
+else
+ {
+ n++;
+ }
+*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; also from the daemon loop.
+
+Argument: the stacking pool storage reset point
+Returns: nothing
+*/
+
+void *
+smtp_reset(void *reset_point)
+{
+recipients_list = NULL;
+rcpt_count = rcpt_defer_count = rcpt_fail_count =
+ raw_recipients_count = recipients_count = recipients_list_max = 0;
+message_linecount = 0;
+message_size = -1;
+message_body = message_body_end = NULL;
+acl_added_headers = NULL;
+acl_removed_headers = NULL;
+f.queue_only_policy = FALSE;
+rcpt_smtp_response = NULL;
+fl.rcpt_smtp_response_same = TRUE;
+fl.rcpt_in_progress = FALSE;
+f.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
+f.no_mbox_unspool = FALSE; /* Can be set by ACL */
+#endif
+f.submission_mode = FALSE; /* Can be set by ACL */
+f.suppress_local_fixups = f.suppress_local_fixups_default; /* Can be set by ACL */
+f.active_local_from_check = local_from_check; /* Can be set by ACL */
+f.active_local_sender_retain = local_sender_retain; /* Can be set by ACL */
+sending_ip_address = NULL;
+return_path = sender_address = NULL;
+deliver_localpart_data = deliver_domain_data =
+recipient_data = sender_data = NULL; /* Can be set by ACL */
+recipient_verify_failure = NULL;
+deliver_localpart_parent = deliver_localpart_orig = NULL;
+deliver_domain_parent = deliver_domain_orig = NULL;
+callout_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));
+
+authenticated_sender = NULL;
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+bmi_run = 0;
+bmi_verdicts = NULL;
+#endif
+dnslist_domain = dnslist_matched = NULL;
+#ifdef SUPPORT_SPF
+spf_header_comment = spf_received = spf_result = spf_smtp_comment = NULL;
+spf_result_guessed = FALSE;
+#endif
+#ifndef DISABLE_DKIM
+dkim_cur_signer = dkim_signers =
+dkim_signing_domain = dkim_signing_selector = dkim_signatures = NULL;
+dkim_cur_signer = dkim_signers = dkim_signing_domain = dkim_signing_selector = NULL;
+f.dkim_disable_verify = FALSE;
+dkim_collect_input = 0;
+dkim_verify_overall = dkim_verify_status = dkim_verify_reason = NULL;
+dkim_key_length = 0;
+#endif
+#ifdef SUPPORT_DMARC
+f.dmarc_has_been_checked = f.dmarc_disable_verify = f.dmarc_enable_forensic = FALSE;
+dmarc_domain_policy = dmarc_status = dmarc_status_text =
+dmarc_used_domain = NULL;
+#endif
+#ifdef EXPERIMENTAL_ARC
+arc_state = arc_state_reason = NULL;
+arc_received_instance = 0;
+#endif
+dsn_ret = 0;
+dsn_envid = NULL;
+deliver_host = deliver_host_address = NULL; /* Can be set by ACL */
+#ifndef DISABLE_PRDR
+prdr_requested = FALSE;
+#endif
+#ifdef SUPPORT_I18N
+message_smtputf8 = FALSE;
+#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;
+
+/* Warning log messages are 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)
+ {
+ string_item *this = acl_warn_logged;
+ acl_warn_logged = acl_warn_logged->next;
+ store_free(this);
+ }
+
+message_tidyup();
+store_reset(reset_point);
+
+message_start();
+return store_mark();
+}
+
+
+
+
+
+/*************************************************
+* 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;
+rmark reset_point = store_mark();
+
+/* 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 */
+
+cancel_cutthrough_connection(TRUE, US"smtp_setup_batch_msg");
+reset_point = 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, GETC_BUFFER_UNLIMITED))
+ {
+ /* 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:
+ cancel_cutthrough_connection(TRUE, US"RSET received");
+ reset_point = 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:
+ smtp_mailcmd_count++; /* Count for no-mail log */
+ if (sender_address)
+ /* 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 */
+
+ cancel_cutthrough_connection(TRUE, US"MAIL received");
+ reset_point = smtp_reset(reset_point);
+
+ /* Apply SMTP rewrite */
+
+ raw_sender = rewrite_existflags & rewrite_smtp
+ /* deconst ok as smtp_cmd_data was not const */
+ ? US 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)
+ /* 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
+ && sender_address[0] != 0 && sender_address[0] != '@')
+ if (f.allow_unqualified_sender)
+ {
+ /* deconst ok as sender_address was not const */
+ sender_address = US 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)
+ /* 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
+ /* deconst ok as smtp_cmd_data was not const */
+ ? US rewrite_one(smtp_cmd_data, rewrite_smtp, NULL, FALSE, US"",
+ global_rewrite_rules)
+ : smtp_cmd_data;
+
+ recipient = parse_extract_address(recipient, &errmess, &start, &end,
+ &recipient_domain, FALSE);
+
+ if (!recipient)
+ /* 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)
+ if (f.allow_unqualified_recipient)
+ {
+ DEBUG(D_receive) debug_printf("unqualified address %s accepted\n",
+ recipient);
+ /* deconst ok as recipient was not const */
+ recipient = US 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 || recipients_count <= 0)
+ /* The function moan_smtp_batch() does not return. */
+ if (!sender_address)
+ 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 QUIT_CMD:
+ f.smtp_in_quit = TRUE;
+ case EOF_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 */
+}
+
+
+
+
+#ifndef DISABLE_TLS
+static BOOL
+smtp_log_tls_fail(const uschar * errstr)
+{
+const uschar * conn_info = smtp_get_connection_info();
+
+if (Ustrncmp(conn_info, US"SMTP ", 5) == 0) conn_info += 5;
+/* I'd like to get separated H= here, but too hard for now */
+
+log_write(0, LOG_MAIN, "TLS error on %s %s", conn_info, errstr);
+return FALSE;
+}
+#endif
+
+
+
+
+#ifdef TCP_FASTOPEN
+static void
+tfo_in_check(void)
+{
+# ifdef __FreeBSD__
+int is_fastopen;
+socklen_t len = sizeof(is_fastopen);
+
+/* The tinfo TCPOPT_FAST_OPEN bit seems unreliable, and we don't see state
+TCP_SYN_RCV (as of 12.1) so no idea about data-use. */
+
+if (getsockopt(fileno(smtp_out), IPPROTO_TCP, TCP_FASTOPEN, &is_fastopen, &len) == 0)
+ {
+ if (is_fastopen)
+ {
+ DEBUG(D_receive)
+ debug_printf("TFO mode connection (TCP_FASTOPEN getsockopt)\n");
+ f.tcp_in_fastopen = TRUE;
+ }
+ }
+else DEBUG(D_receive)
+ debug_printf("TCP_INFO getsockopt: %s\n", strerror(errno));
+
+# elif defined(TCP_INFO)
+struct tcp_info tinfo;
+socklen_t len = sizeof(tinfo);
+
+if (getsockopt(fileno(smtp_out), IPPROTO_TCP, TCP_INFO, &tinfo, &len) == 0)
+# ifdef TCPI_OPT_SYN_DATA /* FreeBSD 11,12 do not seem to have this yet */
+ if (tinfo.tcpi_options & TCPI_OPT_SYN_DATA)
+ {
+ DEBUG(D_receive)
+ debug_printf("TFO mode connection (ACKd data-on-SYN)\n");
+ f.tcp_in_fastopen_data = f.tcp_in_fastopen = TRUE;
+ }
+ else
+# endif
+ if (tinfo.tcpi_state == TCP_SYN_RECV) /* Not seen on FreeBSD 12.1 */
+ {
+ DEBUG(D_receive)
+ debug_printf("TFO mode connection (state TCP_SYN_RECV)\n");
+ f.tcp_in_fastopen = TRUE;
+ }
+else DEBUG(D_receive)
+ debug_printf("TCP_INFO getsockopt: %s\n", strerror(errno));
+# endif
+}
+#endif
+
+
+/*************************************************
+* 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 esclen;
+uschar *user_msg, *log_msg;
+uschar *code, *esc;
+uschar *p, *s;
+gstring * ss;
+
+gettimeofday(&smtp_connection_start, 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 */
+
+fl.helo_seen = fl.esmtp = fl.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;
+fl.auth_advertised = FALSE;
+f.smtp_in_pipelining_advertised = f.smtp_in_pipelining_used = FALSE;
+f.pipelining_enable = TRUE;
+sync_cmd_limit = NON_SYNC_CMD_NON_PIPELINING;
+fl.smtp_exit_function_called = FALSE; /* For avoiding loop in not-quit exit */
+
+/* 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 && !f.sender_host_notsocket)
+ sender_host_auth_pubname = sender_host_authenticated = NULL;
+authenticated_by = NULL;
+
+#ifndef DISABLE_TLS
+tls_in.ver = tls_in.cipher = tls_in.peerdn = NULL;
+tls_in.ourcert = tls_in.peercert = NULL;
+tls_in.sni = NULL;
+tls_in.ocsp = OCSP_NOT_REQ;
+fl.tls_advertised = FALSE;
+#endif
+fl.dsn_advertised = FALSE;
+#ifdef SUPPORT_I18N
+fl.smtputf8_advertised = FALSE;
+#endif
+
+/* Reset ACL connection variables */
+
+acl_var_c = NULL;
+
+/* Allow for trailing 0 in the command and data buffers. Tainted. */
+
+smtp_cmd_buffer = store_get_perm(2*SMTP_CMD_BUFFER_SIZE + 2, GET_TAINTED);
+
+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) 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 =
+ (sender_host_address ? protocols : protocols_local) [pnormal];
+
+/* Set up the buffer for inputting using direct read() calls, and arrange to
+call the local functions instead of the standard C ones. */
+
+smtp_buf_init();
+
+receive_getc = smtp_getc;
+receive_getbuf = smtp_getbuf;
+receive_get_cache = smtp_get_cache;
+receive_hasc = smtp_hasc;
+receive_ungetc = smtp_ungetc;
+receive_feof = smtp_feof;
+receive_ferror = smtp_ferror;
+lwr_receive_getc = NULL;
+lwr_receive_getbuf = NULL;
+lwr_receive_hasc = NULL;
+lwr_receive_ungetc = NULL;
+
+/* 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)
+ {
+ 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 (!f.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 && !f.sender_host_notsocket)
+ {
+ #if OPTSTYLE == 1
+ EXIM_SOCKLEN_T optlen = sizeof(struct ip_options) + MAX_IPOPTLEN;
+ struct ip_options *ipopt = store_get(optlen, GET_UNTAINTED);
+ #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, US (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", FALSE);
+ 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 *adptr;
+ int optcount;
+ struct in_addr addr;
+
+ #if OPTSTYLE == 1
+ uschar *optstart = US (ipopt->__data);
+ #elif OPTSTYLE == 2
+ uschar *optstart = US (ipopt->ip_opts);
+ #else
+ uschar *optstart = US (ipopt->ipopt_list);
+ #endif
+
+ DEBUG(D_receive) debug_printf("IP options exist\n");
+
+ Ustrcpy(p, "IP options on incoming call:");
+ p += Ustrlen(p);
+
+ for (uschar * opt = optstart; opt && opt < US (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:
+ {
+ if (pend - p < 4 + 3*opt[1]) { opt = NULL; break; }
+ Ustrcat(p, "[ ");
+ p += 2;
+ for (int i = 0; i < opt[1]; i++)
+ p += sprintf(CS p, "%2.2x ", opt[i]);
+ *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", FALSE);
+ 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 && !f.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));
+
+ /* Expand smtp_receive_timeout, if needed */
+
+ if (smtp_receive_timeout_s)
+ {
+ uschar * exp;
+ if ( !(exp = expand_string(smtp_receive_timeout_s))
+ || !(*exp)
+ || (smtp_receive_timeout = readconf_readtime(exp, 0, FALSE)) < 0
+ )
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "bad value for smtp_receive_timeout: '%s'", exp ? exp : US"");
+ }
+
+ /* 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", FALSE);
+ 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;
+ if (!(tcp_wrappers_name = expand_string(tcp_wrappers_daemon_name)))
+ 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 ? CS sender_host_name : STRING_UNKNOWN,
+ sender_host_address ? CS sender_host_address : STRING_UNKNOWN,
+ sender_ident ? CS sender_ident : STRING_UNKNOWN))
+ {
+ 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", FALSE);
+ }
+ 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", FALSE);
+ }
+ 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", FALSE, 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", FALSE,
+ 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. */
+
+ f.allow_unqualified_sender =
+ verify_check_host(&sender_unqualified_hosts) == OK;
+
+ f.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. */
+
+ fl.helo_verify_required = verify_check_host(&helo_verify_hosts) == OK;
+ if (!fl.helo_verify_required)
+ fl.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. */
+
+ fl.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;
+
+/* If valid Proxy Protocol source is connecting, set up session.
+Failure will not allow any SMTP function other than QUIT. */
+
+#ifdef SUPPORT_PROXY
+proxy_session = FALSE;
+f.proxy_session_failed = FALSE;
+if (check_proxy_protocol_host())
+ setup_proxy_protocol_host();
+#endif
+
+/* Start up TLS if tls_on_connect is set. This is for supporting the legacy
+smtps port for use with older style SSL MTAs. */
+
+#ifndef DISABLE_TLS
+if (tls_in.on_connect)
+ {
+ if (tls_server_start(&user_msg) != OK)
+ return smtp_log_tls_fail(user_msg);
+ cmd_list[CMD_LIST_TLS_AUTH].is_mail_cmd = TRUE;
+ }
+#endif
+
+/* Run the connect ACL if it exists */
+
+user_msg = NULL;
+if (acl_smtp_connect)
+ {
+ int rc;
+ if ((rc = acl_check(ACL_WHERE_CONNECT, NULL, acl_smtp_connect, &user_msg,
+ &log_msg)) != 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)
+ {
+ if (!(s = expand_string(smtp_banner)))
+ 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, TRUE);
+ 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--;
+s = string_copyn(s, p-s);
+
+/* 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 = string_get(256);
+
+p = s;
+do /* At least once, in case we have an empty string */
+ {
+ int len;
+ uschar *linebreak = Ustrchr(p, '\n');
+ ss = string_catn(ss, code, 3);
+ if (!linebreak)
+ {
+ len = Ustrlen(p);
+ ss = string_catn(ss, US" ", 1);
+ }
+ else
+ {
+ len = linebreak - p;
+ ss = string_catn(ss, US"-", 1);
+ }
+ ss = string_catn(ss, esc, esclen);
+ ss = string_catn(ss, p, len);
+ ss = string_catn(ss, US"\r\n", 2);
+ p += len;
+ if (linebreak) p++;
+ }
+while (*p);
+
+/* Before we write the banner, check that there is no input pending, unless
+this synchronisation check is disabled. */
+
+#ifndef DISABLE_PIPE_CONNECT
+fl.pipe_connect_acceptable =
+ sender_host_address && verify_check_host(&pipe_connect_advertise_hosts) == OK;
+
+if (!check_sync())
+ if (fl.pipe_connect_acceptable)
+ f.smtp_in_early_pipe_used = TRUE;
+ else
+#else
+if (!check_sync())
+#endif
+ {
+ unsigned n = smtp_inend - smtp_inptr;
+ if (n > 128) n = 128;
+
+ 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(string_copyn(smtp_inptr, n)));
+ smtp_printf("554 SMTP synchronization error\r\n", FALSE);
+ return FALSE;
+ }
+
+/* Now output the banner */
+/*XXX the ehlo-resp code does its own tls/nontls bit. Maybe subroutine that? */
+
+smtp_printf("%s",
+#ifndef DISABLE_PIPE_CONNECT
+ fl.pipe_connect_acceptable && pipeline_connect_sends(),
+#else
+ FALSE,
+#endif
+ string_from_gstring(ss));
+
+/* Attempt to see if we sent the banner before the last ACK of the 3-way
+handshake arrived. If so we must have managed a TFO. */
+
+#ifdef TCP_FASTOPEN
+if (sender_host_address && !f.sender_host_notsocket) tfo_in_check();
+#endif
+
+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\", %s)",
+ host_and_ident(FALSE), string_printing(smtp_cmd_buffer),
+ string_from_gstring(s_connhad_log(NULL))
+ );
+ }
+
+if (code > 0)
+ {
+ smtp_printf("%d%c%s%s%s\r\n", FALSE, code, yield == 1 ? '-' : ' ',
+ data ? data : US"", data ? US": " : US"", errmess);
+ if (yield == 1)
+ smtp_printf("%d Too many syntax or protocol errors\r\n", FALSE, code);
+ }
+
+return yield;
+}
+
+
+
+
+/*************************************************
+* 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 && f.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 (fl.rcpt_in_progress)
+ {
+ if (!rcpt_smtp_response)
+ rcpt_smtp_response = string_copy(msg);
+ else if (fl.rcpt_smtp_response_same &&
+ Ustrcmp(rcpt_smtp_response, msg) != 0)
+ fl.rcpt_smtp_response_same = FALSE;
+ fl.rcpt_in_progress = FALSE;
+ }
+
+/* Now output the message, splitting it up into multiple lines if necessary.
+We only handle pipelining these responses as far as nonfinal/final groups,
+not the whole MAIL/RCPT/DATA response set. */
+
+for (;;)
+ {
+ uschar *nl = Ustrchr(msg, '\n');
+ if (!nl)
+ {
+ smtp_printf("%.3s%c%.*s%s\r\n", !final, code, final ? ' ':'-', esclen, esc, msg);
+ return;
+ }
+ else if (nl[1] == 0 || f.no_multiline_responses)
+ {
+ smtp_printf("%.3s%c%.*s%.*s\r\n", !final, code, final ? ' ':'-', esclen, esc,
+ (int)(nl - msg), msg);
+ return;
+ }
+ else
+ {
+ smtp_printf("%.3s-%.*s%.*s\r\n", TRUE, code, esclen, esc, (int)(nl - msg), msg);
+ msg = nl + 1;
+ Uskip_whitespace(&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
+ check_valid if true, verify the response code
+
+Returns: nothing
+*/
+
+void
+smtp_message_code(uschar **code, int *codelen, uschar **msg, uschar **log_msg,
+ BOOL check_valid)
+{
+uschar * match;
+int len;
+
+if (!msg || !*msg || !regex_match(regex_smtp_code, *msg, -1, &match))
+ return;
+
+len = Ustrlen(match);
+if (check_valid && (*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 && *log_msg == *msg)
+ *log_msg = string_sprintf("%s %s", *code, *log_msg + len);
+ }
+else
+ {
+ *code = *msg;
+ *codelen = len; /* Includes final space */
+ }
+*msg += len; /* 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 sends 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. We return 252 if there is no VRFY ACL or it provides
+no explicit code, but if there is one we let it know best.
+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;
+
+if (drop) rc = FAIL;
+
+/* Set the default SMTP code, and allow a user message to change it. */
+
+smtp_code = rc == FAIL ? acl_wherecodes[where] : US"451";
+smtp_message_code(&smtp_code, &codelen, &user_msg, &log_msg,
+ where != ACL_WHERE_VRFY);
+
+/* 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. */
+
+switch (where)
+ {
+#ifdef WITH_CONTENT_SCAN
+ case ACL_WHERE_MIME: what = US"during MIME ACL checks"; break;
+#endif
+ case ACL_WHERE_PREDATA: what = US"DATA"; break;
+ case ACL_WHERE_DATA: what = US"after DATA"; break;
+#ifndef DISABLE_PRDR
+ case ACL_WHERE_PRDR: what = US"after DATA PRDR"; break;
+#endif
+ default:
+ {
+ uschar * place = smtp_cmd_data ? smtp_cmd_data : US"in \"connect\" ACL";
+ int lim = 100;
+
+ if (where == ACL_WHERE_AUTH) /* avoid logging auth creds */
+ {
+ uschar * s;
+ for (s = smtp_cmd_data; *s && !isspace(*s); ) s++;
+ lim = s - smtp_cmd_data; /* atop after method */
+ }
+ what = string_sprintf("%s %.*s", acl_wherenames[where], lim, place);
+ }
+ }
+switch (where)
+ {
+ case ACL_WHERE_RCPT:
+ case ACL_WHERE_DATA:
+#ifdef WITH_CONTENT_SCAN
+ case 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""
+ );
+ break;
+ }
+
+/* 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 &&
+ !testflag(sender_verified_failed, af_sverify_told))
+ {
+ BOOL save_rcpt_in_progress = fl.rcpt_in_progress;
+ fl.rcpt_in_progress = FALSE; /* So as not to treat these as the error */
+
+ setflag(sender_verified_failed, af_sverify_told);
+
+ if (rc != FAIL || LOGGING(sender_verify_fail))
+ 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)
+ 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));
+
+ fl.rcpt_in_progress = save_rcpt_in_progress;
+ }
+
+/* Sort out text for logging */
+
+log_msg = log_msg ? string_sprintf(": %s", log_msg) : US"";
+if ((lognl = Ustrchr(log_msg, '\n'))) *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 ? user_msg : US"Administrative prohibition");
+
+/* 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 (f.acl_temp_details && user_msg)
+ {
+ if ( smtp_return_error_details
+ && sender_verified_failed
+ && sender_verified_failed->message
+ )
+ 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)
+ {
+#ifndef DISABLE_TLS
+ gstring * g = s_tlslog(NULL);
+ uschar * tls = string_from_gstring(g);
+ if (!tls) tls = US"";
+#else
+ uschar * tls = US"";
+#endif
+ log_write(where == ACL_WHERE_CONNECT ? L_connection_reject : 0,
+ log_reject_target, "%s%s%s %s%srejected %s%s",
+ LOGGING(dnssec) && sender_host_dnssec ? US" DS" : US"",
+ host_and_ident(TRUE),
+ tls,
+ 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 function, 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 call */
+
+if (fl.smtp_exit_function_called)
+ {
+ log_write(0, LOG_PANIC, "smtp_notquit_exit() called more than once (%s)",
+ reason);
+ return;
+ }
+fl.smtp_exit_function_called = TRUE;
+
+/* Call the not-QUIT ACL, if there is one, unless no reason is given. */
+
+if (acl_smtp_notquit && reason)
+ {
+ smtp_notquit_reason = reason;
+ if ((rc = acl_check(ACL_WHERE_NOTQUIT, NULL, acl_smtp_notquit, &user_msg,
+ &log_msg)) == ERROR)
+ log_write(0, LOG_MAIN|LOG_PANIC, "ACL for not-QUIT returned ERROR: %s",
+ log_msg);
+ }
+
+/* If the connection was dropped, we certainly are no longer talking TLS */
+tls_in.active.sock = -1;
+
+/* Write an SMTP response if we are expected to give one. As the default
+responses are all internal, they should be reasonable size. */
+
+if (code && defaultrespond)
+ {
+ if (user_msg)
+ smtp_respond(code, 3, TRUE, user_msg);
+ else
+ {
+ gstring * g;
+ va_list ap;
+
+ va_start(ap, defaultrespond);
+ g = string_vformat(NULL, SVFMT_EXTEND|SVFMT_REBUFFER, CS defaultrespond, ap);
+ va_end(ap);
+ smtp_printf("%s %s\r\n", FALSE, code, string_from_gstring(g));
+ }
+ 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");
+ f.helo_verified = TRUE;
+ }
+
+/* Deal with the more common case when there is a sending IP address */
+
+else if (sender_helo_name[0] == '[')
+ {
+ f.helo_verified = Ustrncmp(sender_helo_name+1, sender_host_address,
+ Ustrlen(sender_host_address)) == 0;
+
+#if HAVE_IPV6
+ if (!f.helo_verified)
+ {
+ if (strncmpic(sender_host_address, US"::ffff:", 7) == 0)
+ f.helo_verified = Ustrncmp(sender_helo_name + 1,
+ sender_host_address + 7, Ustrlen(sender_host_address) - 7) == 0;
+ }
+#endif
+
+ HDEBUG(D_receive)
+ { if (f.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)
+ if ((f.helo_verified = strcmpic(sender_host_name, sender_helo_name) == 0))
+ {
+ sender_helo_dnssec = sender_host_dnssec;
+ HDEBUG(D_receive) debug_printf("matched host name\n");
+ }
+ else
+ {
+ uschar **aliases = sender_host_aliases;
+ while (*aliases)
+ if ((f.helo_verified = strcmpic(*aliases++, sender_helo_name) == 0))
+ {
+ sender_helo_dnssec = sender_host_dnssec;
+ break;
+ }
+
+ HDEBUG(D_receive) if (f.helo_verified)
+ debug_printf("matched alias %s\n", *(--aliases));
+ }
+
+ /* Final attempt: try a forward lookup of the helo name */
+
+ if (!f.helo_verified)
+ {
+ int rc;
+ host_item h =
+ {.name = sender_helo_name, .address = NULL, .mx = MX_NONE, .next = NULL};
+ dnssec_domains d =
+ {.request = US"*", .require = US""};
+
+ HDEBUG(D_receive) debug_printf("getting IP address for %s\n",
+ sender_helo_name);
+ rc = host_find_bydns(&h, NULL, HOST_FIND_BY_A | HOST_FIND_BY_AAAA,
+ NULL, NULL, NULL, &d, NULL, NULL);
+ if (rc == HOST_FOUND || rc == HOST_FOUND_LOCAL)
+ for (host_item * hh = &h; hh; hh = hh->next)
+ if (Ustrcmp(hh->address, sender_host_address) == 0)
+ {
+ f.helo_verified = TRUE;
+ if (h.dnssec == DS_YES) sender_helo_dnssec = TRUE;
+ HDEBUG(D_receive)
+ debug_printf("IP address for %s matches calling address\n"
+ "Forward DNS security status: %sverified\n",
+ sender_helo_name, sender_helo_dnssec ? "" : "un");
+ break;
+ }
+ }
+ }
+
+if (!f.helo_verified) f.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, TRUE);
+smtp_respond(code, len, TRUE, user_msg);
+}
+
+
+
+static int
+smtp_in_auth(auth_instance *au, uschar ** s, uschar ** ss)
+{
+const uschar *set_id = NULL;
+int rc;
+
+/* Set up globals for error messages */
+
+authenticator_name = au->name;
+driver_srcfile = au->srcfile;
+driver_srcline = au->srcline;
+
+/* 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 (int i = 0; i < AUTH_VARS; i++) auth_vars[i] = NULL;
+expand_nmax = 0;
+expand_nlength[0] = 0; /* $0 contains nothing */
+
+rc = (au->info->servercode)(au, smtp_cmd_data);
+if (au->set_id) set_id = expand_string(au->set_id);
+expand_nmax = -1; /* Reset numeric variables */
+for (int i = 0; i < AUTH_VARS; i++) auth_vars[i] = NULL; /* Reset $auth<n> */
+driver_srcfile = authenticator_name = NULL; driver_srcline = 0;
+
+/* 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) set_id = string_printing(set_id);
+
+/* For the non-OK cases, set up additional logging data if set_id
+is not empty. */
+
+if (rc != OK)
+ set_id = set_id && *set_id
+ ? string_sprintf(" (set_id=%s)", set_id) : US"";
+
+/* Switch on the result */
+
+switch(rc)
+ {
+ case OK:
+ if (!au->set_id || set_id) /* Complete success */
+ {
+ if (set_id) authenticated_id = string_copy_perm(set_id, TRUE);
+ sender_host_authenticated = au->name;
+ sender_host_auth_pubname = au->public_name;
+ authentication_failed = FALSE;
+ authenticated_fail_id = NULL; /* Impossible to already be set? */
+
+ received_protocol =
+ (sender_host_address ? protocols : protocols_local)
+ [pextend + pauthed + (tls_in.active.sock >= 0 ? pcrpted: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) authenticated_fail_id = string_copy_perm(set_id, TRUE);
+ *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) authenticated_fail_id = string_copy_perm(set_id, TRUE);
+ *s = US"535 Incorrect authentication data";
+ *ss = string_sprintf("535 Incorrect authentication data%s", set_id);
+ break;
+
+ default:
+ if (set_id) authenticated_fail_id = string_copy_perm(set_id, TRUE);
+ *s = US"435 Internal error";
+ *ss = string_sprintf("435 Internal error%s: return %d from authentication "
+ "check", set_id, rc);
+ break;
+ }
+
+return rc;
+}
+
+
+
+
+
+static int
+qualify_recipient(uschar ** recipient, uschar * smtp_cmd_data, uschar * tag)
+{
+int rd;
+if (f.allow_unqualified_recipient || strcmpic(*recipient, US"postmaster") == 0)
+ {
+ DEBUG(D_receive) debug_printf("unqualified address %s accepted\n",
+ *recipient);
+ rd = Ustrlen(recipient) + 1;
+ /* deconst ok as *recipient was not const */
+ *recipient = US rewrite_address_qualify(*recipient, TRUE);
+ return rd;
+ }
+smtp_printf("501 %s: recipient address must contain a domain\r\n", FALSE,
+ smtp_cmd_data);
+log_write(L_smtp_syntax_error,
+ LOG_MAIN|LOG_REJECT, "unqualified %s rejected: <%s> %s%s",
+ tag, *recipient, host_and_ident(TRUE), host_lookup_msg);
+return 0;
+}
+
+
+
+
+static void
+smtp_quit_handler(uschar ** user_msgp, uschar ** log_msgp)
+{
+HAD(SCH_QUIT);
+f.smtp_in_quit = TRUE;
+incomplete_transaction_log(US"QUIT");
+if ( acl_smtp_quit
+ && acl_check(ACL_WHERE_QUIT, NULL, acl_smtp_quit, user_msgp, log_msgp)
+ == ERROR)
+ log_write(0, LOG_MAIN|LOG_PANIC, "ACL for QUIT returned ERROR: %s",
+ *log_msgp);
+
+#ifdef EXIM_TCP_CORK
+(void) setsockopt(fileno(smtp_out), IPPROTO_TCP, EXIM_TCP_CORK, US &on, sizeof(on));
+#endif
+
+if (*user_msgp)
+ smtp_respond(US"221", 3, TRUE, *user_msgp);
+else
+ smtp_printf("221 %s closing connection\r\n", FALSE, smtp_active_hostname);
+
+#ifdef SERVERSIDE_CLOSE_NOWAIT
+# ifndef DISABLE_TLS
+tls_close(NULL, TLS_SHUTDOWN_NOWAIT);
+# endif
+
+log_write(L_smtp_connection, LOG_MAIN, "%s closed by QUIT",
+ smtp_get_connection_info());
+#else
+
+# ifndef DISABLE_TLS
+tls_close(NULL, TLS_SHUTDOWN_WAIT);
+# endif
+
+log_write(L_smtp_connection, LOG_MAIN, "%s closed by QUIT",
+ smtp_get_connection_info());
+
+/* Pause, hoping client will FIN first so that they get the TIME_WAIT.
+The socket should become readble (though with no data) */
+
+(void) poll_one_fd(fileno(smtp_in), POLLIN, 200);
+#endif /*!SERVERSIDE_CLOSE_NOWAIT*/
+}
+
+
+static void
+smtp_rset_handler(void)
+{
+HAD(SCH_RSET);
+incomplete_transaction_log(US"RSET");
+smtp_printf("250 Reset OK\r\n", FALSE);
+cmd_list[CMD_LIST_RSET].is_mail_cmd = FALSE;
+if (chunking_state > CHUNKING_OFFERED)
+ chunking_state = CHUNKING_OFFERED;
+}
+
+
+static int
+expand_mailmax(const uschar * s)
+{
+if (!(s = expand_cstring(s)))
+ log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand smtp_accept_max_per_connection");
+return *s ? Uatoi(s) : 0;
+}
+
+/*************************************************
+* 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;
+rmark reset_point = store_mark();
+
+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. */
+
+reset_point = smtp_reset(reset_point);
+message_ended = END_NOTSTARTED;
+
+chunking_state = f.chunking_offered ? CHUNKING_OFFERED : CHUNKING_NOT_OFFERED;
+
+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;
+#ifndef DISABLE_TLS
+cmd_list[CMD_LIST_STARTTLS].is_mail_cmd = TRUE;
+#endif
+
+if (lwr_receive_getc != NULL)
+ {
+ /* This should have already happened, but if we've gotten confused,
+ force a reset here. */
+ DEBUG(D_receive) debug_printf("WARNING: smtp_setup_msg had to restore receive functions to lowers\n");
+ bdat_pop_receive_functions();
+ }
+
+/* Set the local signal handler for SIGTERM - it tries to end off tidily */
+
+had_command_sigterm = 0;
+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();
+
+#ifdef TCP_QUICKACK
+if (smtp_in) /* Avoid pure-ACKs while in cmd pingpong phase */
+ (void) setsockopt(fileno(smtp_in), IPPROTO_TCP, TCP_QUICKACK,
+ US &off, sizeof(off));
+#endif
+
+/* 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)
+ {
+ const 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 *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 rc;
+ int c;
+ uschar *orcpt = NULL;
+ int dsn_flags;
+ gstring * g;
+
+#ifdef AUTH_TLS
+ /* Check once per STARTTLS or SSL-on-connect for a TLS AUTH */
+ if ( tls_in.active.sock >= 0
+ && tls_in.peercert
+ && tls_in.certificate_verified
+ && cmd_list[CMD_LIST_TLS_AUTH].is_mail_cmd
+ )
+ {
+ cmd_list[CMD_LIST_TLS_AUTH].is_mail_cmd = FALSE;
+
+ for (auth_instance * au = auths; au; au = au->next)
+ if (strcmpic(US"tls", au->driver_name) == 0)
+ {
+ if ( acl_smtp_auth
+ && (rc = acl_check(ACL_WHERE_AUTH, NULL, acl_smtp_auth,
+ &user_msg, &log_msg)) != OK
+ )
+ done = smtp_handle_acl_fail(ACL_WHERE_AUTH, rc, user_msg, log_msg);
+ else
+ {
+ smtp_cmd_data = NULL;
+
+ if (smtp_in_auth(au, &s, &ss) == OK)
+ { DEBUG(D_auth) debug_printf("tls auth succeeded\n"); }
+ else
+ { DEBUG(D_auth) debug_printf("tls auth not succeeded\n"); }
+ }
+ break;
+ }
+ }
+#endif
+
+ switch(smtp_read_command(
+#ifndef DISABLE_PIPE_CONNECT
+ !fl.pipe_connect_acceptable,
+#else
+ TRUE,
+#endif
+ GETC_BUFFER_UNLIMITED))
+ {
+ /* 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 (!fl.auth_advertised && !f.allow_auth_unadvertised)
+ {
+ done = synprot_error(L_smtp_protocol_error, 503, NULL,
+ US"AUTH command used when not advertised");
+ break;
+ }
+ if (sender_host_authenticated)
+ {
+ done = synprot_error(L_smtp_protocol_error, 503, NULL,
+ US"already authenticated");
+ break;
+ }
+ if (sender_address)
+ {
+ done = synprot_error(L_smtp_protocol_error, 503, NULL,
+ US"not permitted in mail transaction");
+ break;
+ }
+
+ /* Check the ACL */
+
+ if ( acl_smtp_auth
+ && (rc = acl_check(ACL_WHERE_AUTH, NULL, acl_smtp_auth,
+ &user_msg, &log_msg)) != 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;
+ for (; (c = *smtp_cmd_data) && !isspace(c); smtp_cmd_data++)
+ if (!isalnum(c) && c != '-' && c != '_')
+ {
+ done = synprot_error(L_smtp_syntax_error, 501, NULL,
+ US"invalid character in authentication mechanism name");
+ goto COMMAND_LOOP;
+ }
+
+ /* 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)
+ {
+ *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). */
+
+ {
+ auth_instance * au;
+ for (au = auths; au; au = au->next)
+ if (strcmpic(s, au->public_name) == 0 && au->server &&
+ (au->advertised || f.allow_auth_unadvertised))
+ break;
+
+ if (au)
+ {
+ c = smtp_in_auth(au, &s, &ss);
+
+ smtp_printf("%s\r\n", FALSE, s);
+ if (c != OK)
+ log_write(0, LOG_MAIN|LOG_REJECT, "%s authenticator failed for %s: %s",
+ au->name, host_and_ident(FALSE), ss);
+ }
+ else
+ done = synprot_error(L_smtp_protocol_error, 504, NULL,
+ string_sprintf("%s authentication mechanism not supported", s));
+ }
+
+ 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";
+ fl.esmtp = FALSE;
+ goto HELO_EHLO;
+
+ case EHLO_CMD:
+ HAD(SCH_EHLO);
+ hello = US"EHLO";
+ fl.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", FALSE, 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\", %s)",
+ host_and_ident(FALSE), string_printing(smtp_cmd_buffer),
+ string_from_gstring(s_connhad_log(NULL))
+ );
+ 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_verify_required
+ is set, ensure that the HELO name matches the actual host. If helo_verify
+ is set, do the same check, but softly. */
+
+ if (!f.sender_host_unknown)
+ {
+ BOOL old_helo_verified = f.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
+ && match_isinlist(sender_helo_name, CUSS &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.sock >= 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_verify_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. */
+
+ f.helo_verified = f.helo_verify_failed = sender_helo_dnssec = FALSE;
+ if (fl.helo_verify_required || fl.helo_verify)
+ {
+ BOOL tempfail = !smtp_verify_helo();
+ if (!f.helo_verified)
+ {
+ if (fl.helo_verify_required)
+ {
+ smtp_printf("%d %s argument does not match calling host\r\n", FALSE,
+ 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));
+ f.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 SUPPORT_SPF
+ /* set up SPF context */
+ spf_conn_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)
+ if ((rc = acl_check(ACL_WHERE_HELO, NULL, acl_smtp_helo,
+ &user_msg, &log_msg)) != OK)
+ {
+ done = smtp_handle_acl_fail(ACL_WHERE_HELO, rc, user_msg, log_msg);
+ sender_helo_name = NULL;
+ host_build_sender_fullhost(); /* Rebuild */
+ break;
+ }
+#ifndef DISABLE_PIPE_CONNECT
+ else if (!fl.pipe_connect_acceptable && !check_sync())
+#else
+ else if (!check_sync())
+#endif
+ 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(). */
+
+ fl.auth_advertised = FALSE;
+ f.smtp_in_pipelining_advertised = FALSE;
+#ifndef DISABLE_TLS
+ fl.tls_advertised = FALSE;
+#endif
+ fl.dsn_advertised = FALSE;
+#ifdef SUPPORT_I18N
+ fl.smtputf8_advertised = FALSE;
+#endif
+
+ /* Expand the per-connection message count limit option */
+ smtp_mailcmd_max = expand_mailmax(smtp_accept_max_per_connection);
+
+ smtp_code = US"250 "; /* Default response code plus space*/
+ if (!user_msg)
+ {
+ /* sender_host_name below will be tainted, so save on copy when we hit it */
+ g = string_get_tainted(24, GET_TAINTED);
+ g = string_fmt_append(g, "%.3s %s Hello %s%s%s",
+ smtp_code,
+ smtp_active_hostname,
+ sender_ident ? sender_ident : US"",
+ sender_ident ? US" at " : US"",
+ sender_host_name ? sender_host_name : sender_helo_name);
+
+ if (sender_host_address)
+ g = string_fmt_append(g, " [%s]", sender_host_address);
+ }
+
+ /* 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, TRUE);
+ 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;
+ }
+ g = string_cat(NULL, s);
+ }
+
+ g = string_catn(g, US"\r\n", 2);
+
+ /* If we received EHLO, we must create a multiline response which includes
+ the functions supported. */
+
+ if (fl.esmtp)
+ {
+ g->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)
+ g = string_fmt_append(g, "%.3s-SIZE %d\r\n", smtp_code,
+ thismessage_size_limit);
+ else
+ {
+ g = string_catn(g, smtp_code, 3);
+ g = string_catn(g, US"-SIZE\r\n", 7);
+ }
+
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ if ( (smtp_mailcmd_max > 0 || recipients_max)
+ && verify_check_host(&limits_advertise_hosts) == OK)
+ {
+ g = string_fmt_append(g, "%.3s-LIMITS", smtp_code);
+ if (smtp_mailcmd_max > 0)
+ g = string_fmt_append(g, " MAILMAX=%d", smtp_mailcmd_max);
+ if (recipients_max)
+ g = string_fmt_append(g, " RCPTMAX=%d", recipients_max);
+ g = string_catn(g, US"\r\n", 2);
+ }
+#endif
+
+ /* 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)
+ {
+ g = string_catn(g, smtp_code, 3);
+ g = string_catn(g, US"-8BITMIME\r\n", 11);
+ }
+
+ /* Advertise DSN support if configured to do so. */
+ if (verify_check_host(&dsn_advertise_hosts) != FAIL)
+ {
+ g = string_catn(g, smtp_code, 3);
+ g = string_catn(g, US"-DSN\r\n", 6);
+ fl.dsn_advertised = TRUE;
+ }
+
+ /* Advertise ETRN/VRFY/EXPN if there's are ACL checking whether a host is
+ permitted to issue them; a check is made when any host actually tries. */
+
+ if (acl_smtp_etrn)
+ {
+ g = string_catn(g, smtp_code, 3);
+ g = string_catn(g, US"-ETRN\r\n", 7);
+ }
+ if (acl_smtp_vrfy)
+ {
+ g = string_catn(g, smtp_code, 3);
+ g = string_catn(g, US"-VRFY\r\n", 7);
+ }
+ if (acl_smtp_expn)
+ {
+ g = string_catn(g, smtp_code, 3);
+ g = string_catn(g, 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 ( f.pipelining_enable
+ && verify_check_host(&pipelining_advertise_hosts) == OK)
+ {
+ g = string_catn(g, smtp_code, 3);
+ g = string_catn(g, US"-PIPELINING\r\n", 13);
+ sync_cmd_limit = NON_SYNC_CMD_PIPELINING;
+ f.smtp_in_pipelining_advertised = TRUE;
+
+#ifndef DISABLE_PIPE_CONNECT
+ if (fl.pipe_connect_acceptable)
+ {
+ f.smtp_in_early_pipe_advertised = TRUE;
+ g = string_catn(g, smtp_code, 3);
+ g = string_catn(g, US"-" EARLY_PIPE_FEATURE_NAME "\r\n", EARLY_PIPE_FEATURE_LEN+3);
+ }
+#endif
+ }
+
+
+ /* 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
+#ifdef AUTH_TLS
+ && !sender_host_authenticated
+#endif
+ && verify_check_host(&auth_advertise_hosts) == OK
+ )
+ {
+ BOOL first = TRUE;
+ for (auth_instance * au = auths; au; au = au->next)
+ {
+ au->advertised = FALSE;
+ if (au->server)
+ {
+ DEBUG(D_auth+D_expand) debug_printf_indent(
+ "Evaluating advertise_condition for %s %s athenticator\n",
+ au->name, au->public_name);
+ if ( !au->advertise_condition
+ || expand_check_condition(au->advertise_condition, au->name,
+ US"authenticator")
+ )
+ {
+ int saveptr;
+ if (first)
+ {
+ g = string_catn(g, smtp_code, 3);
+ g = string_catn(g, US"-AUTH", 5);
+ first = FALSE;
+ fl.auth_advertised = TRUE;
+ }
+ saveptr = g->ptr;
+ g = string_catn(g, US" ", 1);
+ g = string_cat (g, au->public_name);
+ while (++saveptr < g->ptr) g->s[saveptr] = toupper(g->s[saveptr]);
+ au->advertised = TRUE;
+ }
+ }
+ }
+
+ if (!first) g = string_catn(g, US"\r\n", 2);
+ }
+
+ /* RFC 3030 CHUNKING */
+
+ if (verify_check_host(&chunking_advertise_hosts) != FAIL)
+ {
+ g = string_catn(g, smtp_code, 3);
+ g = string_catn(g, US"-CHUNKING\r\n", 11);
+ f.chunking_offered = TRUE;
+ chunking_state = CHUNKING_OFFERED;
+ }
+
+ /* 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. */
+
+#ifndef DISABLE_TLS
+ if (tls_in.active.sock < 0 &&
+ verify_check_host(&tls_advertise_hosts) != FAIL)
+ {
+ g = string_catn(g, smtp_code, 3);
+ g = string_catn(g, US"-STARTTLS\r\n", 11);
+ fl.tls_advertised = TRUE;
+ }
+#endif
+
+#ifndef DISABLE_PRDR
+ /* Per Recipient Data Response, draft by Eric A. Hall extending RFC */
+ if (prdr_enable)
+ {
+ g = string_catn(g, smtp_code, 3);
+ g = string_catn(g, US"-PRDR\r\n", 7);
+ }
+#endif
+
+#ifdef SUPPORT_I18N
+ if ( accept_8bitmime
+ && verify_check_host(&smtputf8_advertise_hosts) != FAIL)
+ {
+ g = string_catn(g, smtp_code, 3);
+ g = string_catn(g, US"-SMTPUTF8\r\n", 11);
+ fl.smtputf8_advertised = TRUE;
+ }
+#endif
+
+ /* Finish off the multiline reply with one that is always available. */
+
+ g = string_catn(g, smtp_code, 3);
+ g = string_catn(g, US" HELP\r\n", 7);
+ }
+
+ /* Terminate the string (for debug), write it, and note that HELO/EHLO
+ has been seen. */
+
+#ifndef DISABLE_TLS
+ if (tls_in.active.sock >= 0)
+ (void)tls_write(NULL, g->s, g->ptr,
+# ifndef DISABLE_PIPE_CONNECT
+ fl.pipe_connect_acceptable && pipeline_connect_sends());
+# else
+ FALSE);
+# endif
+ else
+#endif
+ (void) fwrite(g->s, 1, g->ptr, smtp_out);
+
+ DEBUG(D_receive) for (const uschar * t, * s = string_from_gstring(g);
+ s && (t = Ustrchr(s, '\r'));
+ s = t + 2) /* \r\n */
+ debug_printf("%s %.*s\n",
+ s == g->s ? "SMTP>>" : " ",
+ (int)(t - s), s);
+ fl.helo_seen = TRUE;
+
+ /* Reset the protocol and the state, abandoning any previous message. */
+ received_protocol =
+ (sender_host_address ? protocols : protocols_local)
+ [ (fl.esmtp
+ ? pextend + (sender_host_authenticated ? pauthed : 0)
+ : pnormal)
+ + (tls_in.active.sock >= 0 ? pcrpted : 0)
+ ];
+ cancel_cutthrough_connection(TRUE, US"sent EHLO response");
+ reset_point = 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 */
+ message_start();
+ was_rej_mail = TRUE; /* Reset if accepted */
+ env_mail_type_t * mail_args; /* Sanity check & validate args */
+
+ if (!fl.helo_seen)
+ if ( fl.helo_verify_required
+ || verify_check_host(&hosts_require_helo) == OK)
+ {
+ smtp_printf("503 HELO or EHLO required\r\n", FALSE);
+ log_write(0, LOG_MAIN|LOG_REJECT, "rejected MAIL from %s: no "
+ "HELO/EHLO given", host_and_ident(FALSE));
+ break;
+ }
+ else if (smtp_mailcmd_max < 0)
+ smtp_mailcmd_max = expand_mailmax(smtp_accept_max_per_connection);
+
+ if (sender_address)
+ {
+ done = synprot_error(L_smtp_protocol_error, 503, NULL,
+ US"sender already given");
+ break;
+ }
+
+ if (!*smtp_cmd_data)
+ {
+ 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_mailcmd_max > 0 && smtp_mailcmd_count > smtp_mailcmd_max)
+ {
+ smtp_printf("421 too many messages in this connection\r\n", FALSE);
+ 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. */
+
+ cancel_cutthrough_connection(TRUE, US"MAIL received");
+ reset_point = smtp_reset(reset_point);
+ toomany = FALSE;
+ sender_data = recipient_data = NULL;
+
+ /* Loop, checking for ESMTP additions to the MAIL FROM command. */
+
+ if (fl.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;
+ mail_args->value != ENV_MAIL_OPT_NULL;
+ 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;
+
+ /* 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 (fl.dsn_advertised)
+ {
+ /* Check if RET has already been set */
+ if (dsn_ret > 0)
+ {
+ done = 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)
+ {
+ done = synprot_error(L_smtp_syntax_error, 501, NULL,
+ US"Value for RET is invalid");
+ goto COMMAND_LOOP;
+ }
+ }
+ break;
+ case ENV_MAIL_OPT_ENVID:
+ if (fl.dsn_advertised)
+ {
+ /* Check if the dsn envid has been already set */
+ if (dsn_envid)
+ {
+ done = 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;
+
+ /* 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)
+ {
+ ignore_msg = US"client not authenticated";
+ rc = sender_host_authenticated ? 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
+
+#ifdef SUPPORT_I18N
+ case ENV_MAIL_OPT_UTF8:
+ if (!fl.smtputf8_advertised)
+ {
+ done = synprot_error(L_smtp_syntax_error, 501, NULL,
+ US"SMTPUTF8 used when not advertised");
+ goto COMMAND_LOOP;
+ }
+
+ DEBUG(D_receive) debug_printf("smtputf8 requested\n");
+ message_smtputf8 = allow_utf8_domains = TRUE;
+ if (Ustrncmp(received_protocol, US"utf8", 4) != 0)
+ {
+ int old_pool = store_pool;
+ store_pool = POOL_PERM;
+ received_protocol = string_sprintf("utf8%s", received_protocol);
+ store_pool = old_pool;
+ }
+ break;
+#endif
+
+ /* No valid 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. */
+ case ENV_MAIL_OPT_NULL:
+ value[-1] = '=';
+ name[-1] = ' ';
+ arg_error = TRUE;
+ break;
+
+ default: assert(0);
+ }
+ /* 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
+ /* deconst ok as smtp_cmd_data was not const */
+ ? US rewrite_one(smtp_cmd_data, rewrite_smtp, NULL, FALSE, US"",
+ global_rewrite_rules)
+ : smtp_cmd_data;
+
+ raw_sender =
+ parse_extract_address(raw_sender, &errmess, &start, &end, &sender_domain,
+ TRUE);
+
+ if (!raw_sender)
+ {
+ 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", FALSE);
+ 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", FALSE);
+ 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 && *sender_address)
+ if (f.allow_unqualified_sender)
+ {
+ sender_domain = Ustrlen(sender_address) + 1;
+ /* deconst ok as sender_address was not const */
+ sender_address = US 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", FALSE,
+ 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 && !f.smtp_in_pipelining_advertised && !check_sync())
+ goto SYNC_FAILURE;
+ }
+ else
+ rc = OK;
+
+ if (rc == OK || rc == DISCARD)
+ {
+ BOOL more = pipeline_response();
+
+ if (!user_msg)
+ smtp_printf("%s%s%s", more, 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;
+ f.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);
+ /* We got really to many recipients. A check against configured
+ limits is done later */
+ if (rcpt_count < 0 || rcpt_count >= INT_MAX/2)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Too many recipients: %d", rcpt_count);
+ rcpt_count++;
+ was_rcpt = fl.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)
+ {
+ if (f.smtp_in_pipelining_advertised && last_was_rej_mail)
+ {
+ smtp_printf("503 sender not yet given\r\n", FALSE);
+ 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])
+ {
+ done = synprot_error(L_smtp_syntax_error, 501, NULL,
+ US"RCPT must have an address operand");
+ rcpt_fail_count++;
+ break;
+ }
+
+ /* Set the DSN flags orcpt and dsn_flags from the session*/
+ orcpt = NULL;
+ dsn_flags = 0;
+
+ if (fl.esmtp) for(;;)
+ {
+ uschar *name, *value;
+
+ if (!extract_option(&name, &value))
+ break;
+
+ if (fl.dsn_advertised && strcmpic(name, US"ORCPT") == 0)
+ {
+ /* Check whether orcpt has been already set */
+ if (orcpt)
+ {
+ done = 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 (fl.dsn_advertised && strcmpic(name, US"NOTIFY") == 0)
+ {
+ /* Check if the notify flags have been already set */
+ if (dsn_flags > 0)
+ {
+ done = synprot_error(L_smtp_syntax_error, 501, NULL,
+ US"NOTIFY can be specified once only");
+ goto COMMAND_LOOP;
+ }
+ if (strcmpic(value, US"NEVER") == 0)
+ dsn_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");
+ dsn_flags |= rf_notify_success;
+ }
+ else if (strcmpic(p, US"FAILURE") == 0)
+ {
+ DEBUG(D_receive) debug_printf("DSN: Setting notify failure\n");
+ dsn_flags |= rf_notify_failure;
+ }
+ else if (strcmpic(p, US"DELAY") == 0)
+ {
+ DEBUG(D_receive) debug_printf("DSN: Setting notify delay\n");
+ dsn_flags |= rf_notify_delay;
+ }
+ else
+ {
+ /* Catch any strange values */
+ done = 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", dsn_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;
+ }
+ }
+
+ /* Apply SMTP rewriting then extract the working address. Don't allow "<>"
+ as a recipient address */
+
+ recipient = rewrite_existflags & rewrite_smtp
+ /* deconst ok as smtp_cmd_data was not const */
+ ? US rewrite_one(smtp_cmd_data, rewrite_smtp, NULL, FALSE, US"",
+ global_rewrite_rules)
+ : smtp_cmd_data;
+
+ if (!(recipient = parse_extract_address(recipient, &errmess, &start, &end,
+ &recipient_domain, FALSE)))
+ {
+ 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)
+ if (!(recipient_domain = qualify_recipient(&recipient, smtp_cmd_data,
+ US"recipient")))
+ {
+ rcpt_fail_count++;
+ break;
+ }
+
+ /* Check maximum allowed */
+
+ if (rcpt_count+1 < 0 || rcpt_count > recipients_max && recipients_max > 0)
+ {
+ if (recipients_max_reject)
+ {
+ rcpt_fail_count++;
+ smtp_printf("552 too many recipients\r\n", FALSE);
+ 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", FALSE);
+ 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 (f.recipients_discarded)
+ rc = DISCARD;
+ else
+ if ( (rc = acl_check(ACL_WHERE_RCPT, recipient, acl_smtp_rcpt, &user_msg,
+ &log_msg)) == OK
+ && !f.smtp_in_pipelining_advertised && !check_sync())
+ goto SYNC_FAILURE;
+
+ /* The ACL was happy */
+
+ if (rc == OK)
+ {
+ BOOL more = pipeline_response();
+
+ if (user_msg)
+ smtp_user_msg(US"250", user_msg);
+ else
+ smtp_printf("250 Accepted\r\n", more);
+ receive_add_recipient(recipient, -1);
+
+ /* Set the dsn flags in the recipients_list */
+ recipients_list[recipients_count-1].orcpt = orcpt;
+ recipients_list[recipients_count-1].dsn_flags = dsn_flags;
+
+ /* DEBUG(D_receive) debug_printf("DSN: orcpt: %s flags: %d\n",
+ recipients_list[recipients_count-1].orcpt,
+ recipients_list[recipients_count-1].dsn_flags); */
+ }
+
+ /* The recipient was discarded */
+
+ else if (rc == DISCARD)
+ {
+ if (user_msg)
+ smtp_user_msg(US"250", user_msg);
+ else
+ smtp_printf("250 Accepted\r\n", FALSE);
+ rcpt_fail_count++;
+ discarded = TRUE;
+ log_write(0, LOG_MAIN|LOG_REJECT, "%s F=<%s> RCPT %s: "
+ "discarded by %s ACL%s%s", host_and_ident(TRUE),
+ sender_address_unrewritten ? sender_address_unrewritten : sender_address,
+ smtp_cmd_argument, f.recipients_discarded ? "MAIL" : "RCPT",
+ log_msg ? US": " : US"", log_msg ? log_msg : US"");
+ }
+
+ /* 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 BDAT_CMD:
+ {
+ int n;
+
+ HAD(SCH_BDAT);
+ if (chunking_state != CHUNKING_OFFERED)
+ {
+ done = synprot_error(L_smtp_protocol_error, 503, NULL,
+ US"BDAT command used when CHUNKING not advertised");
+ break;
+ }
+
+ /* grab size, endmarker */
+
+ if (sscanf(CS smtp_cmd_data, "%u %n", &chunking_datasize, &n) < 1)
+ {
+ done = synprot_error(L_smtp_protocol_error, 501, NULL,
+ US"missing size for BDAT command");
+ break;
+ }
+ chunking_state = strcmpic(smtp_cmd_data+n, US"LAST") == 0
+ ? CHUNKING_LAST : CHUNKING_ACTIVE;
+ chunking_data_left = chunking_datasize;
+ DEBUG(D_receive) debug_printf("chunking state %d, %d bytes\n",
+ (int)chunking_state, chunking_data_left);
+
+ f.bdat_readers_wanted = TRUE; /* FIXME: redundant vs chunking_state? */
+ f.dot_ends = FALSE;
+
+ goto DATA_BDAT;
+ }
+
+ case DATA_CMD:
+ HAD(SCH_DATA);
+ f.dot_ends = TRUE;
+ f.bdat_readers_wanted = FALSE;
+
+ DATA_BDAT: /* Common code for DATA and BDAT */
+#ifndef DISABLE_PIPE_CONNECT
+ fl.pipe_connect_acceptable = FALSE;
+#endif
+ if (!discarded && recipients_count <= 0)
+ {
+ if (fl.rcpt_smtp_response_same && rcpt_smtp_response)
+ {
+ 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 (f.smtp_in_pipelining_advertised && last_was_rcpt)
+ smtp_printf("503 Valid RCPT command must precede %s\r\n", FALSE,
+ smtp_names[smtp_connection_had[SMTP_HBUFF_PREV(smtp_ch_index)]]);
+ else
+ done = synprot_error(L_smtp_protocol_error, 503, NULL,
+ smtp_connection_had[SMTP_HBUFF_PREV(smtp_ch_index)] == SCH_DATA
+ ? US"valid RCPT command must precede DATA"
+ : US"valid RCPT command must precede BDAT");
+
+ if (chunking_state > CHUNKING_OFFERED)
+ {
+ bdat_push_receive_functions();
+ bdat_flush_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", FALSE);
+
+ if (chunking_state > CHUNKING_OFFERED)
+ {
+ bdat_push_receive_functions();
+ bdat_flush_data();
+ }
+ break;
+ }
+
+ if (chunking_state > CHUNKING_OFFERED)
+ rc = OK; /* No predata ACL or go-ahead output for BDAT */
+ else
+ {
+ /* 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 && cutthrough.cctx.sock < 0)
+ rc = OK;
+ else
+ {
+ uschar * acl = acl_smtp_predata ? acl_smtp_predata : US"accept";
+ f.enable_dollar_recipients = TRUE;
+ rc = acl_check(ACL_WHERE_PREDATA, NULL, acl, &user_msg,
+ &log_msg);
+ f.enable_dollar_recipients = FALSE;
+ if (rc == OK && !check_sync())
+ goto SYNC_FAILURE;
+
+ if (rc != OK)
+ { /* Either the ACL failed the address, or it was deferred. */
+ done = smtp_handle_acl_fail(ACL_WHERE_PREDATA, rc, user_msg, log_msg);
+ break;
+ }
+ }
+
+ if (user_msg)
+ smtp_user_msg(US"354", user_msg);
+ else
+ smtp_printf(
+ "354 Enter message, ending with \".\" on a line by itself\r\n", FALSE);
+ }
+
+ if (f.bdat_readers_wanted)
+ bdat_push_receive_functions();
+
+#ifdef TCP_QUICKACK
+ if (smtp_in) /* all ACKs needed to ramp window up for bulk data */
+ (void) setsockopt(fileno(smtp_in), IPPROTO_TCP, TCP_QUICKACK,
+ US &on, sizeof(on));
+#endif
+ done = 3;
+ message_ended = END_NOTENDED; /* Indicate in middle of data */
+
+ break;
+
+
+ case VRFY_CMD:
+ {
+ uschar * address;
+
+ HAD(SCH_VRFY);
+
+ if (!(address = parse_extract_address(smtp_cmd_data, &errmess,
+ &start, &end, &recipient_domain, FALSE)))
+ {
+ smtp_printf("501 %s\r\n", FALSE, errmess);
+ break;
+ }
+
+ if (!recipient_domain)
+ if (!(recipient_domain = qualify_recipient(&address, smtp_cmd_data,
+ US"verify")))
+ break;
+
+ if ((rc = acl_check(ACL_WHERE_VRFY, address, acl_smtp_vrfy,
+ &user_msg, &log_msg)) != OK)
+ done = smtp_handle_acl_fail(ACL_WHERE_VRFY, rc, user_msg, log_msg);
+ else
+ {
+ uschar * s = NULL;
+ 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", FALSE, 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 = f.log_testing_mode;
+ f.address_test_mode = f.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);
+ f.address_test_mode = FALSE;
+ f.log_testing_mode = save_log_testing_mode; /* true for -bh */
+ }
+ break;
+
+
+ #ifndef DISABLE_TLS
+
+ case STARTTLS_CMD:
+ HAD(SCH_STARTTLS);
+ if (!fl.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
+ && (rc = acl_check(ACL_WHERE_STARTTLS, NULL, acl_smtp_starttls,
+ &user_msg, &log_msg)) != 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 reasonable to
+ do an implied RSET when STARTTLS is received. */
+
+ incomplete_transaction_log(US"STARTTLS");
+ cancel_cutthrough_connection(TRUE, US"STARTTLS received");
+ reset_point = 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_hasc())
+ {
+ DEBUG(D_any)
+ debug_printf("Non-empty input buffer after STARTTLS; naive attack?\n");
+ if (tls_in.active.sock < 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 successfully
+ 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. */
+
+ s = NULL;
+ if ((rc = tls_server_start(&s)) == OK)
+ {
+ if (!tls_remember_esmtp)
+ fl.helo_seen = fl.esmtp = fl.auth_advertised = f.smtp_in_pipelining_advertised = FALSE;
+ cmd_list[CMD_LIST_EHLO].is_mail_cmd = TRUE;
+ cmd_list[CMD_LIST_AUTH].is_mail_cmd = TRUE;
+ cmd_list[CMD_LIST_TLS_AUTH].is_mail_cmd = TRUE;
+ if (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 =
+ (sender_host_address ? protocols : protocols_local)
+ [ (fl.esmtp
+ ? pextend + (sender_host_authenticated ? pauthed : 0)
+ : pnormal)
+ + (tls_in.active.sock >= 0 ? pcrpted : 0)
+ ];
+
+ sender_host_auth_pubname = 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 */
+ }
+ else
+ (void) smtp_log_tls_fail(s);
+
+ /* Some local configuration problem was discovered before actually trying
+ to do a TLS handshake; give a temporary error. */
+
+ if (rc == DEFER)
+ {
+ smtp_printf("454 TLS currently unavailable\r\n", FALSE);
+ 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. Another is a handshake
+ failure - and there may some encrypted data still in the pipe to us, which we
+ see as garbage commands. */
+
+ DEBUG(D_tls) debug_printf("TLS failed to start\n");
+ while (done <= 0) switch(smtp_read_command(FALSE, GETC_BUFFER_UNLIMITED))
+ {
+ 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:
+ f.smtp_in_quit = TRUE;
+ user_msg = NULL;
+ if ( acl_smtp_quit
+ && ((rc = acl_check(ACL_WHERE_QUIT, NULL, acl_smtp_quit, &user_msg,
+ &log_msg)) == ERROR))
+ log_write(0, LOG_MAIN|LOG_PANIC, "ACL for QUIT returned ERROR: %s",
+ log_msg);
+ if (user_msg)
+ smtp_respond(US"221", 3, TRUE, user_msg);
+ else
+ smtp_printf("221 %s closing connection\r\n", FALSE, smtp_active_hostname);
+ 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", FALSE);
+ break;
+ }
+ tls_close(NULL, TLS_SHUTDOWN_NOWAIT);
+ 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:
+ smtp_quit_handler(&user_msg, &log_msg);
+ done = 2;
+ break;
+
+
+ case RSET_CMD:
+ smtp_rset_handler();
+ cancel_cutthrough_connection(TRUE, US"RSET received");
+ reset_point = smtp_reset(reset_point);
+ toomany = FALSE;
+ break;
+
+
+ case NOOP_CMD:
+ HAD(SCH_NOOP);
+ smtp_printf("250 OK\r\n", FALSE);
+ 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", TRUE);
+ {
+ uschar buffer[256];
+ buffer[0] = 0;
+ Ustrcat(buffer, US" AUTH");
+ #ifndef DISABLE_TLS
+ if (tls_in.active.sock < 0 &&
+ verify_check_host(&tls_advertise_hosts) != FAIL)
+ Ustrcat(buffer, US" STARTTLS");
+ #endif
+ Ustrcat(buffer, US" HELO EHLO MAIL RCPT DATA BDAT");
+ Ustrcat(buffer, US" NOOP QUIT RSET HELP");
+ if (acl_smtp_etrn) Ustrcat(buffer, US" ETRN");
+ if (acl_smtp_expn) Ustrcat(buffer, US" EXPN");
+ if (acl_smtp_vrfy) Ustrcat(buffer, US" VRFY");
+ smtp_printf("214%s\r\n", FALSE, 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 || recipients_count > 0)
+ log_write(L_lost_incoming_connection, LOG_MAIN,
+ "unexpected %s while reading SMTP command from %s%s%s D=%s",
+ f.sender_host_unknown ? "EOF" : "disconnection",
+ f.tcp_in_fastopen_logged
+ ? US""
+ : f.tcp_in_fastopen
+ ? f.tcp_in_fastopen_data ? US"TFO* " : US"TFO "
+ : US"",
+ host_and_ident(FALSE), smtp_read_error,
+ string_timesince(&smtp_connection_start)
+ );
+
+ else
+ log_write(L_smtp_connection, LOG_MAIN, "%s %slost%s D=%s",
+ smtp_get_connection_info(),
+ f.tcp_in_fastopen && !f.tcp_in_fastopen_logged ? US"TFO " : US"",
+ smtp_read_error,
+ string_timesince(&smtp_connection_start)
+ );
+
+ done = 1;
+ break;
+
+
+ case ETRN_CMD:
+ HAD(SCH_ETRN);
+ if (sender_address)
+ {
+ 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));
+
+ if ((rc = acl_check(ACL_WHERE_ETRN, NULL, acl_smtp_etrn,
+ &user_msg, &log_msg)) != 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)
+ {
+ 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,
+ FALSE, 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", FALSE);
+ 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 = CUSS child_exec_exim(CEE_RETURN_ARGV, TRUE, NULL, TRUE,
+ *queue_name ? 4 : 2,
+ US"-R", smtp_cmd_data,
+ US"-MCG", queue_name);
+ }
+
+ /* 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", FALSE);
+ 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, 1))
+ {
+ smtp_printf("458 Already processing %s\r\n", FALSE, 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 = exim_fork(US"etrn-command")) == 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 = exim_fork(US"etrn-serialised-command")) == 0)
+ {
+ DEBUG(D_exec) debug_print_argv(argv);
+ exim_nullstd(); /* Ensure std{in,out,err} exist */
+ /* argv[0] should be untainted, from child_exec_exim() */
+ 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);
+ exim_underbar_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", FALSE);
+ if (smtp_etrn_serialize) enq_end(etrn_serialize_key);
+ }
+ else
+ if (!user_msg)
+ smtp_printf("250 OK\r\n", FALSE);
+ 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"NUL character(s) present (shown as '?')");
+ smtp_printf("501 NUL characters are not allowed in SMTP commands\r\n",
+ FALSE);
+ 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; /* limit logged amount */
+ 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\"",
+ f.smtp_in_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 SUPPORT_PROXY
+ case PROXY_FAIL_IGNORE_CMD:
+ smtp_printf("503 Command refused, required Proxy negotiation failed\r\n", FALSE);
+ 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),
+ string_printing(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 */
+ }
+
+return done - 2; /* Convert yield values */
+}
+
+
+
+gstring *
+authres_smtpauth(gstring * g)
+{
+if (!sender_host_authenticated)
+ return g;
+
+g = string_append(g, 2, US";\n\tauth=pass (", sender_host_auth_pubname);
+
+if (Ustrcmp(sender_host_auth_pubname, "tls") == 0)
+ g = authenticated_id
+ ? string_append(g, 2, US") x509.auth=", authenticated_id)
+ : string_cat(g, US") reason=x509.auth");
+else
+ g = authenticated_id
+ ? string_append(g, 2, US") smtp.auth=", authenticated_id)
+ : string_cat(g, US", no id saved)");
+
+if (authenticated_sender)
+ g = string_append(g, 2, US" smtp.mailfrom=", authenticated_sender);
+return g;
+}
+
+
+
+/* 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
index 0000000..7b7bdf7
--- /dev/null
+++ b/src/smtp_out.c
@@ -0,0 +1,936 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* A number of functions for driving outgoing SMTP calls. */
+
+
+#include "exim.h"
+#include "transports/smtp.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)
+ interface point this to the interface if there is one defined
+ 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,
+ uschar **interface, uschar *msg)
+{
+const uschar * expint;
+uschar *iface;
+int sep = 0;
+
+if (!istring) return TRUE;
+
+if (!(expint = expand_string(istring)))
+ {
+ if (f.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 (is_tainted(expint))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "attempt to use tainted value '%s' from '%s' for interface",
+ expint, istring);
+ addr->transport_return = PANIC;
+ addr->message = string_sprintf("failed to expand \"interface\" "
+ "option for %s: configuration error", msg);
+ return FALSE;
+ }
+
+Uskip_whitespace(&expint);
+if (!*expint) return TRUE;
+
+while ((iface = string_nextinlist(&expint, &sep, NULL, 0)))
+ {
+ int if_af = string_is_ip_address(iface, NULL);
+ if (if_af == 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 ((if_af == 4 ? AF_INET : AF_INET6) == host_af)
+ break;
+ }
+
+*interface = 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)
+ {
+ 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)
+ {
+ 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;
+}
+
+
+
+
+#ifdef TCP_FASTOPEN
+/* Try to record if TFO was attmepted and if it was successfully used. */
+
+static void
+tfo_out_check(int sock)
+{
+static BOOL done_once = FALSE;
+
+if (done_once) return;
+done_once = TRUE;
+
+# ifdef __FreeBSD__
+struct tcp_info tinfo;
+socklen_t len = sizeof(tinfo);
+
+/* A getsockopt TCP_FASTOPEN unfortunately returns "was-used" for a TFO/R as
+well as a TFO/C. Use what we can of the Linux hack below; reliability issues ditto. */
+switch (tcp_out_fastopen)
+ {
+ case TFO_ATTEMPTED_NODATA:
+ if ( getsockopt(sock, IPPROTO_TCP, TCP_INFO, &tinfo, &len) == 0
+ && tinfo.tcpi_state == TCPS_SYN_SENT
+ && tinfo.__tcpi_unacked > 0
+ )
+ {
+ DEBUG(D_transport|D_v)
+ debug_printf("TCP_FASTOPEN tcpi_unacked %d\n", tinfo.__tcpi_unacked);
+ tcp_out_fastopen = TFO_USED_NODATA;
+ }
+ break;
+ /*
+ case TFO_ATTEMPTED_DATA:
+ case TFO_ATTEMPTED_DATA:
+ if (tinfo.tcpi_options & TCPI_OPT_SYN_DATA) XXX no equvalent as of 12.2
+ */
+ }
+
+switch (tcp_out_fastopen)
+ {
+ case TFO_ATTEMPTED_DATA: tcp_out_fastopen = TFO_USED_DATA; break;
+ default: break; /* compiler quietening */
+ }
+
+# else /* Linux & Apple */
+# if defined(TCP_INFO) && defined(EXIM_HAVE_TCPI_UNACKED)
+struct tcp_info tinfo;
+socklen_t len = sizeof(tinfo);
+
+switch (tcp_out_fastopen)
+ {
+ /* This is a somewhat dubious detection method; totally undocumented so likely
+ to fail in future kernels. There seems to be no documented way. What we really
+ want to know is if the server sent smtp-banner data before our ACK of his SYN,ACK
+ hit him. What this (possibly?) detects is whether we sent a TFO cookie with our
+ SYN, as distinct from a TFO request. This gets a false-positive when the server
+ key is rotated; we send the old one (which this test sees) but the server returns
+ the new one and does not send its SMTP banner before we ACK his SYN,ACK.
+ To force that rotation case:
+ '# echo -n "00000000-00000000-00000000-0000000" >/proc/sys/net/ipv4/tcp_fastopen_key'
+ The kernel seems to be counting unack'd packets. */
+
+ case TFO_ATTEMPTED_NODATA:
+ if ( getsockopt(sock, IPPROTO_TCP, TCP_INFO, &tinfo, &len) == 0
+ && tinfo.tcpi_state == TCP_SYN_SENT
+ && tinfo.tcpi_unacked > 1
+ )
+ {
+ DEBUG(D_transport|D_v)
+ debug_printf("TCP_FASTOPEN tcpi_unacked %d\n", tinfo.tcpi_unacked);
+ tcp_out_fastopen = TFO_USED_NODATA;
+ }
+ break;
+
+ /* When called after waiting for received data we should be able
+ to tell if data we sent was accepted. */
+
+ case TFO_ATTEMPTED_DATA:
+ if ( getsockopt(sock, IPPROTO_TCP, TCP_INFO, &tinfo, &len) == 0
+ && tinfo.tcpi_state == TCP_ESTABLISHED
+ )
+ if (tinfo.tcpi_options & TCPI_OPT_SYN_DATA)
+ {
+ DEBUG(D_transport|D_v) debug_printf("TFO: data was acked\n");
+ tcp_out_fastopen = TFO_USED_DATA;
+ }
+ else
+ {
+ DEBUG(D_transport|D_v) debug_printf("TFO: had to retransmit\n");
+ tcp_out_fastopen = TFO_NOT_USED;
+ }
+ break;
+
+ default: break; /* compiler quietening */
+ }
+# endif
+# endif /* Linux & Apple */
+}
+#endif
+
+
+/* Create and bind a socket, given the connect-args.
+Update those with the state. Return the fd, or -1 with errno set.
+*/
+
+int
+smtp_boundsock(smtp_connect_args * sc)
+{
+transport_instance * tb = sc->tblock;
+smtp_transport_options_block * ob =
+ (smtp_transport_options_block *)tb->options_block;
+const uschar * dscp = ob->dscp;
+int sock, dscp_value, dscp_level, dscp_option;
+
+if ((sock = ip_socket(SOCK_STREAM, sc->host_af)) < 0)
+ return -1;
+
+/* Set TCP_NODELAY; Exim does its own buffering. */
+
+if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, US &on, sizeof(on)))
+ HDEBUG(D_transport|D_acl|D_v)
+ debug_printf_indent("failed to set NODELAY: %s ", strerror(errno));
+
+/* 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, sc->host_af, &dscp_level, &dscp_option, &dscp_value))
+ {
+ HDEBUG(D_transport|D_acl|D_v)
+ debug_printf_indent("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_indent("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 (sc->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 (sc->interface)
+ {
+ union sockaddr_46 interface_sock;
+ EXIM_SOCKLEN_T size = sizeof(interface_sock);
+
+ if ( ip_bind(sock, sc->host_af, sc->interface, 0) < 0
+ || getsockname(sock, (struct sockaddr *) &interface_sock, &size) < 0
+ )
+ {
+ HDEBUG(D_transport|D_acl|D_v)
+ debug_printf_indent("unable to bind outgoing SMTP call to %s: %s", sc->interface,
+ strerror(errno));
+ close(sock);
+ return -1;
+ }
+ sending_ip_address = host_ntoa(-1, &interface_sock, NULL, &sending_port);
+ }
+
+sc->sock = sock;
+return sock;
+}
+
+
+/* Arguments:
+ host host item containing name and address and port
+ host_af AF_INET or AF_INET6
+ port TCP port number
+ interface outgoing interface address or NULL
+ tb transport
+ timeout timeout value or 0
+ early_data if non-NULL, idempotent data to be sent -
+ preferably in the TCP SYN segment
+ Special case: non-NULL but with NULL blob.data - caller is
+ client-data-first (eg. TLS-on-connect) and a lazy-TCP-connect is
+ acceptable.
+
+Returns: connected socket number, or -1 with errno set
+*/
+
+int
+smtp_sock_connect(smtp_connect_args * sc, int timeout, const blob * early_data)
+{
+smtp_transport_options_block * ob =
+ (smtp_transport_options_block *)sc->tblock->options_block;
+int sock;
+int save_errno = 0;
+const blob * fastopen_blob = NULL;
+
+
+#ifndef DISABLE_EVENT
+deliver_host_address = sc->host->address;
+deliver_host_port = sc->host->port;
+if (event_raise(sc->tblock->event_action, US"tcp:connect", NULL, &errno)) return -1;
+#endif
+
+if ( (sock = sc->sock) < 0
+ && (sock = smtp_boundsock(sc)) < 0)
+ save_errno = errno;
+sc->sock = -1;
+
+/* Connect to the remote host, and add keepalive to the socket before returning
+it, if requested. If the build supports TFO, request it - and if the caller
+requested some early-data then include that in the TFO request. If there is
+early-data but no TFO support, send it after connecting. */
+
+if (!save_errno)
+ {
+#ifdef TCP_FASTOPEN
+ /* See if TCP Fast Open usable. Default is a traditional 3WHS connect */
+ if (verify_check_given_host(CUSS &ob->hosts_try_fastopen, sc->host) == OK)
+ {
+ if (!early_data)
+ fastopen_blob = &tcp_fastopen_nodata; /* TFO, with no data */
+ else if (early_data->data)
+ fastopen_blob = early_data; /* TFO, with data */
+# ifdef TCP_FASTOPEN_CONNECT
+ else
+ { /* expecting client data */
+ debug_printf(" set up lazy-connect\n");
+ setsockopt(sock, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, US &on, sizeof(on));
+ /* fastopen_blob = NULL; lazy TFO, triggered by data write */
+ }
+# endif
+ }
+#endif
+
+ if (ip_connect(sock, sc->host_af, sc->host->address, sc->host->port, timeout, fastopen_blob) < 0)
+ save_errno = errno;
+ else if (early_data && !fastopen_blob && early_data->data && early_data->len)
+ {
+ /* We had some early-data to send, but couldn't do TFO */
+ HDEBUG(D_transport|D_acl|D_v)
+ debug_printf("sending %ld nonTFO early-data\n", (long)early_data->len);
+
+#ifdef TCP_QUICKACK_notdef
+ (void) setsockopt(sock, IPPROTO_TCP, TCP_QUICKACK, US &off, sizeof(off));
+#endif
+ if (send(sock, early_data->data, early_data->len, 0) < 0)
+ save_errno = errno;
+ }
+#ifdef TCP_QUICKACK_notdef
+ /* Under TFO (with openssl & pipe-conn; testcase 4069, as of
+ 5.10.8-100.fc32.x86_64) this seems to be inop.
+ Perhaps overwritten when we (client) go -> ESTABLISHED on seeing the 3rd-ACK?
+ For that case, added at smtp_reap_banner(). */
+ (void) setsockopt(sock, IPPROTO_TCP, TCP_QUICKACK, US &off, sizeof(off));
+#endif
+ }
+
+if (!save_errno)
+ {
+ union sockaddr_46 interface_sock;
+ EXIM_SOCKLEN_T size = sizeof(interface_sock);
+
+ /* Both bind() and connect() succeeded, and any early-data */
+
+ HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" 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 (ob->keepalive) ip_keepalive(sock, sc->host->address, TRUE);
+#ifdef TCP_FASTOPEN
+ tfo_out_check(sock);
+#endif
+ return sock;
+ }
+
+/* Either bind() or connect() failed */
+
+HDEBUG(D_transport|D_acl|D_v)
+ {
+ debug_printf_indent(" 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;
+}
+
+
+
+
+
+void
+smtp_port_for_connect(host_item * host, int port)
+{
+if (host->port != PORT_NONE)
+ {
+ HDEBUG(D_transport|D_acl|D_v) if (port != host->port)
+ debug_printf_indent("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 */
+}
+
+
+/*************************************************
+* 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.
+
+Arguments:
+ sc details for making connection: host, af, interface, transport
+ early_data if non-NULL, data to be sent - preferably in the TCP SYN segment
+ Special case: non-NULL but with NULL blob.data - caller is
+ client-data-first (eg. TLS-on-connect) and a lazy-TCP-connect is
+ acceptable.
+
+Returns: connected socket number, or -1 with errno set
+*/
+
+int
+smtp_connect(smtp_connect_args * sc, const blob * early_data)
+{
+int port = sc->host->port;
+smtp_transport_options_block * ob = sc->ob;
+
+callout_address = string_sprintf("[%s]:%d", sc->host->address, port);
+
+HDEBUG(D_transport|D_acl|D_v)
+ {
+ uschar * s = US" ";
+ if (sc->interface) s = string_sprintf(" from %s ", sc->interface);
+#ifdef SUPPORT_SOCKS
+ if (ob->socks_proxy) s = string_sprintf("%svia proxy ", s);
+#endif
+ debug_printf_indent("Connecting to %s %s%s... ", sc->host->name, callout_address, s);
+ }
+
+/* Create and connect the socket */
+
+#ifdef SUPPORT_SOCKS
+if (ob->socks_proxy)
+ {
+ int sock = socks_sock_connect(sc->host, sc->host_af, port, sc->interface,
+ sc->tblock, ob->connect_timeout);
+
+ if (sock >= 0)
+ {
+ if (early_data && early_data->data && early_data->len)
+ if (send(sock, early_data->data, early_data->len, 0) < 0)
+ {
+ int save_errno = errno;
+ HDEBUG(D_transport|D_acl|D_v)
+ {
+ debug_printf_indent("failed: %s", CUstrerror(save_errno));
+ if (save_errno == ETIMEDOUT)
+ debug_printf(" (timeout=%s)", readconf_printtime(ob->connect_timeout));
+ debug_printf("\n");
+ }
+ (void)close(sock);
+ sock = -1;
+ errno = save_errno;
+ }
+ }
+ return sock;
+ }
+#endif
+
+return smtp_sock_connect(sc, ob->connect_timeout, early_data);
+}
+
+
+/*************************************************
+* 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
+ mode further data expected, or plain
+
+Returns: TRUE if OK, FALSE on error, with errno set
+*/
+
+static BOOL
+flush_buffer(smtp_outblock * outblock, int mode)
+{
+int rc;
+int n = outblock->ptr - outblock->buffer;
+BOOL more = mode == SCMD_MORE;
+client_conn_ctx * cctx;
+
+HDEBUG(D_transport|D_acl) debug_printf_indent("cmd buf flush %d bytes%s\n", n,
+ more ? " (more expected)" : "");
+
+if (!(cctx = outblock->cctx))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "null conn-context pointer");
+ errno = 0;
+ return FALSE;
+ }
+
+#ifndef DISABLE_TLS
+if (cctx->tls_ctx) /*XXX have seen a null cctx here, rvfy sending QUIT, hence check above */
+ rc = tls_write(cctx->tls_ctx, outblock->buffer, n, more);
+else
+#endif
+
+ {
+ if (outblock->conn_args)
+ {
+ blob early_data = { .data = outblock->buffer, .len = n };
+
+ /* We ignore the more-flag if we're doing a connect with early-data, which
+ means we won't get BDAT+data. A pity, but wise due to the idempotency
+ requirement: TFO with data can, in rare cases, replay the data to the
+ receiver. */
+
+ if ( (cctx->sock = smtp_connect(outblock->conn_args, &early_data))
+ < 0)
+ return FALSE;
+ outblock->conn_args = NULL;
+ rc = n;
+ }
+ else
+ {
+ rc = send(cctx->sock, outblock->buffer, n,
+#ifdef MSG_MORE
+ more ? MSG_MORE : 0
+#else
+ 0
+#endif
+ );
+
+#if defined(__linux__)
+ /* This is a workaround for a current linux kernel bug: as of
+ 5.6.8-200.fc31.x86_64 small (<MSS) writes get delayed by about 200ms,
+ This is despite NODELAY being active.
+ https://bugzilla.redhat.com/show_bug.cgi?id=1803806 */
+
+ if (!more)
+ setsockopt(cctx->sock, IPPROTO_TCP, TCP_CORK, &off, sizeof(off));
+#endif
+ }
+ }
+
+if (rc <= 0)
+ {
+ HDEBUG(D_transport|D_acl) debug_printf_indent("send failed: %s\n", strerror(errno));
+ return FALSE;
+ }
+
+outblock->ptr = outblock->buffer;
+outblock->cmd_count = 0;
+return TRUE;
+}
+
+
+
+/* This might be called both due to callout and then from delivery.
+Use memory that will not be released between those phases.
+*/
+static void
+smtp_debug_resp(const uschar * buf)
+{
+#ifndef DISABLE_CLIENT_CMD_LOG
+int old_pool = store_pool;
+store_pool = POOL_PERM;
+client_cmd_log = string_append_listele_n(client_cmd_log, ':', buf,
+ buf[3] == ' ' ? 3 : 4);
+store_pool = old_pool;
+#endif
+}
+
+
+/*************************************************
+* Write SMTP command *
+*************************************************/
+
+/* The formatted command is left in big_buffer so that it can be reflected in
+any error message.
+
+Arguments:
+ sx SMTP connection, contains buffer for pipelining, and socket
+ mode buffer, write-with-more-likely, write
+ format a format, starting with one of
+ of HELO, MAIL FROM, RCPT TO, DATA, ".", or QUIT.
+ If NULL, flush pipeline buffer only.
+ ... 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(void * sx, int mode, const char * format, ...)
+{
+smtp_outblock * outblock = &((smtp_context *)sx)->outblock;
+int rc = 0;
+
+if (format)
+ {
+ gstring gs = { .size = big_buffer_size, .ptr = 0, .s = big_buffer };
+ va_list ap;
+
+ /* Use taint-unchecked routines for writing into big_buffer, trusting that
+ we'll never expand the results. Actually, the error-message use - leaving
+ the results in big_buffer for potential later use - is uncomfortably distant.
+ XXX Would be better to assume all smtp commands are short, use normal pool
+ alloc rather than big_buffer, and another global for the data-for-error. */
+
+ va_start(ap, format);
+ if (!string_vformat(&gs, SVFMT_TAINT_NOCHK, CS format, ap))
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "overlong write_command in outgoing "
+ "SMTP");
+ va_end(ap);
+ string_from_gstring(&gs);
+
+ if (gs.ptr > outblock->buffersize)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "overlong write_command in outgoing "
+ "SMTP");
+
+ if (gs.ptr > outblock->buffersize - (outblock->ptr - outblock->buffer))
+ {
+ rc = outblock->cmd_count; /* flush resets */
+ if (!flush_buffer(outblock, SCMD_FLUSH)) return -1;
+ }
+
+ Ustrncpy(outblock->ptr, gs.s, gs.ptr);
+ outblock->ptr += gs.ptr;
+ outblock->cmd_count++;
+ gs.ptr -= 2; string_from_gstring(&gs); /* 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) *p++ = '*';
+ }
+
+ smtp_debug_cmd(big_buffer, mode);
+ }
+
+if (mode != SCMD_BUFFER)
+ {
+ rc += outblock->cmd_count; /* flush resets */
+ if (!flush_buffer(outblock, mode)) 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
+ timelimit deadline for reading the lime, seconds past epoch
+
+Returns: length of a line that has been put in the buffer
+ -1 otherwise, with errno set, and inblock->ptr adjusted
+*/
+
+static int
+read_response_line(smtp_inblock *inblock, uschar *buffer, int size, time_t timelimit)
+{
+uschar *p = buffer;
+uschar *ptr = inblock->ptr;
+uschar *ptrend = inblock->ptrend;
+client_conn_ctx * cctx = inblock->cctx;
+
+/* 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;
+ inblock->ptr = ptr;
+ return -1;
+ }
+ }
+
+ /* Need to read a new input packet. */
+
+ if((rc = ip_recv(cctx, inblock->buffer, inblock->buffersize, timelimit)) <= 0)
+ {
+ DEBUG(D_deliver|D_transport|D_acl|D_v)
+ debug_printf_indent(errno ? " SMTP(%s)<<\n" : " SMTP(closed)<<\n",
+ strerror(errno));
+ 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_indent("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. */
+
+inblock->ptr = inblock->ptrend = inblock->buffer;
+*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:
+ sx the SMTP connection (contains input block with 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, in seconds
+
+Returns: TRUE if a valid, non-error response was received; else FALSE
+*/
+/*XXX could move to smtp transport; no other users */
+
+BOOL
+smtp_read_response(void * sx0, uschar * buffer, int size, int okdigit,
+ int timeout)
+{
+smtp_context * sx = sx0;
+uschar * ptr = buffer;
+int count = 0;
+time_t timelimit = time(NULL) + timeout;
+BOOL yield = FALSE;
+
+errno = 0; /* Ensure errno starts out zero */
+buffer[0] = '\0';
+
+#ifndef DISABLE_PIPE_CONNECT
+if (sx->pending_BANNER || sx->pending_EHLO)
+ {
+ int rc;
+ if ((rc = smtp_reap_early_pipe(sx, &count)) != OK)
+ {
+ DEBUG(D_transport) debug_printf("failed reaping pipelined cmd responsess\n");
+ if (rc == DEFER) errno = ERRNO_TLSFAILURE;
+ goto out;
+ }
+ }
+#endif
+
+/* This is a loop to read and concatenate the lines that make up a multi-line
+response. */
+
+for (;;)
+ {
+ if ((count = read_response_line(&sx->inblock, ptr, size, timelimit)) < 0)
+ return FALSE;
+
+ HDEBUG(D_transport|D_acl|D_v)
+ debug_printf_indent(" %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 */
+ goto out;
+ }
+
+ /* 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;
+ }
+
+#ifdef TCP_FASTOPEN
+tfo_out_check(sx->cctx.sock);
+#endif
+
+/* 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;
+yield = buffer[0] == okdigit;
+
+out:
+ smtp_debug_resp(buffer);
+ return yield;
+}
+
+/* End of smtp_out.c */
+/* vi: aw ai sw=2
+*/
diff --git a/src/spam.c b/src/spam.c
new file mode 100644
index 0000000..a68b9bf
--- /dev/null
+++ b/src/spam.c
@@ -0,0 +1,614 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/*
+ * Copyright (c) The Exim Maintainers 2016 - 2022
+ * Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003 - 2015
+ * 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_action_buffer[32];
+uschar spam_report_buffer[32600];
+uschar * prev_user_name = NULL;
+int spam_ok = 0;
+int spam_rc = 0;
+uschar *prev_spamd_address_work = NULL;
+
+static const uschar * loglabel = US"spam acl condition:";
+
+
+static int
+spamd_param_init(spamd_address_container *spamd)
+{
+/* default spamd server weight, time and priority value */
+spamd->is_rspamd = FALSE;
+spamd->is_failed = FALSE;
+spamd->weight = SPAMD_WEIGHT;
+spamd->timeout = SPAMD_TIMEOUT;
+spamd->retry = 0;
+spamd->priority = SPAMD_PRIORITY;
+return 0;
+}
+
+
+static int
+spamd_param(const uschar * param, spamd_address_container * spamd)
+{
+static int timesinceday = -1;
+const uschar * s;
+const uschar * name;
+
+/*XXX more clever parsing could discard embedded spaces? */
+
+if (sscanf(CCS param, "pri=%u", &spamd->priority))
+ return 0; /* OK */
+
+if (sscanf(CCS param, "weight=%u", &spamd->weight))
+ {
+ if (spamd->weight == 0) /* this server disabled: skip it */
+ return 1;
+ return 0; /* OK */
+ }
+
+if (Ustrncmp(param, "time=", 5) == 0)
+ {
+ unsigned int start_h = 0, start_m = 0, start_s = 0;
+ unsigned int end_h = 24, end_m = 0, end_s = 0;
+ unsigned int time_start, time_end;
+ const uschar * end_string;
+
+ name = US"time";
+ s = param+5;
+ if ((end_string = Ustrchr(s, '-')))
+ {
+ end_string++;
+ if ( sscanf(CS end_string, "%u.%u.%u", &end_h, &end_m, &end_s) == 0
+ || sscanf(CS s, "%u.%u.%u", &start_h, &start_m, &start_s) == 0
+ )
+ goto badval;
+ }
+ else
+ goto badval;
+
+ if (timesinceday < 0)
+ {
+ time_t now = time(NULL);
+ struct tm *tmp = localtime(&now);
+ timesinceday = tmp->tm_hour*3600 + tmp->tm_min*60 + tmp->tm_sec;
+ }
+
+ time_start = start_h*3600 + start_m*60 + start_s;
+ time_end = end_h*3600 + end_m*60 + end_s;
+
+ if (timesinceday < time_start || timesinceday >= time_end)
+ return 1; /* skip spamd server */
+
+ return 0; /* OK */
+ }
+
+if (Ustrcmp(param, "variant=rspamd") == 0)
+ {
+ spamd->is_rspamd = TRUE;
+ return 0;
+ }
+
+if (Ustrncmp(param, "tmo=", 4) == 0)
+ {
+ int sec = readconf_readtime((s = param+4), '\0', FALSE);
+ name = US"timeout";
+ if (sec < 0)
+ goto badval;
+ spamd->timeout = sec;
+ return 0;
+ }
+
+if (Ustrncmp(param, "retry=", 6) == 0)
+ {
+ int sec = readconf_readtime((s = param+6), '\0', FALSE);
+ name = US"retry";
+ if (sec < 0)
+ goto badval;
+ spamd->retry = sec;
+ return 0;
+ }
+
+log_write(0, LOG_MAIN, "%s warning - invalid spamd parameter: '%s'",
+ loglabel, param);
+return -1; /* syntax error */
+
+badval:
+ log_write(0, LOG_MAIN,
+ "%s warning - invalid spamd %s value: '%s'", loglabel, name, s);
+ return -1; /* syntax error */
+}
+
+
+static int
+spamd_get_server(spamd_address_container ** spamds, int num_servers)
+{
+unsigned int i;
+spamd_address_container * sd;
+long weights;
+unsigned pri;
+
+/* speedup, if we have only 1 server */
+if (num_servers == 1)
+ return (spamds[0]->is_failed ? -1 : 0);
+
+/* scan for highest pri */
+for (pri = 0, i = 0; i < num_servers; i++)
+ {
+ sd = spamds[i];
+ if (!sd->is_failed && sd->priority > pri) pri = sd->priority;
+ }
+
+/* get sum of weights */
+for (weights = 0, i = 0; i < num_servers; i++)
+ {
+ sd = spamds[i];
+ if (!sd->is_failed && sd->priority == pri) weights += sd->weight;
+ }
+if (weights == 0) /* all servers failed */
+ return -1;
+
+for (long rnd = random_number(weights), i = 0; i < num_servers; i++)
+ {
+ sd = spamds[i];
+ if (!sd->is_failed && sd->priority == pri)
+ if ((rnd -= sd->weight) < 0)
+ return i;
+ }
+
+log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s unknown error (memory/cpu corruption?)", loglabel);
+return -1;
+}
+
+
+int
+spam(const uschar **listptr)
+{
+int sep = 0;
+const uschar *list = *listptr;
+uschar *user_name;
+unsigned long mbox_size;
+FILE *mbox_file;
+client_conn_ctx spamd_cctx = {.sock = -1};
+uschar spamd_buffer[32600];
+int i, j, offset, result;
+uschar spamd_version[8];
+uschar spamd_short_result[8];
+uschar spamd_score_char;
+double spamd_threshold, spamd_score, spamd_reject_score;
+int spamd_report_offset;
+uschar *p,*q;
+int override = 0;
+time_t start;
+size_t read, wrote;
+uschar *spamd_address_work;
+spamd_address_container * sd;
+
+/* stop compiler warning */
+result = 0;
+
+/* find the username from the option list */
+if (!(user_name = string_nextinlist(&list, &sep, NULL, 0)))
+ {
+ /* 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 = spamd_address;
+else if (!(spamd_address_work = expand_string(spamd_address)))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s spamd_address starts with $, but expansion failed: %s",
+ loglabel, expand_string_message);
+ return DEFER;
+ }
+
+DEBUG(D_acl) debug_printf_indent("spamd: addrlist '%s'\n", spamd_address_work);
+
+/* 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)
+ return override ? OK : spam_rc;
+
+/* make sure the eml mbox file is spooled up */
+
+if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
+ { /* error while spooling */
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s error while creating mbox spool file", loglabel);
+ return DEFER;
+ }
+
+start = time(NULL);
+
+ {
+ int num_servers = 0;
+ int current_server;
+ uschar * address;
+ const uschar * spamd_address_list_ptr = spamd_address_work;
+ spamd_address_container * spamd_address_vector[32];
+
+ /* Check how many spamd servers we have
+ and register their addresses */
+ sep = 0; /* default colon-sep */
+ while ((address = string_nextinlist(&spamd_address_list_ptr, &sep, NULL, 0)))
+ {
+ const uschar * sublist;
+ int sublist_sep = -(int)' '; /* default space-sep */
+ unsigned args;
+ uschar * s;
+
+ DEBUG(D_acl) debug_printf_indent("spamd: addr entry '%s'\n", address);
+ sd = store_get(sizeof(spamd_address_container), GET_UNTAINTED);
+
+ for (sublist = address, args = 0, spamd_param_init(sd);
+ (s = string_nextinlist(&sublist, &sublist_sep, NULL, 0));
+ args++
+ )
+ {
+ DEBUG(D_acl) debug_printf_indent("spamd: addr parm '%s'\n", s);
+ switch (args)
+ {
+ case 0: sd->hostspec = s;
+ if (*s == '/') args++; /* local; no port */
+ break;
+ case 1: sd->hostspec = string_sprintf("%s %s", sd->hostspec, s);
+ break;
+ default: spamd_param(s, sd);
+ break;
+ }
+ }
+ if (args < 2)
+ {
+ log_write(0, LOG_MAIN,
+ "%s warning - invalid spamd address: '%s'", loglabel, address);
+ continue;
+ }
+
+ spamd_address_vector[num_servers] = sd;
+ if (++num_servers > 31)
+ break;
+ }
+
+ /* check if we have at least one server */
+ if (!num_servers)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s no useable spamd server addresses in spamd_address configuration option.",
+ loglabel);
+ goto defer;
+ }
+
+ current_server = spamd_get_server(spamd_address_vector, num_servers);
+ sd = spamd_address_vector[current_server];
+ for(;;)
+ {
+ uschar * errstr;
+
+ DEBUG(D_acl) debug_printf_indent("spamd: trying server %s\n", sd->hostspec);
+
+ for (;;)
+ {
+ /*XXX could potentially use TFO early-data here */
+ if ( (spamd_cctx.sock = ip_streamsocket(sd->hostspec, &errstr, 5, NULL)) >= 0
+ || sd->retry <= 0
+ )
+ break;
+ DEBUG(D_acl) debug_printf_indent("spamd: server %s: retry conn\n", sd->hostspec);
+ while (sd->retry > 0) sd->retry = sleep(sd->retry);
+ }
+ if (spamd_cctx.sock >= 0)
+ break;
+
+ log_write(0, LOG_MAIN, "%s spamd: %s", loglabel, errstr);
+ sd->is_failed = TRUE;
+
+ current_server = spamd_get_server(spamd_address_vector, num_servers);
+ if (current_server < 0)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "%s all spamd servers failed", loglabel);
+ goto defer;
+ }
+ sd = spamd_address_vector[current_server];
+ }
+ }
+
+(void)fcntl(spamd_cctx.sock, F_SETFL, O_NONBLOCK);
+/* now we are connected to spamd on spamd_cctx.sock */
+if (sd->is_rspamd)
+ {
+ gstring * req_str;
+ const uschar * s;
+
+ req_str = string_append(NULL, 8,
+ "CHECK RSPAMC/1.3\r\nContent-length: ", string_sprintf("%lu\r\n", mbox_size),
+ "Queue-Id: ", message_id,
+ "\r\nFrom: <", sender_address,
+ ">\r\nRecipient-Number: ", string_sprintf("%d\r\n", recipients_count));
+
+ for (int i = 0; i < recipients_count; i++)
+ req_str = string_append(req_str, 3,
+ "Rcpt: <", recipients_list[i].address, ">\r\n");
+ if ((s = expand_string(US"$sender_helo_name")) && *s)
+ req_str = string_append(req_str, 3, "Helo: ", s, "\r\n");
+ if ((s = expand_string(US"$sender_host_name")) && *s)
+ req_str = string_append(req_str, 3, "Hostname: ", s, "\r\n");
+ if (sender_host_address)
+ req_str = string_append(req_str, 3, "IP: ", sender_host_address, "\r\n");
+ if ((s = expand_string(US"$authenticated_id")) && *s)
+ req_str = string_append(req_str, 3, "User: ", s, "\r\n");
+ req_str = string_catn(req_str, US"\r\n", 2);
+ wrote = send(spamd_cctx.sock, req_str->s, req_str->ptr, 0);
+ }
+else
+ { /* spamassassin variant */
+ int n;
+ uschar * s = string_sprintf(
+ "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n%n",
+ user_name, mbox_size, &n);
+ /* send our request */
+ wrote = send(spamd_cctx.sock, s, n, 0);
+ }
+
+if (wrote == -1)
+ {
+ (void)close(spamd_cctx.sock);
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s spamd %s send failed: %s", loglabel, callout_address, strerror(errno));
+ goto defer;
+ }
+
+/* now send the file */
+/* spamd sometimes accepts connections 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 descriptor to make sure that we
+can write without blocking. Short writes are gracefully handled and if the
+whole transaction 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). Workaround using select() removed 2021/11 (jgh).
+ */
+#ifdef NO_POLL_H
+# error Need poll(2) support
+#endif
+
+(void)fcntl(spamd_cctx.sock, F_SETFL, O_NONBLOCK);
+do
+ {
+ read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
+ if (read > 0)
+ {
+ offset = 0;
+again:
+ result = poll_one_fd(spamd_cctx.sock, POLLOUT, 1000);
+ if (result == -1 && errno == EINTR)
+ goto again;
+ else if (result < 1)
+ {
+ if (result == -1)
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s %s on spamd %s socket", loglabel, callout_address, strerror(errno));
+ else
+ {
+ if (time(NULL) - start < sd->timeout)
+ goto again;
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s timed out writing spamd %s, socket", loglabel, callout_address);
+ }
+ (void)close(spamd_cctx.sock);
+ goto defer;
+ }
+
+ wrote = send(spamd_cctx.sock,spamd_buffer + offset,read - offset,0);
+ if (wrote == -1)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s %s on spamd %s socket", loglabel, callout_address, strerror(errno));
+ (void)close(spamd_cctx.sock);
+ goto 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,
+ "%s error reading spool file: %s", loglabel, strerror(errno));
+ (void)close(spamd_cctx.sock);
+ goto defer;
+ }
+
+(void)fclose(mbox_file);
+
+/* we're done sending, close socket for writing */
+if (!sd->is_rspamd)
+ shutdown(spamd_cctx.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_cctx,
+ spamd_buffer + offset,
+ sizeof(spamd_buffer) - offset - 1,
+ sd->timeout + start)) > 0)
+ offset += i;
+spamd_buffer[offset] = '\0'; /* guard byte */
+
+/* error handling */
+if (i <= 0 && errno != 0)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s error reading from spamd %s, socket: %s", loglabel, callout_address, strerror(errno));
+ (void)close(spamd_cctx.sock);
+ return DEFER;
+ }
+
+/* reading done */
+(void)close(spamd_cctx.sock);
+
+if (sd->is_rspamd)
+ { /* rspamd variant of reply */
+ int r;
+ if ( (r = sscanf(CS spamd_buffer,
+ "RSPAMD/%7s 0 EX_OK\r\nMetric: default; %7s %lf / %lf / %lf\r\n%n",
+ spamd_version, spamd_short_result, &spamd_score, &spamd_threshold,
+ &spamd_reject_score, &spamd_report_offset)) != 5
+ || spamd_report_offset >= offset /* verify within buffer */
+ )
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s cannot parse spamd %s, output: %d", loglabel, callout_address, r);
+ return DEFER;
+ }
+ /* now parse action */
+ p = &spamd_buffer[spamd_report_offset];
+
+ if (Ustrncmp(p, "Action: ", sizeof("Action: ") - 1) == 0)
+ {
+ p += sizeof("Action: ") - 1;
+ q = &spam_action_buffer[0];
+ while (*p && *p != '\r' && (q - spam_action_buffer) < sizeof(spam_action_buffer) - 1)
+ *q++ = *p++;
+ *q = '\0';
+ }
+ }
+else
+ { /* spamassassin */
+ /* 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,
+ "%s cannot parse spamd %s output", loglabel, callout_address);
+ return DEFER;
+ }
+ }
+
+ Ustrcpy(spam_action_buffer,
+ spamd_score >= spamd_threshold ? US"reject" : US"no action");
+ }
+
+/* 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;
+ if (*p++ == '\n')
+ {
+ /* add an extra space after the newline to ensure
+ that it is treated as a header continuation line */
+ *q++ = ' ';
+ }
+ }
+/* NULL-terminate */
+*q-- = '\0';
+/* cut off trailing leftovers */
+while (*q <= ' ')
+ *q-- = '\0';
+
+spam_report = spam_report_buffer;
+spam_action = spam_action_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 */
+spam_rc = spamd_score >= spamd_threshold
+ ? OK /* spam as determined by user's threshold */
+ : FAIL; /* not spam */
+
+/* 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 */
+prev_user_name = user_name;
+spam_ok = 1;
+
+return override
+ ? OK /* always return OK, no matter what the score */
+ : spam_rc;
+
+defer:
+ (void)fclose(mbox_file);
+ return DEFER;
+}
+
+#endif
+/* vi: aw ai sw=2
+*/
diff --git a/src/spam.h b/src/spam.h
new file mode 100644
index 0000000..cc36ffd
--- /dev/null
+++ b/src/spam.h
@@ -0,0 +1,40 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003 - 2015 */
+/* Copyright (c) The Exim Maintainers 2021 */
+/* License: GPL */
+
+/* spam defines */
+
+#ifdef WITH_CONTENT_SCAN
+
+/* timeout for reading and writing spamd */
+#define SPAMD_TIMEOUT 120
+
+/* maximum length of the spam bar, please update the
+ * spec, the max length is mentioned there */
+#define MAX_SPAM_BAR_CHARS 50
+
+/* SHUT_WR seems to be undefined on Unixware ? */
+#ifndef SHUT_WR
+# define SHUT_WR 1
+#endif
+
+/* Defaults */
+#define SPAMD_WEIGHT 1
+#define SPAMD_PRIORITY 1
+
+typedef struct spamd_address_container
+{
+ uschar * hostspec;
+ int is_rspamd:1;
+ int is_failed:1;
+ unsigned int weight;
+ unsigned int timeout;
+ unsigned int retry;
+ unsigned int priority;
+} spamd_address_container;
+
+#endif
diff --git a/src/spf.c b/src/spf.c
new file mode 100644
index 0000000..db6eea3
--- /dev/null
+++ b/src/spf.c
@@ -0,0 +1,420 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* SPF support.
+ Copyright (c) The Exim Maintainers 2015 - 2022
+ 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 SUPPORT_SPF
+
+/* must be kept in numeric order */
+static spf_result_id spf_result_id_list[] = {
+ /* name value */
+ { US"invalid", 0},
+ { US"neutral", 1 },
+ { US"pass", 2 },
+ { US"fail", 3 },
+ { US"softfail", 4 },
+ { US"none", 5 },
+ { 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_dns_rr_t * spf_nxdomain = NULL;
+
+
+gstring *
+spf_lib_version_report(gstring * g)
+{
+int maj, min, patch;
+
+SPF_get_lib_version(&maj, &min, &patch);
+g = string_fmt_append(g, "Library version: spf2: Compile: %d.%d.%d\n",
+ SPF_LIB_VERSION_MAJOR, SPF_LIB_VERSION_MINOR, SPF_LIB_VERSION_PATCH);
+g = string_fmt_append(g, " Runtime: %d.%d.%d\n",
+ maj, min, patch);
+return g;
+}
+
+
+
+static SPF_dns_rr_t *
+SPF_dns_exim_lookup(SPF_dns_server_t *spf_dns_server,
+ const char *domain, ns_type rr_type, int should_cache)
+{
+dns_answer * dnsa = store_get_dns_answer();
+dns_scan dnss;
+SPF_dns_rr_t * spfrr;
+unsigned found = 0;
+
+SPF_dns_rr_t srr = {
+ .domain = CS domain, /* query information */
+ .domain_buf_len = 0,
+ .rr_type = rr_type,
+
+ .rr_buf_len = 0, /* answer information */
+ .rr_buf_num = 0, /* no free of s */
+ .utc_ttl = 0,
+
+ .hook = NULL, /* misc information */
+ .source = spf_dns_server
+};
+int dns_rc;
+
+DEBUG(D_receive) debug_printf("SPF_dns_exim_lookup '%s'\n", domain);
+
+/* Shortcircuit SPF RR lookups by returning NO_DATA. They were obsoleted by
+RFC 6686/7208 years ago. see bug #1294 */
+
+if (rr_type == T_SPF)
+ {
+ HDEBUG(D_host_lookup) debug_printf("faking NO_DATA for SPF RR(99) lookup\n");
+ srr.herrno = NO_DATA;
+ SPF_dns_rr_dup(&spfrr, &srr);
+ store_free_dns_answer(dnsa);
+ return spfrr;
+ }
+
+switch (dns_rc = dns_lookup(dnsa, US domain, rr_type, NULL))
+ {
+ case DNS_SUCCEED: srr.herrno = NETDB_SUCCESS; break;
+ case DNS_AGAIN: srr.herrno = TRY_AGAIN; break;
+ case DNS_NOMATCH: srr.herrno = HOST_NOT_FOUND; break;
+ case DNS_NODATA: srr.herrno = NO_DATA; break;
+ case DNS_FAIL:
+ default: srr.herrno = NO_RECOVERY; break;
+ }
+
+for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT))
+ if (rr->type == rr_type) found++;
+
+if (found == 0)
+ {
+ SPF_dns_rr_dup(&spfrr, &srr);
+ store_free_dns_answer(dnsa);
+ return spfrr;
+ }
+
+srr.rr = store_malloc(sizeof(SPF_dns_rr_data_t) * found);
+
+found = 0;
+for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT))
+ if (rr->type == rr_type)
+ {
+ const uschar * s = rr->data;
+
+ srr.ttl = rr->ttl;
+ switch(rr_type)
+ {
+ case T_MX:
+ s += 2; /* skip the MX precedence field */
+ case T_PTR:
+ {
+ uschar * buf = store_malloc(256);
+ (void)dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen, s,
+ (DN_EXPAND_ARG4_TYPE)buf, 256);
+ s = buf;
+ break;
+ }
+
+ case T_TXT:
+ {
+ gstring * g = NULL;
+ uschar chunk_len;
+
+ if (strncmpic(rr->data+1, US SPF_VER_STR, 6) != 0)
+ {
+ HDEBUG(D_host_lookup) debug_printf("not an spf record: %.*s\n",
+ (int) s[0], s+1);
+ continue;
+ }
+
+ for (int off = 0; off < rr->size; off += chunk_len)
+ {
+ if (!(chunk_len = s[off++])) break;
+ g = string_catn(g, s+off, chunk_len);
+ }
+ if (!g)
+ continue;
+ gstring_release_unused(g);
+ s = string_copy_malloc(string_from_gstring(g));
+ DEBUG(D_receive) debug_printf("SPF_dns_exim_lookup '%s'\n", s);
+ break;
+ }
+
+ case T_A:
+ case T_AAAA:
+ default:
+ {
+ uschar * buf = store_malloc(dnsa->answerlen + 1);
+ s = memcpy(buf, s, dnsa->answerlen + 1);
+ break;
+ }
+ }
+ srr.rr[found++] = (void *) s;
+ }
+
+/* Did we filter out all TXT RRs? Return NO_DATA instead of SUCCESS with
+empty ANSWER section. */
+
+if (!(srr.num_rr = found))
+ srr.herrno = NO_DATA;
+
+/* spfrr->rr must have been malloc()d for this */
+SPF_dns_rr_dup(&spfrr, &srr);
+store_free_dns_answer(dnsa);
+return spfrr;
+}
+
+
+
+SPF_dns_server_t *
+SPF_dns_exim_new(int debug)
+{
+SPF_dns_server_t * spf_dns_server = store_malloc(sizeof(SPF_dns_server_t));
+
+DEBUG(D_receive) debug_printf("SPF_dns_exim_new\n");
+
+memset(spf_dns_server, 0, sizeof(SPF_dns_server_t));
+spf_dns_server->destroy = NULL;
+spf_dns_server->lookup = SPF_dns_exim_lookup;
+spf_dns_server->get_spf = NULL;
+spf_dns_server->get_exp = NULL;
+spf_dns_server->add_cache = NULL;
+spf_dns_server->layer_below = NULL;
+spf_dns_server->name = "exim";
+spf_dns_server->debug = debug;
+
+/* XXX This might have to return NO_DATA sometimes. */
+
+spf_nxdomain = SPF_dns_rr_new_init(spf_dns_server,
+ "", ns_t_any, 24 * 60 * 60, HOST_NOT_FOUND);
+if (!spf_nxdomain)
+ {
+ free(spf_dns_server);
+ return NULL;
+ }
+
+return spf_dns_server;
+}
+
+
+
+
+/* Construct the SPF library stack.
+ Return: Boolean success.
+*/
+
+BOOL
+spf_init(void)
+{
+SPF_dns_server_t * dc;
+int debug = 0;
+const uschar *s;
+
+DEBUG(D_receive) debug = 1;
+
+/* We insert our own DNS access layer rather than letting the spf library
+do it, so that our dns access path is used for debug tracing and for the
+testsuite. */
+
+if (!(dc = SPF_dns_exim_new(debug)))
+ {
+ DEBUG(D_receive) debug_printf("spf: SPF_dns_exim_new() failed\n");
+ return FALSE;
+ }
+if (!(dc = SPF_dns_cache_new(dc, NULL, debug, 8)))
+ {
+ DEBUG(D_receive) debug_printf("spf: SPF_dns_cache_new() failed\n");
+ return FALSE;
+ }
+if (!(spf_server = SPF_server_new_dns(dc, debug)))
+ {
+ DEBUG(D_receive) debug_printf("spf: SPF_server_new() failed.\n");
+ return FALSE;
+ }
+ /* Override the outdated explanation URL.
+ See https://www.mail-archive.com/mailop@mailop.org/msg08019.html
+ Used to work as "Please%_see%_http://www.open-spf.org/Why?id=%{S}&ip=%{C}&receiver=%{R}",
+ but is broken now (May 18th, 2020) */
+if (!(s = expand_string(spf_smtp_comment_template)))
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "expansion of spf_smtp_comment_template failed");
+
+SPF_server_set_explanation(spf_server, CCS s, &spf_response);
+if (SPF_response_errcode(spf_response) != SPF_E_SUCCESS)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s", SPF_strerror(SPF_response_errcode(spf_response)));
+
+return TRUE;
+}
+
+
+/* Set 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).
+
+Return: Boolean success
+*/
+
+BOOL
+spf_conn_init(uschar * spf_helo_domain, uschar * spf_remote_addr)
+{
+DEBUG(D_receive)
+ debug_printf("spf_conn_init: %s %s\n", spf_helo_domain, spf_remote_addr);
+
+if (!spf_server && !spf_init()) return FALSE;
+
+if (SPF_server_set_rec_dom(spf_server, CS primary_hostname))
+ {
+ DEBUG(D_receive) debug_printf("spf: SPF_server_set_rec_dom(\"%s\") failed.\n",
+ primary_hostname);
+ spf_server = NULL;
+ return FALSE;
+ }
+
+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(D_receive)
+ 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 FALSE;
+ }
+
+if (SPF_request_set_helo_dom(spf_request, CS spf_helo_domain))
+ {
+ DEBUG(D_receive) debug_printf("spf: SPF_set_helo_dom(\"%s\") failed.\n",
+ spf_helo_domain);
+ spf_server = NULL;
+ spf_request = NULL;
+ return FALSE;
+ }
+
+return TRUE;
+}
+
+
+void
+spf_response_debug(SPF_response_t * spf_response)
+{
+if (SPF_response_messages(spf_response) == 0)
+ debug_printf(" (no errors)\n");
+else for (int i = 0; i < SPF_response_messages(spf_response); i++)
+ {
+ SPF_error_t * err = SPF_response_message(spf_response, i);
+ debug_printf( "%s_msg = (%d) %s\n",
+ (SPF_error_errorp(err) ? "warn" : "err"),
+ SPF_error_code(err),
+ SPF_error_message(err));
+ }
+}
+
+
+/* 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.
+
+Return: OK/FAIL */
+
+int
+spf_process(const uschar **listptr, uschar *spf_envelope_sender, int action)
+{
+int sep = 0;
+const uschar *list = *listptr;
+uschar *spf_result_id;
+int rc = SPF_RESULT_PERMERROR;
+
+DEBUG(D_receive) debug_printf("spf_process\n");
+
+if (!(spf_server && spf_request))
+ /* no global context, assume temp error and skip to evaluation */
+ rc = SPF_RESULT_PERMERROR;
+
+else if (SPF_request_set_env_from(spf_request, CS spf_envelope_sender))
+ /* Invalid sender address. This should be a real rare occurrence */
+ rc = SPF_RESULT_PERMERROR;
+
+else
+ {
+ /* get SPF result */
+ if (action == SPF_PROCESS_FALLBACK)
+ {
+ SPF_request_query_fallback(spf_request, &spf_response, CS spf_guess);
+ spf_result_guessed = TRUE;
+ }
+ else
+ SPF_request_query_mailfrom(spf_request, &spf_response);
+
+ /* set up expansion items */
+ spf_header_comment = US SPF_response_get_header_comment(spf_response);
+ spf_received = US SPF_response_get_received_spf(spf_response);
+ spf_result = US SPF_strresult(SPF_response_result(spf_response));
+ spf_smtp_comment = US SPF_response_get_smtp_comment(spf_response);
+
+ rc = SPF_response_result(spf_response);
+
+ DEBUG(D_acl) spf_response_debug(spf_response);
+ }
+
+/* We got a result. Now see if we should return OK or FAIL for it */
+DEBUG(D_acl) 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, NULL, 0)))
+ {
+ BOOL negate, result;
+
+ if ((negate = spf_result_id[0] == '!'))
+ spf_result_id++;
+
+ result = Ustrcmp(spf_result_id, spf_result_id_list[rc].name) == 0;
+ if (negate != result) return OK;
+ }
+
+/* no match */
+return FAIL;
+}
+
+
+
+gstring *
+authres_spf(gstring * g)
+{
+uschar * s;
+if (!spf_result) return g;
+
+g = string_append(g, 2, US";\n\tspf=", spf_result);
+if (spf_result_guessed)
+ g = string_cat(g, US" (best guess record for domain)");
+
+s = expand_string(US"$sender_address_domain");
+if (s && *s)
+ return string_append(g, 2, US" smtp.mailfrom=", s);
+
+s = sender_helo_name;
+return s && *s
+ ? string_append(g, 2, US" smtp.helo=", s)
+ : string_cat(g, US" smtp.mailfrom=<>");
+}
+
+
+#endif
diff --git a/src/spf.h b/src/spf.h
new file mode 100644
index 0000000..f32d069
--- /dev/null
+++ b/src/spf.h
@@ -0,0 +1,38 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Experimental SPF support.
+ Copyright (c) The Exim Maintainers 2016 - 2022
+ Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004
+ License: GPL
+*/
+
+#ifdef SUPPORT_SPF
+
+/* Yes, we do have ns_type. spf.h redefines it if we don't set this. Doh */
+#if !defined(HAVE_NS_TYPE) && defined(NS_INADDRSZ)
+# 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 */
+gstring * spf_lib_version_report(gstring *);
+BOOL spf_init(void);
+BOOL spf_conn_init(uschar *, uschar *);
+int spf_process(const uschar **, uschar *, int);
+void spf_response_debug(SPF_response_t *);
+
+#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
index 0000000..2aa0b0b
--- /dev/null
+++ b/src/spool_in.c
@@ -0,0 +1,1094 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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.
+
+As called by deliver_message() (at least) we are operating as root.
+
+Argument: the id of the message
+Returns: fd if file successfully opened and locked, else -1
+
+Side effect: message_subdir is set for the (possibly split) spool directory
+*/
+
+int
+spool_open_datafile(uschar *id)
+{
+struct stat statbuf;
+flock_t lock_data;
+int fd;
+
+/* 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 (int i = 0; i < 2; i++)
+ {
+ uschar * fname;
+ int save_errno;
+
+ set_subdir_str(message_subdir, id, i);
+ fname = spool_fname(US"input", message_subdir, id, US"-D");
+ DEBUG(D_deliver) debug_printf_indent("Trying spool file %s\n", fname);
+
+ /* We protect against symlink attacks both in not propagating the
+ * file-descriptor to other processes as we exec, and also ensuring that we
+ * don't even open symlinks.
+ * No -D file inside the spool area should be a symlink.
+ */
+ if ((fd = Uopen(fname,
+#ifdef O_CLOEXEC
+ O_CLOEXEC |
+#endif
+#ifdef O_NOFOLLOW
+ O_NOFOLLOW |
+#endif
+ O_RDWR | O_APPEND, 0)) >= 0)
+ break;
+ save_errno = errno;
+ if (errno == ENOENT)
+ {
+ if (i == 0) continue;
+ if (!f.queue_running)
+ log_write(0, LOG_MAIN, "Spool%s%s file %s-D not found",
+ *queue_name ? US" Q=" : US"",
+ *queue_name ? queue_name : US"",
+ id);
+ }
+ else
+ log_write(0, LOG_MAIN, "Spool error for %s: %s", fname, strerror(errno));
+ errno = save_errno;
+ return -1;
+ }
+
+/* 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. */
+
+#ifndef O_CLOEXEC
+(void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+#endif
+
+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(fd, F_SETLK, &lock_data) < 0)
+ {
+ log_write(L_skip_delivery, LOG_MAIN,
+ "Spool file for %s is locked (another process is handling this message)",
+ id);
+ (void)close(fd);
+ errno = 0;
+ return -1;
+ }
+
+/* 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(fd, &statbuf) == 0)
+ {
+ message_body_size = statbuf.st_size - SPOOL_DATA_START_OFFSET;
+ message_size = message_body_size + 1;
+ }
+
+return fd;
+}
+#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, GET_TAINTED); /* rcpt names tainted */
+*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;
+}
+
+
+
+
+/* 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. */
+
+void
+spool_clear_header_globals(void)
+{
+acl_var_c = acl_var_m = NULL;
+authenticated_id = NULL;
+authenticated_sender = NULL;
+f.allow_unqualified_recipient = FALSE;
+f.allow_unqualified_sender = FALSE;
+body_linecount = 0;
+body_zerocount = 0;
+f.deliver_firsttime = FALSE;
+f.deliver_freeze = FALSE;
+deliver_frozen_at = 0;
+f.deliver_manual_thaw = FALSE;
+/* f.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;
+f.local_error_message = FALSE;
+#ifdef HAVE_LOCAL_SCAN
+local_scan_data = NULL;
+#endif
+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 = sender_host_auth_pubname = NULL;
+sender_ident = NULL;
+f.sender_local = FALSE;
+f.sender_set_untrusted = FALSE;
+smtp_active_hostname = primary_hostname;
+#ifndef COMPILE_UTILITY
+f.spool_file_wireformat = FALSE;
+#endif
+tree_nonrecipients = NULL;
+
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+bmi_run = 0;
+bmi_verdicts = NULL;
+#endif
+
+#ifndef DISABLE_DKIM
+dkim_signers = NULL;
+f.dkim_disable_verify = FALSE;
+dkim_collect_input = 0;
+#endif
+
+#ifndef DISABLE_TLS
+tls_in.certificate_verified = FALSE;
+# ifdef SUPPORT_DANE
+tls_in.dane_verified = FALSE;
+# endif
+tls_in.ver = tls_in.cipher = NULL;
+# ifndef COMPILE_UTILITY /* tls support fns not built in */
+tls_free_cert(&tls_in.ourcert);
+tls_free_cert(&tls_in.peercert);
+# endif
+tls_in.peerdn = NULL;
+tls_in.sni = NULL;
+tls_in.ocsp = OCSP_NOT_REQ;
+#endif
+
+#ifdef WITH_CONTENT_SCAN
+spam_bar = NULL;
+spam_score = NULL;
+spam_score_int = NULL;
+#endif
+
+#if defined(SUPPORT_I18N) && !defined(COMPILE_UTILITY)
+message_smtputf8 = FALSE;
+message_utf8_downconvert = 0;
+#endif
+
+#ifndef COMPILE_UTILITY
+debuglog_name[0] = '\0';
+#endif
+dsn_ret = 0;
+dsn_envid = NULL;
+}
+
+static void *
+fgets_big_buffer(FILE *fp)
+{
+int len = 0;
+
+big_buffer[0] = 0;
+if (Ufgets(big_buffer, big_buffer_size, fp) == NULL) return NULL;
+
+while ((len = Ustrlen(big_buffer)) == big_buffer_size-1
+ && big_buffer[len-1] != '\n')
+ {
+ uschar *newbuffer;
+ int newsize;
+
+ if (big_buffer_size >= BIG_BUFFER_SIZE * 4) return NULL;
+ newsize = big_buffer_size * 2;
+ newbuffer = store_get_perm(newsize, FALSE);
+ memcpy(newbuffer, big_buffer, len);
+
+ big_buffer = newbuffer;
+ big_buffer_size = newsize;
+ if (Ufgets(big_buffer + len, big_buffer_size - len, fp) == NULL) return NULL;
+ }
+
+if (len <= 0 || big_buffer[len-1] != '\n') return NULL;
+return big_buffer;
+}
+
+
+
+/*************************************************
+* 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.
+
+As called from deliver_message() (at least) we are running as root.
+
+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_hdrerror error in the header portion
+*/
+
+int
+spool_read_header(uschar *name, BOOL read_headers, BOOL subdir_set)
+{
+FILE * fp = NULL;
+int n;
+int rcount = 0;
+long int uid, gid;
+BOOL inheader = FALSE;
+
+/* 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. */
+
+spool_clear_header_globals();
+
+/* 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 (int n = 0; n < 2; n++)
+ {
+ if (!subdir_set)
+ set_subdir_str(message_subdir, name, n);
+
+ if ((fp = Ufopen(spool_fname(US"input", message_subdir, name, US""), "rb")))
+ break;
+ if (n != 0 || subdir_set || errno != ENOENT)
+ return spool_read_notopen;
+ }
+
+errno = 0;
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_deliver) debug_printf_indent("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, fp) == 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, fp) == NULL) goto SPOOL_READ_ERROR;
+
+ {
+ uschar *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;
+
+/* envelope from */
+if (Ufgets(big_buffer, big_buffer_size, fp) == 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, GET_TAINTED);
+Ustrncpy(sender_address, big_buffer+1, n-3);
+sender_address[n-3] = 0;
+
+/* time */
+if (Ufgets(big_buffer, big_buffer_size, fp) == NULL) goto SPOOL_READ_ERROR;
+if (sscanf(CS big_buffer, TIME_T_FMT " %d", &received_time.tv_sec, &warning_count) != 2)
+ goto SPOOL_FORMAT_ERROR;
+received_time.tv_usec = 0;
+received_time_complete = received_time;
+
+
+message_age = time(NULL) - received_time.tv_sec;
+#ifndef COMPILE_UTILITY
+if (f.running_in_test_harness)
+ message_age = test_harness_fudged_queue_time(message_age);
+#endif
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_deliver) debug_printf_indent("user=%s uid=%ld gid=%ld sender=%s\n",
+ originator_login, (long int)originator_uid, (long int)originator_gid,
+ sender_address);
+#endif
+
+/* 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.
+
+If the line starts with "--" the content of the variable is tainted.
+If the line start "--(<lookuptype>)" it is also quoted for the given <lookuptype>.
+*/
+
+for (;;)
+ {
+ const void * proto_mem;
+ uschar * var;
+ const uschar * p;
+
+ if (fgets_big_buffer(fp) == NULL) goto SPOOL_READ_ERROR;
+ if (big_buffer[0] != '-') break;
+ big_buffer[Ustrlen(big_buffer)-1] = 0;
+
+ proto_mem = big_buffer[1] == '-' ? GET_TAINTED : GET_UNTAINTED;
+ var = big_buffer + (proto_mem == GET_UNTAINTED ? 1 : 2);
+ if (*var == '(') /* marker for quoted value */
+ {
+ uschar * s;
+ int idx;
+ for (s = ++var; *s != ')'; ) s++;
+#ifndef COMPILE_UTILITY
+ if ((idx = search_findtype(var, s - var)) < 0)
+ {
+ DEBUG(D_any) debug_printf("Unrecognised quoter %.*s\n", (int)(s - var), var+1);
+ goto SPOOL_FORMAT_ERROR;
+ }
+ proto_mem = store_get_quoted(1, GET_TAINTED, idx);
+#endif /* COMPILE_UTILITY */
+ var = s + 1;
+ }
+ p = var + 1;
+
+ switch(*var)
+ {
+ 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(var + 5, ' ');
+ if (endptr == NULL) goto SPOOL_FORMAT_ERROR;
+ name = string_sprintf("%c%.*s", var[3],
+ (int)(endptr - var - 5), var + 5);
+ if (sscanf(CS endptr, " %d", &count) != 1) goto SPOOL_FORMAT_ERROR;
+ node = acl_var_create(name);
+ node->data.ptr = store_get(count + 1, proto_mem);
+ if (fread(node->data.ptr, 1, count+1, fp) < count) goto SPOOL_READ_ERROR;
+ ((uschar*)node->data.ptr)[count] = 0;
+ }
+
+ else if (Ustrcmp(p, "llow_unqualified_recipient") == 0)
+ f.allow_unqualified_recipient = TRUE;
+ else if (Ustrcmp(p, "llow_unqualified_sender") == 0)
+ f.allow_unqualified_sender = TRUE;
+
+ else if (Ustrncmp(p, "uth_id", 6) == 0)
+ authenticated_id = string_copy_taint(var + 8, proto_mem);
+ else if (Ustrncmp(p, "uth_sender", 10) == 0)
+ authenticated_sender = string_copy_taint(var + 12, proto_mem);
+ else if (Ustrncmp(p, "ctive_hostname", 14) == 0)
+ smtp_active_hostname = string_copy_taint(var + 16, proto_mem);
+
+ /* 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)
+ {
+ unsigned index, count;
+ uschar name[20]; /* Need plenty of space for %u format */
+ tree_node * node;
+ if ( sscanf(CS var + 4, "%u %u", &index, &count) != 2
+ || index >= 20
+ || count > 16384 /* arbitrary limit on variable size */
+ )
+ goto SPOOL_FORMAT_ERROR;
+ if (index < 10)
+ (void) string_format(name, sizeof(name), "%c%u", 'c', index);
+ else
+ (void) string_format(name, sizeof(name), "%c%u", 'm', index - 10);
+ node = acl_var_create(name);
+ node->data.ptr = store_get(count + 1, proto_mem);
+ /* We sanity-checked the count, so disable the Coverity error */
+ /* coverity[tainted_data] */
+ if (fread(node->data.ptr, 1, count+1, fp) < count) goto SPOOL_READ_ERROR;
+ (US node->data.ptr)[count] = '\0';
+ }
+ break;
+
+ case 'b':
+ if (Ustrncmp(p, "ody_linecount", 13) == 0)
+ body_linecount = Uatoi(var + 14);
+ else if (Ustrncmp(p, "ody_zerocount", 13) == 0)
+ body_zerocount = Uatoi(var + 14);
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+ else if (Ustrncmp(p, "mi_verdicts ", 12) == 0)
+ bmi_verdicts = string_copy_taint(var + 13, proto_mem);
+#endif
+ break;
+
+ case 'd':
+ if (Ustrcmp(p, "eliver_firsttime") == 0)
+ f.deliver_firsttime = TRUE;
+ else if (Ustrncmp(p, "sn_ret", 6) == 0)
+ dsn_ret= atoi(CS var + 7);
+ else if (Ustrncmp(p, "sn_envid", 8) == 0)
+ dsn_envid = string_copy_taint(var + 10, proto_mem);
+#ifndef COMPILE_UTILITY
+ else if (Ustrncmp(p, "ebug_selector ", 14) == 0)
+ debug_selector = strtol(CS var + 15, NULL, 0);
+ else if (Ustrncmp(p, "ebuglog_name ", 13) == 0)
+ debug_logging_from_spool(var + 14);
+#endif
+ break;
+
+ case 'f':
+ if (Ustrncmp(p, "rozen", 5) == 0)
+ {
+ f.deliver_freeze = TRUE;
+ if (sscanf(CS var+6, TIME_T_FMT, &deliver_frozen_at) != 1)
+ goto SPOOL_READ_ERROR;
+ }
+ 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_pubname", 16) == 0)
+ sender_host_auth_pubname = string_copy_taint(var + 18, proto_mem);
+ else if (Ustrncmp(p, "ost_auth", 8) == 0)
+ sender_host_authenticated = string_copy_taint(var + 10, proto_mem);
+ else if (Ustrncmp(p, "ost_name", 8) == 0)
+ sender_host_name = string_copy_taint(var + 10, proto_mem);
+ else if (Ustrncmp(p, "elo_name", 8) == 0)
+ sender_helo_name = string_copy_taint(var + 10, proto_mem);
+
+ /* 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(var + 13);
+ sender_host_address = string_copy_taint(var + 13, proto_mem);
+ }
+ break;
+
+ case 'i':
+ if (Ustrncmp(p, "nterface_address", 16) == 0)
+ {
+ interface_port = host_address_extract_port(var + 18);
+ interface_address = string_copy_taint(var + 18, proto_mem);
+ }
+ else if (Ustrncmp(p, "dent", 4) == 0)
+ sender_ident = string_copy_taint(var + 6, proto_mem);
+ break;
+
+ case 'l':
+ if (Ustrcmp(p, "ocal") == 0)
+ f.sender_local = TRUE;
+ else if (Ustrcmp(var, "localerror") == 0)
+ f.local_error_message = TRUE;
+#ifdef HAVE_LOCAL_SCAN
+ else if (Ustrncmp(p, "ocal_scan ", 10) == 0)
+ local_scan_data = string_copy_taint(var + 11, proto_mem);
+#endif
+ break;
+
+ case 'm':
+ if (Ustrcmp(p, "anual_thaw") == 0)
+ f.deliver_manual_thaw = TRUE;
+ else if (Ustrncmp(p, "ax_received_linelength", 22) == 0)
+ max_received_linelength = Uatoi(var + 23);
+ break;
+
+ case 'N':
+ if (*p == 0) f.dont_deliver = TRUE; /* -N */
+ break;
+
+ case 'r':
+ if (Ustrncmp(p, "eceived_protocol", 16) == 0)
+ received_protocol = string_copy_taint(var + 18, proto_mem);
+ else if (Ustrncmp(p, "eceived_time_usec", 17) == 0)
+ {
+ unsigned usec;
+ if (sscanf(CS var + 20, "%u", &usec) == 1)
+ {
+ received_time.tv_usec = usec;
+ if (!received_time_complete.tv_sec) received_time_complete.tv_usec = usec;
+ }
+ }
+ else if (Ustrncmp(p, "eceived_time_complete", 21) == 0)
+ {
+ unsigned sec, usec;
+ if (sscanf(CS var + 23, "%u.%u", &sec, &usec) == 2)
+ {
+ received_time_complete.tv_sec = sec;
+ received_time_complete.tv_usec = usec;
+ }
+ }
+ break;
+
+ case 's':
+ if (Ustrncmp(p, "ender_set_untrusted", 19) == 0)
+ f.sender_set_untrusted = TRUE;
+#ifdef WITH_CONTENT_SCAN
+ else if (Ustrncmp(p, "pam_bar ", 8) == 0)
+ spam_bar = string_copy_taint(var + 9, proto_mem);
+ else if (Ustrncmp(p, "pam_score ", 10) == 0)
+ spam_score = string_copy_taint(var + 11, proto_mem);
+ else if (Ustrncmp(p, "pam_score_int ", 14) == 0)
+ spam_score_int = string_copy_taint(var + 15, proto_mem);
+#endif
+#ifndef COMPILE_UTILITY
+ else if (Ustrncmp(p, "pool_file_wireformat", 20) == 0)
+ f.spool_file_wireformat = TRUE;
+#endif
+#if defined(SUPPORT_I18N) && !defined(COMPILE_UTILITY)
+ else if (Ustrncmp(p, "mtputf8", 7) == 0)
+ message_smtputf8 = TRUE;
+#endif
+ break;
+
+#ifndef DISABLE_TLS
+ case 't':
+ if (Ustrncmp(p, "ls_", 3) == 0)
+ {
+ const uschar * q = p + 3;
+ if (Ustrncmp(q, "certificate_verified", 20) == 0)
+ tls_in.certificate_verified = TRUE;
+ else if (Ustrncmp(q, "cipher", 6) == 0)
+ tls_in.cipher = string_copy_taint(q+7, proto_mem);
+# ifndef COMPILE_UTILITY /* tls support fns not built in */
+ else if (Ustrncmp(q, "ourcert", 7) == 0)
+ (void) tls_import_cert(q+8, &tls_in.ourcert);
+ else if (Ustrncmp(q, "peercert", 8) == 0)
+ (void) tls_import_cert(q+9, &tls_in.peercert);
+# endif
+ else if (Ustrncmp(q, "peerdn", 6) == 0)
+ tls_in.peerdn = string_unprinting(string_copy_taint(q+7, proto_mem));
+ else if (Ustrncmp(q, "sni", 3) == 0)
+ tls_in.sni = string_unprinting(string_copy_taint(q+4, proto_mem));
+ else if (Ustrncmp(q, "ocsp", 4) == 0)
+ tls_in.ocsp = q[5] - '0';
+# ifndef DISABLE_TLS_RESUME
+ else if (Ustrncmp(q, "resumption", 10) == 0)
+ tls_in.resumption = q[11] - 'A';
+# endif
+ else if (Ustrncmp(q, "ver", 3) == 0)
+ tls_in.ver = string_copy_taint(q+4, proto_mem);
+ }
+ break;
+#endif
+
+#if defined(SUPPORT_I18N) && !defined(COMPILE_UTILITY)
+ case 'u':
+ if (Ustrncmp(p, "tf8_downcvt", 11) == 0)
+ message_utf8_downconvert = 1;
+ else if (Ustrncmp(p, "tf8_optdowncvt", 15) == 0)
+ message_utf8_downconvert = -1;
+ 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_indent("sender_local=%d ident=%s\n", f.sender_local,
+ sender_ident ? sender_ident : US"unset");
+#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, fp, big_buffer, big_buffer_size))
+ goto SPOOL_FORMAT_ERROR;
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_deliver) debug_print_tree("Non-recipients", 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.
+Apply an arbitrary sanity check.*/
+
+if (Ufgets(big_buffer, big_buffer_size, fp) == NULL) goto SPOOL_READ_ERROR;
+if (sscanf(CS big_buffer, "%d", &rcount) != 1 || rcount > 16384)
+ goto SPOOL_FORMAT_ERROR;
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_deliver) debug_printf_indent("recipients_count=%d\n", rcount);
+#endif /* COMPILE_UTILITY */
+
+recipients_list_max = rcount;
+recipients_list = store_get(rcount * sizeof(recipient_item), GET_UNTAINTED);
+
+/* We sanitised the count and know we have enough memory, so disable
+the Coverity error on recipients_count */
+/* coverity[tainted_data] */
+
+for (recipients_count = 0; recipients_count < rcount; recipients_count++)
+ {
+ int nn;
+ int pno = -1;
+ int dsn_flags = 0;
+ uschar *orcpt = NULL;
+ uschar *errors_to = NULL;
+ uschar *p;
+
+ if (fgets_big_buffer(fp) == 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;
+#if !defined (COMPILE_UTILITY)
+ DEBUG(D_deliver) debug_printf_indent("**** SPOOL_IN - Exim 3 spool file\n");
+#endif
+ 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 == ' ')
+ {
+#if !defined (COMPILE_UTILITY)
+ DEBUG(D_deliver) debug_printf_indent("**** SPOOL_IN - early Exim 4 spool file\n");
+#endif
+ *p++ = 0;
+ (void)sscanf(CS p, "%d", &pno);
+ }
+
+ /* Handle current format Exim 4 spool files */
+
+ else if (*p == '#')
+ {
+ int flags;
+
+#if !defined (COMPILE_UTILITY)
+ DEBUG(D_deliver) debug_printf_indent("**** SPOOL_IN - Exim standard format spoolfile\n");
+#endif
+
+ (void)sscanf(CS p+1, "%d", &flags);
+
+ if (flags & 0x01) /* 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_taint(p, GET_TAINTED);
+ }
+ }
+
+ *--p = 0; /* Terminate address */
+ if (flags & 0x02) /* 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_taint(p, GET_TAINTED);
+ }
+ }
+
+ *--p = 0; /* Terminate address */
+ }
+#if !defined(COMPILE_UTILITY)
+ else
+ { DEBUG(D_deliver) debug_printf_indent("**** SPOOL_IN - No additional fields\n"); }
+
+ if (orcpt || dsn_flags)
+ DEBUG(D_deliver) debug_printf_indent("**** SPOOL_IN - address: <%s> orcpt: <%s> dsn_flags: 0x%x\n",
+ big_buffer, orcpt, dsn_flags);
+ if (errors_to)
+ DEBUG(D_deliver) debug_printf_indent("**** SPOOL_IN - address: <%s> errorsto: <%s>\n",
+ big_buffer, errors_to);
+#endif
+
+ recipients_list[recipients_count].address = string_copy_taint(big_buffer, GET_TAINTED);
+ recipients_list[recipients_count].pno = pno;
+ recipients_list[recipients_count].errors_to = errors_to;
+ recipients_list[recipients_count].orcpt = orcpt;
+ recipients_list[recipients_count].dsn_flags = dsn_flags;
+ }
+
+/* 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, fp) == NULL) goto SPOOL_READ_ERROR;
+if (big_buffer[0] != '\n') goto SPOOL_FORMAT_ERROR;
+
+while ((n = fgetc(fp)) != EOF)
+ {
+ header_line *h;
+ uschar flag[4];
+ int i;
+
+ if (!isdigit(n)) goto SPOOL_FORMAT_ERROR;
+ if(ungetc(n, fp) == EOF || fscanf(fp, "%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), GET_UNTAINTED);
+ h->next = NULL;
+ h->type = flag[0];
+ h->slen = n;
+ h->text = store_get(n+1, GET_TAINTED);
+
+ if (h->type == htype_received) received_count++;
+
+ if (header_list) header_last->next = h;
+ else header_list = h;
+ header_last = h;
+
+ for (i = 0; i < n; i++)
+ {
+ int c = fgetc(fp);
+ 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(fp);
+ 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_indent("body_linecount=%d message_linecount=%d\n",
+ body_linecount, message_linecount);
+#endif /* COMPILE_UTILITY */
+
+message_linecount += body_linecount;
+
+fclose(fp);
+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(fp);
+ 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(fp);
+errno = ERRNO_SPOOLFORMAT;
+return inheader? spool_read_hdrerror : spool_read_enverror;
+}
+
+
+#ifndef COMPILE_UTILITY
+/* Read out just the (envelope) sender string from the spool -H file.
+Remove the <> wrap and return it in allocated store. Return NULL on error.
+
+We assume that message_subdir is already set.
+*/
+
+uschar *
+spool_sender_from_msgid(const uschar * id)
+{
+uschar * name = string_sprintf("%s-H", id);
+FILE * fp;
+int n;
+uschar * yield = NULL;
+
+if (!(fp = Ufopen(spool_fname(US"input", message_subdir, name, US""), "rb")))
+ return NULL;
+
+DEBUG(D_deliver) debug_printf_indent("reading spool file %s\n", name);
+
+/* Skip the line with the copy of the filename, then the line with login/uid/gid.
+Read the next line, which should be the envelope sender.
+Do basic validation on that. */
+
+if ( Ufgets(big_buffer, big_buffer_size, fp) != NULL
+ && Ufgets(big_buffer, big_buffer_size, fp) != NULL
+ && Ufgets(big_buffer, big_buffer_size, fp) != NULL
+ && (n = Ustrlen(big_buffer)) >= 3
+ && big_buffer[0] == '<' && big_buffer[n-2] == '>'
+ )
+ {
+ yield = store_get(n-2, GET_TAINTED);
+ Ustrncpy(yield, big_buffer+1, n-3);
+ yield[n-3] = 0;
+ }
+fclose(fp);
+return yield;
+}
+#endif /* COMPILE_UTILITY */
+
+/* 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
index 0000000..8b2aae3
--- /dev/null
+++ b/src/spool_mbox.c
@@ -0,0 +1,247 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003 - 2015
+ * License: GPL
+ * Copyright (c) The Exim Maintainers 2016 - 2021
+ */
+
+/* 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
+
+extern int malware_ok;
+extern int spam_ok;
+
+int spool_mbox_ok = 0;
+uschar spooled_message_id[MESSAGE_ID_LENGTH+1];
+
+/*
+Create an MBOX-style message file from the spooled files.
+
+Returns a pointer to the FILE, and puts the size in bytes into mbox_file_size.
+If mbox_fname is non-null, fill in a pointer to the name.
+Normally, source_file_override is NULL
+*/
+
+FILE *
+spool_mbox(unsigned long *mbox_file_size, const uschar *source_file_override,
+ uschar ** mbox_fname)
+{
+uschar message_subdir[2];
+uschar buffer[16384];
+uschar *temp_string;
+uschar *mbox_path;
+FILE *mbox_file = NULL, *l_data_file = NULL, *yield = NULL;
+struct stat statbuf;
+int j;
+rmark reset_point;
+
+mbox_path = string_sprintf("%s/scan/%s/%s.eml",
+ spool_directory, message_id, message_id);
+if (mbox_fname) *mbox_fname = mbox_path;
+
+reset_point = store_mark();
+
+/* 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("scan directory %s/scan/%s", spool_directory, temp_string));
+ goto OUT;
+ }
+
+ /* open [message_id].eml file for writing */
+
+ if (!(mbox_file = modefopen(mbox_path, "wb", SPOOL_MODE)))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "%s",
+ string_open_failed("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)
+ if (fwrite(temp_string, Ustrlen(temp_string), 1, mbox_file) != 1)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \
+ mailbox headers to %s", mbox_path);
+ goto OUT;
+ }
+
+ /* write all non-deleted header lines to mbox file */
+
+ for (header_line * my_headerlist = header_list; my_headerlist;
+ my_headerlist = my_headerlist->next)
+ if (my_headerlist->type != '*')
+ if (fwrite(my_headerlist->text, my_headerlist->slen, 1, mbox_file) != 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 the main receive still has it open then it is holding
+ a lock, and we must not close it (which releases the lock), so just use the
+ global file handle. */
+ if (source_file_override)
+ l_data_file = Ufopen(source_file_override, "rb");
+ else if (spool_data_file)
+ l_data_file = spool_data_file;
+ else
+ {
+ message_subdir[1] = '\0';
+ for (int i = 0; i < 2; i++)
+ {
+ set_subdir_str(message_subdir, message_id, i);
+ temp_string = spool_fname(US"input", message_subdir, message_id, US"-D");
+ if ((l_data_file = Ufopen(temp_string, "rb"))) break;
+ }
+ }
+
+ if (!l_data_file)
+ {
+ 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, l_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(l_data_file, SPOOL_DATA_START_OFFSET, SEEK_SET);
+
+ do
+ {
+ uschar * s;
+
+ if (!f.spool_file_wireformat || source_file_override)
+ j = fread(buffer, 1, sizeof(buffer), l_data_file);
+ else /* needs CRLF -> NL */
+ if ((s = US fgets(CS buffer, sizeof(buffer), l_data_file)))
+ {
+ uschar * p = s + Ustrlen(s) - 1;
+
+ if (*p == '\n' && p[-1] == '\r')
+ *--p = '\n';
+ else if (*p == '\r')
+ ungetc(*p--, l_data_file);
+
+ j = p - buffer;
+ }
+ else
+ j = 0;
+
+ if (j > 0)
+ if (fwrite(buffer, j, 1, mbox_file) != 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;
+
+ Ustrncpy(spooled_message_id, message_id, sizeof(spooled_message_id));
+ spooled_message_id[sizeof(spooled_message_id)-1] = '\0';
+ spool_mbox_ok = 1;
+ }
+
+/* get the size of the mbox message and open [message_id].eml file for reading*/
+
+if ( !(yield = Ufopen(mbox_path,"rb"))
+ || fstat(fileno(yield), &statbuf) != 0
+ )
+ log_write(0, LOG_MAIN|LOG_PANIC, "%s",
+ string_open_failed( "scan file %s", mbox_path));
+else
+ *mbox_file_size = statbuf.st_size;
+
+OUT:
+if (l_data_file && !spool_data_file) (void)fclose(l_data_file);
+if (mbox_file) (void)fclose(mbox_file);
+store_reset(reset_point);
+return yield;
+}
+
+
+
+
+
+/* remove mbox spool file and temp directory */
+void
+unspool_mbox(void)
+{
+spam_ok = 0;
+malware_ok = 0;
+
+if (spool_mbox_ok && !f.no_mbox_unspool)
+ {
+ uschar *file_path;
+ DIR *tempdir;
+ rmark reset_point = store_mark();
+ uschar * mbox_path = string_sprintf("%s/scan/%s", spool_directory, spooled_message_id);
+
+ if (!(tempdir = exim_opendir(mbox_path)))
+ {
+ debug_printf("Unable to opendir(%s): %s\n", mbox_path, strerror(errno));
+ /* Just in case we still can: */
+ (void) rmdir(CS mbox_path);
+ return;
+ }
+ /* loop thru dir & delete entries */
+ for (struct dirent *entry; entry = readdir(tempdir); )
+ {
+ 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);
+ if (unlink(CS file_path) != 0)
+ log_write(0, LOG_MAIN|LOG_PANIC, "unlink(%s): %s", file_path, strerror(errno));
+ }
+
+ closedir(tempdir);
+
+ /* remove directory */
+ if (rmdir(CS mbox_path) != 0)
+ log_write(0, LOG_MAIN|LOG_PANIC, "rmdir(%s): %s", mbox_path, strerror(errno));
+ store_reset(reset_point);
+ }
+spool_mbox_ok = 0;
+}
+
+#endif
diff --git a/src/spool_out.c b/src/spool_out.c
new file mode 100644
index 0000000..510eda6
--- /dev/null
+++ b/src/spool_out.c
@@ -0,0 +1,580 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 an 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 ? 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) Uunlink(temp_name);
+if (f) (void)fclose(f);
+
+if (errmsg)
+ *errmsg = msg;
+else
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s", 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 (exim_fchown(fd, exim_uid, exim_gid, temp_name) || 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;
+}
+
+
+
+static const uschar *
+zap_newlines(const uschar *s)
+{
+uschar *z, *p;
+
+if (Ustrchr(s, '\n') == NULL) return s;
+
+p = z = string_copy(s);
+while ((p = Ustrchr(p, '\n')) != NULL) *p++ = ' ';
+return z;
+}
+
+static void
+spool_var_write(FILE * fp, const uschar * name, const uschar * val)
+{
+putc('-', fp);
+if (is_tainted(val))
+ {
+ int q = quoter_for_address(val);
+ putc('-', fp);
+ if (is_real_quoter(q)) fprintf(fp, "(%s)", lookup_list[q]->name);
+ }
+fprintf(fp, "%s %s\n", name, val);
+}
+
+/*************************************************
+* 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 size_correction;
+FILE * fp;
+struct stat statbuf;
+uschar * tname;
+uschar * fname;
+
+tname = spool_fname(US"input", message_subdir, US"hdr.", message_id);
+
+if ((fd = spool_open_temp(tname)) < 0)
+ return spool_write_error(where, errmsg, US"open", NULL, NULL);
+fp = fdopen(fd, "wb");
+DEBUG(D_receive|D_deliver) debug_printf("Writing spool header file: %s\n", tname);
+
+/* 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(fp, "%s-H\n", message_id);
+fprintf(fp, "%.63s %ld %ld\n", originator_login, (long int)originator_uid,
+ (long int)originator_gid);
+fprintf(fp, "<%s>\n", sender_address);
+fprintf(fp, "%d %d\n", (int)received_time.tv_sec, warning_count);
+
+fprintf(fp, "-received_time_usec .%06d\n", (int)received_time.tv_usec);
+fprintf(fp, "-received_time_complete %d.%06d\n",
+ (int)received_time_complete.tv_sec, (int)received_time_complete.tv_usec);
+
+/* 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) spool_var_write(fp, US"helo_name", sender_helo_name);
+
+if (sender_host_address)
+ {
+ if (is_tainted(sender_host_address)) putc('-', fp);
+ fprintf(fp, "-host_address [%s]:%d\n", sender_host_address, sender_host_port);
+ if (sender_host_name)
+ spool_var_write(fp, US"host_name", sender_host_name);
+ }
+if (sender_host_authenticated)
+ spool_var_write(fp, US"host_auth", sender_host_authenticated);
+if (sender_host_auth_pubname)
+ spool_var_write(fp, US"host_auth_pubname", sender_host_auth_pubname);
+
+/* Also about the interface a message came in on */
+
+if (interface_address)
+ {
+ if (is_tainted(interface_address)) putc('-', fp);
+ fprintf(fp, "-interface_address [%s]:%d\n", interface_address, interface_port);
+ }
+
+if (smtp_active_hostname != primary_hostname)
+ spool_var_write(fp, US"active_hostname", 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)
+ spool_var_write(fp, US"ident", sender_ident);
+
+/* Ditto for the received protocol */
+
+if (received_protocol)
+ spool_var_write(fp, US"received_protocol", received_protocol);
+
+/* Preserve any ACL variables that are set. */
+
+tree_walk(acl_var_c, &acl_var_write, fp);
+tree_walk(acl_var_m, &acl_var_write, fp);
+
+/* Now any other data that needs to be remembered. */
+
+if (*debuglog_name)
+ {
+ fprintf(fp, "-debug_selector 0x%x\n", debug_selector);
+ fprintf(fp, "-debuglog_name %s\n", debuglog_name);
+ }
+
+if (f.spool_file_wireformat)
+ fprintf(fp, "-spool_file_wireformat\n");
+else
+ fprintf(fp, "-body_linecount %d\n", body_linecount);
+fprintf(fp, "-max_received_linelength %d\n", max_received_linelength);
+
+if (body_zerocount > 0) fprintf(fp, "-body_zerocount %d\n", body_zerocount);
+
+if (authenticated_id)
+ spool_var_write(fp, US"auth_id", authenticated_id);
+if (authenticated_sender)
+ spool_var_write(fp, US"auth_sender", zap_newlines(authenticated_sender));
+
+if (f.allow_unqualified_recipient) fprintf(fp, "-allow_unqualified_recipient\n");
+if (f.allow_unqualified_sender) fprintf(fp, "-allow_unqualified_sender\n");
+if (f.deliver_firsttime) fprintf(fp, "-deliver_firsttime\n");
+if (f.deliver_freeze) fprintf(fp, "-frozen " TIME_T_FMT "\n", deliver_frozen_at);
+if (f.dont_deliver) fprintf(fp, "-N\n");
+if (host_lookup_deferred) fprintf(fp, "-host_lookup_deferred\n");
+if (host_lookup_failed) fprintf(fp, "-host_lookup_failed\n");
+if (f.sender_local) fprintf(fp, "-local\n");
+if (f.local_error_message) fprintf(fp, "-localerror\n");
+#ifdef HAVE_LOCAL_SCAN
+if (local_scan_data) spool_var_write(fp, US"local_scan", local_scan_data);
+#endif
+#ifdef WITH_CONTENT_SCAN
+if (spam_bar) spool_var_write(fp, US"spam_bar", spam_bar);
+if (spam_score) spool_var_write(fp, US"spam_score", spam_score);
+if (spam_score_int) spool_var_write(fp, US"spam_score_int", spam_score_int);
+#endif
+if (f.deliver_manual_thaw) fprintf(fp, "-manual_thaw\n");
+if (f.sender_set_untrusted) fprintf(fp, "-sender_set_untrusted\n");
+
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+if (bmi_verdicts) spool_var_write(fp, US"bmi_verdicts", bmi_verdicts);
+#endif
+
+#ifndef DISABLE_TLS
+if (tls_in.certificate_verified) fprintf(fp, "-tls_certificate_verified\n");
+if (tls_in.cipher) spool_var_write(fp, US"tls_cipher", tls_in.cipher);
+if (tls_in.peercert)
+ {
+ if (tls_export_cert(big_buffer, big_buffer_size, tls_in.peercert))
+ fprintf(fp, "--tls_peercert %s\n", CS big_buffer);
+ }
+if (tls_in.peerdn) spool_var_write(fp, US"tls_peerdn", string_printing(tls_in.peerdn));
+if (tls_in.sni) spool_var_write(fp, US"tls_sni", string_printing(tls_in.sni));
+if (tls_in.ourcert)
+ {
+ if (tls_export_cert(big_buffer, big_buffer_size, tls_in.ourcert))
+ fprintf(fp, "-tls_ourcert %s\n", CS big_buffer);
+ }
+if (tls_in.ocsp) fprintf(fp, "-tls_ocsp %d\n", tls_in.ocsp);
+# ifndef DISABLE_TLS_RESUME
+fprintf(fp, "-tls_resumption %c\n", 'A' + tls_in.resumption);
+# endif
+if (tls_in.ver) spool_var_write(fp, US"tls_ver", tls_in.ver);
+#endif
+
+#ifdef SUPPORT_I18N
+if (message_smtputf8)
+ {
+ fprintf(fp, "-smtputf8\n");
+ if (message_utf8_downconvert)
+ fprintf(fp, "-utf8_%sdowncvt\n", message_utf8_downconvert < 0 ? "opt" : "");
+ }
+#endif
+
+/* 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) fprintf(fp, "-dsn_envid %s\n", dsn_envid);
+/* DEBUG(D_deliver) debug_printf("DSN: Write SPOOL: -dsn_ret %d\n", dsn_ret); */
+if (dsn_ret) fprintf(fp, "-dsn_ret %d\n", dsn_ret);
+
+/* 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, fp);
+fprintf(fp, "%d\n", recipients_count);
+for (int i = 0; i < recipients_count; i++)
+ {
+ recipient_item *r = recipients_list + i;
+ const uschar *address = zap_newlines(r->address);
+
+ /* DEBUG(D_deliver) debug_printf("DSN: Flags: 0x%x\n", r->dsn_flags); */
+
+ if (r->pno < 0 && !r->errors_to && r->dsn_flags == 0)
+ fprintf(fp, "%s\n", address);
+ else
+ {
+ const uschar *errors_to = r->errors_to ? zap_newlines(r->errors_to) : CUS"";
+ /* for DSN SUPPORT extend exim 4 spool in a compatible way by
+ adding new values upfront and add flag 0x02 */
+ const uschar *orcpt = r->orcpt ? zap_newlines(r->orcpt) : CUS"";
+
+ fprintf(fp, "%s %s %d,%d %s %d,%d#3\n", address, orcpt, Ustrlen(orcpt),
+ r->dsn_flags, errors_to, Ustrlen(errors_to), r->pno);
+ }
+
+ DEBUG(D_deliver) debug_printf("DSN: **** SPOOL_OUT - "
+ "address: <%s> errorsto: <%s> orcpt: <%s> dsn_flags: 0x%x\n",
+ r->address, r->errors_to, r->orcpt, r->dsn_flags);
+ }
+
+/* Put a blank line before the headers */
+
+fprintf(fp, "\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(fp);
+if (fstat(fd, &statbuf))
+ return spool_write_error(where, errmsg, US"fstat", tname, fp);
+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 (header_line * h = header_list; h; h = h->next)
+ {
+ fprintf(fp, "%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(fp) != 0 || ferror(fp))
+ return spool_write_error(where, errmsg, US"write", tname, fp);
+
+/* 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(fp)) < 0)
+ return spool_write_error(where, errmsg, US"sync", tname, fp);
+
+/* Get the size of the file, and close it. */
+
+if (fstat(fd, &statbuf) != 0)
+ return spool_write_error(where, errmsg, US"fstat", tname, NULL);
+if (fclose(fp) != 0)
+ return spool_write_error(where, errmsg, US"close", tname, NULL);
+
+/* Rename the file to its correct name, thereby replacing any previous
+incarnation. */
+
+fname = spool_fname(US"input", message_subdir, id, US"-H");
+DEBUG(D_receive|D_deliver) debug_printf("Renaming spool header file: %s\n", fname);
+
+if (Urename(tname, fname) < 0)
+ return spool_write_error(where, errmsg, US"rename", tname, 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
+
+tname = spool_fname(US"input", message_subdir, US".", US"");
+
+# ifndef O_DIRECTORY
+# define O_DIRECTORY 0
+# endif
+
+if ((fd = Uopen(tname, O_RDONLY|O_DIRECTORY, 0)) < 0)
+ return spool_write_error(where, errmsg, US"directory open", fname, NULL);
+
+if (EXIMfsync(fd) < 0 && errno != EINVAL)
+ return spool_write_error(where, errmsg, US"directory sync", fname, NULL);
+
+if (close(fd) < 0)
+ return spool_write_error(where, errmsg, US"directory close", fname, NULL);
+
+#endif /* NEED_SYNC_DIRECTORY */
+
+/* Return the number of characters in the headers, which is the file size, less
+the preliminary 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;
+}
+
+
+/************************************************
+* 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
+ dq destiinationqueue 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 * dq, uschar *subdir, uschar *id, uschar *suffix,
+ uschar *from, uschar *to, BOOL noentok)
+{
+uschar * fname = spool_fname(string_sprintf("%s%s", from, dir), subdir, id, suffix);
+uschar * tname = spool_q_fname(string_sprintf("%s%s", to, dir), dq, subdir, id, suffix);
+if (Ulink(fname, tname) < 0 && (!noentok || errno != ENOENT))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "link(\"%s\", \"%s\") failed while moving "
+ "message: %s", fname, tname, 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 * fname = spool_fname(string_sprintf("%s%s", from, dir), subdir, id, suffix);
+if (Uunlink(fname) < 0 && (!noentok || errno != ENOENT))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "unlink(\"%s\") failed while moving "
+ "message: %s", fname, 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.
+
+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)
+{
+uschar * dest_qname = queue_name_dest ? queue_name_dest : queue_name;
+
+/* Since we are working within the spool, de-taint the dest queue name */
+dest_qname = string_copy_taint(dest_qname, GET_UNTAINTED);
+
+/* Create any output directories that do not exist. */
+
+(void) directory_make(spool_directory,
+ spool_q_sname(string_sprintf("%sinput", to), dest_qname, subdir),
+ INPUT_DIRECTORY_MODE, TRUE);
+(void) directory_make(spool_directory,
+ spool_q_sname(string_sprintf("%smsglog", to), dest_qname, subdir),
+ 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 messages 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", dest_qname, subdir, id, US"", from, to, TRUE) ||
+ !make_link(US"input", dest_qname, subdir, id, US"-D", from, to, FALSE) ||
+ !make_link(US"input", dest_qname, 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 %s%s%s%sinput, %smsglog to %s%s%s%sinput, %smsglog",
+ *queue_name?"(":"", *queue_name?queue_name:US"", *queue_name?") ":"",
+ from, from,
+ *dest_qname?"(":"", *dest_qname?dest_qname:US"", *dest_qname?") ":"",
+ to, to);
+
+return TRUE;
+}
+
+
+/* End of spool_out.c */
+/* vi: aw ai sw=2
+*/
diff --git a/src/std-crypto.c b/src/std-crypto.c
new file mode 100644
index 0000000..200fb71
--- /dev/null
+++ b/src/std-crypto.c
@@ -0,0 +1,1031 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Phil Pennock 2012, 2016
+ * Copyright (c) The Exim Maintainers 2017 - 2021
+ * 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"
+
+#ifdef DISABLE_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"
+"MIIBDAKBgQCxC4+WoIDgHd6S3l6uXVTsUsmfvPsGo8aaap3KUtI7YWBz4oZ1oj0Y\n"
+"mDjvHi7mUsAT7LSuqQYRIySXXDzUm4O/rMvdfZDEvXCYSI6cIZpzck7/1vrlZEc4\n"
+"+qMaT/VbzMChUa9fDci0vUW/N982XBpl5oz9p21NpwjfH7K8LkpDcQKBgQCk0cvV\n"
+"w/00EmdlpELvuZkF+BBN0lisUH/WQGz/FCZtMSZv6h5cQVZLd35pD1UE8hMWAhe0\n"
+"sBuIal6RVH+eJ0n01/vX07mpLuGQnQ0iY/gKdqaiTAh6CR9THb8KAWm2oorWYqTR\n"
+"jnOvoy13nVkY0IvIhY9Nzvl8KiSFXm7rIrOy5QICAKA=\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"
+"MIICDgKCAQEArRB+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+gIC\n"
+"AOA=\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"
+"MIICDQKCAQEAh6jmHbS2Zjz/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"
+"KM3GfrYYS1I9HbJGwy9jB4SQ8A741kfRSNR5VFFeIyfP75jFgmZLTA9sxBZZAgIB\n"
+"AA==\n"
+"-----END DH PARAMETERS-----\n";
+
+/* ------------------------------------------------------------------------- */
+/* RFC 7919 Published August 2016, so strength estimates date from then.
+
+A.1. ffdhe2048
+
+ The 2048-bit group has registry value 256 and is calculated from the
+ following formula:
+
+ The modulus is:
+
+ p = 2^2048 - 2^1984 + {[2^1918 * e] + 560316 } * 2^64 - 1
+
+ The hexadecimal representation of p is:
+
+ FFFFFFFF FFFFFFFF ADF85458 A2BB4A9A AFDC5620 273D3CF1
+ D8B9C583 CE2D3695 A9E13641 146433FB CC939DCE 249B3EF9
+ 7D2FE363 630C75D8 F681B202 AEC4617A D3DF1ED5 D5FD6561
+ 2433F51F 5F066ED0 85636555 3DED1AF3 B557135E 7F57C935
+ 984F0C70 E0E68B77 E2A689DA F3EFE872 1DF158A1 36ADE735
+ 30ACCA4F 483A797A BC0AB182 B324FB61 D108A94B B2C8E3FB
+ B96ADAB7 60D7F468 1D4F42A3 DE394DF4 AE56EDE7 6372BB19
+ 0B07A7C8 EE0A6D70 9E02FCE1 CDF7E2EC C03404CD 28342F61
+ 9172FE9C E98583FF 8E4F1232 EEF28183 C3FE3B1B 4C6FAD73
+ 3BB5FCBC 2EC22005 C58EF183 7D1683B2 C6F34A26 C1B2EFFA
+ 886B4238 61285C97 FFFFFFFF FFFFFFFF
+
+ The generator is: g = 2
+
+ The group size is: q = (p-1)/2
+
+ The hexadecimal representation of q is:
+
+ 7FFFFFFF FFFFFFFF D6FC2A2C 515DA54D 57EE2B10 139E9E78
+ EC5CE2C1 E7169B4A D4F09B20 8A3219FD E649CEE7 124D9F7C
+ BE97F1B1 B1863AEC 7B40D901 576230BD 69EF8F6A EAFEB2B0
+ 9219FA8F AF833768 42B1B2AA 9EF68D79 DAAB89AF 3FABE49A
+ CC278638 707345BB F15344ED 79F7F439 0EF8AC50 9B56F39A
+ 98566527 A41D3CBD 5E0558C1 59927DB0 E88454A5 D96471FD
+ DCB56D5B B06BFA34 0EA7A151 EF1CA6FA 572B76F3 B1B95D8C
+ 8583D3E4 770536B8 4F017E70 E6FBF176 601A0266 941A17B0
+ C8B97F4E 74C2C1FF C7278919 777940C1 E1FF1D8D A637D6B9
+ 9DDAFE5E 17611002 E2C778C1 BE8B41D9 6379A513 60D977FD
+ 4435A11C 30942E4B FFFFFFFF FFFFFFFF
+
+ The estimated symmetric-equivalent strength of this group is 103
+ bits.
+*/
+static const char dh_ffdhe2048_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIIBDAKCAQEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz\n"
+"+8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a\n"
+"87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7\n"
+"YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi\n"
+"7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD\n"
+"ssbzSibBsu/6iGtCOGEoXJf//////////wIBAgICB/8=\n"
+"-----END DH PARAMETERS-----\n";
+
+/*
+A.2. ffdhe3072
+
+ The 3072-bit prime has registry value 257 and is calculated from the
+ following formula:
+
+ The modulus is:
+
+ p = 2^3072 - 2^3008 + {[2^2942 * e] + 2625351} * 2^64 - 1
+
+ The hexadecimal representation of p is:
+
+ FFFFFFFF FFFFFFFF ADF85458 A2BB4A9A AFDC5620 273D3CF1
+ D8B9C583 CE2D3695 A9E13641 146433FB CC939DCE 249B3EF9
+ 7D2FE363 630C75D8 F681B202 AEC4617A D3DF1ED5 D5FD6561
+ 2433F51F 5F066ED0 85636555 3DED1AF3 B557135E 7F57C935
+ 984F0C70 E0E68B77 E2A689DA F3EFE872 1DF158A1 36ADE735
+ 30ACCA4F 483A797A BC0AB182 B324FB61 D108A94B B2C8E3FB
+ B96ADAB7 60D7F468 1D4F42A3 DE394DF4 AE56EDE7 6372BB19
+ 0B07A7C8 EE0A6D70 9E02FCE1 CDF7E2EC C03404CD 28342F61
+ 9172FE9C E98583FF 8E4F1232 EEF28183 C3FE3B1B 4C6FAD73
+ 3BB5FCBC 2EC22005 C58EF183 7D1683B2 C6F34A26 C1B2EFFA
+ 886B4238 611FCFDC DE355B3B 6519035B BC34F4DE F99C0238
+ 61B46FC9 D6E6C907 7AD91D26 91F7F7EE 598CB0FA C186D91C
+ AEFE1309 85139270 B4130C93 BC437944 F4FD4452 E2D74DD3
+ 64F2E21E 71F54BFF 5CAE82AB 9C9DF69E E86D2BC5 22363A0D
+ ABC52197 9B0DEADA 1DBF9A42 D5C4484E 0ABCD06B FA53DDEF
+ 3C1B20EE 3FD59D7C 25E41D2B 66C62E37 FFFFFFFF FFFFFFFF
+
+ The generator is: g = 2
+
+ The group size is: q = (p-1)/2
+
+ The hexadecimal representation of q is:
+
+ 7FFFFFFF FFFFFFFF D6FC2A2C 515DA54D 57EE2B10 139E9E78
+ EC5CE2C1 E7169B4A D4F09B20 8A3219FD E649CEE7 124D9F7C
+ BE97F1B1 B1863AEC 7B40D901 576230BD 69EF8F6A EAFEB2B0
+ 9219FA8F AF833768 42B1B2AA 9EF68D79 DAAB89AF 3FABE49A
+ CC278638 707345BB F15344ED 79F7F439 0EF8AC50 9B56F39A
+ 98566527 A41D3CBD 5E0558C1 59927DB0 E88454A5 D96471FD
+ DCB56D5B B06BFA34 0EA7A151 EF1CA6FA 572B76F3 B1B95D8C
+ 8583D3E4 770536B8 4F017E70 E6FBF176 601A0266 941A17B0
+ C8B97F4E 74C2C1FF C7278919 777940C1 E1FF1D8D A637D6B9
+ 9DDAFE5E 17611002 E2C778C1 BE8B41D9 6379A513 60D977FD
+ 4435A11C 308FE7EE 6F1AAD9D B28C81AD DE1A7A6F 7CCE011C
+ 30DA37E4 EB736483 BD6C8E93 48FBFBF7 2CC6587D 60C36C8E
+ 577F0984 C289C938 5A098649 DE21BCA2 7A7EA229 716BA6E9
+ B279710F 38FAA5FF AE574155 CE4EFB4F 743695E2 911B1D06
+ D5E290CB CD86F56D 0EDFCD21 6AE22427 055E6835 FD29EEF7
+ 9E0D9077 1FEACEBE 12F20E95 B363171B FFFFFFFF FFFFFFFF
+
+ The estimated symmetric-equivalent strength of this group is 125
+ bits.
+*/
+static const char dh_ffdhe3072_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIIBjAKCAYEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz\n"
+"+8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a\n"
+"87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7\n"
+"YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi\n"
+"7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD\n"
+"ssbzSibBsu/6iGtCOGEfz9zeNVs7ZRkDW7w09N75nAI4YbRvydbmyQd62R0mkff3\n"
+"7lmMsPrBhtkcrv4TCYUTknC0EwyTvEN5RPT9RFLi103TZPLiHnH1S/9croKrnJ32\n"
+"nuhtK8UiNjoNq8Uhl5sN6todv5pC1cRITgq80Gv6U93vPBsg7j/VnXwl5B0rZsYu\n"
+"N///////////AgECAgIL/w==\n"
+"-----END DH PARAMETERS-----\n";
+
+/*
+A.3. ffdhe4096
+
+ The 4096-bit group has registry value 258 and is calculated from the
+ following formula:
+
+ The modulus is:
+
+ p = 2^4096 - 2^4032 + {[2^3966 * e] + 5736041} * 2^64 - 1
+
+ The hexadecimal representation of p is:
+
+ FFFFFFFF FFFFFFFF ADF85458 A2BB4A9A AFDC5620 273D3CF1
+ D8B9C583 CE2D3695 A9E13641 146433FB CC939DCE 249B3EF9
+ 7D2FE363 630C75D8 F681B202 AEC4617A D3DF1ED5 D5FD6561
+ 2433F51F 5F066ED0 85636555 3DED1AF3 B557135E 7F57C935
+ 984F0C70 E0E68B77 E2A689DA F3EFE872 1DF158A1 36ADE735
+ 30ACCA4F 483A797A BC0AB182 B324FB61 D108A94B B2C8E3FB
+ B96ADAB7 60D7F468 1D4F42A3 DE394DF4 AE56EDE7 6372BB19
+ 0B07A7C8 EE0A6D70 9E02FCE1 CDF7E2EC C03404CD 28342F61
+ 9172FE9C E98583FF 8E4F1232 EEF28183 C3FE3B1B 4C6FAD73
+ 3BB5FCBC 2EC22005 C58EF183 7D1683B2 C6F34A26 C1B2EFFA
+ 886B4238 611FCFDC DE355B3B 6519035B BC34F4DE F99C0238
+ 61B46FC9 D6E6C907 7AD91D26 91F7F7EE 598CB0FA C186D91C
+ AEFE1309 85139270 B4130C93 BC437944 F4FD4452 E2D74DD3
+ 64F2E21E 71F54BFF 5CAE82AB 9C9DF69E E86D2BC5 22363A0D
+ ABC52197 9B0DEADA 1DBF9A42 D5C4484E 0ABCD06B FA53DDEF
+ 3C1B20EE 3FD59D7C 25E41D2B 669E1EF1 6E6F52C3 164DF4FB
+ 7930E9E4 E58857B6 AC7D5F42 D69F6D18 7763CF1D 55034004
+ 87F55BA5 7E31CC7A 7135C886 EFB4318A ED6A1E01 2D9E6832
+ A907600A 918130C4 6DC778F9 71AD0038 092999A3 33CB8B7A
+ 1A1DB93D 7140003C 2A4ECEA9 F98D0ACC 0A8291CD CEC97DCF
+ 8EC9B55A 7F88A46B 4DB5A851 F44182E1 C68A007E 5E655F6A
+ FFFFFFFF FFFFFFFF
+
+ The generator is: g = 2
+
+ The group size is: q = (p-1)/2
+
+ The hexadecimal representation of q is:
+
+ 7FFFFFFF FFFFFFFF D6FC2A2C 515DA54D 57EE2B10 139E9E78
+ EC5CE2C1 E7169B4A D4F09B20 8A3219FD E649CEE7 124D9F7C
+ BE97F1B1 B1863AEC 7B40D901 576230BD 69EF8F6A EAFEB2B0
+ 9219FA8F AF833768 42B1B2AA 9EF68D79 DAAB89AF 3FABE49A
+ CC278638 707345BB F15344ED 79F7F439 0EF8AC50 9B56F39A
+ 98566527 A41D3CBD 5E0558C1 59927DB0 E88454A5 D96471FD
+ DCB56D5B B06BFA34 0EA7A151 EF1CA6FA 572B76F3 B1B95D8C
+ 8583D3E4 770536B8 4F017E70 E6FBF176 601A0266 941A17B0
+ C8B97F4E 74C2C1FF C7278919 777940C1 E1FF1D8D A637D6B9
+ 9DDAFE5E 17611002 E2C778C1 BE8B41D9 6379A513 60D977FD
+ 4435A11C 308FE7EE 6F1AAD9D B28C81AD DE1A7A6F 7CCE011C
+ 30DA37E4 EB736483 BD6C8E93 48FBFBF7 2CC6587D 60C36C8E
+ 577F0984 C289C938 5A098649 DE21BCA2 7A7EA229 716BA6E9
+ B279710F 38FAA5FF AE574155 CE4EFB4F 743695E2 911B1D06
+ D5E290CB CD86F56D 0EDFCD21 6AE22427 055E6835 FD29EEF7
+ 9E0D9077 1FEACEBE 12F20E95 B34F0F78 B737A961 8B26FA7D
+ BC9874F2 72C42BDB 563EAFA1 6B4FB68C 3BB1E78E AA81A002
+ 43FAADD2 BF18E63D 389AE443 77DA18C5 76B50F00 96CF3419
+ 5483B005 48C09862 36E3BC7C B8D6801C 0494CCD1 99E5C5BD
+ 0D0EDC9E B8A0001E 15276754 FCC68566 054148E6 E764BEE7
+ C764DAAD 3FC45235 A6DAD428 FA20C170 E345003F 2F32AFB5
+ 7FFFFFFF FFFFFFFF
+
+ The estimated symmetric-equivalent strength of this group is 150
+ bits.
+*/
+static const char dh_ffdhe4096_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIICDAKCAgEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz\n"
+"+8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a\n"
+"87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7\n"
+"YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi\n"
+"7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD\n"
+"ssbzSibBsu/6iGtCOGEfz9zeNVs7ZRkDW7w09N75nAI4YbRvydbmyQd62R0mkff3\n"
+"7lmMsPrBhtkcrv4TCYUTknC0EwyTvEN5RPT9RFLi103TZPLiHnH1S/9croKrnJ32\n"
+"nuhtK8UiNjoNq8Uhl5sN6todv5pC1cRITgq80Gv6U93vPBsg7j/VnXwl5B0rZp4e\n"
+"8W5vUsMWTfT7eTDp5OWIV7asfV9C1p9tGHdjzx1VA0AEh/VbpX4xzHpxNciG77Qx\n"
+"iu1qHgEtnmgyqQdgCpGBMMRtx3j5ca0AOAkpmaMzy4t6Gh25PXFAADwqTs6p+Y0K\n"
+"zAqCkc3OyX3Pjsm1Wn+IpGtNtahR9EGC4caKAH5eZV9q//////////8CAQICAg//\n"
+"-----END DH PARAMETERS-----\n";
+
+/*
+A.4. ffdhe6144
+
+ The 6144-bit group has registry value 259 and is calculated from the
+ following formula:
+
+ The modulus is:
+
+ p = 2^6144 - 2^6080 + {[2^6014 * e] + 15705020} * 2^64 - 1
+
+ The hexadecimal representation of p is:
+
+ FFFFFFFF FFFFFFFF ADF85458 A2BB4A9A AFDC5620 273D3CF1
+ D8B9C583 CE2D3695 A9E13641 146433FB CC939DCE 249B3EF9
+ 7D2FE363 630C75D8 F681B202 AEC4617A D3DF1ED5 D5FD6561
+ 2433F51F 5F066ED0 85636555 3DED1AF3 B557135E 7F57C935
+ 984F0C70 E0E68B77 E2A689DA F3EFE872 1DF158A1 36ADE735
+ 30ACCA4F 483A797A BC0AB182 B324FB61 D108A94B B2C8E3FB
+ B96ADAB7 60D7F468 1D4F42A3 DE394DF4 AE56EDE7 6372BB19
+ 0B07A7C8 EE0A6D70 9E02FCE1 CDF7E2EC C03404CD 28342F61
+ 9172FE9C E98583FF 8E4F1232 EEF28183 C3FE3B1B 4C6FAD73
+ 3BB5FCBC 2EC22005 C58EF183 7D1683B2 C6F34A26 C1B2EFFA
+ 886B4238 611FCFDC DE355B3B 6519035B BC34F4DE F99C0238
+ 61B46FC9 D6E6C907 7AD91D26 91F7F7EE 598CB0FA C186D91C
+ AEFE1309 85139270 B4130C93 BC437944 F4FD4452 E2D74DD3
+ 64F2E21E 71F54BFF 5CAE82AB 9C9DF69E E86D2BC5 22363A0D
+ ABC52197 9B0DEADA 1DBF9A42 D5C4484E 0ABCD06B FA53DDEF
+ 3C1B20EE 3FD59D7C 25E41D2B 669E1EF1 6E6F52C3 164DF4FB
+ 7930E9E4 E58857B6 AC7D5F42 D69F6D18 7763CF1D 55034004
+ 87F55BA5 7E31CC7A 7135C886 EFB4318A ED6A1E01 2D9E6832
+ A907600A 918130C4 6DC778F9 71AD0038 092999A3 33CB8B7A
+ 1A1DB93D 7140003C 2A4ECEA9 F98D0ACC 0A8291CD CEC97DCF
+ 8EC9B55A 7F88A46B 4DB5A851 F44182E1 C68A007E 5E0DD902
+ 0BFD64B6 45036C7A 4E677D2C 38532A3A 23BA4442 CAF53EA6
+ 3BB45432 9B7624C8 917BDD64 B1C0FD4C B38E8C33 4C701C3A
+ CDAD0657 FCCFEC71 9B1F5C3E 4E46041F 388147FB 4CFDB477
+ A52471F7 A9A96910 B855322E DB6340D8 A00EF092 350511E3
+ 0ABEC1FF F9E3A26E 7FB29F8C 183023C3 587E38DA 0077D9B4
+ 763E4E4B 94B2BBC1 94C6651E 77CAF992 EEAAC023 2A281BF6
+ B3A739C1 22611682 0AE8DB58 47A67CBE F9C9091B 462D538C
+ D72B0374 6AE77F5E 62292C31 1562A846 505DC82D B854338A
+ E49F5235 C95B9117 8CCF2DD5 CACEF403 EC9D1810 C6272B04
+ 5B3B71F9 DC6B80D6 3FDD4A8E 9ADB1E69 62A69526 D43161C1
+ A41D570D 7938DAD4 A40E329C D0E40E65 FFFFFFFF FFFFFFFF
+
+ The generator is: g = 2
+
+ The group size is: q = (p-1)/2
+
+ The hexadecimal representation of q is:
+
+ 7FFFFFFF FFFFFFFF D6FC2A2C 515DA54D 57EE2B10 139E9E78
+ EC5CE2C1 E7169B4A D4F09B20 8A3219FD E649CEE7 124D9F7C
+ BE97F1B1 B1863AEC 7B40D901 576230BD 69EF8F6A EAFEB2B0
+ 9219FA8F AF833768 42B1B2AA 9EF68D79 DAAB89AF 3FABE49A
+ CC278638 707345BB F15344ED 79F7F439 0EF8AC50 9B56F39A
+ 98566527 A41D3CBD 5E0558C1 59927DB0 E88454A5 D96471FD
+ DCB56D5B B06BFA34 0EA7A151 EF1CA6FA 572B76F3 B1B95D8C
+ 8583D3E4 770536B8 4F017E70 E6FBF176 601A0266 941A17B0
+ C8B97F4E 74C2C1FF C7278919 777940C1 E1FF1D8D A637D6B9
+ 9DDAFE5E 17611002 E2C778C1 BE8B41D9 6379A513 60D977FD
+ 4435A11C 308FE7EE 6F1AAD9D B28C81AD DE1A7A6F 7CCE011C
+ 30DA37E4 EB736483 BD6C8E93 48FBFBF7 2CC6587D 60C36C8E
+ 577F0984 C289C938 5A098649 DE21BCA2 7A7EA229 716BA6E9
+ B279710F 38FAA5FF AE574155 CE4EFB4F 743695E2 911B1D06
+ D5E290CB CD86F56D 0EDFCD21 6AE22427 055E6835 FD29EEF7
+ 9E0D9077 1FEACEBE 12F20E95 B34F0F78 B737A961 8B26FA7D
+ BC9874F2 72C42BDB 563EAFA1 6B4FB68C 3BB1E78E AA81A002
+ 43FAADD2 BF18E63D 389AE443 77DA18C5 76B50F00 96CF3419
+ 5483B005 48C09862 36E3BC7C B8D6801C 0494CCD1 99E5C5BD
+ 0D0EDC9E B8A0001E 15276754 FCC68566 054148E6 E764BEE7
+ C764DAAD 3FC45235 A6DAD428 FA20C170 E345003F 2F06EC81
+ 05FEB25B 2281B63D 2733BE96 1C29951D 11DD2221 657A9F53
+ 1DDA2A19 4DBB1264 48BDEEB2 58E07EA6 59C74619 A6380E1D
+ 66D6832B FE67F638 CD8FAE1F 2723020F 9C40A3FD A67EDA3B
+ D29238FB D4D4B488 5C2A9917 6DB1A06C 50077849 1A8288F1
+ 855F60FF FCF1D137 3FD94FC6 0C1811E1 AC3F1C6D 003BECDA
+ 3B1F2725 CA595DE0 CA63328F 3BE57CC9 77556011 95140DFB
+ 59D39CE0 91308B41 05746DAC 23D33E5F 7CE4848D A316A9C6
+ 6B9581BA 3573BFAF 31149618 8AB15423 282EE416 DC2A19C5
+ 724FA91A E4ADC88B C66796EA E5677A01 F64E8C08 63139582
+ 2D9DB8FC EE35C06B 1FEEA547 4D6D8F34 B1534A93 6A18B0E0
+ D20EAB86 BC9C6D6A 5207194E 68720732 FFFFFFFF FFFFFFFF
+
+ The estimated symmetric-equivalent strength of this group is 175
+ bits.
+*/
+static const char dh_ffdhe6144_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIIDDAKCAwEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz\n"
+"+8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a\n"
+"87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7\n"
+"YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi\n"
+"7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD\n"
+"ssbzSibBsu/6iGtCOGEfz9zeNVs7ZRkDW7w09N75nAI4YbRvydbmyQd62R0mkff3\n"
+"7lmMsPrBhtkcrv4TCYUTknC0EwyTvEN5RPT9RFLi103TZPLiHnH1S/9croKrnJ32\n"
+"nuhtK8UiNjoNq8Uhl5sN6todv5pC1cRITgq80Gv6U93vPBsg7j/VnXwl5B0rZp4e\n"
+"8W5vUsMWTfT7eTDp5OWIV7asfV9C1p9tGHdjzx1VA0AEh/VbpX4xzHpxNciG77Qx\n"
+"iu1qHgEtnmgyqQdgCpGBMMRtx3j5ca0AOAkpmaMzy4t6Gh25PXFAADwqTs6p+Y0K\n"
+"zAqCkc3OyX3Pjsm1Wn+IpGtNtahR9EGC4caKAH5eDdkCC/1ktkUDbHpOZ30sOFMq\n"
+"OiO6RELK9T6mO7RUMpt2JMiRe91kscD9TLOOjDNMcBw6za0GV/zP7HGbH1w+TkYE\n"
+"HziBR/tM/bR3pSRx96mpaRC4VTIu22NA2KAO8JI1BRHjCr7B//njom5/sp+MGDAj\n"
+"w1h+ONoAd9m0dj5OS5Syu8GUxmUed8r5ku6qwCMqKBv2s6c5wSJhFoIK6NtYR6Z8\n"
+"vvnJCRtGLVOM1ysDdGrnf15iKSwxFWKoRlBdyC24VDOK5J9SNclbkReMzy3Vys70\n"
+"A+ydGBDGJysEWztx+dxrgNY/3UqOmtseaWKmlSbUMWHBpB1XDXk42tSkDjKc0OQO\n"
+"Zf//////////AgECAgIX/w==\n"
+"-----END DH PARAMETERS-----\n";
+
+/*
+A.5. ffdhe8192
+
+ The 8192-bit group has registry value 260 and is calculated from the
+ following formula:
+
+ The modulus is:
+
+ p = 2^8192 - 2^8128 + {[2^8062 * e] + 10965728} * 2^64 - 1
+
+ The hexadecimal representation of p is:
+
+ FFFFFFFF FFFFFFFF ADF85458 A2BB4A9A AFDC5620 273D3CF1
+ D8B9C583 CE2D3695 A9E13641 146433FB CC939DCE 249B3EF9
+ 7D2FE363 630C75D8 F681B202 AEC4617A D3DF1ED5 D5FD6561
+ 2433F51F 5F066ED0 85636555 3DED1AF3 B557135E 7F57C935
+ 984F0C70 E0E68B77 E2A689DA F3EFE872 1DF158A1 36ADE735
+ 30ACCA4F 483A797A BC0AB182 B324FB61 D108A94B B2C8E3FB
+ B96ADAB7 60D7F468 1D4F42A3 DE394DF4 AE56EDE7 6372BB19
+ 0B07A7C8 EE0A6D70 9E02FCE1 CDF7E2EC C03404CD 28342F61
+ 9172FE9C E98583FF 8E4F1232 EEF28183 C3FE3B1B 4C6FAD73
+ 3BB5FCBC 2EC22005 C58EF183 7D1683B2 C6F34A26 C1B2EFFA
+ 886B4238 611FCFDC DE355B3B 6519035B BC34F4DE F99C0238
+ 61B46FC9 D6E6C907 7AD91D26 91F7F7EE 598CB0FA C186D91C
+ AEFE1309 85139270 B4130C93 BC437944 F4FD4452 E2D74DD3
+ 64F2E21E 71F54BFF 5CAE82AB 9C9DF69E E86D2BC5 22363A0D
+ ABC52197 9B0DEADA 1DBF9A42 D5C4484E 0ABCD06B FA53DDEF
+ 3C1B20EE 3FD59D7C 25E41D2B 669E1EF1 6E6F52C3 164DF4FB
+ 7930E9E4 E58857B6 AC7D5F42 D69F6D18 7763CF1D 55034004
+ 87F55BA5 7E31CC7A 7135C886 EFB4318A ED6A1E01 2D9E6832
+ A907600A 918130C4 6DC778F9 71AD0038 092999A3 33CB8B7A
+ 1A1DB93D 7140003C 2A4ECEA9 F98D0ACC 0A8291CD CEC97DCF
+ 8EC9B55A 7F88A46B 4DB5A851 F44182E1 C68A007E 5E0DD902
+ 0BFD64B6 45036C7A 4E677D2C 38532A3A 23BA4442 CAF53EA6
+ 3BB45432 9B7624C8 917BDD64 B1C0FD4C B38E8C33 4C701C3A
+ CDAD0657 FCCFEC71 9B1F5C3E 4E46041F 388147FB 4CFDB477
+ A52471F7 A9A96910 B855322E DB6340D8 A00EF092 350511E3
+ 0ABEC1FF F9E3A26E 7FB29F8C 183023C3 587E38DA 0077D9B4
+ 763E4E4B 94B2BBC1 94C6651E 77CAF992 EEAAC023 2A281BF6
+ B3A739C1 22611682 0AE8DB58 47A67CBE F9C9091B 462D538C
+ D72B0374 6AE77F5E 62292C31 1562A846 505DC82D B854338A
+ E49F5235 C95B9117 8CCF2DD5 CACEF403 EC9D1810 C6272B04
+ 5B3B71F9 DC6B80D6 3FDD4A8E 9ADB1E69 62A69526 D43161C1
+ A41D570D 7938DAD4 A40E329C CFF46AAA 36AD004C F600C838
+ 1E425A31 D951AE64 FDB23FCE C9509D43 687FEB69 EDD1CC5E
+ 0B8CC3BD F64B10EF 86B63142 A3AB8829 555B2F74 7C932665
+ CB2C0F1C C01BD702 29388839 D2AF05E4 54504AC7 8B758282
+ 2846C0BA 35C35F5C 59160CC0 46FD8251 541FC68C 9C86B022
+ BB709987 6A460E74 51A8A931 09703FEE 1C217E6C 3826E52C
+ 51AA691E 0E423CFC 99E9E316 50C1217B 624816CD AD9A95F9
+ D5B80194 88D9C0A0 A1FE3075 A577E231 83F81D4A 3F2FA457
+ 1EFC8CE0 BA8A4FE8 B6855DFE 72B0A66E DED2FBAB FBE58A30
+ FAFABE1C 5D71A87E 2F741EF8 C1FE86FE A6BBFDE5 30677F0D
+ 97D11D49 F7A8443D 0822E506 A9F4614E 011E2A94 838FF88C
+ D68C8BB7 C5C6424C FFFFFFFF FFFFFFFF
+
+ The generator is: g = 2
+
+ The group size is: q = (p-1)/2
+
+ The hexadecimal representation of q is:
+
+ 7FFFFFFF FFFFFFFF D6FC2A2C 515DA54D 57EE2B10 139E9E78
+ EC5CE2C1 E7169B4A D4F09B20 8A3219FD E649CEE7 124D9F7C
+ BE97F1B1 B1863AEC 7B40D901 576230BD 69EF8F6A EAFEB2B0
+ 9219FA8F AF833768 42B1B2AA 9EF68D79 DAAB89AF 3FABE49A
+ CC278638 707345BB F15344ED 79F7F439 0EF8AC50 9B56F39A
+ 98566527 A41D3CBD 5E0558C1 59927DB0 E88454A5 D96471FD
+ DCB56D5B B06BFA34 0EA7A151 EF1CA6FA 572B76F3 B1B95D8C
+ 8583D3E4 770536B8 4F017E70 E6FBF176 601A0266 941A17B0
+ C8B97F4E 74C2C1FF C7278919 777940C1 E1FF1D8D A637D6B9
+ 9DDAFE5E 17611002 E2C778C1 BE8B41D9 6379A513 60D977FD
+ 4435A11C 308FE7EE 6F1AAD9D B28C81AD DE1A7A6F 7CCE011C
+ 30DA37E4 EB736483 BD6C8E93 48FBFBF7 2CC6587D 60C36C8E
+ 577F0984 C289C938 5A098649 DE21BCA2 7A7EA229 716BA6E9
+ B279710F 38FAA5FF AE574155 CE4EFB4F 743695E2 911B1D06
+ D5E290CB CD86F56D 0EDFCD21 6AE22427 055E6835 FD29EEF7
+ 9E0D9077 1FEACEBE 12F20E95 B34F0F78 B737A961 8B26FA7D
+ BC9874F2 72C42BDB 563EAFA1 6B4FB68C 3BB1E78E AA81A002
+ 43FAADD2 BF18E63D 389AE443 77DA18C5 76B50F00 96CF3419
+ 5483B005 48C09862 36E3BC7C B8D6801C 0494CCD1 99E5C5BD
+ 0D0EDC9E B8A0001E 15276754 FCC68566 054148E6 E764BEE7
+ C764DAAD 3FC45235 A6DAD428 FA20C170 E345003F 2F06EC81
+ 05FEB25B 2281B63D 2733BE96 1C29951D 11DD2221 657A9F53
+ 1DDA2A19 4DBB1264 48BDEEB2 58E07EA6 59C74619 A6380E1D
+ 66D6832B FE67F638 CD8FAE1F 2723020F 9C40A3FD A67EDA3B
+ D29238FB D4D4B488 5C2A9917 6DB1A06C 50077849 1A8288F1
+ 855F60FF FCF1D137 3FD94FC6 0C1811E1 AC3F1C6D 003BECDA
+ 3B1F2725 CA595DE0 CA63328F 3BE57CC9 77556011 95140DFB
+ 59D39CE0 91308B41 05746DAC 23D33E5F 7CE4848D A316A9C6
+ 6B9581BA 3573BFAF 31149618 8AB15423 282EE416 DC2A19C5
+ 724FA91A E4ADC88B C66796EA E5677A01 F64E8C08 63139582
+ 2D9DB8FC EE35C06B 1FEEA547 4D6D8F34 B1534A93 6A18B0E0
+ D20EAB86 BC9C6D6A 5207194E 67FA3555 1B568026 7B00641C
+ 0F212D18 ECA8D732 7ED91FE7 64A84EA1 B43FF5B4 F6E8E62F
+ 05C661DE FB258877 C35B18A1 51D5C414 AAAD97BA 3E499332
+ E596078E 600DEB81 149C441C E95782F2 2A282563 C5BAC141
+ 1423605D 1AE1AFAE 2C8B0660 237EC128 AA0FE346 4E435811
+ 5DB84CC3 B523073A 28D45498 84B81FF7 0E10BF36 1C137296
+ 28D5348F 07211E7E 4CF4F18B 286090BD B1240B66 D6CD4AFC
+ EADC00CA 446CE050 50FF183A D2BBF118 C1FC0EA5 1F97D22B
+ 8F7E4670 5D4527F4 5B42AEFF 39585337 6F697DD5 FDF2C518
+ 7D7D5F0E 2EB8D43F 17BA0F7C 60FF437F 535DFEF2 9833BF86
+ CBE88EA4 FBD4221E 84117283 54FA30A7 008F154A 41C7FC46
+ 6B4645DB E2E32126 7FFFFFFF FFFFFFFF
+
+ The estimated symmetric-equivalent strength of this group is 192
+ bits.
+*/
+static const char dh_ffdhe8192_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIIEDAKCBAEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz\n"
+"+8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a\n"
+"87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7\n"
+"YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi\n"
+"7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD\n"
+"ssbzSibBsu/6iGtCOGEfz9zeNVs7ZRkDW7w09N75nAI4YbRvydbmyQd62R0mkff3\n"
+"7lmMsPrBhtkcrv4TCYUTknC0EwyTvEN5RPT9RFLi103TZPLiHnH1S/9croKrnJ32\n"
+"nuhtK8UiNjoNq8Uhl5sN6todv5pC1cRITgq80Gv6U93vPBsg7j/VnXwl5B0rZp4e\n"
+"8W5vUsMWTfT7eTDp5OWIV7asfV9C1p9tGHdjzx1VA0AEh/VbpX4xzHpxNciG77Qx\n"
+"iu1qHgEtnmgyqQdgCpGBMMRtx3j5ca0AOAkpmaMzy4t6Gh25PXFAADwqTs6p+Y0K\n"
+"zAqCkc3OyX3Pjsm1Wn+IpGtNtahR9EGC4caKAH5eDdkCC/1ktkUDbHpOZ30sOFMq\n"
+"OiO6RELK9T6mO7RUMpt2JMiRe91kscD9TLOOjDNMcBw6za0GV/zP7HGbH1w+TkYE\n"
+"HziBR/tM/bR3pSRx96mpaRC4VTIu22NA2KAO8JI1BRHjCr7B//njom5/sp+MGDAj\n"
+"w1h+ONoAd9m0dj5OS5Syu8GUxmUed8r5ku6qwCMqKBv2s6c5wSJhFoIK6NtYR6Z8\n"
+"vvnJCRtGLVOM1ysDdGrnf15iKSwxFWKoRlBdyC24VDOK5J9SNclbkReMzy3Vys70\n"
+"A+ydGBDGJysEWztx+dxrgNY/3UqOmtseaWKmlSbUMWHBpB1XDXk42tSkDjKcz/Rq\n"
+"qjatAEz2AMg4HkJaMdlRrmT9sj/OyVCdQ2h/62nt0cxeC4zDvfZLEO+GtjFCo6uI\n"
+"KVVbL3R8kyZlyywPHMAb1wIpOIg50q8F5FRQSseLdYKCKEbAujXDX1xZFgzARv2C\n"
+"UVQfxoychrAiu3CZh2pGDnRRqKkxCXA/7hwhfmw4JuUsUappHg5CPPyZ6eMWUMEh\n"
+"e2JIFs2tmpX51bgBlIjZwKCh/jB1pXfiMYP4HUo/L6RXHvyM4LqKT+i2hV3+crCm\n"
+"bt7S+6v75Yow+vq+HF1xqH4vdB74wf6G/qa7/eUwZ38Nl9EdSfeoRD0IIuUGqfRh\n"
+"TgEeKpSDj/iM1oyLt8XGQkz//////////wIBAgICH/8=\n"
+"-----END DH PARAMETERS-----\n";
+
+/* ========================================================================= */
+
+/* Generated by Phil as a non-standard option.
+openssl dhparam -2 2048
+No provenance to prove non-tampering available, beyond trusting that this
+developer generated this as stated above. */
+
+
+/* MacOSX 10.10.5 invoking system OpenSSL 0.9.8zg */
+static const char dh_exim_20160529_1[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIIBCAKCAQEA8ZMf89Gaye4bDEX1BXZ9+2edkXym9EK0GxmFilHEGpnhgLNmCk+H\n"
+"cCb+zn8Ed5bpCOmRuEv9N/VKPjSpno8jYiQbFgUL3vh8uKvQLJNTzDVDbpd3YO7E\n"
+"tiS0L0qWL57zIf8b3VZTMRsH4Orz2Rla61wVl6XpxE5WRfGqPS264Vvfew7xmCoi\n"
+"INaFzIU6zwk2WeD6K5asctYlQG/UtgY1nRFkQTebIOpm03a6/hw7F14l3yUZgXfv\n"
+"I3m4MFaWvxGcuZxddTijXw3VfjMdWvdH3Iz7IcqD32uEzK6Rgi/t4OVSw1kE2oDt\n"
+"cFThPUCWb7O4TVq9Xt2UZqZFNU6kUAkv2wIBAg==\n"
+"-----END DH PARAMETERS-----\n";
+
+/* MacOSX 10.10.5 invoking OpenSSL 1.0.2h installed from brew bottle */
+static const char dh_exim_20160529_2[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIIBCAKCAQEAot84eqyfSb5l8GRCN2ioWP5T85Z/2lVX9A9r9JzwDfvliAAqm6Vp\n"
+"UcHdAfVt54kc8DsmLiHdDhxY1I/wo+DcBylfVx13cmkroAocowOD5dwQMYk6iXjV\n"
+"ys4heRJhYlAHgt8QZH8dA8c/HLs+rlAHhSUPnetsZmcoPE0LRsjigJsiVXasm+sl\n"
+"g/77u5FCkgSrFILcD9PLPto1ciIXp2y8cjXQDk+D9FH1HaSCXLCLkuHxhQXxjTYO\n"
+"C3Q53aNLkDJ4zpPt7Kc9NxQFBVlNc260IFDOHTWhgV2zpyG6oIzQoHSmmiLAAfcF\n"
+"HrG7I06uZBLjuNGGaM0eeuxHNhs2G2EduwIBAg==\n"
+"-----END DH PARAMETERS-----\n";
+
+/* Ubuntu 14.04.4 running on dual-core Atom D2500 with OneRNG entropy key */
+static const char dh_exim_20160529_3[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIIBCAKCAQEAkbRYVoge2PtrmV1eKCKluSBFELgckuLSnkuH0TffqbmfoYM34lFu\n"
+"2vPM2LhnzKvEBQlIICOTzQD29kROacRfSKpsNINRXhXKUqI6sFXzUZu4Flk69XKG\n"
+"ZOSDYvWkI5pSn1amQ4Nnvn6s+uwn/f0ZDZDiKLW9TgntxJV4A2+yeymaeoGCbIXX\n"
+"5q8WgajFhAeut36RL93HBnXT1hT7Eja1Y81w9fOzQrwBuXhyfCkAdiMA/VCp0UD4\n"
+"0p7uf+okpckVnwD6WnUCHMij8nGlVblZELFYzNi0udtzIrSwlALbZXIeAqhbZXJO\n"
+"lCuYspJhzV0Vs0lDJwrxvNwtdg1ernVIowIBAg==\n"
+"-----END DH PARAMETERS-----\n";
+
+/* ========================================================================= */
+
+struct dh_constant {
+ const char * label;
+ const char * pem;
+ int logging;
+};
+
+#define EXIM_DH_PRIME_DEFAULT dh_exim_20160529_3
+
+/* KEEP SORTED ALPHABETICALLY;
+duplicate PEM are okay, if we want aliases, but names must be alphabetical */
+
+static struct dh_constant dh_constants[] = {
+ /* label pem */
+ { "default", EXIM_DH_PRIME_DEFAULT, 0 },
+ { "exim.dev.20160529.1", dh_exim_20160529_1, 0 },
+ { "exim.dev.20160529.2", dh_exim_20160529_2, 0 },
+ { "exim.dev.20160529.3", dh_exim_20160529_3, 0 },
+ { "ffdhe2048", dh_ffdhe2048_pem, 0 },
+ { "ffdhe3072", dh_ffdhe3072_pem, 0 },
+ { "ffdhe4096", dh_ffdhe4096_pem, 0 },
+ { "ffdhe6144", dh_ffdhe6144_pem, 0 },
+ { "ffdhe8192", dh_ffdhe8192_pem, 0 },
+ { "ike1", dh_ike_1_pem, LOG_MAIN | LOG_PANIC },
+ { "ike14", dh_ike_14_pem, 0 },
+ { "ike15", dh_ike_15_pem, 0 },
+ { "ike16", dh_ike_16_pem, 0 },
+ { "ike17", dh_ike_17_pem, 0 },
+ { "ike18", dh_ike_18_pem, 0 },
+ { "ike2", dh_ike_2_pem, LOG_MAIN },
+ { "ike22", dh_ike_22_pem, LOG_MAIN | LOG_PANIC },
+ { "ike23", dh_ike_23_pem, LOG_MAIN },
+ { "ike24", dh_ike_24_pem, LOG_MAIN },
+ { "ike5", dh_ike_5_pem, 0 },
+};
+static const int dh_constants_count = nelem(dh_constants);
+
+
+/* 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 EXIM_DH_PRIME_DEFAULT;
+}
+
+
+/* Return PEM string for given name */
+
+const char *
+std_dh_prime_named(const uschar * name)
+{
+for (int first = 0, last = dh_constants_count; last > first; )
+ {
+ int middle = (first + last)/2;
+ struct dh_constant * dp = &dh_constants[middle];
+ int c = Ustrcmp(name, dp->label);
+ if (c == 0)
+ {
+ if (dp->logging)
+ log_write(0, dp->logging,
+ "WARNING: deprecated Diffie-Hellman parameter '%s' used", dp->label);
+ return dp->pem;
+ }
+ else if (c > 0)
+ first = middle + 1;
+ else
+ last = middle;
+ }
+return NULL;
+}
+
+#endif /*DISABLE_TLS*/
+/* EOF */
diff --git a/src/store.c b/src/store.c
new file mode 100644
index 0000000..c98fcbf
--- /dev/null
+++ b/src/store.c
@@ -0,0 +1,1301 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim maintainers 2019 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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.
+
+- There is another pool (POOL_MESSAGE) used for medium-lifetime objects; within
+ a single message transaction but needed for longer than the use of the main
+ pool permits. Currently this means only receive-time DKIM information.
+
+- There is a dedicated pool for configuration data read from the config file(s).
+ Once complete, it is made readonly.
+
+- There are pools for each active combination of lookup-quoting, dynamically created.
+
+. Orthogonal to the four main pool types, there are two classes of memory: untainted
+ and tainted. The latter is used for values derived from untrusted input, and
+ the string-expansion mechanism refuses to operate on such values (obviously,
+ it can expand an untainted value to return a tainted result). The classes
+ are implemented by duplicating the four pool types. Pool resets are requested
+ against the nontainted sibling and apply to both siblings.
+
+ Only memory blocks requested for tainted use are regarded as tainted; anything
+ else (including stack auto variables) is untainted. Care is needed when coding
+ to not copy untrusted data into untainted memory, as downstream taint-checks
+ would be avoided.
+
+ Intermediate layers (eg. the string functions) can test for taint, and use this
+ for ensurinng that results have proper state. For example the
+ string_vformat_trc() routing supporting the string_sprintf() interface will
+ recopy a string being built into a tainted allocation if it meets a %s for a
+ tainted argument. Any intermediate-layer function that (can) return a new
+ allocation should behave this way; returning a tainted result if any tainted
+ content is used. Intermediate-layer functions (eg. Ustrncpy) that modify
+ existing allocations fail if tainted data is written into an untainted area.
+ Users of functions that modify existing allocations should check if a tainted
+ source and an untainted destination is used, and fail instead (sprintf() being
+ the classic case).
+*/
+
+
+#include "exim.h"
+/* keep config.h before memcheck.h, for NVALGRIND */
+#include "config.h"
+
+#include <sys/mman.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))
+
+/* 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;
+
+/* Pool descriptor struct */
+
+typedef struct pooldesc {
+ storeblock * chainbase; /* list of blocks in pool */
+ storeblock * current_block; /* top block, still with free space */
+ void * next_yield; /* next allocation point */
+ int yield_length; /* remaining space in current block */
+ unsigned store_block_order; /* log2(size) block allocation size */
+
+ /* 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;
+
+ /* These are purely for stats-gathering */
+
+ int nbytes;
+ int maxbytes;
+ int nblocks;
+ int maxblocks;
+ unsigned maxorder;
+} pooldesc;
+
+/* Enhanced pool descriptor for quoted pools */
+
+typedef struct quoted_pooldesc {
+ pooldesc pool;
+ unsigned quoter;
+ struct quoted_pooldesc * next;
+} quoted_pooldesc;
+
+/* 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)
+
+/* Size of block to get from malloc to carve up into smaller ones. This
+must be a multiple of the alignment. We assume that 4096 is going to be
+suitably aligned. Double the size per-pool for every malloc, to mitigate
+certain denial-of-service attacks. Don't bother to decrease on block frees.
+We waste average half the current alloc size per pool. This could be several
+hundred kB now, vs. 4kB with a constant-size block size. But the search time
+for is_tainted(), linear in the number of blocks for the pool, is O(n log n)
+rather than O(n^2).
+A test of 2000 RCPTs and just accept ACL had 370kB in 21 blocks before,
+504kB in 6 blocks now, for the untainted-main (largest) pool.
+Builds for restricted-memory system can disable the expansion by
+defining RESTRICTED_MEMORY */
+/*XXX should we allow any for malloc's own overhead? But how much? */
+
+/* #define RESTRICTED_MEMORY */
+#define STORE_BLOCK_SIZE(order) ((1U << (order)) - ALIGNED_SIZEOF_STOREBLOCK)
+
+/* 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_MAIN;
+
+pooldesc paired_pools[N_PAIRED_POOLS];
+quoted_pooldesc * quoted_pools = NULL;
+
+static int n_nonpool_blocks; /* current number of direct store_malloc() blocks */
+static int max_nonpool_blocks;
+static int max_pool_malloc; /* max value for pool_malloc */
+static int max_nonpool_malloc; /* max value for nonpool_malloc */
+
+/* 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;
+static int nonpool_malloc;
+
+
+#ifndef COMPILE_UTILITY
+static const uschar * pooluse[N_PAIRED_POOLS] = {
+[POOL_MAIN] = US"main",
+[POOL_PERM] = US"perm",
+[POOL_CONFIG] = US"config",
+[POOL_SEARCH] = US"search",
+[POOL_MESSAGE] = US"message",
+[POOL_TAINT_MAIN] = US"main",
+[POOL_TAINT_PERM] = US"perm",
+[POOL_TAINT_CONFIG] = US"config",
+[POOL_TAINT_SEARCH] = US"search",
+[POOL_TAINT_MESSAGE] = US"message",
+};
+static const uschar * poolclass[N_PAIRED_POOLS] = {
+[POOL_MAIN] = US"untainted",
+[POOL_PERM] = US"untainted",
+[POOL_CONFIG] = US"untainted",
+[POOL_SEARCH] = US"untainted",
+[POOL_MESSAGE] = US"untainted",
+[POOL_TAINT_MAIN] = US"tainted",
+[POOL_TAINT_PERM] = US"tainted",
+[POOL_TAINT_CONFIG] = US"tainted",
+[POOL_TAINT_SEARCH] = US"tainted",
+[POOL_TAINT_MESSAGE] = US"tainted",
+};
+#endif
+
+
+static void * internal_store_malloc(size_t, const char *, int);
+static void internal_store_free(void *, const char *, int linenumber);
+
+/******************************************************************************/
+
+static void
+pool_init(pooldesc * pp)
+{
+memset(pp, 0, sizeof(*pp));
+pp->yield_length = -1;
+pp->store_block_order = 12; /* log2(allocation_size) ie. 4kB */
+}
+
+/* Initialisation, for things fragile with parameter channges when using
+static initialisers. */
+
+void
+store_init(void)
+{
+for (pooldesc * pp = paired_pools; pp < paired_pools + N_PAIRED_POOLS; pp++)
+ pool_init(pp);
+}
+
+/******************************************************************************/
+/* Locating elements given memory pointer */
+
+static BOOL
+is_pointer_in_block(const storeblock * b, const void * p)
+{
+uschar * bc = US b + ALIGNED_SIZEOF_STOREBLOCK;
+return US p >= bc && US p < bc + b->length;
+}
+
+static pooldesc *
+pool_current_for_pointer(const void * p)
+{
+storeblock * b;
+
+for (quoted_pooldesc * qp = quoted_pools; qp; qp = qp->next)
+ if ((b = qp->pool.current_block) && is_pointer_in_block(b, p))
+ return &qp->pool;
+
+for (pooldesc * pp = paired_pools; pp < paired_pools + N_PAIRED_POOLS; pp++)
+ if ((b = pp->current_block) && is_pointer_in_block(b, p))
+ return pp;
+return NULL;
+}
+
+static pooldesc *
+pool_for_pointer(const void * p, const char * func, int linenumber)
+{
+pooldesc * pp;
+storeblock * b;
+
+if ((pp = pool_current_for_pointer(p))) return pp;
+
+for (quoted_pooldesc * qp = quoted_pools; qp; qp = qp->next)
+ for (b = qp->pool.chainbase; b; b = b->next)
+ if (is_pointer_in_block(b, p)) return &qp->pool;
+
+for (pp = paired_pools; pp < paired_pools + N_PAIRED_POOLS; pp++)
+ for (b = pp->chainbase; b; b = b->next)
+ if (is_pointer_in_block(b, p)) return pp;
+
+log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "bad memory reference; pool not found, at %s %d", func, linenumber);
+return NULL;
+}
+
+/******************************************************************************/
+/* Test if a pointer refers to tainted memory.
+
+Slower version check, for use when platform intermixes malloc and mmap area
+addresses. Test against the current-block of all tainted pools first, then all
+blocks of all tainted pools.
+
+Return: TRUE iff tainted
+*/
+
+BOOL
+is_tainted_fn(const void * p)
+{
+storeblock * b;
+
+if (p == GET_UNTAINTED) return FALSE;
+if (p == GET_TAINTED) return TRUE;
+
+for (pooldesc * pp = paired_pools + POOL_TAINT_BASE;
+ pp < paired_pools + N_PAIRED_POOLS; pp++)
+ if ((b = pp->current_block))
+ if (is_pointer_in_block(b, p)) return TRUE;
+
+for (quoted_pooldesc * qp = quoted_pools; qp; qp = qp->next)
+ if (b = qp->pool.current_block)
+ if (is_pointer_in_block(b, p)) return TRUE;
+
+for (pooldesc * pp = paired_pools + POOL_TAINT_BASE;
+ pp < paired_pools + N_PAIRED_POOLS; pp++)
+ for (b = pp->chainbase; b; b = b->next)
+ if (is_pointer_in_block(b, p)) return TRUE;
+
+for (quoted_pooldesc * qp = quoted_pools; qp; qp = qp->next)
+ for (b = qp->pool.chainbase; b; b = b->next)
+ if (is_pointer_in_block(b, p)) return TRUE;
+
+return FALSE;
+}
+
+
+void
+die_tainted(const uschar * msg, const uschar * func, int line)
+{
+log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Taint mismatch, %s: %s %d\n",
+ msg, func, line);
+}
+
+
+#ifndef COMPILE_UTILITY
+/* Return the pool for the given quoter, or null */
+
+static pooldesc *
+pool_for_quoter(unsigned quoter)
+{
+for (quoted_pooldesc * qp = quoted_pools; qp; qp = qp->next)
+ if (qp->quoter == quoter)
+ return &qp->pool;
+return NULL;
+}
+
+/* Allocate/init a new quoted-pool and return the pool */
+
+static pooldesc *
+quoted_pool_new(unsigned quoter)
+{
+// debug_printf("allocating quoted-pool\n");
+quoted_pooldesc * qp = store_get_perm(sizeof(quoted_pooldesc), GET_UNTAINTED);
+
+pool_init(&qp->pool);
+qp->quoter = quoter;
+qp->next = quoted_pools;
+quoted_pools = qp;
+return &qp->pool;
+}
+#endif
+
+
+/******************************************************************************/
+void
+store_writeprotect(int pool)
+{
+#if !defined(COMPILE_UTILITY) && !defined(MISSING_POSIX_MEMALIGN)
+for (storeblock * b = paired_pools[pool].chainbase; b; b = b->next)
+ if (mprotect(b, ALIGNED_SIZEOF_STOREBLOCK + b->length, PROT_READ) != 0)
+ DEBUG(D_any) debug_printf("config block mprotect: (%d) %s\n", errno, strerror(errno));
+#endif
+}
+
+/******************************************************************************/
+
+static void *
+pool_get(pooldesc * pp, int size, BOOL align_mem, const char * func, int linenumber)
+{
+/* Ensure we've been asked to allocate memory.
+A negative size is a sign of a security problem.
+A zero size might be also suspect, but our internal usage deliberately
+does this to return a current watermark value for a later release of
+allocated store. */
+
+if (size < 0 || size >= INT_MAX/2)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "bad memory allocation requested (%d bytes) from %s %d",
+ size, func, 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 > pp->yield_length)
+ {
+ int length = MAX(
+ STORE_BLOCK_SIZE(pp->store_block_order) - ALIGNED_SIZEOF_STOREBLOCK,
+ size);
+ int mlength = length + ALIGNED_SIZEOF_STOREBLOCK;
+ storeblock * newblock;
+
+ /* Sometimes store_reset() may leave a block for us; check if we can use it */
+
+ if ( (newblock = pp->current_block)
+ && (newblock = newblock->next)
+ && newblock->length < length
+ )
+ {
+ /* Give up on this block, because it's too small */
+ pp->nblocks--;
+ internal_store_free(newblock, func, linenumber);
+ newblock = NULL;
+ }
+
+ /* If there was no free block, get a new one */
+
+ if (!newblock)
+ {
+ if ((pp->nbytes += mlength) > pp->maxbytes)
+ pp->maxbytes = pp->nbytes;
+ if ((pool_malloc += mlength) > max_pool_malloc) /* Used in pools */
+ max_pool_malloc = pool_malloc;
+ nonpool_malloc -= mlength; /* Exclude from overall total */
+ if (++pp->nblocks > pp->maxblocks)
+ pp->maxblocks = pp->nblocks;
+
+#ifndef MISSING_POSIX_MEMALIGN
+ if (align_mem)
+ {
+ long pgsize = sysconf(_SC_PAGESIZE);
+ int err = posix_memalign((void **)&newblock,
+ pgsize, (mlength + pgsize - 1) & ~(pgsize - 1));
+ if (err)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "failed to alloc (using posix_memalign) %d bytes of memory: '%s'"
+ "called from line %d in %s",
+ size, strerror(err), linenumber, func);
+ }
+ else
+#endif
+ newblock = internal_store_malloc(mlength, func, linenumber);
+ newblock->next = NULL;
+ newblock->length = length;
+#ifndef RESTRICTED_MEMORY
+ if (pp->store_block_order++ > pp->maxorder)
+ pp->maxorder = pp->store_block_order;
+#endif
+
+ if (! pp->chainbase)
+ pp->chainbase = newblock;
+ else
+ pp->current_block->next = newblock;
+ }
+
+ pp->current_block = newblock;
+ pp->yield_length = newblock->length;
+ pp->next_yield =
+ (void *)(CS pp->current_block + ALIGNED_SIZEOF_STOREBLOCK);
+ (void) VALGRIND_MAKE_MEM_NOACCESS(pp->next_yield, pp->yield_length);
+ }
+
+/* There's (now) enough room in the current block; the yield is the next
+pointer. */
+
+pp->store_last_get = pp->next_yield;
+
+(void) VALGRIND_MAKE_MEM_UNDEFINED(pp->store_last_get, size);
+/* Update next pointer and number of bytes left in the current block. */
+
+pp->next_yield = (void *)(CS pp->next_yield + size);
+pp->yield_length -= size;
+return pp->store_last_get;
+}
+
+/*************************************************
+* Get a block from the current pool *
+*************************************************/
+
+/* Running out of store is a total disaster. This function is called via the
+macro store_get(). The current store_pool is used, adjusting for taint.
+If the protoype is quoted, use a quoted-pool.
+Return a block of store within the current big block of the pool, getting a new
+one if necessary. The address is saved in store_last_get for the pool.
+
+Arguments:
+ size amount wanted, bytes
+ proto_mem class: get store conformant to this
+ Special values: 0 forces untainted, 1 forces tainted
+ func function from which called
+ linenumber line number in source file
+
+Returns: pointer to store (panic on malloc failure)
+*/
+
+void *
+store_get_3(int size, const void * proto_mem, const char * func, int linenumber)
+{
+#ifndef COMPILE_UTILITY
+int quoter = quoter_for_address(proto_mem);
+#endif
+pooldesc * pp;
+void * yield;
+
+#ifndef COMPILE_UTILITY
+if (!is_real_quoter(quoter))
+#endif
+ {
+ BOOL tainted = is_tainted(proto_mem);
+ int pool = tainted ? store_pool + POOL_TAINT_BASE : store_pool;
+ pp = paired_pools + pool;
+ yield = pool_get(pp, size, (pool == POOL_CONFIG), func, linenumber);
+
+ /* Cut out the debugging stuff for utilities, but stop picky compilers from
+ giving warnings. */
+
+#ifndef COMPILE_UTILITY
+ DEBUG(D_memory)
+ debug_printf("---%d Get %6p %5d %-14s %4d\n", pool,
+ pp->store_last_get, size, func, linenumber);
+#endif
+ }
+#ifndef COMPILE_UTILITY
+else
+ {
+ DEBUG(D_memory)
+ debug_printf("allocating quoted-block for quoter %u (from %s %d)\n",
+ quoter, func, linenumber);
+ if (!(pp = pool_for_quoter(quoter))) pp = quoted_pool_new(quoter);
+ yield = pool_get(pp, size, FALSE, func, linenumber);
+ DEBUG(D_memory)
+ debug_printf("---QQ Get %6p %5d %-14s %4d\n",
+ pp->store_last_get, size, func, linenumber);
+ }
+#endif
+return yield;
+}
+
+
+
+/*************************************************
+* 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
+ proto_mem class: get store conformant to this
+ func function 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 void * proto_mem, const char * func, int linenumber)
+{
+void * yield;
+int old_pool = store_pool;
+store_pool = POOL_PERM;
+yield = store_get_3(size, proto_mem, func, linenumber);
+store_pool = old_pool;
+return yield;
+}
+
+
+#ifndef COMPILE_UTILITY
+/*************************************************
+* Get a block annotated as being lookup-quoted *
+*************************************************/
+
+/* Allocate from pool a pool consistent with the proto_mem augmented by the
+requested quoter type.
+
+XXX currently not handling mark/release
+
+Args: size number of bytes to allocate
+ quoter id for the quoting type
+ func caller, for debug
+ linenumber caller, for debug
+
+Return: allocated memory block
+*/
+
+static void *
+store_force_get_quoted(int size, unsigned quoter,
+ const char * func, int linenumber)
+{
+pooldesc * pp = pool_for_quoter(quoter);
+void * yield;
+
+DEBUG(D_memory)
+ debug_printf("allocating quoted-block for quoter %u (from %s %d)\n", quoter, func, linenumber);
+
+if (!pp) pp = quoted_pool_new(quoter);
+yield = pool_get(pp, size, FALSE, func, linenumber);
+
+DEBUG(D_memory)
+ debug_printf("---QQ Get %6p %5d %-14s %4d\n",
+ pp->store_last_get, size, func, linenumber);
+
+return yield;
+}
+
+/* Maybe get memory for the specified quoter, but only if the
+prototype memory is tainted. Otherwise, get plain memory.
+*/
+void *
+store_get_quoted_3(int size, const void * proto_mem, unsigned quoter,
+ const char * func, int linenumber)
+{
+// debug_printf("store_get_quoted_3: quoter %u\n", quoter);
+return is_tainted(proto_mem)
+ ? store_force_get_quoted(size, quoter, func, linenumber)
+ : store_get_3(size, proto_mem, func, linenumber);
+}
+
+/* Return quoter for given address, or -1 if not in a quoted-pool. */
+int
+quoter_for_address(const void * p)
+{
+for (quoted_pooldesc * qp = quoted_pools; qp; qp = qp->next)
+ {
+ pooldesc * pp = &qp->pool;
+ storeblock * b;
+
+ if (b = pp->current_block)
+ if (is_pointer_in_block(b, p))
+ return qp->quoter;
+
+ for (b = pp->chainbase; b; b = b->next)
+ if (is_pointer_in_block(b, p))
+ return qp->quoter;
+ }
+return -1;
+}
+
+/* Return TRUE iff the given address is quoted for the given type.
+There is extra complexity to handle lookup providers with multiple
+find variants but shared quote functions. */
+BOOL
+is_quoted_like(const void * p, unsigned quoter)
+{
+int pq = quoter_for_address(p);
+BOOL y =
+ is_real_quoter(pq) && lookup_list[pq]->quote == lookup_list[quoter]->quote;
+/* debug_printf("is_quoted(%p, %u): %c\n", p, quoter, y?'T':'F'); */
+return y;
+}
+
+/* Return TRUE if the quoter value indicates an actual quoter */
+BOOL
+is_real_quoter(int quoter)
+{
+return quoter >= 0;
+}
+
+/* Return TRUE if the "new" data requires that the "old" data
+be recopied to new-class memory. We order the classes as
+
+ 2: tainted, not quoted
+ 1: quoted (which is also tainted)
+ 0: untainted
+
+If the "new" is higher-order than the "old", they are not compatible
+and a copy is needed. If both are quoted, but the quoters differ,
+not compatible. Otherwise they are compatible.
+*/
+BOOL
+is_incompatible_fn(const void * old, const void * new)
+{
+int oq, nq;
+unsigned oi, ni;
+
+ni = is_real_quoter(nq = quoter_for_address(new)) ? 1 : is_tainted(new) ? 2 : 0;
+oi = is_real_quoter(oq = quoter_for_address(old)) ? 1 : is_tainted(old) ? 2 : 0;
+return ni > oi || ni == oi && nq != oq;
+}
+
+#endif /*!COMPILE_UTILITY*/
+
+/*************************************************
+* 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 within 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.
+The block itself is not expanded; only the top allocation from it.
+
+Arguments:
+ ptr pointer to store block
+ oldsize current size of the block, as requested by user
+ newsize new size required
+ func function 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
+
+XXX needs extension for quoted-tracking. This assumes that the global store_pool
+is the one to alloc from, which breaks with separated pools.
+*/
+
+BOOL
+store_extend_3(void * ptr, int oldsize, int newsize,
+ const char * func, int linenumber)
+{
+pooldesc * pp = pool_for_pointer(ptr, func, linenumber);
+int inc = newsize - oldsize;
+int rounded_oldsize = oldsize;
+
+if (oldsize < 0 || newsize < oldsize || newsize >= INT_MAX/2)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "bad memory extension requested (%d -> %d bytes) at %s %d",
+ oldsize, newsize, func, linenumber);
+
+if (rounded_oldsize % alignment != 0)
+ rounded_oldsize += alignment - (rounded_oldsize % alignment);
+
+if (CS ptr + rounded_oldsize != CS (pp->next_yield) ||
+ inc > pp->yield_length + rounded_oldsize - oldsize)
+ return FALSE;
+
+/* Cut out the debugging stuff for utilities, but stop picky compilers from
+giving warnings. */
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_memory)
+ {
+ quoted_pooldesc * qp;
+ for (qp = quoted_pools; qp; qp = qp->next)
+ if (pp == &qp->pool)
+ {
+ debug_printf("---Q%d Ext %6p %5d %-14s %4d\n",
+ (int)(qp - quoted_pools),
+ ptr, newsize, func, linenumber);
+ break;
+ }
+ if (!qp)
+ debug_printf("---%d Ext %6p %5d %-14s %4d\n",
+ (int)(pp - paired_pools),
+ ptr, newsize, func, linenumber);
+ }
+#endif /* COMPILE_UTILITY */
+
+if (newsize % alignment != 0) newsize += alignment - (newsize % alignment);
+pp->next_yield = CS ptr + newsize;
+pp->yield_length -= newsize - rounded_oldsize;
+(void) VALGRIND_MAKE_MEM_UNDEFINED(ptr + oldsize, inc);
+return TRUE;
+}
+
+
+
+
+static BOOL
+is_pwr2_size(int len)
+{
+unsigned x = len;
+return (x & (x - 1)) == 0;
+}
+
+
+/*************************************************
+* Back up to a previous point on the stack *
+*************************************************/
+
+/* This function resets the next pointer, freeing any subsequent whole blocks
+that are now unused. Call with a cookie obtained from store_mark() only; do
+not call with a pointer returned by store_get(). Both the untainted and tainted
+pools corresposding to store_pool are reset.
+
+Quoted pools are not handled.
+
+Arguments:
+ ptr place to back up to
+ pool pool holding the pointer
+ func function from which called
+ linenumber line number in source file
+
+Returns: nothing
+*/
+
+static void
+internal_store_reset(void * ptr, int pool, const char *func, int linenumber)
+{
+storeblock * bb;
+pooldesc * pp = paired_pools + pool;
+storeblock * b = pp->current_block;
+char * bc = CS b + ALIGNED_SIZEOF_STOREBLOCK;
+int newlength, count;
+#ifndef COMPILE_UTILITY
+int oldmalloc = pool_malloc;
+#endif
+
+if (!b) return; /* exim_dumpdb gets this, becuse it has never used tainted mem */
+
+/* Last store operation was not a get */
+
+pp->store_last_get = 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 (CS ptr < bc || CS ptr > bc + b->length)
+ {
+ for (b = pp->chainbase; b; b = b->next)
+ {
+ bc = CS b + ALIGNED_SIZEOF_STOREBLOCK;
+ if (CS ptr >= bc && CS ptr <= bc + b->length) break;
+ }
+ if (!b)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "internal error: store_reset(%p) "
+ "failed: pool=%d %-14s %4d", ptr, pool, func, linenumber);
+ }
+
+/* Back up, rounding to the alignment if necessary. When testing, flatten
+the released memory. */
+
+newlength = bc + b->length - CS ptr;
+#ifndef COMPILE_UTILITY
+if (debug_store)
+ {
+ assert_no_variables(ptr, newlength, func, linenumber);
+ if (f.running_in_test_harness)
+ {
+ (void) VALGRIND_MAKE_MEM_DEFINED(ptr, newlength);
+ memset(ptr, 0xF0, newlength);
+ }
+ }
+#endif
+(void) VALGRIND_MAKE_MEM_NOACCESS(ptr, newlength);
+pp->next_yield = CS ptr + (newlength % alignment);
+count = pp->yield_length;
+count = (pp->yield_length = newlength - (newlength % alignment)) - count;
+pp->current_block = 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
+a power-of-two size so probably is not a custom inflated one. */
+
+if ( pp->yield_length < STOREPOOL_MIN_SIZE
+ && b->next
+ && is_pwr2_size(b->next->length + ALIGNED_SIZEOF_STOREBLOCK))
+ {
+ b = b->next;
+#ifndef COMPILE_UTILITY
+ if (debug_store)
+ assert_no_variables(b, b->length + ALIGNED_SIZEOF_STOREBLOCK,
+ func, linenumber);
+#endif
+ (void) VALGRIND_MAKE_MEM_NOACCESS(CS b + ALIGNED_SIZEOF_STOREBLOCK,
+ b->length - ALIGNED_SIZEOF_STOREBLOCK);
+ }
+
+bb = b->next;
+if (pool != POOL_CONFIG)
+ b->next = NULL;
+
+while ((b = bb))
+ {
+ int siz = b->length + ALIGNED_SIZEOF_STOREBLOCK;
+
+#ifndef COMPILE_UTILITY
+ if (debug_store)
+ assert_no_variables(b, b->length + ALIGNED_SIZEOF_STOREBLOCK,
+ func, linenumber);
+#endif
+ bb = bb->next;
+ pp->nbytes -= siz;
+ pool_malloc -= siz;
+ pp->nblocks--;
+ if (pool != POOL_CONFIG)
+ internal_store_free(b, func, linenumber);
+
+#ifndef RESTRICTED_MEMORY
+ if (pp->store_block_order > 13) pp->store_block_order--;
+#endif
+ }
+
+/* Cut out the debugging stuff for utilities, but stop picky compilers from
+giving warnings. */
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_memory)
+ debug_printf("---%d Rst %6p %5d %-14s %4d\tpool %d\n", pool, ptr,
+ count + oldmalloc - pool_malloc,
+ func, linenumber, pool_malloc);
+#endif /* COMPILE_UTILITY */
+}
+
+
+/* Back up the pool pair, untainted and tainted, of the store_pool setting.
+Quoted pools are not handled.
+*/
+
+rmark
+store_reset_3(rmark r, const char * func, int linenumber)
+{
+void ** ptr = r;
+
+if (store_pool >= POOL_TAINT_BASE)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "store_reset called for pool %d: %s %d\n", store_pool, func, linenumber);
+if (!r)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "store_reset called with bad mark: %s %d\n", func, linenumber);
+
+internal_store_reset(*ptr, store_pool + POOL_TAINT_BASE, func, linenumber);
+internal_store_reset(ptr, store_pool, func, linenumber);
+return NULL;
+}
+
+
+/**************/
+
+/* Free tail-end unused allocation. This lets us allocate a big chunk
+early, for cases when we only discover later how much was really needed.
+
+Can be called with a value from store_get(), or an offset after such. Only
+the tainted or untainted pool that serviced the store_get() will be affected.
+
+This is mostly a cut-down version of internal_store_reset().
+XXX needs rationalising
+*/
+
+void
+store_release_above_3(void * ptr, const char * func, int linenumber)
+{
+pooldesc * pp;
+
+/* Search all pools' "current" blocks. If it isn't one of those,
+ignore it (it usually will be). */
+
+if ((pp = pool_current_for_pointer(ptr)))
+ {
+ storeblock * b = pp->current_block;
+ int count, newlength;
+
+ /* Last store operation was not a get */
+
+ pp->store_last_get = NULL;
+
+ /* Back up, rounding to the alignment if necessary. When testing, flatten
+ the released memory. */
+
+ newlength = (CS b + ALIGNED_SIZEOF_STOREBLOCK) + b->length - CS ptr;
+#ifndef COMPILE_UTILITY
+ if (debug_store)
+ {
+ assert_no_variables(ptr, newlength, func, linenumber);
+ if (f.running_in_test_harness)
+ {
+ (void) VALGRIND_MAKE_MEM_DEFINED(ptr, newlength);
+ memset(ptr, 0xF0, newlength);
+ }
+ }
+#endif
+ (void) VALGRIND_MAKE_MEM_NOACCESS(ptr, newlength);
+ pp->next_yield = CS ptr + (newlength % alignment);
+ count = pp->yield_length;
+ count = (pp->yield_length = newlength - (newlength % alignment)) - count;
+
+ /* Cut out the debugging stuff for utilities, but stop picky compilers from
+ giving warnings. */
+
+#ifndef COMPILE_UTILITY
+ DEBUG(D_memory)
+ {
+ quoted_pooldesc * qp;
+ for (qp = quoted_pools; qp; qp = qp->next)
+ if (pp == &qp->pool)
+ debug_printf("---Q%d Rel %6p %5d %-14s %4d\tpool %d\n",
+ (int)(qp - quoted_pools),
+ ptr, count, func, linenumber, pool_malloc);
+ if (!qp)
+ debug_printf("---%d Rel %6p %5d %-14s %4d\tpool %d\n",
+ (int)(pp - paired_pools), ptr, count,
+ func, linenumber, pool_malloc);
+ }
+#endif
+ return;
+ }
+#ifndef COMPILE_UTILITY
+DEBUG(D_memory)
+ debug_printf("non-last memory release try: %s %d\n", func, linenumber);
+#endif
+}
+
+
+
+rmark
+store_mark_3(const char * func, int linenumber)
+{
+void ** p;
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_memory)
+ debug_printf("---%d Mrk %-14s %4d\tpool %d\n",
+ store_pool, func, linenumber, pool_malloc);
+#endif /* COMPILE_UTILITY */
+
+if (store_pool >= POOL_TAINT_BASE)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "store_mark called for pool %d: %s %d\n", store_pool, func, linenumber);
+
+/* Stash a mark for the tainted-twin release, in the untainted twin. Return
+a cookie (actually the address in the untainted pool) to the caller.
+Reset uses the cookie to recover the t-mark, winds back the tainted pool with it
+and winds back the untainted pool with the cookie. */
+
+p = store_get_3(sizeof(void *), GET_UNTAINTED, func, linenumber);
+*p = store_get_3(0, GET_TAINTED, func, linenumber);
+return p;
+}
+
+
+
+
+/************************************************
+* Release store *
+************************************************/
+
+/* 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
+ pp pool containing the block
+ func function from which called
+ linenumber line number in source file
+
+Returns: nothing
+*/
+
+static void
+store_release_3(void * block, pooldesc * pp, const char * func, int linenumber)
+{
+/* It will never be the first block, so no need to check that. */
+
+for (storeblock * b = pp->chainbase; b; b = b->next)
+ {
+ storeblock * bb = b->next;
+ if (bb && CS block == CS bb + ALIGNED_SIZEOF_STOREBLOCK)
+ {
+ int siz = bb->length + ALIGNED_SIZEOF_STOREBLOCK;
+ b->next = bb->next;
+ pp->nbytes -= siz;
+ pool_malloc -= siz;
+ pp->nblocks--;
+
+ /* Cut out the debugging stuff for utilities, but stop picky compilers
+ from giving warnings. */
+
+#ifndef COMPILE_UTILITY
+ DEBUG(D_memory)
+ debug_printf("-Release %6p %-20s %4d %d\n", (void *)bb, func,
+ linenumber, pool_malloc);
+
+ if (f.running_in_test_harness)
+ memset(bb, 0xF0, bb->length+ALIGNED_SIZEOF_STOREBLOCK);
+#endif /* COMPILE_UTILITY */
+
+ internal_store_free(bb, func, linenumber);
+ return;
+ }
+ }
+}
+
+
+/************************************************
+* Move store *
+************************************************/
+
+/* Allocate a new block big enough to expend to the given size and
+copy the current data into it. Free the old one if possible.
+
+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 that nothing
+has been allocated since. If so, releases that block.
+
+Arguments:
+ oldblock
+ newsize requested size
+ len current size
+
+Returns: new location of data
+*/
+
+void *
+store_newblock_3(void * oldblock, int newsize, int len,
+ const char * func, int linenumber)
+{
+pooldesc * pp = pool_for_pointer(oldblock, func, linenumber);
+BOOL release_ok = !is_tainted(oldblock) && pp->store_last_get == oldblock; /*XXX why tainted not handled? */
+uschar * newblock;
+
+if (len < 0 || len > newsize)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "bad memory extension requested (%d -> %d bytes) at %s %d",
+ len, newsize, func, linenumber);
+
+newblock = store_get(newsize, oldblock);
+memcpy(newblock, oldblock, len);
+if (release_ok) store_release_3(oldblock, pp, func, linenumber);
+return (void *)newblock;
+}
+
+
+
+
+/*************************************************
+* 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
+ func function from which called
+ line line number in source file
+
+Returns: pointer to gotten store (panic on failure)
+*/
+
+static void *
+internal_store_malloc(size_t size, const char *func, int line)
+{
+void * yield;
+
+/* Check specifically for a possibly result of conversion from
+a negative int, to the (unsigned, wider) size_t */
+
+if (size >= INT_MAX/2)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "bad internal_store_malloc request (" SIZE_T_FMT " bytes) from %s %d",
+ size, func, line);
+
+size += sizeof(size_t); /* space to store the size, used under debug */
+if (size < 16) size = 16;
+
+if (!(yield = malloc(size)))
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to malloc " SIZE_T_FMT " bytes of memory: "
+ "called from line %d in %s", size, line, func);
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_any) *(size_t *)yield = size;
+#endif
+yield = US yield + sizeof(size_t);
+
+if ((nonpool_malloc += size) > max_nonpool_malloc)
+ max_nonpool_malloc = nonpool_malloc;
+
+/* Cut out the debugging stuff for utilities, but stop picky compilers from
+giving warnings. */
+
+#ifndef COMPILE_UTILITY
+/* If running in test harness, spend time making sure all the new store
+is not filled with zeros so as to catch problems. */
+
+if (f.running_in_test_harness)
+ memset(yield, 0xF0, size - sizeof(size_t));
+DEBUG(D_memory) debug_printf("--Malloc %6p %5lu bytes\t%-20s %4d\tpool %5d nonpool %5d\n",
+ yield, size, func, line, pool_malloc, nonpool_malloc);
+#endif /* COMPILE_UTILITY */
+
+return yield;
+}
+
+void *
+store_malloc_3(size_t size, const char *func, int linenumber)
+{
+if (n_nonpool_blocks++ > max_nonpool_blocks)
+ max_nonpool_blocks = n_nonpool_blocks;
+return internal_store_malloc(size, func, linenumber);
+}
+
+
+/************************************************
+* Free store *
+************************************************/
+
+/* This function is called by the macro store_free().
+
+Arguments:
+ block block of store to free
+ func function from which called
+ linenumber line number in source file
+
+Returns: nothing
+*/
+
+static void
+internal_store_free(void * block, const char * func, int linenumber)
+{
+uschar * p = US block - sizeof(size_t);
+#ifndef COMPILE_UTILITY
+DEBUG(D_any) nonpool_malloc -= *(size_t *)p;
+DEBUG(D_memory) debug_printf("----Free %6p %5ld bytes\t%-20s %4d\n",
+ block, *(size_t *)p, func, linenumber);
+#endif
+free(p);
+}
+
+void
+store_free_3(void * block, const char * func, int linenumber)
+{
+n_nonpool_blocks--;
+internal_store_free(block, func, linenumber);
+}
+
+/******************************************************************************/
+/* Stats output on process exit */
+void
+store_exit(void)
+{
+#ifndef COMPILE_UTILITY
+DEBUG(D_memory)
+ {
+ int i;
+ debug_printf("----Exit nonpool max: %3d kB in %d blocks\n",
+ (max_nonpool_malloc+1023)/1024, max_nonpool_blocks);
+ debug_printf("----Exit npools max: %3d kB\n", max_pool_malloc/1024);
+
+ for (i = 0; i < N_PAIRED_POOLS; i++)
+ {
+ pooldesc * pp = paired_pools + i;
+ debug_printf("----Exit pool %2d max: %3d kB in %d blocks at order %u\t%s %s\n",
+ i, (pp->maxbytes+1023)/1024, pp->maxblocks, pp->maxorder,
+ poolclass[i], pooluse[i]);
+ }
+ i = 0;
+ for (quoted_pooldesc * qp = quoted_pools; qp; i++, qp = qp->next)
+ {
+ pooldesc * pp = &qp->pool;
+ debug_printf("----Exit pool Q%d max: %3d kB in %d blocks at order %u\ttainted quoted:%s\n",
+ i, (pp->maxbytes+1023)/1024, pp->maxblocks, pp->maxorder, lookup_list[qp->quoter]->name);
+ }
+ }
+#endif
+}
+
+
+/******************************************************************************/
+/* Per-message pool management */
+
+static rmark message_reset_point = NULL;
+
+void
+message_start(void)
+{
+int oldpool = store_pool;
+store_pool = POOL_MESSAGE;
+if (!message_reset_point) message_reset_point = store_mark();
+store_pool = oldpool;
+}
+
+void
+message_tidyup(void)
+{
+int oldpool;
+if (!message_reset_point) return;
+oldpool = store_pool;
+store_pool = POOL_MESSAGE;
+message_reset_point = store_reset(message_reset_point);
+store_pool = oldpool;
+}
+
+/******************************************************************************/
+/* Debug analysis of address */
+
+#ifndef COMPILE_UTILITY
+void
+debug_print_taint(const void * p)
+{
+int q = quoter_for_address(p);
+if (!is_tainted(p)) return;
+debug_printf("(tainted");
+if (is_real_quoter(q)) debug_printf(", quoted:%s", lookup_list[q]->name);
+debug_printf(")\n");
+}
+#endif
+
+/* End of store.c */
diff --git a/src/store.h b/src/store.h
new file mode 100644
index 0000000..ee6d79c
--- /dev/null
+++ b/src/store.h
@@ -0,0 +1,89 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* 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_CONFIG,
+ POOL_SEARCH,
+ POOL_MESSAGE,
+
+ POOL_TAINT_BASE,
+
+ POOL_TAINT_MAIN = POOL_TAINT_BASE,
+ POOL_TAINT_PERM,
+ POOL_TAINT_CONFIG,
+ POOL_TAINT_SEARCH,
+ POOL_TAINT_MESSAGE,
+
+ N_PAIRED_POOLS
+};
+
+/* 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[];
+
+/* 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, __FUNCTION__, __LINE__)
+
+#define store_free(addr) \
+ store_free_3(addr, __FUNCTION__, __LINE__)
+/* store_get & store_get_perm are in local_scan.h */
+#define store_get_quoted(size, proto_mem, quoter) \
+ store_get_quoted_3((size), (proto_mem), (quoter), __FUNCTION__, __LINE__)
+#define store_malloc(size) \
+ store_malloc_3(size, __FUNCTION__, __LINE__)
+#define store_mark(void) \
+ store_mark_3(__FUNCTION__, __LINE__)
+#define store_newblock(oldblock, newsize, datalen) \
+ store_newblock_3(oldblock, newsize, datalen, __FUNCTION__, __LINE__)
+#define store_release_above(addr) \
+ store_release_above_3(addr, __FUNCTION__, __LINE__)
+#define store_reset(mark) \
+ store_reset_3(mark, __FUNCTION__, __LINE__)
+
+
+/* The real functions */
+typedef void ** rmark;
+
+extern BOOL store_extend_3(void *, int, int, const char *, int);
+extern void store_free_3(void *, const char *, int);
+/* store_get_3 & store_get_perm_3 are in local_scan.h */
+extern void * store_get_quoted_3(int, const void *, unsigned, const char *, int);
+extern void * store_malloc_3(size_t, const char *, int) ALLOC ALLOC_SIZE(1) WARN_UNUSED_RESULT;
+extern rmark store_mark_3(const char *, int);
+extern void * store_newblock_3(void *, int, int, const char *, int);
+extern void store_release_above_3(void *, const char *, int);
+extern rmark store_reset_3(rmark, const char *, int);
+
+#define GET_UNTAINTED (const void *)0
+#define GET_TAINTED (const void *)1
+
+extern int quoter_for_address(const void *);
+extern BOOL is_quoted_like(const void *, unsigned);
+extern BOOL is_real_quoter(int);
+extern void debug_print_taint(const void * p);
+
+#endif /* STORE_H */
+
+/* End of store.h */
diff --git a/src/string.c b/src/string.c
new file mode 100644
index 0000000..a5161bb
--- /dev/null
+++ b/src/string.c
@@ -0,0 +1,1859 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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"
+#include <assert.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 yield = 4;
+
+/* If an optional mask is permitted, check for it. If found, pass back the
+offset. */
+
+if (maskptr)
+ {
+ 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;
+
+ 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 (int 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 (int i = 0; i < 4; i++)
+ {
+ long n;
+ uschar * end;
+
+ if (i != 0 && *s++ != '.') return 0;
+ n = strtol(CCS s, CSS &end, 10);
+ if (n > 255 || n < 0 || end <= s || end > s+3) return 0;
+ s = end;
+ }
+
+return !*s || (*s == '/' && maskptr && *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(buffer, US" ");
+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
+ If the backslash is the last character in the string, it
+ is not interpreted.
+Returns: the value of the character escape
+*/
+
+int
+string_interpret_escape(const uschar **pp)
+{
+#ifdef COMPILE_UTILITY
+const uschar *hex_digits= CUS"0123456789abcdef";
+#endif
+int ch;
+const uschar *p = *pp;
+ch = *(++p);
+if (ch == '\0') return **pp;
+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 flags to 0.
+
+Arguments:
+ s the input string
+ flags Bit 0: convert tabs. Bit 1: convert spaces.
+
+Returns: string with non-printers encoded as printing sequences
+*/
+
+const uschar *
+string_printing2(const uschar *s, int flags)
+{
+int nonprintcount = 0;
+int length = 0;
+const uschar *t = s;
+uschar *ss, *tt;
+
+while (*t)
+ {
+ int c = *t++;
+ if ( !mac_isprint(c)
+ || flags & SP_TAB && c == '\t'
+ || flags & SP_SPACE && c == ' '
+ ) nonprintcount++;
+ length++;
+ }
+
+if (nonprintcount == 0) return s;
+
+/* Get a new block of store guaranteed big enough to hold the
+expanded string. */
+
+tt = ss = store_get(length + nonprintcount * 3 + 1, s);
+
+/* Copy everything, escaping non printers. */
+
+for (t = s; *t; )
+ {
+ int c = *t;
+ if ( mac_isprint(c)
+ && (!(flags & SP_TAB) || c != '\t')
+ && (!(flags & SP_SPACE) || c != ' ')
+ )
+ *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, s);
+
+q = ss;
+off = p - s;
+if (off)
+ {
+ memcpy(q, s, off);
+ q += off;
+ }
+
+while (*p)
+ {
+ if (*p == '\\')
+ {
+ *q++ = string_interpret_escape((const uschar **)&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;
+}
+
+
+
+
+#if (defined(HAVE_LOCAL_SCAN) || defined(EXPAND_DLFUNC)) \
+ && !defined(MACRO_PREDEF) && !defined(COMPILE_UTILITY)
+/*************************************************
+* Copy and save string *
+*************************************************/
+
+/*
+Argument: string to copy
+Returns: copy of string in new store with the same taint status
+*/
+
+uschar *
+string_copy_function(const uschar * s)
+{
+return string_copy_taint(s, s);
+}
+
+/* As above, but explicitly specifying the result taint status
+*/
+
+uschar *
+string_copy_taint_function(const uschar * s, const void * proto_mem)
+{
+return string_copy_taint(s, proto_mem);
+}
+
+
+
+/*************************************************
+* 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_function(const uschar * s, int n)
+{
+return string_copyn(s, n);
+}
+#endif
+
+
+/*************************************************
+* 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(const uschar * s)
+{
+int len = Ustrlen(s) + 1;
+uschar * ss = store_malloc(len);
+memcpy(ss, s, len);
+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 || Ustrlen(msg) <= 75) return msg;
+s = ss = msg = string_copy(msg);
+
+for (;;)
+ {
+ int i = 0;
+ while (i < 75 && *ss && *ss != '\n') ss++, i++;
+ if (!*ss) 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) tt = t;
+ }
+ }
+
+ if (!tt) /* Can't split behind - try ahead */
+ {
+ t = ss + 1;
+ while (*t)
+ {
+ if (*t == ' ' || *t == '\n')
+ { tt = t; break; }
+ t++;
+ }
+ }
+
+ if (!tt) 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, GET_TAINTED); /* always treat as tainted */
+
+while (*s)
+ {
+ 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)
+ *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(const uschar ** sptr)
+{
+const uschar * s = * sptr;
+uschar * t, * yield;
+
+/* First find the end of the string */
+
+if (*s != '\"')
+ while (*s && !isspace(*s)) s++;
+else
+ {
+ s++;
+ while (*s && *s != '\"')
+ {
+ if (*s == '\\') (void)string_interpret_escape(&s);
+ s++;
+ }
+ if (*s) s++;
+ }
+
+/* Get enough store to copy into */
+
+t = yield = store_get(s - *sptr + 1, *sptr);
+s = *sptr;
+
+/* Do the copy */
+
+if (*s != '\"')
+ while (*s && !isspace(*s)) *t++ = *s++;
+else
+ {
+ s++;
+ while (*s && *s != '\"')
+ {
+ *t++ = *s == '\\' ? string_interpret_escape(&s) : *s;
+ s++;
+ }
+ if (*s) 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_vformat, which checks the length of
+everything. Taint is taken from the worst of the arguments.
+
+Arguments:
+ format a printf() format - deliberately char * rather than uschar *
+ because it will most usually be a literal string
+ func caller, for debug
+ line caller, for debug
+ ... arguments for format
+
+Returns: pointer to fresh piece of store containing sprintf'ed string
+*/
+
+uschar *
+string_sprintf_trc(const char * format, const uschar * func, unsigned line, ...)
+{
+#ifdef COMPILE_UTILITY
+uschar buffer[STRING_SPRINTF_BUFFER_SIZE];
+gstring gs = { .size = STRING_SPRINTF_BUFFER_SIZE, .ptr = 0, .s = buffer };
+gstring * g = &gs;
+unsigned flags = 0;
+#else
+gstring * g = NULL;
+unsigned flags = SVFMT_REBUFFER|SVFMT_EXTEND;
+#endif
+
+va_list ap;
+va_start(ap, line);
+g = string_vformat_trc(g, func, line, STRING_SPRINTF_BUFFER_SIZE,
+ flags, format, ap);
+va_end(ap);
+
+if (!g)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "string_sprintf expansion was longer than %d; format string was (%s)\n"
+ " called from %s %d\n",
+ STRING_SPRINTF_BUFFER_SIZE, format, func, line);
+
+#ifdef COMPILE_UTILITY
+return string_copyn(g->s, g->ptr);
+#else
+gstring_release_unused(g);
+return string_from_gstring(g);
+#endif
+}
+
+
+
+/*************************************************
+* 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)
+ {
+ 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
+*/
+
+const uschar *
+strstric_c(const uschar * s, const uschar * t, BOOL space_follows)
+{
+const uschar * p = t;
+const uschar * yield = NULL;
+int cl = tolower(*p);
+int cu = toupper(*p);
+
+while (*s)
+ {
+ if (*s == cl || *s == cu)
+ {
+ if (!yield) yield = s;
+ if (!*++p)
+ {
+ if (!space_follows || s[1] == ' ' || s[1] == '\n' ) return yield;
+ yield = NULL;
+ p = t;
+ }
+ cl = tolower(*p);
+ cu = toupper(*p);
+ s++;
+ }
+ else if (yield)
+ {
+ yield = NULL;
+ p = t;
+ cl = tolower(*p);
+ cu = toupper(*p);
+ }
+ else s++;
+ }
+return NULL;
+}
+
+uschar *
+strstric(uschar * s, uschar * t, BOOL space_follows)
+{
+return US strstric_c(s, t, space_follows);
+}
+
+
+#ifdef COMPILE_UTILITY
+/* Dummy version for this function; it should never be called */
+static void
+gstring_grow(gstring * g, int count)
+{
+assert(FALSE);
+}
+#endif
+
+
+
+#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
+ Note that if the list is tainted then a provided buffer must be
+ also (else we trap, with a message referencing the callsite).
+ If we do the allocation, taint is handled there.
+ buflen when buffer is not NULL, the size of buffer; otherwise ignored
+
+ func caller, for debug
+ line caller, for debug
+
+Returns: pointer to buffer, containing the next substring,
+ or NULL if no more substrings
+*/
+
+uschar *
+string_nextinlist_trc(const uschar ** listptr, int * separator, uschar * buffer,
+ int buflen, const uschar * func, int line)
+{
+int sep = *separator;
+const uschar * s = *listptr;
+BOOL sep_is_special;
+
+if (!s) 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];
+ if (*++s) ++s;
+ while (isspace(*s) && *s != sep) s++;
+ }
+ else
+ sep = sep ? -sep : ':';
+ *separator = sep;
+ }
+
+/* An empty string has no list elements */
+
+if (!*s) 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. */
+/*XXX need to also deal with qouted-requirements mismatch */
+
+if (buffer)
+ {
+ int p = 0;
+ if (is_tainted(s) && !is_tainted(buffer))
+ die_tainted(US"string_nextinlist", func, line);
+ for (; *s; 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
+ {
+ gstring * g = NULL;
+
+ /* 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)
+ 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 (;;)
+ {
+ const uschar * ss;
+ for (ss = s + 1; *ss && *ss != sep; ) ss++;
+ g = string_catn(g, s, ss-s);
+ s = ss;
+ if (!*s || *++s != sep || sep_is_special) break;
+ }
+
+ /* Trim trailing spaces from the returned string */
+
+ /* while (g->ptr > 0 && isspace(g->s[g->ptr-1])) g->ptr--; */
+ while ( g->ptr > 0 && isspace(g->s[g->ptr-1])
+ && (g->ptr == 1 || g->s[g->ptr-2] != '\\') )
+ g->ptr--;
+ buffer = string_from_gstring(g);
+ gstring_release_unused_trc(g, CCS func, line);
+ }
+
+/* Update the current pointer and return the new string */
+
+*listptr = s;
+return buffer;
+}
+
+
+static const uschar *
+Ustrnchr(const uschar * s, int c, unsigned * len)
+{
+unsigned siz = *len;
+while (siz)
+ {
+ if (!*s) return NULL;
+ if (*s == c)
+ {
+ *len = siz;
+ return s;
+ }
+ s++;
+ siz--;
+ }
+return NULL;
+}
+
+
+/************************************************
+* Add element to separated list *
+************************************************/
+/* This function is used to build a list, returning an allocated null-terminated
+growable string. The given element has any embedded separator characters
+doubled.
+
+Despite having the same growable-string interface as string_cat() the list is
+always returned null-terminated.
+
+Arguments:
+ list expanding-string for the list that is being built, or NULL
+ if this is a new list that has no contents yet
+ sep list separator character
+ ele new element to be appended to the list
+
+Returns: pointer to the start of the list, changed if copied for expansion.
+*/
+
+gstring *
+string_append_listele(gstring * list, uschar sep, const uschar * ele)
+{
+uschar * sp;
+
+if (list && list->ptr)
+ list = string_catn(list, &sep, 1);
+
+while((sp = Ustrchr(ele, sep)))
+ {
+ list = string_catn(list, ele, sp-ele+1);
+ list = string_catn(list, &sep, 1);
+ ele = sp+1;
+ }
+list = string_cat(list, ele);
+(void) string_from_gstring(list);
+return list;
+}
+
+
+gstring *
+string_append_listele_n(gstring * list, uschar sep, const uschar * ele,
+ unsigned len)
+{
+const uschar * sp;
+
+if (list && list->ptr)
+ list = string_catn(list, &sep, 1);
+
+while((sp = Ustrnchr(ele, sep, &len)))
+ {
+ list = string_catn(list, ele, sp-ele+1);
+ list = string_catn(list, &sep, 1);
+ ele = sp+1;
+ len--;
+ }
+list = string_catn(list, ele, len);
+(void) string_from_gstring(list);
+return list;
+}
+
+
+
+/* A slightly-bogus listmaker utility; the separator is a string so
+can be multiple chars - there is no checking for the element content
+containing any of the separator. */
+
+gstring *
+string_append2_listele_n(gstring * list, const uschar * sepstr,
+ const uschar * ele, unsigned len)
+{
+if (list && list->ptr)
+ list = string_cat(list, sepstr);
+
+list = string_catn(list, ele, len);
+(void) string_from_gstring(list);
+return list;
+}
+
+
+
+/************************************************/
+/* Add more space to a growable-string. The caller should check
+first if growth is required. The gstring struct is modified on
+return; specifically, the string-base-pointer may have been changed.
+
+Arguments:
+ g the growable-string
+ count amount needed for g->ptr to increase by
+*/
+
+static void
+gstring_grow(gstring * g, int count)
+{
+int p = g->ptr;
+int oldsize = g->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. */
+
+unsigned inc = oldsize < 4096 ? 127 : 1023;
+
+if (g->ptr < 0 || g->ptr > g->size || g->size >= INT_MAX/2)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "internal error in gstring_grow (ptr %d size %d)", g->ptr, g->size);
+
+if (count <= 0) return;
+
+if (count >= INT_MAX/2 - g->ptr)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "internal error in gstring_grow (ptr %d count %d)", g->ptr, count);
+
+g->size = (p + count + inc + 1) & ~inc; /* one for a NUL */
+
+/* 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. */
+
+if (!store_extend(g->s, oldsize, g->size))
+ g->s = store_newblock(g->s, g->size, p);
+}
+
+
+
+/*************************************************
+* 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:
+ g growable-string that is being built, or NULL if not assigned yet
+ 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.
+
+Returns: growable 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.
+ NULL is a possible return.
+
+*/
+/* coverity[+alloc] */
+
+gstring *
+string_catn(gstring * g, const uschar * s, int count)
+{
+int p;
+
+if (count < 0)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "internal error in string_catn (count %d)", count);
+if (count == 0) return g;
+
+/*debug_printf("string_catn '%.*s'\n", count, s);*/
+if (!g)
+ {
+ unsigned inc = count < 4096 ? 127 : 1023;
+ unsigned size = ((count + inc) & ~inc) + 1; /* round up requested count */
+ g = string_get_tainted(size, s);
+ }
+else if (!g->s) /* should not happen */
+ {
+ g->s = string_copyn(s, count);
+ g->ptr = count;
+ g->size = count; /*XXX suboptimal*/
+ return g;
+ }
+else if (is_incompatible(g->s, s))
+ {
+/* debug_printf("rebuf A\n"); */
+ gstring_rebuffer(g, s);
+ }
+
+if (g->ptr < 0 || g->ptr > g->size)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "internal error in string_catn (ptr %d size %d)", g->ptr, g->size);
+
+p = g->ptr;
+if (count >= g->size - p)
+ gstring_grow(g, count);
+
+/* 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(g->s + p, s, count);
+g->ptr = p + count;
+return g;
+}
+
+
+gstring *
+string_cat(gstring * g, const uschar * s)
+{
+return string_catn(g, s, Ustrlen(s));
+}
+
+
+
+/*************************************************
+* 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:
+ g growable-string that is being built, or NULL if not yet assigned
+ count the number of strings to append
+ ... "count" uschar* arguments, which must be valid zero-terminated
+ C strings
+
+Returns: growable string, changed if copied for expansion.
+ The string is not zero-terminated - see string_cat() above.
+*/
+
+__inline__ gstring *
+string_append(gstring * g, int count, ...)
+{
+va_list ap;
+
+va_start(ap, count);
+while (count-- > 0)
+ {
+ uschar * t = va_arg(ap, uschar *);
+ g = string_cat(g, t);
+ }
+va_end(ap);
+
+return g;
+}
+#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
+three additions for strings: %S forces lower case, %T forces upper 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_trc(uschar * buffer, int buflen,
+ const uschar * func, unsigned line, const char * format, ...)
+{
+gstring g = { .size = buflen, .ptr = 0, .s = buffer }, * gp;
+va_list ap;
+va_start(ap, format);
+gp = string_vformat_trc(&g, func, line, STRING_SPRINTF_BUFFER_SIZE,
+ 0, format, ap);
+va_end(ap);
+g.s[g.ptr] = '\0';
+return !!gp;
+}
+
+
+
+
+/* Build or append to a growing-string, sprintf-style.
+
+Arguments:
+ g a growable-string
+ func called-from function name, for debug
+ line called-from file line number, for debug
+ limit maximum string size
+ flags see below
+ format printf-like format string
+ ap variable-args pointer
+
+Flags:
+ SVFMT_EXTEND buffer can be created or exteded as needed
+ SVFMT_REBUFFER buffer can be recopied to tainted mem as needed
+ SVFMT_TAINT_NOCHK do not check inputs for taint
+
+If the "extend" flag is true, the string passed in can be NULL,
+empty, or non-empty. Growing is subject to an overall limit given
+by the limit argument.
+
+If the "extend" flag is false, the string passed in may not be NULL,
+will not be grown, and is usable in the original place after return.
+The return value can be NULL to signify overflow.
+
+Returns the possibly-new (if copy for growth or taint-handling was needed)
+string, not nul-terminated.
+*/
+
+gstring *
+string_vformat_trc(gstring * g, const uschar * func, unsigned line,
+ unsigned size_limit, unsigned flags, const char * format, va_list ap)
+{
+enum ltypes { L_NORMAL=1, L_SHORT=2, L_LONG=3, L_LONGLONG=4, L_LONGDOUBLE=5, L_SIZE=6 };
+
+int width, precision, off, lim, need;
+const char * fp = format; /* Deliberately not unsigned */
+
+string_datestamp_offset = -1; /* Datestamp not inserted */
+string_datestamp_length = 0; /* Datestamp not inserted */
+string_datestamp_type = 0; /* Datestamp not inserted */
+
+#ifdef COMPILE_UTILITY
+assert(!(flags & SVFMT_EXTEND));
+assert(g);
+#else
+
+/* Ensure we have a string, to save on checking later */
+if (!g) g = string_get(16);
+
+if (!(flags & SVFMT_TAINT_NOCHK) && is_incompatible(g->s, format))
+ {
+#ifndef MACRO_PREDEF
+ if (!(flags & SVFMT_REBUFFER))
+ die_tainted(US"string_vformat", func, line);
+#endif
+/* debug_printf("rebuf B\n"); */
+ gstring_rebuffer(g, format);
+ }
+#endif /*!COMPILE_UTILITY*/
+
+lim = g->size - 1; /* leave one for a nul */
+off = g->ptr; /* remember initial offset in gstring */
+
+/* Scan the format and handle the insertions */
+
+while (*fp)
+ {
+ 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 */
+ char * gp = CS g->s + g->ptr; /* ) */
+
+ /* Non-% characters just get copied verbatim */
+
+ if (*fp != '%')
+ {
+ /* Avoid string_copyn() due to COMPILE_UTILITY */
+ if ((need = g->ptr + 1) > lim)
+ {
+ if (!(flags & SVFMT_EXTEND) || need > size_limit) return NULL;
+ gstring_grow(g, 1);
+ lim = g->size - 1;
+ }
+ g->s[g->ptr++] = (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
+ for (precision = 0; isdigit((uschar)*fp); 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 = g->ptr - off;
+ break;
+
+ case 'd':
+ case 'o':
+ case 'u':
+ case 'x':
+ case 'X':
+ width = length > L_LONG ? 24 : 12;
+ if ((need = g->ptr + width) > lim)
+ {
+ if (!(flags & SVFMT_EXTEND) || need >= size_limit) return NULL;
+ gstring_grow(g, width);
+ lim = g->size - 1;
+ gp = CS g->s + g->ptr;
+ }
+ 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:
+ g->ptr += sprintf(gp, newformat, va_arg(ap, int)); break;
+ case L_LONG:
+ g->ptr += sprintf(gp, newformat, va_arg(ap, long int)); break;
+ case L_LONGLONG:
+ g->ptr += sprintf(gp, newformat, va_arg(ap, LONGLONG_T)); break;
+ case L_SIZE:
+ g->ptr += sprintf(gp, newformat, va_arg(ap, size_t)); break;
+ }
+ break;
+
+ case 'p':
+ {
+ void * ptr;
+ if ((need = g->ptr + 24) > lim)
+ {
+ if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
+ gstring_grow(g, 24);
+ lim = g->size - 1;
+ gp = CS g->s + g->ptr;
+ }
+ /* sprintf() saying "(nil)" for a null pointer seems unreliable.
+ Handle it explicitly. */
+ if ((ptr = va_arg(ap, void *)))
+ {
+ strncpy(newformat, item_start, fp - item_start);
+ newformat[fp - item_start] = 0;
+ g->ptr += sprintf(gp, newformat, ptr);
+ }
+ else
+ g->ptr += sprintf(gp, "(nil)");
+ }
+ 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 ((need = g->ptr + precision + 8) > lim)
+ {
+ if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
+ gstring_grow(g, precision+8);
+ lim = g->size - 1;
+ gp = CS g->s + g->ptr;
+ }
+ strncpy(newformat, item_start, fp - item_start);
+ newformat[fp-item_start] = 0;
+ if (length == L_LONGDOUBLE)
+ g->ptr += sprintf(gp, newformat, va_arg(ap, long double));
+ else
+ g->ptr += sprintf(gp, newformat, va_arg(ap, double));
+ break;
+
+ /* String types */
+
+ case '%':
+ if ((need = g->ptr + 1) > lim)
+ {
+ if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
+ gstring_grow(g, 1);
+ lim = g->size - 1;
+ }
+ g->s[g->ptr++] = (uschar) '%';
+ break;
+
+ case 'c':
+ if ((need = g->ptr + 1) > lim)
+ {
+ if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
+ gstring_grow(g, 1);
+ lim = g->size - 1;
+ }
+ g->s[g->ptr++] = (uschar) 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 = g->ptr; /* 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 = g->ptr; /* 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 */
+ case 'T': /* Forces *upper* case */
+ s = va_arg(ap, char *);
+
+ if (!s) s = null;
+ slen = Ustrlen(s);
+
+ if (!(flags & SVFMT_TAINT_NOCHK) && is_incompatible(g->s, s))
+ if (flags & SVFMT_REBUFFER)
+ {
+/* debug_printf("%s %d: untainted workarea, tainted %%s :- rebuffer\n", __FUNCTION__, __LINE__); */
+ gstring_rebuffer(g, s);
+ gp = CS g->s + g->ptr;
+ }
+#ifndef MACRO_PREDEF
+ else
+ die_tainted(US"string_vformat", func, line);
+#endif
+
+ INSERT_STRING: /* Come to from %D or %M above */
+
+ {
+ BOOL truncated = FALSE;
+
+ /* 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;
+
+ if ((need = g->ptr + width) >= size_limit || !(flags & SVFMT_EXTEND))
+ {
+ if (g->ptr == lim) return NULL;
+ if (need > lim)
+ {
+ truncated = TRUE;
+ width = precision = lim - g->ptr - 1;
+ if (width < 0) width = 0;
+ if (precision < 0) precision = 0;
+ }
+ }
+ else if (need > lim)
+ {
+ gstring_grow(g, width);
+ lim = g->size - 1;
+ gp = CS g->s + g->ptr;
+ }
+
+ g->ptr += sprintf(gp, "%*.*s", width, precision, s);
+ if (fp[-1] == 'S')
+ while (*gp) { *gp = tolower(*gp); gp++; }
+ else if (fp[-1] == 'T')
+ while (*gp) { *gp = toupper(*gp); gp++; }
+
+ if (truncated) return NULL;
+ 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;
+ }
+ }
+
+if (g->ptr > g->size)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+ "string_format internal error: caller %s %d", func, line);
+return g;
+}
+
+
+
+#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:
+ format a text format string - deliberately not uschar *
+ func caller, for debug
+ line caller, for debug
+ ... arguments for the format string
+
+Returns: a message, in dynamic store
+*/
+
+uschar *
+string_open_failed_trc(const uschar * func, unsigned line,
+ const char * format, ...)
+{
+va_list ap;
+gstring * g = string_get(1024);
+
+g = string_catn(g, US"failed to open ", 15);
+
+/* 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. */
+
+va_start(ap, format);
+(void) string_vformat_trc(g, func, line, STRING_SPRINTF_BUFFER_SIZE,
+ SVFMT_REBUFFER, format, ap);
+va_end(ap);
+
+g = string_catn(g, US": ", 2);
+g = string_cat(g, US strerror(errno));
+
+if (errno == EACCES)
+ {
+ int save_errno = errno;
+ g = string_fmt_append(g, " (euid=%ld egid=%ld)",
+ (long int)geteuid(), (long int)getegid());
+ errno = save_errno;
+ }
+gstring_release_unused(g);
+return string_from_gstring(g);
+}
+
+
+
+
+
+/* qsort(3), currently used to sort the environment variables
+for -bP environment output, needs a function to compare two pointers to string
+pointers. Here it is. */
+
+int
+string_compare_by_pointer(const void *a, const void *b)
+{
+return Ustrcmp(* CUSS a, * CUSS b);
+}
+#endif /* COMPILE_UTILITY */
+
+
+
+
+/*************************************************
+**************************************************
+* Stand-alone test program *
+**************************************************
+*************************************************/
+
+#ifdef STAND_ALONE
+int main(void)
+{
+uschar buffer[256];
+
+printf("Testing is_ip_address\n");
+store_init();
+
+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
index 0000000..b38aa6a
--- /dev/null
+++ b/src/structs.h
@@ -0,0 +1,960 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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;
+
+/* Growable-string */
+typedef struct gstring {
+ int size; /* Current capacity of string memory */
+ int ptr; /* Offset at which to append further chars */
+ uschar * s; /* The string memory */
+} gstring;
+
+/* Structure for remembering macros for the configuration file */
+
+typedef struct macro_item {
+ struct macro_item * next;
+ BOOL command_line;
+ unsigned namelen;
+ unsigned replen;
+ const uschar * name;
+ const uschar * replacement;
+} macro_item;
+
+/* Structure for bit tables for debugging and logging */
+
+typedef struct bit_table {
+ uschar *name;
+ 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;
+
+typedef enum { CHUNKING_NOT_OFFERED = -1,
+ CHUNKING_OFFERED,
+ CHUNKING_ACTIVE,
+ CHUNKING_LAST} chunking_state_t;
+
+typedef enum { TFO_NOT_USED = 0,
+ TFO_ATTEMPTED_NODATA,
+ TFO_ATTEMPTED_DATA,
+ TFO_USED_NODATA,
+ TFO_USED_DATA } tfo_state_t;
+
+/* 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;
+ const uschar *name; /* Host name */
+#ifndef DISABLE_TLS
+ const uschar *certname; /* Name used for certificate checks */
+#endif
+ const 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 */
+ const uschar *srcfile; /* and config source info for errors */
+ int srcline;
+} 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 */
+ const uschar *srcfile;
+ int srcline;
+
+ 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; /* ) */
+ /**************************************/
+ uschar *expand_multi_domain; /* ) */
+ 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 *max_parallel; /* Number of concurrent instances */
+ 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 */
+#ifndef DISABLE_EVENT
+ uschar *event_action; /* String to expand on notable events */
+#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;
+
+
+/* smtp transport datachunk callback */
+
+#define tc_reap_prev BIT(0) /* Flags: reap previous SMTP cmd responses */
+#define tc_chunk_last BIT(1) /* annotate chunk SMTP cmd as LAST */
+
+struct transport_context;
+typedef int (*tpt_chunk_cmd_cb)(struct transport_context *, unsigned, unsigned);
+
+/* Structure for information about a delivery-in-progress */
+
+typedef struct transport_context {
+ union { /* discriminated by option topt_output_string */
+ int fd; /* file descriptor to write message to */
+ gstring * msg; /* allocated string with written message */
+ } u;
+ transport_instance * tblock; /* transport */
+ struct address_item * addr;
+ uschar * check_string; /* string replacement */
+ uschar * escape_string;
+ int options; /* output processing topt_* */
+
+ /* items below only used with option topt_use_bdat */
+ tpt_chunk_cmd_cb chunk_cb; /* per-datachunk callback */
+ void * smtp_context;
+} transport_ctx;
+
+
+
+typedef struct {
+ uschar *request;
+ uschar *require;
+} dnssec_domains;
+
+/* 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 */
+ const uschar *srcfile;
+ int srcline;
+
+ 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 */
+ uschar *set; /* Variable = value to set; list */
+ 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 */
+ BOOL dsn_lasthop; /* If TRUE, this router is a DSN endpoint */
+
+ 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 */
+
+ dnssec_domains dnssec;
+} 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 */
+ const uschar *srcfile;
+ int srcline;
+
+ 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 *,
+ void *, /* smtp conn, with socket, output and input buffers */
+ int, /* command timeout */
+ uschar *, /* buffer for reading response */
+ int); /* sizeof buffer */
+ gstring * (*version_report)( /* diagnostic version reporting */
+ gstring *); /* string to append to */
+ void (*macros_create)(void); /* feature-macro creation */
+} 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];
+ uschar * log; /* portion of "listening on" log line */
+} 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 */
+ void *variables; /* router-vasriables */
+
+ BOOL ignore_error:1; /* ignore delivery error */
+#ifdef SUPPORT_I18N
+ BOOL utf8_msg:1; /* requires SMTPUTF8 processing */
+ BOOL utf8_downcvt:1; /* mandatory downconvert on delivery */
+ BOOL utf8_downcvt_maybe:1; /* optional downconvert on delivery */
+#endif
+} address_item_propagated;
+
+
+/* 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 *prefix_v; /* variable part of above */
+ uschar *suffix; /* stripped suffix of local part */
+ uschar *suffix_v; /* variable part of above */
+ const 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 */
+
+#ifndef DISABLE_TLS
+ const uschar *tlsver; /* version used for transport */
+ 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
+
+#ifdef EXPERIMENTAL_DSN_INFO
+ const uschar *smtp_greeting; /* peer self-identification */
+ const uschar *helo_response; /* peer message */
+#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 */
+
+ uschar *dsn_orcpt; /* DSN orcpt value */
+ int dsn_flags; /* DSN flags */
+ int dsn_aware; /* DSN aware flag */
+
+ uid_t uid; /* uid for transporting */
+ gid_t gid; /* gid for transporting */
+
+ /* flags */
+ struct {
+ BOOL af_allow_file:1; /* allow file in generated address */
+ BOOL af_allow_pipe:1; /* allow pipe in generated address */
+ BOOL af_allow_reply:1; /* allow autoreply in generated address */
+ BOOL af_dr_retry_exists:1; /* router retry record exists */
+ BOOL af_expand_pipe:1; /* expand pipe arguments */
+ BOOL af_file:1; /* file delivery; always with pfr */
+ BOOL af_gid_set:1; /* gid field is set */
+ BOOL af_home_expanded:1; /* home_dir is already expanded */
+ BOOL af_initgroups:1; /* use initgroups() for local transporting */
+ BOOL af_local_host_removed:1; /* local host was backup */
+ BOOL af_lt_retry_exists:1; /* local transport retry exists */
+ BOOL af_pfr:1; /* pipe or file or reply delivery */
+ BOOL af_retry_skipped:1; /* true if retry caused some skipping */
+ BOOL af_retry_timedout:1; /* true if retry timed out */
+ BOOL af_uid_set:1; /* uid field is set */
+ BOOL af_hide_child:1; /* hide child in bounce/defer msgs */
+ BOOL af_sverify_told:1; /* sender verify failure notified */
+ BOOL af_verify_pmfail:1; /* verify failure was postmaster callout */
+ BOOL af_verify_nsfail:1; /* verify failure was null sender callout */
+ BOOL af_homonym:1; /* an ancestor has same address */
+ BOOL af_verify_routed:1; /* for cached sender verify: routed OK */
+ BOOL af_verify_callout:1; /* for cached sender verify: callout was specified */
+ BOOL af_include_affixes:1; /* delivered with affixes in RCPT */
+ BOOL af_new_conn:1; /* delivered on an fresh TCP conn */
+ BOOL af_cont_conn:1; /* delivered (with new MAIL cmd) on an existing TCP conn */
+ BOOL af_cert_verified:1; /* delivered with verified TLS cert */
+ BOOL af_pass_message:1; /* pass message in bounces */
+ BOOL af_bad_reply:1; /* filter could not generate autoreply */
+ BOOL af_tcp_fastopen_conn:1; /* delivery connection used TCP Fast Open */
+ BOOL af_tcp_fastopen:1; /* delivery usefully used TCP Fast Open */
+ BOOL af_tcp_fastopen_data:1; /* delivery sent SMTP commands on TCP Fast Open */
+ BOOL af_pipelining:1; /* delivery used (traditional) pipelining */
+#ifndef DISABLE_PIPE_CONNECT
+ BOOL af_early_pipe:1; /* delivery used connect-time pipelining */
+#endif
+#ifndef DISABLE_PRDR
+ BOOL af_prdr_used:1; /* delivery used SMTP PRDR */
+#endif
+ BOOL af_chunking_used:1; /* delivery used SMTP CHUNKING */
+ BOOL af_force_command:1; /* force_command in pipe transport */
+#ifdef SUPPORT_DANE
+ BOOL af_dane_verified:1; /* TLS cert verify done with DANE */
+#endif
+#ifdef SUPPORT_I18N
+ BOOL af_utf8_downcvt:1; /* downconvert was done for delivery */
+#endif
+#ifndef DISABLE_TLS_RESUME
+ BOOL af_tls_resume:1; /* TLS used a resumed session */
+#endif
+ } flags;
+
+ 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 basic_errno; /* status after failure */
+ int more_errno; /* additional error information */
+ struct timeval delivery_time; /* time taken to do delivery/attempt */
+
+ unsigned short 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 prop; /* 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;
+ const 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 time-limited data such as DNS returns.
+We use this rather than extending tree_node to avoid wasting
+space for most tree use (variables...) at the cost of complexity
+for the lookups cache.
+We also store any options used for the lookup. */
+
+typedef struct expiring_data {
+ time_t expiry; /* if nonzero, data invalid after this time */
+ const uschar * opts; /* options, or NULL */
+ union
+ {
+ void * ptr; /* pointer to data */
+ int val; /* or integer data */
+ } data;
+} expiring_data;
+
+/* 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 */
+ unsigned short ttl; /* time-to-live, seconds */
+ int size; /* size of data */
+ const uschar *data; /* pointer to data */
+} dns_record;
+
+/* Structure for holding the result of a DNS query. A touch over
+64k big, so take care to release as soon as possible. */
+
+typedef struct {
+ int answerlen; /* length of the answer */
+ uschar answer[NS_MAXMSG]; /* 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 */
+ const 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;
+
+/* Information for making an smtp connection */
+typedef struct {
+ transport_instance * tblock;
+ void * ob; /* smtp_transport_options_block * */
+ host_item * host;
+ int host_af;
+ uschar * interface;
+
+ int sock; /* used for a bound but not connected socket */
+ uschar * sending_ip_address; /* used for TLS resumption */
+ const uschar * host_lbserver; /* ditto, for server-behind LB */
+ BOOL have_lbserver:1; /* host_lbserver is valid */
+
+#ifdef SUPPORT_DANE
+ BOOL dane:1; /* connection must do dane */
+ dns_answer tlsa_dnsa; /* strictly, this should use tainted mem */
+#endif
+} smtp_connect_args;
+
+/* A client-initiated connection. If TLS, the second element is non-NULL */
+typedef struct {
+ int sock;
+ void * tls_ctx;
+} client_conn_ctx;
+
+
+/* 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 {
+ client_conn_ctx * cctx; /* the connection */
+ 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 {
+ client_conn_ctx * cctx; /* the connection */
+ 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_connect_args * conn_args; /* to make connection, if not yet made */
+} 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 {
+ const uschar *host_name;
+ const uschar *host_address;
+ const 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 {
+ const uschar *string; /* the list string */
+ namedlist_cacheblock *cache_data; /* cached domain_data or localpart_data */
+ short number; /* the number of the list for caching */
+ BOOL hide; /* -bP does not display value */
+} 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;
+ int srcline;
+ const uschar * srcfile;
+} acl_block;
+
+/* smtp transport calc outbound_ip */
+typedef BOOL (*oicf) (uschar *message_id, void *data);
+
+/* DKIM information for transport */
+struct ob_dkim {
+ uschar *dkim_domain;
+ uschar *dkim_identity;
+ uschar *dkim_private_key;
+ uschar *dkim_selector;
+ uschar *dkim_canon;
+ uschar *dkim_sign_headers;
+ uschar *dkim_strict;
+ uschar *dkim_hash;
+ uschar *dkim_timestamps;
+ BOOL dot_stuffed;
+ BOOL force_bodyhash;
+#ifdef EXPERIMENTAL_ARC
+ uschar *arc_signspec;
+#endif
+};
+
+/* End of structs.h */
diff --git a/src/tls-cipher-stdname.c b/src/tls-cipher-stdname.c
new file mode 100644
index 0000000..ab973af
--- /dev/null
+++ b/src/tls-cipher-stdname.c
@@ -0,0 +1,393 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Jeremy Harris 2019 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Translate an IETF TLS ciphersuite code to an IETF ciphersuite name,
+for use when the TLS library do not provide such names.
+This file is #included by the tls-<library>.c file.
+
+Values for these tables pulled on 2019/02/03 from
+https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml */
+
+
+
+static const uschar * ctb_00[] = {
+[0x00] = US "TLS_NULL_WITH_NULL_NULL",
+[0x01] = US "TLS_RSA_WITH_NULL_MD5",
+[0x02] = US "TLS_RSA_WITH_NULL_SHA",
+[0x03] = US "TLS_RSA_EXPORT_WITH_RC4_40_MD5",
+[0x04] = US "TLS_RSA_WITH_RC4_128_MD5",
+[0x05] = US "TLS_RSA_WITH_RC4_128_SHA",
+[0x06] = US "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5",
+[0x07] = US "TLS_RSA_WITH_IDEA_CBC_SHA",
+[0x08] = US "TLS_RSA_EXPORT_WITH_DES40_CBC_SHA",
+[0x09] = US "TLS_RSA_WITH_DES_CBC_SHA",
+[0x0A] = US "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
+[0x0B] = US "TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA",
+[0x0C] = US "TLS_DH_DSS_WITH_DES_CBC_SHA",
+[0x0D] = US "TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA",
+[0x0E] = US "TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA",
+[0x0F] = US "TLS_DH_RSA_WITH_DES_CBC_SHA",
+[0x10] = US "TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA",
+[0x11] = US "TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA",
+[0x12] = US "TLS_DHE_DSS_WITH_DES_CBC_SHA",
+[0x13] = US "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA",
+[0x14] = US "TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
+[0x15] = US "TLS_DHE_RSA_WITH_DES_CBC_SHA",
+[0x16] = US "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA",
+[0x17] = US "TLS_DH_anon_EXPORT_WITH_RC4_40_MD5",
+[0x18] = US "TLS_DH_anon_WITH_RC4_128_MD5",
+[0x19] = US "TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA",
+[0x1A] = US "TLS_DH_anon_WITH_DES_CBC_SHA",
+[0x1B] = US "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA",
+
+[0x1E] = US "TLS_KRB5_WITH_DES_CBC_SHA",
+[0x1F] = US "TLS_KRB5_WITH_3DES_EDE_CBC_SHA",
+[0x20] = US "TLS_KRB5_WITH_RC4_128_SHA",
+[0x21] = US "TLS_KRB5_WITH_IDEA_CBC_SHA",
+[0x22] = US "TLS_KRB5_WITH_DES_CBC_MD5",
+[0x23] = US "TLS_KRB5_WITH_3DES_EDE_CBC_MD5",
+[0x24] = US "TLS_KRB5_WITH_RC4_128_MD5",
+[0x25] = US "TLS_KRB5_WITH_IDEA_CBC_MD5",
+[0x26] = US "TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA",
+[0x27] = US "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA",
+[0x28] = US "TLS_KRB5_EXPORT_WITH_RC4_40_SHA",
+[0x29] = US "TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5",
+[0x2A] = US "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5",
+[0x2B] = US "TLS_KRB5_EXPORT_WITH_RC4_40_MD5",
+[0x2C] = US "TLS_PSK_WITH_NULL_SHA",
+[0x2D] = US "TLS_DHE_PSK_WITH_NULL_SHA",
+[0x2E] = US "TLS_RSA_PSK_WITH_NULL_SHA",
+[0x2F] = US "TLS_RSA_WITH_AES_128_CBC_SHA",
+[0x30] = US "TLS_DH_DSS_WITH_AES_128_CBC_SHA",
+[0x31] = US "TLS_DH_RSA_WITH_AES_128_CBC_SHA",
+[0x32] = US "TLS_DHE_DSS_WITH_AES_128_CBC_SHA",
+[0x33] = US "TLS_DHE_RSA_WITH_AES_128_CBC_SHA",
+[0x34] = US "TLS_DH_anon_WITH_AES_128_CBC_SHA",
+[0x35] = US "TLS_RSA_WITH_AES_256_CBC_SHA",
+[0x36] = US "TLS_DH_DSS_WITH_AES_256_CBC_SHA",
+[0x37] = US "TLS_DH_RSA_WITH_AES_256_CBC_SHA",
+[0x38] = US "TLS_DHE_DSS_WITH_AES_256_CBC_SHA",
+[0x39] = US "TLS_DHE_RSA_WITH_AES_256_CBC_SHA",
+[0x3A] = US "TLS_DH_anon_WITH_AES_256_CBC_SHA",
+[0x3B] = US "TLS_RSA_WITH_NULL_SHA256",
+[0x3C] = US "TLS_RSA_WITH_AES_128_CBC_SHA256",
+[0x3D] = US "TLS_RSA_WITH_AES_256_CBC_SHA256",
+[0x3E] = US "TLS_DH_DSS_WITH_AES_128_CBC_SHA256",
+[0x3F] = US "TLS_DH_RSA_WITH_AES_128_CBC_SHA256",
+[0x40] = US "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256",
+[0x41] = US "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA",
+[0x42] = US "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA",
+[0x43] = US "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA",
+[0x44] = US "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA",
+[0x45] = US "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA",
+[0x46] = US "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA",
+
+[0x67] = US "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",
+[0x68] = US "TLS_DH_DSS_WITH_AES_256_CBC_SHA256",
+[0x69] = US "TLS_DH_RSA_WITH_AES_256_CBC_SHA256",
+[0x6A] = US "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256",
+[0x6B] = US "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
+[0x6C] = US "TLS_DH_anon_WITH_AES_128_CBC_SHA256",
+[0x6D] = US "TLS_DH_anon_WITH_AES_256_CBC_SHA256",
+
+[0x84] = US "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA",
+[0x85] = US "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA",
+[0x86] = US "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA",
+[0x87] = US "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA",
+[0x88] = US "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA",
+[0x89] = US "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA",
+[0x8A] = US "TLS_PSK_WITH_RC4_128_SHA",
+[0x8B] = US "TLS_PSK_WITH_3DES_EDE_CBC_SHA",
+[0x8C] = US "TLS_PSK_WITH_AES_128_CBC_SHA",
+[0x8D] = US "TLS_PSK_WITH_AES_256_CBC_SHA",
+[0x8E] = US "TLS_DHE_PSK_WITH_RC4_128_SHA",
+[0x8F] = US "TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA",
+[0x90] = US "TLS_DHE_PSK_WITH_AES_128_CBC_SHA",
+[0x91] = US "TLS_DHE_PSK_WITH_AES_256_CBC_SHA",
+[0x92] = US "TLS_RSA_PSK_WITH_RC4_128_SHA",
+[0x93] = US "TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA",
+[0x94] = US "TLS_RSA_PSK_WITH_AES_128_CBC_SHA",
+[0x95] = US "TLS_RSA_PSK_WITH_AES_256_CBC_SHA",
+[0x96] = US "TLS_RSA_WITH_SEED_CBC_SHA",
+[0x97] = US "TLS_DH_DSS_WITH_SEED_CBC_SHA",
+[0x98] = US "TLS_DH_RSA_WITH_SEED_CBC_SHA",
+[0x99] = US "TLS_DHE_DSS_WITH_SEED_CBC_SHA",
+[0x9A] = US "TLS_DHE_RSA_WITH_SEED_CBC_SHA",
+[0x9B] = US "TLS_DH_anon_WITH_SEED_CBC_SHA",
+[0x9C] = US "TLS_RSA_WITH_AES_128_GCM_SHA256",
+[0x9D] = US "TLS_RSA_WITH_AES_256_GCM_SHA384",
+[0x9E] = US "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
+[0x9F] = US "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
+[0xA0] = US "TLS_DH_RSA_WITH_AES_128_GCM_SHA256",
+[0xA1] = US "TLS_DH_RSA_WITH_AES_256_GCM_SHA384",
+[0xA2] = US "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256",
+[0xA3] = US "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384",
+[0xA4] = US "TLS_DH_DSS_WITH_AES_128_GCM_SHA256",
+[0xA5] = US "TLS_DH_DSS_WITH_AES_256_GCM_SHA384",
+[0xA6] = US "TLS_DH_anon_WITH_AES_128_GCM_SHA256",
+[0xA7] = US "TLS_DH_anon_WITH_AES_256_GCM_SHA384",
+[0xA8] = US "TLS_PSK_WITH_AES_128_GCM_SHA256",
+[0xA9] = US "TLS_PSK_WITH_AES_256_GCM_SHA384",
+[0xAA] = US "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256",
+[0xAB] = US "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384",
+[0xAC] = US "TLS_RSA_PSK_WITH_AES_128_GCM_SHA256",
+[0xAD] = US "TLS_RSA_PSK_WITH_AES_256_GCM_SHA384",
+[0xAE] = US "TLS_PSK_WITH_AES_128_CBC_SHA256",
+[0xAF] = US "TLS_PSK_WITH_AES_256_CBC_SHA384",
+[0xB0] = US "TLS_PSK_WITH_NULL_SHA256",
+[0xB1] = US "TLS_PSK_WITH_NULL_SHA384",
+[0xB2] = US "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256",
+[0xB3] = US "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384",
+[0xB4] = US "TLS_DHE_PSK_WITH_NULL_SHA256",
+[0xB5] = US "TLS_DHE_PSK_WITH_NULL_SHA384",
+[0xB6] = US "TLS_RSA_PSK_WITH_AES_128_CBC_SHA256",
+[0xB7] = US "TLS_RSA_PSK_WITH_AES_256_CBC_SHA384",
+[0xB8] = US "TLS_RSA_PSK_WITH_NULL_SHA256",
+[0xB9] = US "TLS_RSA_PSK_WITH_NULL_SHA384",
+[0xBA] = US "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256",
+[0xBB] = US "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256",
+[0xBC] = US "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256",
+[0xBD] = US "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256",
+[0xBE] = US "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256",
+[0xBF] = US "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256",
+[0xC0] = US "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256",
+[0xC1] = US "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256",
+[0xC2] = US "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256",
+[0xC3] = US "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256",
+[0xC4] = US "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256",
+[0xC5] = US "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256",
+};
+static const uschar * ctb_13[] = {
+[0x01] = US "TLS_AES_128_GCM_SHA256",
+[0x02] = US "TLS_AES_256_GCM_SHA384",
+[0x03] = US "TLS_CHACHA20_POLY1305_SHA256",
+[0x04] = US "TLS_AES_128_CCM_SHA256",
+[0x05] = US "TLS_AES_128_CCM_8_SHA256",
+};
+static const uschar * ctb_c0[] = {
+[0x01] = US "TLS_ECDH_ECDSA_WITH_NULL_SHA",
+[0x02] = US "TLS_ECDH_ECDSA_WITH_RC4_128_SHA",
+[0x03] = US "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA",
+[0x04] = US "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA",
+[0x05] = US "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA",
+[0x06] = US "TLS_ECDHE_ECDSA_WITH_NULL_SHA",
+[0x07] = US "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA",
+[0x08] = US "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA",
+[0x09] = US "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
+[0x0A] = US "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
+[0x0B] = US "TLS_ECDH_RSA_WITH_NULL_SHA",
+[0x0C] = US "TLS_ECDH_RSA_WITH_RC4_128_SHA",
+[0x0D] = US "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA",
+[0x0E] = US "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA",
+[0x0F] = US "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA",
+[0x10] = US "TLS_ECDHE_RSA_WITH_NULL_SHA",
+[0x11] = US "TLS_ECDHE_RSA_WITH_RC4_128_SHA",
+[0x12] = US "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
+[0x13] = US "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
+[0x14] = US "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
+[0x15] = US "TLS_ECDH_anon_WITH_NULL_SHA",
+[0x16] = US "TLS_ECDH_anon_WITH_RC4_128_SHA",
+[0x17] = US "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA",
+[0x18] = US "TLS_ECDH_anon_WITH_AES_128_CBC_SHA",
+[0x19] = US "TLS_ECDH_anon_WITH_AES_256_CBC_SHA",
+[0x1A] = US "TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA",
+[0x1B] = US "TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA",
+[0x1C] = US "TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA",
+[0x1D] = US "TLS_SRP_SHA_WITH_AES_128_CBC_SHA",
+[0x1E] = US "TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA",
+[0x1F] = US "TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA",
+[0x20] = US "TLS_SRP_SHA_WITH_AES_256_CBC_SHA",
+[0x21] = US "TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA",
+[0x22] = US "TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA",
+[0x23] = US "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
+[0x24] = US "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",
+[0x25] = US "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256",
+[0x26] = US "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384",
+[0x27] = US "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
+[0x28] = US "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",
+[0x29] = US "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256",
+[0x2A] = US "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384",
+[0x2B] = US "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
+[0x2C] = US "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
+[0x2D] = US "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256",
+[0x2E] = US "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384",
+[0x2F] = US "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
+[0x30] = US "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
+[0x31] = US "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256",
+[0x32] = US "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384",
+[0x33] = US "TLS_ECDHE_PSK_WITH_RC4_128_SHA",
+[0x34] = US "TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA",
+[0x35] = US "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA",
+[0x36] = US "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA",
+[0x37] = US "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256",
+[0x38] = US "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384",
+[0x39] = US "TLS_ECDHE_PSK_WITH_NULL_SHA",
+[0x3A] = US "TLS_ECDHE_PSK_WITH_NULL_SHA256",
+[0x3B] = US "TLS_ECDHE_PSK_WITH_NULL_SHA384",
+[0x3C] = US "TLS_RSA_WITH_ARIA_128_CBC_SHA256",
+[0x3D] = US "TLS_RSA_WITH_ARIA_256_CBC_SHA384",
+[0x3E] = US "TLS_DH_DSS_WITH_ARIA_128_CBC_SHA256",
+[0x3F] = US "TLS_DH_DSS_WITH_ARIA_256_CBC_SHA384",
+[0x40] = US "TLS_DH_RSA_WITH_ARIA_128_CBC_SHA256",
+[0x41] = US "TLS_DH_RSA_WITH_ARIA_256_CBC_SHA384",
+[0x42] = US "TLS_DHE_DSS_WITH_ARIA_128_CBC_SHA256",
+[0x43] = US "TLS_DHE_DSS_WITH_ARIA_256_CBC_SHA384",
+[0x44] = US "TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256",
+[0x45] = US "TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384",
+[0x46] = US "TLS_DH_anon_WITH_ARIA_128_CBC_SHA256",
+[0x47] = US "TLS_DH_anon_WITH_ARIA_256_CBC_SHA384",
+[0x48] = US "TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256",
+[0x49] = US "TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384",
+[0x4A] = US "TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256",
+[0x4B] = US "TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384",
+[0x4C] = US "TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256",
+[0x4D] = US "TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384",
+[0x4E] = US "TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256",
+[0x4F] = US "TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384",
+[0x50] = US "TLS_RSA_WITH_ARIA_128_GCM_SHA256",
+[0x51] = US "TLS_RSA_WITH_ARIA_256_GCM_SHA384",
+[0x52] = US "TLS_DHE_RSA_WITH_ARIA_128_GCM_SHA256",
+[0x53] = US "TLS_DHE_RSA_WITH_ARIA_256_GCM_SHA384",
+[0x54] = US "TLS_DH_RSA_WITH_ARIA_128_GCM_SHA256",
+[0x55] = US "TLS_DH_RSA_WITH_ARIA_256_GCM_SHA384",
+[0x56] = US "TLS_DHE_DSS_WITH_ARIA_128_GCM_SHA256",
+[0x57] = US "TLS_DHE_DSS_WITH_ARIA_256_GCM_SHA384",
+[0x58] = US "TLS_DH_DSS_WITH_ARIA_128_GCM_SHA256",
+[0x59] = US "TLS_DH_DSS_WITH_ARIA_256_GCM_SHA384",
+[0x5A] = US "TLS_DH_anon_WITH_ARIA_128_GCM_SHA256",
+[0x5B] = US "TLS_DH_anon_WITH_ARIA_256_GCM_SHA384",
+[0x5C] = US "TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256",
+[0x5D] = US "TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384",
+[0x5E] = US "TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256",
+[0x5F] = US "TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384",
+[0x60] = US "TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256",
+[0x61] = US "TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384",
+[0x62] = US "TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256",
+[0x63] = US "TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384",
+[0x64] = US "TLS_PSK_WITH_ARIA_128_CBC_SHA256",
+[0x65] = US "TLS_PSK_WITH_ARIA_256_CBC_SHA384",
+[0x66] = US "TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256",
+[0x67] = US "TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384",
+[0x68] = US "TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256",
+[0x69] = US "TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384",
+[0x6A] = US "TLS_PSK_WITH_ARIA_128_GCM_SHA256",
+[0x6B] = US "TLS_PSK_WITH_ARIA_256_GCM_SHA384",
+[0x6C] = US "TLS_DHE_PSK_WITH_ARIA_128_GCM_SHA256",
+[0x6D] = US "TLS_DHE_PSK_WITH_ARIA_256_GCM_SHA384",
+[0x6E] = US "TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256",
+[0x6F] = US "TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384",
+[0x70] = US "TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256",
+[0x71] = US "TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384",
+[0x72] = US "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256",
+[0x73] = US "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384",
+[0x74] = US "TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256",
+[0x75] = US "TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384",
+[0x76] = US "TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256",
+[0x77] = US "TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384",
+[0x78] = US "TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256",
+[0x79] = US "TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384",
+[0x7A] = US "TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256",
+[0x7B] = US "TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384",
+[0x7C] = US "TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256",
+[0x7D] = US "TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384",
+[0x7E] = US "TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256",
+[0x7F] = US "TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384",
+[0x80] = US "TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256",
+[0x81] = US "TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384",
+[0x82] = US "TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256",
+[0x83] = US "TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384",
+[0x84] = US "TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256",
+[0x85] = US "TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384",
+[0x86] = US "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256",
+[0x87] = US "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384",
+[0x88] = US "TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256",
+[0x89] = US "TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384",
+[0x8A] = US "TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256",
+[0x8B] = US "TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384",
+[0x8C] = US "TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256",
+[0x8D] = US "TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384",
+[0x8E] = US "TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256",
+[0x8F] = US "TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384",
+[0x90] = US "TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256",
+[0x91] = US "TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384",
+[0x92] = US "TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256",
+[0x93] = US "TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384",
+[0x94] = US "TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256",
+[0x95] = US "TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384",
+[0x96] = US "TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256",
+[0x97] = US "TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384",
+[0x98] = US "TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256",
+[0x99] = US "TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384",
+[0x9A] = US "TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256",
+[0x9B] = US "TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384",
+[0x9C] = US "TLS_RSA_WITH_AES_128_CCM",
+[0x9D] = US "TLS_RSA_WITH_AES_256_CCM",
+[0x9E] = US "TLS_DHE_RSA_WITH_AES_128_CCM",
+[0x9F] = US "TLS_DHE_RSA_WITH_AES_256_CCM",
+[0xA0] = US "TLS_RSA_WITH_AES_128_CCM_8",
+[0xA1] = US "TLS_RSA_WITH_AES_256_CCM_8",
+[0xA2] = US "TLS_DHE_RSA_WITH_AES_128_CCM_8",
+[0xA3] = US "TLS_DHE_RSA_WITH_AES_256_CCM_8",
+[0xA4] = US "TLS_PSK_WITH_AES_128_CCM",
+[0xA5] = US "TLS_PSK_WITH_AES_256_CCM",
+[0xA6] = US "TLS_DHE_PSK_WITH_AES_128_CCM",
+[0xA7] = US "TLS_DHE_PSK_WITH_AES_256_CCM",
+[0xA8] = US "TLS_PSK_WITH_AES_128_CCM_8",
+[0xA9] = US "TLS_PSK_WITH_AES_256_CCM_8",
+[0xAA] = US "TLS_PSK_DHE_WITH_AES_128_CCM_8",
+[0xAB] = US "TLS_PSK_DHE_WITH_AES_256_CCM_8",
+[0xAC] = US "TLS_ECDHE_ECDSA_WITH_AES_128_CCM",
+[0xAD] = US "TLS_ECDHE_ECDSA_WITH_AES_256_CCM",
+[0xAE] = US "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8",
+[0xAF] = US "TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8",
+[0xB0] = US "TLS_ECCPWD_WITH_AES_128_GCM_SHA256",
+[0xB1] = US "TLS_ECCPWD_WITH_AES_256_GCM_SHA384",
+[0xB2] = US "TLS_ECCPWD_WITH_AES_128_CCM_SHA256",
+[0xB3] = US "TLS_ECCPWD_WITH_AES_256_CCM_SHA384",
+[0xB4] = US "TLS_SHA256_SHA256",
+[0xB5] = US "TLS_SHA384_SHA384",
+};
+static const uschar * ctb_cc[] = {
+[0xA8] = US "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
+[0xA9] = US "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
+[0xAA] = US "TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
+[0xAB] = US "TLS_PSK_WITH_CHACHA20_POLY1305_SHA256",
+[0xAC] = US "TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256",
+[0xAD] = US "TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256",
+[0xAE] = US "TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256",
+};
+static const uschar * ctb_d0[] = {
+[0x01] = US "TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256",
+[0x02] = US "TLS_ECDHE_PSK_WITH_AES_256_GCM_SHA384",
+[0x03] = US "TLS_ECDHE_PSK_WITH_AES_128_CCM_8_SHA256",
+
+[0x05] = US "TLS_ECDHE_PSK_WITH_AES_128_CCM_SHA256",
+};
+
+static const uschar *
+cipher_stdname_tb(uschar idx, const uschar ** tb, int lim)
+{
+return idx >= lim ? NULL : tb[idx];
+}
+
+static const uschar *
+cipher_stdname(uschar id0, uschar id1)
+{
+switch (id0)
+ {
+ case 0x00: return cipher_stdname_tb(id1, ctb_00, nelem(ctb_00));
+ case 0x13: return cipher_stdname_tb(id1, ctb_13, nelem(ctb_00));
+ case 0xc0: return cipher_stdname_tb(id1, ctb_c0, nelem(ctb_c0));
+ case 0xcc: return cipher_stdname_tb(id1, ctb_cc, nelem(ctb_cc));
+ case 0xd0: return cipher_stdname_tb(id1, ctb_d0, nelem(ctb_d0));
+ default: return NULL;
+ }
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of tls-cipher-stdname.c */
diff --git a/src/tls-gnu.c b/src/tls-gnu.c
new file mode 100644
index 0000000..fcb8f7a
--- /dev/null
+++ b/src/tls-gnu.c
@@ -0,0 +1,4280 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) Phil Pennock 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* 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
+Mavrogiannopoulos. 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>
+# define SUPPORT_PARAM_TO_PK_BITS
+#endif
+#if GNUTLS_VERSION_NUMBER < 0x030103 && !defined(DISABLE_OCSP)
+# warning "GnuTLS library version too old; define DISABLE_OCSP in Makefile"
+# define DISABLE_OCSP
+#endif
+#if GNUTLS_VERSION_NUMBER < 0x020a00 && !defined(DISABLE_EVENT)
+# warning "GnuTLS library version too old; tls:cert event unsupported"
+# define DISABLE_EVENT
+#endif
+#if GNUTLS_VERSION_NUMBER >= 0x030000
+# define SUPPORT_SELFSIGN /* Uncertain what version is first usable but 2.12.23 is not */
+#endif
+#if GNUTLS_VERSION_NUMBER >= 0x030306
+# define SUPPORT_CA_DIR
+#else
+# undef SUPPORT_CA_DIR
+#endif
+#if GNUTLS_VERSION_NUMBER >= 0x030014
+# define SUPPORT_SYSDEFAULT_CABUNDLE
+#endif
+#if GNUTLS_VERSION_NUMBER >= 0x030104
+# define GNUTLS_CERT_VFY_STATUS_PRINT
+#endif
+#if GNUTLS_VERSION_NUMBER >= 0x030109
+# define SUPPORT_CORK
+#endif
+#if GNUTLS_VERSION_NUMBER >= 0x03010a
+# define SUPPORT_GNUTLS_SESS_DESC
+#endif
+#if GNUTLS_VERSION_NUMBER >= 0x030300
+# define GNUTLS_AUTO_GLOBAL_INIT
+# define GNUTLS_AUTO_PKCS11_MANUAL
+#endif
+#if (GNUTLS_VERSION_NUMBER >= 0x030404) \
+ || (GNUTLS_VERSION_NUMBER >= 0x030311) && (GNUTLS_VERSION_NUMBER & 0xffff00 == 0x030300)
+# ifndef DISABLE_OCSP
+# define EXIM_HAVE_OCSP
+# endif
+#endif
+#if GNUTLS_VERSION_NUMBER >= 0x030500
+# define SUPPORT_GNUTLS_KEYLOG
+#endif
+#if GNUTLS_VERSION_NUMBER >= 0x030506 && !defined(DISABLE_OCSP)
+# define SUPPORT_SRV_OCSP_STACK
+#endif
+#if GNUTLS_VERSION_NUMBER >= 0x030603
+# define EXIM_HAVE_TLS1_3
+# define SUPPORT_GNUTLS_EXT_RAW_PARSE
+# define GNUTLS_OCSP_STATUS_REQUEST_GET2
+#endif
+
+#ifdef SUPPORT_DANE
+# if GNUTLS_VERSION_NUMBER >= 0x030000
+# define DANESSL_USAGE_DANE_TA 2
+# define DANESSL_USAGE_DANE_EE 3
+# else
+# error GnuTLS version too early for DANE
+# endif
+# if GNUTLS_VERSION_NUMBER < 0x999999
+# define GNUTLS_BROKEN_DANE_VALIDATION
+# endif
+#endif
+
+#ifndef DISABLE_TLS_RESUME
+# if GNUTLS_VERSION_NUMBER >= 0x030603
+# define EXIM_HAVE_TLS_RESUME
+# else
+# warning "GnuTLS library version too old; resumption unsupported"
+# endif
+#endif
+
+#if GNUTLS_VERSION_NUMBER >= 0x030200
+# ifdef SUPPORT_GNUTLS_EXT_RAW_PARSE
+# define EXIM_HAVE_ALPN
+# endif
+#endif
+
+#ifndef DISABLE_OCSP
+# include <gnutls/ocsp.h>
+#endif
+#ifdef SUPPORT_DANE
+# include <gnutls/dane.h>
+#endif
+
+#include "tls-cipher-stdname.c"
+
+
+#ifdef MACRO_PREDEF
+void
+options_tls(void)
+{
+# ifndef DISABLE_TLS_RESUME
+builtin_macro_create_var(US"_RESUME_DECODE", RESUME_DECODE_STRING );
+# endif
+# ifdef EXIM_HAVE_TLS1_3
+builtin_macro_create(US"_HAVE_TLS1_3");
+# endif
+# ifdef EXIM_HAVE_OCSP
+builtin_macro_create(US"_HAVE_TLS_OCSP");
+# endif
+# ifdef SUPPORT_SRV_OCSP_STACK
+builtin_macro_create(US"_HAVE_TLS_OCSP_LIST");
+# endif
+#if defined(EXIM_HAVE_INOTIFY) || defined(EXIM_HAVE_KEVENT)
+builtin_macro_create(US"_HAVE_TLS_CA_CACHE");
+# endif
+# ifdef EXIM_HAVE_ALPN
+builtin_macro_create(US"_HAVE_TLS_ALPN");
+# endif
+}
+#else
+
+
+/* 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, VERIFY_DANE };
+
+/* 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 tlsp->tls_channelbinding.
+*/
+
+typedef struct exim_gnutls_state {
+ gnutls_session_t session;
+
+ exim_tlslib_state lib_state;
+#define x509_cred libdata0
+#define pri_cache libdata1
+
+ enum peer_verify_requirement verify_requirement;
+ int fd_in;
+ int fd_out;
+
+ BOOL peer_cert_verified:1;
+ BOOL peer_dane_verified:1;
+ BOOL trigger_sni_changes:1;
+ BOOL have_set_peerdn:1;
+ BOOL xfer_eof:1; /*XXX never gets set! */
+ BOOL xfer_error:1;
+#ifdef SUPPORT_CORK
+ BOOL corked:1;
+#endif
+
+ const struct host_item *host; /* NULL if server */
+ 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;
+ const uschar *exp_tls_verify_cert_hostnames;
+#ifndef DISABLE_EVENT
+ uschar *event_action;
+#endif
+#ifdef SUPPORT_DANE
+ char * const * dane_data;
+ const int * dane_data_len;
+#endif
+
+ tls_support *tlsp; /* set in tls_init() */
+
+ uschar *xfer_buffer;
+ int xfer_buffer_lwm;
+ int xfer_buffer_hwm;
+} exim_gnutls_state_st;
+
+static const exim_gnutls_state_st exim_gnutls_state_init = {
+ /* all elements not explicitly intialised here get 0/NULL/FALSE */
+ .fd_in = -1,
+ .fd_out = -1,
+};
+
+/* 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.
+XXX But see gnutls_session_get_ptr()
+*/
+
+static exim_gnutls_state_st state_server = {
+ /* all elements not explicitly intialised here get 0/NULL/FALSE */
+ .fd_in = -1,
+ .fd_out = -1,
+};
+
+/* 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;
+
+static int ssl_session_timeout = 7200; /* Two hours */
+
+static const uschar * const exim_default_gnutls_priority = US"NORMAL";
+
+/* Guard library core initialisation */
+
+static BOOL exim_gnutls_base_init_done = FALSE;
+
+#ifndef DISABLE_OCSP
+static BOOL gnutls_buggy_ocsp = FALSE;
+static BOOL exim_testharness_disable_ocsp_validity_check = FALSE;
+#endif
+
+#ifdef EXIM_HAVE_ALPN
+static int server_seen_alpn = -1; /* count of names */
+#endif
+#ifdef EXIM_HAVE_TLS_RESUME
+static gnutls_datum_t server_sessticket_key;
+#endif
+
+
+/* ------------------------------------------------------------------------ */
+/* 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. GNuTLS also looks for an environment variable - except not for
+setuid binaries, making it useless - "GNUTLS_DEBUG_LEVEL".
+Allegedly the testscript line "GNUTLS_DEBUG_LEVEL=9 sudo exim ..." would work,
+but the env var must be added to /etc/sudoers too. */
+#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 Expand_check_tlsvar(Varname, errstr) \
+ expand_check(state->Varname, US #Varname, &state->exp_##Varname, errstr)
+
+#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
+
+#if GNUTLS_VERSION_NUMBER >= 0x030404
+# define HAVE_GNUTLS_PRF_RFC5705
+#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);
+
+#ifdef EXIM_HAVE_TLS_RESUME
+static int
+tls_server_ticket_cb(gnutls_session_t sess, u_int htype, unsigned when,
+ unsigned incoming, const gnutls_datum_t * msg);
+#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
+ 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
+ errstr pointer to returned error string
+
+Returns: OK/DEFER/FAIL
+*/
+
+static int
+tls_error(const uschar *prefix, const uschar *msg, const host_item *host,
+ uschar ** errstr)
+{
+if (errstr)
+ *errstr = string_sprintf("(%s)%s%s", prefix, msg ? ": " : "", msg ? msg : US"");
+return host ? FAIL : DEFER;
+}
+
+
+static int
+tls_error_gnu(exim_gnutls_state_st * state, const uschar *prefix, int err,
+ uschar ** errstr)
+{
+return tls_error(prefix,
+ state && err == GNUTLS_E_FATAL_ALERT_RECEIVED
+ ? US gnutls_alert_get_name(gnutls_alert_get(state->session))
+ : US gnutls_strerror(err),
+ state ? state->host : NULL,
+ errstr);
+}
+
+static int
+tls_error_sys(const uschar *prefix, int err, const host_item *host,
+ uschar ** errstr)
+{
+return tls_error(prefix, US strerror(err), host, errstr);
+}
+
+
+/* ------------------------------------------------------------------------ */
+/* Initialisation */
+
+#ifndef DISABLE_OCSP
+
+static BOOL
+tls_is_buggy_ocsp(void)
+{
+const uschar * s;
+uschar maj, mid, mic;
+
+s = CUS gnutls_check_version(NULL);
+maj = atoi(CCS s);
+if (maj == 3)
+ {
+ while (*s && *s != '.') s++;
+ mid = atoi(CCS ++s);
+ if (mid <= 2)
+ return TRUE;
+ else if (mid >= 5)
+ return FALSE;
+ else
+ {
+ while (*s && *s != '.') s++;
+ mic = atoi(CCS ++s);
+ return mic <= (mid == 3 ? 16 : 3);
+ }
+ }
+return FALSE;
+}
+
+#endif
+
+
+static int
+tls_g_init(uschar ** errstr)
+{
+int rc;
+DEBUG(D_tls) debug_printf("GnuTLS global init required\n");
+
+#if defined(HAVE_GNUTLS_PKCS11) && !defined(GNUTLS_AUTO_PKCS11_MANUAL)
+/* 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)
+ if ((rc = gnutls_pkcs11_init(GNUTLS_PKCS11_FLAG_MANUAL, NULL)))
+ return tls_error_gnu(NULL, US"gnutls_pkcs11_init", rc, errstr);
+#endif
+
+#ifndef GNUTLS_AUTO_GLOBAL_INIT
+if ((rc = gnutls_global_init()))
+ return tls_error_gnu(NULL, US"gnutls_global_init", rc, errstr);
+#endif
+
+#if EXIM_GNUTLS_LIBRARY_LOG_LEVEL >= 0
+DEBUG(D_tls)
+ {
+ gnutls_global_set_log_function(exim_gnutls_logger_cb);
+ /* arbitrarily chosen level; bump up to 9 for more */
+ gnutls_global_set_log_level(EXIM_GNUTLS_LIBRARY_LOG_LEVEL);
+ }
+#endif
+
+#ifndef DISABLE_OCSP
+if (tls_ocsp_file && (gnutls_buggy_ocsp = tls_is_buggy_ocsp()))
+ log_write(0, LOG_MAIN, "OCSP unusable with this GnuTLS library version");
+#endif
+
+exim_gnutls_base_init_done = TRUE;
+return OK;
+}
+
+
+
+/* Daemon-call before each connection. Nothing to do for GnuTLS. */
+
+static void
+tls_per_lib_daemon_tick(void)
+{
+}
+
+/* Daemon one-time initialisation */
+
+static void
+tls_per_lib_daemon_init(void)
+{
+uschar * dummy_errstr;
+static BOOL once = FALSE;
+
+if (!exim_gnutls_base_init_done)
+ tls_g_init(&dummy_errstr);
+
+if (!once)
+ {
+ once = TRUE;
+
+#ifdef EXIM_HAVE_TLS_RESUME
+ /* We are dependent on the GnuTLS implementation of the Session Ticket
+ encryption; both the strength and the key rotation period. We hope that
+ the strength at least matches that of the ciphersuite (but GnuTLS does not
+ document this). */
+
+ gnutls_session_ticket_key_generate(&server_sessticket_key); /* >= 2.10.0 */
+ if (f.running_in_test_harness) ssl_session_timeout = 6;
+#endif
+
+ tls_daemon_creds_reload();
+ }
+}
+
+/* ------------------------------------------------------------------------ */
+
+/*************************************************
+* 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 rc is 0
+
+Returns: nothing
+*/
+
+static void
+record_io_error(exim_gnutls_state_st *state, int rc, uschar *when, uschar *text)
+{
+const uschar * msg;
+uschar * errstr;
+
+msg = rc == GNUTLS_E_FATAL_ALERT_RECEIVED
+ ? string_sprintf("A TLS fatal alert has been received: %s",
+ US gnutls_alert_get_name(gnutls_alert_get(state->session)))
+#ifdef GNUTLS_E_PREMATURE_TERMINATION
+ : rc == GNUTLS_E_PREMATURE_TERMINATION && errno
+ ? string_sprintf("%s: syscall: %s", US gnutls_strerror(rc), strerror(errno))
+#endif
+ : US gnutls_strerror(rc);
+
+(void) tls_error(when, msg, state->host, &errstr);
+
+if (state->host)
+ log_write(0, LOG_MAIN, "H=%s [%s] TLS error on connection %s",
+ state->host->name, state->host->address, errstr);
+else
+ {
+ uschar * conn_info = smtp_get_connection_info();
+ if (Ustrncmp(conn_info, US"SMTP ", 5) == 0) conn_info += 5;
+ /* I'd like to get separated H= here, but too hard for now */
+ log_write(0, LOG_MAIN, "TLS error on %s %s", conn_info, errstr);
+ }
+}
+
+
+
+
+/*************************************************
+* 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_t * 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 for some SASL mechanisms
+ tls_ver a string
+ 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)
+{
+tls_support * tlsp = state->tlsp;
+
+tlsp->active.sock = state->fd_out;
+tlsp->active.tls_ctx = state;
+
+DEBUG(D_tls) debug_printf("cipher: %s\n", state->ciphersuite);
+
+tlsp->certificate_verified = state->peer_cert_verified;
+#ifdef SUPPORT_DANE
+tlsp->dane_verified = state->peer_dane_verified;
+#endif
+
+/* note that tls_channelbinding is not saved to the spool file, since it's
+only available for use for authenticators while this TLS session is running. */
+
+tlsp->channelbinding = NULL;
+#ifdef HAVE_GNUTLS_SESSION_CHANNEL_BINDING
+ {
+ gnutls_datum_t channel = {.data = NULL, .size = 0};
+ uschar * buf;
+ int rc;
+
+# ifdef HAVE_GNUTLS_PRF_RFC5705
+ /* Older libraries may not have GNUTLS_TLS1_3 defined! */
+ if (gnutls_protocol_get_version(state->session) > GNUTLS_TLS1_2)
+ {
+ buf = store_get(32, state->host ? GET_TAINTED : GET_UNTAINTED);
+ rc = gnutls_prf_rfc5705(state->session,
+ (size_t)24, "EXPORTER-Channel-Binding", (size_t)0, "",
+ 32, CS buf);
+ channel.data = buf;
+ channel.size = 32;
+ }
+ else
+# endif
+ rc = gnutls_session_channel_binding(state->session, GNUTLS_CB_TLS_UNIQUE, &channel);
+
+ if (rc)
+ { DEBUG(D_tls) debug_printf("extracting channel binding: %s\n", gnutls_strerror(rc)); }
+ else
+ {
+ int old_pool = store_pool;
+ /* Declare the taintedness of the binding info. On server, untainted; on
+ client, tainted - being the Finish msg from the server. */
+
+ store_pool = POOL_PERM;
+ tlsp->channelbinding = b64encode_taint(CUS channel.data, (int)channel.size,
+ state->host ? GET_TAINTED : GET_UNTAINTED);
+ 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;
+
+/* do not corrupt sni sent by client; record sni rxd by server */
+if (!state->host)
+ tlsp->sni = state->received_sni;
+
+/* record our certificate */
+ {
+ const gnutls_datum_t * 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(uschar ** errstr)
+{
+int fd, rc;
+unsigned int dh_bits;
+gnutls_datum_t m;
+uschar filename_buf[PATH_MAX];
+uschar *filename = NULL;
+size_t sz;
+uschar *exp_tls_dhparam;
+BOOL use_file_in_spool = FALSE;
+host_item *host = NULL; /* dummy for macros */
+
+DEBUG(D_tls) debug_printf("Initialising GnuTLS server params\n");
+
+if ((rc = gnutls_dh_params_init(&dh_server_params)))
+ return tls_error_gnu(NULL, US"gnutls_dh_params_init", rc, errstr);
+
+m.data = NULL;
+m.size = 0;
+
+if (!expand_check(tls_dhparam, US"tls_dhparam", &exp_tls_dhparam, errstr))
+ 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] != '/')
+ {
+ if (!(m.data = US std_dh_prime_named(exp_tls_dhparam)))
+ return tls_error(US"No standard prime named", exp_tls_dhparam, NULL, errstr);
+ m.size = Ustrlen(m.data);
+ }
+else
+ filename = exp_tls_dhparam;
+
+if (m.data)
+ {
+ if ((rc = gnutls_dh_params_import_pkcs3(dh_server_params, &m, GNUTLS_X509_FMT_PEM)))
+ return tls_error_gnu(NULL, US"gnutls_dh_params_import_pkcs3", rc, errstr);
+ 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. */
+
+if (!(dh_bits = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, GNUTLS_SEC_PARAM_NORMAL)))
+ return tls_error(US"gnutls_sec_param_to_pk_bits() failed", NULL, NULL, errstr);
+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, errstr);
+ filename = filename_buf;
+ }
+
+/* Open the cache file for reading and if successful, read it and set up the
+parameters. */
+
+if ((fd = Uopen(filename, O_RDONLY, 0)) >= 0)
+ {
+ struct stat statbuf;
+ FILE *fp;
+ int saved_errno;
+
+ if (fstat(fd, &statbuf) < 0) /* EIO */
+ {
+ saved_errno = errno;
+ (void)close(fd);
+ return tls_error_sys(US"TLS cache stat failed", saved_errno, NULL, errstr);
+ }
+ if (!S_ISREG(statbuf.st_mode))
+ {
+ (void)close(fd);
+ return tls_error(US"TLS cache not a file", NULL, NULL, errstr);
+ }
+ if (!(fp = fdopen(fd, "rb")))
+ {
+ saved_errno = errno;
+ (void)close(fd);
+ return tls_error_sys(US"fdopen(TLS cache stat fd) failed",
+ saved_errno, NULL, errstr);
+ }
+
+ m.size = statbuf.st_size;
+ if (!(m.data = store_malloc(m.size)))
+ {
+ fclose(fp);
+ return tls_error_sys(US"malloc failed", errno, NULL, errstr);
+ }
+ if (!(sz = fread(m.data, m.size, 1, fp)))
+ {
+ saved_errno = errno;
+ fclose(fp);
+ store_free(m.data);
+ return tls_error_sys(US"fread failed", saved_errno, NULL, errstr);
+ }
+ fclose(fp);
+
+ rc = gnutls_dh_params_import_pkcs3(dh_server_params, &m, GNUTLS_X509_FMT_PEM);
+ store_free(m.data);
+ if (rc)
+ return tls_error_gnu(NULL, US"gnutls_dh_params_import_pkcs3", rc, errstr);
+ 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("\"%s\" for reading", filename),
+ NULL, NULL, errstr);
+
+/* 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",
+ filename, NULL, errstr);
+
+ temp_fn = string_copy(US"exim-dh.XXXXXXX");
+ if ((fd = mkstemp(CS temp_fn)) < 0) /* modifies temp_fn */
+ return tls_error_sys(US"Unable to open temp file", errno, NULL, errstr);
+ (void)exim_chown(temp_fn, 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);
+ if ((rc = gnutls_dh_params_generate2(dh_server_params, dh_bits_gen)))
+ return tls_error_gnu(NULL, US"gnutls_dh_params_generate2", rc, errstr);
+
+ /* 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;
+ if ( (rc = gnutls_dh_params_export_pkcs3(dh_server_params,
+ GNUTLS_X509_FMT_PEM, m.data, &sz))
+ && rc != GNUTLS_E_SHORT_MEMORY_BUFFER)
+ return tls_error_gnu(NULL, US"gnutls_dh_params_export_pkcs3(NULL) sizing",
+ rc, errstr);
+ m.size = sz;
+ if (!(m.data = store_malloc(m.size)))
+ return tls_error_sys(US"memory allocation failed", errno, NULL, errstr);
+
+ /* this will return a size 1 less than the allocation size above */
+ if ((rc = gnutls_dh_params_export_pkcs3(dh_server_params, GNUTLS_X509_FMT_PEM,
+ m.data, &sz)))
+ {
+ store_free(m.data);
+ return tls_error_gnu(NULL, US"gnutls_dh_params_export_pkcs3() real", rc, errstr);
+ }
+ m.size = sz; /* shrink by 1, probably */
+
+ if ((sz = write_to_fd_buf(fd, m.data, (size_t) m.size)) != m.size)
+ {
+ store_free(m.data);
+ return tls_error_sys(US"TLS cache write D-H params failed",
+ errno, NULL, errstr);
+ }
+ store_free(m.data);
+ if ((sz = write_to_fd_buf(fd, US"\n", 1)) != 1)
+ return tls_error_sys(US"TLS cache write D-H params final newline failed",
+ errno, NULL, errstr);
+
+ if ((rc = close(fd)))
+ return tls_error_sys(US"TLS cache write close() failed", errno, NULL, errstr);
+
+ if (Urename(temp_fn, filename) < 0)
+ return tls_error_sys(string_sprintf("failed to rename \"%s\" as \"%s\"",
+ temp_fn, filename), errno, NULL, errstr);
+
+ 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;
+}
+
+
+
+
+/* Create and install a selfsigned certificate, for use in server mode. */
+
+static int
+tls_install_selfsign(exim_gnutls_state_st * state, uschar ** errstr)
+{
+gnutls_x509_crt_t cert = NULL;
+time_t now;
+gnutls_x509_privkey_t pkey = NULL;
+const uschar * where;
+int rc;
+
+#ifndef SUPPORT_SELFSIGN
+where = US"library too old";
+rc = GNUTLS_E_NO_CERTIFICATE_FOUND;
+if (TRUE) goto err;
+#endif
+
+DEBUG(D_tls) debug_printf("TLS: generating selfsigned server cert\n");
+where = US"initialising pkey";
+if ((rc = gnutls_x509_privkey_init(&pkey))) goto err;
+
+where = US"initialising cert";
+if ((rc = gnutls_x509_crt_init(&cert))) goto err;
+
+where = US"generating pkey"; /* Hangs on 2.12.23 */
+if ((rc = gnutls_x509_privkey_generate(pkey, GNUTLS_PK_RSA,
+#ifdef SUPPORT_PARAM_TO_PK_BITS
+# ifndef GNUTLS_SEC_PARAM_MEDIUM
+# define GNUTLS_SEC_PARAM_MEDIUM GNUTLS_SEC_PARAM_HIGH
+# endif
+ gnutls_sec_param_to_pk_bits(GNUTLS_PK_RSA, GNUTLS_SEC_PARAM_MEDIUM),
+#else
+ 2048,
+#endif
+ 0)))
+ goto err;
+
+where = US"configuring cert";
+now = 1;
+if ( (rc = gnutls_x509_crt_set_version(cert, 3))
+ || (rc = gnutls_x509_crt_set_serial(cert, &now, sizeof(now)))
+ || (rc = gnutls_x509_crt_set_activation_time(cert, now = time(NULL)))
+ || (rc = gnutls_x509_crt_set_expiration_time(cert, (long)2 * 60 * 60)) /* 2 hour */
+ || (rc = gnutls_x509_crt_set_key(cert, pkey))
+
+ || (rc = gnutls_x509_crt_set_dn_by_oid(cert,
+ GNUTLS_OID_X520_COUNTRY_NAME, 0, "UK", 2))
+ || (rc = gnutls_x509_crt_set_dn_by_oid(cert,
+ GNUTLS_OID_X520_ORGANIZATION_NAME, 0, "Exim Developers", 15))
+ || (rc = gnutls_x509_crt_set_dn_by_oid(cert,
+ GNUTLS_OID_X520_COMMON_NAME, 0,
+ smtp_active_hostname, Ustrlen(smtp_active_hostname)))
+ )
+ goto err;
+
+where = US"signing cert";
+if ((rc = gnutls_x509_crt_sign(cert, cert, pkey))) goto err;
+
+where = US"installing selfsign cert";
+ /* Since: 2.4.0 */
+if ((rc = gnutls_certificate_set_x509_key(state->lib_state.x509_cred,
+ &cert, 1, pkey)))
+ goto err;
+
+rc = OK;
+
+out:
+ if (cert) gnutls_x509_crt_deinit(cert);
+ if (pkey) gnutls_x509_privkey_deinit(pkey);
+ return rc;
+
+err:
+ rc = tls_error_gnu(state, where, rc, errstr);
+ goto out;
+}
+
+
+
+
+/* Add certificate and key, from files.
+
+Return:
+ Zero or negative: good. Negate value for certificate index if < 0.
+ Greater than zero: FAIL or DEFER code.
+*/
+
+static int
+tls_add_certfile(exim_gnutls_state_st * state, const host_item * host,
+ const uschar * certfile, const uschar * keyfile, uschar ** errstr)
+{
+int rc = gnutls_certificate_set_x509_key_file(state->lib_state.x509_cred,
+ CCS certfile, CCS keyfile, GNUTLS_X509_FMT_PEM);
+if (rc < 0)
+ return tls_error_gnu(state,
+ string_sprintf("cert/key setup: cert=%s key=%s", certfile, keyfile),
+ rc, errstr);
+return -rc;
+}
+
+
+#if !defined(DISABLE_OCSP) && !defined(SUPPORT_GNUTLS_EXT_RAW_PARSE)
+/* Load an OCSP proof from file for sending by the server. Called
+on getting a status-request handshake message, for earlier versions
+of GnuTLS. */
+
+static int
+server_ocsp_stapling_cb(gnutls_session_t session, void * ptr,
+ gnutls_datum_t * ocsp_response)
+{
+int ret;
+DEBUG(D_tls) debug_printf("OCSP stapling callback: %s\n", US ptr);
+
+if ((ret = gnutls_load_file(ptr, ocsp_response)) < 0)
+ {
+ DEBUG(D_tls) debug_printf("Failed to load ocsp stapling file %s\n",
+ CS ptr);
+ tls_in.ocsp = OCSP_NOT_RESP;
+ return GNUTLS_E_NO_CERTIFICATE_STATUS;
+ }
+
+tls_in.ocsp = OCSP_VFY_NOT_TRIED;
+return 0;
+}
+#endif
+
+
+#ifdef SUPPORT_GNUTLS_EXT_RAW_PARSE
+/* Make a note that we saw a status-request */
+static int
+tls_server_clienthello_ext(void * ctx, unsigned tls_id,
+ const uschar * data, unsigned size)
+{
+/* The values for tls_id are documented here:
+https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml */
+switch (tls_id)
+ {
+ case 5: /* Status Request */
+ DEBUG(D_tls) debug_printf("Seen status_request extension from client\n");
+ tls_in.ocsp = OCSP_NOT_RESP;
+ break;
+#ifdef EXIM_HAVE_ALPN
+ case 16: /* Application Layer Protocol Notification */
+ /* The format of "data" here doesn't seem to be documented, but appears
+ to be a 2-byte field with a (redundant, given the "size" arg) total length
+ then a sequence of one-byte size then string (not nul-term) names. The
+ latter is as described in OpenSSL documentation. */
+
+ DEBUG(D_tls) debug_printf("Seen ALPN extension from client (s=%u):", size);
+ for (const uschar * s = data+2; s-data < size-1; s += *s + 1)
+ {
+ server_seen_alpn++;
+ DEBUG(D_tls) debug_printf(" '%.*s'", (int)*s, s+1);
+ }
+ DEBUG(D_tls) debug_printf("\n");
+ if (server_seen_alpn > 1)
+ {
+ DEBUG(D_tls) debug_printf("TLS: too many ALPNs presented in handshake\n");
+ return GNUTLS_E_NO_APPLICATION_PROTOCOL;
+ }
+ break;
+#endif
+ }
+return 0;
+}
+
+/* Callback for client-hello, on server, if we think we might serve stapled-OCSP */
+static int
+tls_server_clienthello_cb(gnutls_session_t session, unsigned int htype,
+ unsigned when, unsigned int incoming, const gnutls_datum_t * msg)
+{
+/* Call fn for each extension seen. 3.6.3 onwards */
+return gnutls_ext_raw_parse(NULL, tls_server_clienthello_ext, msg,
+ GNUTLS_EXT_RAW_FLAG_TLS_CLIENT_HELLO);
+}
+
+
+# ifdef notdef_crashes
+/* Make a note that we saw a status-response */
+static int
+tls_server_servercerts_ext(void * ctx, unsigned tls_id,
+ const unsigned char *data, unsigned size)
+{
+/* debug_printf("%s %u\n", __FUNCTION__, tls_id); */
+/* https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml */
+if (FALSE && tls_id == 5) /* status_request */
+ {
+ DEBUG(D_tls) debug_printf("Seen status_request extension\n");
+ tls_in.ocsp = exim_testharness_disable_ocsp_validity_check
+ ? OCSP_VFY_NOT_TRIED : OCSP_VFIED; /* We know that GnuTLS verifies responses */
+ }
+return 0;
+}
+# endif
+
+/* Callback for certificates packet, on server, if we think we might serve stapled-OCSP */
+static int
+tls_server_servercerts_cb(gnutls_session_t session, unsigned int htype,
+ unsigned when, unsigned int incoming, const gnutls_datum_t * msg)
+{
+/* Call fn for each extension seen. 3.6.3 onwards */
+# ifdef notdef_crashes
+ /*XXX crashes */
+return gnutls_ext_raw_parse(NULL, tls_server_servercerts_ext, msg, 0);
+# endif
+}
+#endif /*SUPPORT_GNUTLS_EXT_RAW_PARSE*/
+
+/*XXX in tls1.3 the cert-status travel as an extension next to the cert, in the
+ "Handshake Protocol: Certificate" record.
+So we need to spot the Certificate handshake message, parse it and spot any status_request extension(s)
+
+This is different to tls1.2 - where it is a separate record (wireshark term) / handshake message (gnutls term).
+*/
+
+#if defined(EXIM_HAVE_TLS_RESUME) || defined(SUPPORT_GNUTLS_EXT_RAW_PARSE)
+/* Callback for certificate-status, on server. We sent stapled OCSP. */
+static int
+tls_server_certstatus_cb(gnutls_session_t session, unsigned int htype,
+ unsigned when, unsigned int incoming, const gnutls_datum_t * msg)
+{
+DEBUG(D_tls) debug_printf("Sending certificate-status\n"); /*XXX we get this for tls1.2 but not for 1.3 */
+# ifdef SUPPORT_SRV_OCSP_STACK
+tls_in.ocsp = exim_testharness_disable_ocsp_validity_check
+ ? OCSP_VFY_NOT_TRIED : OCSP_VFIED; /* We know that GnuTLS verifies responses */
+# else
+tls_in.ocsp = OCSP_VFY_NOT_TRIED;
+# endif
+return 0;
+}
+
+/* Callback for handshake messages, on server */
+static int
+tls_server_hook_cb(gnutls_session_t sess, u_int htype, unsigned when,
+ unsigned incoming, const gnutls_datum_t * msg)
+{
+/* debug_printf("%s: htype %u\n", __FUNCTION__, htype); */
+switch (htype)
+ {
+# ifdef SUPPORT_GNUTLS_EXT_RAW_PARSE
+ case GNUTLS_HANDSHAKE_CLIENT_HELLO:
+ return tls_server_clienthello_cb(sess, htype, when, incoming, msg);
+ case GNUTLS_HANDSHAKE_CERTIFICATE_PKT:
+ return tls_server_servercerts_cb(sess, htype, when, incoming, msg);
+# endif
+ case GNUTLS_HANDSHAKE_CERTIFICATE_STATUS:
+ return tls_server_certstatus_cb(sess, htype, when, incoming, msg);
+# ifdef EXIM_HAVE_TLS_RESUME
+ case GNUTLS_HANDSHAKE_NEW_SESSION_TICKET:
+ return tls_server_ticket_cb(sess, htype, when, incoming, msg);
+# endif
+ default:
+ return 0;
+ }
+}
+#endif
+
+
+#if !defined(DISABLE_OCSP) && defined(SUPPORT_GNUTLS_EXT_RAW_PARSE)
+static void
+tls_server_testharness_ocsp_fiddle(void)
+{
+extern char ** environ;
+if (environ) for (uschar ** p = USS environ; *p; p++)
+ if (Ustrncmp(*p, "EXIM_TESTHARNESS_DISABLE_OCSPVALIDITYCHECK", 42) == 0)
+ {
+ DEBUG(D_tls) debug_printf("Permitting known bad OCSP response\n");
+ exim_testharness_disable_ocsp_validity_check = TRUE;
+ }
+}
+#endif
+
+/**************************************************
+* One-time init credentials for server and client *
+**************************************************/
+
+static void
+creds_basic_init(gnutls_certificate_credentials_t x509_cred, BOOL server)
+{
+#ifdef SUPPORT_SRV_OCSP_STACK
+gnutls_certificate_set_flags(x509_cred, GNUTLS_CERTIFICATE_API_V2);
+
+# if !defined(DISABLE_OCSP) && defined(SUPPORT_GNUTLS_EXT_RAW_PARSE)
+if (server && tls_ocsp_file)
+ {
+ if (f.running_in_test_harness)
+ tls_server_testharness_ocsp_fiddle();
+
+ if (exim_testharness_disable_ocsp_validity_check)
+ gnutls_certificate_set_flags(x509_cred,
+ GNUTLS_CERTIFICATE_API_V2 | GNUTLS_CERTIFICATE_SKIP_OCSP_RESPONSE_CHECK);
+ }
+# endif
+#endif
+DEBUG(D_tls)
+ debug_printf("TLS: basic cred init, %s\n", server ? "server" : "client");
+}
+
+static int
+creds_load_server_certs(exim_gnutls_state_st * state, const uschar * cert,
+ const uschar * pkey, const uschar * ocsp, uschar ** errstr)
+{
+const uschar * clist = cert;
+const uschar * klist = pkey;
+const uschar * olist;
+int csep = 0, ksep = 0, osep = 0, cnt = 0, rc;
+uschar * cfile, * kfile, * ofile;
+#ifndef DISABLE_OCSP
+# ifdef SUPPORT_GNUTLS_EXT_RAW_PARSE
+gnutls_x509_crt_fmt_t ocsp_fmt = GNUTLS_X509_FMT_DER;
+# endif
+
+if (!expand_check(ocsp, US"tls_ocsp_file", &ofile, errstr))
+ return DEFER;
+olist = ofile;
+#endif
+
+while (cfile = string_nextinlist(&clist, &csep, NULL, 0))
+
+ if (!(kfile = string_nextinlist(&klist, &ksep, NULL, 0)))
+ return tls_error(US"cert/key setup: out of keys", NULL, NULL, errstr);
+ else if ((rc = tls_add_certfile(state, NULL, cfile, kfile, errstr)) > 0)
+ return rc;
+ else
+ {
+ int gnutls_cert_index = -rc;
+ DEBUG(D_tls) debug_printf("TLS: cert/key %d %s registered\n",
+ gnutls_cert_index, cfile);
+
+#ifndef DISABLE_OCSP
+ if (ocsp)
+ {
+ /* Set the OCSP stapling server info */
+ if (gnutls_buggy_ocsp)
+ {
+ DEBUG(D_tls)
+ debug_printf("GnuTLS library is buggy for OCSP; avoiding\n");
+ }
+ else if ((ofile = string_nextinlist(&olist, &osep, NULL, 0)))
+ {
+ DEBUG(D_tls) debug_printf("OCSP response file %d = %s\n",
+ gnutls_cert_index, ofile);
+# ifdef SUPPORT_GNUTLS_EXT_RAW_PARSE
+ if (Ustrncmp(ofile, US"PEM ", 4) == 0)
+ {
+ ocsp_fmt = GNUTLS_X509_FMT_PEM;
+ ofile += 4;
+ }
+ else if (Ustrncmp(ofile, US"DER ", 4) == 0)
+ {
+ ocsp_fmt = GNUTLS_X509_FMT_DER;
+ ofile += 4;
+ }
+
+ if ((rc = gnutls_certificate_set_ocsp_status_request_file2(
+ state->lib_state.x509_cred, CCS ofile, gnutls_cert_index,
+ ocsp_fmt)) < 0)
+ return tls_error_gnu(state,
+ US"gnutls_certificate_set_ocsp_status_request_file2",
+ rc, errstr);
+ DEBUG(D_tls)
+ debug_printf(" %d response%s loaded\n", rc, rc>1 ? "s":"");
+
+ /* Arrange callbacks for OCSP request observability */
+
+ if (state->session)
+ gnutls_handshake_set_hook_function(state->session,
+ GNUTLS_HANDSHAKE_ANY, GNUTLS_HOOK_POST, tls_server_hook_cb);
+ else
+ state->lib_state.ocsp_hook = TRUE;
+
+
+# else
+# if defined(SUPPORT_SRV_OCSP_STACK)
+ if ((rc = gnutls_certificate_set_ocsp_status_request_function2(
+ state->lib_state.x509_cred, gnutls_cert_index,
+ server_ocsp_stapling_cb, ofile)))
+ return tls_error_gnu(state,
+ US"gnutls_certificate_set_ocsp_status_request_function2",
+ rc, errstr);
+ else
+# endif
+ {
+ if (cnt++ > 0)
+ {
+ DEBUG(D_tls)
+ debug_printf("oops; multiple OCSP files not supported\n");
+ break;
+ }
+ gnutls_certificate_set_ocsp_status_request_function(
+ state->lib_state.x509_cred, server_ocsp_stapling_cb, ofile);
+ }
+# endif /* SUPPORT_GNUTLS_EXT_RAW_PARSE */
+ }
+ else
+ DEBUG(D_tls) debug_printf("ran out of OCSP response files in list\n");
+ }
+#endif /* DISABLE_OCSP */
+ }
+return 0;
+}
+
+static int
+creds_load_client_certs(exim_gnutls_state_st * state, const host_item * host,
+ const uschar * cert, const uschar * pkey, uschar ** errstr)
+{
+int rc = tls_add_certfile(state, host, cert, pkey, errstr);
+if (rc > 0) return rc;
+DEBUG(D_tls) debug_printf("TLS: cert/key registered\n");
+return 0;
+}
+
+static int
+creds_load_cabundle(exim_gnutls_state_st * state, const uschar * bundle,
+ const host_item * host, uschar ** errstr)
+{
+int cert_count;
+struct stat statbuf;
+
+#ifdef SUPPORT_SYSDEFAULT_CABUNDLE
+if (Ustrcmp(bundle, "system") == 0 || Ustrncmp(bundle, "system,", 7) == 0)
+ cert_count = gnutls_certificate_set_x509_system_trust(state->lib_state.x509_cred);
+else
+#endif
+ {
+ if (Ustat(bundle, &statbuf) < 0)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "could not stat '%s' "
+ "(tls_verify_certificates): %s", bundle, strerror(errno));
+ return DEFER;
+ }
+
+#ifndef SUPPORT_CA_DIR
+ /* 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 messaging ... */
+ if (S_ISDIR(statbuf.st_mode))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "tls_verify_certificates \"%s\" is a directory", bundle);
+ return DEFER;
+ }
+#endif
+
+ DEBUG(D_tls) debug_printf("verify certificates = %s size=" OFF_T_FMT "\n",
+ bundle, 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 =
+
+#ifdef SUPPORT_CA_DIR
+ (statbuf.st_mode & S_IFMT) == S_IFDIR
+ ?
+ gnutls_certificate_set_x509_trust_dir(state->lib_state.x509_cred,
+ CS bundle, GNUTLS_X509_FMT_PEM)
+ :
+#endif
+ gnutls_certificate_set_x509_trust_file(state->lib_state.x509_cred,
+ CS bundle, GNUTLS_X509_FMT_PEM);
+
+#ifdef SUPPORT_CA_DIR
+ /* Mimic the behaviour with OpenSSL of not advertising a usable-cert list
+ when using the directory-of-certs config model. */
+
+ if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
+ if (state->session)
+ gnutls_certificate_send_x509_rdn_sequence(state->session, 1);
+ else
+ state->lib_state.ca_rdn_emulate = TRUE;
+#endif
+ }
+
+if (cert_count < 0)
+ return tls_error_gnu(state, US"setting certificate trust", cert_count, errstr);
+DEBUG(D_tls)
+ debug_printf("Added %d certificate authorities\n", cert_count);
+
+return OK;
+}
+
+
+static int
+creds_load_crl(exim_gnutls_state_st * state, const uschar * crl, uschar ** errstr)
+{
+int cert_count;
+DEBUG(D_tls) debug_printf("loading CRL file = %s\n", crl);
+if ((cert_count = gnutls_certificate_set_x509_crl_file(state->lib_state.x509_cred,
+ CS crl, GNUTLS_X509_FMT_PEM)) < 0)
+ return tls_error_gnu(state, US"gnutls_certificate_set_x509_crl_file",
+ cert_count, errstr);
+
+DEBUG(D_tls) debug_printf("Processed %d CRLs\n", cert_count);
+return OK;
+}
+
+
+static int
+creds_load_pristring(exim_gnutls_state_st * state, const uschar * p,
+ const char ** errpos)
+{
+if (!p)
+ {
+ p = exim_default_gnutls_priority;
+ DEBUG(D_tls)
+ debug_printf("GnuTLS using default session cipher/priority \"%s\"\n", p);
+ }
+return gnutls_priority_init( (gnutls_priority_t *) &state->lib_state.pri_cache,
+ CCS p, errpos);
+}
+
+static unsigned
+tls_server_creds_init(void)
+{
+uschar * dummy_errstr;
+unsigned lifetime = 0;
+
+state_server.lib_state = null_tls_preload;
+if (gnutls_certificate_allocate_credentials(
+ (gnutls_certificate_credentials_t *) &state_server.lib_state.x509_cred))
+ {
+ state_server.lib_state.x509_cred = NULL;
+ return lifetime;
+ }
+creds_basic_init(state_server.lib_state.x509_cred, TRUE);
+
+#if defined(EXIM_HAVE_INOTIFY) || defined(EXIM_HAVE_KEVENT)
+/* If tls_certificate has any $ indicating expansions, it is not good.
+If tls_privatekey is set but has $, not good. Likewise for tls_ocsp_file.
+If all good (and tls_certificate set), load the cert(s). */
+
+if ( opt_set_and_noexpand(tls_certificate)
+# ifndef DISABLE_OCSP
+ && opt_unset_or_noexpand(tls_ocsp_file)
+# endif
+ && opt_unset_or_noexpand(tls_privatekey))
+ {
+ /* Set watches on the filenames. The implementation does de-duplication
+ so we can just blindly do them all.
+ */
+
+ if ( tls_set_watch(tls_certificate, TRUE)
+# ifndef DISABLE_OCSP
+ && tls_set_watch(tls_ocsp_file, TRUE)
+# endif
+ && tls_set_watch(tls_privatekey, TRUE))
+ {
+ DEBUG(D_tls) debug_printf("TLS: preloading server certs\n");
+ if (creds_load_server_certs(&state_server, tls_certificate,
+ tls_privatekey && *tls_privatekey ? tls_privatekey : tls_certificate,
+# ifdef DISABLE_OCSP
+ NULL,
+# else
+ tls_ocsp_file,
+# endif
+ &dummy_errstr) == 0)
+ state_server.lib_state.conn_certs = TRUE;
+ }
+ }
+else if ( !tls_certificate && !tls_privatekey
+# ifndef DISABLE_OCSP
+ && !tls_ocsp_file
+# endif
+ )
+ { /* Generate & preload a selfsigned cert. No files to watch. */
+ if ((tls_install_selfsign(&state_server, &dummy_errstr)) == OK)
+ {
+ state_server.lib_state.conn_certs = TRUE;
+ lifetime = f.running_in_test_harness ? 2 : 60 * 60; /* 1 hour */
+ }
+ }
+else
+ DEBUG(D_tls) debug_printf("TLS: not preloading server certs\n");
+
+/* If tls_verify_certificates is non-empty and has no $, load CAs.
+If none was configured and we can't handle "system", treat as empty. */
+
+if ( opt_set_and_noexpand(tls_verify_certificates)
+#ifndef SUPPORT_SYSDEFAULT_CABUNDLE
+ && Ustrcmp(tls_verify_certificates, "system") != 0
+#endif
+ )
+ {
+ if (tls_set_watch(tls_verify_certificates, FALSE))
+ {
+ DEBUG(D_tls) debug_printf("TLS: preloading CA bundle for server\n");
+ if (creds_load_cabundle(&state_server, tls_verify_certificates,
+ NULL, &dummy_errstr) != OK)
+ return lifetime;
+ state_server.lib_state.cabundle = TRUE;
+
+ /* If CAs loaded and tls_crl is non-empty and has no $, load it */
+
+ if (opt_set_and_noexpand(tls_crl))
+ {
+ if (tls_set_watch(tls_crl, FALSE))
+ {
+ DEBUG(D_tls) debug_printf("TLS: preloading CRL for server\n");
+ if (creds_load_crl(&state_server, tls_crl, &dummy_errstr) != OK)
+ return lifetime;
+ state_server.lib_state.crl = TRUE;
+ }
+ }
+ else
+ DEBUG(D_tls) debug_printf("TLS: not preloading CRL for server\n");
+ }
+ }
+else
+ DEBUG(D_tls) debug_printf("TLS: not preloading CA bundle for server\n");
+#endif /* EXIM_HAVE_INOTIFY */
+
+/* If tls_require_ciphers is non-empty and has no $, load the
+ciphers priority cache. If unset, load with the default.
+(server-only as the client one depends on non/DANE) */
+
+if (!tls_require_ciphers || opt_set_and_noexpand(tls_require_ciphers))
+ {
+ const char * dummy_errpos;
+ DEBUG(D_tls) debug_printf("TLS: preloading cipher list for server: %s\n",
+ tls_require_ciphers);
+ if ( creds_load_pristring(&state_server, tls_require_ciphers, &dummy_errpos)
+ == OK)
+ state_server.lib_state.pri_string = TRUE;
+ }
+else
+ DEBUG(D_tls) debug_printf("TLS: not preloading cipher list for server\n");
+return lifetime;
+}
+
+
+/* Preload whatever creds are static, onto a transport. The client can then
+just copy the pointer as it starts up. */
+
+/*XXX this is not called for a cmdline send. But one needing to use >1 conn would benefit,
+and there seems little downside. */
+
+static void
+tls_client_creds_init(transport_instance * t, BOOL watch)
+{
+smtp_transport_options_block * ob = t->options_block;
+exim_gnutls_state_st tpt_dummy_state;
+host_item * dummy_host = (host_item *)1;
+uschar * dummy_errstr;
+
+if ( !exim_gnutls_base_init_done
+ && tls_g_init(&dummy_errstr) != OK)
+ return;
+
+ob->tls_preload = null_tls_preload;
+if (gnutls_certificate_allocate_credentials(
+ (struct gnutls_certificate_credentials_st **)&ob->tls_preload.x509_cred))
+ {
+ ob->tls_preload.x509_cred = NULL;
+ return;
+ }
+creds_basic_init(ob->tls_preload.x509_cred, FALSE);
+
+tpt_dummy_state.session = NULL;
+tpt_dummy_state.lib_state = ob->tls_preload;
+
+#if defined(EXIM_HAVE_INOTIFY) || defined(EXIM_HAVE_KEVENT)
+if ( opt_set_and_noexpand(ob->tls_certificate)
+ && opt_unset_or_noexpand(ob->tls_privatekey))
+ {
+ if ( !watch
+ || ( tls_set_watch(ob->tls_certificate, FALSE)
+ && tls_set_watch(ob->tls_privatekey, FALSE)
+ ) )
+ {
+ const uschar * pkey = ob->tls_privatekey;
+
+ DEBUG(D_tls)
+ debug_printf("TLS: preloading client certs for transport '%s'\n", t->name);
+
+ /* The state->lib_state.x509_cred is used for the certs load, and is the sole
+ structure element used. So we can set up a dummy. The hoat arg only
+ selects a retcode in case of fail, so any value */
+
+ if (creds_load_client_certs(&tpt_dummy_state, dummy_host,
+ ob->tls_certificate, pkey ? pkey : ob->tls_certificate,
+ &dummy_errstr) == OK)
+ ob->tls_preload.conn_certs = TRUE;
+ }
+ }
+else
+ DEBUG(D_tls)
+ debug_printf("TLS: not preloading client certs, for transport '%s'\n", t->name);
+
+/* If tls_verify_certificates is non-empty and has no $, load CAs.
+If none was configured and we can't handle "system", treat as empty. */
+
+if ( opt_set_and_noexpand(ob->tls_verify_certificates)
+#ifndef SUPPORT_SYSDEFAULT_CABUNDLE
+ && Ustrcmp(ob->tls_verify_certificates, "system") != 0
+#endif
+ )
+ {
+ if (!watch || tls_set_watch(ob->tls_verify_certificates, FALSE))
+ {
+ DEBUG(D_tls)
+ debug_printf("TLS: preloading CA bundle for transport '%s'\n", t->name);
+ if (creds_load_cabundle(&tpt_dummy_state, ob->tls_verify_certificates,
+ dummy_host, &dummy_errstr) != OK)
+ return;
+ ob->tls_preload.cabundle = TRUE;
+
+ if (opt_set_and_noexpand(ob->tls_crl))
+ {
+ if (!watch || tls_set_watch(ob->tls_crl, FALSE))
+ {
+ DEBUG(D_tls) debug_printf("TLS: preloading CRL for transport '%s'\n", t->name);
+ if (creds_load_crl(&tpt_dummy_state, ob->tls_crl, &dummy_errstr) != OK)
+ return;
+ ob->tls_preload.crl = TRUE;
+ }
+ }
+ else
+ DEBUG(D_tls) debug_printf("TLS: not preloading CRL, for transport '%s'\n", t->name);
+ }
+ }
+else
+ DEBUG(D_tls)
+ debug_printf("TLS: not preloading CA bundle, for transport '%s'\n", t->name);
+
+/* We do not preload tls_require_ciphers to to the transport as it implicitly
+depends on DANE or plain usage. */
+
+#endif
+}
+
+
+#if defined(EXIM_HAVE_INOTIFY) || defined(EXIM_HAVE_KEVENT)
+/* Invalidate the creds cached, by dropping the current ones.
+Call when we notice one of the source files has changed. */
+
+static void
+tls_server_creds_invalidate(void)
+{
+if (state_server.lib_state.pri_cache)
+ gnutls_priority_deinit(state_server.lib_state.pri_cache);
+state_server.lib_state.pri_cache = NULL;
+
+if (state_server.lib_state.x509_cred)
+ gnutls_certificate_free_credentials(state_server.lib_state.x509_cred);
+state_server.lib_state = null_tls_preload;
+}
+
+
+static void
+tls_client_creds_invalidate(transport_instance * t)
+{
+smtp_transport_options_block * ob = t->options_block;
+if (ob->tls_preload.x509_cred)
+ gnutls_certificate_free_credentials(ob->tls_preload.x509_cred);
+ob->tls_preload = null_tls_preload;
+}
+#endif
+
+
+/*************************************************
+* 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 *
+ errstr error string pointer
+
+Returns: OK/DEFER/FAIL
+*/
+
+static int
+tls_expand_session_files(exim_gnutls_state_st * state, uschar ** errstr)
+{
+int rc;
+const host_item *host = state->host; /* macro should be reconsidered? */
+const uschar *saved_tls_certificate = NULL;
+const uschar *saved_tls_privatekey = NULL;
+const uschar *saved_tls_verify_certificates = NULL;
+const 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 /* SNI callback case */
+ {
+ /* 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;
+ }
+
+if (!state->lib_state.x509_cred)
+ {
+ if ((rc = gnutls_certificate_allocate_credentials(
+ (gnutls_certificate_credentials_t *) &state->lib_state.x509_cred)))
+ return tls_error_gnu(state, US"gnutls_certificate_allocate_credentials",
+ rc, errstr);
+ creds_basic_init(state->lib_state.x509_cred, !host);
+ }
+
+
+/* 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 (!state->lib_state.conn_certs)
+ {
+ if (!Expand_check_tlsvar(tls_certificate, errstr))
+ return DEFER;
+
+ /* certificate is mandatory in server, optional in client */
+
+ if ( !state->exp_tls_certificate
+ || !*state->exp_tls_certificate
+ )
+ if (!host)
+ return tls_install_selfsign(state, errstr);
+ else
+ DEBUG(D_tls) debug_printf("TLS: no client certificate specified; okay\n");
+
+ if (state->tls_privatekey && !Expand_check_tlsvar(tls_privatekey, errstr))
+ return DEFER;
+
+ /* tls_privatekey is optional, defaulting to same file as certificate */
+
+ if (!state->tls_privatekey || !*state->tls_privatekey)
+ {
+ state->tls_privatekey = state->tls_certificate;
+ state->exp_tls_privatekey = state->exp_tls_certificate;
+ }
+
+ if (state->exp_tls_certificate && *state->exp_tls_certificate)
+ {
+ BOOL load = TRUE;
+ 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");
+ load = FALSE; /* avoid re-loading the same certs */
+ }
+ else /* unload the pre-SNI certs before loading new ones */
+ {
+ DEBUG(D_tls) debug_printf("TLS SNI: have a changed cert/key pair\n");
+ gnutls_certificate_free_keys(state->lib_state.x509_cred);
+ }
+
+ if ( load
+ && (rc = host
+ ? creds_load_client_certs(state, host, state->exp_tls_certificate,
+ state->exp_tls_privatekey, errstr)
+ : creds_load_server_certs(state, state->exp_tls_certificate,
+ state->exp_tls_privatekey,
+#ifdef DISABLE_OCSP
+ NULL,
+#else
+ tls_ocsp_file,
+#endif
+ errstr)
+ ) ) return rc;
+ }
+ }
+else
+ {
+ DEBUG(D_tls)
+ debug_printf("%s certs were preloaded\n", host ? "client" : "server");
+
+ if (!state->tls_privatekey) state->tls_privatekey = state->tls_certificate;
+ state->exp_tls_certificate = US state->tls_certificate;
+ state->exp_tls_privatekey = US state->tls_privatekey;
+
+#ifdef SUPPORT_GNUTLS_EXT_RAW_PARSE
+ if (state->lib_state.ocsp_hook)
+ gnutls_handshake_set_hook_function(state->session,
+ GNUTLS_HANDSHAKE_ANY, GNUTLS_HOOK_POST, tls_server_hook_cb);
+#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 none was configured and we can't handle "system", treat as empty. */
+
+if (!state->lib_state.cabundle)
+ {
+ if (state->tls_verify_certificates && *state->tls_verify_certificates)
+ {
+ if (!Expand_check_tlsvar(tls_verify_certificates, errstr))
+ return DEFER;
+#ifndef SUPPORT_SYSDEFAULT_CABUNDLE
+ if (Ustrcmp(state->exp_tls_verify_certificates, "system") == 0)
+ state->exp_tls_verify_certificates = NULL;
+#endif
+ if (state->tls_crl && *state->tls_crl)
+ if (!Expand_check_tlsvar(tls_crl, errstr))
+ 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;
+ }
+ rc = creds_load_cabundle(state, state->exp_tls_verify_certificates, host, errstr);
+ if (rc != OK) return rc;
+ }
+else
+ {
+ DEBUG(D_tls)
+ debug_printf("%s CA bundle was preloaded\n", host ? "client" : "server");
+ state->exp_tls_verify_certificates = US state->tls_verify_certificates;
+
+#ifdef SUPPORT_CA_DIR
+/* Mimic the behaviour with OpenSSL of not advertising a usable-cert list
+when using the directory-of-certs config model. */
+ if (state->lib_state.ca_rdn_emulate)
+ gnutls_certificate_send_x509_rdn_sequence(state->session, 1);
+#endif
+ }
+
+
+if (!state->lib_state.crl)
+ {
+ if ( state->tls_crl && *state->tls_crl
+ && state->exp_tls_crl && *state->exp_tls_crl)
+ return creds_load_crl(state, state->exp_tls_crl, errstr);
+ }
+else
+ {
+ DEBUG(D_tls)
+ debug_printf("%s CRL was preloaded\n", host ? "client" : "server");
+ state->exp_tls_crl = US state->tls_crl;
+ }
+
+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 *
+ errstr error string pointer
+
+Returns: OK/DEFER/FAIL
+*/
+
+static int
+tls_set_remaining_x509(exim_gnutls_state_st *state, uschar ** errstr)
+{
+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)
+ if ((rc = init_server_dh(errstr)) != OK) return rc;
+
+ /* Unnecessary & discouraged with 3.6.0 or later, according to docs. But without it,
+ no DHE- ciphers are advertised. */
+ gnutls_certificate_set_dh_params(state->lib_state.x509_cred, dh_server_params);
+ }
+
+/* Link the credentials to the session. */
+
+if ((rc = gnutls_credentials_set(state->session,
+ GNUTLS_CRD_CERTIFICATE, state->lib_state.x509_cred)))
+ return tls_error_gnu(state, US"gnutls_credentials_set", rc, errstr);
+
+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
+ ob tranport options block, if client; NULL if server
+ require_ciphers tls_require_ciphers setting
+ caller_state returned state-info structure
+ errstr error string pointer
+
+Returns: OK/DEFER/FAIL
+*/
+
+static int
+tls_init(
+ const host_item *host,
+ smtp_transport_options_block * ob,
+ const uschar * require_ciphers,
+ exim_gnutls_state_st **caller_state,
+ tls_support * tlsp,
+ uschar ** errstr)
+{
+exim_gnutls_state_st * state;
+int rc;
+size_t sz;
+
+if ( !exim_gnutls_base_init_done
+ && (rc = tls_g_init(errstr)) != OK)
+ return rc;
+
+if (host)
+ {
+ /* For client-side sessions we allocate a context. This lets us run
+ several in parallel. */
+
+ int old_pool = store_pool;
+ store_pool = POOL_PERM;
+ state = store_get(sizeof(exim_gnutls_state_st), GET_UNTAINTED);
+ store_pool = old_pool;
+
+ memcpy(state, &exim_gnutls_state_init, sizeof(exim_gnutls_state_init));
+ state->lib_state = ob->tls_preload;
+ state->tlsp = tlsp;
+ DEBUG(D_tls) debug_printf("initialising GnuTLS client session\n");
+ rc = gnutls_init(&state->session, GNUTLS_CLIENT);
+
+ state->tls_certificate = ob->tls_certificate;
+ state->tls_privatekey = ob->tls_privatekey;
+ state->tls_sni = ob->tls_sni;
+ state->tls_verify_certificates = ob->tls_verify_certificates;
+ state->tls_crl = ob->tls_crl;
+ }
+else
+ {
+ /* Server operations always use the one state_server context. It is not
+ shared because we have forked a fresh process for every receive. However it
+ can get re-used for successive TLS sessions on a single TCP connection. */
+
+ state = &state_server;
+ state->tlsp = tlsp;
+ DEBUG(D_tls) debug_printf("initialising GnuTLS server session\n");
+ rc = gnutls_init(&state->session, GNUTLS_SERVER);
+
+ state->tls_certificate = tls_certificate;
+ state->tls_privatekey = tls_privatekey;
+ state->tls_sni = NULL;
+ state->tls_verify_certificates = tls_verify_certificates;
+ state->tls_crl = tls_crl;
+ }
+if (rc)
+ return tls_error_gnu(state, US"gnutls_init", rc, errstr);
+
+state->tls_require_ciphers = require_ciphers;
+state->host = host;
+
+/* This handles the variables that might get re-expanded after TLS SNI;
+tls_certificate, tls_privatekey, tls_verify_certificates, tls_crl */
+
+DEBUG(D_tls)
+ debug_printf("Expanding various TLS configuration options for session credentials\n");
+if ((rc = tls_expand_session_files(state, errstr)) != OK) return rc;
+
+/* These are all other parts of the x509_cred handling, since SNI in GnuTLS
+requires a new structure afterwards. */
+
+if ((rc = tls_set_remaining_x509(state, errstr)) != OK) return rc;
+
+/* set SNI in client, only */
+if (host)
+ {
+ if (!expand_check(state->tls_sni, US"tls_out_sni", &state->tlsp->sni, errstr))
+ 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);
+ if ((rc = gnutls_server_name_set(state->session,
+ GNUTLS_NAME_DNS, state->tlsp->sni, sz)))
+ return tls_error_gnu(state, US"gnutls_server_name_set", rc, errstr);
+ }
+ }
+else if (state->tls_sni)
+ DEBUG(D_tls) debug_printf("*** PROBABLY A BUG *** " \
+ "have an SNI set for a server [%s]\n", state->tls_sni);
+
+if (!state->lib_state.pri_string)
+ {
+ const uschar * p = NULL;
+ const char * errpos;
+
+ /* 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. */
+
+ if (state->tls_require_ciphers && *state->tls_require_ciphers)
+ {
+ if (!Expand_check_tlsvar(tls_require_ciphers, errstr))
+ return DEFER;
+ if (state->exp_tls_require_ciphers && *state->exp_tls_require_ciphers)
+ {
+ p = state->exp_tls_require_ciphers;
+ DEBUG(D_tls) debug_printf("GnuTLS session cipher/priority \"%s\"\n", p);
+ }
+ }
+
+ if ((rc = creds_load_pristring(state, p, &errpos)))
+ return tls_error_gnu(state, string_sprintf(
+ "gnutls_priority_init(%s) failed at offset %ld, \"%.6s..\"",
+ p, (long)(errpos - CS p), errpos),
+ rc, errstr);
+ }
+else
+ {
+ DEBUG(D_tls) debug_printf("cipher list preloaded\n");
+ state->exp_tls_require_ciphers = US state->tls_require_ciphers;
+ }
+
+
+if ((rc = gnutls_priority_set(state->session, state->lib_state.pri_cache)))
+ return tls_error_gnu(state, US"gnutls_priority_set", rc, errstr);
+
+/* This also sets the server ticket expiration time to the same, and
+the STEK rotation time to 3x. */
+
+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 *
+*************************************************/
+
+static const uschar *
+cipher_stdname_kcm(gnutls_kx_algorithm_t kx, gnutls_cipher_algorithm_t cipher,
+ gnutls_mac_algorithm_t mac)
+{
+uschar cs_id[2];
+gnutls_kx_algorithm_t kx_i;
+gnutls_cipher_algorithm_t cipher_i;
+gnutls_mac_algorithm_t mac_i;
+
+for (size_t i = 0;
+ gnutls_cipher_suite_info(i, cs_id, &kx_i, &cipher_i, &mac_i, NULL);
+ i++)
+ if (kx_i == kx && cipher_i == cipher && mac_i == mac)
+ return cipher_stdname(cs_id[0], cs_id[1]);
+return NULL;
+}
+
+
+
+/* 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 *
+ errstr pointer to error string
+
+Returns: OK/DEFER/FAIL
+*/
+
+static int
+peer_status(exim_gnutls_state_st * state, uschar ** errstr)
+{
+gnutls_session_t session = state->session;
+const gnutls_datum_t * 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 * 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(session);
+protocol = gnutls_protocol_get_version(session);
+mac = gnutls_mac_get(session);
+kx =
+#ifdef GNUTLS_TLS1_3
+ protocol >= GNUTLS_TLS1_3 ? 0 :
+#endif
+ gnutls_kx_get(session);
+
+old_pool = store_pool;
+ {
+ tls_support * tlsp = state->tlsp;
+ store_pool = POOL_PERM;
+
+#ifdef SUPPORT_GNUTLS_SESS_DESC
+ {
+ gstring * g = NULL;
+ uschar * s = US gnutls_session_get_desc(session), c;
+
+ /* Nikos M suggests we use this by preference. It returns like:
+ (TLS1.3)-(ECDHE-SECP256R1)-(RSA-PSS-RSAE-SHA256)-(AES-256-GCM)
+
+ For partial back-compat, put a colon after the TLS version, replace the
+ )-( grouping with __, replace in-group - with _ and append the :keysize. */
+
+ /* debug_printf("peer_status: gnutls_session_get_desc %s\n", s); */
+
+ for (s++; (c = *s) && c != ')'; s++) g = string_catn(g, s, 1);
+
+ tlsp->ver = string_copyn(g->s, g->ptr);
+ for (uschar * p = US tlsp->ver; *p; p++)
+ if (*p == '-') { *p = '\0'; break; } /* TLS1.0-PKIX -> TLS1.0 */
+
+ g = string_catn(g, US":", 1);
+ if (*s) s++; /* now on _ between groups */
+ while ((c = *s))
+ {
+ for (*++s && ++s; (c = *s) && c != ')'; s++)
+ g = string_catn(g, c == '-' ? US"_" : s, 1);
+ /* now on ) closing group */
+ if ((c = *s) && *++s == '-') g = string_catn(g, US"__", 2);
+ /* now on _ between groups */
+ }
+ g = string_catn(g, US":", 1);
+ g = string_cat(g, string_sprintf("%d", (int) gnutls_cipher_get_key_size(cipher) * 8));
+ state->ciphersuite = string_from_gstring(g);
+ }
+#else
+ state->ciphersuite = string_sprintf("%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 (uschar * p = state->ciphersuite; *p; p++) if (isspace(*p)) *p = '-';
+ tlsp->ver = string_copyn(state->ciphersuite,
+ Ustrchr(state->ciphersuite, ':') - state->ciphersuite);
+#endif
+
+/* debug_printf("peer_status: ciphersuite %s\n", state->ciphersuite); */
+
+ tlsp->cipher = state->ciphersuite;
+ tlsp->bits = gnutls_cipher_get_key_size(cipher) * 8;
+
+ tlsp->cipher_stdname = cipher_stdname_kcm(kx, cipher, mac);
+ }
+store_pool = old_pool;
+
+/* tls_peerdn */
+cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
+
+if (!cert_list || 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",
+ US"no certificate received from peer", state->host, errstr);
+ return OK;
+ }
+
+if ((ct = gnutls_certificate_type_get(session)) != GNUTLS_CRT_X509)
+ {
+ const uschar * ctn = US 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, errstr);
+ 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_gnu(state, (Label), rc, errstr); \
+ 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, GET_TAINTED);
+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 *
+ errstr 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, uschar ** errstr)
+{
+int rc;
+uint verify;
+
+DEBUG(D_tls) debug_printf("TLS: checking peer certificate\n");
+*errstr = NULL;
+rc = peer_status(state, errstr);
+
+if (state->verify_requirement == VERIFY_NONE)
+ return TRUE;
+
+if (rc != OK || !state->peerdn)
+ {
+ verify = GNUTLS_CERT_INVALID;
+ *errstr = US"certificate not supplied";
+ }
+else
+
+ {
+#ifdef SUPPORT_DANE
+ if (state->verify_requirement == VERIFY_DANE && state->host)
+ {
+ /* Using dane_verify_session_crt() would be easy, as it does it all for us
+ including talking to a DNS resolver. But we want to do that bit ourselves
+ as the testsuite intercepts and fakes its own DNS environment. */
+
+ dane_state_t s;
+ dane_query_t r;
+ uint lsize;
+ const gnutls_datum_t * certlist =
+ gnutls_certificate_get_peers(state->session, &lsize);
+ int usage = tls_out.tlsa_usage;
+
+# ifdef GNUTLS_BROKEN_DANE_VALIDATION
+ /* Split the TLSA records into two sets, TA and EE selectors. Run the
+ dane-verification separately so that we know which selector verified;
+ then we know whether to do name-verification (needed for TA but not EE). */
+
+ if (usage == ((1<<DANESSL_USAGE_DANE_TA) | (1<<DANESSL_USAGE_DANE_EE)))
+ { /* a mixed-usage bundle */
+ int i, j, nrec;
+ const char ** dd;
+ int * ddl;
+
+ for (nrec = 0; state->dane_data_len[nrec]; ) nrec++;
+ nrec++;
+
+ dd = store_get(nrec * sizeof(uschar *), GET_UNTAINTED);
+ ddl = store_get(nrec * sizeof(int), GET_UNTAINTED);
+ nrec--;
+
+ if ((rc = dane_state_init(&s, 0)))
+ goto tlsa_prob;
+
+ for (usage = DANESSL_USAGE_DANE_EE;
+ usage >= DANESSL_USAGE_DANE_TA; usage--)
+ { /* take records with this usage */
+ for (j = i = 0; i < nrec; i++)
+ if (state->dane_data[i][0] == usage)
+ {
+ dd[j] = state->dane_data[i];
+ ddl[j++] = state->dane_data_len[i];
+ }
+ if (j)
+ {
+ dd[j] = NULL;
+ ddl[j] = 0;
+
+ if ((rc = dane_raw_tlsa(s, &r, (char * const *)dd, ddl, 1, 0)))
+ goto tlsa_prob;
+
+ if ((rc = dane_verify_crt_raw(s, certlist, lsize,
+ gnutls_certificate_type_get(state->session),
+ r, 0,
+ usage == DANESSL_USAGE_DANE_EE
+ ? DANE_VFLAG_ONLY_CHECK_EE_USAGE : 0,
+ &verify)))
+ {
+ DEBUG(D_tls)
+ debug_printf("TLSA record problem: %s\n", dane_strerror(rc));
+ }
+ else if (verify == 0) /* verification passed */
+ {
+ usage = 1 << usage;
+ break;
+ }
+ }
+ }
+
+ if (rc) goto tlsa_prob;
+ }
+ else
+# endif
+ {
+ if ( (rc = dane_state_init(&s, 0))
+ || (rc = dane_raw_tlsa(s, &r, state->dane_data, state->dane_data_len,
+ 1, 0))
+ || (rc = dane_verify_crt_raw(s, certlist, lsize,
+ gnutls_certificate_type_get(state->session),
+ r, 0,
+# ifdef GNUTLS_BROKEN_DANE_VALIDATION
+ usage == (1 << DANESSL_USAGE_DANE_EE)
+ ? DANE_VFLAG_ONLY_CHECK_EE_USAGE : 0,
+# else
+ 0,
+# endif
+ &verify))
+ )
+ goto tlsa_prob;
+ }
+
+ if (verify != 0) /* verification failed */
+ {
+ gnutls_datum_t str;
+ (void) dane_verification_status_print(verify, &str, 0);
+ *errstr = US str.data; /* don't bother to free */
+ goto badcert;
+ }
+
+# ifdef GNUTLS_BROKEN_DANE_VALIDATION
+ /* If a TA-mode TLSA record was used for verification we must additionally
+ verify the cert name (but not the CA chain). For EE-mode, skip it. */
+
+ if (usage & (1 << DANESSL_USAGE_DANE_EE))
+# endif
+ {
+ state->peer_dane_verified = state->peer_cert_verified = TRUE;
+ goto goodcert;
+ }
+# ifdef GNUTLS_BROKEN_DANE_VALIDATION
+ /* Assume that the name on the A-record is the one that should be matching
+ the cert. An alternate view is that the domain part of the email address
+ is also permissible. */
+
+ if (gnutls_x509_crt_check_hostname(state->tlsp->peercert,
+ CS state->host->name))
+ {
+ state->peer_dane_verified = state->peer_cert_verified = TRUE;
+ goto goodcert;
+ }
+# endif
+ }
+#endif /*SUPPORT_DANE*/
+
+ rc = gnutls_certificate_verify_peers2(state->session, &verify);
+ }
+
+/* Handle the result of verification. INVALID is set if any others are. */
+
+if (rc < 0 || verify & (GNUTLS_CERT_INVALID|GNUTLS_CERT_REVOKED))
+ {
+ state->peer_cert_verified = FALSE;
+ if (!*errstr)
+ {
+#ifdef GNUTLS_CERT_VFY_STATUS_PRINT
+ DEBUG(D_tls)
+ {
+ gnutls_datum_t txt;
+
+ if (gnutls_certificate_verification_status_print(verify,
+ gnutls_certificate_type_get(state->session), &txt, 0)
+ == GNUTLS_E_SUCCESS)
+ {
+ debug_printf("%s\n", txt.data);
+ gnutls_free(txt.data);
+ }
+ }
+#endif
+ *errstr = verify & GNUTLS_CERT_REVOKED
+ ? US"certificate revoked" : US"certificate invalid";
+ }
+
+ DEBUG(D_tls)
+ debug_printf("TLS certificate verification failed (%s): peerdn=\"%s\"\n",
+ *errstr, state->peerdn ? state->peerdn : US"<unset>");
+
+ if (state->verify_requirement >= VERIFY_REQUIRED)
+ goto badcert;
+ DEBUG(D_tls)
+ debug_printf("TLS verify failure overridden (host in tls_try_verify_hosts)\n");
+ }
+
+else
+ {
+ /* Client side, check the server's certificate name versus the name on the
+ A-record for the connection we made. What to do for server side - what name
+ to use for client? We document that there is no such checking for server
+ side. */
+
+ if ( state->exp_tls_verify_cert_hostnames
+ && !gnutls_x509_crt_check_hostname(state->tlsp->peercert,
+ CS state->exp_tls_verify_cert_hostnames)
+ )
+ {
+ DEBUG(D_tls)
+ debug_printf("TLS certificate verification failed: cert name mismatch\n");
+ if (state->verify_requirement >= VERIFY_REQUIRED)
+ goto badcert;
+ return TRUE;
+ }
+
+ state->peer_cert_verified = TRUE;
+ DEBUG(D_tls) debug_printf("TLS certificate verified: peerdn=\"%s\"\n",
+ state->peerdn ? state->peerdn : US"<unset>");
+ }
+
+goodcert:
+ state->tlsp->peerdn = state->peerdn;
+ return TRUE;
+
+#ifdef SUPPORT_DANE
+tlsa_prob:
+ *errstr = string_sprintf("TLSA record problem: %s",
+ rc == DANE_E_REQUESTED_DATA_NOT_AVAILABLE ? "none usable" : dane_strerror(rc));
+#endif
+
+badcert:
+ gnutls_alert_send(state->session, GNUTLS_AL_FATAL, GNUTLS_A_BAD_CERTIFICATE);
+ return FALSE;
+}
+
+
+
+
+/* ------------------------------------------------------------------------ */
+/* 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;
+uschar * dummy_errstr;
+
+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_copy_taint(US sni_name, GET_TAINTED);
+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;
+
+if ((rc = tls_expand_session_files(state, &dummy_errstr)) != 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, &dummy_errstr);
+if (rc != OK) return GNUTLS_E_APPLICATION_ERROR_MIN;
+
+return 0;
+}
+
+
+
+#ifndef DISABLE_EVENT
+/*
+We use this callback to get observability and detail-level control
+for an exim TLS connection (either direction), raising a tls:cert event
+for each cert in the chain presented by the peer. Any event
+can deny verification.
+
+Return 0 for the handshake to continue or non-zero to terminate.
+*/
+
+static int
+verify_cb(gnutls_session_t session)
+{
+const gnutls_datum_t * cert_list;
+unsigned int cert_list_size = 0;
+gnutls_x509_crt_t crt;
+int rc;
+uschar * yield;
+exim_gnutls_state_st * state = gnutls_session_get_ptr(session);
+
+if ((cert_list = gnutls_certificate_get_peers(session, &cert_list_size)))
+ while (cert_list_size--)
+ {
+ if ((rc = import_cert(&cert_list[cert_list_size], &crt)) != GNUTLS_E_SUCCESS)
+ {
+ DEBUG(D_tls) debug_printf("TLS: peer cert problem: depth %d: %s\n",
+ cert_list_size, gnutls_strerror(rc));
+ break;
+ }
+
+ state->tlsp->peercert = crt;
+ if ((yield = event_raise(state->event_action,
+ US"tls:cert", string_sprintf("%d", cert_list_size), &errno)))
+ {
+ log_write(0, LOG_MAIN,
+ "SSL verify denied by event-action: depth=%d: %s",
+ cert_list_size, yield);
+ return 1; /* reject */
+ }
+ state->tlsp->peercert = NULL;
+ }
+
+return 0;
+}
+
+#endif
+
+
+static gstring *
+ddump(gnutls_datum_t * d)
+{
+gstring * g = string_get((d->size+1) * 2);
+uschar * s = d->data;
+for (unsigned i = d->size; i > 0; i--, s++)
+ {
+ g = string_catn(g, US "0123456789abcdef" + (*s >> 4), 1);
+ g = string_catn(g, US "0123456789abcdef" + (*s & 0xf), 1);
+ }
+return g;
+}
+
+static void
+post_handshake_debug(exim_gnutls_state_st * state)
+{
+#ifdef SUPPORT_GNUTLS_SESS_DESC
+debug_printf("%s\n", gnutls_session_get_desc(state->session));
+#endif
+
+#ifdef SUPPORT_GNUTLS_KEYLOG
+# ifdef EXIM_HAVE_TLS1_3
+if (gnutls_protocol_get_version(state->session) < GNUTLS_TLS1_3)
+# else
+if (TRUE)
+# endif
+ {
+ gnutls_datum_t c, s;
+ gstring * gc, * gs;
+ /* For TLS1.2 we only want the client random and the master secret */
+ gnutls_session_get_random(state->session, &c, &s);
+ gnutls_session_get_master_secret(state->session, &s);
+ gc = ddump(&c);
+ gs = ddump(&s);
+ debug_printf("CLIENT_RANDOM %.*s %.*s\n", (int)gc->ptr, gc->s, (int)gs->ptr, gs->s);
+ }
+else
+ debug_printf("To get keying info for TLS1.3 is hard:\n"
+ " Set environment variable SSLKEYLOGFILE to a filename relative to the spool directory,\n"
+ " and make sure it is writable by the Exim runtime user.\n"
+ " Add SSLKEYLOGFILE to keep_environment in the exim config.\n"
+ " Start Exim as root.\n"
+ " If using sudo, add SSLKEYLOGFILE to env_keep in /etc/sudoers\n"
+ " (works for TLS1.2 also, and saves cut-paste into file).\n"
+ " Trying to use add_environment for this will not work\n");
+#endif
+}
+
+
+#ifdef EXIM_HAVE_TLS_RESUME
+static int
+tls_server_ticket_cb(gnutls_session_t sess, u_int htype, unsigned when,
+ unsigned incoming, const gnutls_datum_t * msg)
+{
+DEBUG(D_tls) debug_printf("newticket cb\n");
+tls_in.resumption |= RESUME_CLIENT_REQUESTED;
+return 0;
+}
+
+static void
+tls_server_resume_prehandshake(exim_gnutls_state_st * state)
+{
+/* Should the server offer session resumption? */
+tls_in.resumption = RESUME_SUPPORTED;
+if (verify_check_host(&tls_resumption_hosts) == OK)
+ {
+ int rc;
+ /* GnuTLS appears to not do ticket overlap, but does emit a fresh ticket when
+ an offered resumption is unacceptable. We lose one resumption per ticket
+ lifetime, and sessions cannot be indefinitely re-used. There seems to be no
+ way (3.6.7) of changing the default number of 2 TLS1.3 tickets issued, but at
+ least they go out in a single packet. */
+
+ if (!(rc = gnutls_session_ticket_enable_server(state->session,
+ &server_sessticket_key)))
+ tls_in.resumption |= RESUME_SERVER_TICKET;
+ else
+ DEBUG(D_tls)
+ debug_printf("enabling session tickets: %s\n", US gnutls_strerror(rc));
+
+ /* Try to tell if we see a ticket request */
+ gnutls_handshake_set_hook_function(state->session,
+ GNUTLS_HANDSHAKE_ANY, GNUTLS_HOOK_POST, tls_server_hook_cb);
+ }
+}
+
+static void
+tls_server_resume_posthandshake(exim_gnutls_state_st * state)
+{
+if (gnutls_session_resumption_requested(state->session))
+ {
+ /* This tells us the client sent a full ticket. We use a
+ callback on session-ticket request, elsewhere, to tell
+ if a client asked for a ticket. */
+
+ tls_in.resumption |= RESUME_CLIENT_SUGGESTED;
+ DEBUG(D_tls) debug_printf("client requested resumption\n");
+ }
+if (gnutls_session_is_resumed(state->session))
+ {
+ tls_in.resumption |= RESUME_USED;
+ DEBUG(D_tls) debug_printf("Session resumed\n");
+ }
+}
+#endif /* EXIM_HAVE_TLS_RESUME */
+
+
+#ifdef EXIM_HAVE_ALPN
+/* Expand and convert an Exim list to a gnutls_datum list. False return for fail.
+NULL plist return for silent no-ALPN.
+*/
+
+static BOOL
+tls_alpn_plist(uschar ** tls_alpn, const gnutls_datum_t ** plist, unsigned * plen,
+ uschar ** errstr)
+{
+uschar * exp_alpn;
+
+if (!expand_check(*tls_alpn, US"tls_alpn", &exp_alpn, errstr))
+ return FALSE;
+
+if (!exp_alpn)
+ {
+ DEBUG(D_tls) debug_printf("Setting TLS ALPN forced to fail, not sending\n");
+ *plist = NULL;
+ }
+else
+ {
+ const uschar * list = exp_alpn;
+ int sep = 0;
+ unsigned cnt = 0;
+ gnutls_datum_t * p;
+ uschar * s;
+
+ while (string_nextinlist(&list, &sep, NULL, 0)) cnt++;
+
+ p = store_get(sizeof(gnutls_datum_t) * cnt, exp_alpn);
+ list = exp_alpn;
+ for (int i = 0; s = string_nextinlist(&list, &sep, NULL, 0); i++)
+ { p[i].data = s; p[i].size = Ustrlen(s); }
+ *plist = (*plen = cnt) ? p : NULL;
+ }
+return TRUE;
+}
+
+static void
+tls_server_set_acceptable_alpns(exim_gnutls_state_st * state, uschar ** errstr)
+{
+uschar * local_alpn = string_copy(tls_alpn);
+int rc;
+const gnutls_datum_t * plist;
+unsigned plen;
+
+if (tls_alpn_plist(&local_alpn, &plist, &plen, errstr) && plist)
+ {
+ /* This seems to be only mandatory if the client sends an ALPN extension;
+ not trying ALPN is ok. Need to decide how to support server-side must-alpn. */
+
+ server_seen_alpn = 0;
+ if (!(rc = gnutls_alpn_set_protocols(state->session, plist, plen,
+ GNUTLS_ALPN_MANDATORY)))
+ gnutls_handshake_set_hook_function(state->session,
+ GNUTLS_HANDSHAKE_ANY, GNUTLS_HOOK_POST, tls_server_hook_cb);
+ else
+ DEBUG(D_tls)
+ debug_printf("setting alpn protocols: %s\n", US gnutls_strerror(rc));
+ }
+}
+#endif /* EXIM_HAVE_ALPN */
+
+/* ------------------------------------------------------------------------ */
+/* 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:
+ errstr pointer to error string
+
+Returns: OK on success
+ DEFER for errors before the start of the negotiation
+ FAIL for errors during the negotiation; the server can't
+ continue running.
+*/
+
+int
+tls_server_start(uschar ** errstr)
+{
+int rc;
+exim_gnutls_state_st * state = NULL;
+
+/* Check for previous activation */
+if (tls_in.active.sock >= 0)
+ {
+ tls_error(US"STARTTLS received after TLS started", US "", NULL, errstr);
+ smtp_printf("554 Already in TLS\r\n", FALSE);
+ 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");
+
+ {
+#ifdef MEASURE_TIMING
+ struct timeval t0;
+ gettimeofday(&t0, NULL);
+#endif
+
+ if ((rc = tls_init(NULL, NULL,
+ tls_require_ciphers, &state, &tls_in, errstr)) != OK) return rc;
+
+#ifdef MEASURE_TIMING
+ report_time_since(&t0, US"server tls_init (delta)");
+#endif
+ }
+
+#ifdef EXIM_HAVE_ALPN
+tls_server_set_acceptable_alpns(state, errstr);
+#endif
+
+#ifdef EXIM_HAVE_TLS_RESUME
+tls_server_resume_prehandshake(state);
+#endif
+
+/* 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);
+ }
+
+#ifndef DISABLE_EVENT
+if (event_action)
+ {
+ state->event_action = event_action;
+ gnutls_session_set_ptr(state->session, state);
+ gnutls_certificate_set_verify_function(state->lib_state.x509_cred, verify_cb);
+ }
+#endif
+
+/* 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", FALSE);
+ fflush(smtp_out);
+ }
+
+/* Now negotiate the TLS session. We put our own timer on it, since it seems
+that the GnuTLS library doesn't.
+From 3.1.0 there is gnutls_handshake_set_timeout() - but it requires you
+to set (and clear down afterwards) up a pull-timeout callback function that does
+a select, so we're no better off unless avoiding signals becomes an issue. */
+
+gnutls_transport_set_ptr2(state->session,
+ (gnutls_transport_ptr_t)(long) fileno(smtp_in),
+ (gnutls_transport_ptr_t)(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_CLR(0);
+
+if (rc != GNUTLS_E_SUCCESS)
+ {
+ DEBUG(D_tls) debug_printf(" error %d from gnutls_handshake: %s\n",
+ rc, gnutls_strerror(rc));
+
+ /* 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)
+ {
+ tls_error(US"gnutls_handshake", US"timed out", NULL, errstr);
+#ifndef DISABLE_EVENT
+ (void) event_raise(event_action, US"tls:fail:connect", *errstr, NULL);
+#endif
+ gnutls_db_remove_session(state->session);
+ }
+ else
+ {
+ tls_error_gnu(state, US"gnutls_handshake", rc, errstr);
+#ifndef DISABLE_EVENT
+ (void) event_raise(event_action, US"tls:fail:connect", *errstr, NULL);
+#endif
+ (void) gnutls_alert_send_appropriate(state->session, rc);
+ gnutls_deinit(state->session);
+ millisleep(500);
+ shutdown(state->fd_out, SHUT_WR);
+ for (int i = 1024; fgetc(smtp_in) != EOF && i > 0; ) i--; /* drain skt */
+ (void)fclose(smtp_out);
+ (void)fclose(smtp_in);
+ smtp_out = smtp_in = NULL;
+ }
+
+ return FAIL;
+ }
+
+#ifdef GNUTLS_SFLAGS_EXT_MASTER_SECRET
+if (gnutls_session_get_flags(state->session) & GNUTLS_SFLAGS_EXT_MASTER_SECRET)
+ tls_in.ext_master_secret = TRUE;
+#endif
+
+#ifdef EXIM_HAVE_TLS_RESUME
+tls_server_resume_posthandshake(state);
+#endif
+
+DEBUG(D_tls) post_handshake_debug(state);
+
+#ifdef EXIM_HAVE_ALPN
+if (server_seen_alpn > 0)
+ {
+ DEBUG(D_tls)
+ { /* The client offered ALPN. See what was negotiated. */
+ gnutls_datum_t p = {.size = 0};
+ int rc = gnutls_alpn_get_selected_protocol(state->session, &p);
+ if (!rc)
+ debug_printf("ALPN negotiated: %.*s\n", (int)p.size, p.data);
+ else
+ debug_printf("getting alpn protocol: %s\n", US gnutls_strerror(rc));
+
+ }
+ }
+else if (server_seen_alpn == 0)
+ if (verify_check_host(&hosts_require_alpn) == OK)
+ {
+ gnutls_alert_send(state->session, GNUTLS_AL_FATAL, GNUTLS_A_NO_APPLICATION_PROTOCOL);
+ tls_error(US"handshake", US"ALPN required but not negotiated", NULL, errstr);
+ return FAIL;
+ }
+ else
+ DEBUG(D_tls) debug_printf("TLS: no ALPN presented in handshake\n");
+else
+ DEBUG(D_tls) debug_printf("TLS: was not watching for ALPN\n");
+#endif
+
+/* Verify after the fact */
+
+if (!verify_certificate(state, errstr))
+ {
+ if (state->verify_requirement != VERIFY_OPTIONAL)
+ {
+ (void) tls_error(US"certificate verification failed", *errstr, NULL, errstr);
+ return FAIL;
+ }
+ DEBUG(D_tls)
+ debug_printf("TLS: continuing on only because verification was optional, after: %s\n",
+ *errstr);
+ }
+
+/* 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_getbuf = tls_getbuf;
+receive_get_cache = tls_get_cache;
+receive_hasc = tls_hasc;
+receive_ungetc = tls_ungetc;
+receive_feof = tls_feof;
+receive_ferror = tls_ferror;
+
+return OK;
+}
+
+
+
+
+static void
+tls_client_setup_hostname_checks(host_item * host, exim_gnutls_state_st * state,
+ smtp_transport_options_block * ob)
+{
+if (verify_check_given_host(CUSS &ob->tls_verify_cert_hostnames, host) == OK)
+ {
+ state->exp_tls_verify_cert_hostnames =
+#ifdef SUPPORT_I18N
+ string_domain_utf8_to_alabel(host->certname, NULL);
+#else
+ host->certname;
+#endif
+ DEBUG(D_tls)
+ debug_printf("TLS: server cert verification includes hostname: \"%s\"\n",
+ state->exp_tls_verify_cert_hostnames);
+ }
+}
+
+
+
+
+#ifdef SUPPORT_DANE
+/* Given our list of RRs from the TLSA lookup, build a lookup block in
+GnuTLS-DANE's preferred format. Hang it on the state str for later
+use in DANE verification.
+
+We point at the dnsa data not copy it, so it must remain valid until
+after verification is done.*/
+
+static BOOL
+dane_tlsa_load(exim_gnutls_state_st * state, dns_answer * dnsa)
+{
+dns_scan dnss;
+int i;
+const char ** dane_data;
+int * dane_data_len;
+
+i = 1;
+for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)
+ ) if (rr->type == T_TLSA) i++;
+
+dane_data = store_get(i * sizeof(uschar *), GET_UNTAINTED);
+dane_data_len = store_get(i * sizeof(int), GET_UNTAINTED);
+
+i = 0;
+for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)
+ ) if (rr->type == T_TLSA && rr->size > 3)
+ {
+ const uschar * p = rr->data;
+/*XXX need somehow to mark rr and its data as tainted. Doues this mean copying it? */
+ uint8_t usage = p[0], sel = p[1], type = p[2];
+
+ DEBUG(D_tls)
+ debug_printf("TLSA: %d %d %d size %d\n", usage, sel, type, rr->size);
+
+ if ( (usage != DANESSL_USAGE_DANE_TA && usage != DANESSL_USAGE_DANE_EE)
+ || (sel != 0 && sel != 1)
+ )
+ continue;
+ switch(type)
+ {
+ case 0: /* Full: cannot check at present */
+ break;
+ case 1: if (rr->size != 3 + 256/8) continue; /* sha2-256 */
+ break;
+ case 2: if (rr->size != 3 + 512/8) continue; /* sha2-512 */
+ break;
+ default: continue;
+ }
+
+ tls_out.tlsa_usage |= 1<<usage;
+ dane_data[i] = CS p;
+ dane_data_len[i++] = rr->size;
+ }
+
+if (!i) return FALSE;
+
+dane_data[i] = NULL;
+dane_data_len[i] = 0;
+
+state->dane_data = (char * const *)dane_data;
+state->dane_data_len = dane_data_len;
+return TRUE;
+}
+#endif
+
+
+
+#ifdef EXIM_HAVE_TLS_RESUME
+/* On the client, get any stashed session for the given IP from hints db
+and apply it to the ssl-connection for attempted resumption. Although
+there is a gnutls_session_ticket_enable_client() interface it is
+documented as unnecessary (as of 3.6.7) as "session tickets are emabled
+by deafult". There seems to be no way to disable them, so even hosts not
+enabled by the transport option will be sent a ticket request. We will
+however avoid storing and retrieving session information. */
+
+static void
+tls_retrieve_session(tls_support * tlsp, gnutls_session_t session,
+ smtp_connect_args * conn_args, smtp_transport_options_block * ob)
+{
+tlsp->resumption = RESUME_SUPPORTED;
+
+if (!conn_args->have_lbserver)
+ { DEBUG(D_tls) debug_printf("resumption not supported on continued-connection\n"); }
+else if (verify_check_given_host(CUSS &ob->tls_resumption_hosts, conn_args->host) == OK)
+ {
+ dbdata_tls_session * dt;
+ int len, rc;
+ open_db dbblock, * dbm_file;
+
+ tlsp->host_resumable = TRUE;
+ tls_client_resmption_key(tlsp, conn_args, ob);
+
+ tlsp->resumption |= RESUME_CLIENT_REQUESTED;
+ if ((dbm_file = dbfn_open(US"tls", O_RDONLY, &dbblock, FALSE, FALSE)))
+ {
+ /* We'd like to filter the retrieved session for ticket advisory expiry,
+ but 3.6.1 seems to give no access to that */
+
+ if ((dt = dbfn_read_with_length(dbm_file, tlsp->resume_index, &len)))
+ if (!(rc = gnutls_session_set_data(session,
+ CUS dt->session, (size_t)len - sizeof(dbdata_tls_session))))
+ {
+ DEBUG(D_tls) debug_printf("good session\n");
+ tlsp->resumption |= RESUME_CLIENT_SUGGESTED;
+ }
+ else DEBUG(D_tls) debug_printf("setting session resumption data: %s\n",
+ US gnutls_strerror(rc));
+ dbfn_close(dbm_file);
+ }
+ }
+}
+
+
+static void
+tls_save_session(tls_support * tlsp, gnutls_session_t session, const host_item * host)
+{
+/* TLS 1.2 - we get both the callback and the direct posthandshake call,
+but this flag is not set until the second. TLS 1.3 it's the other way about.
+Keep both calls as the session data cannot be extracted before handshake
+completes. */
+
+if (gnutls_session_get_flags(session) & GNUTLS_SFLAGS_SESSION_TICKET)
+ {
+ gnutls_datum_t tkt;
+ int rc;
+
+ DEBUG(D_tls) debug_printf("server offered session ticket\n");
+ tlsp->ticket_received = TRUE;
+ tlsp->resumption |= RESUME_SERVER_TICKET;
+
+ if (tlsp->host_resumable)
+ if (!(rc = gnutls_session_get_data2(session, &tkt)))
+ {
+ open_db dbblock, * dbm_file;
+ int dlen = sizeof(dbdata_tls_session) + tkt.size;
+ dbdata_tls_session * dt = store_get(dlen, GET_TAINTED);
+
+ DEBUG(D_tls) debug_printf("session data size %u\n", (unsigned)tkt.size);
+ memcpy(dt->session, tkt.data, tkt.size);
+ gnutls_free(tkt.data);
+
+ if ((dbm_file = dbfn_open(US"tls", O_RDWR, &dbblock, FALSE, FALSE)))
+ {
+ /* key for the db is the IP */
+ dbfn_write(dbm_file, tlsp->resume_index, dt, dlen);
+ dbfn_close(dbm_file);
+
+ DEBUG(D_tls)
+ debug_printf("wrote session db (len %u)\n", (unsigned)dlen);
+ }
+ }
+ else DEBUG(D_tls)
+ debug_printf("extract session data: %s\n", US gnutls_strerror(rc));
+ }
+}
+
+
+/* With a TLS1.3 session, the ticket(s) are not seen until
+the first data read is attempted. And there's often two of them.
+Pick them up with this callback. We are also called for 1.2
+but we do nothing.
+*/
+static int
+tls_client_ticket_cb(gnutls_session_t sess, u_int htype, unsigned when,
+ unsigned incoming, const gnutls_datum_t * msg)
+{
+exim_gnutls_state_st * state = gnutls_session_get_ptr(sess);
+tls_support * tlsp = state->tlsp;
+
+DEBUG(D_tls) debug_printf("newticket cb\n");
+
+if (!tlsp->ticket_received)
+ tls_save_session(tlsp, sess, state->host);
+return 0;
+}
+
+
+static void
+tls_client_resume_prehandshake(exim_gnutls_state_st * state,
+ tls_support * tlsp, smtp_connect_args * conn_args,
+ smtp_transport_options_block * ob)
+{
+gnutls_session_set_ptr(state->session, state);
+gnutls_handshake_set_hook_function(state->session,
+ GNUTLS_HANDSHAKE_NEW_SESSION_TICKET, GNUTLS_HOOK_POST, tls_client_ticket_cb);
+
+tls_retrieve_session(tlsp, state->session, conn_args, ob);
+}
+
+static void
+tls_client_resume_posthandshake(exim_gnutls_state_st * state,
+ tls_support * tlsp, host_item * host)
+{
+if (gnutls_session_is_resumed(state->session))
+ {
+ DEBUG(D_tls) debug_printf("Session resumed\n");
+ tlsp->resumption |= RESUME_USED;
+ }
+
+tls_save_session(tlsp, state->session, host);
+}
+#endif /* !DISABLE_TLS_RESUME */
+
+
+/*************************************************
+* Start a TLS session in a client *
+*************************************************/
+
+/* Called from the smtp transport after STARTTLS has been accepted.
+
+Arguments:
+ cctx connection context
+ conn_args connection details
+ cookie datum for randomness (not used)
+ tlsp record details of channel configuration here; must be non-NULL
+ errstr error string pointer
+
+Returns: TRUE for success with TLS session context set in smtp context,
+ FALSE on error
+*/
+
+BOOL
+tls_client_start(client_conn_ctx * cctx, smtp_connect_args * conn_args,
+ void * cookie ARG_UNUSED,
+ tls_support * tlsp, uschar ** errstr)
+{
+host_item * host = conn_args->host; /* for msgs and option-tests */
+transport_instance * tb = conn_args->tblock; /* always smtp or NULL */
+smtp_transport_options_block * ob = tb
+ ? (smtp_transport_options_block *)tb->options_block
+ : &smtp_transport_option_defaults;
+int rc;
+exim_gnutls_state_st * state = NULL;
+uschar * cipher_list = NULL;
+
+#ifndef DISABLE_OCSP
+BOOL require_ocsp =
+ verify_check_given_host(CUSS &ob->hosts_require_ocsp, host) == OK;
+BOOL request_ocsp = require_ocsp ? TRUE
+ : verify_check_given_host(CUSS &ob->hosts_request_ocsp, host) == OK;
+#endif
+
+DEBUG(D_tls) debug_printf("initialising GnuTLS as a client on fd %d\n", cctx->sock);
+
+#ifdef SUPPORT_DANE
+/* If dane is flagged, have either request or require dane for this host, and
+a TLSA record found. Therefore, dane verify required. Which implies cert must
+be requested and supplied, dane verify must pass, and cert verify irrelevant
+(incl. hostnames), and (caller handled) require_tls and sni=$domain */
+
+if (conn_args->dane && ob->dane_require_tls_ciphers)
+ {
+ /* not using Expand_check_tlsvar because not yet in state */
+ if (!expand_check(ob->dane_require_tls_ciphers, US"dane_require_tls_ciphers",
+ &cipher_list, errstr))
+ return FALSE;
+ cipher_list = cipher_list && *cipher_list
+ ? ob->dane_require_tls_ciphers : ob->tls_require_ciphers;
+ }
+#endif
+
+if (!cipher_list)
+ cipher_list = ob->tls_require_ciphers;
+
+ {
+#ifdef MEASURE_TIMING
+ struct timeval t0;
+ gettimeofday(&t0, NULL);
+#endif
+
+ if (tls_init(host, ob, cipher_list, &state, tlsp, errstr) != OK)
+ return FALSE;
+
+#ifdef MEASURE_TIMING
+ report_time_since(&t0, US"client tls_init (delta)");
+#endif
+ }
+
+if (ob->tls_alpn)
+#ifdef EXIM_HAVE_ALPN
+ {
+ const gnutls_datum_t * plist;
+ unsigned plen;
+
+ if (!tls_alpn_plist(&ob->tls_alpn, &plist, &plen, errstr))
+ return FALSE;
+ if (plist)
+ if (gnutls_alpn_set_protocols(state->session, plist, plen, 0) != 0)
+ {
+ tls_error(US"alpn init", NULL, state->host, errstr);
+ return FALSE;
+ }
+ else
+ DEBUG(D_tls) debug_printf("Setting TLS ALPN '%s'\n", ob->tls_alpn);
+ }
+#else
+ log_write(0, LOG_MAIN, "ALPN unusable with this GnuTLS library version; ignoring \"%s\"\n",
+ ob->tls_alpn);
+#endif
+
+ {
+ 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 */
+
+#ifdef SUPPORT_DANE
+if (conn_args->dane && dane_tlsa_load(state, &conn_args->tlsa_dnsa))
+ {
+ DEBUG(D_tls)
+ debug_printf("TLS: server certificate DANE required\n");
+ state->verify_requirement = VERIFY_DANE;
+ gnutls_certificate_server_set_request(state->session, GNUTLS_CERT_REQUIRE);
+ }
+else
+#endif
+ if ( ( state->exp_tls_verify_certificates
+ && !ob->tls_verify_hosts
+ && (!ob->tls_try_verify_hosts || !*ob->tls_try_verify_hosts)
+ )
+ || verify_check_given_host(CUSS &ob->tls_verify_hosts, host) == OK
+ )
+ {
+ tls_client_setup_hostname_checks(host, state, ob);
+ 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_given_host(CUSS &ob->tls_try_verify_hosts, host) == OK)
+ {
+ tls_client_setup_hostname_checks(host, state, ob);
+ 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)
+ {
+ tls_error_gnu(state, US"cert-status-req", rc, errstr);
+ return FALSE;
+ }
+ tlsp->ocsp = OCSP_NOT_RESP;
+ }
+#endif
+
+#ifdef EXIM_HAVE_TLS_RESUME
+tls_client_resume_prehandshake(state, tlsp, conn_args, ob);
+#endif
+
+#ifndef DISABLE_EVENT
+if (tb && tb->event_action)
+ {
+ state->event_action = tb->event_action;
+ gnutls_session_set_ptr(state->session, state);
+ gnutls_certificate_set_verify_function(state->lib_state.x509_cred, verify_cb);
+ }
+#endif
+
+gnutls_transport_set_ptr(state->session, (gnutls_transport_ptr_t)(long) cctx->sock);
+state->fd_in = cctx->sock;
+state->fd_out = cctx->sock;
+
+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_CLR(0);
+
+if (rc != GNUTLS_E_SUCCESS)
+ {
+ if (sigalrm_seen)
+ {
+ gnutls_alert_send(state->session, GNUTLS_AL_FATAL, GNUTLS_A_USER_CANCELED);
+ tls_error(US"gnutls_handshake", US"timed out", state->host, errstr);
+ }
+ else
+ tls_error_gnu(state, US"gnutls_handshake", rc, errstr);
+ return FALSE;
+ }
+
+DEBUG(D_tls) post_handshake_debug(state);
+
+/* Verify late */
+
+if (!verify_certificate(state, errstr))
+ {
+ tls_error(US"certificate verification failed", *errstr, state->host, errstr);
+ return FALSE;
+ }
+
+#ifdef GNUTLS_SFLAGS_EXT_MASTER_SECRET
+if (gnutls_session_get_flags(state->session) & GNUTLS_SFLAGS_EXT_MASTER_SECRET)
+ tlsp->ext_master_secret = TRUE;
+#endif
+
+#ifndef DISABLE_OCSP
+if (request_ocsp)
+ {
+ DEBUG(D_tls)
+ {
+ gnutls_datum_t stapling;
+ gnutls_ocsp_resp_t resp;
+ gnutls_datum_t printed;
+ unsigned idx = 0;
+
+ for (;
+# ifdef GNUTLS_OCSP_STATUS_REQUEST_GET2
+ (rc = gnutls_ocsp_status_request_get2(state->session, idx, &stapling)) == 0;
+#else
+ (rc = gnutls_ocsp_status_request_get(state->session, &stapling)) == 0;
+#endif
+ idx++)
+ if ( (rc= gnutls_ocsp_resp_init(&resp)) == 0
+ && (rc= gnutls_ocsp_resp_import(resp, &stapling)) == 0
+ && (rc= gnutls_ocsp_resp_print(resp, GNUTLS_OCSP_PRINT_COMPACT, &printed)) == 0
+ )
+ {
+ debug_printf("%.4096s", printed.data);
+ gnutls_free(printed.data);
+ }
+ else
+ (void) tls_error_gnu(state, US"ocsp decode", rc, errstr);
+ if (idx == 0 && rc)
+ (void) tls_error_gnu(state, US"ocsp decode", rc, errstr);
+ }
+
+ if (gnutls_ocsp_status_request_is_checked(state->session, 0) == 0)
+ {
+ tlsp->ocsp = OCSP_FAILED;
+ tls_error(US"certificate status check failed", NULL, state->host, errstr);
+ if (require_ocsp)
+ return FALSE;
+ }
+ else
+ {
+ DEBUG(D_tls) debug_printf("Passed OCSP checking\n");
+ tlsp->ocsp = OCSP_VFIED;
+ }
+ }
+#endif
+
+#ifdef EXIM_HAVE_TLS_RESUME
+tls_client_resume_posthandshake(state, tlsp, host);
+#endif
+
+#ifdef EXIM_HAVE_ALPN
+if (ob->tls_alpn) /* We requested. See what was negotiated. */
+ {
+ gnutls_datum_t p = {.size = 0};
+
+ if (gnutls_alpn_get_selected_protocol(state->session, &p) == 0)
+ { DEBUG(D_tls) debug_printf("ALPN negotiated: '%.*s'\n", (int)p.size, p.data); }
+ else if (verify_check_given_host(CUSS &ob->hosts_require_alpn, host) == OK)
+ {
+ gnutls_alert_send(state->session, GNUTLS_AL_FATAL, GNUTLS_A_NO_APPLICATION_PROTOCOL);
+ tls_error(US"handshake", US"ALPN required but not negotiated", state->host, errstr);
+ return FALSE;
+ }
+ else
+ DEBUG(D_tls) debug_printf("No ALPN negotiated");
+ }
+#endif
+
+/* Sets various Exim expansion variables; may need to adjust for ACL callouts */
+
+extract_exim_vars_from_tls_state(state);
+
+cctx->tls_ctx = state;
+return TRUE;
+}
+
+
+
+
+/*
+Arguments:
+ ct_ctx client TLS context pointer, or NULL for the one global server context
+*/
+
+void
+tls_shutdown_wr(void * ct_ctx)
+{
+exim_gnutls_state_st * state = ct_ctx ? ct_ctx : &state_server;
+tls_support * tlsp = state->tlsp;
+
+if (!tlsp || tlsp->active.sock < 0) return; /* TLS was not active */
+
+tls_write(ct_ctx, NULL, 0, FALSE); /* flush write buffer */
+
+HDEBUG(D_transport|D_tls|D_acl|D_v) debug_printf_indent(" SMTP(TLS shutdown)>>\n");
+gnutls_bye(state->session, GNUTLS_SHUT_WR);
+}
+
+/*************************************************
+* 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:
+ ct_ctx client context pointer, or NULL for the one global server context
+ do_shutdown 0 no data-flush or TLS close-alert
+ 1 if TLS close-alert is to be sent,
+ 2 if also response to be waited for (2s timeout)
+
+Returns: nothing
+*/
+
+void
+tls_close(void * ct_ctx, int do_shutdown)
+{
+exim_gnutls_state_st * state = ct_ctx ? ct_ctx : &state_server;
+tls_support * tlsp = state->tlsp;
+
+if (!tlsp || tlsp->active.sock < 0) return; /* TLS was not active */
+
+if (do_shutdown)
+ {
+ DEBUG(D_tls) debug_printf("tls_close(): shutting down TLS%s\n",
+ do_shutdown > TLS_SHUTDOWN_NOWAIT ? " (with response-wait)" : "");
+
+ tls_write(ct_ctx, NULL, 0, FALSE); /* flush write buffer */
+
+#ifdef EXIM_TCP_CORK
+ if (do_shutdown == TLS_SHUTDOWN_WAIT)
+ (void) setsockopt(tlsp->active.sock, IPPROTO_TCP, EXIM_TCP_CORK, US &off, sizeof(off));
+#endif
+
+ /* The library seems to have no way to only wait for a peer's
+ shutdown, so handle the same as TLS_SHUTDOWN_WAIT */
+
+ ALARM(2);
+ gnutls_bye(state->session,
+ do_shutdown > TLS_SHUTDOWN_NOWAIT ? GNUTLS_SHUT_RDWR : GNUTLS_SHUT_WR);
+ ALARM_CLR(0);
+ }
+
+if (!ct_ctx) /* server */
+ {
+ receive_getc = smtp_getc;
+ receive_getbuf = smtp_getbuf;
+ receive_get_cache = smtp_get_cache;
+ receive_hasc = smtp_hasc;
+ receive_ungetc = smtp_ungetc;
+ receive_feof = smtp_feof;
+ receive_ferror = smtp_ferror;
+ }
+
+gnutls_deinit(state->session);
+tlsp->active.sock = -1;
+tlsp->active.tls_ctx = NULL;
+/* Leave bits, peercert, cipher, peerdn, certificate_verified set, for logging */
+tlsp->channelbinding = NULL;
+
+
+if (state->xfer_buffer) store_free(state->xfer_buffer);
+}
+
+
+
+
+static BOOL
+tls_refill(unsigned lim)
+{
+exim_gnutls_state_st * state = &state_server;
+ssize_t inbytes;
+
+DEBUG(D_tls) debug_printf("Calling gnutls_record_recv(session=%p, buffer=%p, buffersize=%u)\n",
+ state->session, state->xfer_buffer, ssl_xfer_buffer_size);
+
+sigalrm_seen = FALSE;
+if (smtp_receive_timeout > 0) ALARM(smtp_receive_timeout);
+
+errno = 0;
+do
+ inbytes = gnutls_record_recv(state->session, state->xfer_buffer,
+ MIN(ssl_xfer_buffer_size, lim));
+while (inbytes == GNUTLS_E_AGAIN);
+
+if (smtp_receive_timeout > 0) ALARM_CLR(0);
+
+if (had_command_timeout) /* set by signal handler */
+ smtp_command_timeout_exit(); /* does not return */
+if (had_command_sigterm)
+ smtp_command_sigterm_exit();
+if (had_data_timeout)
+ smtp_data_timeout_exit();
+if (had_data_sigint)
+ smtp_data_sigint_exit();
+
+/* Timeouts do not get this far. 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 (sigalrm_seen)
+ {
+ DEBUG(D_tls) debug_printf("Got tls read timeout\n");
+ state->xfer_error = TRUE;
+ return FALSE;
+ }
+
+else if (inbytes == 0)
+ {
+ DEBUG(D_tls) debug_printf("Got TLS_EOF\n");
+ tls_close(NULL, TLS_NO_SHUTDOWN);
+ return FALSE;
+ }
+
+/* Handle genuine errors */
+
+else if (inbytes < 0)
+ {
+ DEBUG(D_tls) debug_printf("%s: err from gnutls_record_recv\n", __FUNCTION__);
+ record_io_error(state, (int) inbytes, US"recv", NULL);
+ state->xfer_error = TRUE;
+ return FALSE;
+ }
+#ifndef DISABLE_DKIM
+dkim_exim_verify_feed(state->xfer_buffer, inbytes);
+#endif
+state->xfer_buffer_hwm = (int) inbytes;
+state->xfer_buffer_lwm = 0;
+return TRUE;
+}
+
+/*************************************************
+* 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: lim Maximum amount to read/buffer
+Returns: the next character or EOF
+*/
+
+int
+tls_getc(unsigned lim)
+{
+exim_gnutls_state_st * state = &state_server;
+
+if (state->xfer_buffer_lwm >= state->xfer_buffer_hwm)
+ if (!tls_refill(lim))
+ return state->xfer_error ? EOF : smtp_getc(lim);
+
+/* Something in the buffer; return next uschar */
+
+return state->xfer_buffer[state->xfer_buffer_lwm++];
+}
+
+BOOL
+tls_hasc(void)
+{
+exim_gnutls_state_st * state = &state_server;
+return state->xfer_buffer_lwm < state->xfer_buffer_hwm;
+}
+
+uschar *
+tls_getbuf(unsigned * len)
+{
+exim_gnutls_state_st * state = &state_server;
+unsigned size;
+uschar * buf;
+
+if (state->xfer_buffer_lwm >= state->xfer_buffer_hwm)
+ if (!tls_refill(*len))
+ {
+ if (!state->xfer_error) return smtp_getbuf(len);
+ *len = 0;
+ return NULL;
+ }
+
+if ((size = state->xfer_buffer_hwm - state->xfer_buffer_lwm) > *len)
+ size = *len;
+buf = &state->xfer_buffer[state->xfer_buffer_lwm];
+state->xfer_buffer_lwm += size;
+*len = size;
+return buf;
+}
+
+
+/* Get up to the given number of bytes from any cached data, and feed to dkim. */
+void
+tls_get_cache(unsigned lim)
+{
+#ifndef DISABLE_DKIM
+exim_gnutls_state_st * state = &state_server;
+int n = state->xfer_buffer_hwm - state->xfer_buffer_lwm;
+if (n > lim)
+ n = lim;
+if (n > 0)
+ dkim_exim_verify_feed(state->xfer_buffer+state->xfer_buffer_lwm, n);
+#endif
+}
+
+
+BOOL
+tls_could_getc(void)
+{
+return state_server.xfer_buffer_lwm < state_server.xfer_buffer_hwm
+ || gnutls_record_check_pending(state_server.session) > 0;
+}
+
+
+/*************************************************
+* 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:
+ ct_ctx client context pointer, or NULL for the one global server context
+ buff buffer of data
+ len size of buffer
+
+Returns: the number of bytes read
+ -1 after a failed read, including EOF
+*/
+
+int
+tls_read(void * ct_ctx, uschar *buff, size_t len)
+{
+exim_gnutls_state_st * state = ct_ctx ? ct_ctx : &state_server;
+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(session=%p, buffer=%p, len=" SIZE_T_FMT ")\n",
+ state->session, buff, len);
+
+errno = 0;
+do
+ inbytes = gnutls_record_recv(state->session, buff, len);
+while (inbytes == GNUTLS_E_AGAIN);
+
+if (inbytes > 0) return inbytes;
+if (inbytes == 0)
+ {
+ DEBUG(D_tls) debug_printf("Got TLS_EOF\n");
+ }
+else
+ {
+ DEBUG(D_tls) debug_printf("%s: err from gnutls_record_recv\n", __FUNCTION__);
+ record_io_error(state, (int)inbytes, US"recv", NULL);
+ }
+
+return -1;
+}
+
+
+
+
+/*************************************************
+* Write bytes down TLS channel *
+*************************************************/
+
+/*
+Arguments:
+ ct_ctx client context pointer, or NULL for the one global server context
+ buff buffer of data
+ len number of bytes
+ more more data expected soon
+
+Calling with len zero and more unset will flush buffered writes. The buff
+argument can be null for that case.
+
+Returns: the number of bytes after a successful write,
+ -1 after a failed write
+*/
+
+int
+tls_write(void * ct_ctx, const uschar * buff, size_t len, BOOL more)
+{
+ssize_t outbytes;
+size_t left = len;
+exim_gnutls_state_st * state = ct_ctx ? ct_ctx : &state_server;
+
+#ifdef SUPPORT_CORK
+if (more && !state->corked)
+ {
+ DEBUG(D_tls) debug_printf("gnutls_record_cork(session=%p)\n", state->session);
+ gnutls_record_cork(state->session);
+ state->corked = TRUE;
+ }
+#endif
+
+DEBUG(D_tls) debug_printf("%s(%p, " SIZE_T_FMT "%s)\n", __FUNCTION__,
+ buff, left, more ? ", more" : "");
+
+while (left > 0)
+ {
+ DEBUG(D_tls) debug_printf("gnutls_record_send(session=%p, buffer=%p, left=" SIZE_T_FMT ")\n",
+ state->session, buff, left);
+
+ errno = 0;
+ do
+ outbytes = gnutls_record_send(state->session, buff, left);
+ while (outbytes == GNUTLS_E_AGAIN);
+
+ DEBUG(D_tls) debug_printf("outbytes=" SSIZE_T_FMT "\n", outbytes);
+
+ if (outbytes < 0)
+ {
+#ifdef GNUTLS_E_PREMATURE_TERMINATION
+ if ( outbytes == GNUTLS_E_PREMATURE_TERMINATION && errno == ECONNRESET
+ && !ct_ctx && f.smtp_in_quit
+ )
+ { /* Outlook, dammit */
+ if (LOGGING(protocol_detail))
+ log_write(0, LOG_MAIN, "[%s] after QUIT, client reset TCP before"
+ " SMTP response and TLS close\n", sender_host_address);
+ else
+ DEBUG(D_tls) debug_printf("[%s] SSL_write: after QUIT,"
+ " client reset TCP before TLS close\n", sender_host_address);
+ }
+ else
+#endif
+ {
+ DEBUG(D_tls) debug_printf("%s: gnutls_record_send err\n", __FUNCTION__);
+ 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;
+ }
+
+#ifdef SUPPORT_CORK
+if (!more && state->corked)
+ {
+ DEBUG(D_tls) debug_printf("gnutls_record_uncork(session=%p)\n", state->session);
+ do
+ /* We can't use GNUTLS_RECORD_WAIT here, as it retries on
+ GNUTLS_E_AGAIN || GNUTLS_E_INTR, which would break our timeout set by alarm().
+ The GNUTLS_E_AGAIN should not happen ever, as our sockets are blocking anyway.
+ But who knows. (That all relies on the fact that GNUTLS_E_INTR and GNUTLS_E_AGAIN
+ match the EINTR and EAGAIN errno values.) */
+ outbytes = gnutls_record_uncork(state->session, 0);
+ while (outbytes == GNUTLS_E_AGAIN);
+
+ if (outbytes < 0)
+ {
+ record_io_error(state, len, US"uncork", NULL);
+ return -1;
+ }
+
+ state->corked = FALSE;
+ }
+#endif
+
+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 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 (uschar * p = smallbuf; needed_len; --needed_len, ++p)
+ r = r * 256 + *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;
+uschar * dummy_errstr;
+
+#ifdef GNUTLS_AUTO_GLOBAL_INIT
+# define validate_check_rc(Label) do { \
+ if (rc != GNUTLS_E_SUCCESS) { if (exim_gnutls_base_init_done) \
+ return string_sprintf("%s failed: %s", (Label), gnutls_strerror(rc)); } } while (0)
+# define return_deinit(Label) do { return (Label); } while (0)
+#else
+# 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)
+#endif
+
+if (exim_gnutls_base_init_done)
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "already initialised GnuTLS, Exim developer bug");
+
+#if defined(HAVE_GNUTLS_PKCS11) && !defined(GNUTLS_AUTO_PKCS11_MANUAL)
+if (!gnutls_allow_auto_pkcs11)
+ {
+ rc = gnutls_pkcs11_init(GNUTLS_PKCS11_FLAG_MANUAL, NULL);
+ validate_check_rc(US"gnutls_pkcs11_init");
+ }
+#endif
+#ifndef GNUTLS_AUTO_GLOBAL_INIT
+rc = gnutls_global_init();
+validate_check_rc(US"gnutls_global_init()");
+#endif
+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,
+ &dummy_errstr))
+ 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, (long)(errpos - CS expciphers), errpos));
+
+#undef return_deinit
+#undef validate_check_rc
+#ifndef GNUTLS_AUTO_GLOBAL_INIT
+gnutls_global_deinit();
+#endif
+
+return NULL;
+}
+
+
+
+
+/*************************************************
+* Report the library versions. *
+*************************************************/
+
+/* See a description in tls-openssl.c for an explanation of why this exists.
+
+Arguments: string to append to
+Returns: string
+*/
+
+gstring *
+tls_version_report(gstring * g)
+{
+return string_fmt_append(g,
+ "Library version: GnuTLS: Compile: %s\n"
+ " Runtime: %s\n",
+ LIBGNUTLS_VERSION,
+ gnutls_check_version(NULL));
+}
+
+#endif /*!MACRO_PREDEF*/
+/* 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
index 0000000..2b8a4e6
--- /dev/null
+++ b/src/tls-openssl.c
@@ -0,0 +1,4894 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2019 */
+/* 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 OPENSSL_NO_ECDH
+# include <openssl/ec.h>
+#endif
+#ifndef DISABLE_OCSP
+# include <openssl/ocsp.h>
+#endif
+#ifdef SUPPORT_DANE
+# include "danessl.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 OPENSSL_VERSION_NUMBER >= 0x00908000L
+# define EXIM_HAVE_RSA_GENKEY_EX
+#endif
+#if OPENSSL_VERSION_NUMBER >= 0x10100000L
+# define EXIM_HAVE_OCSP_RESP_COUNT
+# define OPENSSL_AUTO_SHA256
+#else
+# define EXIM_HAVE_EPHEM_RSA_KEX
+# define EXIM_HAVE_RAND_PSEUDO
+#endif
+#if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256)
+# define EXIM_HAVE_SHA256
+#endif
+
+/* X509_check_host provides sane certificate hostname checking, but was added
+to OpenSSL late, after other projects forked off the code-base. So in
+addition to guarding against the base version number, beware that LibreSSL
+does not (at this time) support this function.
+
+If LibreSSL gains a different API, perhaps via libtls, then we'll probably
+opt to disentangle and ask a LibreSSL user to provide glue for a third
+crypto provider for libtls instead of continuing to tie the OpenSSL glue
+into even twistier knots. If LibreSSL gains the same API, we can just
+change this guard and punt the issue for a while longer. */
+
+#ifndef LIBRESSL_VERSION_NUMBER
+# if OPENSSL_VERSION_NUMBER >= 0x010100000L
+# define EXIM_HAVE_OPENSSL_CHECKHOST
+# define EXIM_HAVE_OPENSSL_DH_BITS
+# define EXIM_HAVE_OPENSSL_TLS_METHOD
+# define EXIM_HAVE_OPENSSL_KEYLOG
+# define EXIM_HAVE_OPENSSL_CIPHER_GET_ID
+# define EXIM_HAVE_SESSION_TICKET
+# define EXIM_HAVE_OPESSL_TRACE
+# define EXIM_HAVE_OPESSL_GET0_SERIAL
+# ifndef DISABLE_OCSP
+# define EXIM_HAVE_OCSP
+# endif
+# define EXIM_HAVE_ALPN /* fail ret from hshake-cb is ignored by LibreSSL */
+# else
+# define EXIM_NEED_OPENSSL_INIT
+# endif
+# if OPENSSL_VERSION_NUMBER >= 0x010000000L \
+ && (OPENSSL_VERSION_NUMBER & 0x0000ff000L) >= 0x000002000L
+# define EXIM_HAVE_OPENSSL_CHECKHOST
+# endif
+#endif
+
+#if LIBRESSL_VERSION_NUMBER >= 0x3040000fL
+# define EXIM_HAVE_OPENSSL_CIPHER_GET_ID
+#endif
+
+#if !defined(LIBRESSL_VERSION_NUMBER) \
+ || LIBRESSL_VERSION_NUMBER >= 0x20010000L
+# if !defined(OPENSSL_NO_ECDH)
+# if OPENSSL_VERSION_NUMBER >= 0x0090800fL
+# define EXIM_HAVE_ECDH
+# endif
+# if OPENSSL_VERSION_NUMBER >= 0x10002000L
+# define EXIM_HAVE_OPENSSL_EC_NIST2NID
+# endif
+# endif
+#endif
+
+#ifndef LIBRESSL_VERSION_NUMBER
+# if OPENSSL_VERSION_NUMBER >= 0x010101000L
+# define OPENSSL_HAVE_KEYLOG_CB
+# define OPENSSL_HAVE_NUM_TICKETS
+# define EXIM_HAVE_OPENSSL_CIPHER_STD_NAME
+# else
+# define OPENSSL_BAD_SRVR_OURCERT
+# endif
+#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
+
+#ifndef DISABLE_TLS_RESUME
+# if OPENSSL_VERSION_NUMBER < 0x0101010L
+# error OpenSSL version too old for session-resumption
+# endif
+#endif
+
+#ifdef EXIM_HAVE_OPENSSL_CHECKHOST
+# include <openssl/x509v3.h>
+#endif
+
+#ifndef EXIM_HAVE_OPENSSL_CIPHER_STD_NAME
+# ifndef EXIM_HAVE_OPENSSL_CIPHER_GET_ID
+# define SSL_CIPHER_get_id(c) (c->id)
+# endif
+# ifndef MACRO_PREDEF
+# include "tls-cipher-stdname.c"
+# endif
+#endif
+
+/*************************************************
+* OpenSSL option parse *
+*************************************************/
+
+typedef struct exim_openssl_option {
+ uschar *name;
+ long value;
+} exim_openssl_option;
+/* 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.1.1c <==
+
+XXX could we autobuild this list, as with predefined-macros?
+Seems just parsing ssl.h for SSL_OP_.* would be enough (except to exclude DTLS).
+Also allow a numeric literal?
+*/
+static exim_openssl_option exim_openssl_options[] = {
+/* KEEP SORTED ALPHABETICALLY! */
+#ifdef SSL_OP_ALL
+ { US"all", (long) SSL_OP_ALL },
+#endif
+#ifdef SSL_OP_ALLOW_NO_DHE_KEX
+ { US"allow_no_dhe_kex", SSL_OP_ALLOW_NO_DHE_KEX },
+#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_CRYPTOPRO_TLSEXT_BUG
+ { US"cryptopro_tlsext_bug", SSL_OP_CRYPTOPRO_TLSEXT_BUG },
+#endif
+#ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
+ { US"dont_insert_empty_fragments", SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS },
+#endif
+#ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT
+ { US"enable_middlebox_compat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT },
+#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_ANTI_REPLAY
+ { US"no_anti_replay", SSL_OP_NO_ANTI_REPLAY },
+#endif
+#ifdef SSL_OP_NO_COMPRESSION
+ { US"no_compression", SSL_OP_NO_COMPRESSION },
+#endif
+#ifdef SSL_OP_NO_ENCRYPT_THEN_MAC
+ { US"no_encrypt_then_mac", SSL_OP_NO_ENCRYPT_THEN_MAC },
+#endif
+#ifdef SSL_OP_NO_RENEGOTIATION
+ { US"no_renegotiation", SSL_OP_NO_RENEGOTIATION },
+#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 OPENSSL_VERSION_NUMBER < 0x30000000L
+# 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
+# define NO_SSL_OP_NO_TLSv1_1
+# endif
+# endif
+# ifndef NO_SSL_OP_NO_TLSv1_1
+ { 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_NO_TLSv1_3
+ { US"no_tlsv1_3", SSL_OP_NO_TLSv1_3 },
+#endif
+#ifdef SSL_OP_PRIORITIZE_CHACHA
+ { US"prioritize_chacha", SSL_OP_PRIORITIZE_CHACHA },
+#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
+#ifdef SSL_OP_TLSEXT_PADDING
+ { US"tlsext_padding", SSL_OP_TLSEXT_PADDING },
+#endif
+};
+
+#ifndef MACRO_PREDEF
+static int exim_openssl_options_size = nelem(exim_openssl_options);
+static long init_options = 0;
+#endif
+
+#ifdef MACRO_PREDEF
+void
+options_tls(void)
+{
+uschar buf[64];
+
+for (struct exim_openssl_option * o = exim_openssl_options;
+ o < exim_openssl_options + nelem(exim_openssl_options); o++)
+ {
+ /* Trailing X is workaround for problem with _OPT_OPENSSL_NO_TLSV1
+ being a ".ifdef _OPT_OPENSSL_NO_TLSV1_3" match */
+
+ spf(buf, sizeof(buf), US"_OPT_OPENSSL_%T_X", o->name);
+ builtin_macro_create(buf);
+ }
+
+# ifndef DISABLE_TLS_RESUME
+builtin_macro_create_var(US"_RESUME_DECODE", RESUME_DECODE_STRING );
+# endif
+# ifdef SSL_OP_NO_TLSv1_3
+builtin_macro_create(US"_HAVE_TLS1_3");
+# endif
+# ifdef OPENSSL_BAD_SRVR_OURCERT
+builtin_macro_create(US"_TLS_BAD_MULTICERT_IN_OURCERT");
+# endif
+# ifdef EXIM_HAVE_OCSP
+builtin_macro_create(US"_HAVE_TLS_OCSP");
+builtin_macro_create(US"_HAVE_TLS_OCSP_LIST");
+# endif
+# ifdef EXIM_HAVE_ALPN
+builtin_macro_create(US"_HAVE_TLS_ALPN");
+# endif
+}
+#else
+
+/******************************************************************************/
+
+/* 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. We may also concurrently want to make TLS connections
+ to utility daemons, so client-contexts are allocated and passed around in call
+ args rather than using a gobal.
+
+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.
+*/
+
+typedef struct {
+ SSL_CTX * ctx;
+ SSL * ssl;
+ gstring * corked;
+} exim_openssl_client_tls_ctx;
+
+
+/* static SSL_CTX *server_ctx = NULL; */
+/* static SSL *server_ssl = NULL; */
+
+#ifdef EXIM_HAVE_OPENSSL_TLSEXT
+static SSL_CTX *server_sni = NULL;
+#endif
+#ifdef EXIM_HAVE_ALPN
+static BOOL server_seen_alpn = FALSE;
+#endif
+
+static char ssl_errstring[256];
+
+static int ssl_session_timeout = 7200; /* Two hours */
+static BOOL client_verify_optional = FALSE;
+static BOOL server_verify_optional = FALSE;
+
+static BOOL reexpand_tls_files_for_sni = FALSE;
+
+
+typedef struct ocsp_resp {
+ struct ocsp_resp * next;
+ OCSP_RESPONSE * resp;
+} ocsp_resplist;
+
+typedef struct exim_openssl_state {
+ exim_tlslib_state lib_state;
+#define lib_ctx libdata0
+#define lib_ssl libdata1
+
+ tls_support * tlsp;
+ uschar * certificate;
+ uschar * privatekey;
+ BOOL is_server;
+#ifndef DISABLE_OCSP
+ STACK_OF(X509) *verify_stack; /* chain for verifying the proof */
+ union {
+ struct {
+ uschar *file;
+ const uschar *file_expanded;
+ ocsp_resplist *olist;
+ } 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;
+ const uschar * verify_cert_hostnames;
+#ifndef DISABLE_EVENT
+ uschar * event_action;
+#endif
+} exim_openssl_state_st;
+
+/* 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. */
+exim_openssl_state_st *client_static_state = NULL; /*XXX should not use static; multiple concurrent clients! */
+exim_openssl_state_st state_server = {.is_server = TRUE};
+
+static int
+setup_certs(SSL_CTX *sctx, uschar *certs, uschar *crl, host_item *host,
+ uschar ** errstr );
+
+/* Callbacks */
+#ifndef DISABLE_OCSP
+static int tls_server_stapling_cb(SSL *s, void *arg);
+#endif
+
+
+
+/* Daemon-called, before every connection, key create/rotate */
+#ifndef DISABLE_TLS_RESUME
+static void tk_init(void);
+static int tls_exdata_idx = -1;
+#endif
+
+static void
+tls_per_lib_daemon_tick(void)
+{
+#ifndef DISABLE_TLS_RESUME
+tk_init();
+#endif
+}
+
+/* Called once at daemon startup */
+
+static void
+tls_per_lib_daemon_init(void)
+{
+tls_daemon_creds_reload();
+}
+
+
+/*************************************************
+* 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
+ errstr pointer to output error message
+
+Returns: OK/DEFER/FAIL
+*/
+
+static int
+tls_error(uschar * prefix, const host_item * host, uschar * msg, uschar ** errstr)
+{
+if (!msg)
+ {
+ ERR_error_string_n(ERR_get_error(), ssl_errstring, sizeof(ssl_errstring));
+ msg = US ssl_errstring;
+ }
+
+msg = string_sprintf("(%s): %s", prefix, msg);
+DEBUG(D_tls) debug_printf("TLS error '%s'\n", msg);
+if (errstr) *errstr = msg;
+return host ? FAIL : DEFER;
+}
+
+
+
+/**************************************************
+* General library initalisation *
+**************************************************/
+
+static BOOL
+lib_rand_init(void * addr)
+{
+randstuff r;
+if (!RAND_status()) return TRUE;
+
+gettimeofday(&r.tv, NULL);
+r.p = getpid();
+RAND_seed(US (&r), sizeof(r));
+RAND_seed(US big_buffer, big_buffer_size);
+if (addr) RAND_seed(US addr, sizeof(addr));
+
+return RAND_status();
+}
+
+
+static void
+tls_openssl_init(void)
+{
+static BOOL once = FALSE;
+if (once) return;
+once = TRUE;
+
+#ifdef EXIM_NEED_OPENSSL_INIT
+SSL_load_error_strings(); /* basic set up */
+OpenSSL_add_ssl_algorithms();
+#endif
+
+#if defined(EXIM_HAVE_SHA256) && !defined(OPENSSL_AUTO_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
+
+(void) lib_rand_init(NULL);
+(void) tls_openssl_options_parse(openssl_options, &init_options);
+}
+
+
+
+/*************************************************
+* Initialize for DH *
+*************************************************/
+
+/* If dhparam is set, expand it, and load up the parameters for DH encryption.
+Server only.
+
+Arguments:
+ sctx The current SSL CTX (inbound or outbound)
+ dhparam DH parameter file or fixed parameter identity string
+ errstr error string pointer
+
+Returns: TRUE if OK (nothing to set up, or setup worked)
+*/
+
+static BOOL
+init_dh(SSL_CTX * sctx, uschar * dhparam, uschar ** errstr)
+{
+BIO * bio;
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+DH * dh;
+#else
+EVP_PKEY * pkey;
+#endif
+uschar * dhexpanded;
+const char * pem;
+int dh_bitsize;
+
+if (!expand_check(dhparam, US"tls_dhparam", &dhexpanded, errstr))
+ 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),
+ NULL, US strerror(errno), errstr);
+ 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),
+ NULL, US strerror(errno), errstr);
+ return FALSE;
+ }
+ bio = BIO_new_mem_buf(CS pem, -1);
+ }
+
+if (!(
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+ dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL)
+#else
+ pkey = PEM_read_bio_Parameters_ex(bio, NULL, NULL, NULL)
+#endif
+ ) )
+ {
+ BIO_free(bio);
+ tls_error(string_sprintf("Could not read tls_dhparams \"%s\"", dhexpanded),
+ NULL, NULL, errstr);
+ return FALSE;
+ }
+
+/* note: our default limit of 2236 is not a multiple of 8; the limit comes from
+an NSS limit, and the GnuTLS APIs handle bit-sizes fine, so we went with 2236.
+But older OpenSSL can only report in bytes (octets), not bits. If someone wants
+to dance at the edge, then they can raise the limit or use current libraries. */
+
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+# ifdef EXIM_HAVE_OPENSSL_DH_BITS
+/* Added in commit 26c79d5641d; `git describe --contains` says OpenSSL_1_1_0-pre1~1022
+This predates OpenSSL_1_1_0 (before a, b, ...) so is in all 1.1.0 */
+dh_bitsize = DH_bits(dh);
+# else
+dh_bitsize = 8 * DH_size(dh);
+# endif
+#else /* 3.0.0 + */
+dh_bitsize = EVP_PKEY_get_bits(pkey);
+#endif
+
+/* 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. Likewise for a failing attempt to set one. */
+
+if (dh_bitsize <= tls_dh_max_bits)
+ {
+ if (
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+ SSL_CTX_set_tmp_dh(sctx, dh)
+#else
+ SSL_CTX_set0_tmp_dh_pkey(sctx, pkey)
+#endif
+ == 0)
+ {
+ ERR_error_string_n(ERR_get_error(), ssl_errstring, sizeof(ssl_errstring));
+ log_write(0, LOG_MAIN|LOG_PANIC, "TLS error (D-H param setting '%s'): %s",
+ dhexpanded ? dhexpanded : US"default", ssl_errstring);
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
+ /* EVP_PKEY_free(pkey); crashes */
+#endif
+ }
+ else
+ DEBUG(D_tls)
+ debug_printf("Diffie-Hellman initialized from %s with %d-bit prime\n",
+ dhexpanded ? dhexpanded : US"default", dh_bitsize);
+ }
+else
+ DEBUG(D_tls)
+ debug_printf("dhparams '%s' %d bits, is > tls_dh_max_bits limit of %d\n",
+ dhexpanded ? dhexpanded : US"default", dh_bitsize, tls_dh_max_bits);
+
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+DH_free(dh);
+#endif
+/* The EVP_PKEY ownership stays with the ctx; do not free it */
+
+BIO_free(bio);
+return TRUE;
+}
+
+
+
+
+/*************************************************
+* Initialize for ECDH *
+*************************************************/
+
+/* Load parameters for ECDH encryption. Server only.
+
+For now, we stick to NIST P-256 because: it's simple and easy to configure;
+it avoids any patent issues that might bite redistributors; despite events in
+the news and concerns over curve choices, we're not cryptographers, we're not
+pretending to be, and this is "good enough" to be better than no support,
+protecting against most adversaries. Given another year or two, there might
+be sufficient clarity about a "right" way forward to let us make an informed
+decision, instead of a knee-jerk reaction.
+
+Longer-term, we should look at supporting both various named curves and
+external files generated with "openssl ecparam", much as we do for init_dh().
+We should also support "none" as a value, to explicitly avoid initialisation.
+
+Patches welcome.
+
+Arguments:
+ sctx The current SSL CTX (inbound or outbound)
+ errstr error string pointer
+
+Returns: TRUE if OK (nothing to set up, or setup worked)
+*/
+
+static BOOL
+init_ecdh(SSL_CTX * sctx, uschar ** errstr)
+{
+#ifdef OPENSSL_NO_ECDH
+return TRUE;
+#else
+
+uschar * exp_curve;
+int nid;
+BOOL rv;
+
+# ifndef EXIM_HAVE_ECDH
+DEBUG(D_tls)
+ debug_printf("No OpenSSL API to define ECDH parameters, skipping\n");
+return TRUE;
+# else
+
+if (!expand_check(tls_eccurve, US"tls_eccurve", &exp_curve, errstr))
+ return FALSE;
+if (!exp_curve || !*exp_curve)
+ return TRUE;
+
+/* "auto" needs to be handled carefully.
+ * OpenSSL < 1.0.2: we do not select anything, but fallback to prime256v1
+ * OpenSSL < 1.1.0: we have to call SSL_CTX_set_ecdh_auto
+ * (openssl/ssl.h defines SSL_CTRL_SET_ECDH_AUTO)
+ * OpenSSL >= 1.1.0: we do not set anything, the libray does autoselection
+ * https://github.com/openssl/openssl/commit/fe6ef2472db933f01b59cad82aa925736935984b
+ */
+if (Ustrcmp(exp_curve, "auto") == 0)
+ {
+#if OPENSSL_VERSION_NUMBER < 0x10002000L
+ DEBUG(D_tls) debug_printf(
+ "ECDH OpenSSL < 1.0.2: temp key parameter settings: overriding \"auto\" with \"prime256v1\"\n");
+ exp_curve = US"prime256v1";
+#else
+# if defined SSL_CTRL_SET_ECDH_AUTO
+ DEBUG(D_tls) debug_printf(
+ "ECDH OpenSSL 1.0.2+: temp key parameter settings: autoselection\n");
+ SSL_CTX_set_ecdh_auto(sctx, 1);
+ return TRUE;
+# else
+ DEBUG(D_tls) debug_printf(
+ "ECDH OpenSSL 1.1.0+: temp key parameter settings: default selection\n");
+ return TRUE;
+# endif
+#endif
+ }
+
+DEBUG(D_tls) debug_printf("ECDH: curve '%s'\n", exp_curve);
+if ( (nid = OBJ_sn2nid (CCS exp_curve)) == NID_undef
+# ifdef EXIM_HAVE_OPENSSL_EC_NIST2NID
+ && (nid = EC_curve_nist2nid(CCS exp_curve)) == NID_undef
+# endif
+ )
+ {
+ tls_error(string_sprintf("Unknown curve name tls_eccurve '%s'", exp_curve),
+ NULL, NULL, errstr);
+ return FALSE;
+ }
+
+# if OPENSSL_VERSION_NUMBER < 0x30000000L
+ {
+ EC_KEY * ecdh;
+ if (!(ecdh = EC_KEY_new_by_curve_name(nid)))
+ {
+ tls_error(US"Unable to create ec curve", NULL, NULL, errstr);
+ return FALSE;
+ }
+
+ /* The "tmp" in the name here refers to setting a temporary key
+ not to the stability of the interface. */
+
+ if ((rv = SSL_CTX_set_tmp_ecdh(sctx, ecdh) == 0))
+ tls_error(string_sprintf("Error enabling '%s' curve", exp_curve), NULL, NULL, errstr);
+ else
+ DEBUG(D_tls) debug_printf("ECDH: enabled '%s' curve\n", exp_curve);
+ EC_KEY_free(ecdh);
+ }
+
+#else /* v 3.0.0 + */
+
+if ((rv = SSL_CTX_set1_groups(sctx, &nid, 1)) == 0)
+ tls_error(string_sprintf("Error enabling '%s' group", exp_curve), NULL, NULL, errstr);
+else
+ DEBUG(D_tls) debug_printf("ECDH: enabled '%s' group\n", exp_curve);
+
+#endif
+
+return !rv;
+
+# endif /*EXIM_HAVE_ECDH*/
+#endif /*OPENSSL_NO_ECDH*/
+}
+
+
+
+/*************************************************
+* Expand key and cert file specs *
+*************************************************/
+
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+/*
+Arguments:
+ s SSL connection (not used)
+ export not used
+ keylength keylength
+
+Returns: pointer to generated key
+*/
+
+static RSA *
+rsa_callback(SSL *s, int export, int keylength)
+{
+RSA *rsa_key;
+#ifdef EXIM_HAVE_RSA_GENKEY_EX
+BIGNUM *bn = BN_new();
+#endif
+
+DEBUG(D_tls) debug_printf("Generating %d bit RSA key...\n", keylength);
+
+# ifdef EXIM_HAVE_RSA_GENKEY_EX
+if ( !BN_set_word(bn, (unsigned long)RSA_F4)
+ || !(rsa_key = RSA_new())
+ || !RSA_generate_key_ex(rsa_key, keylength, bn, NULL)
+ )
+# else
+if (!(rsa_key = RSA_generate_key(keylength, RSA_F4, NULL, NULL)))
+# endif
+
+ {
+ ERR_error_string_n(ERR_get_error(), ssl_errstring, sizeof(ssl_errstring));
+ log_write(0, LOG_MAIN|LOG_PANIC, "TLS error (RSA_generate_key): %s",
+ ssl_errstring);
+ return NULL;
+ }
+return rsa_key;
+}
+#endif /* pre-3.0.0 */
+
+
+
+/* Create and install a selfsigned certificate, for use in server mode */
+/*XXX we could arrange to call this during prelo for a null tls_certificate option.
+The normal cache inval + relo will suffice.
+Just need a timer for inval. */
+
+static int
+tls_install_selfsign(SSL_CTX * sctx, uschar ** errstr)
+{
+X509 * x509 = NULL;
+EVP_PKEY * pkey;
+X509_NAME * name;
+uschar * where;
+
+DEBUG(D_tls) debug_printf("TLS: generating selfsigned server cert\n");
+where = US"allocating pkey";
+if (!(pkey = EVP_PKEY_new()))
+ goto err;
+
+where = US"allocating cert";
+if (!(x509 = X509_new()))
+ goto err;
+
+where = US"generating pkey";
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+ {
+ RSA * rsa;
+ if (!(rsa = rsa_callback(NULL, 0, 2048)))
+ goto err;
+
+ where = US"assigning pkey";
+ if (!EVP_PKEY_assign_RSA(pkey, rsa))
+ goto err;
+ }
+#else
+pkey = EVP_RSA_gen(2048);
+#endif
+
+X509_set_version(x509, 2); /* N+1 - version 3 */
+ASN1_INTEGER_set(X509_get_serialNumber(x509), 1);
+X509_gmtime_adj(X509_get_notBefore(x509), 0);
+X509_gmtime_adj(X509_get_notAfter(x509), (long)2 * 60 * 60); /* 2 hour */
+X509_set_pubkey(x509, pkey);
+
+name = X509_get_subject_name(x509);
+X509_NAME_add_entry_by_txt(name, "C",
+ MBSTRING_ASC, CUS "UK", -1, -1, 0);
+X509_NAME_add_entry_by_txt(name, "O",
+ MBSTRING_ASC, CUS "Exim Developers", -1, -1, 0);
+X509_NAME_add_entry_by_txt(name, "CN",
+ MBSTRING_ASC, CUS smtp_active_hostname, -1, -1, 0);
+X509_set_issuer_name(x509, name);
+
+where = US"signing cert";
+if (!X509_sign(x509, pkey, EVP_md5()))
+ goto err;
+
+where = US"installing selfsign cert";
+if (!SSL_CTX_use_certificate(sctx, x509))
+ goto err;
+
+where = US"installing selfsign key";
+if (!SSL_CTX_use_PrivateKey(sctx, pkey))
+ goto err;
+
+return OK;
+
+err:
+ (void) tls_error(where, NULL, NULL, errstr);
+ if (x509) X509_free(x509);
+ if (pkey) EVP_PKEY_free(pkey);
+ return DEFER;
+}
+
+
+
+
+
+
+
+/*************************************************
+* 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)
+{
+DEBUG(D_tls)
+ {
+ const uschar * str;
+
+ if (where & SSL_ST_CONNECT)
+ str = US"SSL_connect";
+ else if (where & SSL_ST_ACCEPT)
+ str = US"SSL_accept";
+ else
+ str = US"SSL info (undefined)";
+
+ if (where & SSL_CB_LOOP)
+ debug_printf("%s: %s\n", str, SSL_state_string_long(s));
+ else if (where & SSL_CB_ALERT)
+ debug_printf("SSL3 alert %s:%s:%s\n",
+ str = where & SSL_CB_READ ? US"read" : US"write",
+ SSL_alert_type_string_long(ret), SSL_alert_desc_string_long(ret));
+ else if (where & SSL_CB_EXIT)
+ {
+ if (ret == 0)
+ debug_printf("%s: failed in %s\n", str, SSL_state_string_long(s));
+ else if (ret < 0)
+ debug_printf("%s: error in %s\n", str, SSL_state_string_long(s));
+ }
+ else if (where & SSL_CB_HANDSHAKE_START)
+ debug_printf("%s: hshake start: %s\n", str, SSL_state_string_long(s));
+ else if (where & SSL_CB_HANDSHAKE_DONE)
+ debug_printf("%s: hshake done: %s\n", str, SSL_state_string_long(s));
+ }
+}
+
+#ifdef OPENSSL_HAVE_KEYLOG_CB
+static void
+keylog_callback(const SSL *ssl, const char *line)
+{
+char * filename;
+FILE * fp;
+DEBUG(D_tls) debug_printf("%.200s\n", line);
+if (!(filename = getenv("SSLKEYLOGFILE"))) return;
+if (!(fp = fopen(filename, "a"))) return;
+fprintf(fp, "%s\n", line);
+fclose(fp);
+}
+#endif
+
+
+
+
+
+#ifndef DISABLE_EVENT
+static int
+verify_event(tls_support * tlsp, X509 * cert, int depth, const uschar * dn,
+ BOOL *calledp, const BOOL *optionalp, const uschar * what)
+{
+uschar * ev;
+uschar * yield;
+X509 * old_cert;
+
+ev = tlsp == &tls_out ? client_static_state->event_action : event_action;
+if (ev)
+ {
+ DEBUG(D_tls) debug_printf("verify_event: %s %d\n", what, depth);
+ old_cert = tlsp->peercert;
+ tlsp->peercert = X509_dup(cert);
+ /* NB we do not bother setting peerdn */
+ if ((yield = event_raise(ev, US"tls:cert", string_sprintf("%d", depth), &errno)))
+ {
+ log_write(0, LOG_MAIN, "[%s] %s verify denied by event-action: "
+ "depth=%d cert=%s: %s",
+ tlsp == &tls_out ? deliver_host_address : sender_host_address,
+ what, depth, dn, yield);
+ *calledp = TRUE;
+ if (!*optionalp)
+ {
+ if (old_cert) tlsp->peercert = old_cert; /* restore 1st failing cert */
+ return 1; /* reject (leaving peercert set) */
+ }
+ DEBUG(D_tls) debug_printf("Event-action verify failure overridden "
+ "(host in tls_try_verify_hosts)\n");
+ tlsp->verify_override = TRUE;
+ }
+ X509_free(tlsp->peercert);
+ tlsp->peercert = old_cert;
+ }
+return 0;
+}
+#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 the certificate-verified flag. 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. 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.
+
+May be called multiple times for different issues with a certificate, even
+for a given "depth" in the certificate chain.
+
+Arguments:
+ preverify_ok current yes/no state as 1/0
+ x509ctx certificate information.
+ tlsp per-direction (client vs. server) support data
+ calledp has-been-called flag
+ optionalp verification-is-optional flag
+
+Returns: 0 if verification should fail, otherwise 1
+*/
+
+static int
+verify_callback(int preverify_ok, X509_STORE_CTX * x509ctx,
+ tls_support * tlsp, BOOL * calledp, BOOL * optionalp)
+{
+X509 * cert = X509_STORE_CTX_get_current_cert(x509ctx);
+int depth = X509_STORE_CTX_get_error_depth(x509ctx);
+uschar dn[256];
+
+if (!X509_NAME_oneline(X509_get_subject_name(cert), CS dn, sizeof(dn)))
+ {
+ DEBUG(D_tls) debug_printf("X509_NAME_oneline() error\n");
+ log_write(0, LOG_MAIN, "[%s] SSL verify error: internal error",
+ tlsp == &tls_out ? deliver_host_address : sender_host_address);
+ return 0;
+ }
+dn[sizeof(dn)-1] = '\0';
+
+tlsp->verify_override = FALSE;
+if (preverify_ok == 0)
+ {
+ uschar * extra = verify_mode ? string_sprintf(" (during %c-verify for [%s])",
+ *verify_mode, sender_host_address)
+ : US"";
+ log_write(0, LOG_MAIN, "[%s] SSL verify error%s: depth=%d error=%s cert=%s",
+ tlsp == &tls_out ? deliver_host_address : sender_host_address,
+ extra, depth,
+ X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509ctx)), dn);
+ *calledp = TRUE;
+ if (!*optionalp)
+ {
+ if (!tlsp->peercert)
+ tlsp->peercert = X509_dup(cert); /* record failing cert */
+ return 0; /* reject */
+ }
+ DEBUG(D_tls) debug_printf("SSL verify failure overridden (host in "
+ "tls_try_verify_hosts)\n");
+ tlsp->verify_override = TRUE;
+ }
+
+else if (depth != 0)
+ {
+ DEBUG(D_tls) debug_printf("SSL verify ok: depth=%d SN=%s\n", depth, dn);
+#ifndef DISABLE_OCSP
+ if (tlsp == &tls_out && client_static_state->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_state->u_ocsp.client.verify_store,
+ cert))
+ ERR_clear_error();
+ sk_X509_push(client_static_state->verify_stack, cert);
+ }
+#endif
+#ifndef DISABLE_EVENT
+ if (verify_event(tlsp, cert, depth, dn, calledp, optionalp, US"SSL"))
+ return 0; /* reject, with peercert set */
+#endif
+ }
+else
+ {
+ const uschar * verify_cert_hostnames;
+
+ if ( tlsp == &tls_out
+ && ((verify_cert_hostnames = client_static_state->verify_cert_hostnames)))
+ /* client, wanting hostname check */
+ {
+
+#ifdef EXIM_HAVE_OPENSSL_CHECKHOST
+# ifndef X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
+# define X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS 0
+# endif
+# ifndef X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS
+# define X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS 0
+# endif
+ int sep = 0;
+ const uschar * list = verify_cert_hostnames;
+ uschar * name;
+ int rc;
+ while ((name = string_nextinlist(&list, &sep, NULL, 0)))
+ if ((rc = X509_check_host(cert, CCS name, 0,
+ X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
+ | X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS,
+ NULL)))
+ {
+ if (rc < 0)
+ {
+ log_write(0, LOG_MAIN, "[%s] SSL verify error: internal error",
+ tlsp == &tls_out ? deliver_host_address : sender_host_address);
+ name = NULL;
+ }
+ break;
+ }
+ if (!name)
+#else
+ if (!tls_is_name_for_cert(verify_cert_hostnames, cert))
+#endif
+ {
+ uschar * extra = verify_mode
+ ? string_sprintf(" (during %c-verify for [%s])",
+ *verify_mode, sender_host_address)
+ : US"";
+ log_write(0, LOG_MAIN,
+ "[%s] SSL verify error%s: certificate name mismatch: DN=\"%s\" H=\"%s\"",
+ tlsp == &tls_out ? deliver_host_address : sender_host_address,
+ extra, dn, verify_cert_hostnames);
+ *calledp = TRUE;
+ if (!*optionalp)
+ {
+ if (!tlsp->peercert)
+ tlsp->peercert = X509_dup(cert); /* record failing cert */
+ return 0; /* reject */
+ }
+ DEBUG(D_tls) debug_printf("SSL verify name failure overridden (host in "
+ "tls_try_verify_hosts)\n");
+ tlsp->verify_override = TRUE;
+ }
+ }
+
+#ifndef DISABLE_EVENT
+ if (verify_event(tlsp, cert, depth, dn, calledp, optionalp, US"SSL"))
+ return 0; /* reject, with peercert set */
+#endif
+
+ DEBUG(D_tls) debug_printf("SSL%s verify ok: depth=0 SN=%s\n",
+ *calledp ? "" : " authenticated", dn);
+ *calledp = TRUE;
+ }
+
+return 1; /* accept, at least for this level */
+}
+
+static int
+verify_callback_client(int preverify_ok, X509_STORE_CTX *x509ctx)
+{
+return verify_callback(preverify_ok, x509ctx, &tls_out,
+ &client_verify_callback_called, &client_verify_optional);
+}
+
+static int
+verify_callback_server(int preverify_ok, X509_STORE_CTX *x509ctx)
+{
+return verify_callback(preverify_ok, x509ctx, &tls_in,
+ &server_verify_callback_called, &server_verify_optional);
+}
+
+
+#ifdef SUPPORT_DANE
+
+/* This gets called *by* the dane library verify callback, which interposes
+itself.
+*/
+static int
+verify_callback_client_dane(int preverify_ok, X509_STORE_CTX * x509ctx)
+{
+X509 * cert = X509_STORE_CTX_get_current_cert(x509ctx);
+uschar dn[256];
+int depth = X509_STORE_CTX_get_error_depth(x509ctx);
+#ifndef DISABLE_EVENT
+BOOL dummy_called, optional = FALSE;
+#endif
+
+if (!X509_NAME_oneline(X509_get_subject_name(cert), CS dn, sizeof(dn)))
+ {
+ DEBUG(D_tls) debug_printf("X509_NAME_oneline() error\n");
+ log_write(0, LOG_MAIN, "[%s] SSL verify error: internal error",
+ deliver_host_address);
+ return 0;
+ }
+dn[sizeof(dn)-1] = '\0';
+
+DEBUG(D_tls) debug_printf("verify_callback_client_dane: %s depth %d %s\n",
+ preverify_ok ? "ok":"BAD", depth, dn);
+
+#ifndef DISABLE_EVENT
+ if (verify_event(&tls_out, cert, depth, dn,
+ &dummy_called, &optional, US"DANE"))
+ return 0; /* reject, with peercert set */
+#endif
+
+if (preverify_ok == 1)
+ {
+ tls_out.dane_verified = TRUE;
+#ifndef DISABLE_OCSP
+ if (client_static_state->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_state->u_ocsp.client.verify_store,
+ cert))
+ ERR_clear_error();
+ sk_X509_push(client_static_state->verify_stack, cert);
+ }
+#endif
+ }
+else
+ {
+ int err = X509_STORE_CTX_get_error(x509ctx);
+ DEBUG(D_tls)
+ debug_printf(" - err %d '%s'\n", err, X509_verify_cert_error_string(err));
+ if (err == X509_V_ERR_APPLICATION_VERIFICATION)
+ preverify_ok = 1;
+ }
+return preverify_ok;
+}
+
+#endif /*SUPPORT_DANE*/
+
+
+#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:
+ state various parts of session state
+ filename the filename putatively holding an OCSP response
+ is_pem file is PEM format; otherwise is DER
+*/
+
+static void
+ocsp_load_response(exim_openssl_state_st * state, const uschar * filename,
+ BOOL is_pem)
+{
+BIO * bio;
+OCSP_RESPONSE * resp;
+OCSP_BASICRESP * basic_response;
+OCSP_SINGLERESP * single_response;
+ASN1_GENERALIZEDTIME * rev, * thisupd, * nextupd;
+STACK_OF(X509) * sk;
+unsigned long verify_flags;
+int status, reason, i;
+
+DEBUG(D_tls)
+ debug_printf("tls_ocsp_file (%s) '%s'\n", is_pem ? "PEM" : "DER", filename);
+
+if (!filename || !*filename) return;
+
+ERR_clear_error();
+if (!(bio = BIO_new_file(CS filename, "rb")))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "Failed to open OCSP response file \"%s\": %.100s",
+ filename, ERR_reason_error_string(ERR_get_error()));
+ return;
+ }
+
+if (is_pem)
+ {
+ uschar * data, * freep;
+ char * dummy;
+ long len;
+ if (!PEM_read_bio(bio, &dummy, &dummy, &data, &len))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "Failed to read PEM file \"%s\": %.100s",
+ filename, ERR_reason_error_string(ERR_get_error()));
+ return;
+ }
+ freep = data;
+ resp = d2i_OCSP_RESPONSE(NULL, CUSS &data, len);
+ OPENSSL_free(freep);
+ }
+else
+ resp = d2i_OCSP_RESPONSE_bio(bio, NULL);
+BIO_free(bio);
+
+if (!resp)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "Error reading OCSP response from \"%s\": %s",
+ filename, ERR_reason_error_string(ERR_get_error()));
+ return;
+ }
+
+if ((status = OCSP_response_status(resp)) != OCSP_RESPONSE_STATUS_SUCCESSFUL)
+ {
+ DEBUG(D_tls) debug_printf("OCSP response not valid: %s (%d)\n",
+ OCSP_response_status_str(status), status);
+ goto bad;
+ }
+
+#ifdef notdef
+ {
+ BIO * bp = BIO_new_fp(debug_file, BIO_NOCLOSE);
+ OCSP_RESPONSE_print(bp, resp, 0); /* extreme debug: stapling content */
+ BIO_free(bp);
+ }
+#endif
+
+if (!(basic_response = OCSP_response_get1_basic(resp)))
+ {
+ DEBUG(D_tls)
+ debug_printf("OCSP response parse error: unable to extract basic response.\n");
+ goto bad;
+ }
+
+sk = state->verify_stack;
+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 */
+
+/* This does a full verify on the OCSP proof before we load it for serving
+up; possibly overkill - just date-checks might be nice enough.
+
+OCSP_basic_verify takes a "store" arg, but does not
+use it for the chain verification, which is all we do
+when OCSP_NOVERIFY is set. The content from the wire
+"basic_response" and a cert-stack "sk" are all that is used.
+
+We have a stack, loaded in setup_certs() if tls_verify_certificates
+was a file (not a directory, or "system"). It is unfortunate we
+cannot used the connection context store, as that would neatly
+handle the "system" case too, but there seems to be no library
+function for getting a stack from a store.
+[ In OpenSSL 1.1 - ? X509_STORE_CTX_get0_chain(ctx) ? ]
+We do not free the stack since it could be needed a second time for
+SNI handling.
+
+Separately we might try to replace using OCSP_basic_verify() - which seems to not
+be a public interface into the OpenSSL library (there's no manual entry) -
+But what with? We also use OCSP_basic_verify in the client stapling callback.
+And there we NEED it; we must verify that status... unless the
+library does it for us anyway? */
+
+if ((i = OCSP_basic_verify(basic_response, sk, NULL, verify_flags)) < 0)
+ {
+ DEBUG(D_tls)
+ {
+ ERR_error_string_n(ERR_get_error(), ssl_errstring, sizeof(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.
+
+XXX that will change when we add support for (TLS1.3) whole-chain stapling
+*/
+
+if (!(single_response = OCSP_resp_get0(basic_response, 0)))
+ {
+ 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:
+ /* Add the resp to the list used by tls_server_stapling_cb() */
+ {
+ ocsp_resplist ** op = &state->u_ocsp.server.olist, * oentry;
+ while (oentry = *op)
+ op = &oentry->next;
+ *op = oentry = store_get(sizeof(ocsp_resplist), GET_UNTAINTED);
+ oentry->next = NULL;
+ oentry->resp = resp;
+ }
+return;
+
+bad:
+ if (f.running_in_test_harness)
+ {
+ extern char ** environ;
+ if (environ) for (uschar ** p = USS environ; *p; 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;
+}
+
+
+static void
+ocsp_free_response_list(exim_openssl_state_st * cbinfo)
+{
+for (ocsp_resplist * olist = cbinfo->u_ocsp.server.olist; olist;
+ olist = olist->next)
+ OCSP_RESPONSE_free(olist->resp);
+cbinfo->u_ocsp.server.olist = NULL;
+}
+#endif /*!DISABLE_OCSP*/
+
+
+
+
+
+static int
+tls_add_certfile(SSL_CTX * sctx, exim_openssl_state_st * cbinfo, uschar * file,
+ uschar ** errstr)
+{
+DEBUG(D_tls) debug_printf("tls_certificate file '%s'\n", file);
+if (!SSL_CTX_use_certificate_chain_file(sctx, CS file))
+ return tls_error(string_sprintf(
+ "SSL_CTX_use_certificate_chain_file file=%s", file),
+ cbinfo->host, NULL, errstr);
+return 0;
+}
+
+static int
+tls_add_pkeyfile(SSL_CTX * sctx, exim_openssl_state_st * cbinfo, uschar * file,
+ uschar ** errstr)
+{
+DEBUG(D_tls) debug_printf("tls_privatekey file '%s'\n", file);
+if (!SSL_CTX_use_PrivateKey_file(sctx, CS file, SSL_FILETYPE_PEM))
+ return tls_error(string_sprintf(
+ "SSL_CTX_use_PrivateKey_file file=%s", file), cbinfo->host, NULL, errstr);
+return 0;
+}
+
+
+
+
+/* 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
+ state various parts of session state
+ errstr error string pointer
+
+Returns: OK/DEFER/FAIL
+*/
+
+static int
+tls_expand_session_files(SSL_CTX * sctx, exim_openssl_state_st * state,
+ uschar ** errstr)
+{
+uschar * expanded;
+
+if (!state->certificate)
+ {
+ if (!state->is_server) /* client */
+ return OK;
+ /* server */
+ if (tls_install_selfsign(sctx, errstr) != OK)
+ return DEFER;
+ }
+else
+ {
+ int err;
+
+ if ( !reexpand_tls_files_for_sni
+ && ( Ustrstr(state->certificate, US"tls_sni")
+ || Ustrstr(state->certificate, US"tls_in_sni")
+ || Ustrstr(state->certificate, US"tls_out_sni")
+ ) )
+ reexpand_tls_files_for_sni = TRUE;
+
+ if (!expand_check(state->certificate, US"tls_certificate", &expanded, errstr))
+ return DEFER;
+
+ if (expanded)
+ if (state->is_server)
+ {
+ const uschar * file_list = expanded;
+ int sep = 0;
+ uschar * file;
+#ifndef DISABLE_OCSP
+ const uschar * olist = state->u_ocsp.server.file;
+ int osep = 0;
+ uschar * ofile;
+ BOOL fmt_pem = FALSE;
+
+ if (olist)
+ if (!expand_check(olist, US"tls_ocsp_file", USS &olist, errstr))
+ return DEFER;
+ if (olist && !*olist)
+ olist = NULL;
+
+ if ( state->u_ocsp.server.file_expanded && olist
+ && (Ustrcmp(olist, state->u_ocsp.server.file_expanded) == 0))
+ {
+ DEBUG(D_tls) debug_printf(" - value unchanged, using existing values\n");
+ olist = NULL;
+ }
+ else
+ {
+ ocsp_free_response_list(state);
+ state->u_ocsp.server.file_expanded = olist;
+ }
+#endif
+
+ while (file = string_nextinlist(&file_list, &sep, NULL, 0))
+ {
+ if ((err = tls_add_certfile(sctx, state, file, errstr)))
+ return err;
+
+#ifndef DISABLE_OCSP
+ if (olist)
+ if ((ofile = string_nextinlist(&olist, &osep, NULL, 0)))
+ {
+ if (Ustrncmp(ofile, US"PEM ", 4) == 0)
+ {
+ fmt_pem = TRUE;
+ ofile += 4;
+ }
+ else if (Ustrncmp(ofile, US"DER ", 4) == 0)
+ {
+ fmt_pem = FALSE;
+ ofile += 4;
+ }
+ ocsp_load_response(state, ofile, fmt_pem);
+ }
+ else
+ DEBUG(D_tls) debug_printf("ran out of ocsp file list\n");
+#endif
+ }
+ }
+ else /* would there ever be a need for multiple client certs? */
+ if ((err = tls_add_certfile(sctx, state, expanded, errstr)))
+ return err;
+
+ if ( state->privatekey
+ && !expand_check(state->privatekey, US"tls_privatekey", &expanded, errstr))
+ 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 && *expanded)
+ if (state->is_server)
+ {
+ const uschar * file_list = expanded;
+ int sep = 0;
+ uschar * file;
+
+ while (file = string_nextinlist(&file_list, &sep, NULL, 0))
+ if ((err = tls_add_pkeyfile(sctx, state, file, errstr)))
+ return err;
+ }
+ else /* would there ever be a need for multiple client certs? */
+ if ((err = tls_add_pkeyfile(sctx, state, expanded, errstr)))
+ return err;
+ }
+
+return OK;
+}
+
+
+
+
+/**************************************************
+* One-time init credentials for server and client *
+**************************************************/
+
+static void
+normalise_ciphers(uschar ** ciphers, const uschar * pre_expansion_ciphers)
+{
+uschar * s = *ciphers;
+
+if (!s || !Ustrchr(s, '_')) return; /* no change needed */
+
+if (s == pre_expansion_ciphers)
+ s = string_copy(s); /* get writable copy */
+
+for (uschar * t = s; *t; t++) if (*t == '_') *t = '-';
+*ciphers = s;
+}
+
+static int
+server_load_ciphers(SSL_CTX * ctx, exim_openssl_state_st * state,
+ uschar * ciphers, uschar ** errstr)
+{
+DEBUG(D_tls) debug_printf("required ciphers: %s\n", ciphers);
+if (!SSL_CTX_set_cipher_list(ctx, CS ciphers))
+ return tls_error(US"SSL_CTX_set_cipher_list", NULL, NULL, errstr);
+state->server_cipher_list = ciphers;
+return OK;
+}
+
+
+
+static int
+lib_ctx_new(SSL_CTX ** ctxp, host_item * host, uschar ** errstr)
+{
+SSL_CTX * ctx;
+#ifdef EXIM_HAVE_OPENSSL_TLS_METHOD
+if (!(ctx = SSL_CTX_new(host ? TLS_client_method() : TLS_server_method())))
+#else
+if (!(ctx = SSL_CTX_new(host ? SSLv23_client_method() : SSLv23_server_method())))
+#endif
+ return tls_error(US"SSL_CTX_new", host, NULL, errstr);
+
+/* Set up the information callback, which outputs if debugging is at a suitable
+level. */
+
+DEBUG(D_tls)
+ {
+ SSL_CTX_set_info_callback(ctx, (void (*)())info_callback);
+#if defined(EXIM_HAVE_OPESSL_TRACE) && !defined(OPENSSL_NO_SSL_TRACE)
+ /* this needs a debug build of OpenSSL */
+ SSL_CTX_set_msg_callback(ctx, (void (*)())SSL_trace);
+#endif
+#ifdef OPENSSL_HAVE_KEYLOG_CB
+ SSL_CTX_set_keylog_callback(ctx, (void (*)())keylog_callback);
+#endif
+ }
+
+/* Automatically re-try reads/writes after renegotiation. */
+(void) SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
+*ctxp = ctx;
+return OK;
+}
+
+
+static unsigned
+tls_server_creds_init(void)
+{
+SSL_CTX * ctx;
+uschar * dummy_errstr;
+unsigned lifetime = 0;
+
+tls_openssl_init();
+
+state_server.lib_state = null_tls_preload;
+
+if (lib_ctx_new(&ctx, NULL, &dummy_errstr) != OK)
+ return 0;
+state_server.lib_state.lib_ctx = ctx;
+
+/* Preload DH params and EC curve */
+
+if (opt_unset_or_noexpand(tls_dhparam))
+ {
+ DEBUG(D_tls) debug_printf("TLS: preloading DH params for server\n");
+ if (init_dh(ctx, tls_dhparam, &dummy_errstr))
+ state_server.lib_state.dh = TRUE;
+ }
+else
+ DEBUG(D_tls) debug_printf("TLS: not preloading DH params for server\n");
+if (opt_unset_or_noexpand(tls_eccurve))
+ {
+ DEBUG(D_tls) debug_printf("TLS: preloading ECDH curve for server\n");
+ if (init_ecdh(ctx, &dummy_errstr))
+ state_server.lib_state.ecdh = TRUE;
+ }
+else
+ DEBUG(D_tls) debug_printf("TLS: not preloading ECDH curve for server\n");
+
+#if defined(EXIM_HAVE_INOTIFY) || defined(EXIM_HAVE_KEVENT)
+/* If we can, preload the server-side cert, key and ocsp */
+
+if ( opt_set_and_noexpand(tls_certificate)
+# ifndef DISABLE_OCSP
+ && opt_unset_or_noexpand(tls_ocsp_file)
+#endif
+ && opt_unset_or_noexpand(tls_privatekey))
+ {
+ /* Set watches on the filenames. The implementation does de-duplication
+ so we can just blindly do them all. */
+
+ if ( tls_set_watch(tls_certificate, TRUE)
+# ifndef DISABLE_OCSP
+ && tls_set_watch(tls_ocsp_file, TRUE)
+#endif
+ && tls_set_watch(tls_privatekey, TRUE))
+ {
+ state_server.certificate = tls_certificate;
+ state_server.privatekey = tls_privatekey;
+#ifndef DISABLE_OCSP
+ state_server.u_ocsp.server.file = tls_ocsp_file;
+#endif
+
+ DEBUG(D_tls) debug_printf("TLS: preloading server certs\n");
+ if (tls_expand_session_files(ctx, &state_server, &dummy_errstr) == OK)
+ state_server.lib_state.conn_certs = TRUE;
+ }
+ }
+else if ( !tls_certificate && !tls_privatekey
+# ifndef DISABLE_OCSP
+ && !tls_ocsp_file
+#endif
+ )
+ { /* Generate & preload a selfsigned cert. No files to watch. */
+ if (tls_expand_session_files(ctx, &state_server, &dummy_errstr) == OK)
+ {
+ state_server.lib_state.conn_certs = TRUE;
+ lifetime = f.running_in_test_harness ? 2 : 60 * 60; /* 1 hour */
+ }
+ }
+else
+ DEBUG(D_tls) debug_printf("TLS: not preloading server certs\n");
+
+
+/* If we can, preload the Authorities for checking client certs against.
+Actual choice to do verify is made (tls_{,try_}verify_hosts)
+at TLS conn startup */
+
+if ( opt_set_and_noexpand(tls_verify_certificates)
+ && opt_unset_or_noexpand(tls_crl))
+ {
+ /* Watch the default dir also as they are always included */
+
+ if ( tls_set_watch(CUS X509_get_default_cert_file(), FALSE)
+ && tls_set_watch(tls_verify_certificates, FALSE)
+ && tls_set_watch(tls_crl, FALSE))
+ {
+ DEBUG(D_tls) debug_printf("TLS: preloading CA bundle for server\n");
+
+ if (setup_certs(ctx, tls_verify_certificates, tls_crl, NULL, &dummy_errstr)
+ == OK)
+ state_server.lib_state.cabundle = TRUE;
+ }
+ }
+else
+ DEBUG(D_tls) debug_printf("TLS: not preloading CA bundle for server\n");
+#endif /* EXIM_HAVE_INOTIFY */
+
+
+/* If we can, preload the ciphers control string */
+
+if (opt_set_and_noexpand(tls_require_ciphers))
+ {
+ DEBUG(D_tls) debug_printf("TLS: preloading cipher list for server\n");
+ normalise_ciphers(&tls_require_ciphers, tls_require_ciphers);
+ if (server_load_ciphers(ctx, &state_server, tls_require_ciphers,
+ &dummy_errstr) == OK)
+ state_server.lib_state.pri_string = TRUE;
+ }
+else
+ DEBUG(D_tls) debug_printf("TLS: not preloading cipher list for server\n");
+return lifetime;
+}
+
+
+
+
+/* Preload whatever creds are static, onto a transport. The client can then
+just copy the pointer as it starts up.
+Called from the daemon after a cache-invalidate with watch set; called from
+a queue-run startup with watch clear. */
+
+static void
+tls_client_creds_init(transport_instance * t, BOOL watch)
+{
+smtp_transport_options_block * ob = t->options_block;
+exim_openssl_state_st tpt_dummy_state;
+host_item * dummy_host = (host_item *)1;
+uschar * dummy_errstr;
+SSL_CTX * ctx;
+
+tls_openssl_init();
+
+ob->tls_preload = null_tls_preload;
+if (lib_ctx_new(&ctx, dummy_host, &dummy_errstr) != OK)
+ return;
+ob->tls_preload.lib_ctx = ctx;
+
+tpt_dummy_state.lib_state = ob->tls_preload;
+
+#if defined(EXIM_HAVE_INOTIFY) || defined(EXIM_HAVE_KEVENT)
+if ( opt_set_and_noexpand(ob->tls_certificate)
+ && opt_unset_or_noexpand(ob->tls_privatekey))
+ {
+ if ( !watch
+ || ( tls_set_watch(ob->tls_certificate, FALSE)
+ && tls_set_watch(ob->tls_privatekey, FALSE)
+ ) )
+ {
+ uschar * pkey = ob->tls_privatekey;
+
+ DEBUG(D_tls)
+ debug_printf("TLS: preloading client certs for transport '%s'\n",t->name);
+
+ if ( tls_add_certfile(ctx, &tpt_dummy_state, ob->tls_certificate,
+ &dummy_errstr) == 0
+ && tls_add_pkeyfile(ctx, &tpt_dummy_state,
+ pkey ? pkey : ob->tls_certificate,
+ &dummy_errstr) == 0
+ )
+ ob->tls_preload.conn_certs = TRUE;
+ }
+ }
+else
+ DEBUG(D_tls)
+ debug_printf("TLS: not preloading client certs, for transport '%s'\n", t->name);
+
+
+if ( opt_set_and_noexpand(ob->tls_verify_certificates)
+ && opt_unset_or_noexpand(ob->tls_crl))
+ {
+ if ( !watch
+ || tls_set_watch(CUS X509_get_default_cert_file(), FALSE)
+ && tls_set_watch(ob->tls_verify_certificates, FALSE)
+ && tls_set_watch(ob->tls_crl, FALSE)
+ )
+ {
+ DEBUG(D_tls)
+ debug_printf("TLS: preloading CA bundle for transport '%s'\n", t->name);
+
+ if (setup_certs(ctx, ob->tls_verify_certificates,
+ ob->tls_crl, dummy_host, &dummy_errstr) == OK)
+ ob->tls_preload.cabundle = TRUE;
+ }
+ }
+else
+ DEBUG(D_tls)
+ debug_printf("TLS: not preloading CA bundle, for transport '%s'\n", t->name);
+
+#endif /*EXIM_HAVE_INOTIFY*/
+}
+
+
+#if defined(EXIM_HAVE_INOTIFY) || defined(EXIM_HAVE_KEVENT)
+/* Invalidate the creds cached, by dropping the current ones.
+Call when we notice one of the source files has changed. */
+
+static void
+tls_server_creds_invalidate(void)
+{
+SSL_CTX_free(state_server.lib_state.lib_ctx);
+state_server.lib_state = null_tls_preload;
+}
+
+
+static void
+tls_client_creds_invalidate(transport_instance * t)
+{
+smtp_transport_options_block * ob = t->options_block;
+SSL_CTX_free(ob->tls_preload.lib_ctx);
+ob->tls_preload = null_tls_preload;
+}
+
+#else
+
+static void
+tls_server_creds_invalidate(void)
+{ return; }
+
+static void
+tls_client_creds_invalidate(transport_instance * t)
+{ return; }
+
+#endif /*EXIM_HAVE_INOTIFY*/
+
+
+/* Extreme debug
+#ifndef DISABLE_OCSP
+void
+x509_store_dump_cert_s_names(X509_STORE * store)
+{
+STACK_OF(X509_OBJECT) * roots= store->objs;
+static uschar name[256];
+
+for (int 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_NAME * sn = X509_get_subject_name(tmp_obj->data.x509);
+ if (X509_NAME_oneline(sn, CS name, sizeof(name)))
+ {
+ name[sizeof(name)-1] = '\0';
+ debug_printf(" %s\n", name);
+ }
+ }
+ }
+}
+#endif
+*/
+
+
+#ifndef DISABLE_TLS_RESUME
+/* Manage the keysets used for encrypting the session tickets, on the server. */
+
+typedef struct { /* Session ticket encryption key */
+ uschar name[16];
+
+ const EVP_CIPHER * aes_cipher;
+ uschar aes_key[32]; /* size needed depends on cipher. aes_128 implies 128/8 = 16? */
+# if OPENSSL_VERSION_NUMBER < 0x30000000L
+ const EVP_MD * hmac_hash;
+# else
+ const uschar * hmac_hashname;
+# endif
+ uschar hmac_key[16];
+ time_t renew;
+ time_t expire;
+} exim_stek;
+
+static exim_stek exim_tk; /* current key */
+static exim_stek exim_tk_old; /* previous key */
+
+static void
+tk_init(void)
+{
+time_t t = time(NULL);
+
+if (exim_tk.name[0])
+ {
+ if (exim_tk.renew >= t) return;
+ exim_tk_old = exim_tk;
+ }
+
+if (f.running_in_test_harness) ssl_session_timeout = 6;
+
+DEBUG(D_tls) debug_printf("OpenSSL: %s STEK\n", exim_tk.name[0] ? "rotating" : "creating");
+if (RAND_bytes(exim_tk.aes_key, sizeof(exim_tk.aes_key)) <= 0) return;
+if (RAND_bytes(exim_tk.hmac_key, sizeof(exim_tk.hmac_key)) <= 0) return;
+if (RAND_bytes(exim_tk.name+1, sizeof(exim_tk.name)-1) <= 0) return;
+
+exim_tk.name[0] = 'E';
+exim_tk.aes_cipher = EVP_aes_256_cbc();
+# if OPENSSL_VERSION_NUMBER < 0x30000000L
+exim_tk.hmac_hash = EVP_sha256();
+# else
+exim_tk.hmac_hashname = US "sha256";
+# endif
+exim_tk.expire = t + ssl_session_timeout;
+exim_tk.renew = t + ssl_session_timeout/2;
+}
+
+static exim_stek *
+tk_current(void)
+{
+if (!exim_tk.name[0]) return NULL;
+return &exim_tk;
+}
+
+static exim_stek *
+tk_find(const uschar * name)
+{
+return memcmp(name, exim_tk.name, sizeof(exim_tk.name)) == 0 ? &exim_tk
+ : memcmp(name, exim_tk_old.name, sizeof(exim_tk_old.name)) == 0 ? &exim_tk_old
+ : NULL;
+}
+
+
+static int
+tk_hmac_init(
+# if OPENSSL_VERSION_NUMBER < 0x30000000L
+ HMAC_CTX * hctx,
+#else
+ EVP_MAC_CTX * hctx,
+#endif
+ exim_stek * key
+ )
+{
+/*XXX will want these dependent on the ssl session strength */
+# if OPENSSL_VERSION_NUMBER < 0x30000000L
+ HMAC_Init_ex(hctx, key->hmac_key, sizeof(key->hmac_key),
+ key->hmac_hash, NULL);
+#else
+ {
+ OSSL_PARAM params[3];
+ uschar * hk = string_copy(key->hmac_hashname); /* need nonconst */
+ params[0] = OSSL_PARAM_construct_octet_string("key", key->hmac_key, sizeof(key->hmac_key));
+ params[1] = OSSL_PARAM_construct_utf8_string("digest", CS hk, 0);
+ params[2] = OSSL_PARAM_construct_end();
+ if (EVP_MAC_CTX_set_params(hctx, params) == 0)
+ {
+ DEBUG(D_tls) debug_printf("EVP_MAC_CTX_set_params: %s\n",
+ ERR_reason_error_string(ERR_get_error()));
+ return 0; /* error in mac initialisation */
+ }
+}
+#endif
+return 1;
+}
+
+/* Callback for session tickets, on server */
+static int
+ticket_key_callback(SSL * ssl, uschar key_name[16],
+ uschar * iv, EVP_CIPHER_CTX * c_ctx,
+# if OPENSSL_VERSION_NUMBER < 0x30000000L
+ HMAC_CTX * hctx,
+#else
+ EVP_MAC_CTX * hctx,
+#endif
+ int enc)
+{
+tls_support * tlsp = state_server.tlsp;
+exim_stek * key;
+
+if (enc)
+ {
+ DEBUG(D_tls) debug_printf("ticket_key_callback: create new session\n");
+ tlsp->resumption |= RESUME_CLIENT_REQUESTED;
+
+ if (RAND_bytes(iv, EVP_MAX_IV_LENGTH) <= 0)
+ return -1; /* insufficient random */
+
+ if (!(key = tk_current())) /* current key doesn't exist or isn't valid */
+ return 0; /* key couldn't be created */
+ memcpy(key_name, key->name, 16);
+ DEBUG(D_tls) debug_printf("STEK expire " TIME_T_FMT "\n", key->expire - time(NULL));
+
+ if (tk_hmac_init(hctx, key) == 0) return 0;
+ EVP_EncryptInit_ex(c_ctx, key->aes_cipher, NULL, key->aes_key, iv);
+
+ DEBUG(D_tls) debug_printf("ticket created\n");
+ return 1;
+ }
+else
+ {
+ time_t now = time(NULL);
+
+ DEBUG(D_tls) debug_printf("ticket_key_callback: retrieve session\n");
+ tlsp->resumption |= RESUME_CLIENT_SUGGESTED;
+
+ if (!(key = tk_find(key_name)) || key->expire < now)
+ {
+ DEBUG(D_tls)
+ {
+ debug_printf("ticket not usable (%s)\n", key ? "expired" : "not found");
+ if (key) debug_printf("STEK expire " TIME_T_FMT "\n", key->expire - now);
+ }
+ return 0;
+ }
+
+ if (tk_hmac_init(hctx, key) == 0) return 0;
+ EVP_DecryptInit_ex(c_ctx, key->aes_cipher, NULL, key->aes_key, iv);
+
+ DEBUG(D_tls) debug_printf("ticket usable, STEK expire " TIME_T_FMT "\n", key->expire - now);
+
+ /* The ticket lifetime and renewal are the same as the STEK lifetime and
+ renewal, which is overenthusiastic. A factor of, say, 3x longer STEK would
+ be better. To do that we'd have to encode ticket lifetime in the name as
+ we don't yet see the restored session. Could check posthandshake for TLS1.3
+ and trigger a new ticket then, but cannot do that for TLS1.2 */
+ return key->renew < now ? 2 : 1;
+ }
+}
+#endif /* !DISABLE_TLS_RESUME */
+
+
+
+static void
+setup_cert_verify(SSL_CTX * ctx, BOOL optional,
+ int (*cert_vfy_cb)(int, X509_STORE_CTX *))
+{
+/* If verification is optional, don't fail if no certificate */
+
+SSL_CTX_set_verify(ctx,
+ SSL_VERIFY_PEER | (optional ? 0 : SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
+ cert_vfy_cb);
+}
+
+
+/*************************************************
+* 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}
+
+XXX might need to change to using ClientHello callback,
+per https://www.openssl.org/docs/manmaster/man3/SSL_client_hello_cb_fn.html
+*/
+
+#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);
+exim_openssl_state_st *state = (exim_openssl_state_st *) arg;
+int rc;
+int old_pool = store_pool;
+uschar * dummy_errstr;
+
+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_taint(US servername, GET_TAINTED);
+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 (lib_ctx_new(&server_sni, NULL, &dummy_errstr) != OK)
+ goto bad;
+
+/* Not sure how many of these are actually needed, since SSL object
+already exists. Might even need this selfsame callback, for reneg? */
+
+ {
+ SSL_CTX * ctx = state_server.lib_state.lib_ctx;
+ SSL_CTX_set_info_callback(server_sni, SSL_CTX_get_info_callback(ctx));
+ SSL_CTX_set_mode(server_sni, SSL_CTX_get_mode(ctx));
+ SSL_CTX_set_options(server_sni, SSL_CTX_get_options(ctx));
+ SSL_CTX_set_timeout(server_sni, SSL_CTX_get_timeout(ctx));
+ SSL_CTX_set_tlsext_servername_callback(server_sni, tls_servername_cb);
+ SSL_CTX_set_tlsext_servername_arg(server_sni, state);
+ }
+
+if ( !init_dh(server_sni, state->dhparam, &dummy_errstr)
+ || !init_ecdh(server_sni, &dummy_errstr)
+ )
+ goto bad;
+
+if ( state->server_cipher_list
+ && !SSL_CTX_set_cipher_list(server_sni, CS state->server_cipher_list))
+ goto bad;
+
+#ifndef DISABLE_OCSP
+if (state->u_ocsp.server.file)
+ {
+ SSL_CTX_set_tlsext_status_cb(server_sni, tls_server_stapling_cb);
+ SSL_CTX_set_tlsext_status_arg(server_sni, state);
+ }
+#endif
+
+ {
+ uschar * expcerts;
+ if ( !expand_check(tls_verify_certificates, US"tls_verify_certificates",
+ &expcerts, &dummy_errstr)
+ || (rc = setup_certs(server_sni, expcerts, tls_crl, NULL,
+ &dummy_errstr)) != OK)
+ goto bad;
+
+ if (expcerts && *expcerts)
+ setup_cert_verify(server_sni, FALSE, verify_callback_server);
+ }
+
+/* do this after setup_certs, because this can require the certs for verifying
+OCSP information. */
+if ((rc = tls_expand_session_files(server_sni, state, &dummy_errstr)) != OK)
+ goto bad;
+
+DEBUG(D_tls) debug_printf("Switching SSL context.\n");
+SSL_set_SSL_CTX(s, server_sni);
+return SSL_TLSEXT_ERR_OK;
+
+bad: return SSL_TLSEXT_ERR_ALERT_FATAL;
+}
+#endif /* EXIM_HAVE_OPENSSL_TLSEXT */
+
+
+
+
+#ifdef EXIM_HAVE_ALPN
+/*************************************************
+* Callback to handle ALPN *
+*************************************************/
+
+/* Called on server if tls_alpn nonblank after expansion,
+when client offers ALPN, after the SNI callback.
+If set and not matching the list then we dump the connection */
+
+static int
+tls_server_alpn_cb(SSL *ssl, const uschar ** out, uschar * outlen,
+ const uschar * in, unsigned int inlen, void * arg)
+{
+server_seen_alpn = TRUE;
+DEBUG(D_tls)
+ {
+ debug_printf("Received TLS ALPN offer:");
+ for (int pos = 0, siz; pos < inlen; pos += siz+1)
+ {
+ siz = in[pos];
+ if (pos + 1 + siz > inlen) siz = inlen - pos - 1;
+ debug_printf(" '%.*s'", siz, in + pos + 1);
+ }
+ debug_printf(". Our list: '%s'\n", tls_alpn);
+ }
+
+/* Look for an acceptable ALPN */
+
+if ( inlen > 1 /* at least one name */
+ && in[0]+1 == inlen /* filling the vector, so exactly one name */
+ )
+ {
+ const uschar * list = tls_alpn;
+ int sep = 0;
+ for (uschar * name; name = string_nextinlist(&list, &sep, NULL, 0); )
+ if (Ustrncmp(in+1, name, in[0]) == 0)
+ {
+ *out = in+1; /* we checked for exactly one, so can just point to it */
+ *outlen = inlen;
+ return SSL_TLSEXT_ERR_OK; /* use ALPN */
+ }
+ }
+
+/* More than one name from clilent, or name did not match our list. */
+
+/* This will be fatal to the TLS conn; would be nice to kill TCP also.
+Maybe as an option in future; for now leave control to the config (must-tls). */
+
+DEBUG(D_tls) debug_printf("TLS ALPN rejected\n");
+return SSL_TLSEXT_ERR_ALERT_FATAL;
+}
+#endif /* EXIM_HAVE_ALPN */
+
+
+
+#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 exim_openssl_state_st * state = arg;
+ocsp_resplist * olist = state->u_ocsp.server.olist;
+uschar * response_der; /*XXX blob */
+int response_der_len;
+
+DEBUG(D_tls)
+ debug_printf("Received TLS status request (OCSP stapling); %s response list\n",
+ olist ? "have" : "lack");
+
+tls_in.ocsp = OCSP_NOT_RESP;
+if (!olist)
+ return SSL_TLSEXT_ERR_NOACK;
+
+#ifdef EXIM_HAVE_OPESSL_GET0_SERIAL
+ {
+ const X509 * cert_sent = SSL_get_certificate(s);
+ const ASN1_INTEGER * cert_serial = X509_get0_serialNumber(cert_sent);
+ const BIGNUM * cert_bn = ASN1_INTEGER_to_BN(cert_serial, NULL);
+
+ for (; olist; olist = olist->next)
+ {
+ OCSP_BASICRESP * bs = OCSP_response_get1_basic(olist->resp);
+ const OCSP_SINGLERESP * single = OCSP_resp_get0(bs, 0);
+ const OCSP_CERTID * cid = OCSP_SINGLERESP_get0_id(single);
+ ASN1_INTEGER * res_cert_serial;
+ const BIGNUM * resp_bn;
+ ASN1_OCTET_STRING * res_cert_iNameHash;
+
+
+ (void) OCSP_id_get0_info(&res_cert_iNameHash, NULL, NULL, &res_cert_serial,
+ (OCSP_CERTID *) cid);
+ resp_bn = ASN1_INTEGER_to_BN(res_cert_serial, NULL);
+
+ DEBUG(D_tls)
+ {
+ debug_printf("cert serial: %s\n", BN_bn2hex(cert_bn));
+ debug_printf("resp serial: %s\n", BN_bn2hex(resp_bn));
+ }
+
+ if (BN_cmp(cert_bn, resp_bn) == 0)
+ {
+ DEBUG(D_tls) debug_printf("matched serial for ocsp\n");
+
+ /*XXX TODO: check the rest of the list for duplicate matches.
+ If any, need to also check the Issuer Name hash.
+ Without this, we will provide the wrong status in the case of
+ duplicate id. */
+
+ break;
+ }
+ DEBUG(D_tls) debug_printf("not match serial for ocsp\n");
+ }
+ if (!olist)
+ {
+ DEBUG(D_tls) debug_printf("failed to find match for ocsp\n");
+ return SSL_TLSEXT_ERR_NOACK;
+ }
+ }
+#else
+if (olist->next)
+ {
+ DEBUG(D_tls) debug_printf("OpenSSL version too early to support multi-leaf OCSP\n");
+ return SSL_TLSEXT_ERR_NOACK;
+ }
+#endif
+
+/*XXX could we do the i2d earlier, rather than during the callback? */
+response_der = NULL;
+response_der_len = i2d_OCSP_RESPONSE(olist->resp, &response_der);
+if (response_der_len <= 0)
+ return SSL_TLSEXT_ERR_NOACK;
+
+SSL_set_tlsext_status_ocsp_resp(state_server.lib_state.lib_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)
+{
+exim_openssl_state_st * cbinfo = arg;
+const unsigned char * p;
+int len;
+OCSP_RESPONSE * rsp;
+OCSP_BASICRESP * bs;
+int i;
+
+DEBUG(D_tls) debug_printf("Received TLS status callback (OCSP stapling):\n");
+len = SSL_get_tlsext_status_ocsp_resp(s, &p);
+if(!p)
+ { /* Expect this when we requested ocsp but got none */
+ if (SSL_session_reused(s) && tls_out.ocsp == OCSP_VFIED)
+ {
+ DEBUG(D_tls) debug_printf(" null, but resumed; ocsp vfy stored with session is good\n");
+ return 1;
+ }
+ if (cbinfo->u_ocsp.client.verify_required && LOGGING(tls_cipher))
+ log_write(0, LOG_MAIN, "Required TLS certificate status not received");
+ 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; /*XXX should use tlsp-> to permit concurrent outbound */
+ if (LOGGING(tls_cipher))
+ log_write(0, LOG_MAIN, "Received TLS cert 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 (LOGGING(tls_cipher))
+ log_write(0, LOG_MAIN, "Received TLS cert 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;
+#ifndef EXIM_HAVE_OCSP_RESP_COUNT
+ STACK_OF(OCSP_SINGLERESP) * sresp = bs->tbsResponseData->responses;
+#endif
+
+ DEBUG(D_tls) bp = BIO_new(BIO_s_mem());
+
+ /*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, cbinfo->verify_stack,
+ cbinfo->u_ocsp.client.verify_store, OCSP_NOEXPLICIT)) <= 0)
+ if (ERR_peek_error())
+ {
+ tls_out.ocsp = OCSP_FAILED;
+ if (LOGGING(tls_cipher)) log_write(0, LOG_MAIN,
+ "Received TLS cert status response, itself unverifiable: %s",
+ ERR_reason_error_string(ERR_peek_error()));
+ DEBUG(D_tls)
+ {
+ BIO_printf(bp, "OCSP response verify failure\n");
+ ERR_print_errors(bp);
+ OCSP_RESPONSE_print(bp, rsp, 0);
+ }
+ goto failed;
+ }
+ else
+ DEBUG(D_tls) debug_printf("no explicit trust for OCSP signing"
+ " in the root CA certificate; ignoring\n");
+
+ DEBUG(D_tls) debug_printf("OCSP response well-formed and signed OK\n");
+
+ /*XXX So we have a good stapled OCSP status. How do we know
+ it is for the cert of interest? OpenSSL 1.1.0 has a routine
+ OCSP_resp_find_status() which matches on a cert id, which presumably
+ we should use. Making an id needs OCSP_cert_id_new(), which takes
+ issuerName, issuerKey, serialNumber. Are they all in the cert?
+
+ For now, carry on blindly accepting the resp. */
+
+ for (int idx =
+#ifdef EXIM_HAVE_OCSP_RESP_COUNT
+ OCSP_resp_count(bs) - 1;
+#else
+ sk_OCSP_SINGLERESP_num(sresp) - 1;
+#endif
+ idx >= 0; idx--)
+ {
+ OCSP_SINGLERESP * single = OCSP_resp_get0(bs, idx);
+ int status, reason;
+ ASN1_GENERALIZEDTIME * rev, * thisupd, * nextupd;
+
+ /*XXX so I can see putting a loop in here to handle a rsp with >1 singleresp
+ - but what happens with a GnuTLS-style input?
+
+ we could do with a debug label for each singleresp
+ - it has a certID with a serialNumber, but I see no API to get that
+ */
+ status = OCSP_single_get0_status(single, &reason, &rev,
+ &thisupd, &nextupd);
+
+ DEBUG(D_tls)
+ {
+ time_print(bp, "This OCSP Update", thisupd);
+ 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, "OCSP dates invalid");
+ goto failed;
+ }
+
+ DEBUG(D_tls) BIO_printf(bp, "Certificate status: %s\n",
+ OCSP_cert_status_str(status));
+ switch(status)
+ {
+ case V_OCSP_CERTSTATUS_GOOD:
+ continue; /* the idx loop */
+ case V_OCSP_CERTSTATUS_REVOKED:
+ 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);
+ break;
+ default:
+ log_write(0, LOG_MAIN,
+ "Server certificate status unknown, in OCSP stapling");
+ break;
+ }
+
+ goto failed;
+ }
+
+ i = 1;
+ tls_out.ocsp = OCSP_VFIED;
+ goto good;
+
+ failed:
+ tls_out.ocsp = OCSP_FAILED;
+ i = cbinfo->u_ocsp.client.verify_required ? 0 : 1;
+ good:
+ {
+ uschar * s = NULL;
+ int len = (int) BIO_get_mem_data(bp, CSS &s);
+ if (len > 0) debug_printf("%.*s", len, s);
+ }
+ 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
+ ob transport options block, if client; NULL if server
+ ocsp_file file of stapling info (server); flag for require ocsp (client)
+ addr address if client; NULL if server (for some randomness)
+ caller_state place to put pointer to allocated state-struct
+ errstr error string pointer
+
+Returns: OK/DEFER/FAIL
+*/
+
+static int
+tls_init(host_item * host, smtp_transport_options_block * ob,
+#ifndef DISABLE_OCSP
+ uschar *ocsp_file,
+#endif
+ address_item *addr, exim_openssl_state_st ** caller_state,
+ tls_support * tlsp,
+ uschar ** errstr)
+{
+SSL_CTX * ctx;
+exim_openssl_state_st * state;
+int rc;
+
+if (host) /* client */
+ {
+ state = store_malloc(sizeof(exim_openssl_state_st));
+ memset(state, 0, sizeof(*state));
+ state->certificate = ob->tls_certificate;
+ state->privatekey = ob->tls_privatekey;
+ state->is_server = FALSE;
+ state->dhparam = NULL;
+ state->lib_state = ob->tls_preload;
+ }
+else /* server */
+ {
+ state = &state_server;
+ state->certificate = tls_certificate;
+ state->privatekey = tls_privatekey;
+ state->is_server = TRUE;
+ state->dhparam = tls_dhparam;
+ state->lib_state = state_server.lib_state;
+ }
+
+state->tlsp = tlsp;
+state->host = host;
+
+if (!state->lib_state.pri_string)
+ state->server_cipher_list = NULL;
+
+#ifndef DISABLE_EVENT
+state->event_action = NULL;
+#endif
+
+tls_openssl_init();
+
+/* 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.
+
+Although we likely called this before, at daemon startup, this is a chance
+to mix in further variable info (time, pid) if needed. */
+
+if (!lib_rand_init(addr))
+ return tls_error(US"RAND_status", host,
+ US"unable to seed random number generator", errstr);
+
+/* 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. */
+
+if (!init_options)
+ if (!tls_openssl_options_parse(openssl_options, &init_options))
+ return tls_error(US"openssl_options parsing failed", host, NULL, errstr);
+
+/* 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. */
+
+if (!(ctx = state->lib_state.lib_ctx))
+ {
+ if ((rc = lib_ctx_new(&ctx, host, errstr)) != OK)
+ return rc;
+ state->lib_state.lib_ctx = ctx;
+ }
+
+#ifndef DISABLE_TLS_RESUME
+tlsp->resumption = RESUME_SUPPORTED;
+#endif
+if (init_options)
+ {
+#ifndef DISABLE_TLS_RESUME
+ /* Should the server offer session resumption? */
+ if (!host && verify_check_host(&tls_resumption_hosts) == OK)
+ {
+ DEBUG(D_tls) debug_printf("tls_resumption_hosts overrides openssl_options\n");
+ init_options &= ~SSL_OP_NO_TICKET;
+ tlsp->resumption |= RESUME_SERVER_TICKET; /* server will give ticket on request */
+ tlsp->host_resumable = TRUE;
+ }
+#endif
+
+ DEBUG(D_tls) debug_printf("setting SSL CTX options: %#lx\n", init_options);
+ if (!(SSL_CTX_set_options(ctx, init_options)))
+ return tls_error(string_sprintf(
+ "SSL_CTX_set_option(%#lx)", init_options), host, NULL, errstr);
+ }
+else
+ DEBUG(D_tls) debug_printf("no SSL CTX options to set\n");
+
+/* We'd like to disable session cache unconditionally, but foolish Outlook
+Express clients then give up the first TLS connection and make a second one
+(which works). Only when there is an IMAP service on the same machine.
+Presumably OE is trying to use the cache for A on B. Leave it enabled for
+now, until we work out a decent way of presenting control to the config. It
+will never be used because we use a new context every time. */
+#ifdef notdef
+(void) SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
+#endif
+
+/* Initialize with DH parameters if supplied */
+/* Initialize ECDH temp key parameter selection */
+
+if (!host)
+ {
+ if (state->lib_state.dh)
+ { DEBUG(D_tls) debug_printf("TLS: DH params were preloaded\n"); }
+ else
+ if (!init_dh(ctx, state->dhparam, errstr)) return DEFER;
+
+ if (state->lib_state.ecdh)
+ { DEBUG(D_tls) debug_printf("TLS: ECDH curve was preloaded\n"); }
+ else
+ if (!init_ecdh(ctx, errstr)) return DEFER;
+ }
+
+/* Set up certificate and key (and perhaps OCSP info) */
+
+if (state->lib_state.conn_certs)
+ {
+ DEBUG(D_tls)
+ debug_printf("TLS: %s certs were preloaded\n", host ? "client":"server");
+ }
+else
+ {
+#ifndef DISABLE_OCSP
+ if (!host)
+ {
+ state->u_ocsp.server.file = ocsp_file;
+ state->u_ocsp.server.file_expanded = NULL;
+ state->u_ocsp.server.olist = NULL;
+ }
+#endif
+ if ((rc = tls_expand_session_files(ctx, state, errstr)) != OK) return rc;
+ }
+
+/* If we need to handle SNI or OCSP, do so */
+
+#ifdef EXIM_HAVE_OPENSSL_TLSEXT
+# ifndef DISABLE_OCSP
+ if (!(state->verify_stack = sk_X509_new_null()))
+ {
+ DEBUG(D_tls) debug_printf("failed to create stack for stapling verify\n");
+ return FAIL;
+ }
+# endif
+
+if (!host) /* server */
+ {
+# ifndef DISABLE_OCSP
+ /* We check u_ocsp.server.file, not server.olist, 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 (state->u_ocsp.server.file)
+ {
+ SSL_CTX_set_tlsext_status_cb(ctx, tls_server_stapling_cb);
+ SSL_CTX_set_tlsext_status_arg(ctx, state);
+ }
+# endif
+ /* We always do this, so that $tls_sni is available even if not used in
+ tls_certificate */
+ SSL_CTX_set_tlsext_servername_callback(ctx, tls_servername_cb);
+ SSL_CTX_set_tlsext_servername_arg(ctx, state);
+
+# ifdef EXIM_HAVE_ALPN
+ if (tls_alpn && *tls_alpn)
+ {
+ uschar * exp_alpn;
+ if ( expand_check(tls_alpn, US"tls_alpn", &exp_alpn, errstr)
+ && *exp_alpn && !isblank(*exp_alpn))
+ {
+ tls_alpn = exp_alpn; /* subprocess so ok to overwrite */
+ SSL_CTX_set_alpn_select_cb(ctx, tls_server_alpn_cb, state);
+ }
+ else
+ tls_alpn = NULL;
+ }
+# endif
+ }
+# ifndef DISABLE_OCSP
+else /* client */
+ if(ocsp_file) /* wanting stapling */
+ {
+ if (!(state->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(ctx, tls_client_stapling_cb);
+ SSL_CTX_set_tlsext_status_arg(ctx, state);
+ }
+# endif
+#endif
+
+state->verify_cert_hostnames = NULL;
+
+#ifdef EXIM_HAVE_EPHEM_RSA_KEX
+/* Set up the RSA callback */
+SSL_CTX_set_tmp_rsa_callback(ctx, rsa_callback);
+#endif
+
+/* Finally, set the session cache timeout, and we are done.
+The period appears to be also used for (server-generated) session tickets */
+
+SSL_CTX_set_timeout(ctx, ssl_session_timeout);
+DEBUG(D_tls) debug_printf("Initialized TLS\n");
+
+*caller_state = state;
+
+return OK;
+}
+
+
+
+
+/*************************************************
+* Get name of cipher in use *
+*************************************************/
+
+/*
+Argument: pointer to an SSL structure for the connection
+ pointer to number of bits for cipher
+Returns: pointer to allocated string in perm-pool
+*/
+
+static uschar *
+construct_cipher_name(SSL * ssl, const uschar * ver, int * bits)
+{
+int pool = store_pool;
+/* With OpenSSL 1.0.0a, 'c' 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 SSL_CIPHER *) SSL_get_current_cipher(ssl);
+uschar * s;
+
+SSL_CIPHER_get_bits(c, bits);
+
+store_pool = POOL_PERM;
+s = string_sprintf("%s:%s:%u", ver, SSL_CIPHER_get_name(c), *bits);
+store_pool = pool;
+DEBUG(D_tls) debug_printf("Cipher: %s\n", s);
+return s;
+}
+
+
+/* Get IETF-standard name for ciphersuite.
+Argument: pointer to an SSL structure for the connection
+Returns: pointer to string
+*/
+
+static const uschar *
+cipher_stdname_ssl(SSL * ssl)
+{
+#ifdef EXIM_HAVE_OPENSSL_CIPHER_STD_NAME
+return CUS SSL_CIPHER_standard_name(SSL_get_current_cipher(ssl));
+#else
+ushort id = 0xffff & SSL_CIPHER_get_id(SSL_get_current_cipher(ssl));
+return cipher_stdname(id >> 8, id & 0xff);
+#endif
+}
+
+
+static const uschar *
+tlsver_name(SSL * ssl)
+{
+uschar * s, * p;
+int pool = store_pool;
+
+store_pool = POOL_PERM;
+s = string_copy(US SSL_get_version(ssl));
+store_pool = pool;
+if ((p = Ustrchr(s, 'v'))) /* TLSv1.2 -> TLS1.2 */
+ for (;; p++) if (!(*p = p[1])) break;
+return CUS s;
+}
+
+
+static void
+peer_cert(SSL * ssl, tls_support * tlsp, uschar * peerdn, unsigned siz)
+{
+/*XXX we might consider a list-of-certs variable for the cert chain.
+SSL_get_peer_cert_chain(SSL*). We'd need a new variable type and support
+in list-handling functions, also consider the difference between the entire
+chain and the elements sent by the peer. */
+
+tlsp->peerdn = NULL;
+
+/* Will have already noted peercert on a verify fail; possibly not the leaf */
+if (!tlsp->peercert)
+ tlsp->peercert = SSL_get_peer_certificate(ssl);
+/* Beware anonymous ciphers which lead to server_cert being NULL */
+if (tlsp->peercert)
+ if (!X509_NAME_oneline(X509_get_subject_name(tlsp->peercert), CS peerdn, siz))
+ { DEBUG(D_tls) debug_printf("X509_NAME_oneline() error\n"); }
+ else
+ {
+ int oldpool = store_pool;
+
+ peerdn[siz-1] = '\0'; /* paranoia */
+ store_pool = POOL_PERM;
+ tlsp->peerdn = string_copy(peerdn);
+ store_pool = oldpool;
+
+ /* We used to set CV in the cert-verify callbacks (either plain or dane)
+ but they don't get called on session-resumption. So use the official
+ interface, which uses the resumed value. Unfortunately this claims verified
+ when it actually failed but we're in try-verify mode, due to us wanting the
+ knowlege that it failed so needing to have the callback and forcing a
+ permissive return. If we don't force it, the TLS startup is failed.
+ The extra bit of information is set in verify_override in the cb, stashed
+ for resumption next to the TLS session, and used here. */
+
+ if (!tlsp->verify_override)
+ tlsp->certificate_verified =
+#ifdef SUPPORT_DANE
+ tlsp->dane_verified ||
+#endif
+ SSL_get_verify_result(ssl) == X509_V_OK;
+ }
+}
+
+
+
+
+
+/*************************************************
+* Set up for verifying certificates *
+*************************************************/
+
+#ifndef DISABLE_OCSP
+/* Load certs from file, return TRUE on success */
+
+static BOOL
+chain_from_pem_file(const uschar * file, STACK_OF(X509) ** vp)
+{
+BIO * bp;
+STACK_OF(X509) * verify_stack = *vp;
+
+if (verify_stack)
+ while (sk_X509_num(verify_stack) > 0)
+ X509_free(sk_X509_pop(verify_stack));
+else
+ verify_stack = sk_X509_new_null();
+
+if (!(bp = BIO_new_file(CS file, "r"))) return FALSE;
+for (X509 * x; x = PEM_read_bio_X509(bp, NULL, 0, NULL); )
+ sk_X509_push(verify_stack, x);
+BIO_free(bp);
+*vp = verify_stack;
+return TRUE;
+}
+#endif
+
+
+
+/* Called by both client and server startup; on the server possibly
+repeated after a Server Name Indication.
+
+Arguments:
+ sctx SSL_CTX* to initialise
+ certs certs file, expanded
+ crl CRL file or NULL
+ host NULL in a server; the remote host in a client
+ errstr error string pointer
+
+Returns: OK/DEFER/FAIL
+*/
+
+static int
+setup_certs(SSL_CTX *sctx, uschar *certs, uschar *crl, host_item *host,
+ uschar ** errstr)
+{
+uschar *expcerts, *expcrl;
+
+if (!expand_check(certs, US"tls_verify_certificates", &expcerts, errstr))
+ return DEFER;
+DEBUG(D_tls) debug_printf("tls_verify_certificates: %s\n", expcerts);
+
+if (expcerts && *expcerts)
+ {
+ /* Tell the library to use its compiled-in location for the system default
+ CA bundle. Then add the ones specified in the config, if any. */
+
+ if (!SSL_CTX_set_default_verify_paths(sctx))
+ return tls_error(US"SSL_CTX_set_default_verify_paths", host, NULL, errstr);
+
+ if (Ustrcmp(expcerts, "system") != 0 && Ustrncmp(expcerts, "system,", 7) != 0)
+ {
+ struct stat statbuf;
+
+ 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
+ {
+ STACK_OF(X509) * verify_stack =
+#ifndef DISABLE_OCSP
+ !host ? state_server.verify_stack :
+#endif
+ NULL;
+ STACK_OF(X509) ** vp = &verify_stack;
+
+ file = expcerts; dir = NULL;
+#ifndef DISABLE_OCSP
+ /* In the server if we will be offering an OCSP proof, load chain from
+ file for verifying the OCSP proof at load time. */
+
+/*XXX Glitch! The file here is tls_verify_certs: the chain for verifying the client cert.
+This is inconsistent with the need to verify the OCSP proof of the server cert.
+*/
+ if ( !host
+ && statbuf.st_size > 0
+ && state_server.u_ocsp.server.file
+ && !chain_from_pem_file(file, vp)
+ )
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "failed to load cert chain from %s", file);
+ return DEFER;
+ }
+#endif
+ }
+
+ /* If a certificate file is empty, the load 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 || 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, errstr);
+
+ /* On the server load the list of CAs for which we will accept certs, for
+ sending to the client. This is only for the one-file
+ tls_verify_certificates variant.
+ If a list isn't loaded into the server, but some verify locations are set,
+ the server end appears to make a wildcard request for client certs.
+ Meanwhile, the client library as default behaviour *ignores* the list
+ we send over the wire - see man SSL_CTX_set_client_cert_cb.
+ Because of this, and that the dir variant is likely only used for
+ the public-CA bundle (not for a private CA), not worth fixing. */
+
+ if (file)
+ {
+ STACK_OF(X509_NAME) * names = SSL_load_client_CA_file(CS file);
+ int i = sk_X509_NAME_num(names);
+
+ if (!host) SSL_CTX_set_client_CA_list(sctx, names);
+ DEBUG(D_tls) debug_printf("Added %d additional certificate authorit%s\n",
+ i, i>1 ? "ies":"y");
+ }
+ else
+ DEBUG(D_tls)
+ debug_printf("Added dir for additional certificate authorities\n");
+ }
+ }
+
+ /* 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, errstr)) return DEFER;
+ if (expcrl && *expcrl)
+ {
+ 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, errstr);
+
+ /* 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 */
+ }
+
+return OK;
+}
+
+
+
+static void
+tls_dump_keylog(SSL * ssl)
+{
+#ifdef EXIM_HAVE_OPENSSL_KEYLOG
+ BIO * bp = BIO_new(BIO_s_mem());
+ uschar * s = NULL;
+ int len;
+ SSL_SESSION_print_keylog(bp, SSL_get_session(ssl));
+ len = (int) BIO_get_mem_data(bp, CSS &s);
+ if (len > 0) debug_printf("%.*s", len, s);
+ BIO_free(bp);
+#endif
+}
+
+
+/*************************************************
+* 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:
+ errstr pointer to error message
+
+Returns: OK on success
+ DEFER for errors before the start of the negotiation
+ FAIL for errors during the negotiation; the server can't
+ continue running.
+*/
+
+int
+tls_server_start(uschar ** errstr)
+{
+int rc;
+uschar * expciphers;
+exim_openssl_state_st * dummy_statep;
+SSL_CTX * ctx;
+SSL * ssl;
+static uschar peerdn[256];
+
+/* Check for previous activation */
+
+if (tls_in.active.sock >= 0)
+ {
+ tls_error(US"STARTTLS received after TLS started", NULL, US"", errstr);
+ smtp_printf("554 Already in TLS\r\n", FALSE);
+ return FAIL;
+ }
+
+/* Initialize the SSL library. If it fails, it will already have logged
+the error. */
+
+rc = tls_init(NULL, NULL,
+#ifndef DISABLE_OCSP
+ tls_ocsp_file,
+#endif
+ NULL, &dummy_statep, &tls_in, errstr);
+if (rc != OK) return rc;
+ctx = state_server.lib_state.lib_ctx;
+
+/* 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.
+
+XXX SSL_CTX_set_cipher_list() is replaced by SSL_CTX_set_ciphersuites()
+for TLS 1.3 . Since we do not call it at present we get the default list:
+TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256
+*/
+
+if (state_server.lib_state.pri_string)
+ { DEBUG(D_tls) debug_printf("TLS: cipher list was preloaded\n"); }
+else
+ {
+ if (!expand_check(tls_require_ciphers, US"tls_require_ciphers", &expciphers, errstr))
+ return FAIL;
+
+ if (expciphers)
+ {
+ normalise_ciphers(&expciphers, tls_require_ciphers);
+ if ((rc = server_load_ciphers(ctx, &state_server, expciphers, errstr)) != OK)
+ return rc;
+ }
+ }
+
+/* If this is a host for which certificate verification is mandatory or
+optional, set up appropriately. */
+
+tls_in.certificate_verified = FALSE;
+#ifdef SUPPORT_DANE
+tls_in.dane_verified = FALSE;
+#endif
+server_verify_callback_called = FALSE;
+
+if (verify_check_host(&tls_verify_hosts) == OK)
+ server_verify_optional = FALSE;
+else if (verify_check_host(&tls_try_verify_hosts) == OK)
+ server_verify_optional = TRUE;
+else
+ goto skip_certs;
+
+ {
+ uschar * expcerts;
+ if (!expand_check(tls_verify_certificates, US"tls_verify_certificates",
+ &expcerts, errstr))
+ return DEFER;
+ DEBUG(D_tls) debug_printf("tls_verify_certificates: %s\n", expcerts);
+
+ if (state_server.lib_state.cabundle)
+ { DEBUG(D_tls) debug_printf("TLS: CA bundle for server was preloaded\n"); }
+ else
+ if ((rc = setup_certs(ctx, expcerts, tls_crl, NULL, errstr)) != OK)
+ return rc;
+
+ if (expcerts && *expcerts)
+ setup_cert_verify(ctx, server_verify_optional, verify_callback_server);
+ }
+skip_certs: ;
+
+#ifndef DISABLE_TLS_RESUME
+# if OPENSSL_VERSION_NUMBER < 0x30000000L
+SSL_CTX_set_tlsext_ticket_key_cb(ctx, ticket_key_callback);
+/* despite working, appears to always return failure, so ignoring */
+# else
+SSL_CTX_set_tlsext_ticket_key_evp_cb(ctx, ticket_key_callback);
+/* despite working, appears to always return failure, so ignoring */
+# endif
+#endif
+
+#ifdef OPENSSL_HAVE_NUM_TICKETS
+# ifndef DISABLE_TLS_RESUME
+SSL_CTX_set_num_tickets(ctx, tls_in.host_resumable ? 1 : 0);
+# else
+SSL_CTX_set_num_tickets(ctx, 0); /* send no TLS1.3 stateful-tickets */
+# endif
+#endif
+
+
+/* Prepare for new connection */
+
+if (!(ssl = SSL_new(ctx)))
+ return tls_error(US"SSL_new", NULL, NULL, errstr);
+state_server.lib_state.lib_ssl = ssl;
+
+/* 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(ssl, sid_ctx, Ustrlen(sid_ctx));
+if (!tls_in.on_connect)
+ {
+ smtp_printf("220 TLS go ahead\r\n", FALSE);
+ 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(ssl, fileno(smtp_out));
+SSL_set_rfd(ssl, fileno(smtp_in));
+SSL_set_accept_state(ssl);
+
+DEBUG(D_tls) debug_printf("Calling SSL_accept\n");
+
+ERR_clear_error();
+sigalrm_seen = FALSE;
+if (smtp_receive_timeout > 0) ALARM(smtp_receive_timeout);
+rc = SSL_accept(ssl);
+ALARM_CLR(0);
+
+if (rc <= 0)
+ {
+ int error = SSL_get_error(ssl, rc);
+ switch(error)
+ {
+ case SSL_ERROR_NONE:
+ break;
+
+ case SSL_ERROR_ZERO_RETURN:
+ DEBUG(D_tls) debug_printf("Got SSL_ERROR_ZERO_RETURN\n");
+ (void) tls_error(US"SSL_accept", NULL, sigalrm_seen ? US"timed out" : NULL, errstr);
+#ifndef DISABLE_EVENT
+ (void) event_raise(event_action, US"tls:fail:connect", *errstr, NULL);
+#endif
+ if (SSL_get_shutdown(ssl) == SSL_RECEIVED_SHUTDOWN)
+ SSL_shutdown(ssl);
+
+ tls_close(NULL, TLS_NO_SHUTDOWN);
+ return FAIL;
+
+ /* Handle genuine errors */
+ case SSL_ERROR_SSL:
+ {
+ uschar * s = NULL;
+ int r = ERR_GET_REASON(ERR_peek_error());
+ if ( r == SSL_R_WRONG_VERSION_NUMBER
+#ifdef SSL_R_VERSION_TOO_LOW
+ || r == SSL_R_VERSION_TOO_LOW
+#endif
+ || r == SSL_R_UNKNOWN_PROTOCOL || r == SSL_R_UNSUPPORTED_PROTOCOL)
+ s = string_sprintf("(%s)", SSL_get_version(ssl));
+ (void) tls_error(US"SSL_accept", NULL, sigalrm_seen ? US"timed out" : s, errstr);
+#ifndef DISABLE_EVENT
+ (void) event_raise(event_action, US"tls:fail:connect", *errstr, NULL);
+#endif
+ return FAIL;
+ }
+
+ default:
+ DEBUG(D_tls) debug_printf("Got SSL error %d\n", error);
+ if (error == SSL_ERROR_SYSCALL)
+ {
+ if (!errno)
+ {
+ *errstr = US"SSL_accept: TCP connection closed by peer";
+#ifndef DISABLE_EVENT
+ (void) event_raise(event_action, US"tls:fail:connect", *errstr, NULL);
+#endif
+ return FAIL;
+ }
+ DEBUG(D_tls) debug_printf(" - syscall %s\n", strerror(errno));
+ }
+ (void) tls_error(US"SSL_accept", NULL,
+ sigalrm_seen ? US"timed out"
+ : ERR_peek_error() ? NULL : string_sprintf("ret %d", error),
+ errstr);
+#ifndef DISABLE_EVENT
+ (void) event_raise(event_action, US"tls:fail:connect", *errstr, NULL);
+#endif
+ return FAIL;
+ }
+ }
+
+DEBUG(D_tls) debug_printf("SSL_accept was successful\n");
+ERR_clear_error(); /* Even success can leave errors in the stack. Seen with
+ anon-authentication ciphersuite negotiated. */
+
+#ifndef DISABLE_TLS_RESUME
+if (SSL_session_reused(ssl))
+ {
+ tls_in.resumption |= RESUME_USED;
+ DEBUG(D_tls) debug_printf("Session reused\n");
+ }
+#endif
+
+#ifdef EXIM_HAVE_ALPN
+/* If require-alpn, check server_seen_alpn here. Else abort TLS */
+if (!tls_alpn || !*tls_alpn)
+ { DEBUG(D_tls) debug_printf("TLS: was not watching for ALPN\n"); }
+else if (!server_seen_alpn)
+ if (verify_check_host(&hosts_require_alpn) == OK)
+ {
+ /* We'd like to send a definitive Alert but OpenSSL provides no facility */
+ SSL_shutdown(ssl);
+ tls_error(US"handshake", NULL, US"ALPN required but not negotiated", errstr);
+ return FAIL;
+ }
+ else
+ { DEBUG(D_tls) debug_printf("TLS: no ALPN presented in handshake\n"); }
+else DEBUG(D_tls)
+ {
+ const uschar * name;
+ unsigned len;
+ SSL_get0_alpn_selected(ssl, &name, &len);
+ if (len && name)
+ debug_printf("ALPN negotiated: '%.*s'\n", (int)*name, name+1);
+ else
+ debug_printf("ALPN: no protocol negotiated\n");
+ }
+#endif
+
+
+/* TLS has been set up. Record data for the connection,
+adjust the input functions to read via TLS, and initialize things. */
+
+#ifdef SSL_get_extms_support
+tls_in.ext_master_secret = SSL_get_extms_support(ssl) == 1;
+#endif
+peer_cert(ssl, &tls_in, peerdn, sizeof(peerdn));
+
+tls_in.ver = tlsver_name(ssl);
+tls_in.cipher = construct_cipher_name(ssl, tls_in.ver, &tls_in.bits);
+tls_in.cipher_stdname = cipher_stdname_ssl(ssl);
+
+DEBUG(D_tls)
+ {
+ uschar buf[2048];
+ if (SSL_get_shared_ciphers(ssl, CS buf, sizeof(buf)))
+ debug_printf("Shared ciphers: %s\n", buf);
+
+ tls_dump_keylog(ssl);
+
+#ifdef EXIM_HAVE_SESSION_TICKET
+ {
+ SSL_SESSION * ss = SSL_get_session(ssl);
+ if (SSL_SESSION_has_ticket(ss)) /* 1.1.0 */
+ debug_printf("The session has a ticket, life %lu seconds\n",
+ SSL_SESSION_get_ticket_lifetime_hint(ss));
+ }
+#endif
+ }
+
+/* Record the certificate we presented */
+ {
+ X509 * crt = SSL_get_certificate(ssl);
+ tls_in.ourcert = crt ? X509_dup(crt) : NULL;
+ }
+
+/* Channel-binding info for authenticators
+See description in https://paquier.xyz/postgresql-2/channel-binding-openssl/ */
+ {
+ uschar c, * s;
+ size_t len = SSL_get_peer_finished(ssl, &c, 0);
+ int old_pool = store_pool;
+
+ SSL_get_peer_finished(ssl, s = store_get((int)len, GET_UNTAINTED), len);
+ store_pool = POOL_PERM;
+ tls_in.channelbinding = b64encode_taint(CUS s, (int)len, GET_UNTAINTED);
+ store_pool = old_pool;
+ DEBUG(D_tls) debug_printf("Have channel bindings cached for possible auth usage %p\n", tls_in.channelbinding);
+ }
+
+/* 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.
+ */
+if (!ssl_xfer_buffer) ssl_xfer_buffer = store_malloc(ssl_xfer_buffer_size);
+ssl_xfer_buffer_lwm = ssl_xfer_buffer_hwm = 0;
+ssl_xfer_eof = ssl_xfer_error = FALSE;
+
+receive_getc = tls_getc;
+receive_getbuf = tls_getbuf;
+receive_get_cache = tls_get_cache;
+receive_hasc = tls_hasc;
+receive_ungetc = tls_ungetc;
+receive_feof = tls_feof;
+receive_ferror = tls_ferror;
+
+tls_in.active.sock = fileno(smtp_out);
+tls_in.active.tls_ctx = NULL; /* not using explicit ctx for server-side */
+return OK;
+}
+
+
+
+
+static int
+tls_client_basic_ctx_init(SSL_CTX * ctx,
+ host_item * host, smtp_transport_options_block * ob, exim_openssl_state_st * state,
+ uschar ** errstr)
+{
+int rc;
+
+/* Back-compatible old behaviour if tls_verify_certificates is set but both
+tls_verify_hosts and tls_try_verify_hosts are not set. Check only the specified
+host patterns if one of them is set with content. */
+
+if ( ( ( !ob->tls_verify_hosts || !ob->tls_verify_hosts
+ || Ustrcmp(ob->tls_try_verify_hosts, ":") == 0
+ )
+ && ( !ob->tls_try_verify_hosts || !*ob->tls_try_verify_hosts
+ || Ustrcmp(ob->tls_try_verify_hosts, ":") == 0
+ )
+ )
+ || verify_check_given_host(CUSS &ob->tls_verify_hosts, host) == OK
+ )
+ client_verify_optional = FALSE;
+else if (verify_check_given_host(CUSS &ob->tls_try_verify_hosts, host) == OK)
+ client_verify_optional = TRUE;
+else
+ return OK;
+
+ {
+ uschar * expcerts;
+ if (!expand_check(ob->tls_verify_certificates, US"tls_verify_certificates",
+ &expcerts, errstr))
+ return DEFER;
+ DEBUG(D_tls) debug_printf("tls_verify_certificates: %s\n", expcerts);
+
+ if (state->lib_state.cabundle)
+ { DEBUG(D_tls) debug_printf("TLS: CA bundle was preloaded\n"); }
+ else
+ if ((rc = setup_certs(ctx, expcerts, ob->tls_crl, host, errstr)) != OK)
+ return rc;
+
+ if (expcerts && *expcerts)
+ setup_cert_verify(ctx, client_verify_optional, verify_callback_client);
+ }
+
+if (verify_check_given_host(CUSS &ob->tls_verify_cert_hostnames, host) == OK)
+ {
+ state->verify_cert_hostnames =
+#ifdef SUPPORT_I18N
+ string_domain_utf8_to_alabel(host->certname, NULL);
+#else
+ host->certname;
+#endif
+ DEBUG(D_tls) debug_printf("Cert hostname to check: \"%s\"\n",
+ state->verify_cert_hostnames);
+ }
+return OK;
+}
+
+
+#ifdef SUPPORT_DANE
+static int
+dane_tlsa_load(SSL * ssl, host_item * host, dns_answer * dnsa, uschar ** errstr)
+{
+dns_scan dnss;
+const char * hostnames[2] = { CS host->name, NULL };
+int found = 0;
+
+if (DANESSL_init(ssl, NULL, hostnames) != 1)
+ return tls_error(US"hostnames load", host, NULL, errstr);
+
+for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)
+ ) if (rr->type == T_TLSA && rr->size > 3)
+ {
+ const uschar * p = rr->data;
+ uint8_t usage, selector, mtype;
+ const char * mdname;
+
+ usage = *p++;
+
+ /* Only DANE-TA(2) and DANE-EE(3) are supported */
+ if (usage != 2 && usage != 3) continue;
+
+ selector = *p++;
+ mtype = *p++;
+
+ switch (mtype)
+ {
+ default: continue; /* Only match-types 0, 1, 2 are supported */
+ case 0: mdname = NULL; break;
+ case 1: mdname = "sha256"; break;
+ case 2: mdname = "sha512"; break;
+ }
+
+ found++;
+ switch (DANESSL_add_tlsa(ssl, usage, selector, mdname, p, rr->size - 3))
+ {
+ default:
+ return tls_error(US"tlsa load", host, NULL, errstr);
+ case 0: /* action not taken */
+ case 1: break;
+ }
+
+ tls_out.tlsa_usage |= 1<<usage;
+ }
+
+if (found)
+ return OK;
+
+log_write(0, LOG_MAIN, "DANE error: No usable TLSA records");
+return DEFER;
+}
+#endif /*SUPPORT_DANE*/
+
+
+
+#ifndef DISABLE_TLS_RESUME
+/* On the client, get any stashed session for the given IP from hints db
+and apply it to the ssl-connection for attempted resumption. */
+
+static void
+tls_retrieve_session(tls_support * tlsp, SSL * ssl)
+{
+if (tlsp->host_resumable)
+ {
+ const uschar * key = tlsp->resume_index;
+ dbdata_tls_session * dt;
+ int len;
+ open_db dbblock, * dbm_file;
+
+ tlsp->resumption |= RESUME_CLIENT_REQUESTED;
+ DEBUG(D_tls)
+ debug_printf("checking for resumable session for %s\n", tlsp->resume_index);
+ if ((dbm_file = dbfn_open(US"tls", O_RDWR, &dbblock, FALSE, FALSE)))
+ {
+ if ((dt = dbfn_read_with_length(dbm_file, tlsp->resume_index, &len)))
+ {
+ SSL_SESSION * ss = NULL;
+ const uschar * sess_asn1 = dt->session;
+
+ len -= sizeof(dbdata_tls_session);
+ if (!(d2i_SSL_SESSION(&ss, &sess_asn1, (long)len)))
+ {
+ DEBUG(D_tls)
+ {
+ ERR_error_string_n(ERR_get_error(),
+ ssl_errstring, sizeof(ssl_errstring));
+ debug_printf("decoding session: %s\n", ssl_errstring);
+ }
+ }
+ else
+ {
+ unsigned long lifetime =
+#ifdef EXIM_HAVE_SESSION_TICKET
+ SSL_SESSION_get_ticket_lifetime_hint(ss);
+#else /* Use, fairly arbitrilarily, what we as server would */
+ f.running_in_test_harness ? 6 : ssl_session_timeout;
+#endif
+ if (lifetime + dt->time_stamp < time(NULL))
+ {
+ DEBUG(D_tls) debug_printf("session expired\n");
+ dbfn_delete(dbm_file, tlsp->resume_index);
+ }
+ else if (SSL_set_session(ssl, ss))
+ {
+ DEBUG(D_tls) debug_printf("good session\n");
+ tlsp->resumption |= RESUME_CLIENT_SUGGESTED;
+ tlsp->verify_override = dt->verify_override;
+ tlsp->ocsp = dt->ocsp;
+ }
+ else DEBUG(D_tls)
+ {
+ ERR_error_string_n(ERR_get_error(),
+ ssl_errstring, sizeof(ssl_errstring));
+ debug_printf("applying session to ssl: %s\n", ssl_errstring);
+ }
+ }
+ }
+ else
+ DEBUG(D_tls) debug_printf("no session record\n");
+ dbfn_close(dbm_file);
+ }
+ }
+}
+
+
+/* On the client, save the session for later resumption */
+
+static int
+tls_save_session_cb(SSL * ssl, SSL_SESSION * ss)
+{
+exim_openssl_state_st * cbinfo = SSL_get_ex_data(ssl, tls_exdata_idx);
+tls_support * tlsp;
+
+DEBUG(D_tls) debug_printf("tls_save_session_cb\n");
+
+if (!cbinfo || !(tlsp = cbinfo->tlsp)->host_resumable) return 0;
+
+# ifdef OPENSSL_HAVE_NUM_TICKETS
+if (SSL_SESSION_is_resumable(ss)) /* 1.1.1 */
+# endif
+ {
+ int len = i2d_SSL_SESSION(ss, NULL);
+ int dlen = sizeof(dbdata_tls_session) + len;
+ dbdata_tls_session * dt = store_get(dlen, GET_TAINTED);
+ uschar * s = dt->session;
+ open_db dbblock, * dbm_file;
+
+ DEBUG(D_tls) debug_printf("session is resumable\n");
+ tlsp->resumption |= RESUME_SERVER_TICKET; /* server gave us a ticket */
+
+ dt->verify_override = tlsp->verify_override;
+ dt->ocsp = tlsp->ocsp;
+ (void) i2d_SSL_SESSION(ss, &s); /* s gets bumped to end */
+
+ if ((dbm_file = dbfn_open(US"tls", O_RDWR, &dbblock, FALSE, FALSE)))
+ {
+ dbfn_write(dbm_file, tlsp->resume_index, dt, dlen);
+ dbfn_close(dbm_file);
+ DEBUG(D_tls) debug_printf("wrote session (len %u) to db\n",
+ (unsigned)dlen);
+ }
+ }
+return 1;
+}
+
+
+/* Construct a key for session DB lookup, and setup the SSL_CTX for resumption */
+
+static void
+tls_client_ctx_resume_prehandshake(
+ exim_openssl_client_tls_ctx * exim_client_ctx, smtp_connect_args * conn_args,
+ tls_support * tlsp, smtp_transport_options_block * ob)
+{
+tlsp->host_resumable = TRUE;
+tls_client_resmption_key(tlsp, conn_args, ob);
+
+SSL_CTX_set_session_cache_mode(exim_client_ctx->ctx,
+ SSL_SESS_CACHE_CLIENT
+ | SSL_SESS_CACHE_NO_INTERNAL | SSL_SESS_CACHE_NO_AUTO_CLEAR);
+SSL_CTX_sess_set_new_cb(exim_client_ctx->ctx, tls_save_session_cb);
+}
+
+static BOOL
+tls_client_ssl_resume_prehandshake(SSL * ssl, tls_support * tlsp,
+ host_item * host, uschar ** errstr)
+{
+if (tlsp->host_resumable)
+ {
+ DEBUG(D_tls)
+ debug_printf("tls_resumption_hosts overrides openssl_options, enabling tickets\n");
+ SSL_clear_options(ssl, SSL_OP_NO_TICKET);
+
+ tls_exdata_idx = SSL_get_ex_new_index(0, 0, 0, 0, 0);
+ if (!SSL_set_ex_data(ssl, tls_exdata_idx, client_static_state))
+ {
+ tls_error(US"set ex_data", host, NULL, errstr);
+ return FALSE;
+ }
+ debug_printf("tls_exdata_idx %d cbinfo %p\n", tls_exdata_idx, client_static_state);
+ }
+
+tlsp->resumption = RESUME_SUPPORTED;
+/* Pick up a previous session, saved on an old ticket */
+tls_retrieve_session(tlsp, ssl);
+return TRUE;
+}
+
+static void
+tls_client_resume_posthandshake(exim_openssl_client_tls_ctx * exim_client_ctx,
+ tls_support * tlsp)
+{
+if (SSL_session_reused(exim_client_ctx->ssl))
+ {
+ DEBUG(D_tls) debug_printf("The session was reused\n");
+ tlsp->resumption |= RESUME_USED;
+ }
+}
+#endif /* !DISABLE_TLS_RESUME */
+
+
+#ifdef EXIM_HAVE_ALPN
+/* Expand and convert an Exim list to an ALPN list. False return for fail.
+NULL plist return for silent no-ALPN.
+
+Overwite the passed-in list with the expanded version.
+*/
+
+static BOOL
+tls_alpn_plist(uschar ** tls_alpn, const uschar ** plist, unsigned * plen,
+ uschar ** errstr)
+{
+uschar * exp_alpn;
+
+if (!expand_check(*tls_alpn, US"tls_alpn", &exp_alpn, errstr))
+ return FALSE;
+*tls_alpn = exp_alpn;
+
+if (!exp_alpn)
+ {
+ DEBUG(D_tls) debug_printf("Setting TLS ALPN forced to fail, not sending\n");
+ *plist = NULL;
+ }
+else
+ {
+ /* The server implementation only accepts exactly one protocol name
+ but it's little extra code complexity in the client. */
+
+ const uschar * list = exp_alpn;
+ uschar * p = store_get(Ustrlen(exp_alpn), exp_alpn), * s, * t;
+ int sep = 0;
+ uschar len;
+
+ for (t = p; s = string_nextinlist(&list, &sep, NULL, 0); t += len)
+ {
+ *t++ = len = (uschar) Ustrlen(s);
+ memcpy(t, s, len);
+ }
+ *plist = (*plen = t - p) ? p : NULL;
+ }
+return TRUE;
+}
+#endif /* EXIM_HAVE_ALPN */
+
+
+/*************************************************
+* Start a TLS session in a client *
+*************************************************/
+
+/* Called from the smtp transport after STARTTLS has been accepted.
+
+Arguments:
+ cctx connection context
+ conn_args connection details
+ cookie datum for randomness; can be NULL
+ tlsp record details of TLS channel configuration here; must be non-NULL
+ errstr error string pointer
+
+Returns: TRUE for success with TLS session context set in connection context,
+ FALSE on error
+*/
+
+BOOL
+tls_client_start(client_conn_ctx * cctx, smtp_connect_args * conn_args,
+ void * cookie, tls_support * tlsp, uschar ** errstr)
+{
+host_item * host = conn_args->host; /* for msgs and option-tests */
+transport_instance * tb = conn_args->tblock; /* always smtp or NULL */
+smtp_transport_options_block * ob = tb
+ ? (smtp_transport_options_block *)tb->options_block
+ : &smtp_transport_option_defaults;
+exim_openssl_client_tls_ctx * exim_client_ctx;
+uschar * expciphers;
+int rc;
+static uschar peerdn[256];
+
+#ifndef DISABLE_OCSP
+BOOL request_ocsp = FALSE;
+BOOL require_ocsp = FALSE;
+#endif
+
+rc = store_pool;
+store_pool = POOL_PERM;
+exim_client_ctx = store_get(sizeof(exim_openssl_client_tls_ctx), GET_UNTAINTED);
+exim_client_ctx->corked = NULL;
+store_pool = rc;
+
+#ifdef SUPPORT_DANE
+tlsp->tlsa_usage = 0;
+#endif
+
+#ifndef DISABLE_OCSP
+ {
+# ifdef SUPPORT_DANE
+ /*XXX this should be moved to caller, to be common across gnutls/openssl */
+ if ( conn_args->dane
+ && ob->hosts_request_ocsp[0] == '*'
+ && ob->hosts_request_ocsp[1] == '\0'
+ )
+ {
+ /* Unchanged from default. Use a safer one under DANE */
+ request_ocsp = TRUE;
+ ob->hosts_request_ocsp = US"${if or { {= {0}{$tls_out_tlsa_usage}} "
+ " {= {4}{$tls_out_tlsa_usage}} } "
+ " {*}{}}";
+ }
+# endif
+
+ if ((require_ocsp =
+ verify_check_given_host(CUSS &ob->hosts_require_ocsp, host) == OK))
+ request_ocsp = TRUE;
+ else
+# ifdef SUPPORT_DANE
+ if (!request_ocsp)
+# endif
+ request_ocsp =
+ verify_check_given_host(CUSS &ob->hosts_request_ocsp, host) == OK;
+ }
+#endif
+
+rc = tls_init(host, ob,
+#ifndef DISABLE_OCSP
+ (void *)(long)request_ocsp,
+#endif
+ cookie, &client_static_state, tlsp, errstr);
+if (rc != OK) return FALSE;
+
+exim_client_ctx->ctx = client_static_state->lib_state.lib_ctx;
+
+tlsp->certificate_verified = FALSE;
+client_verify_callback_called = FALSE;
+
+expciphers = NULL;
+#ifdef SUPPORT_DANE
+if (conn_args->dane)
+ {
+ /* We fall back to tls_require_ciphers if unset, empty or forced failure, but
+ other failures should be treated as problems. */
+ if (ob->dane_require_tls_ciphers &&
+ !expand_check(ob->dane_require_tls_ciphers, US"dane_require_tls_ciphers",
+ &expciphers, errstr))
+ return FALSE;
+ if (expciphers && *expciphers == '\0')
+ expciphers = NULL;
+
+ normalise_ciphers(&expciphers, ob->dane_require_tls_ciphers);
+ }
+#endif
+if (!expciphers)
+ {
+ if (!expand_check(ob->tls_require_ciphers, US"tls_require_ciphers",
+ &expciphers, errstr))
+ return FALSE;
+
+ /* 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. */
+
+ normalise_ciphers(&expciphers, ob->tls_require_ciphers);
+ }
+
+if (expciphers)
+ {
+ DEBUG(D_tls) debug_printf("required ciphers: %s\n", expciphers);
+ if (!SSL_CTX_set_cipher_list(exim_client_ctx->ctx, CS expciphers))
+ {
+ tls_error(US"SSL_CTX_set_cipher_list", host, NULL, errstr);
+ return FALSE;
+ }
+ }
+
+#ifdef SUPPORT_DANE
+if (conn_args->dane)
+ {
+ SSL_CTX_set_verify(exim_client_ctx->ctx,
+ SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
+ verify_callback_client_dane);
+
+ if (!DANESSL_library_init())
+ {
+ tls_error(US"library init", host, NULL, errstr);
+ return FALSE;
+ }
+ if (DANESSL_CTX_init(exim_client_ctx->ctx) <= 0)
+ {
+ tls_error(US"context init", host, NULL, errstr);
+ return FALSE;
+ }
+ }
+else
+
+#endif
+
+if (tls_client_basic_ctx_init(exim_client_ctx->ctx, host, ob,
+ client_static_state, errstr) != OK)
+ return FALSE;
+
+if (ob->tls_sni)
+ {
+ if (!expand_check(ob->tls_sni, US"tls_sni", &tlsp->sni, errstr))
+ return FALSE;
+ if (!tlsp->sni)
+ { DEBUG(D_tls) debug_printf("Setting TLS SNI forced to fail, not sending\n"); }
+ else if (!Ustrlen(tlsp->sni))
+ tlsp->sni = NULL;
+ else
+ {
+#ifndef EXIM_HAVE_OPENSSL_TLSEXT
+ log_write(0, LOG_MAIN, "SNI unusable with this OpenSSL library version; ignoring \"%s\"\n",
+ tlsp->sni);
+ tlsp->sni = NULL;
+#endif
+ }
+ }
+
+if (ob->tls_alpn)
+#ifdef EXIM_HAVE_ALPN
+ {
+ const uschar * plist;
+ unsigned plen;
+
+ if (!tls_alpn_plist(&ob->tls_alpn, &plist, &plen, errstr))
+ return FALSE;
+ if (plist)
+ if (SSL_CTX_set_alpn_protos(exim_client_ctx->ctx, plist, plen) != 0)
+ {
+ tls_error(US"alpn init", host, NULL, errstr);
+ return FALSE;
+ }
+ else
+ DEBUG(D_tls) debug_printf("Setting TLS ALPN '%s'\n", ob->tls_alpn);
+ }
+#else
+ log_write(0, LOG_MAIN, "ALPN unusable with this OpenSSL library version; ignoring \"%s\"\n",
+ ob->tls_alpn);
+#endif
+
+#ifndef DISABLE_TLS_RESUME
+/*XXX have_lbserver: another cmdline arg possibly, for continued-conn, but use
+will be very low. */
+
+if (!conn_args->have_lbserver) /* wanted for tls_client_resmption_key() */
+ { DEBUG(D_tls) debug_printf("resumption not supported on continued-connection\n"); }
+else if (verify_check_given_host(CUSS &ob->tls_resumption_hosts, host) == OK)
+ tls_client_ctx_resume_prehandshake(exim_client_ctx, conn_args, tlsp, ob);
+#endif
+
+
+if (!(exim_client_ctx->ssl = SSL_new(exim_client_ctx->ctx)))
+ {
+ tls_error(US"SSL_new", host, NULL, errstr);
+ return FALSE;
+ }
+SSL_set_session_id_context(exim_client_ctx->ssl, sid_ctx, Ustrlen(sid_ctx));
+SSL_set_fd(exim_client_ctx->ssl, cctx->sock);
+SSL_set_connect_state(exim_client_ctx->ssl);
+
+#ifdef EXIM_HAVE_OPENSSL_TLSEXT
+if (tlsp->sni)
+ {
+ DEBUG(D_tls) debug_printf("Setting TLS SNI \"%s\"\n", tlsp->sni);
+ SSL_set_tlsext_host_name(exim_client_ctx->ssl, tlsp->sni);
+ }
+#endif
+
+#ifdef SUPPORT_DANE
+if (conn_args->dane)
+ if (dane_tlsa_load(exim_client_ctx->ssl, host, &conn_args->tlsa_dnsa, errstr) != OK)
+ return FALSE;
+#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()) */
+# ifdef SUPPORT_DANE
+if (request_ocsp)
+ {
+ const uschar * s;
+ if ( ((s = ob->hosts_require_ocsp) && Ustrstr(s, US"tls_out_tlsa_usage"))
+ || ((s = ob->hosts_request_ocsp) && Ustrstr(s, US"tls_out_tlsa_usage"))
+ )
+ { /* Re-eval now $tls_out_tlsa_usage is populated. If
+ this means we avoid the OCSP request, we wasted the setup
+ cost in tls_init(). */
+ require_ocsp = verify_check_given_host(CUSS &ob->hosts_require_ocsp, host) == OK;
+ request_ocsp = require_ocsp
+ || verify_check_given_host(CUSS &ob->hosts_request_ocsp, host) == OK;
+ }
+ }
+# endif
+
+if (request_ocsp)
+ {
+ SSL_set_tlsext_status_type(exim_client_ctx->ssl, TLSEXT_STATUSTYPE_ocsp);
+ client_static_state->u_ocsp.client.verify_required = require_ocsp;
+ tlsp->ocsp = OCSP_NOT_RESP;
+ }
+#endif
+
+#ifndef DISABLE_TLS_RESUME
+if (!tls_client_ssl_resume_prehandshake(exim_client_ctx->ssl, tlsp, host,
+ errstr))
+ return FALSE;
+#endif
+
+#ifndef DISABLE_EVENT
+client_static_state->event_action = tb ? tb->event_action : NULL;
+#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(exim_client_ctx->ssl);
+ALARM_CLR(0);
+
+#ifdef SUPPORT_DANE
+if (conn_args->dane)
+ DANESSL_cleanup(exim_client_ctx->ssl);
+#endif
+
+if (rc <= 0)
+ {
+ tls_error(US"SSL_connect", host, sigalrm_seen ? US"timed out" : NULL, errstr);
+ return FALSE;
+ }
+
+DEBUG(D_tls)
+ {
+ debug_printf("SSL_connect succeeded\n");
+ tls_dump_keylog(exim_client_ctx->ssl);
+ }
+
+#ifndef DISABLE_TLS_RESUME
+tls_client_resume_posthandshake(exim_client_ctx, tlsp);
+#endif
+
+#ifdef EXIM_HAVE_ALPN
+if (ob->tls_alpn) /* We requested. See what was negotiated. */
+ {
+ const uschar * name;
+ unsigned len;
+
+ SSL_get0_alpn_selected(exim_client_ctx->ssl, &name, &len);
+ if (len > 0)
+ { DEBUG(D_tls) debug_printf("ALPN negotiated %u: '%.*s'\n", len, (int)*name, name+1); }
+ else if (verify_check_given_host(CUSS &ob->hosts_require_alpn, host) == OK)
+ {
+ /* Would like to send a relevant fatal Alert, but OpenSSL has no API */
+ tls_error(US"handshake", host, US"ALPN required but not negotiated", errstr);
+ return FALSE;
+ }
+ }
+#endif
+
+#ifdef SSL_get_extms_support
+tlsp->ext_master_secret = SSL_get_extms_support(exim_client_ctx->ssl) == 1;
+#endif
+peer_cert(exim_client_ctx->ssl, tlsp, peerdn, sizeof(peerdn));
+
+tlsp->ver = tlsver_name(exim_client_ctx->ssl);
+tlsp->cipher = construct_cipher_name(exim_client_ctx->ssl, tlsp->ver, &tlsp->bits);
+tlsp->cipher_stdname = cipher_stdname_ssl(exim_client_ctx->ssl);
+
+/* Record the certificate we presented */
+ {
+ X509 * crt = SSL_get_certificate(exim_client_ctx->ssl);
+ tlsp->ourcert = crt ? X509_dup(crt) : NULL;
+ }
+
+/*XXX will this work with continued-TLS? */
+/* Channel-binding info for authenticators */
+ {
+ uschar c, * s;
+ size_t len = SSL_get_finished(exim_client_ctx->ssl, &c, 0);
+ int old_pool = store_pool;
+
+ SSL_get_finished(exim_client_ctx->ssl, s = store_get((int)len, GET_TAINTED), len);
+ store_pool = POOL_PERM;
+ tlsp->channelbinding = b64encode_taint(CUS s, (int)len, GET_TAINTED);
+ store_pool = old_pool;
+ DEBUG(D_tls) debug_printf("Have channel bindings cached for possible auth usage %p %p\n", tlsp->channelbinding, tlsp);
+ }
+
+tlsp->active.sock = cctx->sock;
+tlsp->active.tls_ctx = exim_client_ctx;
+cctx->tls_ctx = exim_client_ctx;
+return TRUE;
+}
+
+
+
+
+
+static BOOL
+tls_refill(unsigned lim)
+{
+SSL * ssl = state_server.lib_state.lib_ssl;
+int error;
+int inbytes;
+
+DEBUG(D_tls) debug_printf("Calling SSL_read(%p, %p, %u)\n", ssl,
+ ssl_xfer_buffer, ssl_xfer_buffer_size);
+
+ERR_clear_error();
+if (smtp_receive_timeout > 0) ALARM(smtp_receive_timeout);
+inbytes = SSL_read(ssl, CS ssl_xfer_buffer,
+ MIN(ssl_xfer_buffer_size, lim));
+error = SSL_get_error(ssl, inbytes);
+if (smtp_receive_timeout > 0) ALARM_CLR(0);
+
+if (had_command_timeout) /* set by signal handler */
+ smtp_command_timeout_exit(); /* does not return */
+if (had_command_sigterm)
+ smtp_command_sigterm_exit();
+if (had_data_timeout)
+ smtp_data_timeout_exit();
+if (had_data_sigint)
+ smtp_data_sigint_exit();
+
+/* 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. */
+
+switch(error)
+ {
+ case SSL_ERROR_NONE:
+ break;
+
+ case SSL_ERROR_ZERO_RETURN:
+ DEBUG(D_tls) debug_printf("Got SSL_ERROR_ZERO_RETURN\n");
+
+ if (SSL_get_shutdown(ssl) == SSL_RECEIVED_SHUTDOWN)
+ SSL_shutdown(ssl);
+
+ tls_close(NULL, TLS_NO_SHUTDOWN);
+ return FALSE;
+
+ /* Handle genuine errors */
+ case SSL_ERROR_SSL:
+ ERR_error_string_n(ERR_get_error(), ssl_errstring, sizeof(ssl_errstring));
+ log_write(0, LOG_MAIN, "TLS error (SSL_read): %s", ssl_errstring);
+ ssl_xfer_error = TRUE;
+ return FALSE;
+
+ default:
+ DEBUG(D_tls) debug_printf("Got SSL error %d\n", error);
+ DEBUG(D_tls) if (error == SSL_ERROR_SYSCALL)
+ debug_printf(" - syscall %s\n", strerror(errno));
+ ssl_xfer_error = TRUE;
+ return FALSE;
+ }
+
+#ifndef DISABLE_DKIM
+dkim_exim_verify_feed(ssl_xfer_buffer, inbytes);
+#endif
+ssl_xfer_buffer_hwm = inbytes;
+ssl_xfer_buffer_lwm = 0;
+return TRUE;
+}
+
+
+/*************************************************
+* 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: lim Maximum amount to read/buffer
+Returns: the next character or EOF
+
+Only used by the server-side TLS.
+*/
+
+int
+tls_getc(unsigned lim)
+{
+if (ssl_xfer_buffer_lwm >= ssl_xfer_buffer_hwm)
+ if (!tls_refill(lim))
+ return ssl_xfer_error ? EOF : smtp_getc(lim);
+
+/* Something in the buffer; return next uschar */
+
+return ssl_xfer_buffer[ssl_xfer_buffer_lwm++];
+}
+
+BOOL
+tls_hasc(void)
+{
+return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm;
+}
+
+uschar *
+tls_getbuf(unsigned * len)
+{
+unsigned size;
+uschar * buf;
+
+if (ssl_xfer_buffer_lwm >= ssl_xfer_buffer_hwm)
+ if (!tls_refill(*len))
+ {
+ if (!ssl_xfer_error) return smtp_getbuf(len);
+ *len = 0;
+ return NULL;
+ }
+
+if ((size = ssl_xfer_buffer_hwm - ssl_xfer_buffer_lwm) > *len)
+ size = *len;
+buf = &ssl_xfer_buffer[ssl_xfer_buffer_lwm];
+ssl_xfer_buffer_lwm += size;
+*len = size;
+return buf;
+}
+
+
+void
+tls_get_cache(unsigned lim)
+{
+#ifndef DISABLE_DKIM
+int n = ssl_xfer_buffer_hwm - ssl_xfer_buffer_lwm;
+debug_printf("tls_get_cache\n");
+if (n > lim)
+ n = lim;
+if (n > 0)
+ dkim_exim_verify_feed(ssl_xfer_buffer+ssl_xfer_buffer_lwm, n);
+#endif
+}
+
+
+BOOL
+tls_could_getc(void)
+{
+return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm
+ || SSL_pending(state_server.lib_state.lib_ssl) > 0;
+}
+
+
+/*************************************************
+* Read bytes from TLS channel *
+*************************************************/
+
+/*
+Arguments:
+ ct_ctx client context pointer, or NULL for the one global server context
+ buff buffer of data
+ len size of buffer
+
+Returns: the number of bytes read
+ -1 after a failed read, including EOF
+
+Only used by the client-side TLS.
+*/
+
+int
+tls_read(void * ct_ctx, uschar *buff, size_t len)
+{
+SSL * ssl = ct_ctx ? ((exim_openssl_client_tls_ctx *)ct_ctx)->ssl
+ : state_server.lib_state.lib_ssl;
+int inbytes;
+int error;
+
+DEBUG(D_tls) debug_printf("Calling SSL_read(%p, %p, %u)\n", ssl,
+ buff, (unsigned int)len);
+
+ERR_clear_error();
+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:
+ ct_ctx client context pointer, or NULL for the one global server context
+ buff buffer of data
+ len number of bytes
+ more further data expected soon
+
+Returns: the number of bytes after a successful write,
+ -1 after a failed write
+
+Used by both server-side and client-side TLS. Calling with len zero and more unset
+will flush buffered writes; buff can be null for this case.
+*/
+
+int
+tls_write(void * ct_ctx, const uschar * buff, size_t len, BOOL more)
+{
+size_t olen = len;
+int outbytes, error;
+SSL * ssl = ct_ctx
+ ? ((exim_openssl_client_tls_ctx *)ct_ctx)->ssl
+ : state_server.lib_state.lib_ssl;
+static gstring * server_corked = NULL;
+gstring ** corkedp = ct_ctx
+ ? &((exim_openssl_client_tls_ctx *)ct_ctx)->corked : &server_corked;
+gstring * corked = *corkedp;
+
+DEBUG(D_tls) debug_printf("%s(%p, %lu%s)\n", __FUNCTION__,
+ buff, (unsigned long)len, more ? ", more" : "");
+
+/* Lacking a CORK or MSG_MORE facility (such as GnuTLS has) we copy data when
+"more" is notified. This hack is only ok if small amounts are involved AND only
+one stream does it, in one context (i.e. no store reset). Currently it is used
+for the responses to the received SMTP MAIL , RCPT, DATA sequence, only.
+We support callouts done by the server process by using a separate client
+context for the stashed information. */
+/* + if PIPE_COMMAND, banner & ehlo-resp for smmtp-on-connect. Suspect there's
+a store reset there, so use POOL_PERM. */
+/* + if CHUNKING, cmds EHLO,MAIL,RCPT(s),BDAT */
+
+if (more || corked)
+ {
+ if (!len) buff = US &error; /* dummy just so that string_catn is ok */
+
+ int save_pool = store_pool;
+ store_pool = POOL_PERM;
+
+ corked = string_catn(corked, buff, len);
+
+ store_pool = save_pool;
+
+ if (more)
+ {
+ *corkedp = corked;
+ return len;
+ }
+ buff = CUS corked->s;
+ len = corked->ptr;
+ *corkedp = NULL;
+ }
+
+for (int left = len; left > 0;)
+ {
+ DEBUG(D_tls) debug_printf("SSL_write(%p, %p, %d)\n", ssl, buff, left);
+ ERR_clear_error();
+ 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_NONE: /* the usual case */
+ left -= outbytes;
+ buff += outbytes;
+ break;
+
+ case SSL_ERROR_SSL:
+ ERR_error_string_n(ERR_get_error(), ssl_errstring, sizeof(ssl_errstring));
+ log_write(0, LOG_MAIN, "TLS error (SSL_write): %s", ssl_errstring);
+ return -1;
+
+ case SSL_ERROR_ZERO_RETURN:
+ log_write(0, LOG_MAIN, "SSL channel closed on write");
+ return -1;
+
+ case SSL_ERROR_SYSCALL:
+ if (ct_ctx || errno != ECONNRESET || !f.smtp_in_quit)
+ log_write(0, LOG_MAIN, "SSL_write: (from %s) syscall: %s",
+ sender_fullhost ? sender_fullhost : US"<unknown>",
+ strerror(errno));
+ else if (LOGGING(protocol_detail))
+ log_write(0, LOG_MAIN, "[%s] after QUIT, client reset TCP before"
+ " SMTP response and TLS close\n", sender_host_address);
+ else
+ DEBUG(D_tls) debug_printf("[%s] SSL_write: after QUIT,"
+ " client reset TCP before TLS close\n", sender_host_address);
+ return -1;
+
+ default:
+ log_write(0, LOG_MAIN, "SSL_write error %d", error);
+ return -1;
+ }
+ }
+return olen;
+}
+
+
+
+/*
+Arguments:
+ ct_ctx client TLS context pointer, or NULL for the one global server context
+*/
+
+void
+tls_shutdown_wr(void * ct_ctx)
+{
+exim_openssl_client_tls_ctx * o_ctx = ct_ctx;
+SSL ** sslp = o_ctx ? &o_ctx->ssl : (SSL **) &state_server.lib_state.lib_ssl;
+int * fdp = o_ctx ? &tls_out.active.sock : &tls_in.active.sock;
+int rc;
+
+if (*fdp < 0) return; /* TLS was not active */
+
+tls_write(ct_ctx, NULL, 0, FALSE); /* flush write buffer */
+
+HDEBUG(D_transport|D_tls|D_acl|D_v) debug_printf_indent(" SMTP(TLS shutdown)>>\n");
+rc = SSL_shutdown(*sslp);
+if (rc < 0) DEBUG(D_tls)
+ {
+ ERR_error_string_n(ERR_get_error(), ssl_errstring, sizeof(ssl_errstring));
+ debug_printf("SSL_shutdown: %s\n", ssl_errstring);
+ }
+}
+
+/*************************************************
+* 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:
+ ct_ctx client TLS context pointer, or NULL for the one global server context
+ do_shutdown 0 no data-flush or TLS close-alert
+ 1 if TLS close-alert is to be sent,
+ 2 if also response to be waited for
+
+Returns: nothing
+
+Used by both server-side and client-side TLS.
+*/
+
+void
+tls_close(void * ct_ctx, int do_shutdown)
+{
+exim_openssl_client_tls_ctx * o_ctx = ct_ctx;
+SSL ** sslp = o_ctx ? &o_ctx->ssl : (SSL **) &state_server.lib_state.lib_ssl;
+int * fdp = o_ctx ? &tls_out.active.sock : &tls_in.active.sock;
+
+if (*fdp < 0) return; /* TLS was not active */
+
+if (do_shutdown > TLS_NO_SHUTDOWN)
+ {
+ int rc;
+ DEBUG(D_tls) debug_printf("tls_close(): shutting down TLS%s\n",
+ do_shutdown > TLS_SHUTDOWN_NOWAIT ? " (with response-wait)" : "");
+
+ tls_write(ct_ctx, NULL, 0, FALSE); /* flush write buffer */
+
+ if ( ( do_shutdown >= TLS_SHUTDOWN_WONLY
+ || (rc = SSL_shutdown(*sslp)) == 0 /* send "close notify" alert */
+ )
+ && do_shutdown > TLS_SHUTDOWN_NOWAIT
+ )
+ {
+#ifdef EXIM_TCP_CORK
+ (void) setsockopt(*fdp, IPPROTO_TCP, EXIM_TCP_CORK, US &off, sizeof(off));
+#endif
+ ALARM(2);
+ rc = SSL_shutdown(*sslp); /* wait for response */
+ ALARM_CLR(0);
+ }
+
+ if (rc < 0) DEBUG(D_tls)
+ {
+ ERR_error_string_n(ERR_get_error(), ssl_errstring, sizeof(ssl_errstring));
+ debug_printf("SSL_shutdown: %s\n", ssl_errstring);
+ }
+ }
+
+if (!o_ctx) /* server side */
+ {
+#ifndef DISABLE_OCSP
+ sk_X509_pop_free(state_server.verify_stack, X509_free);
+ state_server.verify_stack = NULL;
+#endif
+
+ receive_getc = smtp_getc;
+ receive_getbuf = smtp_getbuf;
+ receive_get_cache = smtp_get_cache;
+ receive_hasc = smtp_hasc;
+ receive_ungetc = smtp_ungetc;
+ receive_feof = smtp_feof;
+ receive_ferror = smtp_ferror;
+ tls_in.active.tls_ctx = NULL;
+ tls_in.sni = NULL;
+ /* Leave bits, peercert, cipher, peerdn, certificate_verified set, for logging */
+ }
+
+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 * expciphers, * err;
+
+tls_openssl_init();
+
+if (!(tls_require_ciphers && *tls_require_ciphers))
+ return NULL;
+
+if (!expand_check(tls_require_ciphers, US"tls_require_ciphers", &expciphers,
+ &err))
+ return US"failed to expand tls_require_ciphers";
+
+if (!(expciphers && *expciphers))
+ return NULL;
+
+normalise_ciphers(&expciphers, tls_require_ciphers);
+
+err = NULL;
+if (lib_ctx_new(&ctx, NULL, &err) == OK)
+ {
+ 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_n(ERR_get_error(), ssl_errstring, sizeof(ssl_errstring));
+ err = string_sprintf("SSL_CTX_set_cipher_list(%s) failed: %s",
+ expciphers, ssl_errstring);
+ }
+
+ 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: string to append to
+Returns: string
+*/
+
+gstring *
+tls_version_report(gstring * g)
+{
+return string_fmt_append(g,
+ "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 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(US (&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;
+
+#ifdef EXIM_HAVE_RAND_PSEUDO
+/* We do not care if crypto-strong */
+i = RAND_pseudo_bytes(smallbuf, needed_len);
+#else
+i = RAND_bytes(smallbuf, needed_len);
+#endif
+
+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 (uschar * 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
+*/
+
+
+
+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 * exp, * end;
+BOOL adding, item_parsed;
+
+/* Server: send no (<= TLS1.2) session tickets */
+result = SSL_OP_NO_TICKET;
+
+/* 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
+#ifdef SSL_OP_NO_SSLv3
+result |= SSL_OP_NO_SSLv3;
+#endif
+#ifdef SSL_OP_SINGLE_DH_USE
+result |= SSL_OP_SINGLE_DH_USE;
+#endif
+#ifdef SSL_OP_NO_RENEGOTIATION
+result |= SSL_OP_NO_RENEGOTIATION;
+#endif
+
+if (!option_spec)
+ {
+ *results = result;
+ return TRUE;
+ }
+
+if (!expand_check(option_spec, US"openssl_options", &exp, &end))
+ return FALSE;
+
+for (uschar * s = exp; *s; /**/)
+ {
+ 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 && !isspace(*end); ) end++;
+ item_parsed = tls_openssl_one_option_parse(string_copyn(s, end-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 %08lx: %08lx (%s)\n",
+ adding ? "adding to " : "removing from", result, item, s);
+ if (adding)
+ result |= item;
+ else
+ result &= ~item;
+ s = end;
+ }
+
+*results = result;
+return TRUE;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* vi: aw ai sw=2
+*/
+/* End of tls-openssl.c */
diff --git a/src/tls.c b/src/tls.c
new file mode 100644
index 0000000..3ed37bb
--- /dev/null
+++ b/src/tls.c
@@ -0,0 +1,843 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 Mavrogiannopoulos. 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"
+
+#if !defined(DISABLE_TLS) && !defined(USE_OPENSSL) && !defined(USE_GNUTLS)
+# error One of USE_OPENSSL or USE_GNUTLS must be defined for a TLS build
+#endif
+
+
+/* Forward decl. */
+static void tls_client_resmption_key(tls_support *, smtp_connect_args *,
+ smtp_transport_options_block *);
+
+
+#if defined(MACRO_PREDEF) && !defined(DISABLE_TLS)
+# include "macro_predef.h"
+# ifdef USE_GNUTLS
+# include "tls-gnu.c"
+# else
+# include "tls-openssl.c"
+# endif
+#endif
+
+#ifndef MACRO_PREDEF
+
+static void tls_per_lib_daemon_init(void);
+static void tls_per_lib_daemon_tick(void);
+static unsigned tls_server_creds_init(void);
+static void tls_server_creds_invalidate(void);
+static void tls_client_creds_init(transport_instance *, BOOL);
+static void tls_client_creds_invalidate(transport_instance *);
+static void tls_daemon_creds_reload(void);
+static BOOL opt_set_and_noexpand(const uschar *);
+static BOOL opt_unset_or_noexpand(const uschar *);
+
+
+
+/* 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. */
+
+#ifdef DISABLE_TLS
+static void dummy(int x) { dummy(x-1); }
+#else /* most of the rest of the file */
+
+const exim_tlslib_state null_tls_preload = {0};
+
+/* 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;
+#ifdef USE_OPENSSL
+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 = FALSE;
+static BOOL ssl_xfer_error = FALSE;
+#endif
+
+#ifdef EXIM_HAVE_KEVENT
+# define KEV_SIZE 16 /* Eight file,dir pairs */
+static struct kevent kev[KEV_SIZE];
+static int kev_used = 0;
+#endif
+
+static unsigned tls_creds_expire = 0;
+
+/*************************************************
+* 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, uschar ** errstr)
+{
+if (!s)
+ *result = NULL;
+else if ( !(*result = expand_string(US s)) /* need to clean up const more */
+ && !f.expand_string_forcedfail
+ )
+ {
+ *errstr = US"Internal error";
+ log_write(0, LOG_MAIN|LOG_PANIC, "expansion of %s failed: %s", name,
+ expand_string_message);
+ return FALSE;
+ }
+return TRUE;
+}
+
+
+#if defined(EXIM_HAVE_INOTIFY) || defined(EXIM_HAVE_KEVENT)
+/* Add the directory for a filename to the inotify handle, creating that if
+needed. This is enough to see changes to files in that dir.
+Return boolean success.
+
+The word "system" fails, which is on the safe side as we don't know what
+directory it implies nor if the TLS library handles a watch for us.
+
+The string "system,cache" is recognised and explicitly accepted without
+setting a watch. This permits the system CA bundle to be cached even though
+we have no way to tell when it gets modified by an update.
+The call chain for OpenSSL uses a (undocumented) call into the library
+to discover the actual file. We don't know what GnuTLS uses.
+
+A full set of caching including the CAs takes 35ms output off of the
+server tls_init() (GnuTLS, Fedora 32, 2018-class x86_64 laptop hardware).
+*/
+static BOOL
+tls_set_one_watch(const uschar * filename)
+# ifdef EXIM_HAVE_INOTIFY
+{
+uschar * s;
+
+if (Ustrcmp(filename, "system,cache") == 0) return TRUE;
+
+if (!(s = Ustrrchr(filename, '/'))) return FALSE;
+s = string_copyn(filename, s - filename); /* mem released by tls_set_watch */
+DEBUG(D_tls) debug_printf("watch dir '%s'\n", s);
+
+/*XXX unclear what effect symlinked files will have for inotify */
+
+if (inotify_add_watch(tls_watch_fd, CCS s,
+ IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE | IN_DELETE_SELF
+ | IN_MOVED_FROM | IN_MOVED_TO | IN_MOVE_SELF) >= 0)
+ return TRUE;
+DEBUG(D_tls) debug_printf("notify_add_watch: %s\n", strerror(errno));
+return FALSE;
+}
+# endif
+# ifdef EXIM_HAVE_KEVENT
+{
+uschar * s, * t;
+int fd1, fd2, i, j, cnt = 0;
+struct stat sb;
+#ifdef OpenBSD
+struct kevent k_dummy;
+struct timespec ts = {0};
+#endif
+
+errno = 0;
+if (Ustrcmp(filename, "system,cache") == 0) return TRUE;
+
+for (;;)
+ {
+ if (kev_used > KEV_SIZE-2) { s = US"out of kev space"; goto bad; }
+ if (!(s = Ustrrchr(filename, '/'))) return FALSE;
+ s = string_copyn(filename, s - filename); /* mem released by tls_set_watch */
+
+ /* The dir open will fail if there is a symlink on the path. Fine; it's too
+ much effort to handle all possible cases; just refuse the preload. */
+
+ if ((fd2 = open(CCS s, O_RDONLY | O_NOFOLLOW)) < 0) { s = US"open dir"; goto bad; }
+
+ if ((lstat(CCS filename, &sb)) < 0) { s = US"lstat"; goto bad; }
+ if (!S_ISLNK(sb.st_mode))
+ {
+ if ((fd1 = open(CCS filename, O_RDONLY | O_NOFOLLOW)) < 0)
+ { s = US"open file"; goto bad; }
+ DEBUG(D_tls) debug_printf("watch file '%s':\t%d\n", filename, fd1);
+ EV_SET(&kev[kev_used++],
+ (uintptr_t)fd1,
+ EVFILT_VNODE,
+ EV_ADD | EV_ENABLE | EV_ONESHOT,
+ NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND
+ | NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE,
+ 0,
+ NULL);
+ cnt++;
+ }
+ DEBUG(D_tls) debug_printf("watch dir '%s':\t%d\n", s, fd2);
+ EV_SET(&kev[kev_used++],
+ (uintptr_t)fd2,
+ EVFILT_VNODE,
+ EV_ADD | EV_ENABLE | EV_ONESHOT,
+ NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND
+ | NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE,
+ 0,
+ NULL);
+ cnt++;
+
+ if (!(S_ISLNK(sb.st_mode))) break;
+
+ t = store_get(1024, GET_UNTAINTED);
+ Ustrncpy(t, s, 1022);
+ j = Ustrlen(s);
+ t[j++] = '/';
+ if ((i = readlink(CCS filename, (void *)(t+j), 1023-j)) < 0) { s = US"readlink"; goto bad; }
+ filename = t;
+ *(t += i+j) = '\0';
+ store_release_above(t+1);
+ }
+
+#ifdef OpenBSD
+if (kevent(tls_watch_fd, &kev[kev_used-cnt], cnt, &k_dummy, 1, &ts) >= 0)
+ return TRUE;
+#else
+if (kevent(tls_watch_fd, &kev[kev_used-cnt], cnt, NULL, 0, NULL) >= 0)
+ return TRUE;
+#endif
+s = US"kevent";
+
+bad:
+DEBUG(D_tls)
+ if (errno)
+ debug_printf("%s: %s: %s\n", __FUNCTION__, s, strerror(errno));
+ else
+ debug_printf("%s: %s\n", __FUNCTION__, s);
+return FALSE;
+}
+# endif /*EXIM_HAVE_KEVENT*/
+
+
+/* Create an inotify facility if needed.
+Then set watches on the dir containing the given file or (optionally)
+list of files. Return boolean success. */
+
+static BOOL
+tls_set_watch(const uschar * filename, BOOL list)
+{
+rmark r;
+BOOL rc = FALSE;
+
+if (!filename || !*filename) return TRUE;
+if (Ustrncmp(filename, "system", 6) == 0) return TRUE;
+
+DEBUG(D_tls) debug_printf("tls_set_watch: '%s'\n", filename);
+
+if ( tls_watch_fd < 0
+# ifdef EXIM_HAVE_INOTIFY
+ && (tls_watch_fd = inotify_init1(O_CLOEXEC)) < 0
+# endif
+# ifdef EXIM_HAVE_KEVENT
+ && (tls_watch_fd = kqueue()) < 0
+# endif
+ )
+ {
+ DEBUG(D_tls) debug_printf("inotify_init: %s\n", strerror(errno));
+ return FALSE;
+ }
+
+r = store_mark();
+
+if (list)
+ {
+ int sep = 0;
+ for (uschar * s; s = string_nextinlist(&filename, &sep, NULL, 0); )
+ if (!(rc = tls_set_one_watch(s))) break;
+ }
+else
+ rc = tls_set_one_watch(filename);
+
+store_reset(r);
+if (!rc) DEBUG(D_tls) debug_printf("tls_set_watch() fail on '%s': %s\n", filename, strerror(errno));
+return rc;
+}
+
+
+void
+tls_watch_discard_event(int fd)
+{
+#ifdef EXIM_HAVE_INOTIFY
+(void) read(fd, big_buffer, big_buffer_size);
+#endif
+#ifdef EXIM_HAVE_KEVENT
+struct kevent kev;
+struct timespec t = {0};
+(void) kevent(fd, NULL, 0, &kev, 1, &t);
+#endif
+}
+#endif /*EXIM_HAVE_INOTIFY*/
+
+
+void
+tls_client_creds_reload(BOOL watch)
+{
+for(transport_instance * t = transports; t; t = t->next)
+ if (Ustrcmp(t->driver_name, "smtp") == 0)
+ {
+ tls_client_creds_invalidate(t);
+ tls_client_creds_init(t, watch);
+ }
+}
+
+
+void
+tls_watch_invalidate(void)
+{
+if (tls_watch_fd < 0) return;
+
+#ifdef EXIM_HAVE_KEVENT
+/* Close the files we had open for kevent */
+for (int i = 0; i < kev_used; i++)
+ {
+ DEBUG(D_tls) debug_printf("closing watch fd: %d\n", (int) kev[i].ident);
+ (void) close((int) kev[i].ident);
+ kev[i].ident = (uintptr_t)-1;
+ }
+kev_used = 0;
+#endif
+
+close(tls_watch_fd);
+tls_watch_fd = -1;
+}
+
+
+static void
+tls_daemon_creds_reload(void)
+{
+unsigned lifetime;
+
+#ifdef EXIM_HAVE_KEVENT
+tls_watch_invalidate();
+#endif
+
+tls_server_creds_invalidate();
+tls_creds_expire = (lifetime = tls_server_creds_init())
+ ? time(NULL) + lifetime : 0;
+
+tls_client_creds_reload(TRUE);
+}
+
+
+/* Utility predicates for use by the per-library code */
+static BOOL
+opt_set_and_noexpand(const uschar * opt)
+{ return opt && *opt && Ustrchr(opt, '$') == NULL; }
+
+static BOOL
+opt_unset_or_noexpand(const uschar * opt)
+{ return !opt || Ustrchr(opt, '$') == NULL; }
+
+
+
+/* Called every time round the daemon loop.
+
+If we reloaded fd-watcher, return the old watch fd
+having modified the global for the new one. Otherwise
+return -1.
+*/
+
+int
+tls_daemon_tick(void)
+{
+int old_watch_fd = tls_watch_fd;
+
+tls_per_lib_daemon_tick();
+#if defined(EXIM_HAVE_INOTIFY) || defined(EXIM_HAVE_KEVENT)
+if (tls_creds_expire && time(NULL) >= tls_creds_expire)
+ {
+ /* The server cert is a selfsign, with limited lifetime. Dump it and
+ generate a new one. Reload the rest of the creds also as the machinery
+ is all there. */
+
+ DEBUG(D_tls) debug_printf("selfsign cert rotate\n");
+ tls_creds_expire = 0;
+ tls_daemon_creds_reload();
+ return old_watch_fd;
+ }
+else if (tls_watch_trigger_time && time(NULL) >= tls_watch_trigger_time + 5)
+ {
+ /* Called, after a delay for multiple file ops to get done, from
+ the daemon when any of the watches added (above) fire.
+ Dump the set of watches and arrange to reload cached creds (which
+ will set up new watches). */
+
+ DEBUG(D_tls) debug_printf("watch triggered\n");
+ tls_watch_trigger_time = tls_creds_expire = 0;
+ tls_daemon_creds_reload();
+ return old_watch_fd;
+ }
+#endif
+return -1;
+}
+
+/* Called once at daemon startup */
+
+void
+tls_daemon_init(void)
+{
+tls_per_lib_daemon_init();
+}
+
+
+/*************************************************
+* Timezone environment flipping *
+*************************************************/
+
+static uschar *
+to_tz(uschar * tz)
+{
+uschar * old = US getenv("TZ");
+(void) setenv("TZ", CCS tz, 1);
+tzset();
+return old;
+}
+
+static void
+restore_tz(uschar * tz)
+{
+if (tz)
+ (void) setenv("TZ", CCS tz, 1);
+else
+ (void) os_unsetenv(US"TZ");
+tzset();
+}
+
+/*************************************************
+* 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)
+#endif
+
+#ifdef USE_OPENSSL
+# 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)
+{
+if (ssl_xfer_buffer_lwm <= 0)
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "buffer underflow in tls_ungetc");
+
+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 (int)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 (int)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 /*DISABLE_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);
+#ifdef USE_OPENSSL
+modify_variable(US"tls_sni", &dest_tsp->sni);
+#endif
+}
+
+
+#ifndef DISABLE_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)
+{
+for (uschar * 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 list containing optional output 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, const uschar * mod)
+{
+int insep = ',';
+uschar outsep = '\n';
+uschar * ele;
+uschar * match = NULL;
+int len;
+gstring * 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 output separator */
+
+dn_to_list(dn);
+insep = ',';
+len = match ? Ustrlen(match) : -1;
+while ((ele = string_nextinlist(CUSS &dn, &insep, NULL, 0)))
+ if ( !match
+ || Ustrncmp(ele, match, len) == 0 && ele[len] == '='
+ )
+ list = string_append_listele(list, outsep, ele+len+1);
+return string_from_gstring(list);
+}
+
+
+/* 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(const 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)))
+ {
+ const 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)))
+ {
+ const 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;
+}
+
+/* Environment cleanup: The GnuTLS library uses SSLKEYLOGFILE in the environment
+and writes a file by that name. Our OpenSSL code does the same, using keying
+info from the library API.
+The GnuTLS support only works if exim is run by root, not taking advantage of
+the setuid bit.
+You can use either the external environment (modulo the keep_environment config)
+or the add_environment config option for SSLKEYLOGFILE; the latter takes
+precedence.
+
+If the path is absolute, require it starts with the spooldir; otherwise delete
+the env variable. If relative, prefix the spooldir.
+*/
+void
+tls_clean_env(void)
+{
+uschar * path = US getenv("SSLKEYLOGFILE");
+if (path)
+ if (!*path)
+ unsetenv("SSLKEYLOGFILE");
+ else if (*path != '/')
+ {
+ DEBUG(D_tls)
+ debug_printf("prepending spooldir to env SSLKEYLOGFILE\n");
+ setenv("SSLKEYLOGFILE", CCS string_sprintf("%s/%s", spool_directory, path), 1);
+ }
+ else if (Ustrncmp(path, spool_directory, Ustrlen(spool_directory)) != 0)
+ {
+ DEBUG(D_tls)
+ debug_printf("removing env SSLKEYLOGFILE=%s: not under spooldir\n", path);
+ unsetenv("SSLKEYLOGFILE");
+ }
+}
+
+/*************************************************
+* 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.
+*/
+
+BOOL
+tls_dropprivs_validate_require_cipher(BOOL nowarn)
+{
+const uschar *errmsg;
+pid_t pid;
+int rc, status;
+void (*oldsignal)(int);
+
+/* If TLS will never be used, no point checking ciphers */
+
+if ( !tls_advertise_hosts
+ || !*tls_advertise_hosts
+ || Ustrcmp(tls_advertise_hosts, ":") == 0
+ )
+ return TRUE;
+else if (!nowarn && !tls_certificate)
+ log_write(0, LOG_MAIN,
+ "Warning: No server certificate defined; will use a selfsigned one.\n"
+ " Suggested action: either install a certificate or change tls_advertise_hosts option");
+
+oldsignal = signal(SIGCHLD, SIG_DFL);
+
+fflush(NULL);
+if ((pid = exim_fork(US"cipher-validate")) < 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");
+
+ if ((errmsg = tls_validate_require_cipher()))
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+ "tls_require_ciphers invalid: %s", errmsg);
+ fflush(NULL);
+ exim_underbar_exit(EXIT_SUCCESS);
+ }
+
+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;
+}
+
+
+
+
+static void
+tls_client_resmption_key(tls_support * tlsp, smtp_connect_args * conn_args,
+ smtp_transport_options_block * ob)
+{
+#ifndef DISABLE_TLS_RESUME
+hctx * h = &tlsp->resume_hctx;
+blob b;
+gstring * g;
+
+DEBUG(D_tls) if (conn_args->host_lbserver)
+ debug_printf("TLS: lbserver '%s'\n", conn_args->host_lbserver);
+
+# ifdef EXIM_HAVE_SHA2
+exim_sha_init(h, HASH_SHA2_256);
+# else
+exim_sha_init(h, HASH_SHA1);
+# endif
+exim_sha_update_string(h, conn_args->host_lbserver);
+# ifdef SUPPORT_DANE
+if (conn_args->dane)
+ exim_sha_update(h, CUS &conn_args->tlsa_dnsa, sizeof(dns_answer));
+# endif
+exim_sha_update_string(h, conn_args->host->address);
+exim_sha_update(h, CUS &conn_args->host->port, sizeof(conn_args->host->port));
+exim_sha_update_string(h, conn_args->sending_ip_address);
+exim_sha_update_string(h, openssl_options);
+exim_sha_update_string(h, ob->tls_require_ciphers);
+exim_sha_update_string(h, tlsp->sni);
+# ifdef EXIM_HAVE_ALPN
+exim_sha_update_string(h, ob->tls_alpn);
+# endif
+exim_sha_finish(h, &b);
+for (g = string_get(b.len*2+1); b.len-- > 0; )
+ g = string_fmt_append(g, "%02x", *b.data++);
+tlsp->resume_index = string_from_gstring(g);
+DEBUG(D_tls) debug_printf("TLS: resume session index %s\n", tlsp->resume_index);
+#endif
+}
+
+#endif /*!DISABLE_TLS*/
+#endif /*!MACRO_PREDEF*/
+
+/* 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
index 0000000..a40bb30
--- /dev/null
+++ b/src/tlscert-gnu.c
@@ -0,0 +1,482 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/* Copyright (c) Jeremy Harris 2014 - 2018 */
+
+/* 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
+******************************************************
+Return: boolean success
+*/
+
+BOOL
+tls_export_cert(uschar * buf, size_t buflen, void * cert)
+{
+size_t sz = buflen;
+rmark reset_point = store_mark();
+BOOL fail;
+const 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 FALSE;
+ }
+if ((cp = string_printing(buf)) != buf)
+ {
+ Ustrncpy(buf, cp, buflen);
+ if (buf[buflen-1])
+ fail = 1;
+ }
+store_reset(reset_point);
+return !fail;
+}
+
+/* On error, NULL out the destination */
+BOOL
+tls_import_cert(const uschar * buf, void ** cert)
+{
+rmark reset_point = store_mark();
+gnutls_datum_t datum;
+gnutls_x509_crt_t crt = *(gnutls_x509_crt_t *)cert;
+int rc;
+
+if (crt)
+ gnutls_x509_crt_deinit(crt);
+else
+ gnutls_global_init();
+
+gnutls_x509_crt_init(&crt);
+
+datum.data = string_unprinting(US buf);
+datum.size = Ustrlen(datum.data);
+if ((rc = gnutls_x509_crt_import(crt, &datum, GNUTLS_X509_FMT_PEM)))
+ {
+ log_write(0, LOG_MAIN, "TLS error in certificate import: %s",
+ gnutls_strerror(rc));
+ crt = NULL;
+ }
+*cert = (void *)crt;
+store_reset(reset_point);
+return rc != 0;
+}
+
+void
+tls_free_cert(void ** cert)
+{
+gnutls_x509_crt_t crt = *(gnutls_x509_crt_t *)cert;
+if (crt)
+ {
+ gnutls_x509_crt_deinit(crt);
+ gnutls_global_deinit();
+ *cert = NULL;
+ }
+}
+
+/*****************************************************
+* 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;
+size_t len = 32;
+
+if (mod && Ustrcmp(mod, "int") == 0)
+ return string_sprintf("%u", (unsigned)t);
+
+cp = store_get(len, GET_UNTAINTED);
+if (f.timestamps_utc)
+ {
+ uschar * tz = to_tz(US"GMT0");
+ len = strftime(CS cp, len, "%b %e %T %Y %Z", gmtime(&t));
+ restore_tz(tz);
+ }
+else
+ len = strftime(CS cp, len, "%b %e %T %Y %Z", localtime(&t));
+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, CS cp, &siz))
+ != GNUTLS_E_SHORT_MEMORY_BUFFER)
+ return g_err("gi0", __FUNCTION__, ret);
+
+cp = store_get(siz, GET_TAINTED);
+if ((ret = gnutls_x509_crt_get_issuer_dn(cert, CS 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];
+uschar * sp = bin;
+size_t sz = sizeof(bin);
+int ret;
+
+if ((ret = gnutls_x509_crt_get_serial((gnutls_x509_crt_t)cert,
+ bin, &sz)))
+ return g_err("gs0", __FUNCTION__, ret);
+
+for(uschar * dp = txt; sz; sz--)
+ dp += sprintf(CS 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 = NULL;
+uschar * cp2;
+uschar * cp3;
+size_t len = 0;
+int ret;
+
+if ((ret = gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, CS cp1, &len))
+ != GNUTLS_E_SHORT_MEMORY_BUFFER)
+ return g_err("gs0", __FUNCTION__, ret);
+
+cp1 = store_get(len*4+1, GET_TAINTED);
+if (gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, CS cp1, &len) != 0)
+ return g_err("gs1", __FUNCTION__, ret);
+
+for(cp3 = cp2 = cp1+len; cp1 < cp2; cp1++)
+ cp3 += sprintf(CS 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(US 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, CS cp, &siz))
+ != GNUTLS_E_SHORT_MEMORY_BUFFER)
+ return g_err("gs0", __FUNCTION__, ret);
+
+cp = store_get(siz, GET_TAINTED);
+if ((ret = gnutls_x509_crt_get_dn(cert, CS 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,
+ CS oid, idx, CS cp1, &siz, &crit);
+if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
+ return g_err("ge0", __FUNCTION__, ret);
+
+cp1 = store_get(siz*4 + 1, GET_TAINTED);
+
+ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
+ CS oid, idx, CS 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; cp1++)
+ cp3 += sprintf(CS cp3, "%.2x ", *cp1);
+cp3[-1]= '\0';
+
+return cp2;
+}
+
+uschar *
+tls_cert_subject_altname(void * cert, uschar * mod)
+{
+gstring * list = NULL;
+size_t siz;
+int ret;
+uschar sep = '\n';
+uschar * tag = US"";
+uschar * ele;
+int match = -1;
+
+if (mod) while (*mod)
+ {
+ if (*mod == '>' && *++mod) sep = *mod++;
+ else if (Ustrncmp(mod, "dns", 3)==0) { match = GNUTLS_SAN_DNSNAME; mod += 3; }
+ else if (Ustrncmp(mod, "uri", 3)==0) { match = GNUTLS_SAN_URI; mod += 3; }
+ else if (Ustrncmp(mod, "mail", 4)==0) { match = GNUTLS_SAN_RFC822NAME; mod += 4; }
+ else break;
+
+ if (*mod++ != ',')
+ break;
+ }
+
+for (int 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 string_from_gstring(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, GET_TAINTED);
+ 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';
+gstring * list = NULL;
+
+if (mod)
+ if (*mod == '>' && *++mod) sep = *mod++;
+
+for (int 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 string_from_gstring(list);
+ if (ret < 0)
+ return g_err("gai", __FUNCTION__, ret);
+
+ list = string_append_listele_n(list, sep, 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;
+uschar sep = '\n';
+gstring * list = NULL;
+uschar * ele;
+
+if (mod)
+ if (*mod == '>' && *++mod) sep = *mod++;
+
+for (int index = 0;; index++)
+ {
+ size_t 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 string_from_gstring(list);
+ case GNUTLS_E_SHORT_MEMORY_BUFFER:
+ break;
+ default:
+ return g_err("gc0", __FUNCTION__, ret);
+ }
+
+ ele = store_get(siz, GET_TAINTED);
+ 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);
+
+ list = string_append_listele_n(list, sep, ele, siz);
+ }
+/*NOTREACHED*/
+}
+
+
+/*****************************************************
+* Certificate operator routines
+*****************************************************/
+uschar *
+tls_cert_der_b64(void * cert)
+{
+size_t len = 0;
+uschar * cp = NULL;
+int fail;
+
+if ( (fail = gnutls_x509_crt_export((gnutls_x509_crt_t)cert,
+ GNUTLS_X509_FMT_DER, cp, &len)) != GNUTLS_E_SHORT_MEMORY_BUFFER
+ || !(cp = store_get((int)len, GET_TAINTED), TRUE) /* tainted */
+ || (fail = gnutls_x509_crt_export((gnutls_x509_crt_t)cert,
+ GNUTLS_X509_FMT_DER, cp, &len))
+ )
+ {
+ log_write(0, LOG_MAIN, "TLS error in certificate export: %s",
+ gnutls_strerror(fail));
+ return NULL;
+ }
+return b64encode(CUS cp, (int)len);
+}
+
+
+static uschar *
+fingerprint(gnutls_x509_crt_t cert, gnutls_digest_algorithm_t algo)
+{
+int ret;
+size_t siz = 0;
+uschar * cp;
+uschar * cp2;
+
+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, GET_TAINTED);
+if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, cp, &siz)) < 0)
+ return g_err("gf1", __FUNCTION__, ret);
+
+for (uschar * cp3 = cp2 = cp+siz; cp < cp2; cp++)
+ cp3 += sprintf(CS 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
index 0000000..168e35b
--- /dev/null
+++ b/src/tlscert-openssl.c
@@ -0,0 +1,532 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* Copyright (c) Jeremy Harris 2014 - 2019 */
+
+/* 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>
+
+#ifdef LIBRESSL_VERSION_NUMBER /* LibreSSL */
+# if LIBRESSL_VERSION_NUMBER >= 0x2090000fL
+# define EXIM_HAVE_ASN1_MACROS
+# endif
+#else /* OpenSSL */
+# if OPENSSL_VERSION_NUMBER >= 0x10100000L
+# define EXIM_HAVE_ASN1_MACROS
+# endif
+#endif
+
+#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
+# define ASN1_STRING_get0_data ASN1_STRING_data
+#endif
+
+/*****************************************************
+* Export/import a certificate, binary/printable
+******************************************************
+Return booolean success
+*/
+BOOL
+tls_export_cert(uschar * buf, size_t buflen, void * cert)
+{
+BIO * bp = BIO_new(BIO_s_mem());
+BOOL 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;
+}
+
+/* On error, NULL out the destination */
+BOOL
+tls_import_cert(const uschar * buf, void ** cert)
+{
+rmark reset_point = store_mark();
+const uschar * cp = string_unprinting(US buf);
+BIO * bp;
+X509 * x = *(X509 **)cert;
+
+if (x) X509_free(x);
+
+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));
+
+*cert = (void *)x;
+BIO_free(bp);
+store_reset(reset_point);
+return !!x;
+}
+
+void
+tls_free_cert(void ** cert)
+{
+X509 * x = *(X509 **)cert;
+if (x)
+ {
+ X509_free(x);
+ *cert = NULL;
+ }
+}
+
+
+/*****************************************************
+* 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 *
+asn1_time_copy(const ASN1_TIME * asntime, uschar * mod)
+{
+uschar * s = NULL;
+BIO * bp = BIO_new(BIO_s_mem());
+int len;
+
+if (!bp)
+ return badalloc();
+len = ASN1_TIME_print(bp, asntime);
+len = len > 0 ? (int) BIO_get_mem_data(bp, CSS &s) : 0;
+
+if (mod && Ustrcmp(mod, "raw") == 0) /* native ASN */
+ s = string_copyn(s, len);
+else
+ {
+ struct tm tm;
+ struct tm * tm_p = &tm;
+ BOOL mod_tz = TRUE;
+ uschar * tz = to_tz(US"GMT0"); /* need to call strptime with baseline TZ */
+
+ /* Parse OpenSSL ASN1_TIME_print output. A shame there seems to
+ be no other interface for the times.
+ */
+
+ /*XXX %Z might be glibc-specific? Solaris has it, at least*/
+ /*XXX should we switch to POSIX locale for this? */
+ tm.tm_isdst = 0;
+ if (!len || !strptime(CCS s, "%b %e %T %Y %Z", &tm))
+ expand_string_message = US"failed time conversion";
+
+ else
+ {
+ time_t t = mktime(&tm); /* make the tm self-consistent */
+
+ if (mod && Ustrcmp(mod, "int") == 0) /* seconds since epoch */
+ s = string_sprintf(TIME_T_FMT, t);
+
+ else
+ {
+ if (!f.timestamps_utc) /* decoded string in local TZ */
+ { /* shift to local TZ */
+ restore_tz(tz);
+ mod_tz = FALSE;
+ tm_p = localtime(&t);
+ }
+ /* "utc" is default, and rfc5280 says cert times should be Zulu */
+
+ /* convert to string in our format */
+ len = 32;
+ s = store_get(len, GET_UNTAINTED);
+ strftime(CS s, (size_t)len, "%b %e %T %Y %z", tm_p);
+ }
+ }
+
+ if (mod_tz)
+ restore_tz(tz);
+ }
+BIO_free(bp);
+return s;
+}
+
+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("%ld", 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();
+
+#ifdef EXIM_HAVE_ASN1_MACROS
+ASN1_STRING_print(bp, adata);
+#else
+M_ASN1_OCTET_STRING_print(bp, adata);
+#endif
+
+/* binary data, DER encoded */
+/* just dump for now */
+len = BIO_get_mem_data(bp, &cp1);
+cp3 = cp2 = store_get(len*3+1, GET_TAINTED);
+
+while(len)
+ {
+ cp2 += sprintf(CS cp2, "%.2x ", *cp1++);
+ len--;
+ }
+cp2[-1] = '\0';
+
+return cp3;
+}
+
+uschar *
+tls_cert_subject_altname(void * cert, uschar * mod)
+{
+gstring * list = NULL;
+STACK_OF(GENERAL_NAME) * san = (STACK_OF(GENERAL_NAME) *)
+ X509_get_ext_d2i((X509 *)cert, NID_subject_alt_name, NULL, NULL);
+uschar osep = '\n';
+uschar * tag = US"";
+uschar * ele;
+int match = -1;
+int len;
+
+if (!san) return NULL;
+
+while (mod && *mod)
+ {
+ if (*mod == '>' && *++mod) osep = *mod++;
+ else if (Ustrncmp(mod,"dns",3)==0) { match = GEN_DNS; mod += 3; }
+ else if (Ustrncmp(mod,"uri",3)==0) { match = GEN_URI; mod += 3; }
+ else if (Ustrncmp(mod,"mail",4)==0) { match = GEN_EMAIL; mod += 4; }
+ else mod++;
+
+ if (*mod == ',') mod++;
+ }
+
+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 = US ASN1_STRING_get0_data(namePart->d.dNSName);
+ len = ASN1_STRING_length(namePart->d.dNSName);
+ break;
+ case GEN_URI:
+ tag = US"URI";
+ ele = US ASN1_STRING_get0_data(namePart->d.uniformResourceIdentifier);
+ len = ASN1_STRING_length(namePart->d.uniformResourceIdentifier);
+ break;
+ case GEN_EMAIL:
+ tag = US"MAIL";
+ ele = US ASN1_STRING_get0_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 (Ustrlen(ele) == len) /* ignore any with embedded nul */
+ list = string_append_listele(list, osep,
+ match == -1 ? string_sprintf("%s=%s", tag, ele) : ele);
+ }
+
+sk_GENERAL_NAME_free(san);
+return string_from_gstring(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);
+uschar sep = '\n';
+gstring * list = NULL;
+
+if (mod)
+ if (*mod == '>' && *++mod) sep = *mod++;
+
+for (int 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_n(list, sep,
+ US ASN1_STRING_get0_data(ad->location->d.ia5),
+ ASN1_STRING_length(ad->location->d.ia5));
+ }
+sk_ACCESS_DESCRIPTION_free(ads);
+return string_from_gstring(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;
+uschar sep = '\n';
+gstring * list = NULL;
+
+if (mod)
+ if (*mod == '>' && *++mod) sep = *mod++;
+
+if (dps) for (int i = 0, dpsnum = sk_DIST_POINT_num(dps); i < dpsnum; i++)
+ if ((dp = sk_DIST_POINT_value(dps, i)))
+ {
+ STACK_OF(GENERAL_NAME) * names = dp->distpoint->name.fullname;
+ GENERAL_NAME * np;
+
+ for (int j = 0, nnum = sk_GENERAL_NAME_num(names); j < nnum; j++)
+ if ( (np = sk_GENERAL_NAME_value(names, j))
+ && np->type == GEN_URI
+ )
+ list = string_append_listele_n(list, sep,
+ US ASN1_STRING_get0_data(np->d.uniformResourceIdentifier),
+ ASN1_STRING_length(np->d.uniformResourceIdentifier));
+ }
+sk_DIST_POINT_free(dps);
+return string_from_gstring(list);
+}
+
+
+
+/*****************************************************
+* Certificate operator routines
+*****************************************************/
+uschar *
+tls_cert_der_b64(void * cert)
+{
+BIO * bp = BIO_new(BIO_s_mem());
+uschar * cp = NULL;
+
+if (!i2d_X509_bio(bp, (X509 *)cert))
+ log_write(0, LOG_MAIN, "TLS error in certificate export: %s",
+ ERR_error_string(ERR_get_error(), NULL));
+else
+ {
+ long len = BIO_get_mem_data(bp, &cp);
+ cp = b64encode(CUS cp, (int)len);
+ }
+
+BIO_free(bp);
+return cp;
+}
+
+
+static uschar *
+fingerprint(X509 * cert, const EVP_MD * fdig)
+{
+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, GET_TAINTED);
+for (int 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
index 0000000..1f0bcc0
--- /dev/null
+++ b/src/tod.c
@@ -0,0 +1,239 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 */
+/* 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.ddd +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)
+{
+struct timeval now;
+struct tm * t;
+
+gettimeofday(&now, NULL);
+
+/* Styles that don't need local time */
+
+switch(type)
+ {
+ case tod_epoch:
+ (void) snprintf(CS timebuf, sizeof(timebuf), TIME_T_FMT, now.tv_sec); /* Unix epoch format */
+ return timebuf; /* NB the above will be wrong if time_t is FP */
+
+ case tod_epoch_l:
+ /* Unix epoch/usec format */
+ (void) snprintf(CS timebuf, sizeof(timebuf), TIME_T_FMT "%06ld", now.tv_sec, (long) now.tv_usec );
+ return timebuf;
+
+ case tod_zulu:
+ t = gmtime(&now.tv_sec);
+ (void) snprintf(CS timebuf, sizeof(timebuf), "%04u%02u%02u%02u%02u%02uZ",
+ 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday, (uint)t->tm_hour, (uint)t->tm_min,
+ (uint)t->tm_sec);
+ return timebuf;
+ }
+
+/* Vary log type according to timezone requirement */
+
+if (type == tod_log) type = log_timezone ? tod_log_zone : tod_log_bare;
+
+/* Convert to local time or UTC */
+
+t = f.timestamps_utc ? gmtime(&now.tv_sec) : localtime(&now.tv_sec);
+
+switch(type)
+ {
+ case tod_log_bare: /* Format used in logging without timezone */
+#ifndef COMPILE_UTILITY
+ if (LOGGING(millisec))
+ snprintf(CS timebuf, sizeof(timebuf), "%04u-%02u-%02u %02u:%02u:%02u.%03u",
+ 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday,
+ (uint)t->tm_hour, (uint)t->tm_min, (uint)t->tm_sec,
+ (uint)(now.tv_usec/1000));
+ else
+#endif
+ snprintf(CS timebuf, sizeof(timebuf), "%04u-%02u-%02u %02u:%02u:%02u",
+ 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday,
+ (uint)t->tm_hour, (uint)t->tm_min, (uint)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:
+ snprintf(CS timebuf, sizeof(timebuf), "%04u%02u%02u%02u%02u",
+ 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday,
+ (uint)t->tm_hour, (uint)t->tm_min);
+ break;
+
+#else
+ case tod_log_datestamp_daily:
+ snprintf(CS timebuf, sizeof(timebuf), "%04u%02u%02u",
+ 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday);
+ break;
+
+ case tod_log_datestamp_monthly:
+#ifndef COMPILE_UTILITY
+ snprintf(CS timebuf, sizeof(timebuf), "%04u%02u",
+ 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon);
+#endif
+ 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;
+ struct tm * lp = &local;
+ memcpy(lp, t, sizeof(struct tm));
+
+ if (f.timestamps_utc)
+ diff_hour = diff_min = 0;
+ else
+ {
+ struct tm * gmt = gmtime(&now.tv_sec);
+
+ if (local.tm_sec == gmt->tm_sec) /* usual case */
+ {
+ 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);
+ }
+ else /* subminute offset, eg. TAI */
+ {
+ lp = gmt; /* pretend we're in UTC */
+ diff_min = diff_hour = 0;
+ }
+ }
+
+ switch(type)
+ {
+ case tod_log_zone: /* Format used in logging with timezone */
+#ifndef COMPILE_UTILITY
+ if (LOGGING(millisec))
+ (void) snprintf(CS timebuf, sizeof(timebuf),
+ "%04u-%02u-%02u %02u:%02u:%02u.%03u %+03d%02d",
+ 1900 + (uint)lp->tm_year, 1 + (uint)lp->tm_mon, (uint)lp->tm_mday,
+ (uint)lp->tm_hour, (uint)lp->tm_min, (uint)lp->tm_sec, (uint)(now.tv_usec/1000),
+ diff_hour, diff_min);
+ else
+#endif
+ (void) snprintf(CS timebuf, sizeof(timebuf),
+ "%04u-%02u-%02u %02u:%02u:%02u %+03d%02d",
+ 1900 + (uint)lp->tm_year, 1 + (uint)lp->tm_mon, (uint)lp->tm_mday,
+ (uint)lp->tm_hour, (uint)lp->tm_min, (uint)lp->tm_sec,
+ diff_hour, diff_min);
+ break;
+
+ case tod_zone: /* Just the timezone offset */
+ (void) snprintf(CS timebuf, sizeof(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 = snprintf(CS timebuf, sizeof(timebuf),
+ "%02u-", (uint)lp->tm_mday);
+ len += Ustrftime(timebuf + len, sizeof(timebuf) - len,
+ "%b-%Y %H:%M:%S", lp);
+ (void) snprintf(CS timebuf + len, sizeof(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, ", lp);
+ len += snprintf(CS timebuf + len, sizeof(timebuf)-len,
+ "%02u ", (uint)lp->tm_mday);
+ len += Ustrftime(timebuf + len, sizeof(timebuf) - len,
+ "%b %Y %H:%M:%S", lp);
+ (void) snprintf(CS timebuf + len, sizeof(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
index 0000000..db00d87
--- /dev/null
+++ b/src/transport-filter.src
@@ -0,0 +1,93 @@
+#! PERL_COMMAND
+
+# 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
+#############################################################################
+
+use warnings;
+BEGIN { pop @INC if $INC[-1] eq '.' };
+use File::Basename;
+
+if ($ARGV[0] eq '--version') {
+ print basename($0) . ": $0\n",
+ "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION\n",
+ "perl(runtime): $]\n";
+ exit 0;
+}
+
+# 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
index 0000000..cce1c46
--- /dev/null
+++ b/src/transport.c
@@ -0,0 +1,2409 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* General functions concerned with transportation, and generic options for all
+transports. */
+
+
+#include "exim.h"
+
+/* 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. */
+#define LOFF(field) OPT_OFF(transport_instance, field)
+
+optionlist optionlist_transports[] = {
+ /* name type value */
+ { "*expand_group", opt_stringptr|opt_hidden|opt_public,
+ LOFF(expand_gid) },
+ { "*expand_user", opt_stringptr|opt_hidden|opt_public,
+ LOFF(expand_uid) },
+ { "*headers_rewrite_flags", opt_int|opt_public|opt_hidden,
+ LOFF(rewrite_existflags) },
+ { "*headers_rewrite_rules", opt_void|opt_public|opt_hidden,
+ LOFF(rewrite_rules) },
+ { "*set_group", opt_bool|opt_hidden|opt_public,
+ LOFF(gid_set) },
+ { "*set_user", opt_bool|opt_hidden|opt_public,
+ LOFF(uid_set) },
+ { "body_only", opt_bool|opt_public,
+ LOFF(body_only) },
+ { "current_directory", opt_stringptr|opt_public,
+ LOFF(current_dir) },
+ { "debug_print", opt_stringptr | opt_public,
+ LOFF(debug_string) },
+ { "delivery_date_add", opt_bool|opt_public,
+ LOFF(delivery_date_add) },
+ { "disable_logging", opt_bool|opt_public,
+ LOFF(disable_logging) },
+ { "driver", opt_stringptr|opt_public,
+ LOFF(driver_name) },
+ { "envelope_to_add", opt_bool|opt_public,
+ LOFF(envelope_to_add) },
+#ifndef DISABLE_EVENT
+ { "event_action", opt_stringptr | opt_public,
+ LOFF(event_action) },
+#endif
+ { "group", opt_expand_gid|opt_public,
+ LOFF(gid) },
+ { "headers_add", opt_stringptr|opt_public|opt_rep_str,
+ LOFF(add_headers) },
+ { "headers_only", opt_bool|opt_public,
+ LOFF(headers_only) },
+ { "headers_remove", opt_stringptr|opt_public|opt_rep_str,
+ LOFF(remove_headers) },
+ { "headers_rewrite", opt_rewrite|opt_public,
+ LOFF(headers_rewrite) },
+ { "home_directory", opt_stringptr|opt_public,
+ LOFF(home_dir) },
+ { "initgroups", opt_bool|opt_public,
+ LOFF(initgroups) },
+ { "max_parallel", opt_stringptr|opt_public,
+ LOFF(max_parallel) },
+ { "message_size_limit", opt_stringptr|opt_public,
+ LOFF(message_size_limit) },
+ { "rcpt_include_affixes", opt_bool|opt_public,
+ LOFF(rcpt_include_affixes) },
+ { "retry_use_local_part", opt_bool|opt_public,
+ LOFF(retry_use_local_part) },
+ { "return_path", opt_stringptr|opt_public,
+ LOFF(return_path) },
+ { "return_path_add", opt_bool|opt_public,
+ LOFF(return_path_add) },
+ { "shadow_condition", opt_stringptr|opt_public,
+ LOFF(shadow_condition) },
+ { "shadow_transport", opt_stringptr|opt_public,
+ LOFF(shadow) },
+ { "transport_filter", opt_stringptr|opt_public,
+ LOFF(filter_command) },
+ { "transport_filter_timeout", opt_time|opt_public,
+ LOFF(filter_timeout) },
+ { "user", opt_expand_uid|opt_public,
+ LOFF(uid) }
+};
+
+int optionlist_transports_size = nelem(optionlist_transports);
+
+#ifdef MACRO_PREDEF
+
+# include "macro_predef.h"
+
+void
+options_transports(void)
+{
+uschar buf[64];
+
+options_from_list(optionlist_transports, nelem(optionlist_transports), US"TRANSPORTS", NULL);
+
+for (transport_info * ti = transports_available; ti->driver_name[0]; ti++)
+ {
+ spf(buf, sizeof(buf), US"_DRIVER_TRANSPORT_%T", ti->driver_name);
+ builtin_macro_create(buf);
+ options_from_list(ti->options, (unsigned)*ti->options_count, US"TRANSPORT", ti->driver_name);
+ }
+}
+
+#else /*!MACRO_PREDEF*/
+
+/* 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 */
+
+
+/*************************************************
+* 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)
+{
+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 (transport_instance * t = transports; t; t = t->next)
+ {
+ if (!t->info->local && t->shadow)
+ 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 *
+*************************************************/
+
+static int
+tpt_write(int fd, uschar * block, int len, BOOL more, int options)
+{
+return
+#ifndef DISABLE_TLS
+ tls_out.active.sock == fd
+ ? tls_write(tls_out.active.tls_ctx, block, len, more) :
+#endif
+#ifdef MSG_MORE
+ more && !(options & topt_not_socket) ? send(fd, block, len, MSG_MORE) :
+#endif
+ write(fd, block, len);
+}
+
+/* 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:
+ tctx transport context: file descriptor or string to write to
+ block block of bytes to write
+ len number of bytes to write
+ more further data expected soon
+
+Returns: TRUE on success, FALSE on failure (with errno preserved);
+ transport_count is incremented by the number of bytes written
+*/
+
+static BOOL
+transport_write_block_fd(transport_ctx * tctx, uschar * block, int len, BOOL more)
+{
+int rc, save_errno;
+int local_timeout = transport_write_timeout;
+int connretry = 1;
+int fd = tctx->u.fd;
+
+/* This loop is for handling incomplete writes and other retries. In most
+normal cases, it is only ever executed once. */
+
+for (int i = 0; i < 100; i++)
+ {
+ DEBUG(D_transport)
+ debug_printf("writing data block fd=%d size=%d timeout=%d%s\n",
+ fd, len, local_timeout, more ? " (more expected)" : "");
+
+ /* When doing TCP Fast Open we may get this far before the 3-way handshake
+ is complete, and write returns ENOTCONN. Detect that, wait for the socket
+ to become writable, and retry once only. */
+
+ for(;;)
+ {
+ /* 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 */
+ {
+ rc = tpt_write(fd, block, len, more, tctx->options);
+ save_errno = errno;
+ }
+ else /* Timeout wanted. */
+ {
+ sigalrm_seen = FALSE;
+ ALARM(local_timeout);
+ rc = tpt_write(fd, block, len, more, tctx->options);
+ save_errno = errno;
+ local_timeout = ALARM_CLR(0);
+ if (sigalrm_seen)
+ {
+ errno = ETIMEDOUT;
+ return FALSE;
+ }
+ }
+
+ if (rc >= 0 || errno != ENOTCONN || connretry <= 0)
+ break;
+
+ poll_one_fd(fd, POLLOUT, -1); /* could set timeout? retval check? */
+ connretry--;
+ }
+
+ /* 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;
+}
+
+
+BOOL
+transport_write_block(transport_ctx * tctx, uschar *block, int len, BOOL more)
+{
+if (!(tctx->options & topt_output_string))
+ return transport_write_block_fd(tctx, block, len, more);
+
+/* Write to expanding-string. NOTE: not NUL-terminated */
+
+if (!tctx->u.msg)
+ tctx->u.msg = string_get(1024);
+
+tctx->u.msg = string_catn(tctx->u.msg, block, len);
+return TRUE;
+}
+
+
+
+
+/*************************************************
+* 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, ...)
+{
+transport_ctx tctx = {{0}};
+gstring gs = { .size = big_buffer_size, .ptr = 0, .s = big_buffer };
+va_list ap;
+
+/* Use taint-unchecked routines for writing into big_buffer, trusting
+that the result will never be expanded. */
+
+va_start(ap, format);
+if (!string_vformat(&gs, SVFMT_TAINT_NOCHK, format, ap))
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "overlong formatted string in transport");
+va_end(ap);
+tctx.u.fd = fd;
+return transport_write_block(&tctx, gs.s, gs.ptr, FALSE);
+}
+
+
+
+
+void
+transport_write_reset(int options)
+{
+if (!(options & topt_continuation)) chunk_ptr = deliver_out_buffer;
+nl_partial_match = -1;
+nl_check_length = nl_escape_length = 0;
+}
+
+
+
+/*************************************************
+* 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:
+ tctx transport context - processing to be done during output,
+ and file descriptor to write to
+ chunk pointer to data to write
+ len length of data to write
+
+In addition, the static nl_xxx variables must be set as required.
+
+Returns: TRUE on success, FALSE on failure (with errno preserved)
+*/
+
+BOOL
+write_chunk(transport_ctx * tctx, uschar *chunk, int len)
+{
+uschar *start = chunk;
+uschar *end = chunk + len;
+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 (uschar * ptr = start; ptr < end; ptr++)
+ {
+ int ch, len;
+
+ /* 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 ((len = chunk_ptr - deliver_out_buffer) > mlen)
+ {
+ DEBUG(D_transport) debug_printf("flushing headers buffer\n");
+
+ /* If CHUNKING, prefix with BDAT (size) NON-LAST. Also, reap responses
+ from previous SMTP commands. */
+
+ if (tctx->options & topt_use_bdat && tctx->chunk_cb)
+ {
+ if ( tctx->chunk_cb(tctx, (unsigned)len, 0) != OK
+ || !transport_write_block(tctx, deliver_out_buffer, len, FALSE)
+ || tctx->chunk_cb(tctx, 0, tc_reap_prev) != OK
+ )
+ return FALSE;
+ }
+ else
+ if (!transport_write_block(tctx, deliver_out_buffer, len, FALSE))
+ return FALSE;
+ chunk_ptr = deliver_out_buffer;
+ }
+
+ /* Remove CR before NL if required */
+
+ if ( *ptr == '\r' && ptr[1] == '\n'
+ && !(tctx->options & topt_use_crlf)
+ && f.spool_file_wireformat
+ )
+ ptr++;
+
+ if ((ch = *ptr) == '\n')
+ {
+ int left = end - ptr - 1; /* count of chars left after NL */
+
+ /* Insert CR before NL if required */
+
+ if (tctx->options & topt_use_crlf && !f.spool_file_wireformat)
+ *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)
+ {
+ if (!addr->prefix) return addr->address;
+ return addr->address + Ustrlen(addr->prefix);
+ }
+
+at = Ustrrchr(addr->address, '@');
+plen = addr->prefix ? Ustrlen(addr->prefix) : 0;
+slen = Ustrlen(addr->suffix);
+
+return string_sprintf("%.*s@%s", (int)(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
+ tctx transport context - processing to be done during output
+ and the file descriptor to write to
+
+Returns: FALSE if writing failed
+*/
+
+static BOOL
+write_env_to(address_item *p, struct aci **pplist, struct aci **pdlist,
+ BOOL *first, transport_ctx * tctx)
+{
+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; ppp = ppp->next) if (p == ppp->ptr) return TRUE;
+
+ppp = store_get(sizeof(struct aci), GET_UNTAINTED);
+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; dup = dup->next)
+ if (dup->dupof == pp) /* a dup of our address */
+ if (!write_env_to(dup, pplist, pdlist, first, tctx))
+ return FALSE;
+ if (!pp->parent) break;
+ }
+
+/* Check to see if we have already output the progenitor. */
+
+for (ppp = *pplist; ppp; ppp = ppp->next) if (pp == ppp->ptr) break;
+if (ppp) return TRUE;
+
+/* Remember what we have output, and output it. */
+
+ppp = store_get(sizeof(struct aci), GET_UNTAINTED);
+ppp->next = *pplist;
+*pplist = ppp;
+ppp->ptr = pp;
+
+if (!*first && !write_chunk(tctx, US",\n ", 3)) return FALSE;
+*first = FALSE;
+return write_chunk(tctx, pp->address, Ustrlen(pp->address));
+}
+
+
+
+
+/* Add/remove/rewrite headers, and send them plus the empty-line separator.
+
+Globals:
+ header_list
+
+Arguments:
+ addr (chain of) addresses (for extra headers), or NULL;
+ only the first address is used
+ tctx transport context
+ sendfn function for output (transport or verify)
+
+Returns: TRUE on success; FALSE on failure.
+*/
+BOOL
+transport_headers_send(transport_ctx * tctx,
+ BOOL (*sendfn)(transport_ctx * tctx, uschar * s, int len))
+{
+const uschar *list;
+transport_instance * tblock = tctx ? tctx->tblock : NULL;
+address_item * addr = tctx ? tctx->addr : NULL;
+
+/* 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->prop.remove_headers too, provided that addr is not NULL. */
+
+for (header_line * h = header_list; h; h = h->next) if (h->type != htype_old)
+ {
+ BOOL include_header = TRUE;
+
+ list = tblock ? tblock->remove_headers : NULL;
+ for (int i = 0; i < 2; i++) /* For remove_headers && addr->prop.remove_headers */
+ {
+ if (list)
+ {
+ int sep = ':'; /* This is specified as a colon-separated list */
+ uschar *s, *ss;
+ while ((s = string_nextinlist(&list, &sep, NULL, 0)))
+ {
+ int len;
+
+ if (i == 0)
+ if (!(s = expand_string(s)) && !f.expand_string_forcedfail)
+ {
+ errno = ERRNO_CHHEADER_FAIL;
+ return FALSE;
+ }
+ len = s ? Ustrlen(s) : 0;
+ if (len && s[len-1] == '*') /* trailing glob */
+ {
+ if (strncmpic(h->text, s, len-1) == 0) break;
+ }
+ else
+ {
+ if (strncmpic(h->text, s, len) != 0) continue;
+ ss = h->text + len;
+ while (*ss == ' ' || *ss == '\t') ss++;
+ if (*ss == ':') break;
+ }
+ }
+ if (s) { include_header = FALSE; break; }
+ }
+ if (addr) list = addr->prop.remove_headers;
+ }
+
+ /* If this header is to be output, try to rewrite it if there are rewriting
+ rules. */
+
+ if (include_header)
+ {
+ if (tblock && tblock->rewrite_rules)
+ {
+ rmark reset_point = store_mark();
+ header_line *hh;
+
+ if ((hh = rewrite_header(h, NULL, NULL, tblock->rewrite_rules,
+ tblock->rewrite_existflags, FALSE)))
+ {
+ if (!sendfn(tctx, hh->text, hh->slen)) return FALSE;
+ store_reset(reset_point);
+ continue; /* With the next header line */
+ }
+ }
+
+ /* Either no rewriting rules, or it didn't get rewritten */
+
+ if (!sendfn(tctx, h->text, h->slen)) 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)
+ {
+ header_line * hprev = addr->prop.extra_headers, * hnext, * h;
+
+ for (int i = 0; i < 2; i++)
+ for (h = hprev, hprev = NULL; h; h = hnext)
+ {
+ hnext = h->next;
+ h->next = hprev;
+ hprev = h;
+ if (i == 1)
+ {
+ if (!sendfn(tctx, h->text, h->slen)) 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 (tblock && (list = CUS tblock->add_headers))
+ {
+ int sep = '\n';
+ uschar * s;
+
+ while ((s = string_nextinlist(&list, &sep, NULL, 0)))
+ if ((s = expand_string(s)))
+ {
+ int len = Ustrlen(s);
+ if (len > 0)
+ {
+ if (!sendfn(tctx, s, len)) return FALSE;
+ if (s[len-1] != '\n' && !sendfn(tctx, US"\n", 1))
+ return FALSE;
+ DEBUG(D_transport)
+ {
+ debug_printf("added header line:\n %s", s);
+ if (s[len-1] != '\n') debug_printf("\n");
+ debug_printf("---\n");
+ }
+ }
+ }
+ else if (!f.expand_string_forcedfail)
+ { errno = ERRNO_CHHEADER_FAIL; return FALSE; }
+ }
+
+/* Separate headers from body with a blank line */
+
+return sendfn(tctx, US"\n", 1);
+}
+
+
+/*************************************************
+* 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:
+ tctx
+ (fd, msg) Either an fd, to write the message to,
+ or a string: if null write message to allocated space
+ otherwire take content as headers.
+ addr (chain of) addresses (for extra headers), or NULL;
+ only the first address is used
+ tblock optional transport instance block (NULL signifies NULL/0):
+ 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
+ rewrite_rules chain of header rewriting rules
+ rewrite_existflags flags for the rewriting rules
+ 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_flush if TRUE, do not flush at end
+ no_headers if TRUE, omit the headers
+ no_body if TRUE, omit the body
+ 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
+ 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
+
+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(transport_ctx * tctx, int size_limit)
+{
+int len, size = 0;
+
+/* Initialize pointer in output buffer. */
+
+transport_write_reset(tctx->options);
+
+/* Set up the data for start-of-line data checking and escaping */
+
+if (tctx->check_string && tctx->escape_string)
+ {
+ nl_check = tctx->check_string;
+ nl_check_length = Ustrlen(nl_check);
+ nl_escape = tctx->escape_string;
+ nl_escape_length = Ustrlen(nl_escape);
+ }
+
+/* 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 (!(tctx->options & topt_escape_headers))
+ 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. The datasource is not the -D spoolfile
+so temporarily hide the global that adjusts for its format. */
+
+if (!(tctx->options & topt_no_headers))
+ {
+ BOOL save_wireformat = f.spool_file_wireformat;
+ f.spool_file_wireformat = FALSE;
+
+ /* Add return-path: if requested. */
+
+ if (tctx->options & topt_add_return_path)
+ {
+ int n;
+ uschar * s = string_sprintf("Return-path: <%.*s>\n%n",
+ EXIM_EMAILADDR_MAX, return_path, &n);
+ if (!write_chunk(tctx, s, n)) goto bad;
+ }
+
+ /* Add envelope-to: if requested */
+
+ if (tctx->options & topt_add_envelope_to)
+ {
+ BOOL first = TRUE;
+ struct aci *plist = NULL;
+ struct aci *dlist = NULL;
+ rmark reset_point = store_mark();
+
+ if (!write_chunk(tctx, US"Envelope-to: ", 13)) goto bad;
+
+ /* 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 because write_env_to() calls itself recursively. */
+
+ for (address_item * p = tctx->addr; p; p = p->next)
+ if (!write_env_to(p, &plist, &dlist, &first, tctx))
+ goto bad;
+
+ /* Add a final newline and reset the store used for tracking duplicates */
+
+ if (!write_chunk(tctx, US"\n", 1)) goto bad;
+ store_reset(reset_point);
+ }
+
+ /* Add delivery-date: if requested. */
+
+ if (tctx->options & topt_add_delivery_date)
+ {
+ uschar * s = tod_stamp(tod_full);
+
+ if ( !write_chunk(tctx, US"Delivery-date: ", 15)
+ || !write_chunk(tctx, s, Ustrlen(s))
+ || !write_chunk(tctx, US"\n", 1)) goto bad;
+ }
+
+ /* 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->prop.remove_headers too, provided that
+ addr is not NULL. */
+
+ if (!transport_headers_send(tctx, &write_chunk))
+ {
+bad:
+ f.spool_file_wireformat = save_wireformat;
+ return FALSE;
+ }
+
+ f.spool_file_wireformat = save_wireformat;
+ }
+
+/* When doing RFC3030 CHUNKING output, work out how much data would be in a
+last-BDAT, consisting of the current write_chunk() output buffer fill
+(optimally, all of the headers - but it does not matter if we already had to
+flush that buffer with non-last BDAT prependix) plus the amount of body data
+(as expanded for CRLF lines). Then create and write BDAT(s), and ensure
+that further use of write_chunk() will not prepend BDATs.
+The first BDAT written will also first flush any outstanding MAIL and RCPT
+commands which were buffered thans to PIPELINING.
+Commands go out (using a send()) from a different buffer to data (using a
+write()). They might not end up in the same TCP segment, which is
+suboptimal. */
+
+if (tctx->options & topt_use_bdat)
+ {
+ off_t fsize;
+ int hsize;
+
+ if ((hsize = chunk_ptr - deliver_out_buffer) < 0)
+ hsize = 0;
+ if (!(tctx->options & topt_no_body))
+ {
+ if ((fsize = lseek(deliver_datafile, 0, SEEK_END)) < 0) return FALSE;
+ fsize -= SPOOL_DATA_START_OFFSET;
+ if (size_limit > 0 && fsize > size_limit)
+ fsize = size_limit;
+ size = hsize + fsize;
+ if (tctx->options & topt_use_crlf && !f.spool_file_wireformat)
+ size += body_linecount; /* account for CRLF-expansion */
+
+ /* With topt_use_bdat we never do dot-stuffing; no need to
+ account for any expansion due to that. */
+ }
+
+ /* If the message is large, emit first a non-LAST chunk with just the
+ headers, and reap the command responses. This lets us error out early
+ on RCPT rejects rather than sending megabytes of data. Include headers
+ on the assumption they are cheap enough and some clever implementations
+ might errorcheck them too, on-the-fly, and reject that chunk. */
+
+ if (size > DELIVER_OUT_BUFFER_SIZE && hsize > 0)
+ {
+ DEBUG(D_transport)
+ debug_printf("sending small initial BDAT; hsize=%d\n", hsize);
+ if ( tctx->chunk_cb(tctx, hsize, 0) != OK
+ || !transport_write_block(tctx, deliver_out_buffer, hsize, FALSE)
+ || tctx->chunk_cb(tctx, 0, tc_reap_prev) != OK
+ )
+ return FALSE;
+ chunk_ptr = deliver_out_buffer;
+ size -= hsize;
+ }
+
+ /* Emit a LAST datachunk command, and unmark the context for further
+ BDAT commands. */
+
+ if (tctx->chunk_cb(tctx, size, tc_chunk_last) != OK)
+ return FALSE;
+ tctx->options &= ~topt_use_bdat;
+ }
+
+/* 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 we have a wireformat -D file (CRNL lines, non-dotstuffed, no ending dot)
+and we want to send a body without dotstuffing or ending-dot, in-clear,
+then we can just dump it using sendfile.
+This should get used for CHUNKING output and also for writing the -K file for
+dkim signing, when we had CHUNKING input. */
+
+#ifdef OS_SENDFILE
+if ( f.spool_file_wireformat
+ && !(tctx->options & (topt_no_body | topt_end_dot))
+ && !nl_check_length
+ && tls_out.active.sock != tctx->u.fd
+ )
+ {
+ ssize_t copied = 0;
+ off_t offset = SPOOL_DATA_START_OFFSET;
+
+ /* Write out any header data in the buffer */
+
+ if ((len = chunk_ptr - deliver_out_buffer) > 0)
+ {
+ if (!transport_write_block(tctx, deliver_out_buffer, len, TRUE))
+ return FALSE;
+ size -= len;
+ }
+
+ DEBUG(D_transport) debug_printf("using sendfile for body\n");
+
+ while(size > 0)
+ {
+ if ((copied = os_sendfile(tctx->u.fd, deliver_datafile, &offset, size)) <= 0) break;
+ size -= copied;
+ }
+ return copied >= 0;
+ }
+#else
+DEBUG(D_transport) debug_printf("cannot use sendfile for body: no support\n");
+#endif
+
+DEBUG(D_transport)
+ if (!(tctx->options & topt_no_body))
+ debug_printf("cannot use sendfile for body: %s\n",
+ !f.spool_file_wireformat ? "spoolfile not wireformat"
+ : tctx->options & topt_end_dot ? "terminating dot wanted"
+ : nl_check_length ? "dot- or From-stuffing wanted"
+ : "TLS output wanted");
+
+if (!(tctx->options & topt_no_body))
+ {
+ unsigned long size = size_limit > 0 ? size_limit : ULONG_MAX;
+
+ nl_check_length = abs(nl_check_length);
+ nl_partial_match = 0;
+ if (lseek(deliver_datafile, SPOOL_DATA_START_OFFSET, SEEK_SET) < 0)
+ return FALSE;
+ while ( (len = MIN(DELIVER_IN_BUFFER_SIZE, size)) > 0
+ && (len = read(deliver_datafile, deliver_in_buffer, len)) > 0)
+ {
+ if (!write_chunk(tctx, deliver_in_buffer, len))
+ return FALSE;
+ size -= len;
+ }
+
+ /* A read error on the body will have left len == -1 and errno set. */
+
+ if (len != 0) return FALSE;
+ }
+
+/* Finished with the check string, and spool-format consideration */
+
+nl_check_length = nl_escape_length = 0;
+f.spool_file_wireformat = FALSE;
+
+/* If requested, add a terminating "." line (SMTP output). */
+
+if (tctx->options & topt_end_dot)
+ {
+ smtp_debug_cmd(US".", 0);
+ if (!write_chunk(tctx, US".\n", 2))
+ return FALSE;
+ }
+
+/* Write out any remaining data in the buffer before returning. */
+
+return (len = chunk_ptr - deliver_out_buffer) <= 0
+ || transport_write_block(tctx, deliver_out_buffer, len,
+ !!(tctx->options & topt_no_flush));
+}
+
+
+
+
+/*************************************************
+* 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 fd in the transport context. 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(transport_ctx * tctx, int size_limit)
+{
+BOOL last_filter_was_NL = TRUE;
+BOOL save_spool_file_wireformat = f.spool_file_wireformat;
+BOOL yield;
+int rc, len, fd_read, fd_write, save_errno;
+int pfd[2] = {-1, -1};
+pid_t filter_pid, write_pid;
+
+f.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
+ || !*transport_filter_argv
+ || !**transport_filter_argv
+ )
+ return internal_transport_write_message(tctx, size_limit);
+
+/* 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. */
+
+nl_partial_match = -1;
+
+if (tctx->check_string && tctx->escape_string)
+ {
+ nl_check = tctx->check_string;
+ nl_check_length = Ustrlen(nl_check);
+ nl_escape = tctx->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);
+
+ {
+ int bits = fcntl(tctx->u.fd, F_GETFD);
+ (void) fcntl(tctx->u.fd, F_SETFD, bits | FD_CLOEXEC);
+ filter_pid = child_open(USS transport_filter_argv, NULL, 077,
+ &fd_write, &fd_read, FALSE, US"transport-filter");
+ (void) fcntl(tctx->u.fd, F_SETFD, bits & ~FD_CLOEXEC);
+ }
+if (filter_pid < 0) goto TIDY_UP; /* errno set */
+
+DEBUG(D_transport)
+ debug_printf("process %d running as transport filter: fd_write=%d fd_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 = exim_fork(US"tpt-filter-writer")) == 0)
+ {
+ BOOL rc;
+ (void)close(fd_read);
+ (void)close(pfd[pipe_read]);
+ nl_check_length = nl_escape_length = 0;
+
+ tctx->u.fd = fd_write;
+ tctx->check_string = tctx->escape_string = NULL;
+ tctx->options &= ~(topt_use_crlf | topt_end_dot | topt_use_bdat | topt_no_flush);
+
+ rc = internal_transport_write_message(tctx, size_limit);
+
+ 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 *)&tctx->addr->more_errno, sizeof(int))
+ != sizeof(int)
+ || write(pfd[pipe_write], (void *)&tctx->addr->delivery_time, sizeof(struct timeval))
+ != sizeof(struct timeval)
+ )
+ rc = FALSE; /* compiler quietening */
+ exim_underbar_exit(EXIT_SUCCESS);
+ }
+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 */
+
+testharness_pause_ms(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). The output should always be unix-format as we converted
+any wireformat source on writing input to the filter. */
+
+f.spool_file_wireformat = FALSE;
+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_CLR(0);
+ if (sigalrm_seen)
+ {
+ DEBUG(D_transport) debug_printf("timed out reading from filter\n");
+ errno = ETIMEDOUT;
+ f.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(tctx, deliver_in_buffer, len)) 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:
+f.spool_file_wireformat = save_spool_file_wireformat;
+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;
+ tctx->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;
+ if (read(pfd[pipe_read], (void *)&ok, sizeof(BOOL)) != sizeof(BOOL))
+ {
+ DEBUG(D_transport)
+ debug_printf("pipe read from writing process: %s\n", strerror(errno));
+ save_errno = ERRNO_FILTER_FAIL;
+ yield = FALSE;
+ }
+ else if (!ok)
+ { /* Try to drain the pipe; read fails are don't care */
+ int dummy = read(pfd[pipe_read], (void *)&save_errno, sizeof(int));
+ dummy = read(pfd[pipe_read], (void *)&tctx->addr->more_errno, sizeof(int));
+ dummy = read(pfd[pipe_read], (void *)&tctx->addr->delivery_time, sizeof(struct timeval));
+ yield = FALSE;
+ }
+ }
+ else
+ {
+ yield = FALSE;
+ save_errno = ERRNO_FILTER_FAIL;
+ tctx->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;
+ f.spool_file_wireformat = FALSE;
+ if ( tctx->options & topt_end_dot
+ && ( last_filter_was_NL
+ ? !write_chunk(tctx, US".\n", 2)
+ : !write_chunk(tctx, US"\n.\n", 3)
+ ) )
+ { smtp_debug_cmd(US".", 0); yield = FALSE; }
+
+ /* Write out any remaining data in the buffer. */
+
+ else
+ yield = (len = chunk_ptr - deliver_out_buffer) <= 0
+ || transport_write_block(tctx, deliver_out_buffer, len, FALSE);
+ }
+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, tctx->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)
+{
+const uschar *prevname = US"";
+open_db dbblock;
+open_db *dbm_file;
+
+DEBUG(D_transport) debug_printf("updating wait-%s database\n", tpname);
+
+/* Open the database for this transport */
+
+if (!(dbm_file = dbfn_open(string_sprintf("wait-%.200s", tpname),
+ O_RDWR, &dbblock, TRUE, TRUE)))
+ 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_item * host = hostlist; host; host = host->next)
+ {
+ BOOL already = FALSE;
+ dbdata_wait *host_record;
+ int host_length;
+ uschar buffer[256];
+
+ /* 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. */
+
+ if (!(host_record = dbfn_read(dbm_file, host->name)))
+ {
+ host_record = store_get(sizeof(dbdata_wait) + MESSAGE_ID_LENGTH, GET_UNTAINTED);
+ 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 (uschar * 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 (int i = host_record->sequence - 1; i >= 0 && !already; i--)
+ {
+ dbdata_wait *cont;
+ sprintf(CS buffer, "%.200s:%d", host->name, i);
+ if ((cont = dbfn_read(dbm_file, buffer)))
+ {
+ int clen = cont->count * MESSAGE_ID_LENGTH;
+ for (uschar * 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 we're doing a two-phase queue run initial phase, ping the
+ daemon to consider running a delivery on this host. */
+
+ 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);
+#ifndef DISABLE_QUEUE_RAMP
+ if (f.queue_2stage && queue_fast_ramp && !queue_run_in_order)
+ queue_notify_daemon(message_id);
+#endif
+ 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, GET_UNTAINTED);
+ 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 %.*s to queue for %s\n",
+ MESSAGE_ID_LENGTH, message_id, 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
+ oicf_func function to call to validate if it is ok to send
+ to this message_id from the current instance.
+ oicf_data opaque data for oicf_func
+
+Returns: TRUE if new_message_id set; FALSE otherwise
+*/
+
+typedef struct msgq_s
+{
+ uschar message_id [MESSAGE_ID_LENGTH + 1];
+ BOOL bKeep;
+} msgq_t;
+
+BOOL
+transport_check_waiting(const uschar *transport_name, const uschar *hostname,
+ int local_message_max, uschar *new_message_id, oicf oicf_func, void *oicf_data)
+{
+dbdata_wait *host_record;
+int host_length;
+open_db dbblock;
+open_db *dbm_file;
+
+int i;
+struct stat statbuf;
+
+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);
+ acl_level++;
+ }
+
+/* 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_indent("max messages for one connection reached: returning\n");
+ goto retfalse;
+ }
+
+/* Open the waiting information database. */
+
+if (!(dbm_file = dbfn_open(string_sprintf("wait-%.200s", transport_name),
+ O_RDWR, &dbblock, TRUE, TRUE)))
+ goto retfalse;
+
+/* See if there is a record for this host; if not, there's nothing to do. */
+
+if (!(host_record = dbfn_read(dbm_file, hostname)))
+ {
+ dbfn_close(dbm_file);
+ DEBUG(D_transport) debug_printf_indent("no messages waiting for %s\n", hostname);
+ goto retfalse;
+ }
+
+/* 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);
+ goto retfalse;
+ }
+
+/* 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.
+*/
+
+/* For Bug 1141, I refactored this major portion of the routine, it is risky
+but the 1 off will remain without it. This code now allows me to SKIP over
+a message I do not want to send out on this run. */
+
+host_length = host_record->count * MESSAGE_ID_LENGTH;
+
+while (1)
+ {
+ msgq_t *msgq;
+ int msgq_count = 0;
+ int msgq_actual = 0;
+ BOOL bFound = FALSE;
+ BOOL bContinuation = FALSE;
+
+ /* create an array to read entire message queue into memory for processing */
+
+ msgq = store_get(sizeof(msgq_t) * host_record->count, GET_UNTAINTED);
+ msgq_count = host_record->count;
+ msgq_actual = msgq_count;
+
+ for (i = 0; i < host_record->count; ++i)
+ {
+ msgq[i].bKeep = TRUE;
+
+ Ustrncpy_nt(msgq[i].message_id, host_record->text + (i * MESSAGE_ID_LENGTH),
+ MESSAGE_ID_LENGTH);
+ msgq[i].message_id[MESSAGE_ID_LENGTH] = 0;
+ }
+
+ /* first thing remove current message id if it exists */
+ /*XXX but what if it has un-sent addrs? */
+
+ for (i = 0; i < msgq_count; ++i)
+ if (Ustrcmp(msgq[i].message_id, message_id) == 0)
+ {
+ msgq[i].bKeep = FALSE;
+ break;
+ }
+
+ /* now find the next acceptable message_id */
+
+ for (i = msgq_count - 1; i >= 0; --i) if (msgq[i].bKeep)
+ {
+ uschar subdir[2];
+ uschar * mid = msgq[i].message_id;
+
+ set_subdir_str(subdir, mid, 0);
+ if (Ustat(spool_fname(US"input", subdir, mid, US"-D"), &statbuf) != 0)
+ msgq[i].bKeep = FALSE;
+ else if (!oicf_func || oicf_func(mid, oicf_data))
+ {
+ Ustrcpy_nt(new_message_id, mid);
+ msgq[i].bKeep = FALSE;
+ bFound = TRUE;
+ break;
+ }
+ }
+
+ /* re-count */
+ for (msgq_actual = 0, i = 0; i < msgq_count; ++i)
+ if (msgq[i].bKeep)
+ msgq_actual++;
+
+ /* reassemble the host record, based on removed message ids, from in
+ memory queue */
+
+ if (msgq_actual <= 0)
+ {
+ host_length = 0;
+ host_record->count = 0;
+ }
+ else
+ {
+ host_length = msgq_actual * MESSAGE_ID_LENGTH;
+ host_record->count = msgq_actual;
+
+ if (msgq_actual < msgq_count)
+ {
+ int new_count;
+ for (new_count = 0, i = 0; i < msgq_count; ++i)
+ if (msgq[i].bKeep)
+ Ustrncpy(&host_record->text[new_count++ * MESSAGE_ID_LENGTH],
+ msgq[i].message_id, MESSAGE_ID_LENGTH);
+
+ host_record->text[new_count * MESSAGE_ID_LENGTH] = 0;
+ }
+ }
+
+ /* Check for a continuation record. */
+
+ while (host_length <= 0)
+ {
+ dbdata_wait * newr = NULL;
+ uschar buffer[256];
+
+ /* Search for a continuation */
+
+ for (int i = host_record->sequence - 1; i >= 0 && !newr; 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)
+ {
+ 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;
+
+ bContinuation = TRUE;
+ }
+
+ if (bFound) /* Usual exit from main loop */
+ 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_indent("waiting messages already delivered\n");
+ goto retfalse;
+ }
+
+ /* we were not able to find an acceptable message, nor was there a
+ * continuation record. So bug out, outer logic will clean this up.
+ */
+
+ if (!bContinuation)
+ {
+ Ustrcpy(new_message_id, message_id);
+ dbfn_close(dbm_file);
+ goto retfalse;
+ }
+ } /* we need to process a continuation record */
+
+/* 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);
+ }
+
+dbfn_close(dbm_file);
+DEBUG(D_transport) {acl_level--; debug_printf("transport_check_waiting: TRUE\n"); }
+return TRUE;
+
+retfalse:
+DEBUG(D_transport) {acl_level--; debug_printf("transport_check_waiting: FALSE\n"); }
+return FALSE;
+}
+
+/*************************************************
+* Deliver waiting message down same socket *
+*************************************************/
+
+/* Just the regain-root-privilege exec portion */
+void
+transport_do_pass_socket(const uschar *transport_name, const uschar *hostname,
+ const uschar *hostaddress, uschar *id, int socket_fd)
+{
+int i = 13;
+const uschar **argv;
+
+#ifndef DISABLE_TLS
+if (smtp_peer_options & OPTION_TLS) i += 6;
+#endif
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+if (continue_limit_mail || continue_limit_rcpt || continue_limit_rcptdom)
+ i += 4;
+#endif
+if (queue_run_pid != (pid_t)0) i += 3;
+#ifdef SUPPORT_SOCKS
+if (proxy_session) i += 5;
+#endif
+
+/* Set up the calling arguments; use the standard function for the basics,
+but we have a number of extras that may be added. */
+
+argv = CUSS child_exec_exim(CEE_RETURN_ARGV, TRUE, &i, FALSE, 0);
+
+if (f.smtp_authenticated) argv[i++] = US"-MCA";
+if (smtp_peer_options & OPTION_CHUNKING) argv[i++] = US"-MCK";
+if (smtp_peer_options & OPTION_DSN) argv[i++] = US"-MCD";
+if (smtp_peer_options & OPTION_PIPE) argv[i++] = US"-MCP";
+if (smtp_peer_options & OPTION_SIZE) argv[i++] = US"-MCS";
+#ifndef DISABLE_TLS
+if (smtp_peer_options & OPTION_TLS)
+ if (tls_out.active.sock >= 0 || continue_proxy_cipher)
+ {
+ argv[i++] = US"-MCt";
+ argv[i++] = sending_ip_address;
+ argv[i++] = string_sprintf("%d", sending_port);
+ argv[i++] = tls_out.active.sock >= 0 ? tls_out.cipher : continue_proxy_cipher;
+
+ if (tls_out.sni)
+ {
+ argv[i++] =
+#ifdef SUPPORT_DANE
+ tls_out.dane_verified ? US"-MCr" :
+#endif
+ US"-MCs";
+ argv[i++] = tls_out.sni;
+ }
+ }
+ else
+ argv[i++] = US"-MCT";
+#endif
+
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+if (continue_limit_rcpt || continue_limit_rcptdom)
+ {
+ argv[i++] = US"-MCL";
+ argv[i++] = string_sprintf("%u", continue_limit_mail);
+ argv[i++] = string_sprintf("%u", continue_limit_rcpt);
+ argv[i++] = string_sprintf("%u", continue_limit_rcptdom);
+ }
+#endif
+
+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);
+ }
+
+#ifdef SUPPORT_SOCKS
+if (proxy_session)
+ {
+ argv[i++] = US"-MCp";
+ argv[i++] = proxy_local_address;
+ argv[i++] = string_sprintf("%d", proxy_local_port);
+ argv[i++] = proxy_external_address;
+ argv[i++] = string_sprintf("%d", proxy_external_port);
+ }
+#endif
+
+argv[i++] = US"-MC";
+argv[i++] = US transport_name;
+argv[i++] = US hostname;
+argv[i++] = US 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 */
+/* argv[0] should be untainted, from child_exec_exim() */
+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() */
+}
+
+
+
+/* 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(const uschar *transport_name, const uschar *hostname,
+ const uschar *hostaddress, uschar *id, int socket_fd
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ , unsigned peer_limit_mail, unsigned peer_limit_rcpt, unsigned peer_limit_rcptdom
+#endif
+ )
+{
+pid_t pid;
+int status;
+
+DEBUG(D_transport) debug_printf("transport_pass_socket entered\n");
+
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+continue_limit_mail = peer_limit_mail;
+continue_limit_rcpt = peer_limit_rcpt;
+continue_limit_rcptdom = peer_limit_rcptdom;
+#endif
+
+if ((pid = exim_fork(US"continued-transport-interproc")) == 0)
+ {
+ /* 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 = exim_fork(US"continued-transport")) != 0)
+ _exit(EXIT_SUCCESS);
+ testharness_pause_ms(1000);
+
+ transport_do_pass_socket(transport_name, hostname, hostaddress,
+ id, socket_fd);
+ }
+
+/* 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));
+ return TRUE;
+ }
+else
+ {
+ DEBUG(D_transport) debug_printf("transport_pass_socket failed to fork: %s\n",
+ strerror(errno));
+ return FALSE;
+ }
+}
+
+
+
+/* Enforce all args untainted, for consistency with a router-sourced pipe
+command, where (because the whole line is passed as one to the tpt) a
+tainted arg taints the executable name. It's unclear also that letting an
+attacker supply command arguments is wise. */
+
+static BOOL
+arg_is_tainted(const uschar * s, int argn, address_item * addr,
+ const uschar * etext, uschar ** errptr)
+{
+if (is_tainted(s))
+ {
+ uschar * msg = string_sprintf("Tainted arg %d for %s command: '%s'",
+ argn, etext, s);
+ if (addr)
+ {
+ addr->transport_return = FAIL;
+ addr->message = msg;
+ }
+ else *errptr = msg;
+ return TRUE;
+ }
+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 (modified IN PLACE)
+ 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
+ allow_tainted_args as it says; used for ${run}
+ 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(const uschar *** argvptr, const uschar * cmd,
+ BOOL expand_arguments, int expand_failed, address_item * addr,
+ BOOL allow_tainted_args, const uschar * etext, uschar ** errptr)
+{
+const uschar ** argv, * s;
+int address_count = 0, argcount = 0, 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 (address_item * ad = addr; ad; ad = ad->next) address_count++;
+max_args = address_count + 60;
+*argvptr = argv = store_get((max_args+1)*sizeof(uschar *), GET_UNTAINTED);
+
+/* 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;
+Uskip_whitespace(&s);
+
+for (; *s && argcount < max_args; argcount++)
+ {
+ if (*s == '\'')
+ {
+ int n = Ustrcspn(++s, "'");
+ argv[argcount] = string_copyn(s, n);
+ if (*(s += n) == '\'') s++;
+ }
+ else
+ argv[argcount] = string_dequote(CUSS &s);
+ Uskip_whitespace(&s);
+ }
+
+argv[argcount] = NULL;
+
+/* If *s != 0 we have run out of argument slots. */
+
+if (*s)
+ {
+ uschar *msg = string_sprintf("Too many arguments in command \"%s\" in "
+ "%s", cmd, etext);
+ if (addr)
+ {
+ 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 (int i = 0; argv[i]; i++)
+ debug_printf(" argv[%d] = '%s'\n", i, string_printing(argv[i]));
+ }
+
+if (expand_arguments)
+ {
+ BOOL allow_dollar_recipients = addr && addr->parent
+ && Ustrcmp(addr->parent->address, "system-filter") == 0;
+
+ for (int i = 0; argv[i]; i++)
+ {
+ /* Handle special fudge for passing an address list */
+
+ if (addr &&
+ (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 (address_item * ad = addr; ad; ad = ad->next)
+ {
+ /* $pipe_addresses is spefically not checked for taint, because there is
+ a testcase (321) depending on it. It's unclear if the exact thing being
+ done really needs to be legitimate, though I suspect it reflects an
+ actual use-case that showed up a bug.
+ This is a hole in the taint-pretection, mitigated only in that
+ shell-syntax metachars cannot be injected via this route. */
+
+ DEBUG(D_transport) if (is_tainted(ad->address))
+ debug_printf("tainted element '%s' from $pipe_addresses\n", ad->address);
+
+ 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 && testflag(addr,af_force_command) &&
+ (Ustrcmp(argv[i], "$address_pipe") == 0 ||
+ Ustrcmp(argv[i], "${address_pipe}") == 0))
+ {
+ 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 *), GET_UNTAINTED);
+
+ /* +1 because addr->local_part[0] == '|' since af_force_command is set */
+ s = expand_string(addr->local_part + 1);
+
+ if (!s || !*s)
+ {
+ 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;
+ }
+
+ Uskip_whitespace(&s); /* strip leading space */
+
+ while (*s && address_pipe_argcount < address_pipe_max_args)
+ {
+ if (*s == '\'')
+ {
+ int n = Ustrcspn(++s, "'");
+ argv[argcount] = string_copyn(s, n);
+ if (*(s += n) == '\'') s++;
+ }
+ else
+ address_pipe_argv[address_pipe_argcount++] = string_dequote(CUSS &s);
+ Uskip_whitespace(&s); /* strip space after arg */
+ }
+
+ address_pipe_argv[address_pipe_argcount] = NULL;
+
+ /* If *s != 0 we have run out of argument slots. */
+ if (*s)
+ {
+ uschar *msg = string_sprintf("Too many arguments in $address_pipe "
+ "\"%s\" in %s", addr->local_part + 1, etext);
+ if (addr)
+ {
+ 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 + additional 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 (int address_pipe_i = 0;
+ address_pipe_argv[address_pipe_i];
+ address_pipe_i++, argcount++)
+ {
+ uschar * s = address_pipe_argv[address_pipe_i];
+ if (arg_is_tainted(s, i, addr, etext, errptr)) return FALSE;
+ argv[i++] = s;
+ }
+
+ /* Subtract one since we replace $address_pipe */
+ argcount--;
+ i--;
+ }
+
+ /* Handle normal expansion string */
+
+ else
+ {
+ const uschar *expanded_arg;
+ f.enable_dollar_recipients = allow_dollar_recipients;
+ expanded_arg = expand_cstring(argv[i]);
+ f.enable_dollar_recipients = FALSE;
+
+ if (!expanded_arg)
+ {
+ uschar *msg = string_sprintf("Expansion of \"%s\" "
+ "from command \"%s\" in %s failed: %s",
+ argv[i], cmd, etext, expand_string_message);
+ if (addr)
+ {
+ addr->transport_return = expand_failed;
+ addr->message = msg;
+ }
+ else *errptr = msg;
+ return FALSE;
+ }
+
+ if ( f.running_in_test_harness && is_tainted(expanded_arg)
+ && Ustrcmp(etext, "queryprogram router") == 0)
+ { /* hack, would be good to not need it */
+ DEBUG(D_transport)
+ debug_printf("SPECIFIC TESTSUITE EXEMPTION: tainted arg '%s'\n",
+ expanded_arg);
+ }
+ else if ( !allow_tainted_args
+ && arg_is_tainted(expanded_arg, i, addr, etext, errptr))
+ return FALSE;
+ argv[i] = expanded_arg;
+ }
+ }
+
+ DEBUG(D_transport)
+ {
+ debug_printf("direct command after expansion:\n");
+ for (int i = 0; argv[i]; i++)
+ {
+ debug_printf(" argv[%d] = '%s'\n", i, string_printing(argv[i]));
+ debug_print_taint(argv[i]);
+ }
+ }
+ }
+
+return TRUE;
+}
+
+
+
+/* For error messages, a string describing the config location associated
+with current processing. NULL if we are not in a transport. */
+/* Name only, for now */
+
+uschar *
+transport_current_name(void)
+{
+if (!transport_name) return NULL;
+return string_sprintf(" (transport %s, %s %d)", transport_name, driver_srcfile, driver_srcline);
+}
+
+#endif /*!MACRO_PREDEF*/
+/* vi: aw ai sw=2
+*/
+/* End of transport.c */
diff --git a/src/transports/Makefile b/src/transports/Makefile
new file mode 100644
index 0000000..4eea141
--- /dev/null
+++ b/src/transports/Makefile
@@ -0,0 +1,27 @@
+# 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 queuefile.o smtp.o smtp_socks.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
+queuefile.o: $(HDRS) queuefile.c queuefile.h
+smtp.o: $(HDRS) smtp.c smtp.h
+smtp_socks.o: $(HDRS) smtp_socks.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
index 0000000..9ea29fb
--- /dev/null
+++ b/src/transports/README
@@ -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
index 0000000..93281ef
--- /dev/null
+++ b/src/transports/appendfile.c
@@ -0,0 +1,3317 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2020 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "appendfile.h"
+
+#ifdef SUPPORT_MAILDIR
+#include "tf_maildir.h"
+#endif
+
+
+/* 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. */
+#define LOFF(field) OPT_OFF(appendfile_transport_options_block, field)
+
+optionlist appendfile_transport_options[] = {
+#ifdef SUPPORT_MAILDIR
+ { "*expand_maildir_use_size_file", opt_stringptr, LOFF(expand_maildir_use_size_file) },
+#endif
+ { "*set_use_fcntl_lock",opt_bool | opt_hidden, LOFF(set_use_fcntl) },
+ { "*set_use_flock_lock",opt_bool | opt_hidden, LOFF(set_use_flock) },
+ { "*set_use_lockfile", opt_bool | opt_hidden, LOFF(set_use_lockfile) },
+#ifdef SUPPORT_MBX
+ { "*set_use_mbx_lock", opt_bool | opt_hidden, LOFF(set_use_mbx_lock) },
+#endif
+ { "allow_fifo", opt_bool, LOFF(allow_fifo) },
+ { "allow_symlink", opt_bool, LOFF(allow_symlink) },
+ { "batch_id", opt_stringptr | opt_public, OPT_OFF(transport_instance, batch_id) },
+ { "batch_max", opt_int | opt_public, OPT_OFF(transport_instance, batch_max) },
+ { "check_group", opt_bool, LOFF(check_group) },
+ { "check_owner", opt_bool, LOFF(check_owner) },
+ { "check_string", opt_stringptr, LOFF(check_string) },
+ { "create_directory", opt_bool, LOFF(create_directory) },
+ { "create_file", opt_stringptr, LOFF(create_file_string) },
+ { "directory", opt_stringptr, LOFF(dirname) },
+ { "directory_file", opt_stringptr, LOFF(dirfilename) },
+ { "directory_mode", opt_octint, LOFF(dirmode) },
+ { "escape_string", opt_stringptr, LOFF(escape_string) },
+ { "file", opt_stringptr, LOFF(filename) },
+ { "file_format", opt_stringptr, LOFF(file_format) },
+ { "file_must_exist", opt_bool, LOFF(file_must_exist) },
+ { "lock_fcntl_timeout", opt_time, LOFF(lock_fcntl_timeout) },
+ { "lock_flock_timeout", opt_time, LOFF(lock_flock_timeout) },
+ { "lock_interval", opt_time, LOFF(lock_interval) },
+ { "lock_retries", opt_int, LOFF(lock_retries) },
+ { "lockfile_mode", opt_octint, LOFF(lockfile_mode) },
+ { "lockfile_timeout", opt_time, LOFF(lockfile_timeout) },
+ { "mailbox_filecount", opt_stringptr, LOFF(mailbox_filecount_string) },
+ { "mailbox_size", opt_stringptr, LOFF(mailbox_size_string) },
+#ifdef SUPPORT_MAILDIR
+ { "maildir_format", opt_bool, LOFF(maildir_format ) } ,
+ { "maildir_quota_directory_regex", opt_stringptr, LOFF(maildir_dir_regex) },
+ { "maildir_retries", opt_int, LOFF(maildir_retries) },
+ { "maildir_tag", opt_stringptr, LOFF(maildir_tag) },
+ { "maildir_use_size_file", opt_expand_bool, LOFF(maildir_use_size_file ) } ,
+ { "maildirfolder_create_regex", opt_stringptr, LOFF(maildirfolder_create_regex ) },
+#endif /* SUPPORT_MAILDIR */
+#ifdef SUPPORT_MAILSTORE
+ { "mailstore_format", opt_bool, LOFF(mailstore_format ) },
+ { "mailstore_prefix", opt_stringptr, LOFF(mailstore_prefix ) },
+ { "mailstore_suffix", opt_stringptr, LOFF(mailstore_suffix ) },
+#endif /* SUPPORT_MAILSTORE */
+#ifdef SUPPORT_MBX
+ { "mbx_format", opt_bool, LOFF(mbx_format ) } ,
+#endif /* SUPPORT_MBX */
+ { "message_prefix", opt_stringptr, LOFF(message_prefix) },
+ { "message_suffix", opt_stringptr, LOFF(message_suffix) },
+ { "mode", opt_octint, LOFF(mode) },
+ { "mode_fail_narrower",opt_bool, LOFF(mode_fail_narrower) },
+ { "notify_comsat", opt_bool, LOFF(notify_comsat) },
+ { "quota", opt_stringptr, LOFF(quota) },
+ { "quota_directory", opt_stringptr, LOFF(quota_directory) },
+ { "quota_filecount", opt_stringptr, LOFF(quota_filecount) },
+ { "quota_is_inclusive", opt_bool, LOFF(quota_is_inclusive) },
+ { "quota_size_regex", opt_stringptr, LOFF(quota_size_regex) },
+ { "quota_warn_message", opt_stringptr | opt_public, OPT_OFF(transport_instance, warn_message) },
+ { "quota_warn_threshold", opt_stringptr, LOFF(quota_warn_threshold) },
+ { "use_bsmtp", opt_bool, LOFF(use_bsmtp) },
+ { "use_crlf", opt_bool, LOFF(use_crlf) },
+ { "use_fcntl_lock", opt_bool_set, LOFF(use_fcntl) },
+ { "use_flock_lock", opt_bool_set, LOFF(use_flock) },
+ { "use_lockfile", opt_bool_set, LOFF(use_lockfile) },
+#ifdef SUPPORT_MBX
+ { "use_mbx_lock", opt_bool_set, LOFF(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);
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+appendfile_transport_options_block appendfile_transport_option_defaults = {0};
+void appendfile_transport_init(transport_instance *tblock) {}
+BOOL appendfile_transport_entry(transport_instance *tblock, address_item *addr) {return FALSE;}
+
+#else /*!MACRO_PREDEF*/
+
+/* Default private options block for the appendfile transport. */
+
+appendfile_transport_options_block appendfile_transport_option_defaults = {
+ /* all non-mentioned members zero/null/false */
+ .dirfilename = US"q${base62:$tod_epoch}-$inode",
+ .create_file_string = US"anywhere",
+ .maildir_dir_regex = US"^(?:cur|new|\\..*)$",
+ .mailbox_size_value = -1,
+ .mailbox_filecount_value = -1,
+ .mode = APPENDFILE_MODE,
+ .dirmode = APPENDFILE_DIRECTORY_MODE,
+ .lockfile_mode = APPENDFILE_LOCKFILE_MODE,
+ .lockfile_timeout = 30*60,
+ .lock_retries = 10,
+ .lock_interval = 3,
+ .maildir_retries = 10,
+ .create_file = create_anywhere,
+ .check_owner = TRUE,
+ .create_directory = TRUE,
+ .notify_comsat = FALSE,
+ .use_lockfile = TRUE,
+ .use_fcntl = TRUE,
+ .mode_fail_narrower = TRUE,
+ .quota_is_inclusive = TRUE,
+};
+
+
+/* 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))
+
+
+
+/*************************************************
+* 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;
+
+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 (int i = 0; i < 5; i++)
+ {
+ double d = default_value;
+ int no_check = 0;
+ uschar *which = NULL;
+
+ if (q)
+ {
+ uschar * rest, * s;
+
+ if (!(s = expand_string(q)))
+ {
+ *errmsg = string_sprintf("Expansion of \"%s\" in %s transport failed: "
+ "%s", q, tblock->name, expand_string_message);
+ return f.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++;
+ }
+
+
+ /* For quota and quota_filecount there may be options
+ appended. Currently only "no_check", so we can be lazy parsing it */
+ if (i < 2 && Ustrstr(rest, "/no_check") == rest)
+ {
+ no_check = 1;
+ rest += sizeof("/no_check") - 1;
+ }
+
+ Uskip_whitespace(&rest);
+
+ if (*rest)
+ {
+ *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;
+ ob->quota_no_check = no_check;
+ 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;
+ ob->quota_filecount_no_check = no_check;
+ 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)
+ {
+ *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);
+uschar * s;
+
+/* 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 && ob->dirname)
+ 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)
+ {
+ if (ob->quota_filecount)
+ 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)
+ 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)
+ {
+ 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)
+ 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 ((s = ob->create_file_string ) && *s)
+ {
+ int val = 0;
+ if (Ustrcmp(s, "anywhere") == 0) val = create_anywhere;
+ else if (*s == '/' || Ustrcmp(s, "belowhome") == 0) val = create_belowhome;
+ else if (Ustrcmp(s, "inhome") == 0) val = create_inhome;
+ else
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+ "invalid value given for \"create_file\" for the %s transport: '%s'",
+ tblock->name, s);
+ ob->create_file = val;
+ }
+
+/* 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)
+ {
+ if (!tblock->warn_message) 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 && !ob->maildir_format && !ob->mailstore_format)
+ {
+ if (!ob->message_prefix) ob->message_prefix =
+ US"From ${if def:return_path{$return_path}{MAILER-DAEMON}} ${tod_bsdinbox}\n";
+ if (!ob->message_suffix) ob->message_suffix = US"\n";
+ if (!ob->check_string) ob->check_string = US"From ";
+ if (!ob->escape_string) 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;
+uschar * s;
+
+DEBUG(D_transport) debug_printf("notify_comsat called\n");
+
+s = string_sprintf("%.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 (host_item * h = &host; h; 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);
+
+ if ((sock = ip_socket(SOCK_DGRAM, host_af)) < 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, NULL);
+ rc = send(sock, s, Ustrlen(s) + 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)
+{
+const 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 */
+
+/* not expanded so cannot be tainted */
+while ((s = string_nextinlist(&format, &sep, big_buffer, big_buffer_size)))
+ {
+ 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 && tp)
+ {
+ for (transport_instance * tt = transports; tt; 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)
+ re 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(const uschar * dirname, int * countptr, const pcre2_code * re)
+{
+DIR *dir;
+off_t sum = 0;
+int count = *countptr;
+
+if (!(dir = exim_opendir(dirname))) return 0;
+
+for (struct dirent *ent; ent = readdir(dir); )
+ {
+ uschar * path, * name = US ent->d_name;
+ struct stat statbuf;
+
+ if (Ustrcmp(name, ".") == 0 || Ustrcmp(name, "..") == 0) continue;
+
+ count++;
+
+ /* If there's a regex, try to find the size using it */
+
+ if (re)
+ {
+ pcre2_match_data * md = pcre2_match_data_create(2, pcre_gen_ctx);
+ int rc = pcre2_match(re, (PCRE2_SPTR)name, PCRE2_ZERO_TERMINATED,
+ 0, 0, md, pcre_mtc_ctx);
+ PCRE2_SIZE * ovec = pcre2_get_ovector_pointer(md);
+ if ( rc >= 0
+ && (rc = pcre2_get_ovector_count(md)) >= 2)
+ {
+ uschar *endptr;
+ off_t size = (off_t)Ustrtod(name + ovec[2], &endptr);
+ if (endptr == name + ovec[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 */
+
+ path = string_sprintf("%s/%s", dirname, name);
+
+ if (Ustat(path, &statbuf) < 0)
+ {
+ DEBUG(D_transport)
+ debug_printf("check_dir_size: stat error %d for %s: %s\n", errno, path,
+ strerror(errno));
+ }
+ else
+ if ((statbuf.st_mode & S_IFMT) == S_IFREG)
+ sum += statbuf.st_size / statbuf.st_nlink;
+ else if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
+ sum += check_dir_size(path, &count, re);
+ }
+
+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_CLR(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_CLR(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;
+transport_ctx tctx = { .u={.fd = to_fd}, .options = topt_not_socket };
+
+/* If the current mailbox size is zero, write a header block */
+
+if (saved_size == 0)
+ {
+ 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 (int i = 0; i < MBX_NUSERFLAGS; i++)
+ sprintf (CS(s += Ustrlen(s)), "\015\012");
+ if (!transport_write_block (&tctx, deliver_out_buffer, MBX_HDRSIZE, FALSE))
+ 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. */
+
+if (lseek(from_fd, 0 , SEEK_SET) < 0) return DEFER;
+
+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(&tctx, deliver_out_buffer, used + len, FALSE))
+ 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
+ deliver_dir the delivery directory
+
+Returns: TRUE if creation is permitted
+*/
+
+static BOOL
+check_creation(uschar *filename, int create_file, const uschar * deliver_dir)
+{
+BOOL yield = TRUE;
+
+if (deliver_dir && create_file != create_anywhere)
+ {
+ int len = Ustrlen(deliver_dir);
+ uschar *file = filename;
+
+ while (file[0] == '/' && file[1] == '/') file++;
+ if ( Ustrncmp(file, deliver_dir, 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 *next;
+ uschar *rp = NULL;
+ for (uschar * slash = Ustrrchr(file, '/'); /* There is known to be one */
+ !rp && 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)
+ {
+ uschar hdbuffer[PATH_MAX+1];
+ const uschar * rph = deliver_dir;
+ int rlen = Ustrlen(big_buffer);
+
+ if ((rp = US realpath(CS deliver_dir, CS hdbuffer)))
+ {
+ 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_dir);
+ }
+ }
+ }
+ #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 doesn'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;
+const uschar * deliver_dir;
+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 && !ob->dirname)
+ {
+ 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)
+ {
+ if (!(fdname = ob->filename))
+ {
+ fdname = ob->dirname;
+ isdirectory = TRUE;
+ }
+ if (!fdname)
+ {
+ addr->message = string_sprintf("Mandatory file or directory option "
+ "missing from %s transport", tblock->name);
+ goto ret_panic;
+ }
+ }
+
+/* Maildir and mailstore require a directory */
+
+if ((ob->maildir_format || ob->mailstore_format) && !isdirectory)
+ {
+ addr->message = string_sprintf("mail%s_format requires \"directory\" "
+ "to be specified for the %s transport",
+ ob->maildir_format ? "dir" : "store", tblock->name);
+ goto ret_panic;
+ }
+
+if (!(path = expand_string(fdname)))
+ {
+ addr->message = string_sprintf("Expansion of \"%s\" (file or directory "
+ "name for %s transport) failed: %s", fdname, tblock->name,
+ expand_string_message);
+ goto ret_panic;
+ }
+
+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))
+ for (address_item * addr2 = addr; addr2; 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
+ "%s%s"
+ " 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_no_check ? " (no_check)" : "",
+ ob->quota_filecount_no_check ? " (no_check_filecount)" : "",
+ ob->quota_warn_threshold_value,
+ ob->quota_warn_threshold_is_percent ? "%" : "",
+ isdirectory ? "directory" : "file",
+ path, mailbox_formats[mbformat],
+ !ob->message_prefix ? US"null" : string_printing(ob->message_prefix),
+ !ob->message_suffix ? 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 (f.dont_deliver)
+ {
+ DEBUG(D_transport)
+ debug_printf("*** delivery by %s transport bypassed by -N option\n",
+ tblock->name);
+ addr->transport_return = OK;
+ return FALSE;
+ }
+
+/* If an absolute path was given for create_file the it overrides deliver_home
+(here) and de-taints the filename (below, after check_creation() */
+
+deliver_dir = *ob->create_file_string == '/'
+ ? ob->create_file_string : deliver_home;
+
+/* 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.
+ If the create_file option is an absolute path and the file was within it,
+ de-taint. Chaeck for a tainted path. */
+
+ if ( (allow_creation_here = check_creation(path, ob->create_file, deliver_dir))
+ && ob->create_file == create_belowhome)
+ if (is_tainted(path))
+ {
+ DEBUG(D_transport) debug_printf("de-tainting path '%s'\n", path);
+ path = string_copy_taint(path, GET_UNTAINTED);
+ }
+
+ if (is_tainted(path)) goto tainted_ret_panic;
+ dataname = filename = path;
+
+ /* 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 = string_copyn(path, p - path);
+ if (!directory_make(NULL, p, ob->dirmode, FALSE))
+ {
+ addr->basic_errno = errno;
+ addr->message =
+ string_sprintf("failed to create directories for %s: %s", path,
+ exim_errstr(errno));
+ DEBUG(D_transport) debug_printf("%s transport: %s\n", tblock->name, path);
+ return FALSE;
+ }
+ }
+
+ /* 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)
+ {
+ 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)
+ {
+ 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 (exim_chown(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 (%lu)",
+ filename, islink ? " (symlink)" : "", (unsigned long)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 &= 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 " INO_T_FMT " to " INO_T_FMT, 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 && 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; /* Default quota check path */
+ const pcre2_code * re = NULL; /* Regex for file size from file name */
+
+ if (!check_creation(string_sprintf("%s/any", path),
+ ob->create_file, deliver_dir))
+ {
+ 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;
+ }
+
+ /* If the create_file option is an absolute path and the file was within
+ it, de-taint. Otherwise check for taint. */
+
+ if (is_tainted(path))
+ if (ob->create_file == create_belowhome)
+ {
+ DEBUG(D_transport) debug_printf("de-tainting path '%s'\n", path);
+ path = string_copy_taint(path, GET_UNTAINTED);
+ }
+ else
+ goto tainted_ret_panic;
+
+ check_path = path;
+
+ #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)
+ {
+ PCRE2_SIZE offset;
+ int err;
+
+ /* Compile the regex if there is one. */
+
+ if (ob->quota_size_regex)
+ {
+ if (!(re = pcre2_compile((PCRE2_SPTR)ob->quota_size_regex,
+ PCRE2_ZERO_TERMINATED, PCRE_COPT, &err, &offset, pcre_cmp_ctx)))
+ {
+ uschar errbuf[128];
+ pcre2_get_error_message(err, errbuf, sizeof(errbuf));
+ addr->message = string_sprintf("appendfile: regular expression "
+ "error: %s at offset %ld while compiling %s", errbuf, (long)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)
+ {
+ if (!(check_path = expand_string(ob->quota_directory)))
+ {
+ addr->message = string_sprintf("Expansion of \"%s\" (quota_directory "
+ "name for %s transport) failed: %s", ob->quota_directory,
+ tblock->name, expand_string_message);
+ goto ret_panic;
+ }
+
+ 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)
+ {
+ if (!slash[1])
+ {
+ *slash = 0;
+ slash = Ustrrchr(new_check_path, '/');
+ }
+ if (slash)
+ {
+ *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 pcre2_code * dir_regex = NULL;
+ PCRE2_SIZE offset;
+ int err;
+
+ if (ob->maildir_dir_regex)
+ {
+ int check_path_len = Ustrlen(check_path);
+
+ if (!(dir_regex = pcre2_compile((PCRE2_SPTR)ob->maildir_dir_regex,
+ PCRE2_ZERO_TERMINATED, PCRE_COPT, &err, &offset, pcre_cmp_ctx)))
+ {
+ uschar errbuf[128];
+ pcre2_get_error_message(err, errbuf, sizeof(errbuf));
+ addr->message = string_sprintf("appendfile: regular expression "
+ "error: %s at offset %ld while compiling %s", errbuf, (long)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 ? string_sprintf("%s/new", s) : US"new";
+ if (!regex_match(dir_regex, s, -1, NULL))
+ {
+ 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;
+
+ if ((maildirsize_fd = maildir_ensure_sizefile(check_path, ob, re, dir_regex,
+ &size, &filecount)) == -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,
+ * re, 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, re);
+ 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 && !expand_string(nametag) && !f.expand_string_forcedfail)
+ {
+ addr->message = string_sprintf("Expansion of \"%s\" (maildir_tag "
+ "for %s transport) failed: %s", nametag, tblock->name,
+ expand_string_message);
+ goto ret_panic;
+ }
+
+ /* 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 (int i = 1;; i++)
+ {
+ uschar *basename;
+
+ (void)gettimeofday(&msg_tv, NULL);
+ basename = string_sprintf(TIME_T_FMT ".M%luP" PID_T_FMT ".%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)
+ {
+ if ((fd = Uopen(filename, O_WRONLY | O_CREAT | O_EXCL, mode)) >= 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 (exim_chown(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;
+ 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 (exim_chown(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")))
+ {
+ addr->basic_errno = errno;
+ addr->message = string_sprintf("fdopen of %s ("
+ "for %s transport) failed", filename, tblock->name);
+ (void)close(fd);
+ Uunlink(filename);
+ goto ret_panic;
+ }
+
+ /* Write the envelope file, then close it. */
+
+ if (ob->mailstore_prefix)
+ {
+ uschar *s = expand_string(ob->mailstore_prefix);
+ if (!s)
+ {
+ if (!f.expand_string_forcedfail)
+ {
+ 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);
+ goto ret_panic;
+ }
+ }
+ 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 (address_item * taddr = addr; taddr; taddr = taddr->next)
+ fprintf(env_file, "%s@%s\n", taddr->local_part, taddr->domain);
+
+ if (ob->mailstore_suffix)
+ {
+ uschar *s = expand_string(ob->mailstore_suffix);
+ if (!s)
+ {
+ if (!f.expand_string_forcedfail)
+ {
+ 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);
+ goto ret_panic;
+ }
+ }
+ 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. */
+
+ if ((fd = Uopen(dataname, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
+ {
+ addr->basic_errno = errno;
+ addr->message = string_sprintf("while creating file %s", dataname);
+ Uunlink(filename);
+ return FALSE;
+ }
+ if (exim_chown(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 (exim_chown(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)
+ if (!ob->quota_no_check)
+ {
+ DEBUG(D_transport) debug_printf("mailbox quota exceeded\n");
+ yield = DEFER;
+ errno = ERRNO_EXIMQUOTA;
+ }
+ else
+ DEBUG(D_transport) debug_printf("mailbox quota exceeded but ignored\n");
+
+ if (ob->quota_filecount_value > 0
+ && mailbox_filecount + (ob->quota_is_inclusive ? 1:0) >
+ ob->quota_filecount_value)
+ if (!ob->quota_filecount_no_check)
+ {
+ DEBUG(D_transport) debug_printf("mailbox file count quota exceeded\n");
+ yield = DEFER;
+ errno = ERRNO_EXIMQUOTA;
+ filecount_msg = US" filecount";
+ }
+ else DEBUG(D_transport) if (ob->quota_filecount_no_check)
+ debug_printf("mailbox file count quota exceeded but ignored\n");
+
+ }
+
+if (verify_mode)
+ {
+ addr->basic_errno = errno;
+ addr->message = US"Over quota";
+ addr->transport_return = yield;
+ DEBUG(D_transport)
+ debug_printf("appendfile (verify) yields %d with errno=%d more_errno=%d\n",
+ yield, addr->basic_errno, addr->more_errno);
+
+ goto RETURN;
+ }
+
+/* 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)
+ {
+ if (!(temp_file = tmpfile()))
+ {
+ 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 && *ob->message_prefix)
+ {
+ uschar *prefix = expand_string(ob->message_prefix);
+ if (!prefix)
+ {
+ 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
+ {
+ transport_newlines++;
+ for (address_item * a = addr; a; 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)
+ {
+ transport_ctx tctx = {
+ .u = {.fd=fd},
+ .tblock = tblock,
+ .addr = addr,
+ .check_string = ob->check_string,
+ .escape_string = ob->escape_string,
+ .options = ob->options | topt_not_socket
+ };
+ if (!transport_write_message(&tctx, 0))
+ yield = DEFER;
+ }
+
+/* Now a configured suffix. */
+
+if (yield == OK && ob->message_suffix && *ob->message_suffix)
+ {
+ uschar *suffix = expand_string(ob->message_suffix);
+ if (!suffix)
+ {
+ 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 && 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 = f.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 = US"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 resetting 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)
+ {
+ uschar *renameleaf;
+ uschar *old_renameleaf = US"";
+
+ for (int i = 0; ; sleep(1), i++)
+ {
+ deliver_inode = statbuf.st_ino;
+ renameleaf = expand_string(ob->dirfilename);
+ deliver_inode = 0;
+
+ if (!renameleaf)
+ {
+ 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)
+ {
+ uschar *iptr = expand_string(nametag);
+ if (iptr)
+ {
+ uschar *etag = store_get(Ustrlen(iptr) + 2, iptr);
+ uschar *optr = etag;
+ for ( ; *iptr; iptr++)
+ if (mac_isgraph(*iptr) && *iptr != '/')
+ {
+ if (optr == etag && isalnum(*iptr)) *optr++ = ':';
+ *optr++ = *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)
+ 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)
+ {
+ 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;
+
+tainted_ret_panic:
+ addr->message = string_sprintf("Tainted '%s' (file or directory "
+ "name for %s transport) not permitted", path, tblock->name);
+ret_panic:
+ addr->transport_return = PANIC;
+ return FALSE;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of transport/appendfile.c */
diff --git a/src/transports/appendfile.h b/src/transports/appendfile.h
new file mode 100644
index 0000000..3fd2f46
--- /dev/null
+++ b/src/transports/appendfile.h
@@ -0,0 +1,100 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2021 */
+/* 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;
+ BOOL quota_no_check;
+ BOOL quota_filecount_no_check;
+} 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(const uschar *, int *, const pcre2_code *);
+
+/* End of transports/appendfile.h */
diff --git a/src/transports/autoreply.c b/src/transports/autoreply.c
new file mode 100644
index 0000000..211e328
--- /dev/null
+++ b/src/transports/autoreply.c
@@ -0,0 +1,821 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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. */
+#define LOFF(field) OPT_OFF(autoreply_transport_options_block, field)
+
+optionlist autoreply_transport_options[] = {
+ { "bcc", opt_stringptr, LOFF(bcc) },
+ { "cc", opt_stringptr, LOFF(cc) },
+ { "file", opt_stringptr, LOFF(file) },
+ { "file_expand", opt_bool, LOFF(file_expand) },
+ { "file_optional", opt_bool, LOFF(file_optional) },
+ { "from", opt_stringptr, LOFF(from) },
+ { "headers", opt_stringptr, LOFF(headers) },
+ { "log", opt_stringptr, LOFF(logfile) },
+ { "mode", opt_octint, LOFF(mode) },
+ { "never_mail", opt_stringptr, LOFF(never_mail) },
+ { "once", opt_stringptr, LOFF(oncelog) },
+ { "once_file_size", opt_int, LOFF(once_file_size) },
+ { "once_repeat", opt_stringptr, LOFF(once_repeat) },
+ { "reply_to", opt_stringptr, LOFF(reply_to) },
+ { "return_message", opt_bool, LOFF(return_message) },
+ { "subject", opt_stringptr, LOFF(subject) },
+ { "text", opt_stringptr, LOFF(text) },
+ { "to", opt_stringptr, LOFF(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);
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+autoreply_transport_options_block autoreply_transport_option_defaults = {0};
+void autoreply_transport_init(transport_instance *tblock) {}
+BOOL autoreply_transport_entry(transport_instance *tblock, address_item *addr) {return FALSE;}
+
+#else /*!MACRO_PREDEF*/
+
+
+/* Default private options block for the autoreply transport.
+All non-mentioned lements zero/null/false. */
+
+autoreply_transport_options_block autoreply_transport_option_defaults = {
+ .mode = 0600,
+};
+
+
+
+/* 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 *ss = expand_string(s);
+
+if (!ss)
+ {
+ 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 (uschar * t = ss; *t != 0; t++)
+ {
+ int c = *t;
+ const uschar * sp;
+ if (mac_isprint(c)) continue;
+ if (type == cke_hdr && c == '\n' && (t[1] == ' ' || t[1] == '\t')) continue;
+ sp = string_printing(s);
+ addr->transport_return = FAIL;
+ addr->message = string_sprintf("Expansion of \"%s\" in %s transport "
+ "contains non-printing character %d", sp, 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:
+ list list of addresses to be checked
+ never_mail an address list, already expanded
+
+Returns: edited replacement address list, or NULL, or original
+*/
+
+static uschar *
+check_never_mail(uschar * list, const uschar * never_mail)
+{
+rmark reset_point = store_mark();
+uschar * newlist = string_copy(list);
+uschar * s = newlist;
+BOOL hit = FALSE;
+
+while (*s)
+ {
+ 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) 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);
+ hit = TRUE;
+ if (terminator == ',') e++;
+ memmove(s, e, Ustrlen(e) + 1);
+ }
+ else /* Skip over this address */
+ {
+ s = e;
+ if (terminator == ',') s++;
+ }
+ }
+
+/* If no addresses were removed, retrieve the memory used and return
+the original. */
+
+if (!hit)
+ {
+ store_reset(reset_point);
+ return list;
+ }
+
+/* Check to see if we removed the last address, leaving a terminating comma
+that needs to be removed */
+
+s = newlist + Ustrlen(newlist);
+while (s > newlist && (isspace(s[-1]) || s[-1] == ',')) s--;
+*s = 0;
+
+/* Check to see if there any addresses left; if not, return NULL */
+
+s = newlist;
+while (s && isspace(*s)) s++;
+if (*s)
+ return newlist;
+
+store_reset(reset_point);
+return 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 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 *fp;
+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)
+ {
+ 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 && !(from = checkexpand(from, addr, tblock->name, cke_hdr))
+ || reply_to && !(reply_to = checkexpand(reply_to, addr, tblock->name, cke_hdr))
+ || to && !(to = checkexpand(to, addr, tblock->name, cke_hdr))
+ || cc && !(cc = checkexpand(cc, addr, tblock->name, cke_hdr))
+ || bcc && !(bcc = checkexpand(bcc, addr, tblock->name, cke_hdr))
+ || subject && !(subject = checkexpand(subject, addr, tblock->name, cke_hdr))
+ || headers && !(headers = checkexpand(headers, addr, tblock->name, cke_text))
+ || text && !(text = checkexpand(text, addr, tblock->name, cke_text))
+ || file && !(file = checkexpand(file, addr, tblock->name, cke_file))
+ || logfile && !(logfile = checkexpand(logfile, addr, tblock->name, cke_file))
+ || oncelog && !(oncelog = checkexpand(oncelog, addr, tblock->name, cke_file))
+ || oncerepeat && !(oncerepeat = checkexpand(oncerepeat, addr, tblock->name, cke_file))
+ )
+ return FALSE;
+
+ if (oncerepeat)
+ if ((once_repeat_sec = readconf_readtime(oncerepeat, 0, FALSE)) < 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)
+ {
+ const uschar *never_mail = expand_string(ob->never_mail);
+
+ if (!never_mail)
+ {
+ 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) to = check_never_mail(to, never_mail);
+ if (cc) cc = check_never_mail(cc, never_mail);
+ if (bcc) bcc = check_never_mail(bcc, never_mail);
+
+ if (!to && !cc && !bcc)
+ {
+ 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 (f.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 && *oncelog && to)
+ {
+ time_t then = 0;
+
+ if (is_tainted(oncelog))
+ {
+ addr->transport_return = DEFER;
+ addr->basic_errno = EACCES;
+ addr->message = string_sprintf("Tainted '%s' (once file for %s transport)"
+ " not permitted", oncelog, tblock->name);
+ goto END_OFF;
+ }
+
+ /* Handle fixed-size cache file. */
+
+ if (ob->once_file_size > 0)
+ {
+ uschar * nextp;
+ 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->basic_errno = errno;
+ 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, oncelog);
+
+ 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. */
+
+ for (uschar * p = cache_buff; p < cache_buff + cache_size; p = nextp)
+ {
+ uschar *s = p + sizeof(time_t);
+ nextp = s + Ustrlen(s) + 1;
+ if (Ustrcmp(to, s) == 0)
+ {
+ memcpy(&then, p, sizeof(time_t));
+ cache_time = p;
+ break;
+ }
+ }
+ }
+
+ /* Use a DBM file for the list of previous recipients. */
+
+ else
+ {
+ EXIM_DATUM key_datum, result_datum;
+ uschar * dirname, * s;
+
+ dirname = (s = Ustrrchr(oncelog, '/'))
+ ? string_copyn(oncelog, s - oncelog) : NULL;
+ if (!(dbm_file = exim_dbopen(oncelog, dirname, O_RDWR|O_CREAT, ob->mode)))
+ {
+ addr->transport_return = DEFER;
+ addr->basic_errno = errno;
+ 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_set(&key_datum, (void *) to);
+ exim_datum_size_set(&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_get(&result_datum) == sizeof(time_t))
+ memcpy(&then, exim_datum_data_get(&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))
+ {
+ int log_fd;
+ if (is_tainted(logfile))
+ {
+ addr->transport_return = DEFER;
+ addr->basic_errno = EACCES;
+ addr->message = string_sprintf("Tainted '%s' (logfile for %s transport)"
+ " not permitted", logfile, tblock->name);
+ goto END_OFF;
+ }
+
+ DEBUG(D_transport) debug_printf("message previously sent to %s%s\n", to,
+ (once_repeat_sec > 0)? " and repeat time not reached" : "");
+ log_fd = logfile ? Uopen(logfile, O_WRONLY|O_APPEND|O_CREAT, ob->mode) : -1;
+ 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)
+ {
+ if (is_tainted(file))
+ {
+ addr->transport_return = DEFER;
+ addr->basic_errno = EACCES;
+ addr->message = string_sprintf("Tainted '%s' (file for %s transport)"
+ " not permitted", file, tblock->name);
+ return FALSE;
+ }
+ if (!(ff = Ufopen(file, "rb")) && !ob->file_optional)
+ {
+ addr->transport_return = DEFER;
+ addr->basic_errno = errno;
+ 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 */
+
+if ((pid = child_open_exim(&fd, US"autoreply")) < 0)
+ {
+ /* Creation of child failed; defer this delivery. */
+
+ addr->transport_return = DEFER;
+ addr->basic_errno = errno;
+ 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);
+ if (dbm_file) exim_dbclose(dbm_file);
+ 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. */
+
+fp = fdopen(fd, "wb");
+
+if (from) fprintf(fp, "From: %s\n", from);
+if (reply_to) fprintf(fp, "Reply-To: %s\n", reply_to);
+if (to) fprintf(fp, "To: %s\n", to);
+if (cc) fprintf(fp, "Cc: %s\n", cc);
+if (bcc) fprintf(fp, "Bcc: %s\n", bcc);
+if (subject) fprintf(fp, "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; h = h->next)
+ if (h->type == htype_id) break;
+
+if (h)
+ {
+ message_id = Ustrchr(h->text, ':') + 1;
+ while (isspace(*message_id)) message_id++;
+ fprintf(fp, "In-Reply-To: %s", message_id);
+ }
+
+moan_write_references(fp, message_id);
+
+/* Add an Auto-Submitted: header */
+
+fprintf(fp, "Auto-Submitted: auto-replied\n");
+
+/* Add any specially requested headers */
+
+if (headers) fprintf(fp, "%s\n", headers);
+fprintf(fp, "\n");
+
+if (text)
+ {
+ fprintf(fp, "%s", CS text);
+ if (text[Ustrlen(text)-1] != '\n') fprintf(fp, "\n");
+ }
+
+if (ff)
+ {
+ while (Ufgets(big_buffer, big_buffer_size, ff) != NULL)
+ {
+ if (file_expand)
+ {
+ uschar *s = expand_string(big_buffer);
+ DEBUG(D_transport)
+ {
+ if (!s)
+ debug_printf("error while expanding line from file:\n %s\n %s\n",
+ big_buffer, expand_string_message);
+ }
+ fprintf(fp, "%s", s ? CS s : CS big_buffer);
+ }
+ else fprintf(fp, "%s", CS big_buffer);
+ }
+ (void) fclose(ff);
+ }
+
+/* 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";
+ transport_ctx tctx = {
+ .u = {.fd = fileno(fp)},
+ .tblock = tblock,
+ .addr = addr,
+ .check_string = NULL,
+ .escape_string = NULL,
+ .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_not_socket
+ };
+
+ 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(fp, "\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(fp, "\n%s\n", rubric);
+ }
+ else fprintf(fp, "\n%s\n", rubric);
+
+ fflush(fp);
+ transport_count = 0;
+ transport_write_message(&tctx, bounce_return_size_limit);
+ }
+
+/* End the message and wait for the child process to end; no timeout. */
+
+(void)fclose(fp);
+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;
+
+ if (lseek(cache_fd, 0, SEEK_SET) == 0)
+ {
+ if (!cache_time)
+ {
+ 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)
+ {
+ 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_set(&key_datum, to);
+ exim_datum_size_set(&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_set(&value_datum, &now);
+ exim_datum_size_set(&value_datum, 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)
+ {
+ int log_fd = Uopen(logfile, O_WRONLY|O_APPEND|O_CREAT, ob->mode);
+ if (log_fd >= 0)
+ {
+ gstring gs = { .size = LOG_BUFFER_SIZE, .ptr = 0, .s = log_buffer }, *g = &gs;
+
+ /* Use taint-unchecked routines for writing into log_buffer, trusting
+ that we'll never expand it. */
+
+ DEBUG(D_transport) debug_printf("logging message details\n");
+ g = string_fmt_append_f(g, SVFMT_TAINT_NOCHK, "%s\n", tod_stamp(tod_log));
+ if (from)
+ g = string_fmt_append_f(g, SVFMT_TAINT_NOCHK, " From: %s\n", from);
+ if (to)
+ g = string_fmt_append_f(g, SVFMT_TAINT_NOCHK, " To: %s\n", to);
+ if (cc)
+ g = string_fmt_append_f(g, SVFMT_TAINT_NOCHK, " Cc: %s\n", cc);
+ if (bcc)
+ g = string_fmt_append_f(g, SVFMT_TAINT_NOCHK, " Bcc: %s\n", bcc);
+ if (subject)
+ g = string_fmt_append_f(g, SVFMT_TAINT_NOCHK, " Subject: %s\n", subject);
+ if (headers)
+ g = string_fmt_append_f(g, SVFMT_TAINT_NOCHK, " %s\n", headers);
+ if(write(log_fd, g->s, g->ptr) != g->ptr || 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) 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;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of transport/autoreply.c */
diff --git a/src/transports/autoreply.h b/src/transports/autoreply.h
new file mode 100644
index 0000000..fcfd981
--- /dev/null
+++ b/src/transports/autoreply.h
@@ -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
index 0000000..f751771
--- /dev/null
+++ b/src/transports/lmtp.c
@@ -0,0 +1,809 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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,
+ OPT_OFF(transport_instance, batch_id) },
+ { "batch_max", opt_int | opt_public,
+ OPT_OFF(transport_instance, batch_max) },
+ { "command", opt_stringptr,
+ OPT_OFF(lmtp_transport_options_block, cmd) },
+ { "ignore_quota", opt_bool,
+ OPT_OFF(lmtp_transport_options_block, ignore_quota) },
+ { "socket", opt_stringptr,
+ OPT_OFF(lmtp_transport_options_block, skt) },
+ { "timeout", opt_time,
+ OPT_OFF(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);
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+lmtp_transport_options_block lmtp_transport_option_defaults = {0};
+void lmtp_transport_init(transport_instance *tblock) {}
+BOOL lmtp_transport_entry(transport_instance *tblock, address_item *addr) {return FALSE;}
+
+#else /*!MACRO_PREDEF*/
+
+
+/* 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 error 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 = US"failed to write a data block";
+ return FALSE;
+ }
+
+/* Handle error responses from the remote process. */
+
+if (buffer[0] != 0)
+ {
+ const 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, ...)
+{
+gstring gs = { .size = big_buffer_size, .ptr = 0, .s = big_buffer };
+int rc;
+va_list ap;
+
+/*XXX see comment in smtp_write_command() regarding leaving stuff in
+big_buffer */
+
+va_start(ap, format);
+if (!string_vformat(&gs, SVFMT_TAINT_NOCHK, CS format, ap))
+ {
+ va_end(ap);
+ errno = ERRNO_SMTPFORMAT;
+ return FALSE;
+ }
+va_end(ap);
+DEBUG(D_transport|D_v) debug_printf(" LMTP>> %s", string_from_gstring(&gs));
+rc = write(fd, gs.s, gs.ptr);
+gs.ptr -= 2; string_from_gstring(&gs); /* 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_CLR(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)
+ {
+ debug_printf("LMTP input line incomplete in one buffer:\n ");
+ for (int 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;
+uschar *igquotstr = US"";
+uschar *sockname = NULL;
+const 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)
+ {
+ 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, FALSE,
+ buffer, NULL))
+ return FALSE;
+
+ /* If the -N option is set, can't do any more. Presume all has gone well. */
+ if (f.dont_deliver)
+ goto MINUS_N;
+
+/* 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 ((pid = child_open(USS argv, NULL, 0, &fd_in, &fd_out, TRUE,
+ US"lmtp-tpt-cmd")) < 0)
+ {
+ addrlist->message = string_sprintf(
+ "Failed to create child process for %s transport: %s", tblock->name,
+ strerror(errno));
+ 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);
+ if (!(sockname = expand_string(ob->skt)))
+ {
+ 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 (f.dont_deliver)
+ goto MINUS_N;
+
+ 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, US"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 = regex_match(regex_IGNOREQUOTA, buffer, -1, NULL)
+ ? 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 (address_item * addr = addrlist; addr; 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;
+ transport_ctx tctx = {
+ {fd_in},
+ tblock,
+ addrlist,
+ US".", US"..",
+ ob->options
+ };
+
+ 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, US"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(&tctx, 0);
+
+ /* 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, US"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 (address_item * addr = addrlist; addr; 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 (LOGGING(smtp_confirmation))
+ {
+ const uschar *s = string_printing(buffer);
+ /* de-const safe here as string_printing known to have alloc'n'copied */
+ addr->message = (s == buffer)? US string_copy(s) : US 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)
+ {
+ save_errno = errno;
+ check_response(&save_errno, addr->more_errno, buffer, &code,
+ &(addr->message));
+ addr->transport_return = (code == '5')? FAIL : DEFER;
+ for (address_item * a = addr->next; a; 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 = US"Filter process failure";
+else if (errno == ERRNO_WRITEINCOMPLETE)
+ addrlist->message = US"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;
+
+
+MINUS_N:
+ DEBUG(D_transport)
+ debug_printf("*** delivery by %s transport bypassed by -N option",
+ tblock->name);
+ addrlist->transport_return = OK;
+ return FALSE;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of transport/lmtp.c */
diff --git a/src/transports/lmtp.h b/src/transports/lmtp.h
new file mode 100644
index 0000000..93f0f89
--- /dev/null
+++ b/src/transports/lmtp.h
@@ -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
index 0000000..bdbe27d
--- /dev/null
+++ b/src/transports/pipe.c
@@ -0,0 +1,1124 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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.
+*/
+#define LOFF(field) OPT_OFF(pipe_transport_options_block, field)
+
+optionlist pipe_transport_options[] = {
+ { "allow_commands", opt_stringptr, LOFF(allow_commands) },
+ { "batch_id", opt_stringptr | opt_public,
+ OPT_OFF(transport_instance, batch_id) },
+ { "batch_max", opt_int | opt_public,
+ OPT_OFF(transport_instance, batch_max) },
+ { "check_string", opt_stringptr, LOFF(check_string) },
+ { "command", opt_stringptr, LOFF(cmd) },
+ { "environment", opt_stringptr, LOFF(environment) },
+ { "escape_string", opt_stringptr, LOFF(escape_string) },
+ { "force_command", opt_bool, LOFF(force_command) },
+ { "freeze_exec_fail", opt_bool, LOFF(freeze_exec_fail) },
+ { "freeze_signal", opt_bool, LOFF(freeze_signal) },
+ { "ignore_status", opt_bool, LOFF(ignore_status) },
+ { "log_defer_output", opt_bool | opt_public,
+ OPT_OFF(transport_instance, log_defer_output) },
+ { "log_fail_output", opt_bool | opt_public,
+ OPT_OFF(transport_instance, log_fail_output) },
+ { "log_output", opt_bool | opt_public,
+ OPT_OFF(transport_instance, log_output) },
+ { "max_output", opt_mkint, LOFF(max_output) },
+ { "message_prefix", opt_stringptr, LOFF(message_prefix) },
+ { "message_suffix", opt_stringptr, LOFF(message_suffix) },
+ { "path", opt_stringptr, LOFF(path) },
+ { "permit_coredump", opt_bool, LOFF(permit_coredump) },
+ { "pipe_as_creator", opt_bool | opt_public,
+ OPT_OFF(transport_instance, deliver_as_creator) },
+ { "restrict_to_path", opt_bool, LOFF(restrict_to_path) },
+ { "return_fail_output",opt_bool | opt_public,
+ OPT_OFF(transport_instance, return_fail_output) },
+ { "return_output", opt_bool | opt_public,
+ OPT_OFF(transport_instance, return_output) },
+ { "temp_errors", opt_stringptr, LOFF(temp_errors) },
+ { "timeout", opt_time, LOFF(timeout) },
+ { "timeout_defer", opt_bool, LOFF(timeout_defer) },
+ { "umask", opt_octint, LOFF(umask) },
+ { "use_bsmtp", opt_bool, LOFF(use_bsmtp) },
+ #ifdef HAVE_SETCLASSRESOURCES
+ { "use_classresources", opt_bool, LOFF(use_classresources) },
+ #endif
+ { "use_crlf", opt_bool, LOFF(use_crlf) },
+ { "use_shell", opt_bool, LOFF(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);
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+pipe_transport_options_block pipe_transport_option_defaults = {0};
+void pipe_transport_init(transport_instance *tblock) {}
+BOOL pipe_transport_entry(transport_instance *tblock, address_item *addr) {return FALSE;}
+
+#else /*!MACRO_PREDEF*/
+
+
+/* Default private options block for the pipe transport. */
+
+pipe_transport_options_block pipe_transport_option_defaults = {
+ .path = US"/bin:/usr/bin",
+ .temp_errors = US mac_expanded_string(EX_TEMPFAIL) ":"
+ mac_expanded_string(EX_CANTCREAT),
+ .umask = 022,
+ .max_output = 20480,
+ .timeout = 60*60,
+ /* all others null/zero/false */
+};
+
+
+
+/*************************************************
+* 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);
+
+#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(const uschar ***argvptr, uschar *cmd,
+ BOOL expand_arguments, int expand_fail, address_item *addr, uschar *tname,
+ pipe_transport_options_block *ob)
+{
+BOOL permitted = FALSE;
+const uschar **argv;
+
+/* 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). */
+
+if (!transport_set_up_command(argvptr, cmd, expand_arguments, expand_fail,
+ addr, FALSE, string_sprintf("%.50s transport", tname), 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)
+ {
+ int sep = 0;
+ const uschar *s;
+ uschar *p;
+
+ if (!(s = expand_string(ob->allow_commands)))
+ {
+ 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, NULL, 0)))
+ 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)
+ {
+ 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;
+ const uschar *listptr = expand_string(ob->path);
+
+ while ((p = string_nextinlist(&listptr, &sep, NULL, 0)))
+ {
+ 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)
+ {
+ 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(const uschar ***argvptr, uschar *cmd,
+ BOOL expand_arguments, int expand_fail, address_item *addr, uschar *tname)
+{
+const uschar **argv;
+
+*argvptr = argv = store_get((4)*sizeof(uschar *), GET_UNTAINTED);
+
+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. */
+
+if (expand_arguments)
+ {
+ uschar * p = Ustrstr(cmd, "pipe_addresses");
+ gstring * g = NULL;
+
+ DEBUG(D_transport)
+ debug_printf("shell pipe command before expansion:\n %s\n", cmd);
+
+ /* Allow $recipients in the expansion iff it comes from a system filter */
+
+ f.enable_dollar_recipients = addr && addr->parent &&
+ Ustrcmp(addr->parent->address, "system-filter") == 0;
+
+ if (p != NULL && (
+ (p > cmd && p[-1] == '$') ||
+ (p > cmd + 1 && p[-2] == '$' && p[-1] == '{' && p[14] == '}')))
+ {
+ uschar *q = p + 14;
+
+ if (p[-1] == '{') { q++; p--; }
+
+ g = string_get(Ustrlen(cmd) + 64);
+ g = string_catn(g, cmd, p - cmd - 1);
+
+ for (address_item * ad = addr; ad; ad = ad->next)
+ {
+ DEBUG(D_transport) if (is_tainted(ad->address))
+ debug_printf("tainted element '%s' from $pipe_addresses\n", ad->address);
+
+ /*XXX string_append_listele() ? */
+ if (ad != addr) g = string_catn(g, US" ", 1);
+ g = string_cat(g, ad->address);
+ }
+
+ g = string_cat(g, q);
+ argv[2] = (cmd = string_from_gstring(g)) ? expand_string(cmd) : NULL;
+ }
+ else
+ argv[2] = expand_string(cmd);
+
+ f.enable_dollar_recipients = FALSE;
+
+ if (!argv[2])
+ {
+ addr->transport_return = f.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
+ {
+ DEBUG(D_transport)
+ debug_printf("shell pipe command (no expansion):\n %s\n", cmd);
+ argv[2] = cmd;
+ }
+
+argv[3] = US 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;
+const uschar **argv;
+uschar *envp[50];
+const uschar *envlist = ob->environment;
+uschar *cmd, *ss;
+uschar *eol = ob->use_crlf ? US"\r\n" : US"\n";
+transport_ctx tctx = {
+ .tblock = tblock,
+ .addr = addr,
+ .check_string = ob->check_string,
+ .escape_string = ob->escape_string,
+ ob->options | topt_not_socket /* set at initialization time */
+};
+
+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 separate 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 || !*cmd)
+ {
+ addr->transport_return = DEFER;
+ addr->message = string_sprintf("no command specified for %s transport",
+ tblock->name);
+ return FALSE;
+ }
+if (is_tainted(cmd))
+ {
+ DEBUG(D_transport) debug_printf("cmd '%s' is tainted\n", cmd);
+ addr->message = string_sprintf("Tainted '%s' (command "
+ "for %s transport) not permitted", cmd, tblock->name);
+ addr->transport_return = PANIC;
+ 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)
+ {
+ uschar **ss = addr->pipe_expandn;
+ expand_nmax = -1;
+ if (*ss) filter_thisaddress = *ss++;
+ while (*ss)
+ {
+ 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", expand_string(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)
+ envp[envcount++] = string_sprintf("HOST=%s", addr->host_list->name);
+
+if (f.timestamps_utc)
+ envp[envcount++] = US"TZ=UTC";
+else if (timezone_string && timezone_string[0])
+ envp[envcount++] = string_sprintf("TZ=%s", timezone_string);
+
+/* Add any requested items */
+
+if (envlist)
+ if (!(envlist = expand_cstring(envlist)))
+ {
+ 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, NULL, 0)))
+ {
+ if (envcount > nelem(envp) - 2)
+ {
+ addr->transport_return = DEFER;
+ addr->basic_errno = E2BIG;
+ 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 (f.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(USS argv, envp, ob->umask, &fd_in, &fd_out, TRUE,
+ US"pipe-tpt-cmd")) < 0)
+ {
+ addr->transport_return = DEFER;
+ addr->message = string_sprintf(
+ "Failed to create child process for %s transport: %s", tblock->name,
+ strerror(errno));
+ return FALSE;
+ }
+tctx.u.fd = fd_in;
+
+/* Now fork a process to handle the output that comes down the pipe. */
+
+if ((outpid = exim_fork(US"pipe-tpt-output")) < 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.) */
+
+testharness_pause_ms(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)
+ {
+ uschar *prefix = expand_string(ob->message_prefix);
+ if (!prefix)
+ {
+ addr->transport_return = f.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(&tctx, prefix, Ustrlen(prefix), FALSE))
+ 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)
+ {
+ if (!transport_write_string(fd_in, "MAIL FROM:<%s>%s", return_path, eol))
+ goto END_WRITE;
+
+ for (address_item * a = addr; a; 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 */
+
+if (!transport_write_message(&tctx, 0))
+ goto END_WRITE;
+
+/* Now any configured suffix */
+
+if (ob->message_suffix)
+ {
+ uschar *suffix = expand_string(ob->message_suffix);
+ if (!suffix)
+ {
+ addr->transport_return = f.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(&tctx, suffix, Ustrlen(suffix), FALSE))
+ 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",
+ f.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 = US"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
+ ? string_sprintf(" (preceded by %s)", addr->message) : US"";
+
+ /* 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 (f.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 defined 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;
+ gstring * g;
+
+ /* If temp_errors is "*" all codes are temporary. Initialization 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
+ {
+ const uschar *s = ob->temp_errors;
+ uschar *p;
+ int sep = 0;
+
+ addr->transport_return = FAIL;
+ while ((p = string_nextinlist(&s,&sep,NULL,0)))
+ 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);
+ g = string_cat(NULL, addr->message);
+
+ /* 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)
+ {
+ g = string_catn(g, US" ", 1);
+ g = string_cat (g, ss);
+ }
+
+ /* Now add the command and arguments */
+
+ g = string_catn(g, US" from command:", 14);
+
+ for (int i = 0; i < sizeof(argv)/sizeof(int *) && argv[i] != NULL; i++)
+ {
+ BOOL quote = FALSE;
+ g = string_catn(g, US" ", 1);
+ if (Ustrpbrk(argv[i], " \t") != NULL)
+ {
+ quote = TRUE;
+ g = string_catn(g, US"\"", 1);
+ }
+ g = string_cat(g, argv[i]);
+ if (quote)
+ g = string_catn(g, US"\"", 1);
+ }
+
+ /* Add previous filter timeout message, if present. */
+
+ if (*tmsg)
+ g = string_cat(g, tmsg);
+
+ addr->message = string_from_gstring(g);
+ }
+ }
+ }
+
+/* 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;
+}
+
+#endif /*!MACRO_PREDEF*/
+/* End of transport/pipe.c */
diff --git a/src/transports/pipe.h b/src/transports/pipe.h
new file mode 100644
index 0000000..ed5c142
--- /dev/null
+++ b/src/transports/pipe.h
@@ -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/queuefile.c b/src/transports/queuefile.c
new file mode 100644
index 0000000..74131cc
--- /dev/null
+++ b/src/transports/queuefile.c
@@ -0,0 +1,286 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Andrew Colin Kissa <andrew@topdog.za.net> 2016 */
+/* Copyright (c) University of Cambridge 2016 */
+/* Copyright (c) The Exim Maintainers 1995 - 2021 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+
+#include "../exim.h"
+
+#ifdef EXPERIMENTAL_QUEUEFILE /* whole file */
+#include "queuefile.h"
+
+#ifndef EXIM_HAVE_OPENAT
+# error queuefile transport reqires openat() support
+#endif
+
+/* 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 queuefile_transport_options[] = {
+ { "directory", opt_stringptr,
+ OPT_OFF(queuefile_transport_options_block, dirname) },
+};
+
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int queuefile_transport_options_count =
+ sizeof(queuefile_transport_options) / sizeof(optionlist);
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+queuefile_transport_options_block queuefile_transport_option_defaults = {0};
+void queuefile_transport_init(transport_instance *tblock) {}
+BOOL queuefile_transport_entry(transport_instance *tblock, address_item *addr) {return FALSE;}
+
+#else /*!MACRO_PREDEF*/
+
+
+
+/* Default private options block for the appendfile transport. */
+
+queuefile_transport_options_block queuefile_transport_option_defaults = {
+ NULL, /* dirname */
+};
+
+/*************************************************
+* Initialization entry point *
+*************************************************/
+
+void queuefile_transport_init(transport_instance *tblock)
+{
+queuefile_transport_options_block *ob =
+ (queuefile_transport_options_block *) tblock->options_block;
+
+if (!ob->dirname)
+ log_write(0, LOG_PANIC_DIE | LOG_CONFIG,
+ "directory must be set for the %s transport", tblock->name);
+}
+
+/* This function will copy from a file to another
+
+Arguments:
+ dst fd to write to (the destination queue file)
+ src fd to read from (the spool queue file)
+
+Returns: TRUE if all went well, FALSE otherwise with errno set
+*/
+
+static BOOL
+copy_spool_file(int dst, int src)
+{
+int i, j;
+uschar buffer[16384];
+
+if (lseek(src, 0, SEEK_SET) != 0)
+ return FALSE;
+
+do
+ if ((j = read(src, buffer, sizeof(buffer))) > 0)
+ for (uschar * s = buffer; (i = write(dst, s, j)) != j; s += i, j -= i)
+ if (i < 0)
+ return FALSE;
+ else if (j < 0)
+ return FALSE;
+while (j > 0);
+return TRUE;
+}
+
+/* This function performs the actual copying of the header
+and data files to the destination directory
+
+Arguments:
+ tb the transport block
+ addr address_item being processed
+ dstpath destination directory name
+ sdfd int Source directory fd
+ ddfd int Destination directory fd
+ link_file BOOL use linkat instead of data copy
+ srcfd fd for data file, or -1 for header file
+
+Returns: TRUE if all went well, FALSE otherwise
+*/
+
+static BOOL
+copy_spool_files(transport_instance * tb, address_item * addr,
+ const uschar * dstpath, int sdfd, int ddfd, BOOL link_file, int srcfd)
+{
+BOOL is_hdr_file = srcfd < 0;
+const uschar * suffix = srcfd < 0 ? US"H" : US"D";
+int dstfd;
+const uschar * filename = string_sprintf("%s-%s", message_id, suffix);
+const uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix);
+const uschar * s, * op;
+
+dstpath = string_sprintf("%s/%s-%s", dstpath, message_id, suffix);
+
+if (link_file)
+ {
+ DEBUG(D_transport) debug_printf("%s transport, linking %s => %s\n",
+ tb->name, srcpath, dstpath);
+
+ if (linkat(sdfd, CCS filename, ddfd, CCS filename, 0) >= 0)
+ return TRUE;
+
+ op = US"linking";
+ s = dstpath;
+ }
+else /* use data copy */
+ {
+ DEBUG(D_transport) debug_printf("%s transport, copying %s => %s\n",
+ tb->name, srcpath, dstpath);
+
+ if ( (s = dstpath,
+ (dstfd = exim_openat4(ddfd, CCS filename, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE))
+ < 0
+ )
+ || is_hdr_file
+ && (s = srcpath, (srcfd = exim_openat(sdfd, CCS filename, O_RDONLY)) < 0)
+ )
+ op = US"opening";
+
+ else
+ if (s = dstpath, fchmod(dstfd, SPOOL_MODE) != 0)
+ op = US"setting perms on";
+ else
+ if (!copy_spool_file(dstfd, srcfd))
+ op = US"creating";
+ else
+ return TRUE;
+ }
+
+addr->basic_errno = errno;
+addr->message = string_sprintf("%s transport %s file: %s failed with error: %s",
+ tb->name, op, s, strerror(errno));
+addr->transport_return = DEFER;
+return FALSE;
+}
+
+/*************************************************
+* Main entry point *
+*************************************************/
+
+/* This transport always returns FALSE, indicating that the status in
+the first address is the status for all addresses in a batch. */
+
+BOOL
+queuefile_transport_entry(transport_instance * tblock, address_item * addr)
+{
+queuefile_transport_options_block * ob =
+ (queuefile_transport_options_block *) tblock->options_block;
+BOOL can_link;
+uschar * sourcedir = spool_dname(US"input", message_subdir);
+uschar * s, * dstdir;
+struct stat dstatbuf, sstatbuf;
+int ddfd = -1, sdfd = -1;
+
+DEBUG(D_transport)
+ debug_printf("%s transport entered\n", tblock->name);
+
+#ifndef O_DIRECTORY
+# define O_DIRECTORY 0
+#endif
+#ifndef O_NOFOLLOW
+# define O_NOFOLLOW 0
+#endif
+
+if (!(dstdir = expand_string(ob->dirname)))
+ {
+ addr->message = string_sprintf("%s transport: failed to expand dirname option",
+ tblock->name);
+ addr->transport_return = DEFER;
+ return FALSE;
+ }
+if (*dstdir != '/')
+ {
+ addr->transport_return = PANIC;
+ addr->message = string_sprintf("%s transport directory: "
+ "%s is not absolute", tblock->name, dstdir);
+ return FALSE;
+ }
+
+/* Open the source and destination directories and check if they are
+on the same filesystem, so we can hard-link files rather than copying. */
+
+if ( (s = dstdir,
+ (ddfd = Uopen(s, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
+ || (s = sourcedir,
+ (sdfd = Uopen(sourcedir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
+ )
+ {
+ addr->transport_return = PANIC;
+ addr->basic_errno = errno;
+ addr->message = string_sprintf("%s transport accessing directory: %s "
+ "failed with error: %s", tblock->name, s, strerror(errno));
+ if (ddfd >= 0) (void) close(ddfd);
+ return FALSE;
+ }
+
+if ( (s = dstdir, fstat(ddfd, &dstatbuf) < 0)
+ || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0)
+ )
+ {
+ addr->transport_return = PANIC;
+ addr->basic_errno = errno;
+ addr->message = string_sprintf("%s transport fstat on directory fd: "
+ "%s failed with error: %s", tblock->name, s, strerror(errno));
+ goto RETURN;
+ }
+can_link = (dstatbuf.st_dev == sstatbuf.st_dev);
+
+if (f.dont_deliver)
+ {
+ DEBUG(D_transport)
+ debug_printf("*** delivery by %s transport bypassed by -N option\n",
+ tblock->name);
+ addr->transport_return = OK;
+ goto RETURN;
+ }
+
+/* Link or copy the header and data spool files */
+
+DEBUG(D_transport)
+ debug_printf("%s transport, copying header file\n", tblock->name);
+
+if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link, -1))
+ goto RETURN;
+
+DEBUG(D_transport)
+ debug_printf("%s transport, copying data file\n", tblock->name);
+
+if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link,
+ deliver_datafile))
+ {
+ DEBUG(D_transport)
+ debug_printf("%s transport, copying data file failed, "
+ "unlinking the header file\n", tblock->name);
+ Uunlink(string_sprintf("%s/%s-H", dstdir, message_id));
+ goto RETURN;
+ }
+
+DEBUG(D_transport)
+ debug_printf("%s transport succeeded\n", tblock->name);
+
+addr->transport_return = OK;
+
+RETURN:
+if (ddfd >= 0) (void) close(ddfd);
+if (sdfd >= 0) (void) close(sdfd);
+
+/* 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;
+}
+
+#endif /*!MACRO_PREDEF*/
+#endif /*EXPERIMENTAL_QUEUEFILE*/
diff --git a/src/transports/queuefile.h b/src/transports/queuefile.h
new file mode 100644
index 0000000..0e45b51
--- /dev/null
+++ b/src/transports/queuefile.h
@@ -0,0 +1,29 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) Andrew Colin Kissa <andrew@topdog.za.net> 2016 */
+/* Copyright (c) University of Cambridge 2016 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Private structure for the private options. */
+
+typedef struct {
+ uschar *dirname;
+} queuefile_transport_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist queuefile_transport_options[];
+extern int queuefile_transport_options_count;
+
+/* Block containing default values. */
+
+extern queuefile_transport_options_block queuefile_transport_option_defaults;
+
+/* The main and init entry points for the transport */
+
+extern BOOL queuefile_transport_entry(transport_instance *, address_item *);
+extern void queuefile_transport_init(transport_instance *);
+
+/* End of transports/queuefile.h */
diff --git a/src/transports/smtp.c b/src/transports/smtp.c
new file mode 100644
index 0000000..7f529b7
--- /dev/null
+++ b/src/transports/smtp.c
@@ -0,0 +1,6071 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "smtp.h"
+
+#if defined(SUPPORT_DANE) && defined(DISABLE_TLS)
+# error TLS is required for DANE
+#endif
+
+
+/* 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. */
+
+#define LOFF(field) OPT_OFF(smtp_transport_options_block, field)
+
+optionlist smtp_transport_options[] = {
+ { "*expand_multi_domain", opt_stringptr | opt_hidden | opt_public,
+ OPT_OFF(transport_instance, expand_multi_domain) },
+ { "*expand_retry_include_ip_address", opt_stringptr | opt_hidden,
+ LOFF(expand_retry_include_ip_address) },
+
+ { "address_retry_include_sender", opt_bool,
+ LOFF(address_retry_include_sender) },
+ { "allow_localhost", opt_bool, LOFF(allow_localhost) },
+#ifdef EXPERIMENTAL_ARC
+ { "arc_sign", opt_stringptr, LOFF(arc_sign) },
+#endif
+ { "authenticated_sender", opt_stringptr, LOFF(authenticated_sender) },
+ { "authenticated_sender_force", opt_bool, LOFF(authenticated_sender_force) },
+ { "command_timeout", opt_time, LOFF(command_timeout) },
+ { "connect_timeout", opt_time, LOFF(connect_timeout) },
+ { "connection_max_messages", opt_int | opt_public,
+ OPT_OFF(transport_instance, connection_max_messages) },
+# ifdef SUPPORT_DANE
+ { "dane_require_tls_ciphers", opt_stringptr, LOFF(dane_require_tls_ciphers) },
+# endif
+ { "data_timeout", opt_time, LOFF(data_timeout) },
+ { "delay_after_cutoff", opt_bool, LOFF(delay_after_cutoff) },
+#ifndef DISABLE_DKIM
+ { "dkim_canon", opt_stringptr, LOFF(dkim.dkim_canon) },
+ { "dkim_domain", opt_stringptr, LOFF(dkim.dkim_domain) },
+ { "dkim_hash", opt_stringptr, LOFF(dkim.dkim_hash) },
+ { "dkim_identity", opt_stringptr, LOFF(dkim.dkim_identity) },
+ { "dkim_private_key", opt_stringptr, LOFF(dkim.dkim_private_key) },
+ { "dkim_selector", opt_stringptr, LOFF(dkim.dkim_selector) },
+ { "dkim_sign_headers", opt_stringptr, LOFF(dkim.dkim_sign_headers) },
+ { "dkim_strict", opt_stringptr, LOFF(dkim.dkim_strict) },
+ { "dkim_timestamps", opt_stringptr, LOFF(dkim.dkim_timestamps) },
+#endif
+ { "dns_qualify_single", opt_bool, LOFF(dns_qualify_single) },
+ { "dns_search_parents", opt_bool, LOFF(dns_search_parents) },
+ { "dnssec_request_domains", opt_stringptr, LOFF(dnssec.request) },
+ { "dnssec_require_domains", opt_stringptr, LOFF(dnssec.require) },
+ { "dscp", opt_stringptr, LOFF(dscp) },
+ { "fallback_hosts", opt_stringptr, LOFF(fallback_hosts) },
+ { "final_timeout", opt_time, LOFF(final_timeout) },
+ { "gethostbyname", opt_bool, LOFF(gethostbyname) },
+ { "helo_data", opt_stringptr, LOFF(helo_data) },
+#if !defined(DISABLE_TLS) && !defined(DISABLE_TLS_RESUME)
+ { "host_name_extract", opt_stringptr, LOFF(host_name_extract) },
+# endif
+ { "hosts", opt_stringptr, LOFF(hosts) },
+ { "hosts_avoid_esmtp", opt_stringptr, LOFF(hosts_avoid_esmtp) },
+ { "hosts_avoid_pipelining", opt_stringptr, LOFF(hosts_avoid_pipelining) },
+#ifndef DISABLE_TLS
+ { "hosts_avoid_tls", opt_stringptr, LOFF(hosts_avoid_tls) },
+#endif
+ { "hosts_max_try", opt_int, LOFF(hosts_max_try) },
+ { "hosts_max_try_hardlimit", opt_int, LOFF(hosts_max_try_hardlimit) },
+#ifndef DISABLE_TLS
+ { "hosts_nopass_tls", opt_stringptr, LOFF(hosts_nopass_tls) },
+ { "hosts_noproxy_tls", opt_stringptr, LOFF(hosts_noproxy_tls) },
+#endif
+ { "hosts_override", opt_bool, LOFF(hosts_override) },
+#ifndef DISABLE_PIPE_CONNECT
+ { "hosts_pipe_connect", opt_stringptr, LOFF(hosts_pipe_connect) },
+#endif
+ { "hosts_randomize", opt_bool, LOFF(hosts_randomize) },
+#if !defined(DISABLE_TLS) && !defined(DISABLE_OCSP)
+ { "hosts_request_ocsp", opt_stringptr, LOFF(hosts_request_ocsp) },
+#endif
+ { "hosts_require_alpn", opt_stringptr, LOFF(hosts_require_alpn) },
+ { "hosts_require_auth", opt_stringptr, LOFF(hosts_require_auth) },
+#ifndef DISABLE_TLS
+# ifdef SUPPORT_DANE
+ { "hosts_require_dane", opt_stringptr, LOFF(hosts_require_dane) },
+# endif
+# ifndef DISABLE_OCSP
+ { "hosts_require_ocsp", opt_stringptr, LOFF(hosts_require_ocsp) },
+# endif
+ { "hosts_require_tls", opt_stringptr, LOFF(hosts_require_tls) },
+#endif
+ { "hosts_try_auth", opt_stringptr, LOFF(hosts_try_auth) },
+ { "hosts_try_chunking", opt_stringptr, LOFF(hosts_try_chunking) },
+#ifdef SUPPORT_DANE
+ { "hosts_try_dane", opt_stringptr, LOFF(hosts_try_dane) },
+#endif
+ { "hosts_try_fastopen", opt_stringptr, LOFF(hosts_try_fastopen) },
+#ifndef DISABLE_PRDR
+ { "hosts_try_prdr", opt_stringptr, LOFF(hosts_try_prdr) },
+#endif
+#ifndef DISABLE_TLS
+ { "hosts_verify_avoid_tls", opt_stringptr, LOFF(hosts_verify_avoid_tls) },
+#endif
+ { "interface", opt_stringptr, LOFF(interface) },
+ { "keepalive", opt_bool, LOFF(keepalive) },
+ { "lmtp_ignore_quota", opt_bool, LOFF(lmtp_ignore_quota) },
+ { "max_rcpt", opt_int | opt_public,
+ OPT_OFF(transport_instance, max_addresses) },
+ { "message_linelength_limit", opt_int, LOFF(message_linelength_limit) },
+ { "multi_domain", opt_expand_bool | opt_public,
+ OPT_OFF(transport_instance, multi_domain) },
+ { "port", opt_stringptr, LOFF(port) },
+ { "protocol", opt_stringptr, LOFF(protocol) },
+ { "retry_include_ip_address", opt_expand_bool, LOFF(retry_include_ip_address) },
+ { "serialize_hosts", opt_stringptr, LOFF(serialize_hosts) },
+ { "size_addition", opt_int, LOFF(size_addition) },
+#ifdef SUPPORT_SOCKS
+ { "socks_proxy", opt_stringptr, LOFF(socks_proxy) },
+#endif
+#ifndef DISABLE_TLS
+ { "tls_alpn", opt_stringptr, LOFF(tls_alpn) },
+ { "tls_certificate", opt_stringptr, LOFF(tls_certificate) },
+ { "tls_crl", opt_stringptr, LOFF(tls_crl) },
+ { "tls_dh_min_bits", opt_int, LOFF(tls_dh_min_bits) },
+ { "tls_privatekey", opt_stringptr, LOFF(tls_privatekey) },
+ { "tls_require_ciphers", opt_stringptr, LOFF(tls_require_ciphers) },
+# ifndef DISABLE_TLS_RESUME
+ { "tls_resumption_hosts", opt_stringptr, LOFF(tls_resumption_hosts) },
+# endif
+ { "tls_sni", opt_stringptr, LOFF(tls_sni) },
+ { "tls_tempfail_tryclear", opt_bool, LOFF(tls_tempfail_tryclear) },
+ { "tls_try_verify_hosts", opt_stringptr, LOFF(tls_try_verify_hosts) },
+ { "tls_verify_cert_hostnames", opt_stringptr, LOFF(tls_verify_cert_hostnames)},
+ { "tls_verify_certificates", opt_stringptr, LOFF(tls_verify_certificates) },
+ { "tls_verify_hosts", opt_stringptr, LOFF(tls_verify_hosts) },
+#endif
+#ifdef SUPPORT_I18N
+ { "utf8_downconvert", opt_stringptr, LOFF(utf8_downconvert) },
+#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 = nelem(smtp_transport_options);
+
+
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+smtp_transport_options_block smtp_transport_option_defaults = {0};
+void smtp_transport_init(transport_instance *tblock) {}
+BOOL smtp_transport_entry(transport_instance *tblock, address_item *addr) {return FALSE;}
+void smtp_transport_closedown(transport_instance *tblock) {}
+
+#else /*!MACRO_PREDEF*/
+
+
+/* Default private options block for the smtp transport. */
+
+smtp_transport_options_block smtp_transport_option_defaults = {
+ /* All non-mentioned elements 0/NULL/FALSE */
+ .helo_data = US"$primary_hostname",
+ .protocol = US"smtp",
+ .hosts_try_chunking = US"*",
+#ifdef SUPPORT_DANE
+ .hosts_try_dane = US"*",
+#endif
+ .hosts_try_fastopen = US"*",
+#ifndef DISABLE_PRDR
+ .hosts_try_prdr = US"*",
+#endif
+#ifndef DISABLE_OCSP
+ .hosts_request_ocsp = US"*", /* hosts_request_ocsp (except under DANE; tls_client_start()) */
+#endif
+ .command_timeout = 5*60,
+ .connect_timeout = 5*60,
+ .data_timeout = 5*60,
+ .final_timeout = 10*60,
+ .size_addition = 1024,
+ .hosts_max_try = 5,
+ .hosts_max_try_hardlimit = 50,
+ .message_linelength_limit = 998,
+ .address_retry_include_sender = TRUE,
+ .dns_qualify_single = TRUE,
+ .dnssec = { .request= US"*", .require=NULL },
+ .delay_after_cutoff = TRUE,
+ .keepalive = TRUE,
+ .retry_include_ip_address = TRUE,
+#ifndef DISABLE_TLS
+ .tls_verify_certificates = US"system",
+ .tls_dh_min_bits = EXIM_CLIENT_DH_DEFAULT_MIN_BITS,
+ .tls_tempfail_tryclear = TRUE,
+ .tls_try_verify_hosts = US"*",
+ .tls_verify_cert_hostnames = US"*",
+# ifndef DISABLE_TLS_RESUME
+ .host_name_extract = US"${if and {{match{$host}{.outlook.com\\$}} {match{$item}{\\N^250-([\\w.]+)\\s\\N}}} {$1}}",
+# endif
+#endif
+#ifdef SUPPORT_I18N
+ .utf8_downconvert = US"-1",
+#endif
+#ifndef DISABLE_DKIM
+ .dkim =
+ { .dkim_hash = US"sha256", },
+#endif
+};
+
+/* 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[] = { US"NEVER", US"SUCCESS", US"FAILURE", US"DELAY" };
+
+
+
+/* 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 uschar *data_command = US""; /* Points to DATA cmd for error messages */
+static BOOL update_waiting; /* TRUE to update the "wait" database */
+
+/*XXX move to smtp_context */
+static BOOL pipelining_active; /* current transaction is in pipe mode */
+
+
+static unsigned ehlo_response(uschar * buf, unsigned checks);
+
+
+/******************************************************************************/
+
+void
+smtp_deliver_init(void)
+{
+struct list
+ {
+ const pcre2_code ** re;
+ const uschar * string;
+ } list[] =
+ {
+ { &regex_AUTH, AUTHS_REGEX },
+ { &regex_CHUNKING, US"\\n250[\\s\\-]CHUNKING(\\s|\\n|$)" },
+ { &regex_DSN, US"\\n250[\\s\\-]DSN(\\s|\\n|$)" },
+ { &regex_IGNOREQUOTA, US"\\n250[\\s\\-]IGNOREQUOTA(\\s|\\n|$)" },
+ { &regex_PIPELINING, US"\\n250[\\s\\-]PIPELINING(\\s|\\n|$)" },
+ { &regex_SIZE, US"\\n250[\\s\\-]SIZE(\\s|\\n|$)" },
+
+#ifndef DISABLE_TLS
+ { &regex_STARTTLS, US"\\n250[\\s\\-]STARTTLS(\\s|\\n|$)" },
+#endif
+#ifndef DISABLE_PRDR
+ { &regex_PRDR, US"\\n250[\\s\\-]PRDR(\\s|\\n|$)" },
+#endif
+#ifdef SUPPORT_I18N
+ { &regex_UTF8, US"\\n250[\\s\\-]SMTPUTF8(\\s|\\n|$)" },
+#endif
+#ifndef DISABLE_PIPE_CONNECT
+ { &regex_EARLY_PIPE, US"\\n250[\\s\\-]" EARLY_PIPE_FEATURE_NAME "(\\s|\\n|$)" },
+#endif
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ { &regex_LIMITS, US"\\n250[\\s\\-]LIMITS\\s" },
+#endif
+ };
+
+for (struct list * l = list; l < list + nelem(list); l++)
+ if (!*l->re)
+ *l->re = regex_must_compile(l->string, FALSE, TRUE);
+}
+
+
+/*************************************************
+* 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 = SOB tblock->options_block;
+
+/* Pass back options if required. This interface is getting very messy. */
+
+if (tf)
+ {
+ 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; addrlist = addrlist->next)
+ if (!addrlist->fallback_hosts) 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 = SOB tblock->options_block;
+int old_pool = store_pool;
+
+/* 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)
+ 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) 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. */
+
+store_pool = POOL_PERM;
+host_build_hostlist(&ob->fallback_hostlist, ob->fallback_hosts, FALSE);
+store_pool = old_pool;
+}
+
+
+
+
+
+/*************************************************
+* 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
+ host if set, mark addrs as having used this host
+ smtp_greeting from peer
+ helo_response from peer
+ start points to timestamp of delivery start
+
+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, host_item * host,
+#ifdef EXPERIMENTAL_DSN_INFO
+ const uschar * smtp_greeting, const uschar * helo_response,
+#endif
+ struct timeval * start
+ )
+{
+int orvalue = 0;
+struct timeval deliver_time;
+
+if (errno_value == ERRNO_CONNECTTIMEOUT)
+ {
+ errno_value = ETIMEDOUT;
+ orvalue = RTEF_CTOUT;
+ }
+timesince(&deliver_time, start);
+
+for (address_item * addr = addrlist; addr; addr = addr->next)
+ if (addr->transport_return >= PENDING)
+ {
+ addr->basic_errno = errno_value;
+ addr->more_errno |= orvalue;
+ addr->delivery_time = deliver_time;
+ if (msg)
+ {
+ addr->message = msg;
+ if (pass_message) setflag(addr, af_pass_message);
+ }
+ addr->transport_return = rc;
+ if (host)
+ {
+ addr->host_used = host;
+#ifdef EXPERIMENTAL_DSN_INFO
+ if (smtp_greeting)
+ {uschar * s = Ustrchr(smtp_greeting, '\n'); if (s) *s = '\0';}
+ addr->smtp_greeting = smtp_greeting;
+
+ if (helo_response)
+ {uschar * s = Ustrchr(helo_response, '\n'); if (s) *s = '\0';}
+ addr->helo_response = helo_response;
+#endif
+ }
+ }
+}
+
+static void
+set_errno_nohost(address_item * addrlist, int errno_value, uschar * msg, int rc,
+ BOOL pass_message, struct timeval * start)
+{
+set_errno(addrlist, errno_value, msg, rc, pass_message, NULL,
+#ifdef EXPERIMENTAL_DSN_INFO
+ NULL, NULL,
+#endif
+ start);
+}
+
+
+/*************************************************
+* 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 error 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 = pipelining_active ? US"pipelined " : US"";
+const uschar * s;
+
+*yield = '4'; /* Default setting is to give a temporary error */
+
+switch(*errno_value)
+ {
+ case ETIMEDOUT: /* Handle response timeout */
+ *message = US string_sprintf("SMTP timeout after %s%s",
+ pl, smtp_command);
+ if (transport_count > 0)
+ *message = US string_sprintf("%s (%d bytes written)", *message,
+ transport_count);
+ return FALSE;
+
+ case ERRNO_SMTPFORMAT: /* Handle malformed SMTP response */
+ s = string_printing(buffer);
+ while (isspace(*s)) s++;
+ *message = *s == 0
+ ? string_sprintf("Malformed SMTP reply (an empty line) "
+ "in response to %s%s", pl, smtp_command)
+ : string_sprintf("Malformed SMTP reply in response to %s%s: %s",
+ pl, smtp_command, s);
+ return FALSE;
+
+ case ERRNO_TLSFAILURE: /* Handle bad first read; can happen with
+ GnuTLS and TLS1.3 */
+ *message = US"bad first read from TLS conn";
+ return TRUE;
+
+ case ERRNO_FILTER_FAIL: /* Handle a failed filter process error;
+ can't send QUIT as we mustn't end the DATA. */
+ *message = string_sprintf("transport filter process failed (%d)%s",
+ more_errno,
+ more_errno == EX_EXECFAILED ? ": unable to execute command" : "");
+ return FALSE;
+
+ case ERRNO_CHHEADER_FAIL: /* Handle a failed add_headers expansion;
+ can't send QUIT as we mustn't end the DATA. */
+ *message =
+ string_sprintf("failed to expand headers_add or headers_remove: %s",
+ expand_string_message);
+ return FALSE;
+
+ case ERRNO_WRITEINCOMPLETE: /* failure to write a complete data block */
+ *message = US"failed to write a data block";
+ return FALSE;
+
+#ifdef SUPPORT_I18N
+ case ERRNO_UTF8_FWD: /* no advertised SMTPUTF8, for international message */
+ *message = US"utf8 support required but not offered for forwarding";
+ DEBUG(D_deliver|D_transport) debug_printf("%s\n", *message);
+ return TRUE;
+#endif
+ }
+
+/* Handle error responses from the remote mailer. */
+
+if (buffer[0] != 0)
+ {
+ *message = string_sprintf("SMTP error from remote mail server after %s%s: "
+ "%s", pl, smtp_command, s = string_printing(buffer));
+ *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 closed connection "
+ "in response to %s%s", 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:
+ host the current host
+ detail the current message (addr_item->message)
+ basic_errno the errno (addr_item->basic_errno)
+
+Returns: nothing
+*/
+
+static void
+write_logs(const host_item *host, const uschar *suffix, int basic_errno)
+{
+gstring * message = LOGGING(outgoing_port)
+ ? string_fmt_append(NULL, "H=%s [%s]:%d", host->name, host->address,
+ host->port == PORT_NONE ? 25 : host->port)
+ : string_fmt_append(NULL, "H=%s [%s]", host->name, host->address);
+
+if (suffix)
+ {
+ message = string_fmt_append(message, ": %s", suffix);
+ if (basic_errno > 0)
+ message = string_fmt_append(message, ": %s", strerror(basic_errno));
+ }
+else
+ message = string_fmt_append(message, " %s", exim_errstr(basic_errno));
+
+log_write(0, LOG_MAIN, "%s", string_from_gstring(message));
+deliver_msglog("%s %s\n", tod_stamp(tod_log), message->s);
+}
+
+static void
+msglog_line(host_item * host, uschar * message)
+{
+deliver_msglog("%s H=%s [%s] %s\n", tod_stamp(tod_log),
+ host->name, host->address, message);
+}
+
+
+
+#ifndef DISABLE_EVENT
+/*************************************************
+* Post-defer action *
+*************************************************/
+
+/* This expands an arbitrary per-transport string.
+ It might, for example, be used to write to the database log.
+
+Arguments:
+ addr the address item containing error information
+ host the current host
+ evstr the event
+
+Returns: nothing
+*/
+
+static void
+deferred_event_raise(address_item * addr, host_item * host, uschar * evstr)
+{
+uschar * action = addr->transport->event_action;
+const uschar * save_domain;
+uschar * save_local;
+
+if (!action)
+ return;
+
+save_domain = deliver_domain;
+save_local = deliver_localpart;
+
+/*XXX would ip & port already be set up? */
+deliver_host_address = string_copy(host->address);
+deliver_host_port = host->port == PORT_NONE ? 25 : host->port;
+event_defer_errno = addr->basic_errno;
+
+router_name = addr->router->name;
+transport_name = addr->transport->name;
+deliver_domain = addr->domain;
+deliver_localpart = addr->local_part;
+
+(void) event_raise(action, evstr,
+ 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,
+ NULL);
+
+deliver_localpart = save_local;
+deliver_domain = save_domain;
+router_name = transport_name = NULL;
+}
+#endif
+
+/*************************************************
+* Reap SMTP specific responses *
+*************************************************/
+static int
+smtp_discard_responses(smtp_context * sx, smtp_transport_options_block * ob,
+ int count)
+{
+uschar flushbuffer[4096];
+
+while (count-- > 0)
+ {
+ if (!smtp_read_response(sx, flushbuffer, sizeof(flushbuffer),
+ '2', ob->command_timeout)
+ && (errno != 0 || flushbuffer[0] == 0))
+ break;
+ }
+return count;
+}
+
+
+/* Return boolean success */
+
+static BOOL
+smtp_reap_banner(smtp_context * sx)
+{
+BOOL good_response;
+#if defined(__linux__) && defined(TCP_QUICKACK)
+ { /* Hack to get QUICKACK disabled; has to be right after 3whs, and has to on->off */
+ int sock = sx->cctx.sock;
+ struct pollfd p = {.fd = sock, .events = POLLOUT};
+ if (poll(&p, 1, 1000) >= 0) /* retval test solely for compiler quitening */
+ {
+ (void) setsockopt(sock, IPPROTO_TCP, TCP_QUICKACK, US &on, sizeof(on));
+ (void) setsockopt(sock, IPPROTO_TCP, TCP_QUICKACK, US &off, sizeof(off));
+ }
+ }
+#endif
+good_response = smtp_read_response(sx, sx->buffer, sizeof(sx->buffer),
+ '2', (SOB sx->conn_args.ob)->command_timeout);
+#ifdef EXPERIMENTAL_DSN_INFO
+sx->smtp_greeting = string_copy(sx->buffer);
+#endif
+return good_response;
+}
+
+static BOOL
+smtp_reap_ehlo(smtp_context * sx)
+{
+if (!smtp_read_response(sx, sx->buffer, sizeof(sx->buffer), '2',
+ (SOB sx->conn_args.ob)->command_timeout))
+ {
+ if (errno != 0 || sx->buffer[0] == 0 || sx->lmtp)
+ {
+#ifdef EXPERIMENTAL_DSN_INFO
+ sx->helo_response = string_copy(sx->buffer);
+#endif
+ return FALSE;
+ }
+ sx->esmtp = FALSE;
+ }
+#ifdef EXPERIMENTAL_DSN_INFO
+sx->helo_response = string_copy(sx->buffer);
+#endif
+#ifndef DISABLE_EVENT
+(void) event_raise(sx->conn_args.tblock->event_action,
+ US"smtp:ehlo", sx->buffer, NULL);
+#endif
+return TRUE;
+}
+
+
+/* Grab a string differentiating server behind a loadbalancer, for TLS
+resumption when such servers do not share a session-cache */
+
+static void
+ehlo_response_lbserver(smtp_context * sx, smtp_transport_options_block * ob)
+{
+#if !defined(DISABLE_TLS) && !defined(DISABLE_TLS_RESUME)
+const uschar * s;
+uschar * save_item = iterate_item;
+
+if (sx->conn_args.have_lbserver)
+ return;
+iterate_item = sx->buffer;
+s = expand_cstring(ob->host_name_extract);
+iterate_item = save_item;
+sx->conn_args.host_lbserver = s && !*s ? NULL : s;
+sx->conn_args.have_lbserver = TRUE;
+#endif
+}
+
+
+
+/******************************************************************************/
+
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+/* If TLS, or TLS not offered, called with the EHLO response in the buffer.
+Check it for a LIMITS keyword and parse values into the smtp context structure.
+
+We don't bother with peers that we won't talk TLS to, even though they can,
+just ignore their LIMITS advice (if any) and treat them as if they do not.
+This saves us dealing with a duplicate set of values. */
+
+static void
+ehlo_response_limits_read(smtp_context * sx)
+{
+uschar * match;
+
+/* matches up to just after the first space after the keyword */
+
+if (regex_match(regex_LIMITS, sx->buffer, -1, &match))
+ for (const uschar * s = sx->buffer + Ustrlen(match); *s; )
+ {
+ while (isspace(*s)) s++;
+ if (*s == '\n') break;
+
+ if (strncmpic(s, US"MAILMAX=", 8) == 0)
+ {
+ sx->peer_limit_mail = atoi(CS (s += 8));
+ while (isdigit(*s)) s++;
+ }
+ else if (strncmpic(s, US"RCPTMAX=", 8) == 0)
+ {
+ sx->peer_limit_rcpt = atoi(CS (s += 8));
+ while (isdigit(*s)) s++;
+ }
+ else if (strncmpic(s, US"RCPTDOMAINMAX=", 14) == 0)
+ {
+ sx->peer_limit_rcptdom = atoi(CS (s += 14));
+ while (isdigit(*s)) s++;
+ }
+ else
+ while (*s && !isspace(*s)) s++;
+ }
+}
+
+/* Apply given values to the current connection */
+static void
+ehlo_limits_apply(smtp_context * sx,
+ unsigned limit_mail, unsigned limit_rcpt, unsigned limit_rcptdom)
+{
+if (limit_mail && limit_mail < sx->max_mail) sx->max_mail = limit_mail;
+if (limit_rcpt && limit_rcpt < sx->max_rcpt) sx->max_rcpt = limit_rcpt;
+if (limit_rcptdom)
+ {
+ DEBUG(D_transport) debug_printf("will treat as !multi_domain\n");
+ sx->single_rcpt_domain = TRUE;
+ }
+}
+
+/* Apply values from EHLO-resp to the current connection */
+static void
+ehlo_response_limits_apply(smtp_context * sx)
+{
+ehlo_limits_apply(sx, sx->peer_limit_mail, sx->peer_limit_rcpt,
+ sx->peer_limit_rcptdom);
+}
+
+/* Apply values read from cache to the current connection */
+static void
+ehlo_cache_limits_apply(smtp_context * sx)
+{
+# ifndef DISABLE_PIPE_CONNECT
+ehlo_limits_apply(sx, sx->ehlo_resp.limit_mail, sx->ehlo_resp.limit_rcpt,
+ sx->ehlo_resp.limit_rcptdom);
+# endif
+}
+#endif /*EXPERIMENTAL_ESMTP_LIMITS*/
+
+/******************************************************************************/
+
+#ifndef DISABLE_PIPE_CONNECT
+static uschar *
+ehlo_cache_key(const smtp_context * sx)
+{
+host_item * host = sx->conn_args.host;
+return Ustrchr(host->address, ':')
+ ? string_sprintf("[%s]:%d.EHLO", host->address,
+ host->port == PORT_NONE ? sx->port : host->port)
+ : string_sprintf("%s:%d.EHLO", host->address,
+ host->port == PORT_NONE ? sx->port : host->port);
+}
+
+/* Cache EHLO-response info for use by early-pipe.
+Called
+- During a normal flow on EHLO response (either cleartext or under TLS),
+ when we are willing to do PIPECONNECT and it is offered
+- During an early-pipe flow on receiving the actual EHLO response and noting
+ disparity versus the cached info used, when PIPECONNECT is still being offered
+
+We assume that suitable values have been set in the sx.ehlo_resp structure for
+features and auths; we handle the copy of limits. */
+
+static void
+write_ehlo_cache_entry(smtp_context * sx)
+{
+open_db dbblock, * dbm_file;
+
+# ifdef EXPERIMENTAL_ESMTP_LIMITS
+sx->ehlo_resp.limit_mail = sx->peer_limit_mail;
+sx->ehlo_resp.limit_rcpt = sx->peer_limit_rcpt;
+sx->ehlo_resp.limit_rcptdom = sx->peer_limit_rcptdom;
+# endif
+
+if ((dbm_file = dbfn_open(US"misc", O_RDWR, &dbblock, TRUE, TRUE)))
+ {
+ uschar * ehlo_resp_key = ehlo_cache_key(sx);
+ dbdata_ehlo_resp er = { .data = sx->ehlo_resp };
+
+ HDEBUG(D_transport)
+# ifdef EXPERIMENTAL_ESMTP_LIMITS
+ if (sx->ehlo_resp.limit_mail || sx->ehlo_resp.limit_rcpt || sx->ehlo_resp.limit_rcptdom)
+ debug_printf("writing clr %04x/%04x cry %04x/%04x lim %05d/%05d/%05d\n",
+ sx->ehlo_resp.cleartext_features, sx->ehlo_resp.cleartext_auths,
+ sx->ehlo_resp.crypted_features, sx->ehlo_resp.crypted_auths,
+ sx->ehlo_resp.limit_mail, sx->ehlo_resp.limit_rcpt,
+ sx->ehlo_resp.limit_rcptdom);
+ else
+# endif
+ debug_printf("writing clr %04x/%04x cry %04x/%04x\n",
+ sx->ehlo_resp.cleartext_features, sx->ehlo_resp.cleartext_auths,
+ sx->ehlo_resp.crypted_features, sx->ehlo_resp.crypted_auths);
+
+ dbfn_write(dbm_file, ehlo_resp_key, &er, (int)sizeof(er));
+ dbfn_close(dbm_file);
+ }
+}
+
+static void
+invalidate_ehlo_cache_entry(smtp_context * sx)
+{
+open_db dbblock, * dbm_file;
+
+if ( sx->early_pipe_active
+ && (dbm_file = dbfn_open(US"misc", O_RDWR, &dbblock, TRUE, TRUE)))
+ {
+ uschar * ehlo_resp_key = ehlo_cache_key(sx);
+ dbfn_delete(dbm_file, ehlo_resp_key);
+ dbfn_close(dbm_file);
+ }
+}
+
+static BOOL
+read_ehlo_cache_entry(smtp_context * sx)
+{
+open_db dbblock;
+open_db * dbm_file;
+
+if (!(dbm_file = dbfn_open(US"misc", O_RDONLY, &dbblock, FALSE, TRUE)))
+ { DEBUG(D_transport) debug_printf("ehlo-cache: no misc DB\n"); }
+else
+ {
+ uschar * ehlo_resp_key = ehlo_cache_key(sx);
+ dbdata_ehlo_resp * er;
+
+ if (!(er = dbfn_read_enforce_length(dbm_file, ehlo_resp_key, sizeof(dbdata_ehlo_resp))))
+ { DEBUG(D_transport) debug_printf("no ehlo-resp record\n"); }
+ else if (time(NULL) - er->time_stamp > retry_data_expire)
+ {
+ DEBUG(D_transport) debug_printf("ehlo-resp record too old\n");
+ dbfn_close(dbm_file);
+ if ((dbm_file = dbfn_open(US"misc", O_RDWR, &dbblock, TRUE, TRUE)))
+ dbfn_delete(dbm_file, ehlo_resp_key);
+ }
+ else
+ {
+ DEBUG(D_transport)
+# ifdef EXPERIMENTAL_ESMTP_LIMITS
+ if (er->data.limit_mail || er->data.limit_rcpt || er->data.limit_rcptdom)
+ debug_printf("EHLO response bits from cache:"
+ " cleartext 0x%04x/0x%04x crypted 0x%04x/0x%04x lim %05d/%05d/%05d\n",
+ er->data.cleartext_features, er->data.cleartext_auths,
+ er->data.crypted_features, er->data.crypted_auths,
+ er->data.limit_mail, er->data.limit_rcpt, er->data.limit_rcptdom);
+ else
+# endif
+ debug_printf("EHLO response bits from cache:"
+ " cleartext 0x%04x/0x%04x crypted 0x%04x/0x%04x\n",
+ er->data.cleartext_features, er->data.cleartext_auths,
+ er->data.crypted_features, er->data.crypted_auths);
+
+ sx->ehlo_resp = er->data;
+# ifdef EXPERIMENTAL_ESMTP_LIMITS
+ ehlo_cache_limits_apply(sx);
+# endif
+ dbfn_close(dbm_file);
+ return TRUE;
+ }
+ dbfn_close(dbm_file);
+ }
+return FALSE;
+}
+
+
+
+/* Return an auths bitmap for the set of AUTH methods offered by the server
+which match our authenticators. */
+
+static unsigned short
+study_ehlo_auths(smtp_context * sx)
+{
+uschar * names;
+auth_instance * au;
+uschar authnum;
+unsigned short authbits = 0;
+
+if (!sx->esmtp) return 0;
+if (!regex_AUTH) regex_AUTH = regex_must_compile(AUTHS_REGEX, FALSE, TRUE);
+if (!regex_match_and_setup(regex_AUTH, sx->buffer, 0, -1)) return 0;
+expand_nmax = -1; /* reset */
+names = string_copyn(expand_nstring[1], expand_nlength[1]);
+
+for (au = auths, authnum = 0; au; au = au->next, authnum++) if (au->client)
+ {
+ const uschar * list = names;
+ uschar * s;
+ for (int sep = ' '; s = string_nextinlist(&list, &sep, NULL, 0); )
+ if (strcmpic(au->public_name, s) == 0)
+ { authbits |= BIT(authnum); break; }
+ }
+
+DEBUG(D_transport)
+ debug_printf("server offers %s AUTH, methods '%s', bitmap 0x%04x\n",
+ tls_out.active.sock >= 0 ? "crypted" : "plaintext", names, authbits);
+
+if (tls_out.active.sock >= 0)
+ sx->ehlo_resp.crypted_auths = authbits;
+else
+ sx->ehlo_resp.cleartext_auths = authbits;
+return authbits;
+}
+
+
+
+
+/* Wait for and check responses for early-pipelining.
+
+Called from the lower-level smtp_read_response() function
+used for general code that assume synchronisation, if context
+flags indicate outstanding early-pipelining commands. Also
+called fom sync_responses() which handles pipelined commands.
+
+Arguments:
+ sx smtp connection context
+ countp number of outstanding responses, adjusted on return
+
+Return:
+ OK all well
+ DEFER error on first read of TLS'd conn
+ FAIL SMTP error in response
+*/
+int
+smtp_reap_early_pipe(smtp_context * sx, int * countp)
+{
+BOOL pending_BANNER = sx->pending_BANNER;
+BOOL pending_EHLO = sx->pending_EHLO;
+int rc = FAIL;
+
+sx->pending_BANNER = FALSE; /* clear early to avoid recursion */
+sx->pending_EHLO = FALSE;
+
+if (pending_BANNER)
+ {
+ DEBUG(D_transport) debug_printf("%s expect banner\n", __FUNCTION__);
+ (*countp)--;
+ if (!smtp_reap_banner(sx))
+ {
+ DEBUG(D_transport) debug_printf("bad banner\n");
+ if (tls_out.active.sock >= 0) rc = DEFER;
+ goto fail;
+ }
+ /*XXX EXPERIMENTAL_ESMTP_LIMITS ? */
+ ehlo_response_lbserver(sx, sx->conn_args.ob);
+ }
+
+if (pending_EHLO)
+ {
+ unsigned peer_offered;
+ unsigned short authbits = 0, * ap;
+
+ DEBUG(D_transport) debug_printf("%s expect ehlo\n", __FUNCTION__);
+ (*countp)--;
+ if (!smtp_reap_ehlo(sx))
+ {
+ DEBUG(D_transport) debug_printf("bad response for EHLO\n");
+ if (tls_out.active.sock >= 0) rc = DEFER;
+ goto fail;
+ }
+
+ /* Compare the actual EHLO response extensions and AUTH methods to the cached
+ value we assumed; on difference, dump or rewrite the cache and arrange for a
+ retry. */
+
+ ap = tls_out.active.sock < 0
+ ? &sx->ehlo_resp.cleartext_auths : &sx->ehlo_resp.crypted_auths;
+
+ peer_offered = ehlo_response(sx->buffer,
+ (tls_out.active.sock < 0 ? OPTION_TLS : 0)
+ | OPTION_CHUNKING | OPTION_PRDR | OPTION_DSN | OPTION_PIPE | OPTION_SIZE
+ | OPTION_UTF8 | OPTION_EARLY_PIPE
+ );
+# ifdef EXPERIMENTAL_ESMTP_LIMITS
+ if (tls_out.active.sock >= 0 || !(peer_offered & OPTION_TLS))
+ ehlo_response_limits_read(sx);
+# endif
+ if ( peer_offered != sx->peer_offered
+ || (authbits = study_ehlo_auths(sx)) != *ap)
+ {
+ HDEBUG(D_transport)
+ debug_printf("EHLO %s extensions changed, 0x%04x/0x%04x -> 0x%04x/0x%04x\n",
+ tls_out.active.sock < 0 ? "cleartext" : "crypted",
+ sx->peer_offered, *ap, peer_offered, authbits);
+ if (peer_offered & OPTION_EARLY_PIPE)
+ {
+ *(tls_out.active.sock < 0
+ ? &sx->ehlo_resp.cleartext_features : &sx->ehlo_resp.crypted_features) =
+ peer_offered;
+ *ap = authbits;
+ write_ehlo_cache_entry(sx);
+ }
+ else
+ invalidate_ehlo_cache_entry(sx);
+
+ return OK; /* just carry on */
+ }
+# ifdef EXPERIMENTAL_ESMTP_LIMITS
+ /* If we are handling LIMITS, compare the actual EHLO LIMITS values with the
+ cached values and invalidate cache if different. OK to carry on with
+ connect since values are advisory. */
+ {
+ if ( (tls_out.active.sock >= 0 || !(peer_offered & OPTION_TLS))
+ && ( sx->peer_limit_mail != sx->ehlo_resp.limit_mail
+ || sx->peer_limit_rcpt != sx->ehlo_resp.limit_rcpt
+ || sx->peer_limit_rcptdom != sx->ehlo_resp.limit_rcptdom
+ ) )
+ {
+ HDEBUG(D_transport)
+ {
+ debug_printf("EHLO LIMITS changed:");
+ if (sx->peer_limit_mail != sx->ehlo_resp.limit_mail)
+ debug_printf(" MAILMAX %u -> %u\n", sx->ehlo_resp.limit_mail, sx->peer_limit_mail);
+ else if (sx->peer_limit_rcpt != sx->ehlo_resp.limit_rcpt)
+ debug_printf(" RCPTMAX %u -> %u\n", sx->ehlo_resp.limit_rcpt, sx->peer_limit_rcpt);
+ else
+ debug_printf(" RCPTDOMAINMAX %u -> %u\n", sx->ehlo_resp.limit_rcptdom, sx->peer_limit_rcptdom);
+ }
+ invalidate_ehlo_cache_entry(sx);
+ }
+ }
+# endif
+ }
+return OK;
+
+fail:
+ invalidate_ehlo_cache_entry(sx);
+ (void) smtp_discard_responses(sx, sx->conn_args.ob, *countp);
+ return rc;
+}
+#endif /*!DISABLE_PIPE_CONNECT*/
+
+
+/*************************************************
+* 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:
+ sx smtp connection context
+ count the number of responses to read
+ 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
+
+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
+ -4 banner or EHLO failed (early-pipelining)
+ -5 banner or EHLO failed (early-pipelining, TLS)
+*/
+
+static int
+sync_responses(smtp_context * sx, int count, int pending_DATA)
+{
+address_item * addr = sx->sync_addr;
+smtp_transport_options_block * ob = sx->conn_args.ob;
+int yield = 0;
+
+#ifndef DISABLE_PIPE_CONNECT
+int rc;
+if ((rc = smtp_reap_early_pipe(sx, &count)) != OK)
+ return rc == FAIL ? -4 : -5;
+#endif
+
+/* 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 (sx->pending_MAIL)
+ {
+ DEBUG(D_transport) debug_printf("%s expect mail\n", __FUNCTION__);
+ count--;
+ sx->pending_MAIL = sx->RCPT_452 = FALSE;
+ if (!smtp_read_response(sx, sx->buffer, sizeof(sx->buffer),
+ '2', ob->command_timeout))
+ {
+ DEBUG(D_transport) debug_printf("bad response for MAIL\n");
+ Ustrcpy(big_buffer, mail_command); /* Fits, because it came from there! */
+ if (errno == ERRNO_TLSFAILURE)
+ return -5;
+ if (errno == 0 && sx->buffer[0] != 0)
+ {
+ int save_errno = 0;
+ if (sx->buffer[0] == '4')
+ {
+ save_errno = ERRNO_MAIL4XX;
+ addr->more_errno |= ((sx->buffer[1] - '0')*10 + sx->buffer[2] - '0') << 8;
+ }
+ count = smtp_discard_responses(sx, ob, count);
+ errno = save_errno;
+ }
+
+ if (pending_DATA) count--; /* Number of RCPT responses to come */
+ while (count-- > 0) /* Mark any pending addrs with the host used */
+ {
+ while (addr->transport_return != PENDING_DEFER) addr = addr->next;
+ addr->host_used = sx->conn_args.host;
+ addr = addr->next;
+ }
+ 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)
+ if (!(addr = addr->next))
+ return -2;
+
+ /* The address was accepted */
+ addr->host_used = sx->conn_args.host;
+
+ DEBUG(D_transport) debug_printf("%s expect rcpt for %s\n", __FUNCTION__, addr->address);
+ if (smtp_read_response(sx, sx->buffer, sizeof(sx->buffer),
+ '2', ob->command_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);
+ }
+ }
+
+ /* Error on first TLS read */
+
+ else if (errno == ERRNO_TLSFAILURE)
+ return -5;
+
+ /* Timeout while reading the response */
+
+ else if (errno == ETIMEDOUT)
+ {
+ uschar *message = string_sprintf("SMTP timeout after RCPT TO:<%s>",
+ transport_rcpt_address(addr, sx->conn_args.tblock->rcpt_include_affixes));
+ set_errno_nohost(sx->first_addr, ETIMEDOUT, message, DEFER, FALSE, &sx->delivery_start);
+ 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 || sx->buffer[0] == 0)
+ {
+ gstring gs = { .size = big_buffer_size, .ptr = 0, .s = big_buffer }, * g = &gs;
+
+ /* Use taint-unchecked routines for writing into big_buffer, trusting
+ that we'll never expand it. */
+
+ g = string_fmt_append_f(g, SVFMT_TAINT_NOCHK, "RCPT TO:<%s>",
+ transport_rcpt_address(addr, sx->conn_args.tblock->rcpt_include_affixes));
+ string_from_gstring(g);
+ return -2;
+ }
+
+ /* Handle SMTP permanent and temporary response codes. */
+
+ else
+ {
+ addr->message =
+ string_sprintf("SMTP error from remote mail server after RCPT TO:<%s>: "
+ "%s", transport_rcpt_address(addr, sx->conn_args.tblock->rcpt_include_affixes),
+ string_printing(sx->buffer));
+ setflag(addr, af_pass_message);
+ if (!sx->verify)
+ msglog_line(sx->conn_args.host, addr->message);
+
+ /* The response was 5xx */
+
+ if (sx->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 |= ((sx->buffer[1] - '0')*10 + sx->buffer[2] - '0') << 8;
+
+ if (!sx->verify)
+ {
+#ifndef DISABLE_EVENT
+ event_defer_errno = addr->more_errno;
+ msg_event_raise(US"msg:rcpt:host:defer", addr);
+#endif
+ /* If a 452 and we've had at least one 2xx or 5xx, set next_addr to the
+ start point for another MAIL command. */
+
+ if (addr->more_errno >> 8 == 52 && yield & 3)
+ {
+ if (!sx->RCPT_452) /* initialised at MAIL-ack above */
+ {
+ DEBUG(D_transport)
+ debug_printf("%s: seen first 452 too-many-rcpts\n", __FUNCTION__);
+ sx->RCPT_452 = TRUE;
+ sx->next_addr = addr;
+ }
+ addr->transport_return = PENDING_DEFER;
+ addr->basic_errno = 0;
+ }
+ else
+ {
+ /* Log temporary errors if there are more hosts to be tried.
+ If not, log this last one in the == line. */
+
+ if (sx->conn_args.host->next)
+ if (LOGGING(outgoing_port))
+ log_write(0, LOG_MAIN, "H=%s [%s]:%d %s", sx->conn_args.host->name,
+ sx->conn_args.host->address,
+ sx->port == PORT_NONE ? 25 : sx->port, addr->message);
+ else
+ log_write(0, LOG_MAIN, "H=%s [%s]: %s", sx->conn_args.host->name,
+ sx->conn_args.host->address, addr->message);
+
+#ifndef DISABLE_EVENT
+ else
+ msg_event_raise(US"msg:rcpt:defer", addr);
+#endif
+
+ /* 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. */
+
+ retry_add_item(addr,
+ ob->address_retry_include_sender
+ ? string_sprintf("%s:<%s>", addr->address_retry_key, sender_address)
+ : addr->address_retry_key,
+ 0);
+ }
+ }
+ }
+ }
+ if (count && !(addr = addr->next))
+ return -2;
+ } /* 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) sx->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)
+ {
+ DEBUG(D_transport) debug_printf("%s expect data\n", __FUNCTION__);
+ if (!smtp_read_response(sx, sx->buffer, sizeof(sx->buffer),
+ '3', ob->command_timeout))
+ {
+ int code;
+ uschar *msg;
+ BOOL pass_message;
+
+ if (errno == ERRNO_TLSFAILURE) /* Error on first TLS read */
+ return -5;
+
+ if (pending_DATA > 0 || (yield & 1) != 0)
+ {
+ if (errno == 0 && sx->buffer[0] == '4')
+ {
+ errno = ERRNO_DATA4XX;
+ sx->first_addr->more_errno |= ((sx->buffer[1] - '0')*10 + sx->buffer[2] - '0') << 8;
+ }
+ return -3;
+ }
+ (void)check_response(sx->conn_args.host, &errno, 0, sx->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;
+}
+
+
+
+
+
+/* Try an authenticator's client entry */
+
+static int
+try_authenticator(smtp_context * sx, auth_instance * au)
+{
+smtp_transport_options_block * ob = sx->conn_args.ob; /* transport options */
+host_item * host = sx->conn_args.host; /* host to deliver to */
+int rc;
+
+/* Set up globals for error messages */
+
+authenticator_name = au->name;
+driver_srcfile = au->srcfile;
+driver_srcline = au->srcline;
+
+sx->outblock.authenticating = TRUE;
+rc = (au->info->clientcode)(au, sx, ob->command_timeout,
+ sx->buffer, sizeof(sx->buffer));
+sx->outblock.authenticating = FALSE;
+driver_srcfile = authenticator_name = NULL; driver_srcline = 0;
+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:
+ f.smtp_authenticated = TRUE; /* stops the outer loop */
+ client_authenticator = au->name;
+ if (au->set_client_id)
+ 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 || sx->buffer[0] != '5') return FAIL;
+ log_write(0, LOG_MAIN, "%s authenticator failed H=%s [%s] %s",
+ au->name, host->name, host->address, sx->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 (*sx->buffer != 0)
+ log_write(0, LOG_MAIN, "%s authenticator cancelled "
+ "authentication H=%s [%s] %s", au->name, host->name,
+ host->address, sx->buffer);
+ break;
+
+ /* Internal problem, message in buffer. */
+
+ case ERROR:
+ set_errno_nohost(sx->addrlist, ERRNO_AUTHPROB, string_copy(sx->buffer),
+ DEFER, FALSE, &sx->delivery_start);
+ return ERROR;
+ }
+return OK;
+}
+
+
+
+
+/* Do the client side of smtp-level authentication.
+
+Arguments:
+ sx smtp connection context
+
+sx->buffer should have the EHLO response from server (gets overwritten)
+
+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
+*/
+
+static int
+smtp_auth(smtp_context * sx)
+{
+host_item * host = sx->conn_args.host; /* host to deliver to */
+smtp_transport_options_block * ob = sx->conn_args.ob; /* transport options */
+int require_auth = verify_check_given_host(CUSS &ob->hosts_require_auth, host);
+#ifndef DISABLE_PIPE_CONNECT
+unsigned short authbits = tls_out.active.sock >= 0
+ ? sx->ehlo_resp.crypted_auths : sx->ehlo_resp.cleartext_auths;
+#endif
+uschar * fail_reason = US"server did not advertise AUTH support";
+
+f.smtp_authenticated = FALSE;
+client_authenticator = client_authenticated_id = client_authenticated_sender = NULL;
+
+if (!regex_AUTH)
+ regex_AUTH = regex_must_compile(AUTHS_REGEX, FALSE, TRUE);
+
+/* Is the server offering AUTH? */
+
+if ( sx->esmtp
+ &&
+#ifndef DISABLE_PIPE_CONNECT
+ sx->early_pipe_active ? authbits
+ :
+#endif
+ regex_match_and_setup(regex_AUTH, sx->buffer, 0, -1)
+ )
+ {
+ uschar * names = NULL;
+ expand_nmax = -1; /* reset */
+
+#ifndef DISABLE_PIPE_CONNECT
+ if (!sx->early_pipe_active)
+#endif
+ names = string_copyn(expand_nstring[1], expand_nlength[1]);
+
+ /* Must not do this check until after we have saved the result of the
+ regex match above as the check could be another RE. */
+
+ if ( require_auth == OK
+ || verify_check_given_host(CUSS &ob->hosts_try_auth, host) == OK)
+ {
+ DEBUG(D_transport) debug_printf("scanning authentication mechanisms\n");
+ fail_reason = US"no common mechanisms were found";
+
+#ifndef DISABLE_PIPE_CONNECT
+ if (sx->early_pipe_active)
+ {
+ /* Scan our authenticators (which support use by a client and were offered
+ by the server (checked at cache-write time)), not suppressed by
+ client_condition. If one is found, attempt to authenticate by calling its
+ client function. We are limited to supporting up to 16 authenticator
+ public-names by the number of bits in a short. */
+
+ auth_instance * au;
+ uschar bitnum;
+ int rc;
+
+ for (bitnum = 0, au = auths;
+ !f.smtp_authenticated && au && bitnum < 16;
+ bitnum++, au = au->next) if (authbits & BIT(bitnum))
+ {
+ if ( au->client_condition
+ && !expand_check_condition(au->client_condition, au->name,
+ US"client authenticator"))
+ {
+ DEBUG(D_transport) debug_printf("skipping %s authenticator: %s\n",
+ au->name, "client_condition is false");
+ 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";
+
+ if ((rc = try_authenticator(sx, au)) != OK)
+ return rc;
+ }
+ }
+ else
+#endif
+
+ /* 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 (auth_instance * au = auths; !f.smtp_authenticated && au; au = au->next)
+ {
+ uschar *p = names;
+
+ if ( !au->client
+ || ( au->client_condition
+ && !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)
+ {
+ int len = Ustrlen(au->public_name);
+ int rc;
+
+ 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";
+
+ if ((rc = try_authenticator(sx, au)) != OK)
+ return rc;
+
+ 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 && !f.smtp_authenticated)
+ {
+#ifndef DISABLE_PIPE_CONNECT
+ invalidate_ehlo_cache_entry(sx);
+#endif
+ set_errno_nohost(sx->addrlist, ERRNO_AUTHFAIL,
+ string_sprintf("authentication required but %s", fail_reason), DEFER,
+ FALSE, &sx->delivery_start);
+ return DEFER;
+ }
+
+return OK;
+}
+
+
+/* Construct AUTH appendix string for MAIL TO */
+/*
+Arguments
+ sx context for smtp connection
+ p point in sx->buffer to build string
+ addrlist chain of potential addresses to deliver
+
+Globals f.smtp_authenticated
+ client_authenticated_sender
+Return True on error, otherwise buffer has (possibly empty) terminated string
+*/
+
+static BOOL
+smtp_mail_auth_str(smtp_context * sx, uschar * p, address_item * addrlist)
+{
+smtp_transport_options_block * ob = sx->conn_args.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, f.smtp_authenticated?"Y":"N");
+#endif
+
+if (ob->authenticated_sender)
+ {
+ uschar * new = expand_string(ob->authenticated_sender);
+ if (!new)
+ {
+ if (!f.expand_string_forcedfail)
+ {
+ uschar *message = string_sprintf("failed to expand "
+ "authenticated_sender: %s", expand_string_message);
+ set_errno_nohost(addrlist, ERRNO_EXPANDFAIL, message, DEFER, FALSE, &sx->delivery_start);
+ return TRUE;
+ }
+ }
+ else if (*new)
+ local_authenticated_sender = new;
+ }
+
+/* Add the authenticated sender address if present */
+
+if ( (f.smtp_authenticated || ob->authenticated_sender_force)
+ && local_authenticated_sender)
+ {
+ string_format_nt(p, sizeof(sx->buffer) - (p-sx->buffer), " AUTH=%s",
+ auth_xtextencode(local_authenticated_sender,
+ Ustrlen(local_authenticated_sender)));
+ client_authenticated_sender = string_copy(local_authenticated_sender);
+ }
+else
+ *p = 0;
+
+return FALSE;
+}
+
+
+
+typedef struct smtp_compare_s
+{
+ uschar * current_sender_address;
+ struct transport_instance * tblock;
+} smtp_compare_t;
+
+
+/* Create a unique string that identifies this message, it is based on
+sender_address, helo_data and tls_certificate if enabled.
+*/
+
+static uschar *
+smtp_local_identity(uschar * sender, struct transport_instance * tblock)
+{
+address_item * addr1;
+uschar * if1 = US"";
+uschar * helo1 = US"";
+#ifndef DISABLE_TLS
+uschar * tlsc1 = US"";
+#endif
+uschar * save_sender_address = sender_address;
+uschar * local_identity = NULL;
+smtp_transport_options_block * ob = SOB tblock->options_block;
+
+sender_address = sender;
+
+addr1 = deliver_make_addr (sender, TRUE);
+deliver_set_expansions(addr1);
+
+if (ob->interface)
+ if1 = expand_string(ob->interface);
+
+if (ob->helo_data)
+ helo1 = expand_string(ob->helo_data);
+
+#ifndef DISABLE_TLS
+if (ob->tls_certificate)
+ tlsc1 = expand_string(ob->tls_certificate);
+local_identity = string_sprintf ("%s^%s^%s", if1, helo1, tlsc1);
+#else
+local_identity = string_sprintf ("%s^%s", if1, helo1);
+#endif
+
+deliver_set_expansions(NULL);
+sender_address = save_sender_address;
+
+return local_identity;
+}
+
+
+
+/* This routine is a callback that is called from transport_check_waiting.
+This function will evaluate the incoming message versus the previous
+message. If the incoming message is using a different local identity then
+we will veto this new message. */
+
+static BOOL
+smtp_are_same_identities(uschar * message_id, smtp_compare_t * s_compare)
+{
+uschar * message_local_identity,
+ * current_local_identity,
+ * new_sender_address;
+
+current_local_identity =
+ smtp_local_identity(s_compare->current_sender_address, s_compare->tblock);
+
+if (!(new_sender_address = spool_sender_from_msgid(message_id)))
+ return FALSE;
+
+
+message_local_identity =
+ smtp_local_identity(new_sender_address, s_compare->tblock);
+
+return Ustrcmp(current_local_identity, message_local_identity) == 0;
+}
+
+
+
+static unsigned
+ehlo_response(uschar * buf, unsigned checks)
+{
+PCRE2_SIZE bsize = Ustrlen(buf);
+pcre2_match_data * md = pcre2_match_data_create(1, pcre_gen_ctx);
+
+/* debug_printf("%s: check for 0x%04x\n", __FUNCTION__, checks); */
+
+#ifndef DISABLE_TLS
+if ( checks & OPTION_TLS
+ && pcre2_match(regex_STARTTLS,
+ (PCRE2_SPTR)buf, bsize, 0, PCRE_EOPT, md, pcre_mtc_ctx) < 0)
+#endif
+ checks &= ~OPTION_TLS;
+
+if ( checks & OPTION_IGNQ
+ && pcre2_match(regex_IGNOREQUOTA,
+ (PCRE2_SPTR)buf, bsize, 0, PCRE_EOPT, md, pcre_mtc_ctx) < 0)
+ checks &= ~OPTION_IGNQ;
+
+if ( checks & OPTION_CHUNKING
+ && pcre2_match(regex_CHUNKING,
+ (PCRE2_SPTR)buf, bsize, 0, PCRE_EOPT, md, pcre_mtc_ctx) < 0)
+ checks &= ~OPTION_CHUNKING;
+
+#ifndef DISABLE_PRDR
+if ( checks & OPTION_PRDR
+ && pcre2_match(regex_PRDR,
+ (PCRE2_SPTR)buf, bsize, 0, PCRE_EOPT, md, pcre_mtc_ctx) < 0)
+#endif
+ checks &= ~OPTION_PRDR;
+
+#ifdef SUPPORT_I18N
+if ( checks & OPTION_UTF8
+ && pcre2_match(regex_UTF8,
+ (PCRE2_SPTR)buf, bsize, 0, PCRE_EOPT, md, pcre_mtc_ctx) < 0)
+#endif
+ checks &= ~OPTION_UTF8;
+
+if ( checks & OPTION_DSN
+ && pcre2_match(regex_DSN,
+ (PCRE2_SPTR)buf, bsize, 0, PCRE_EOPT, md, pcre_mtc_ctx) < 0)
+ checks &= ~OPTION_DSN;
+
+if ( checks & OPTION_PIPE
+ && pcre2_match(regex_PIPELINING,
+ (PCRE2_SPTR)buf, bsize, 0, PCRE_EOPT, md, pcre_mtc_ctx) < 0)
+ checks &= ~OPTION_PIPE;
+
+if ( checks & OPTION_SIZE
+ && pcre2_match(regex_SIZE,
+ (PCRE2_SPTR)buf, bsize, 0, PCRE_EOPT, md, pcre_mtc_ctx) < 0)
+ checks &= ~OPTION_SIZE;
+
+#ifndef DISABLE_PIPE_CONNECT
+if ( checks & OPTION_EARLY_PIPE
+ && pcre2_match(regex_EARLY_PIPE,
+ (PCRE2_SPTR)buf, bsize, 0, PCRE_EOPT, md, pcre_mtc_ctx) < 0)
+#endif
+ checks &= ~OPTION_EARLY_PIPE;
+
+pcre2_match_data_free(md);
+/* debug_printf("%s: found 0x%04x\n", __FUNCTION__, checks); */
+return checks;
+}
+
+
+
+/* Callback for emitting a BDAT data chunk header.
+
+If given a nonzero size, first flush any buffered SMTP commands
+then emit the command.
+
+Reap previous SMTP command responses if requested, and always reap
+the response from a previous BDAT command.
+
+Args:
+ tctx transport context
+ chunk_size value for SMTP BDAT command
+ flags
+ tc_chunk_last add LAST option to SMTP BDAT command
+ tc_reap_prev reap response to previous SMTP commands
+
+Returns:
+ OK or ERROR
+ DEFER TLS error on first read (EHLO-resp); errno set
+*/
+
+static int
+smtp_chunk_cmd_callback(transport_ctx * tctx, unsigned chunk_size,
+ unsigned flags)
+{
+smtp_transport_options_block * ob = SOB tctx->tblock->options_block;
+smtp_context * sx = tctx->smtp_context;
+int cmd_count = 0;
+int prev_cmd_count;
+
+/* Write SMTP chunk header command. If not reaping responses, note that
+there may be more writes (like, the chunk data) done soon. */
+
+if (chunk_size > 0)
+ {
+#ifndef DISABLE_PIPE_CONNECT
+ BOOL new_conn = !!(sx->outblock.conn_args);
+#endif
+ if((cmd_count = smtp_write_command(sx,
+ flags & tc_reap_prev ? SCMD_FLUSH : SCMD_MORE,
+ "BDAT %u%s\r\n", chunk_size, flags & tc_chunk_last ? " LAST" : "")
+ ) < 0) return ERROR;
+ if (flags & tc_chunk_last)
+ data_command = string_copy(big_buffer); /* Save for later error message */
+#ifndef DISABLE_PIPE_CONNECT
+ /* That command write could have been the one that made the connection.
+ Copy the fd from the client conn ctx (smtp transport specific) to the
+ generic transport ctx. */
+
+ if (new_conn)
+ tctx->u.fd = sx->outblock.cctx->sock;
+#endif
+ }
+
+prev_cmd_count = cmd_count += sx->cmd_count;
+
+/* Reap responses for any previous, but not one we just emitted */
+
+if (chunk_size > 0)
+ prev_cmd_count--;
+if (sx->pending_BDAT)
+ prev_cmd_count--;
+
+if (flags & tc_reap_prev && prev_cmd_count > 0)
+ {
+ DEBUG(D_transport) debug_printf("look for %d responses"
+ " for previous pipelined cmds\n", prev_cmd_count);
+
+ switch(sync_responses(sx, prev_cmd_count, 0))
+ {
+ case 1: /* 2xx (only) => OK */
+ case 3: sx->good_RCPT = TRUE; /* 2xx & 5xx => OK & progress made */
+ case 2: sx->completed_addr = TRUE; /* 5xx (only) => progress made */
+ case 0: break; /* No 2xx or 5xx, but no probs */
+
+ case -5: errno = ERRNO_TLSFAILURE;
+ return DEFER;
+#ifndef DISABLE_PIPE_CONNECT
+ case -4: /* non-2xx for pipelined banner or EHLO */
+#endif
+ case -1: /* Timeout on RCPT */
+ default: return ERROR; /* I/O error, or any MAIL/DATA error */
+ }
+ cmd_count = 1;
+ if (!sx->pending_BDAT)
+ pipelining_active = FALSE;
+ }
+
+/* Reap response for an outstanding BDAT */
+
+if (sx->pending_BDAT)
+ {
+ DEBUG(D_transport) debug_printf("look for one response for BDAT\n");
+
+ if (!smtp_read_response(sx, sx->buffer, sizeof(sx->buffer), '2',
+ ob->command_timeout))
+ {
+ if (errno == 0 && sx->buffer[0] == '4')
+ {
+ errno = ERRNO_DATA4XX; /*XXX does this actually get used? */
+ sx->addrlist->more_errno |=
+ ((sx->buffer[1] - '0')*10 + sx->buffer[2] - '0') << 8;
+ }
+ return ERROR;
+ }
+ cmd_count--;
+ sx->pending_BDAT = FALSE;
+ pipelining_active = FALSE;
+ }
+else if (chunk_size > 0)
+ sx->pending_BDAT = TRUE;
+
+
+sx->cmd_count = cmd_count;
+return OK;
+}
+
+
+
+#ifdef SUPPORT_DANE
+static int
+check_force_dane_conn(smtp_context * sx, smtp_transport_options_block * ob)
+{
+int rc;
+if( sx->dane_required
+ || verify_check_given_host(CUSS &ob->hosts_try_dane, sx->conn_args.host) == OK
+ )
+ switch (rc = tlsa_lookup(sx->conn_args.host, &sx->conn_args.tlsa_dnsa, sx->dane_required))
+ {
+ case OK: sx->conn_args.dane = TRUE;
+ ob->tls_tempfail_tryclear = FALSE; /* force TLS */
+ ob->tls_sni = sx->conn_args.host->name; /* force SNI */
+ break;
+ case FAIL_FORCED: break;
+ default: set_errno_nohost(sx->addrlist, ERRNO_DNSDEFER,
+ string_sprintf("DANE error: tlsa lookup %s",
+ rc_to_string(rc)),
+ rc, FALSE, &sx->delivery_start);
+# ifndef DISABLE_EVENT
+ (void) event_raise(sx->conn_args.tblock->event_action,
+ US"dane:fail", sx->dane_required
+ ? US"dane-required" : US"dnssec-invalid",
+ NULL);
+# endif
+ return rc;
+ }
+return OK;
+}
+#endif
+
+
+/*************************************************
+* Make connection for given message *
+*************************************************/
+
+/*
+Arguments:
+ sx connection context
+ 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;
+ fd is set in the conn context, tls_out set up.
+ 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 - helo_data or add_headers or authenticated_sender is
+ specified for this transport, and the string failed
+ to expand
+*/
+int
+smtp_setup_conn(smtp_context * sx, BOOL suppress_tls)
+{
+smtp_transport_options_block * ob = sx->conn_args.tblock->options_block;
+BOOL pass_message = FALSE;
+uschar * message = NULL;
+int yield = OK;
+#ifndef DISABLE_TLS
+uschar * tls_errstr;
+#endif
+
+/* Many lines of clearing individual elements of *sx that used to
+be here have been replaced by a full memset to zero (de41aff051).
+There are two callers, this file and verify.c . Now we only set
+up nonzero elements. */
+
+sx->conn_args.ob = ob;
+
+sx->lmtp = strcmpic(ob->protocol, US"lmtp") == 0;
+sx->smtps = strcmpic(ob->protocol, US"smtps") == 0;
+sx->send_rset = TRUE;
+sx->send_quit = TRUE;
+sx->setting_up = TRUE;
+sx->esmtp = TRUE;
+sx->dsn_all_lasthop = TRUE;
+#ifdef SUPPORT_DANE
+sx->dane_required =
+ verify_check_given_host(CUSS &ob->hosts_require_dane, sx->conn_args.host) == OK;
+#endif
+
+if ((sx->max_mail = sx->conn_args.tblock->connection_max_messages) == 0) sx->max_mail = 999999;
+if ((sx->max_rcpt = sx->conn_args.tblock->max_addresses) == 0) sx->max_rcpt = 999999;
+sx->igquotstr = US"";
+if (!sx->helo_data) sx->helo_data = ob->helo_data;
+
+smtp_command = US"initial connection";
+
+/* Set up the buffer for reading SMTP response packets. */
+
+sx->inblock.buffer = sx->inbuffer;
+sx->inblock.buffersize = sizeof(sx->inbuffer);
+sx->inblock.ptr = sx->inbuffer;
+sx->inblock.ptrend = sx->inbuffer;
+
+/* Set up the buffer for holding SMTP commands while pipelining */
+
+sx->outblock.buffer = sx->outbuffer;
+sx->outblock.buffersize = sizeof(sx->outbuffer);
+sx->outblock.ptr = sx->outbuffer;
+
+/* 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;
+#ifdef USE_OPENSSL
+tls_out.sni = NULL;
+#endif
+tls_out.ocsp = OCSP_NOT_REQ;
+#ifndef DISABLE_TLS_RESUME
+tls_out.resumption = 0;
+#endif
+tls_out.ver = NULL;
+
+/* 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 (when being used by a transport) as we're in a subprocess.
+For verify, unflipped once the callout is dealt with */
+
+tls_modify_variables(&tls_out);
+
+#ifdef DISABLE_TLS
+if (sx->smtps)
+ {
+ set_errno_nohost(sx->addrlist, ERRNO_TLSFAILURE, US"TLS support not available",
+ DEFER, FALSE, &sx->delivery_start);
+ return ERROR;
+ }
+#else
+
+/* If we have a proxied TLS connection, check usability for this message */
+
+if (continue_hostname && continue_proxy_cipher)
+ {
+ int rc;
+ const uschar * sni = US"";
+
+# ifdef SUPPORT_DANE
+ /* Check if the message will be DANE-verified; if so force TLS and its SNI */
+
+ tls_out.dane_verified = FALSE;
+ smtp_port_for_connect(sx->conn_args.host, sx->port);
+ if ( sx->conn_args.host->dnssec == DS_YES
+ && (rc = check_force_dane_conn(sx, ob)) != OK
+ )
+ return rc;
+# endif
+
+ /* If the SNI or the DANE status required for the new message differs from the
+ existing conn drop the connection to force a new one. */
+
+ if (ob->tls_sni && !(sni = expand_cstring(ob->tls_sni)))
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "<%s>: failed to expand transport's tls_sni value: %s",
+ sx->addrlist->address, expand_string_message);
+
+# ifdef SUPPORT_DANE
+ if ( (continue_proxy_sni ? (Ustrcmp(continue_proxy_sni, sni) == 0) : !*sni)
+ && continue_proxy_dane == sx->conn_args.dane)
+ {
+ tls_out.sni = US sni;
+ if ((tls_out.dane_verified = continue_proxy_dane))
+ sx->conn_args.host->dnssec = DS_YES;
+ }
+# else
+ if ((continue_proxy_sni ? (Ustrcmp(continue_proxy_sni, sni) == 0) : !*sni))
+ tls_out.sni = US sni;
+# endif
+ else
+ {
+ DEBUG(D_transport)
+ debug_printf("Closing proxied-TLS connection due to SNI mismatch\n");
+
+ smtp_debug_cmd(US"QUIT", 0);
+ write(0, "QUIT\r\n", 6);
+ close(0);
+ continue_hostname = continue_proxy_cipher = NULL;
+ f.continue_more = FALSE;
+ continue_sequence = 1; /* Unfortunately, this process cannot affect success log
+ which is done by delivery proc. Would have to pass this
+ back through reporting pipe. */
+ }
+ }
+#endif /*!DISABLE_TLS*/
+
+/* 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)
+ {
+ if (sx->verify)
+ HDEBUG(D_verify) debug_printf("interface=%s port=%d\n", sx->conn_args.interface, sx->port);
+
+ /* Arrange to report to calling process this is a new connection */
+
+ clearflag(sx->first_addr, af_cont_conn);
+ setflag(sx->first_addr, af_new_conn);
+
+ /* Get the actual port the connection will use, into sx->conn_args.host */
+
+ smtp_port_for_connect(sx->conn_args.host, sx->port);
+
+#ifdef SUPPORT_DANE
+ /* Do TLSA lookup for DANE */
+ {
+ tls_out.dane_verified = FALSE;
+ tls_out.tlsa_usage = 0;
+
+ if (sx->conn_args.host->dnssec == DS_YES)
+ {
+ int rc;
+ if ((rc = check_force_dane_conn(sx, ob)) != OK)
+ return rc;
+ }
+ else if (sx->dane_required)
+ {
+ set_errno_nohost(sx->addrlist, ERRNO_DNSDEFER,
+ string_sprintf("DANE error: %s lookup not DNSSEC", sx->conn_args.host->name),
+ FAIL, FALSE, &sx->delivery_start);
+# ifndef DISABLE_EVENT
+ (void) event_raise(sx->conn_args.tblock->event_action,
+ US"dane:fail", US"dane-required", NULL);
+# endif
+ return FAIL;
+ }
+ }
+#endif /*DANE*/
+
+ /* Make the TCP connection */
+
+ sx->cctx.tls_ctx = NULL;
+ sx->inblock.cctx = sx->outblock.cctx = &sx->cctx;
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ sx->peer_limit_mail = sx->peer_limit_rcpt = sx->peer_limit_rcptdom =
+#endif
+ sx->avoid_option = sx->peer_offered = smtp_peer_options = 0;
+#ifndef DISABLE_CLIENT_CMD_LOG
+ client_cmd_log = NULL;
+#endif
+
+#ifndef DISABLE_PIPE_CONNECT
+ if ( verify_check_given_host(CUSS &ob->hosts_pipe_connect,
+ sx->conn_args.host) == OK)
+
+ /* We don't find out the local ip address until the connect, so if
+ the helo string might use it avoid doing early-pipelining. */
+
+ if ( !sx->helo_data
+ || sx->conn_args.interface
+ || !Ustrstr(sx->helo_data, "$sending_ip_address")
+ || Ustrstr(sx->helo_data, "def:sending_ip_address")
+ )
+ {
+ sx->early_pipe_ok = TRUE;
+ if ( read_ehlo_cache_entry(sx)
+ && sx->ehlo_resp.cleartext_features & OPTION_EARLY_PIPE)
+ {
+ DEBUG(D_transport)
+ debug_printf("Using cached cleartext PIPECONNECT\n");
+ sx->early_pipe_active = TRUE;
+ sx->peer_offered = sx->ehlo_resp.cleartext_features;
+ }
+ }
+ else DEBUG(D_transport)
+ debug_printf("helo needs $sending_ip_address; avoid early-pipelining\n");
+
+PIPE_CONNECT_RETRY:
+ if (sx->early_pipe_active)
+ {
+ sx->outblock.conn_args = &sx->conn_args;
+ (void) smtp_boundsock(&sx->conn_args);
+ }
+ else
+#endif
+ {
+ blob lazy_conn = {.data = NULL};
+ /* For TLS-connect, a TFO lazy-connect is useful since the Client Hello
+ can go on the TCP SYN. */
+
+ if ((sx->cctx.sock = smtp_connect(&sx->conn_args,
+ sx->smtps ? &lazy_conn : NULL)) < 0)
+ {
+ set_errno_nohost(sx->addrlist,
+ errno == ETIMEDOUT ? ERRNO_CONNECTTIMEOUT : errno,
+ sx->verify ? US strerror(errno) : NULL,
+ DEFER, FALSE, &sx->delivery_start);
+ sx->send_quit = FALSE;
+ return DEFER;
+ }
+#ifdef TCP_QUICKACK
+ (void) setsockopt(sx->cctx.sock, IPPROTO_TCP, TCP_QUICKACK, US &off,
+ sizeof(off));
+#endif
+ }
+ /* 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_ip_address and $sending_port are set.
+ Those will be known even for a TFO lazy-connect, having been set by the bind().
+ For early-pipe, we are ok if binding to a local interface; otherwise (if
+ $sending_ip_address is seen in helo_data) we disabled early-pipe above. */
+
+ if (sx->helo_data)
+ if (!(sx->helo_data = expand_string(sx->helo_data)))
+ if (sx->verify)
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "<%s>: failed to expand transport's helo_data value for callout: %s",
+ sx->addrlist->address, expand_string_message);
+
+#ifdef SUPPORT_I18N
+ if (sx->helo_data)
+ {
+ expand_string_message = NULL;
+ if ((sx->helo_data = string_domain_utf8_to_alabel(sx->helo_data,
+ &expand_string_message)),
+ expand_string_message)
+ if (sx->verify)
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "<%s>: failed to expand transport's helo_data value for callout: %s",
+ sx->addrlist->address, expand_string_message);
+ else
+ sx->helo_data = NULL;
+ }
+#endif
+
+ /* 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 (!sx->smtps)
+ {
+#ifndef DISABLE_PIPE_CONNECT
+ if (sx->early_pipe_active)
+ {
+ sx->pending_BANNER = TRUE; /* sync_responses() must eventually handle */
+ sx->outblock.cmd_count = 1;
+ }
+ else
+#endif
+ {
+ if (!smtp_reap_banner(sx))
+ goto RESPONSE_FAILED;
+ }
+
+#ifndef DISABLE_EVENT
+ {
+ uschar * s;
+ lookup_dnssec_authenticated = sx->conn_args.host->dnssec==DS_YES ? US"yes"
+ : sx->conn_args.host->dnssec==DS_NO ? US"no" : NULL;
+ s = event_raise(sx->conn_args.tblock->event_action, US"smtp:connect", sx->buffer, NULL);
+ if (s)
+ {
+ set_errno_nohost(sx->addrlist, ERRNO_EXPANDFAIL,
+ string_sprintf("deferred by smtp:connect event expansion: %s", s),
+ DEFER, FALSE, &sx->delivery_start);
+ yield = DEFER;
+ goto SEND_QUIT;
+ }
+ }
+#endif
+
+ /* Now check if the helo_data expansion went well, and sign off cleanly if
+ it didn't. */
+
+ if (!sx->helo_data)
+ {
+ message = string_sprintf("failed to expand helo_data: %s",
+ expand_string_message);
+ set_errno_nohost(sx->addrlist, ERRNO_EXPANDFAIL, message, DEFER, FALSE, &sx->delivery_start);
+ yield = DEFER;
+ goto SEND_QUIT;
+ }
+ }
+
+/** Debugging without sending a message
+sx->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. */
+
+ sx->esmtp = verify_check_given_host(CUSS &ob->hosts_avoid_esmtp, sx->conn_args.host) != 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. */
+#ifndef DISABLE_TLS
+ if (sx->smtps)
+ {
+ smtp_peer_options |= OPTION_TLS;
+ suppress_tls = FALSE;
+ ob->tls_tempfail_tryclear = FALSE;
+ smtp_command = US"SSL-on-connect";
+ goto TLS_NEGOTIATE;
+ }
+#endif
+
+ if (sx->esmtp)
+ {
+ if (smtp_write_command(sx,
+#ifndef DISABLE_PIPE_CONNECT
+ sx->early_pipe_active ? SCMD_BUFFER :
+#endif
+ SCMD_FLUSH,
+ "%s %s\r\n", sx->lmtp ? "LHLO" : "EHLO", sx->helo_data) < 0)
+ goto SEND_FAILED;
+ sx->esmtp_sent = TRUE;
+
+#ifndef DISABLE_PIPE_CONNECT
+ if (sx->early_pipe_active)
+ {
+ sx->pending_EHLO = TRUE;
+
+ /* If we have too many authenticators to handle and might need to AUTH
+ for this transport, pipeline no further as we will need the
+ list of auth methods offered. Reap the banner and EHLO. */
+
+ if ( (ob->hosts_require_auth || ob->hosts_try_auth)
+ && f.smtp_in_early_pipe_no_auth)
+ {
+ DEBUG(D_transport) debug_printf("may need to auth, so pipeline no further\n");
+ if (smtp_write_command(sx, SCMD_FLUSH, NULL) < 0)
+ goto SEND_FAILED;
+ if (sync_responses(sx, 2, 0) != 0)
+ {
+ HDEBUG(D_transport)
+ debug_printf("failed reaping pipelined cmd responses\n");
+ goto RESPONSE_FAILED;
+ }
+ sx->early_pipe_active = FALSE;
+ }
+ }
+ else
+#endif
+ if (!smtp_reap_ehlo(sx))
+ goto RESPONSE_FAILED;
+ }
+ else
+ DEBUG(D_transport)
+ debug_printf("not sending EHLO (host matches hosts_avoid_esmtp)\n");
+
+#ifndef DISABLE_PIPE_CONNECT
+ if (!sx->early_pipe_active)
+#endif
+ if (!sx->esmtp)
+ {
+ BOOL good_response;
+ int n = sizeof(sx->buffer);
+ uschar * rsp = sx->buffer;
+
+ if (sx->esmtp_sent && (n = Ustrlen(sx->buffer) + 1) < sizeof(sx->buffer)/2)
+ { rsp = sx->buffer + n; n = sizeof(sx->buffer) - n; }
+
+ if (smtp_write_command(sx, SCMD_FLUSH, "HELO %s\r\n", sx->helo_data) < 0)
+ goto SEND_FAILED;
+ good_response = smtp_read_response(sx, rsp, n, '2', ob->command_timeout);
+#ifdef EXPERIMENTAL_DSN_INFO
+ sx->helo_response = string_copy(rsp);
+#endif
+ if (!good_response)
+ {
+ /* Handle special logging for a closed connection after HELO
+ when had previously sent EHLO */
+
+ if (rsp != sx->buffer && rsp[0] == 0 && (errno == 0 || errno == ECONNRESET))
+ {
+ errno = ERRNO_SMTPCLOSED;
+ goto EHLOHELO_FAILED;
+ }
+ memmove(sx->buffer, rsp, Ustrlen(rsp));
+ goto RESPONSE_FAILED;
+ }
+ }
+
+ if (sx->esmtp || sx->lmtp)
+ {
+#ifndef DISABLE_PIPE_CONNECT
+ if (!sx->early_pipe_active)
+#endif
+ {
+ sx->peer_offered = ehlo_response(sx->buffer,
+ OPTION_TLS /* others checked later */
+#ifndef DISABLE_PIPE_CONNECT
+ | (sx->early_pipe_ok
+ ? OPTION_IGNQ
+ | OPTION_CHUNKING | OPTION_PRDR | OPTION_DSN | OPTION_PIPE | OPTION_SIZE
+#ifdef SUPPORT_I18N
+ | OPTION_UTF8
+#endif
+ | OPTION_EARLY_PIPE
+ : 0
+ )
+#endif
+ );
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ if (tls_out.active.sock >= 0 || !(sx->peer_offered & OPTION_TLS))
+ {
+ ehlo_response_limits_read(sx);
+ ehlo_response_limits_apply(sx);
+ }
+#endif
+#ifndef DISABLE_PIPE_CONNECT
+ if (sx->early_pipe_ok)
+ {
+ sx->ehlo_resp.cleartext_features = sx->peer_offered;
+
+ if ( (sx->peer_offered & (OPTION_PIPE | OPTION_EARLY_PIPE))
+ == (OPTION_PIPE | OPTION_EARLY_PIPE))
+ {
+ DEBUG(D_transport) debug_printf("PIPECONNECT usable in future for this IP\n");
+ sx->ehlo_resp.cleartext_auths = study_ehlo_auths(sx);
+ write_ehlo_cache_entry(sx);
+ }
+ }
+#endif
+ ehlo_response_lbserver(sx, ob);
+ }
+
+ /* Set tls_offered if the response to EHLO specifies support for STARTTLS. */
+
+#ifndef DISABLE_TLS
+ smtp_peer_options |= sx->peer_offered & OPTION_TLS;
+#endif
+ }
+ }
+
+/* For continuing deliveries down the same channel, having re-exec'd the socket
+is the standard input; for a socket held open from verify it is recorded
+in the cutthrough context block. Either way 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_peer_options will have been
+set from the command line if they were set in the process that passed the
+connection on. */
+
+/*XXX continue case needs to propagate DSN_INFO, prob. in deliver.c
+as the continue goes via transport_pass_socket() and doublefork and exec.
+It does not wait. Unclear how we keep separate host's responses
+separate - we could match up by host ip+port as a bodge. */
+
+else
+ {
+ if (cutthrough.cctx.sock >= 0 && cutthrough.callout_hold_only)
+ {
+ sx->cctx = cutthrough.cctx;
+ sx->conn_args.host->port = sx->port = cutthrough.host.port;
+ }
+ else
+ {
+ sx->cctx.sock = 0; /* stdin */
+ sx->cctx.tls_ctx = NULL;
+ smtp_port_for_connect(sx->conn_args.host, sx->port); /* Record the port that was used */
+ }
+ sx->inblock.cctx = sx->outblock.cctx = &sx->cctx;
+ smtp_command = big_buffer;
+ sx->peer_offered = smtp_peer_options;
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ /* Limits passed by cmdline over exec. */
+ ehlo_limits_apply(sx,
+ sx->peer_limit_mail = continue_limit_mail,
+ sx->peer_limit_rcpt = continue_limit_rcpt,
+ sx->peer_limit_rcptdom = continue_limit_rcptdom);
+#endif
+ sx->helo_data = NULL; /* ensure we re-expand ob->helo_data */
+
+ /* For a continued connection with TLS being proxied for us, or a
+ held-open verify connection with TLS, nothing more to do. */
+
+ if ( continue_proxy_cipher
+ || (cutthrough.cctx.sock >= 0 && cutthrough.callout_hold_only
+ && cutthrough.is_tls)
+ )
+ {
+ sx->pipelining_used = pipelining_active = !!(smtp_peer_options & OPTION_PIPE);
+ HDEBUG(D_transport) debug_printf("continued connection, %s TLS\n",
+ continue_proxy_cipher ? "proxied" : "verify conn with");
+ return OK;
+ }
+ HDEBUG(D_transport) debug_printf("continued connection, no TLS\n");
+ }
+
+/* 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. */
+
+#ifndef DISABLE_TLS
+if ( smtp_peer_options & OPTION_TLS
+ && !suppress_tls
+ && verify_check_given_host(CUSS &ob->hosts_avoid_tls, sx->conn_args.host) != OK
+ && ( !sx->verify
+ || verify_check_given_host(CUSS &ob->hosts_verify_avoid_tls, sx->conn_args.host) != OK
+ ) )
+ {
+ uschar buffer2[4096];
+
+ if (smtp_write_command(sx, SCMD_FLUSH, "STARTTLS\r\n") < 0)
+ goto SEND_FAILED;
+
+#ifndef DISABLE_PIPE_CONNECT
+ /* If doing early-pipelining reap the banner and EHLO-response but leave
+ the response for the STARTTLS we just sent alone. On fail, assume wrong
+ cached capability and retry with the pipelining disabled. */
+
+ if (sx->early_pipe_active)
+ {
+ if (sync_responses(sx, 2, 0) != 0)
+ {
+ HDEBUG(D_transport)
+ debug_printf("failed reaping pipelined cmd responses\n");
+ close(sx->cctx.sock);
+ sx->cctx.sock = -1;
+ sx->early_pipe_active = FALSE;
+ goto PIPE_CONNECT_RETRY;
+ }
+ }
+#endif
+
+ /* 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(sx, buffer2, sizeof(buffer2), '2', ob->command_timeout))
+ {
+ if ( errno != 0
+ || buffer2[0] == 0
+ || (buffer2[0] == '4' && !ob->tls_tempfail_tryclear)
+ )
+ {
+ Ustrncpy(sx->buffer, buffer2, sizeof(sx->buffer));
+ sx->buffer[sizeof(sx->buffer)-1] = '\0';
+ goto RESPONSE_FAILED;
+ }
+ }
+
+ /* STARTTLS accepted: try to negotiate a TLS session. */
+
+ else
+ TLS_NEGOTIATE:
+ {
+ sx->conn_args.sending_ip_address = sending_ip_address;
+ if (!tls_client_start(&sx->cctx, &sx->conn_args, sx->addrlist, &tls_out, &tls_errstr))
+ {
+ /* 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. */
+ TLS_CONN_FAILED:
+ DEBUG(D_tls) debug_printf("TLS session fail: %s\n", tls_errstr);
+
+# ifdef SUPPORT_DANE
+ if (sx->conn_args.dane)
+ {
+ log_write(0, LOG_MAIN,
+ "DANE attempt failed; TLS connection to %s [%s]: %s",
+ sx->conn_args.host->name, sx->conn_args.host->address, tls_errstr);
+# ifndef DISABLE_EVENT
+ (void) event_raise(sx->conn_args.tblock->event_action,
+ US"dane:fail", US"validation-failure", NULL); /* could do with better detail */
+# endif
+ }
+# endif
+
+ errno = ERRNO_TLSFAILURE;
+ message = string_sprintf("TLS session: %s", tls_errstr);
+ sx->send_quit = FALSE;
+ goto TLS_FAILED;
+ }
+ sx->send_tlsclose = TRUE;
+
+ /* TLS session is set up. Check the inblock fill level. If there is
+ content then as we have not yet done a tls read it must have arrived before
+ the TLS handshake, in-clear. That violates the sync requirement of the
+ STARTTLS RFC, so fail. */
+
+ if (sx->inblock.ptr != sx->inblock.ptrend)
+ {
+ DEBUG(D_tls)
+ {
+ int i = sx->inblock.ptrend - sx->inblock.ptr;
+ debug_printf("unused data in input buffer after ack for STARTTLS:\n"
+ "'%.*s'%s\n",
+ i > 100 ? 100 : i, sx->inblock.ptr, i > 100 ? "..." : "");
+ }
+ tls_errstr = US"synch error before connect";
+ goto TLS_CONN_FAILED;
+ }
+
+ smtp_peer_options_wrap = smtp_peer_options;
+ for (address_item * addr = sx->addrlist; addr; 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;
+ addr->tlsver = tls_out.ver;
+ }
+ }
+ }
+
+/* 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.sock >= 0)
+ {
+ uschar * greeting_cmd;
+
+ if (!sx->helo_data && !(sx->helo_data = expand_string(ob->helo_data)))
+ {
+ uschar *message = string_sprintf("failed to expand helo_data: %s",
+ expand_string_message);
+ set_errno_nohost(sx->addrlist, ERRNO_EXPANDFAIL, message, DEFER, FALSE, &sx->delivery_start);
+ yield = DEFER;
+ goto SEND_QUIT;
+ }
+
+#ifndef DISABLE_PIPE_CONNECT
+ /* For SMTPS there is no cleartext early-pipe; use the crypted permission bit.
+ We're unlikely to get the group sent and delivered before the server sends its
+ banner, but it's still worth sending as a group.
+ For STARTTLS allow for cleartext early-pipe but no crypted early-pipe, but not
+ the reverse. */
+
+ if (sx->smtps ? sx->early_pipe_ok : sx->early_pipe_active)
+ {
+ sx->peer_offered = sx->ehlo_resp.crypted_features;
+ if ((sx->early_pipe_active =
+ !!(sx->ehlo_resp.crypted_features & OPTION_EARLY_PIPE)))
+ DEBUG(D_transport) debug_printf("Using cached crypted PIPECONNECT\n");
+ }
+#endif
+#ifdef EXPERIMMENTAL_ESMTP_LIMITS
+ /* As we are about to send another EHLO, forget any LIMITS received so far. */
+ sx->peer_limit_mail = sx->peer_limit_rcpt = sx->peer_limit_rcptdom = 0;
+ if ((sx->max_mail = sx->conn_args.tblock->connection_max_message) == 0) sx->max_mail = 999999;
+ if ((sx->max_rcpt = sx->conn_args.tblock->max_addresses) == 0) sx->max_rcpt = 999999;
+ sx->single_rcpt_domain = FALSE;
+#endif
+
+ /* For SMTPS we need to wait for the initial OK response. */
+ if (sx->smtps)
+#ifndef DISABLE_PIPE_CONNECT
+ if (sx->early_pipe_active)
+ {
+ sx->pending_BANNER = TRUE;
+ sx->outblock.cmd_count = 1;
+ }
+ else
+#endif
+ if (!smtp_reap_banner(sx))
+ goto RESPONSE_FAILED;
+
+ if (sx->lmtp)
+ greeting_cmd = US"LHLO";
+ else if (sx->esmtp)
+ greeting_cmd = US"EHLO";
+ else
+ {
+ greeting_cmd = US"HELO";
+ DEBUG(D_transport)
+ debug_printf("not sending EHLO (host matches hosts_avoid_esmtp)\n");
+ }
+
+ if (smtp_write_command(sx,
+#ifndef DISABLE_PIPE_CONNECT
+ sx->early_pipe_active ? SCMD_BUFFER :
+#endif
+ SCMD_FLUSH,
+ "%s %s\r\n", greeting_cmd, sx->helo_data) < 0)
+ goto SEND_FAILED;
+
+#ifndef DISABLE_PIPE_CONNECT
+ if (sx->early_pipe_active)
+ sx->pending_EHLO = TRUE;
+ else
+#endif
+ {
+ if (!smtp_reap_ehlo(sx))
+#ifdef USE_GNUTLS
+ {
+ /* The GnuTLS layer in Exim only spots a server-rejection of a client
+ cert late, under TLS1.3 - which means here; the first time we try to
+ receive crypted data. Treat it as if it was a connect-time failure.
+ See also the early-pipe equivalent... which will be hard; every call
+ to sync_responses will need to check the result.
+ It would be nicer to have GnuTLS check the cert during the handshake.
+ Can it do that, with all the flexibility we need? */
+
+ tls_errstr = US"error on first read";
+ goto TLS_CONN_FAILED;
+ }
+#else
+ goto RESPONSE_FAILED;
+#endif
+ smtp_peer_options = 0;
+ }
+ }
+
+/* If the host is required to use a secure channel, ensure that we
+have one. */
+
+else if ( sx->smtps
+# ifdef SUPPORT_DANE
+ || sx->conn_args.dane
+# endif
+ || verify_check_given_host(CUSS &ob->hosts_require_tls, sx->conn_args.host) == OK
+ )
+ {
+ errno = ERRNO_TLSREQUIRED;
+ message = string_sprintf("a TLS session is required, but %s",
+ smtp_peer_options & OPTION_TLS
+ ? "an attempt to start TLS failed" : "the server did not offer TLS support");
+# if defined(SUPPORT_DANE) && !defined(DISABLE_EVENT)
+ if (sx->conn_args.dane)
+ (void) event_raise(sx->conn_args.tblock->event_action, US"dane:fail",
+ smtp_peer_options & OPTION_TLS
+ ? US"validation-failure" /* could do with better detail */
+ : US"starttls-not-supported",
+ NULL);
+# endif
+ goto TLS_FAILED;
+ }
+#endif /*DISABLE_TLS*/
+
+/* 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
+#ifndef DISABLE_TLS
+ || tls_out.active.sock >= 0
+#endif
+ )
+ {
+ if (sx->esmtp || sx->lmtp)
+ {
+#ifndef DISABLE_PIPE_CONNECT
+ if (!sx->early_pipe_active)
+#endif
+ {
+ sx->peer_offered = ehlo_response(sx->buffer,
+ 0 /* no TLS */
+#ifndef DISABLE_PIPE_CONNECT
+ | (sx->lmtp && ob->lmtp_ignore_quota ? OPTION_IGNQ : 0)
+ | OPTION_DSN | OPTION_PIPE | OPTION_SIZE
+ | OPTION_CHUNKING | OPTION_PRDR | OPTION_UTF8
+ | (tls_out.active.sock >= 0 ? OPTION_EARLY_PIPE : 0) /* not for lmtp */
+
+#else
+
+ | (sx->lmtp && ob->lmtp_ignore_quota ? OPTION_IGNQ : 0)
+ | OPTION_CHUNKING
+ | OPTION_PRDR
+# ifdef SUPPORT_I18N
+ | (sx->addrlist->prop.utf8_msg ? OPTION_UTF8 : 0)
+ /*XXX if we hand peercaps on to continued-conn processes,
+ must not depend on this addr */
+# endif
+ | OPTION_DSN
+ | OPTION_PIPE
+ | (ob->size_addition >= 0 ? OPTION_SIZE : 0)
+#endif
+ );
+#ifndef DISABLE_PIPE_CONNECT
+ if (tls_out.active.sock >= 0)
+ sx->ehlo_resp.crypted_features = sx->peer_offered;
+#endif
+
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ if (tls_out.active.sock >= 0 || !(sx->peer_offered & OPTION_TLS))
+ {
+ ehlo_response_limits_read(sx);
+ ehlo_response_limits_apply(sx);
+ }
+#endif
+ }
+
+ /* Set for IGNOREQUOTA if the response to LHLO specifies support and the
+ lmtp_ignore_quota option was set. */
+
+ sx->igquotstr = sx->peer_offered & OPTION_IGNQ ? US" IGNOREQUOTA" : US"";
+
+ /* If the response to EHLO specified support for the SIZE parameter, note
+ this, provided size_addition is non-negative. */
+
+ smtp_peer_options |= sx->peer_offered & OPTION_SIZE;
+
+ /* 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. */
+
+ if ( sx->peer_offered & OPTION_PIPE
+ && verify_check_given_host(CUSS &ob->hosts_avoid_pipelining, sx->conn_args.host) != OK)
+ smtp_peer_options |= OPTION_PIPE;
+
+ DEBUG(D_transport) debug_printf("%susing PIPELINING\n",
+ smtp_peer_options & OPTION_PIPE ? "" : "not ");
+
+ if ( sx->peer_offered & OPTION_CHUNKING
+ && verify_check_given_host(CUSS &ob->hosts_try_chunking, sx->conn_args.host) == OK)
+ smtp_peer_options |= OPTION_CHUNKING;
+
+ if (smtp_peer_options & OPTION_CHUNKING)
+ DEBUG(D_transport) debug_printf("CHUNKING usable\n");
+
+#ifndef DISABLE_PRDR
+ if ( sx->peer_offered & OPTION_PRDR
+ && verify_check_given_host(CUSS &ob->hosts_try_prdr, sx->conn_args.host) == OK)
+ smtp_peer_options |= OPTION_PRDR;
+
+ if (smtp_peer_options & OPTION_PRDR)
+ DEBUG(D_transport) debug_printf("PRDR usable\n");
+#endif
+
+ /* Note if the server supports DSN */
+ smtp_peer_options |= sx->peer_offered & OPTION_DSN;
+ DEBUG(D_transport) debug_printf("%susing DSN\n",
+ sx->peer_offered & OPTION_DSN ? "" : "not ");
+
+#ifndef DISABLE_PIPE_CONNECT
+ if ( sx->early_pipe_ok
+ && !sx->early_pipe_active
+ && tls_out.active.sock >= 0
+ && smtp_peer_options & OPTION_PIPE
+ && ( sx->ehlo_resp.cleartext_features | sx->ehlo_resp.crypted_features)
+ & OPTION_EARLY_PIPE)
+ {
+ DEBUG(D_transport) debug_printf("PIPECONNECT usable in future for this IP\n");
+ sx->ehlo_resp.crypted_auths = study_ehlo_auths(sx);
+ write_ehlo_cache_entry(sx);
+ }
+#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(sx))
+ {
+ default: goto SEND_QUIT;
+ case OK: break;
+ case FAIL_SEND: goto SEND_FAILED;
+ case FAIL: goto RESPONSE_FAILED;
+ }
+ }
+ }
+sx->pipelining_used = pipelining_active = !!(smtp_peer_options & OPTION_PIPE);
+
+/* The setting up of the SMTP call is now complete. Any subsequent errors are
+message-specific. */
+
+sx->setting_up = FALSE;
+
+#ifdef SUPPORT_I18N
+if (sx->addrlist->prop.utf8_msg)
+ {
+ uschar * s;
+
+ /* If the transport sets a downconversion mode it overrides any set by ACL
+ for the message. */
+
+ if ((s = ob->utf8_downconvert))
+ {
+ if (!(s = expand_string(s)))
+ {
+ message = string_sprintf("failed to expand utf8_downconvert: %s",
+ expand_string_message);
+ set_errno_nohost(sx->addrlist, ERRNO_EXPANDFAIL, message, DEFER, FALSE, &sx->delivery_start);
+ yield = DEFER;
+ goto SEND_QUIT;
+ }
+ switch (*s)
+ {
+ case '1': sx->addrlist->prop.utf8_downcvt = TRUE;
+ sx->addrlist->prop.utf8_downcvt_maybe = FALSE;
+ break;
+ case '0': sx->addrlist->prop.utf8_downcvt = FALSE;
+ sx->addrlist->prop.utf8_downcvt_maybe = FALSE;
+ break;
+ case '-': if (s[1] == '1')
+ {
+ sx->addrlist->prop.utf8_downcvt = FALSE;
+ sx->addrlist->prop.utf8_downcvt_maybe = TRUE;
+ }
+ break;
+ }
+ }
+
+ sx->utf8_needed = !sx->addrlist->prop.utf8_downcvt
+ && !sx->addrlist->prop.utf8_downcvt_maybe;
+ DEBUG(D_transport) if (!sx->utf8_needed)
+ debug_printf("utf8: %s downconvert\n",
+ sx->addrlist->prop.utf8_downcvt ? "mandatory" : "optional");
+ }
+
+/* If this is an international message we need the host to speak SMTPUTF8 */
+if (sx->utf8_needed && !(sx->peer_offered & OPTION_UTF8))
+ {
+ errno = ERRNO_UTF8_FWD;
+ goto RESPONSE_FAILED;
+ }
+#endif /*SUPPORT_I18N*/
+
+return OK;
+
+
+ {
+ int code;
+
+ RESPONSE_FAILED:
+ if (errno == ECONNREFUSED) /* first-read error on a TFO conn */
+ {
+ /* 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. This is done for
+ a 3whs connection in ip_connect(), but a TFO connection does not error
+ there - instead it gets ECONNREFUSED on the first data read. Tracking
+ that a TFO really was done is too hard, or we would set a
+ sx->pending_conn_done bit and test that in smtp_reap_banner() and
+ smtp_reap_ehlo(). That would let us also add the conn-timeout to the
+ cmd-timeout. */
+
+ if (f.running_in_test_harness && ob->connect_timeout == 999999)
+ errno = ETIMEDOUT;
+ set_errno_nohost(sx->addrlist,
+ errno == ETIMEDOUT ? ERRNO_CONNECTTIMEOUT : errno,
+ sx->verify ? US strerror(errno) : NULL,
+ DEFER, FALSE, &sx->delivery_start);
+ sx->send_quit = FALSE;
+ return DEFER;
+ }
+
+ /* really an error on an SMTP read */
+ message = NULL;
+ sx->send_quit = check_response(sx->conn_args.host, &errno, sx->addrlist->more_errno,
+ sx->buffer, &code, &message, &pass_message);
+ yield = DEFER;
+ goto FAILED;
+
+ SEND_FAILED:
+ code = '4';
+ message = US string_sprintf("smtp send to %s [%s] failed: %s",
+ sx->conn_args.host->name, sx->conn_args.host->address, strerror(errno));
+ sx->send_quit = FALSE;
+ yield = DEFER;
+ goto FAILED;
+
+ EHLOHELO_FAILED:
+ code = '4';
+ message = string_sprintf("Remote host closed connection in response to %s"
+ " (EHLO response was: %s)", smtp_command, sx->buffer);
+ sx->send_quit = FALSE;
+ yield = DEFER;
+ 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 errno, and setting_up will always be true. Treat as
+ a temporary error. */
+
+#ifndef DISABLE_TLS
+ TLS_FAILED:
+ code = '4', yield = DEFER;
+ goto FAILED;
+#endif
+
+ /* 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, and there are no more hosts to try,
+ 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.
+
+ XXX This peeking for another host feels like a layering violation. We want
+ to note the host as unusable, but down here we shouldn't know if this was
+ the last host to try for the addr(list). Perhaps the upper layer should be
+ the one to do set_errno() ? The problem is that currently the addr is where
+ errno etc. are stashed, but until we run out of hosts to try the errors are
+ host-specific. Maybe we should enhance the host_item definition? */
+
+FAILED:
+ sx->ok = FALSE; /* For when reached by GOTO */
+ set_errno(sx->addrlist, errno, message,
+ sx->conn_args.host->next
+ ? DEFER
+ : code == '5'
+#ifdef SUPPORT_I18N
+ || errno == ERRNO_UTF8_FWD
+#endif
+ ? FAIL : DEFER,
+ pass_message,
+ errno == ECONNREFUSED ? NULL : sx->conn_args.host,
+#ifdef EXPERIMENTAL_DSN_INFO
+ sx->smtp_greeting, sx->helo_response,
+#endif
+ &sx->delivery_start);
+ }
+
+
+SEND_QUIT:
+
+if (sx->send_quit)
+ (void)smtp_write_command(sx, SCMD_FLUSH, "QUIT\r\n");
+
+#ifndef DISABLE_TLS
+if (sx->cctx.tls_ctx)
+ {
+ if (sx->send_tlsclose)
+ {
+ tls_close(sx->cctx.tls_ctx, TLS_SHUTDOWN_NOWAIT);
+ sx->send_tlsclose = FALSE;
+ }
+ sx->cctx.tls_ctx = NULL;
+ }
+#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,
+*/
+
+HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" SMTP(close)>>\n");
+if (sx->send_quit)
+ {
+ shutdown(sx->cctx.sock, SHUT_WR);
+ if (fcntl(sx->cctx.sock, F_SETFL, O_NONBLOCK) == 0)
+ for (int i = 16; read(sx->cctx.sock, sx->inbuffer, sizeof(sx->inbuffer)) > 0 && i > 0;)
+ i--; /* drain socket */
+ sx->send_quit = FALSE;
+ }
+(void)close(sx->cctx.sock);
+sx->cctx.sock = -1;
+
+#ifndef DISABLE_EVENT
+(void) event_raise(sx->conn_args.tblock->event_action, US"tcp:close", NULL, NULL);
+#endif
+
+smtp_debug_cmd_report();
+continue_transport = NULL;
+continue_hostname = NULL;
+return yield;
+}
+
+
+
+
+/* Create the string of options that will be appended to the MAIL FROM:
+in the connection context buffer */
+
+static int
+build_mailcmd_options(smtp_context * sx, address_item * addrlist)
+{
+uschar * p = sx->buffer;
+address_item * addr;
+int address_count;
+
+*p = 0;
+
+/* If we know the receiving MTA supports the SIZE qualification, and we know it,
+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.) */
+
+if ( message_size > 0
+ && sx->peer_offered & OPTION_SIZE && !(sx->avoid_option & OPTION_SIZE))
+ {
+/*XXX problem here under spool_files_wireformat?
+Or just forget about lines? Or inflate by a fixed proportion? */
+
+ sprintf(CS p, " SIZE=%d", message_size+message_linecount+(SOB sx->conn_args.ob)->size_addition);
+ while (*p) p++;
+ }
+
+#ifndef DISABLE_PRDR
+/* If it supports Per-Recipient Data Responses, and we have more than one recipient,
+request that */
+
+sx->prdr_active = FALSE;
+if (smtp_peer_options & OPTION_PRDR)
+ for (address_item * addr = addrlist; 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 */
+ sx->prdr_active = TRUE;
+ sprintf(CS p, " PRDR"); p += 5;
+ break;
+ }
+ break;
+ }
+#endif
+
+#ifdef SUPPORT_I18N
+/* If it supports internationalised messages, and this meesage need that,
+request it */
+
+if ( sx->peer_offered & OPTION_UTF8
+ && addrlist->prop.utf8_msg
+ && !addrlist->prop.utf8_downcvt
+ )
+ Ustrcpy(p, US" SMTPUTF8"), p += 9;
+#endif
+
+/* check if all addresses have DSN-lasthop flag; do not send RET and ENVID if so */
+for (sx->dsn_all_lasthop = TRUE, addr = addrlist, address_count = 0;
+ addr && address_count < sx->max_rcpt; /*XXX maybe also || sx->single_rcpt_domain ? */
+ addr = addr->next) if (addr->transport_return == PENDING_DEFER)
+ {
+ address_count++;
+ if (!(addr->dsn_flags & rf_dsnlasthop))
+ {
+ sx->dsn_all_lasthop = FALSE;
+ break;
+ }
+ }
+
+/* Add any DSN flags to the mail command */
+
+if (sx->peer_offered & OPTION_DSN && !sx->dsn_all_lasthop)
+ {
+ if (dsn_ret == dsn_ret_hdrs)
+ { Ustrcpy(p, US" RET=HDRS"); p += 9; }
+ else if (dsn_ret == dsn_ret_full)
+ { Ustrcpy(p, US" RET=FULL"); p += 9; }
+
+ if (dsn_envid)
+ {
+ string_format(p, sizeof(sx->buffer) - (p-sx->buffer), " ENVID=%s", dsn_envid);
+ while (*p) p++;
+ }
+ }
+
+/* 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. */
+
+return smtp_mail_auth_str(sx, p, addrlist) ? ERROR : OK;
+}
+
+
+static void
+build_rcptcmd_options(smtp_context * sx, const address_item * addr)
+{
+uschar * p = sx->buffer;
+*p = 0;
+
+/* Add any DSN flags to the rcpt command */
+
+if (sx->peer_offered & OPTION_DSN && !(addr->dsn_flags & rf_dsnlasthop))
+ {
+ if (addr->dsn_flags & rf_dsnflags)
+ {
+ BOOL first = TRUE;
+
+ Ustrcpy(p, US" NOTIFY=");
+ while (*p) p++;
+ for (int i = 0; i < nelem(rf_list); i++) if (addr->dsn_flags & rf_list[i])
+ {
+ if (!first) *p++ = ',';
+ first = FALSE;
+ Ustrcpy(p, rf_names[i]);
+ while (*p) p++;
+ }
+ }
+
+ if (addr->dsn_orcpt)
+ {
+ string_format(p, sizeof(sx->buffer) - (p-sx->buffer), " ORCPT=%s",
+ addr->dsn_orcpt);
+ while (*p) p++;
+ }
+ }
+}
+
+
+
+/*
+Return:
+ 0 good, rcpt results in addr->transport_return (PENDING_OK, DEFER, FAIL)
+ -1 MAIL response error
+ -2 any non-MAIL read i/o error
+ -3 non-MAIL response timeout
+ -4 internal error; channel still usable
+ -5 transmit failed
+ */
+
+int
+smtp_write_mail_and_rcpt_cmds(smtp_context * sx, int * yield)
+{
+address_item * addr;
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+address_item * restart_addr = NULL;
+#endif
+int address_count, pipe_limit;
+int rc;
+
+if (build_mailcmd_options(sx, sx->first_addr) != OK)
+ {
+ *yield = ERROR;
+ return -4;
+ }
+
+/* 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. */
+
+sx->pending_MAIL = TRUE; /* The block starts with MAIL */
+
+ {
+ uschar * s = sx->from_addr;
+#ifdef SUPPORT_I18N
+ uschar * errstr = NULL;
+
+ /* If we must downconvert, do the from-address here. Remember we had to
+ for the to-addresses (done below), and also (ugly) for re-doing when building
+ the delivery log line. */
+
+ if ( sx->addrlist->prop.utf8_msg
+ && (sx->addrlist->prop.utf8_downcvt || !(sx->peer_offered & OPTION_UTF8))
+ )
+ {
+ if (s = string_address_utf8_to_alabel(s, &errstr), errstr)
+ {
+ set_errno_nohost(sx->addrlist, ERRNO_EXPANDFAIL, errstr, DEFER, FALSE, &sx->delivery_start);
+ *yield = ERROR;
+ return -4;
+ }
+ setflag(sx->addrlist, af_utf8_downcvt);
+ }
+#endif
+
+ rc = smtp_write_command(sx, pipelining_active ? SCMD_BUFFER : SCMD_FLUSH,
+ "MAIL FROM:<%s>%s\r\n", s, sx->buffer);
+ }
+
+mail_command = string_copy(big_buffer); /* Save for later error message */
+
+switch(rc)
+ {
+ case -1: /* Transmission error */
+ return -5;
+
+ case +1: /* Cmd was sent */
+ if (!smtp_read_response(sx, sx->buffer, sizeof(sx->buffer), '2',
+ (SOB sx->conn_args.ob)->command_timeout))
+ {
+ if (errno == 0 && sx->buffer[0] == '4')
+ {
+ errno = ERRNO_MAIL4XX;
+ sx->addrlist->more_errno |= ((sx->buffer[1] - '0')*10 + sx->buffer[2] - '0') << 8;
+ }
+ return -1;
+ }
+ sx->pending_MAIL = FALSE;
+ break;
+
+ /* otherwise zero: command queued for pipeline */
+ }
+
+/* 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 next_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 verify we flush the pipeline after any (the only) rcpt address. */
+
+for (addr = sx->first_addr, address_count = 0, pipe_limit = 100;
+ addr && address_count < sx->max_rcpt;
+ addr = addr->next) if (addr->transport_return == PENDING_DEFER)
+ {
+ int cmds_sent;
+ BOOL no_flush;
+ uschar * rcpt_addr;
+
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ if ( sx->single_rcpt_domain /* restriction on domains */
+ && address_count > 0 /* not first being sent */
+ && Ustrcmp(addr->domain, sx->first_addr->domain) != 0 /* dom diff from first */
+ )
+ {
+ DEBUG(D_transport) debug_printf("skipping different domain %s\n", addr->domain);
+
+ /* Ensure the smtp-response reaper does not think the address had a RCPT
+ command sent for it. Reset to PENDING_DEFER in smtp_deliver(), where we
+ goto SEND_MESSAGE. */
+
+ addr->transport_return = SKIP;
+ if (!restart_addr) restart_addr = addr; /* note restart point */
+ continue; /* skip this one */
+ }
+#endif
+
+ addr->dsn_aware = sx->peer_offered & OPTION_DSN
+ ? dsn_support_yes : dsn_support_no;
+
+ address_count++;
+ if (pipe_limit-- <= 0)
+ { no_flush = FALSE; pipe_limit = 100; }
+ else
+ no_flush = pipelining_active && !sx->verify
+ && (!mua_wrapper || addr->next && address_count < sx->max_rcpt);
+
+ build_rcptcmd_options(sx, addr);
+
+ /* 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. */
+
+ rcpt_addr = transport_rcpt_address(addr, sx->conn_args.tblock->rcpt_include_affixes);
+
+#ifdef SUPPORT_I18N
+ if ( testflag(sx->addrlist, af_utf8_downcvt)
+ && !(rcpt_addr = string_address_utf8_to_alabel(rcpt_addr, NULL))
+ )
+ {
+ /*XXX could we use a per-address errstr here? Not fail the whole send? */
+ errno = ERRNO_EXPANDFAIL;
+ return -5; /*XXX too harsh? */
+ }
+#endif
+
+ cmds_sent = smtp_write_command(sx, no_flush ? SCMD_BUFFER : SCMD_FLUSH,
+ "RCPT TO:<%s>%s%s\r\n", rcpt_addr, sx->igquotstr, sx->buffer);
+
+ if (cmds_sent < 0) return -5;
+ if (cmds_sent > 0)
+ {
+ switch(sync_responses(sx, cmds_sent, 0))
+ {
+ case 3: sx->ok = TRUE; /* 2xx & 5xx => OK & progress made */
+ case 2: sx->completed_addr = TRUE; /* 5xx (only) => progress made */
+ break;
+
+ case 1: sx->ok = TRUE; /* 2xx (only) => OK, but if LMTP, */
+ if (!sx->lmtp) /* can't tell about progress yet */
+ sx->completed_addr = TRUE;
+ case 0: /* No 2xx or 5xx, but no probs */
+ /* If any RCPT got a 452 response then next_addr has been updated
+ for restarting with a new MAIL on the same connection. Send no more
+ RCPTs for this MAIL. */
+
+ if (sx->RCPT_452)
+ {
+ DEBUG(D_transport) debug_printf("seen 452 too-many-rcpts\n");
+ sx->RCPT_452 = FALSE;
+ /* sx->next_addr has been reset for fast_retry */
+ return 0;
+ }
+ break;
+
+ case -1: return -3; /* Timeout on RCPT */
+ case -2: return -2; /* non-MAIL read i/o error */
+ default: return -1; /* any MAIL error */
+
+#ifndef DISABLE_PIPE_CONNECT
+ case -4: return -1; /* non-2xx for pipelined banner or EHLO */
+ case -5: return -1; /* TLS first-read error */
+#endif
+ }
+ }
+ } /* Loop for next address */
+
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+sx->next_addr = restart_addr ? restart_addr : addr;
+#else
+sx->next_addr = addr;
+#endif
+return 0;
+}
+
+
+#ifndef DISABLE_TLS
+/*****************************************************
+* Proxy TLS connection for another transport process *
+******************************************************/
+/*
+Close the unused end of the pipe, fork once more, then use the given buffer
+as a staging area, and select on both the given fd and the TLS'd client-fd for
+data to read (per the coding in ip_recv() and fd_ready() this is legitimate).
+Do blocking full-size writes, and reads under a timeout. Once both input
+channels are closed, exit the process.
+
+Arguments:
+ ct_ctx tls context
+ buf space to use for buffering
+ bufsiz size of buffer
+ pfd pipe filedescriptor array; [0] is comms to proxied process
+ timeout per-read timeout, seconds
+ host hostname of remote
+
+Does not return.
+*/
+
+void
+smtp_proxy_tls(void * ct_ctx, uschar * buf, size_t bsize, int * pfd,
+ int timeout, const uschar * host)
+{
+struct pollfd p[2] = {{.fd = tls_out.active.sock, .events = POLLIN},
+ {.fd = pfd[0], .events = POLLIN}};
+int rc, i;
+BOOL send_tls_shutdown = TRUE;
+
+close(pfd[1]);
+if ((rc = exim_fork(US"tls-proxy")))
+ _exit(rc < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
+
+set_process_info("proxying TLS connection for continued transport to %s\n", host);
+
+do
+ {
+ time_t time_left = timeout;
+ time_t time_start = time(NULL);
+
+ /* wait for data */
+ do
+ {
+ rc = poll(p, 2, time_left * 1000);
+
+ if (rc < 0 && errno == EINTR)
+ if ((time_left -= time(NULL) - time_start) > 0) continue;
+
+ if (rc <= 0)
+ {
+ DEBUG(D_transport) if (rc == 0) debug_printf("%s: timed out\n", __FUNCTION__);
+ goto done;
+ }
+
+ /* For errors where not readable, bomb out */
+
+ if (p[0].revents & POLLERR || p[1].revents & POLLERR)
+ {
+ DEBUG(D_transport) debug_printf("select: exceptional cond on %s fd\n",
+ p[0].revents & POLLERR ? "tls" : "proxy");
+ if (!(p[0].revents & POLLIN || p[1].events & POLLIN))
+ goto done;
+ DEBUG(D_transport) debug_printf("- but also readable; no exit yet\n");
+ }
+ }
+ while (rc < 0 || !(p[0].revents & POLLIN || p[1].revents & POLLIN));
+
+ /* handle inbound data */
+ if (p[0].revents & POLLIN)
+ if ((rc = tls_read(ct_ctx, buf, bsize)) <= 0) /* Expect -1 for EOF; */
+ { /* that reaps the TLS Close Notify record */
+ p[0].fd = -1;
+ shutdown(pfd[0], SHUT_WR);
+ timeout = 5;
+ }
+ else
+ for (int nbytes = 0; rc - nbytes > 0; nbytes += i)
+ if ((i = write(pfd[0], buf + nbytes, rc - nbytes)) < 0) goto done;
+
+ /* Handle outbound data. We cannot combine payload and the TLS-close
+ due to the limitations of the (pipe) channel feeding us. Maybe use a unix-domain
+ socket? */
+ if (p[1].revents & POLLIN)
+ if ((rc = read(pfd[0], buf, bsize)) <= 0)
+ {
+ p[1].fd = -1;
+
+# ifdef EXIM_TCP_CORK /* Use _CORK to get TLS Close Notify in FIN segment */
+ (void) setsockopt(tls_out.active.sock, IPPROTO_TCP, EXIM_TCP_CORK, US &on, sizeof(on));
+# endif
+ tls_shutdown_wr(ct_ctx);
+ send_tls_shutdown = FALSE;
+ shutdown(tls_out.active.sock, SHUT_WR);
+ }
+ else
+ for (int nbytes = 0; rc - nbytes > 0; nbytes += i)
+ if ((i = tls_write(ct_ctx, buf + nbytes, rc - nbytes, FALSE)) < 0)
+ goto done;
+ }
+while (p[0].fd >= 0 || p[1].fd >= 0);
+
+done:
+ if (send_tls_shutdown) tls_close(ct_ctx, TLS_SHUTDOWN_NOWAIT);
+ ct_ctx = NULL;
+ testharness_pause_ms(100); /* let logging complete */
+ exim_exit(EXIT_SUCCESS);
+}
+#endif
+
+
+/*************************************************
+* 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 either
+closes it down before passing the connection on, or inserts a TLS-proxy
+process and passes on a cleartext conection.
+
+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
+ defport default TCP/IP port to use if host does not specify, in host
+ byte order
+ interface interface to bind to, or NULL
+ tblock transport instance block
+ 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
+
+ For all non-OK returns the first addr of the list carries the
+ time taken for the attempt.
+*/
+
+static int
+smtp_deliver(address_item *addrlist, host_item *host, int host_af, int defport,
+ uschar *interface, transport_instance *tblock,
+ BOOL *message_defer, BOOL suppress_tls)
+{
+smtp_transport_options_block * ob = SOB tblock->options_block;
+int yield = OK;
+int save_errno;
+int rc;
+
+uschar *message = NULL;
+uschar new_message_id[MESSAGE_ID_LENGTH + 1];
+smtp_context * sx = store_get(sizeof(*sx), GET_TAINTED); /* tainted, for the data buffers */
+BOOL pass_message = FALSE;
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+BOOL mail_limit = FALSE;
+#endif
+#ifdef SUPPORT_DANE
+BOOL dane_held;
+#endif
+BOOL tcw_done = FALSE, tcw = FALSE;
+
+*message_defer = FALSE;
+
+memset(sx, 0, sizeof(*sx));
+sx->addrlist = addrlist;
+sx->conn_args.host = host;
+sx->conn_args.host_af = host_af;
+sx->port = defport;
+sx->conn_args.interface = interface;
+sx->helo_data = NULL;
+sx->conn_args.tblock = tblock;
+sx->conn_args.sock = -1;
+gettimeofday(&sx->delivery_start, NULL);
+sx->sync_addr = sx->first_addr = addrlist;
+
+REPEAT_CONN:
+#ifdef SUPPORT_DANE
+dane_held = FALSE;
+#endif
+
+/* Get the channel set up ready for a message, MAIL FROM being the next
+SMTP command to send. */
+
+if ((rc = smtp_setup_conn(sx, suppress_tls)) != OK)
+ {
+ timesince(&addrlist->delivery_time, &sx->delivery_start);
+ yield = rc;
+ goto TIDYUP;
+ }
+
+#ifdef SUPPORT_DANE
+/* If the connection used DANE, ignore for now any addresses with incompatible
+domains. The SNI has to be the domain. Arrange a whole new TCP conn later,
+just in case only TLS isn't enough. */
+
+if (sx->conn_args.dane)
+ {
+ const uschar * dane_domain = sx->first_addr->domain;
+
+ for (address_item * a = sx->first_addr->next; a; a = a->next)
+ if ( a->transport_return == PENDING_DEFER
+ && Ustrcmp(dane_domain, a->domain) != 0)
+ {
+ DEBUG(D_transport) debug_printf("DANE: holding %s for later\n", a->domain);
+ dane_held = TRUE;
+ a->transport_return = DANE;
+ }
+ }
+#endif
+
+/* If there is a filter command specified for this transport, we can now
+set it up. This cannot be done until the identity of the host is known. */
+
+if (tblock->filter_command)
+ {
+ transport_filter_timeout = tblock->filter_timeout;
+
+ /* On failure, copy the error to all addresses, abandon the SMTP call, and
+ yield ERROR. */
+
+ if (!transport_set_up_command(&transport_filter_argv,
+ tblock->filter_command, TRUE, DEFER, addrlist, FALSE,
+ string_sprintf("%.50s transport filter", tblock->name), NULL))
+ {
+ set_errno_nohost(addrlist->next, addrlist->basic_errno, addrlist->message, DEFER,
+ FALSE, &sx->delivery_start);
+ yield = ERROR;
+ goto SEND_QUIT;
+ }
+
+ if ( transport_filter_argv
+ && *transport_filter_argv
+ && **transport_filter_argv
+ && smtp_peer_options & OPTION_CHUNKING
+#ifndef DISABLE_DKIM
+ /* When dkim signing, chunking is handled even with a transport-filter */
+ && !(ob->dkim.dkim_private_key && ob->dkim.dkim_domain && ob->dkim.dkim_selector)
+ && !ob->dkim.force_bodyhash
+#endif
+ )
+ {
+ smtp_peer_options &= ~OPTION_CHUNKING;
+ DEBUG(D_transport) debug_printf("CHUNKING not usable due to transport filter\n");
+ }
+ }
+
+/* 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:
+sx->from_addr = return_path;
+sx->sync_addr = sx->first_addr;
+sx->ok = FALSE;
+sx->send_rset = TRUE;
+sx->completed_addr = FALSE;
+
+
+/* If we are a continued-connection-after-verify the MAIL and RCPT
+commands were already sent; do not re-send but do mark the addrs as
+having been accepted up to RCPT stage. A traditional cont-conn
+always has a sequence number greater than one. */
+
+if (continue_hostname && continue_sequence == 1)
+ {
+ /* sx->pending_MAIL = FALSE; */
+ sx->ok = TRUE;
+ /* sx->next_addr = NULL; */
+
+ for (address_item * addr = addrlist; addr; addr = addr->next)
+ addr->transport_return = PENDING_OK;
+ }
+else
+ {
+ /* Initiate a message transfer. */
+
+ switch(smtp_write_mail_and_rcpt_cmds(sx, &yield))
+ {
+ case 0: break;
+ case -1: case -2: goto RESPONSE_FAILED;
+ case -3: goto END_OFF;
+ case -4: goto SEND_QUIT;
+ default: goto SEND_FAILED;
+ }
+
+ /* 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 * a;
+ unsigned cnt;
+
+ for (a = sx->first_addr, cnt = 0; a && cnt < sx->max_rcpt; a = a->next, cnt++)
+ if (a->transport_return != PENDING_OK)
+ {
+ /*XXX could we find a better errno than 0 here? */
+ set_errno_nohost(addrlist, 0, a->message, FAIL,
+ testflag(a, af_pass_message), &sx->delivery_start);
+ sx->ok = FALSE;
+ break;
+ }
+ }
+ }
+
+/* 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 using CHUNKING, do not send a BDAT until we know how big a chunk we want
+to send is. */
+
+if ( !(smtp_peer_options & OPTION_CHUNKING)
+ && (sx->ok || (pipelining_active && !mua_wrapper)))
+ {
+ int count = smtp_write_command(sx, SCMD_FLUSH, "DATA\r\n");
+
+ if (count < 0) goto SEND_FAILED;
+ switch(sync_responses(sx, count, sx->ok ? +1 : -1))
+ {
+ case 3: sx->ok = TRUE; /* 2xx & 5xx => OK & progress made */
+ case 2: sx->completed_addr = TRUE; /* 5xx (only) => progress made */
+ break;
+
+ case 1: sx->ok = TRUE; /* 2xx (only) => OK, but if LMTP, */
+ if (!sx->lmtp) sx->completed_addr = 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 */
+
+#ifndef DISABLE_PIPE_CONNECT
+ case -5: /* TLS first-read error */
+ case -4: HDEBUG(D_transport)
+ debug_printf("failed reaping pipelined cmd responses\n");
+#endif
+ default: goto RESPONSE_FAILED; /* I/O error, or any MAIL/DATA error */
+ }
+ pipelining_active = FALSE;
+ data_command = string_copy(big_buffer); /* Save for later error message */
+ }
+
+/* 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 ( !sx->ok
+ && (!(smtp_peer_options & OPTION_CHUNKING) || !pipelining_active))
+ {
+ /* Save the first address of the next batch. */
+ sx->first_addr = sx->next_addr;
+
+ sx->ok = TRUE;
+ }
+else
+ {
+ transport_ctx tctx = {
+ .u = {.fd = sx->cctx.sock}, /*XXX will this need TLS info? */
+ .tblock = tblock,
+ .addr = addrlist,
+ .check_string = US".",
+ .escape_string = US"..", /* Escaping strings */
+ .options =
+ topt_use_crlf | 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)
+ };
+
+ /* If using CHUNKING we need a callback from the generic transport
+ support to us, for the sending of BDAT smtp commands and the reaping
+ of responses. The callback needs a whole bunch of state so set up
+ a transport-context structure to be passed around. */
+
+ if (smtp_peer_options & OPTION_CHUNKING)
+ {
+ tctx.check_string = tctx.escape_string = NULL;
+ tctx.options |= topt_use_bdat;
+ tctx.chunk_cb = smtp_chunk_cmd_callback;
+ sx->pending_BDAT = FALSE;
+ sx->good_RCPT = sx->ok;
+ sx->cmd_count = 0;
+ tctx.smtp_context = sx;
+ }
+ else
+ tctx.options |= topt_end_dot;
+
+ /* Save the first address of the next batch. */
+ sx->first_addr = sx->next_addr;
+
+ /* Responses from CHUNKING commands go in buffer. Otherwise,
+ there has not been a response. */
+
+ sx->buffer[0] = 0;
+
+ sigalrm_seen = FALSE;
+ transport_write_timeout = ob->data_timeout;
+ smtp_command = US"sending data block"; /* For error messages */
+ DEBUG(D_transport|D_v)
+ if (smtp_peer_options & OPTION_CHUNKING)
+ debug_printf(" will write message using CHUNKING\n");
+ else
+ debug_printf(" SMTP>> (writing message)\n");
+ transport_count = 0;
+
+#ifndef DISABLE_DKIM
+ {
+# ifdef MEASURE_TIMING
+ struct timeval t0;
+ gettimeofday(&t0, NULL);
+# endif
+ dkim_exim_sign_init();
+# ifdef EXPERIMENTAL_ARC
+ {
+ uschar * s = ob->arc_sign;
+ if (s)
+ {
+ if (!(ob->dkim.arc_signspec = s = expand_string(s)))
+ {
+ if (!f.expand_string_forcedfail)
+ {
+ message = US"failed to expand arc_sign";
+ sx->ok = FALSE;
+ goto SEND_FAILED;
+ }
+ }
+ else if (*s)
+ {
+ /* Ask dkim code to hash the body for ARC */
+ (void) arc_ams_setup_sign_bodyhash();
+ ob->dkim.force_bodyhash = TRUE;
+ }
+ }
+ }
+# endif
+# ifdef MEASURE_TIMING
+ report_time_since(&t0, US"dkim_exim_sign_init (delta)");
+# endif
+ }
+#endif
+
+ /* See if we can pipeline QUIT. Reasons not to are
+ - pipelining not active
+ - not ok to send quit
+ - errors in amtp transation responses
+ - more addrs to send for this message or this host
+ - this message was being retried
+ - more messages for this host
+ If we can, we want the message-write to not flush (the tail end of) its data out. */
+
+ if ( sx->pipelining_used
+ && (sx->ok && sx->completed_addr || smtp_peer_options & OPTION_CHUNKING)
+ && sx->send_quit
+ && !(sx->first_addr || f.continue_more)
+ && f.deliver_firsttime
+ )
+ {
+ smtp_compare_t t_compare =
+ {.tblock = tblock, .current_sender_address = sender_address};
+
+ tcw_done = TRUE;
+ tcw =
+#ifndef DISABLE_TLS
+ ( tls_out.active.sock < 0 && !continue_proxy_cipher
+ || verify_check_given_host(CUSS &ob->hosts_nopass_tls, host) != OK
+ )
+ &&
+#endif
+ transport_check_waiting(tblock->name, host->name,
+ tblock->connection_max_messages, new_message_id,
+ (oicf)smtp_are_same_identities, (void*)&t_compare);
+ if (!tcw)
+ {
+ HDEBUG(D_transport) debug_printf("will pipeline QUIT\n");
+ tctx.options |= topt_no_flush;
+ }
+ }
+
+#ifndef DISABLE_DKIM
+ sx->ok = dkim_transport_write_message(&tctx, &ob->dkim, CUSS &message);
+#else
+ sx->ok = transport_write_message(&tctx, 0);
+#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.
+ Or, when CHUNKING, it can be a protocol-detected failure. */
+
+ if (!sx->ok)
+ if (message) goto SEND_FAILED;
+ else 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";
+
+ /* If we can pipeline a QUIT with the data them send it now. If a new message
+ for this host appeared in the queue while data was being sent, we will not see
+ it and it will have to wait for a queue run. If there was one but another
+ thread took it, we might attempt to send it - but locking of spoolfiles will
+ detect that. Use _MORE to get QUIT in FIN segment. */
+
+ if (tcw_done && !tcw)
+ {
+ /*XXX jgh 2021/03/10 google et. al screwup. G, at least, sends TCP FIN in response to TLS
+ close-notify. Under TLS 1.3, violating RFC.
+ However, TLS 1.2 does not have half-close semantics. */
+
+ if ( sx->cctx.tls_ctx
+#if 0 && !defined(DISABLE_TLS)
+ && Ustrcmp(tls_out.ver, "TLS1.3") != 0
+#endif
+ || !f.deliver_firsttime
+ )
+ { /* Send QUIT now and not later */
+ (void)smtp_write_command(sx, SCMD_FLUSH, "QUIT\r\n");
+ sx->send_quit = FALSE;
+ }
+ else
+ { /* add QUIT to the output buffer */
+ (void)smtp_write_command(sx, SCMD_MORE, "QUIT\r\n");
+ sx->send_quit = FALSE; /* avoid sending it later */
+
+#ifndef DISABLE_TLS
+ if (sx->cctx.tls_ctx && sx->send_tlsclose) /* need to send TLS Close Notify */
+ {
+# ifdef EXIM_TCP_CORK /* Use _CORK to get Close Notify in FIN segment */
+ (void) setsockopt(sx->cctx.sock, IPPROTO_TCP, EXIM_TCP_CORK, US &on, sizeof(on));
+# endif
+ tls_shutdown_wr(sx->cctx.tls_ctx);
+ sx->send_tlsclose = FALSE; /* avoid later repeat */
+ }
+#endif
+ HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" SMTP(shutdown)>>\n");
+ shutdown(sx->cctx.sock, SHUT_WR); /* flush output buffer, with TCP FIN */
+ }
+ }
+
+ if (smtp_peer_options & OPTION_CHUNKING && sx->cmd_count > 1)
+ {
+ /* Reap any outstanding MAIL & RCPT commands, but not a DATA-go-ahead */
+ switch(sync_responses(sx, sx->cmd_count-1, 0))
+ {
+ case 3: sx->ok = TRUE; /* 2xx & 5xx => OK & progress made */
+ case 2: sx->completed_addr = TRUE; /* 5xx (only) => progress made */
+ break;
+
+ case 1: sx->ok = TRUE; /* 2xx (only) => OK, but if LMTP, */
+ if (!sx->lmtp) sx->completed_addr = 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 */
+
+#ifndef DISABLE_PIPE_CONNECT
+ case -5: /* TLS first-read error */
+ case -4: HDEBUG(D_transport)
+ debug_printf("failed reaping pipelined cmd responses\n");
+#endif
+ default: goto RESPONSE_FAILED; /* I/O error, or any MAIL/DATA error */
+ }
+ }
+
+#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(sx->prdr_active)
+ {
+ sx->ok = smtp_read_response(sx, sx->buffer, sizeof(sx->buffer), '3', ob->final_timeout);
+ if (!sx->ok && errno == 0) switch(sx->buffer[0])
+ {
+ case '2': sx->prdr_active = FALSE;
+ sx->ok = TRUE;
+ break;
+ case '4': errno = ERRNO_DATA4XX;
+ addrlist->more_errno |=
+ ((sx->buffer[1] - '0')*10 + sx->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 (!sx->lmtp)
+ {
+ sx->ok = smtp_read_response(sx, sx->buffer, sizeof(sx->buffer), '2',
+ ob->final_timeout);
+ if (!sx->ok && errno == 0 && sx->buffer[0] == '4')
+ {
+ errno = ERRNO_DATA4XX;
+ addrlist->more_errno |= ((sx->buffer[1] - '0')*10 + sx->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 (sx->ok)
+ {
+ int flag = '=';
+ struct timeval delivery_time;
+ int len;
+ uschar * conf = NULL;
+
+ timesince(&delivery_time, &sx->delivery_start);
+ sx->send_rset = FALSE;
+ pipelining_active = FALSE;
+
+ /* Set up confirmation if needed - applies only to SMTP */
+
+ if (
+#ifdef DISABLE_EVENT
+ LOGGING(smtp_confirmation) &&
+#endif
+ !sx->lmtp
+ )
+ {
+ const uschar * s = string_printing(sx->buffer);
+ /* deconst cast ok here as string_printing was checked to have alloc'n'copied */
+ conf = s == sx->buffer ? US string_copy(s) : US s;
+ }
+
+ /* Process all transported addresses - for LMTP or PRDR, read a status for
+ each one. We used to drop out at first_addr, until someone returned a 452
+ followed by a 250... and we screwed up the accepted addresses. */
+
+ for (address_item * addr = addrlist; 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 (sx->lmtp || sx->prdr_active)
+#else
+ if (sx->lmtp)
+#endif
+ {
+ if (!smtp_read_response(sx, sx->buffer, sizeof(sx->buffer), '2',
+ ob->final_timeout))
+ {
+ if (errno != 0 || sx->buffer[0] == 0) goto RESPONSE_FAILED;
+ addr->message = string_sprintf(
+#ifndef DISABLE_PRDR
+ "%s error after %s: %s", sx->prdr_active ? "PRDR":"LMTP",
+#else
+ "LMTP error after %s: %s",
+#endif
+ data_command, string_printing(sx->buffer));
+ setflag(addr, af_pass_message); /* Allow message to go to user */
+ if (sx->buffer[0] == '5')
+ addr->transport_return = FAIL;
+ else
+ {
+ errno = ERRNO_DATA4XX;
+ addr->more_errno |= ((sx->buffer[1] - '0')*10 + sx->buffer[2] - '0') << 8;
+ addr->transport_return = DEFER;
+#ifndef DISABLE_PRDR
+ if (!sx->prdr_active)
+#endif
+ retry_add_item(addr, addr->address_retry_key, 0);
+ }
+ continue;
+ }
+ sx->completed_addr = TRUE; /* NOW we can set this flag */
+ if (LOGGING(smtp_confirmation))
+ {
+ const uschar *s = string_printing(sx->buffer);
+ /* deconst cast ok here as string_printing was checked to have alloc'n'copied */
+ conf = (s == sx->buffer) ? US string_copy(s) : US s;
+ }
+ }
+
+ /* SMTP, or success return from LMTP for this address. Pass back the
+ actual host that was used. */
+
+ addr->transport_return = OK;
+ addr->host_used = host;
+ addr->delivery_time = delivery_time;
+ addr->special_action = flag;
+ addr->message = conf;
+
+ if (tcp_out_fastopen)
+ {
+ setflag(addr, af_tcp_fastopen_conn);
+ if (tcp_out_fastopen >= TFO_USED_NODATA) setflag(addr, af_tcp_fastopen);
+ if (tcp_out_fastopen >= TFO_USED_DATA) setflag(addr, af_tcp_fastopen_data);
+ }
+ if (sx->pipelining_used) setflag(addr, af_pipelining);
+#ifndef DISABLE_PIPE_CONNECT
+ if (sx->early_pipe_active) setflag(addr, af_early_pipe);
+#endif
+#ifndef DISABLE_PRDR
+ if (sx->prdr_active) setflag(addr, af_prdr_used);
+#endif
+ if (smtp_peer_options & OPTION_CHUNKING) setflag(addr, af_chunking_used);
+ flag = '-';
+
+#ifndef DISABLE_PRDR
+ if (!sx->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 sx->buffer, "%.500s/%s\n", addr->unique + 3, tblock->name);
+ else
+ sprintf(CS sx->buffer, "%.500s\n", addr->unique);
+
+ DEBUG(D_deliver) debug_printf("S:journalling %s", sx->buffer);
+ len = Ustrlen(CS sx->buffer);
+ if (write(journal_fd, sx->buffer, len) != len)
+ log_write(0, LOG_MAIN|LOG_PANIC, "failed to write journal for "
+ "%s: %s", sx->buffer, strerror(errno));
+ }
+ }
+
+#ifndef DISABLE_PRDR
+ if (sx->prdr_active)
+ {
+ const uschar * overall_message;
+
+ /* PRDR - get the final, overall response. For any non-success
+ upgrade all the address statuses. */
+
+ sx->ok = smtp_read_response(sx, sx->buffer, sizeof(sx->buffer), '2',
+ ob->final_timeout);
+ if (!sx->ok)
+ {
+ if(errno == 0 && sx->buffer[0] == '4')
+ {
+ errno = ERRNO_DATA4XX;
+ addrlist->more_errno |= ((sx->buffer[1] - '0')*10 + sx->buffer[2] - '0') << 8;
+ }
+ for (address_item * addr = addrlist; addr != sx->first_addr; addr = addr->next)
+ if (sx->buffer[0] == '5' || addr->transport_return == OK)
+ addr->transport_return = PENDING_OK; /* allow set_errno action */
+ goto RESPONSE_FAILED;
+ }
+
+ /* Append the overall response to the individual PRDR response for logging
+ and update the journal, or setup retry. */
+
+ overall_message = string_printing(sx->buffer);
+ for (address_item * addr = addrlist; addr != sx->first_addr; addr = addr->next)
+ if (addr->transport_return == OK)
+ addr->message = string_sprintf("%s\\n%s", addr->message, overall_message);
+
+ for (address_item * addr = addrlist; addr != sx->first_addr; addr = addr->next)
+ if (addr->transport_return == OK)
+ {
+ if (testflag(addr, af_homonym))
+ sprintf(CS sx->buffer, "%.500s/%s\n", addr->unique + 3, tblock->name);
+ else
+ sprintf(CS sx->buffer, "%.500s\n", addr->unique);
+
+ DEBUG(D_deliver) debug_printf("journalling(PRDR) %s\n", sx->buffer);
+ len = Ustrlen(CS sx->buffer);
+ if (write(journal_fd, sx->buffer, len) != len)
+ log_write(0, LOG_MAIN|LOG_PANIC, "failed to write journal for "
+ "%s: %s", sx->buffer, strerror(errno));
+ }
+ else if (addr->transport_return == DEFER)
+ /*XXX magic value -2 ? maybe host+message ? */
+ 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 (!sx->ok)
+ {
+ int code, set_rc;
+ uschar * set_message;
+
+ RESPONSE_FAILED:
+ {
+ save_errno = errno;
+ message = NULL;
+ /* Clear send_quit flag if needed. Do not set. */
+ sx->send_quit &= check_response(host, &save_errno, addrlist->more_errno,
+ sx->buffer, &code, &message, &pass_message);
+ goto FAILED;
+ }
+
+ SEND_FAILED:
+ {
+ save_errno = errno;
+ code = '4';
+ message = string_sprintf("smtp send to %s [%s] failed: %s",
+ host->name, host->address, message ? message : US strerror(save_errno));
+ sx->send_quit = FALSE;
+ goto FAILED;
+ }
+
+ FAILED:
+ {
+ BOOL message_error;
+
+ sx->ok = FALSE; /* For when reached by GOTO */
+ set_message = message;
+
+ /* 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. */
+
+ 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;
+
+#ifndef DISABLE_DKIM
+ case EACCES:
+ /* DKIM signing failure: avoid thinking we pipelined quit,
+ just abandon the message and close the socket. */
+
+ message_error = FALSE;
+# ifndef DISABLE_TLS
+ if (sx->cctx.tls_ctx)
+ {
+ tls_close(sx->cctx.tls_ctx,
+ sx->send_tlsclose ? TLS_SHUTDOWN_WAIT : TLS_SHUTDOWN_WONLY);
+ sx->cctx.tls_ctx = NULL;
+ }
+# endif
+ break;
+#endif
+ 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 "."
+ (d) utf8 support required and not offered
+
+ 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 */
+
+ /* If there's an errno, the message contains just the identity of
+ the host. */
+
+ if (code == '5')
+ set_rc = FAIL;
+ else /* Anything other than 5 is treated as temporary */
+ {
+ set_rc = DEFER;
+ if (save_errno > 0)
+ message = US string_sprintf("%s: %s", message, strerror(save_errno));
+
+ write_logs(host, message, sx->first_addr ? sx->first_addr->basic_errno : 0);
+
+ *message_defer = TRUE;
+ }
+#ifdef TIOCOUTQ
+ DEBUG(D_transport) if (sx->cctx.sock >= 0)
+ {
+ int n;
+ if (ioctl(sx->cctx.sock, TIOCOUTQ, &n) == 0)
+ debug_printf("%d bytes remain in socket output buffer\n", n);
+ }
+#endif
+ }
+ /* 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
+ {
+#ifndef DISABLE_PIPE_CONNECT
+ /* If we were early-pipelinng and the actual EHLO response did not match
+ the cached value we assumed, we could have detected it and passed a
+ custom errno through to here. It would be nice to RSET and retry right
+ away, but to reliably do that we eould need an extra synch point before
+ we committed to data and that would discard half the gained roundrips.
+ Or we could summarily drop the TCP connection. but that is also ugly.
+ Instead, we ignore the possibility (having freshened the cache) and rely
+ on the server telling us with a nonmessage error if we have tried to
+ do something it no longer supports. */
+#endif
+ set_rc = DEFER;
+ yield = (save_errno == ERRNO_CHHEADER_FAIL ||
+ save_errno == ERRNO_FILTER_FAIL) ? ERROR : DEFER;
+ }
+ }
+
+ set_errno(addrlist, save_errno, set_message, set_rc, pass_message, host,
+#ifdef EXPERIMENTAL_DSN_INFO
+ sx->smtp_greeting, sx->helo_response,
+#endif
+ &sx->delivery_start);
+ }
+
+/* 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 transferred.
+
+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", sx->ok, sx->send_quit,
+ sx->send_rset, f.continue_more, yield, sx->first_addr ? "not " : "");
+
+if (sx->completed_addr && sx->ok && sx->send_quit)
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ if (mail_limit = continue_sequence >= sx->max_mail)
+ {
+ DEBUG(D_transport)
+ debug_printf("reached limit %u for MAILs per conn\n", sx->max_mail);
+ }
+ else
+#endif
+ {
+ smtp_compare_t t_compare =
+ {.tblock = tblock, .current_sender_address = sender_address};
+
+ if ( sx->first_addr /* more addrs for this message */
+ || f.continue_more /* more addrs for continued-host */
+ || tcw_done && tcw /* more messages for host */
+ || (
+#ifndef DISABLE_TLS
+ ( tls_out.active.sock < 0 && !continue_proxy_cipher
+ || verify_check_given_host(CUSS &ob->hosts_nopass_tls, host) != OK
+ )
+ &&
+#endif
+ transport_check_waiting(tblock->name, host->name,
+ sx->max_mail, new_message_id,
+ (oicf)smtp_are_same_identities, (void*)&t_compare)
+ ) )
+ {
+ uschar *msg;
+ BOOL pass_message;
+
+ if (sx->send_rset)
+ if (! (sx->ok = smtp_write_command(sx, SCMD_FLUSH, "RSET\r\n") >= 0))
+ {
+ msg = US string_sprintf("smtp send to %s [%s] failed: %s", host->name,
+ host->address, strerror(errno));
+ sx->send_quit = FALSE;
+ }
+ else if (! (sx->ok = smtp_read_response(sx, sx->buffer, sizeof(sx->buffer),
+ '2', ob->command_timeout)))
+ {
+ int code;
+ sx->send_quit = check_response(host, &errno, 0, sx->buffer, &code, &msg,
+ &pass_message);
+ if (!sx->send_quit)
+ {
+ DEBUG(D_transport) debug_printf("H=%s [%s] %s\n",
+ host->name, host->address, msg);
+ }
+ }
+
+ /* Either RSET was not needed, or it succeeded */
+
+ if (sx->ok)
+ {
+#ifndef DISABLE_TLS
+ int pfd[2];
+#endif
+ int socket_fd = sx->cctx.sock;
+
+ if (sx->first_addr) /* More addresses still to be sent */
+ { /* for this message */
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ /* Any that we marked as skipped, reset to do now */
+ for (address_item * a = sx->first_addr; a; a = a->next)
+ if (a->transport_return == SKIP)
+ a->transport_return = PENDING_DEFER;
+#endif
+ continue_sequence++; /* for consistency */
+ clearflag(sx->first_addr, af_new_conn);
+ setflag(sx->first_addr, af_cont_conn); /* Causes * in logging */
+ pipelining_active = sx->pipelining_used; /* was cleared at DATA */
+ goto SEND_MESSAGE;
+ }
+
+ /* Unless caller said it already has more messages listed for this host,
+ pass the connection on to a new Exim process (below, the call to
+ transport_pass_socket). If the caller has more ready, just return with
+ the connection still open. */
+
+#ifndef DISABLE_TLS
+ if (tls_out.active.sock >= 0)
+ if ( f.continue_more
+ || verify_check_given_host(CUSS &ob->hosts_noproxy_tls, host) == OK)
+ {
+ /* Before passing the socket on, or returning to caller with it still
+ open, 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. */
+
+ tls_close(sx->cctx.tls_ctx,
+ sx->send_tlsclose ? TLS_SHUTDOWN_WAIT : TLS_SHUTDOWN_WONLY);
+ sx->send_tlsclose = FALSE;
+ sx->cctx.tls_ctx = NULL;
+ tls_out.active.sock = -1;
+ smtp_peer_options = smtp_peer_options_wrap;
+ sx->ok = !sx->smtps
+ && smtp_write_command(sx, SCMD_FLUSH, "EHLO %s\r\n", sx->helo_data)
+ >= 0
+ && smtp_read_response(sx, sx->buffer, sizeof(sx->buffer),
+ '2', ob->command_timeout);
+
+ if (sx->ok && f.continue_more)
+ goto TIDYUP; /* More addresses for another run */
+ }
+ else
+ {
+ /* Set up a pipe for proxying TLS for the new transport process */
+
+ smtp_peer_options |= OPTION_TLS;
+ if ((sx->ok = socketpair(AF_UNIX, SOCK_STREAM, 0, pfd) == 0))
+ socket_fd = pfd[1];
+ else
+ set_errno(sx->first_addr, errno, US"internal allocation problem",
+ DEFER, FALSE, host,
+# ifdef EXPERIMENTAL_DSN_INFO
+ sx->smtp_greeting, sx->helo_response,
+# endif
+ &sx->delivery_start);
+ }
+ else
+#endif
+ if (f.continue_more)
+ goto TIDYUP; /* More addresses for another run */
+
+ /* If the socket is successfully passed, we mustn't send QUIT (or
+ indeed anything!) from here. */
+
+ /*XXX DSN_INFO: assume likely to do new HELO; but for greet we'll want to
+ propagate it from the initial
+ */
+ if (sx->ok && transport_pass_socket(tblock->name, host->name,
+ host->address, new_message_id, socket_fd
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ , sx->peer_limit_mail, sx->peer_limit_rcpt, sx->peer_limit_rcptdom
+#endif
+ ))
+ {
+ sx->send_quit = FALSE;
+
+ /* We have passed the client socket to a fresh transport process.
+ If TLS is still active, we need to proxy it for the transport we
+ just passed the baton to. Fork a child to to do it, and return to
+ get logging done asap. Which way to place the work makes assumptions
+ about post-fork prioritisation which may not hold on all platforms. */
+#ifndef DISABLE_TLS
+ if (tls_out.active.sock >= 0)
+ {
+ int pid = exim_fork(US"tls-proxy-interproc");
+ if (pid == 0) /* child; fork again to disconnect totally */
+ {
+ /* does not return */
+ smtp_proxy_tls(sx->cctx.tls_ctx, sx->buffer, sizeof(sx->buffer), pfd,
+ ob->command_timeout, host->name);
+ }
+
+ if (pid > 0) /* parent */
+ {
+ close(pfd[0]);
+ /* tidy the inter-proc to disconn the proxy proc */
+ waitpid(pid, NULL, 0);
+ tls_close(sx->cctx.tls_ctx, TLS_NO_SHUTDOWN);
+ sx->cctx.tls_ctx = NULL;
+ (void)close(sx->cctx.sock);
+ sx->cctx.sock = -1;
+ continue_transport = NULL;
+ continue_hostname = NULL;
+ goto TIDYUP;
+ }
+ log_write(0, LOG_PANIC_DIE, "fork failed");
+ }
+#endif
+ }
+ }
+
+ /* If RSET failed and there are addresses left, they get deferred. */
+ else
+ set_errno(sx->first_addr, errno, msg, DEFER, FALSE, host,
+#ifdef EXPERIMENTAL_DSN_INFO
+ sx->smtp_greeting, sx->helo_response,
+#endif
+ &sx->delivery_start);
+ }
+ }
+
+/* End off tidily with QUIT unless the connection has died or the socket has
+been passed to another process. */
+
+SEND_QUIT:
+if (sx->send_quit)
+ { /* Use _MORE to get QUIT in FIN segment */
+ (void)smtp_write_command(sx, SCMD_MORE, "QUIT\r\n");
+#ifndef DISABLE_TLS
+ if (sx->cctx.tls_ctx && sx->send_tlsclose)
+ {
+# ifdef EXIM_TCP_CORK /* Use _CORK to get TLS Close Notify in FIN segment */
+ (void) setsockopt(sx->cctx.sock, IPPROTO_TCP, EXIM_TCP_CORK, US &on, sizeof(on));
+# endif
+ tls_shutdown_wr(sx->cctx.tls_ctx);
+ sx->send_tlsclose = FALSE;
+ }
+#endif
+ }
+
+END_OFF:
+
+/* 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. */
+
+if (sx->send_quit)
+ {
+ /* This flushes data queued in the socket, being the QUIT and any TLS Close,
+ sending them along with the client FIN flag. Us (we hope) sending FIN first
+ means we (client) take the TIME_WAIT state, so the server (which likely has a
+ higher connection rate) does not have to. */
+
+ HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" SMTP(shutdown)>>\n");
+ shutdown(sx->cctx.sock, SHUT_WR);
+ }
+
+if (sx->send_quit || tcw_done && !tcw)
+ {
+ /* Wait for (we hope) ack of our QUIT, and a server FIN. Discard any data
+ received, then discard the socket. Any packet received after then, or receive
+ data still in the socket, will get a RST - hence the pause/drain. */
+
+ /* Reap the response to QUIT, timing out after one second */
+ (void) smtp_read_response(sx, sx->buffer, sizeof(sx->buffer), '2', 1);
+#ifndef DISABLE_TLS
+ if (sx->cctx.tls_ctx)
+ {
+ int n;
+
+ /* Reap the TLS Close Notify from the server, timing out after one second */
+ sigalrm_seen = FALSE;
+ ALARM(1);
+ do
+ n = tls_read(sx->cctx.tls_ctx, sx->inbuffer, sizeof(sx->inbuffer));
+ while (!sigalrm_seen && n > 0);
+ ALARM_CLR(0);
+
+ if (sx->send_tlsclose)
+ {
+# ifdef EXIM_TCP_CORK
+ (void) setsockopt(sx->cctx.sock, IPPROTO_TCP, EXIM_TCP_CORK, US &on, sizeof(on));
+# endif
+ tls_close(sx->cctx.tls_ctx, TLS_SHUTDOWN_WAIT);
+ }
+ else
+ tls_close(sx->cctx.tls_ctx, TLS_SHUTDOWN_WONLY);
+ sx->cctx.tls_ctx = NULL;
+ }
+#endif
+
+ /* Drain any trailing data from the socket before close, to avoid sending a RST */
+
+ if ( poll_one_fd(sx->cctx.sock, POLLIN, 20) != 0 /* 20ms */
+ && fcntl(sx->cctx.sock, F_SETFL, O_NONBLOCK) == 0)
+ for (int i = 16, n; /* drain socket */
+ (n = read(sx->cctx.sock, sx->inbuffer, sizeof(sx->inbuffer))) > 0 && i > 0;
+ i--) HDEBUG(D_transport|D_acl|D_v)
+ {
+ int m = MIN(n, 64);
+ debug_printf_indent(" SMTP(drain %d bytes)<< %.*s\n", n, m, sx->inbuffer);
+ for (m = 0; m < n; m++)
+ debug_printf("0x%02x\n", sx->inbuffer[m]);
+ }
+ }
+HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" SMTP(close)>>\n");
+(void)close(sx->cctx.sock);
+sx->cctx.sock = -1;
+continue_transport = NULL;
+continue_hostname = NULL;
+smtp_debug_cmd_report();
+
+#ifndef DISABLE_EVENT
+(void) event_raise(tblock->event_action, US"tcp:close", NULL, NULL);
+#endif
+
+#ifdef SUPPORT_DANE
+if (dane_held)
+ {
+ sx->first_addr = NULL;
+ for (address_item * a = sx->addrlist->next; a; a = a->next)
+ if (a->transport_return == DANE)
+ {
+ a->transport_return = PENDING_DEFER;
+ if (!sx->first_addr)
+ {
+ /* Remember the new start-point in the addrlist, for smtp_setup_conn()
+ to get the domain string for SNI */
+
+ sx->first_addr = a;
+ clearflag(a, af_cont_conn);
+ setflag(a, af_new_conn); /* clear * from logging */
+ DEBUG(D_transport) debug_printf("DANE: go-around for %s\n", a->domain);
+ }
+ }
+ continue_sequence = 1; /* for consistency */
+ goto REPEAT_CONN;
+ }
+#endif
+
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+if (mail_limit && sx->first_addr)
+ {
+ /* Reset the sequence count since we closed the connection. This is flagged
+ on the pipe back to the delivery process so that a non-continued-conn delivery
+ is logged. */
+
+ continue_sequence = 1; /* for consistency */
+ clearflag(sx->first_addr, af_cont_conn);
+ setflag(sx->first_addr, af_new_conn); /* clear * from logging */
+ goto REPEAT_CONN;
+ }
+#endif
+
+return yield;
+
+TIDYUP:
+#ifdef SUPPORT_DANE
+if (dane_held) for (address_item * a = sx->addrlist->next; a; a = a->next)
+ if (a->transport_return == DANE)
+ a->transport_return = PENDING_DEFER;
+#endif
+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 = SOB tblock->options_block;
+client_conn_ctx cctx;
+smtp_context sx;
+uschar buffer[256];
+uschar inbuffer[4096];
+uschar outbuffer[16];
+
+/*XXX really we need an active-smtp-client ctx, rather than assuming stdout */
+cctx.sock = fileno(stdin);
+cctx.tls_ctx = cctx.sock == tls_out.active.sock ? tls_out.active.tls_ctx : NULL;
+
+sx.inblock.cctx = &cctx;
+sx.inblock.buffer = inbuffer;
+sx.inblock.buffersize = sizeof(inbuffer);
+sx.inblock.ptr = inbuffer;
+sx.inblock.ptrend = inbuffer;
+
+sx.outblock.cctx = &cctx;
+sx.outblock.buffersize = sizeof(outbuffer);
+sx.outblock.buffer = outbuffer;
+sx.outblock.ptr = outbuffer;
+sx.outblock.cmd_count = 0;
+sx.outblock.authenticating = FALSE;
+
+(void)smtp_write_command(&sx, SCMD_FLUSH, "QUIT\r\n");
+(void)smtp_read_response(&sx, buffer, sizeof(buffer), '2', ob->command_timeout);
+(void)close(cctx.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;
+for (address_item * addr = addrlist; addr; addr = addr->next)
+ if (addr->transport_return == DEFER)
+ {
+ if (!first_addr) first_addr = addr;
+ addr->transport_return = PENDING_DEFER;
+ addr->basic_errno = 0;
+ addr->more_errno = (host->mx >= 0)? 'M' : 'A';
+ addr->message = NULL;
+#ifndef DISABLE_TLS
+ addr->cipher = NULL;
+ addr->ourcert = NULL;
+ addr->peercert = NULL;
+ addr->peerdn = NULL;
+ addr->ocsp = OCSP_NOT_REQ;
+ addr->tlsver = NULL;
+#endif
+#ifdef EXPERIMENTAL_DSN_INFO
+ addr->smtp_greeting = NULL;
+ addr->helo_response = NULL;
+#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 defport;
+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;
+BOOL expired = TRUE;
+uschar *expanded_hosts = NULL;
+uschar *pistring;
+uschar *tid = string_sprintf("%s transport", tblock->name);
+smtp_transport_options_block *ob = SOB 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 (address_item * addr = addrlist; addr; addr = addr->next)
+ debug_printf(" %s\n", addr->address);
+ if (hostlist)
+ {
+ debug_printf("hostlist:\n");
+ for (host_item * host = hostlist; host; host = host->next)
+ debug_printf(" '%s' IP %s port %d\n", host->name, host->address, host->port);
+ }
+ if (continue_hostname)
+ debug_printf("already connected to %s [%s] (on fd %d)\n",
+ continue_hostname, continue_host_address,
+ cutthrough.cctx.sock >= 0 ? cutthrough.cctx.sock : 0);
+ }
+
+/* Check the restrictions on line length */
+
+if (max_received_linelength > ob->message_linelength_limit)
+ {
+ struct timeval now;
+ gettimeofday(&now, NULL);
+
+ for (address_item * addr = addrlist; addr; addr = addr->next)
+ if (addr->transport_return == DEFER)
+ addr->transport_return = PENDING_DEFER;
+
+ set_errno_nohost(addrlist, ERRNO_SMTPFORMAT,
+ US"message has lines too long for transport", FAIL, TRUE, &now);
+ goto END_TRANSPORT;
+ }
+
+/* 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 || (ob->hosts_override && ob->hosts))
+ {
+ if (!ob->hosts)
+ {
+ 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)
+ {
+ uschar *s = ob->hosts;
+
+ if (Ustrchr(s, '$'))
+ {
+ if (!(expanded_hosts = expand_string(s)))
+ {
+ addrlist->message = string_sprintf("failed to expand list of hosts "
+ "\"%s\" in %s transport: %s", s, tblock->name, expand_string_message);
+ addrlist->transport_return = f.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);
+
+ if (is_tainted(s))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "attempt to use tainted host list '%s' from '%s' in transport %s",
+ s, ob->hosts, tblock->name);
+ /* Avoid leaking info to an attacker */
+ addrlist->message = US"internal configuration error";
+ addrlist->transport_return = PANIC;
+ return FALSE;
+ }
+
+ host_build_hostlist(&hostlist, s, ob->hosts_randomize);
+
+ /* Check that the expansion yielded something useful. */
+ if (!hostlist)
+ {
+ 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) 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 && !continue_hostname)
+ {
+ host_item *newlist = NULL;
+ while (hostlist)
+ {
+ host_item *h = hostlist;
+ hostlist = hostlist->next;
+
+ h->sort_key = random_number(100);
+
+ if (!newlist)
+ {
+ 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)
+ {
+ 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, &defport, 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 (int cutoff_retry = 0;
+ expired && cutoff_retry < (ob->delay_after_cutoff ? 1 : 2);
+ cutoff_retry++)
+ {
+ host_item *nexthost = NULL;
+ int unexpired_hosts_tried = 0;
+ BOOL continue_host_tried = FALSE;
+
+retry_non_continued:
+ for (host = hostlist;
+ host
+ && unexpired_hosts_tried < ob->hosts_max_try
+ && total_hosts_tried < ob->hosts_max_try_hardlimit;
+ host = nexthost)
+ {
+ int rc;
+ int host_af;
+ BOOL host_is_expired = FALSE;
+ BOOL message_defer = 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)
+ {
+ int new_port, flags;
+
+ 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 | HOST_FIND_BY_AAAA;
+ 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, NULL, TRUE);
+ else
+ rc = host_find_bydns(host, NULL, flags, NULL, NULL, NULL,
+ &ob->dnssec, /* domains for request/require */
+ NULL, NULL);
+
+ /* Update the host (and any additional blocks, resulting from
+ multihoming) with a host-specific port, if any. */
+
+ for (host_item * 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_SECURITY || 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 (address_item * addr = addrlist; addr; addr = addr->next)
+ {
+ if (addr->transport_return != DEFER) continue;
+ addr->basic_errno = ERRNO_UNKNOWNHOST;
+ addr->message = string_sprintf(
+ rc == HOST_FIND_SECURITY
+ ? "lookup of IP address for %s was insecure"
+ : "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 (address_item * addr = addrlist; addr; addr = addr->next)
+ {
+ addr->basic_errno = ERRNO_HOST_IS_LOCAL;
+ 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 (continue_hostname)
+ if ( Ustrcmp(continue_hostname, host->name) != 0
+ || Ustrcmp(continue_host_address, host->address) != 0
+ )
+ {
+ expired = FALSE;
+ continue; /* With next host */
+ }
+ else
+ continue_host_tried = TRUE;
+
+ /* 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 ( (!f.deliver_force || f.queue_2stage)
+ && ( f.queue_smtp
+ || match_isinlist(addrlist->domain,
+ CUSS &queue_smtp_domains, 0,
+ &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL) == OK)
+ )
+ {
+ DEBUG(D_transport) debug_printf("first-pass routing only\n");
+ expired = FALSE;
+ for (address_item * addr = addrlist; addr; addr = addr->next)
+ if (addr->transport_return == DEFER)
+ addr->message = US"first-pass only routing due to -odqs, "
+ "queue_smtp_domains or control=queue";
+ 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
+ ? defport : host->port);
+ if (Ustrcmp(pistring, ":25") == 0) pistring = US"";
+
+ /* Select IPv4 or IPv6, and choose an outgoing interface. If the interface
+ string is set, even if constant (as different transports can have different
+ constant settings), 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, ':') ? AF_INET6 : AF_INET;
+ {
+ uschar * s = ob->interface;
+ if (s && *s)
+ {
+ if (!smtp_get_interface(s, host_af, addrlist, &interface, tid))
+ return FALSE;
+ 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)
+ {
+ BOOL incl_ip;
+ /* Ensure the status of the address is set by checking retry data if
+ necessary. There may be 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. */
+
+ if (exp_bool(addrlist, US"transport", tblock->name, D_transport,
+ US"retry_include_ip_address", ob->retry_include_ip_address,
+ ob->expand_retry_include_ip_address, &incl_ip) != OK)
+ continue; /* with next host */
+
+ host_is_expired = retry_check_address(addrlist->domain, host, pistring,
+ incl_ip, &retry_host_key, &retry_message_key);
+
+ DEBUG(D_transport) debug_printf("%s [%s]%s retry-status = %s\n", host->name,
+ host->address ? host->address : US"", 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_insecure:
+ 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) 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
+ || host->status != hstatus_unusable_expired
+ || host->last_try > received_time.tv_sec)
+ 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 ( !continue_hostname
+ && verify_check_given_host(CUSS &ob->serialize_hosts, host) == OK)
+ {
+ serialize_key = string_sprintf("host-serialize-%s", host->name);
+ if (!enq_start(serialize_key, 1))
+ {
+ DEBUG(D_transport)
+ debug_printf("skipping host %s because another Exim process "
+ "is connected to it\n", host->name);
+ hosts_serial++;
+ continue;
+ }
+ }
+
+ /* 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 ? ", ..." : "");
+
+ set_process_info("delivering %s to %s [%s]%s (%s%s)",
+ message_id, host->name, host->address, pistring, addrlist->address,
+ addrlist->next ? ", ..." : "");
+
+ /* This is not for real; don't do the delivery. If there are
+ any remaining hosts, list them. */
+
+ if (f.dont_deliver)
+ {
+ struct timeval now;
+ gettimeofday(&now, NULL);
+ set_errno_nohost(addrlist, 0, NULL, OK, FALSE, &now);
+ for (address_item * addr = addrlist; addr; 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 (host_item * host2 = host; host2; host2 = host2->next)
+ debug_printf(" %s [%s]\n", host2->name,
+ host2->address ? host2->address : US"unset");
+ }
+ 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
+ {
+ host_item * thost;
+ /* Make a copy of the host if it is local to this invocation
+ of the transport. */
+
+ if (expanded_hosts)
+ {
+ thost = store_get(sizeof(host_item), GET_UNTAINTED);
+ *thost = *host;
+ thost->name = string_copy(host->name);
+ thost->address = string_copy(host->address);
+ }
+ else
+ thost = host;
+
+ if (!host_is_expired && ++unexpired_hosts_tried >= ob->hosts_max_try)
+ {
+ DEBUG(D_transport)
+ debug_printf("hosts_max_try limit reached with this host\n");
+ for (host_item * h = host; h; h = h->next) if (h->mx != host->mx)
+ {
+ nexthost = h;
+ unexpired_hosts_tried--;
+ DEBUG(D_transport) debug_printf("however, a higher MX host exists "
+ "and will be tried\n");
+ break;
+ }
+ }
+
+ /* Attempt the delivery. */
+
+ total_hosts_tried++;
+ rc = smtp_deliver(addrlist, thost, host_af, defport, interface, tblock,
+ &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(host, first_addr->message, first_addr->basic_errno);
+
+#ifndef DISABLE_EVENT
+ if (rc == DEFER)
+ deferred_event_raise(first_addr, host, US"msg:host:defer");
+#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().] */
+
+#ifndef DISABLE_TLS
+ if ( rc == DEFER
+ && first_addr->basic_errno == ERRNO_TLSFAILURE
+ && ob->tls_tempfail_tryclear
+ && verify_check_given_host(CUSS &ob->hosts_require_tls, host) != OK
+ )
+ {
+ log_write(0, LOG_MAIN,
+ "%s: delivering unencrypted to H=%s [%s] (not in hosts_require_tls)",
+ first_addr->message, host->name, host->address);
+ first_addr = prepare_addresses(addrlist, host);
+ rc = smtp_deliver(addrlist, thost, host_af, defport, interface, tblock,
+ &message_defer, TRUE);
+ if (rc == DEFER && first_addr->basic_errno != ERRNO_AUTHFAIL)
+ write_logs(host, first_addr->message, first_addr->basic_errno);
+# ifndef DISABLE_EVENT
+ if (rc == DEFER)
+ deferred_event_raise(first_addr, host, US"msg:host:defer");
+# endif
+ }
+#endif /*DISABLE_TLS*/
+
+#ifndef DISABLE_EVENT
+ /* If the last host gave a defer raise a per-message event */
+
+ if ( !( nexthost
+ && unexpired_hosts_tried < ob->hosts_max_try
+ && total_hosts_tried < ob->hosts_max_try_hardlimit
+ )
+ && (message_defer || rc == DEFER)
+ )
+ deferred_event_raise(first_addr, host, US"msg:defer");
+#endif
+ }
+
+ /* Delivery attempt finished */
+
+ set_process_info("delivering %s: just tried %s [%s]%s for %s%s: result %s",
+ message_id, host->name, host->address, pistring, addrlist->address,
+ addrlist->next ? " (& others)" : "", rc_to_string(rc));
+
+ /* Release serialization if set up */
+
+ if (serialize_key) 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)
+ {
+ int delete_flag = rc != DEFER ? rf_delete : 0;
+ if (!retry_host_key)
+ {
+ BOOL incl_ip;
+ if (exp_bool(addrlist, US"transport", tblock->name, D_transport,
+ US"retry_include_ip_address", ob->retry_include_ip_address,
+ ob->expand_retry_include_ip_address, &incl_ip) != OK)
+ incl_ip = TRUE; /* error; use most-specific retry record */
+
+ retry_host_key = incl_ip
+ ? 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 || !continue_hostname)
+ 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)
+ {
+ int delete_flag = message_defer ? 0 : rf_delete;
+ if (!retry_message_key)
+ {
+ BOOL incl_ip;
+ if (exp_bool(addrlist, US"transport", tblock->name, D_transport,
+ US"retry_include_ip_address", ob->retry_include_ip_address,
+ ob->expand_retry_include_ip_address, &incl_ip) != OK)
+ incl_ip = TRUE; /* error; use most-specific retry record */
+
+ retry_message_key = incl_ip
+ ? 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 (address_item * addr = addrlist; addr; 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 (continue_hostname) 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)
+ {
+ BOOL timedout;
+ retry_config *retry = retry_find_config(host->name, NULL, 0, 0);
+
+ if (retry && retry->rules)
+ {
+ retry_rule *last_rule;
+ for (last_rule = retry->rules;
+ last_rule->next;
+ last_rule = last_rule->next);
+ timedout = time(NULL) - received_time.tv_sec > 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");
+ }
+ }
+
+ DEBUG(D_transport)
+ {
+ if (unexpired_hosts_tried >= ob->hosts_max_try)
+ debug_printf("reached transport hosts_max_try limit %d\n",
+ ob->hosts_max_try);
+ if (total_hosts_tried >= ob->hosts_max_try_hardlimit)
+ debug_printf("reached transport hosts_max_try_hardlimit limit %d\n",
+ ob->hosts_max_try_hardlimit);
+ }
+
+ testharness_pause_ms(500); /* let server debug out */
+ } /* End of loop for trying multiple hosts. */
+
+ /* If we failed to find a matching host in the list, for an already-open
+ connection, just close it and start over with the list. This can happen
+ for routing that changes from run to run, or big multi-IP sites with
+ round-robin DNS. */
+
+ if (continue_hostname && !continue_host_tried)
+ {
+ int fd = cutthrough.cctx.sock >= 0 ? cutthrough.cctx.sock : 0;
+
+ DEBUG(D_transport) debug_printf("no hosts match already-open connection\n");
+#ifndef DISABLE_TLS
+ /* A TLS conn could be open for a cutthrough, but not for a plain continued-
+ transport */
+/*XXX doublecheck that! */
+
+ if (cutthrough.cctx.sock >= 0 && cutthrough.is_tls)
+ {
+ (void) tls_write(cutthrough.cctx.tls_ctx, US"QUIT\r\n", 6, FALSE);
+ tls_close(cutthrough.cctx.tls_ctx, TLS_SHUTDOWN_NOWAIT);
+ cutthrough.cctx.tls_ctx = NULL;
+ cutthrough.is_tls = FALSE;
+ }
+ else
+#else
+ (void) write(fd, US"QUIT\r\n", 6);
+#endif
+ (void) close(fd);
+ cutthrough.cctx.sock = -1;
+ continue_hostname = NULL;
+ goto retry_non_continued;
+ }
+
+ /* 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 (address_item * addr = addrlist; addr; 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 (address_item * addr = addrlist; addr; 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)
+ 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 (f.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
+ )
+ {
+ addr->basic_errno = ERRNO_HRETRY;
+ if (continue_hostname)
+ 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 = string_sprintf(
+ "all hosts%s have been failing for a long time %s",
+ addr->domain ? string_sprintf(" for '%s'", addr->domain) : US"",
+ ob->delay_after_cutoff
+ ? US"(and retry time not reached)"
+ : US"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)
+ addr->transport_return = FAIL;
+ }
+ else
+ {
+ const char * s;
+ if (hosts_retry == hosts_total)
+ s = "retry time not reached for any host%s";
+ else if (hosts_fail == hosts_total)
+ s = "all host address lookups%s failed permanently";
+ else if (hosts_defer == hosts_total)
+ s = "all host address lookups%s failed temporarily";
+ else if (hosts_serial == hosts_total)
+ s = "connection limit reached for all hosts%s";
+ else if (hosts_fail+hosts_defer == hosts_total)
+ s = "all host address lookups%s failed";
+ else
+ s = "some host address lookups failed and retry time "
+ "not reached for other hosts or connection limit reached%s";
+
+ addr->message = string_sprintf(s,
+ addr->domain ? string_sprintf(" for '%s'", addr->domain) : US"");
+ }
+ }
+ }
+
+/* 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 */
+}
+
+#endif /*!MACRO_PREDEF*/
+/* 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
index 0000000..319e849
--- /dev/null
+++ b/src/transports/smtp.h
@@ -0,0 +1,252 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#define DELIVER_BUFFER_SIZE 4096
+
+#define PENDING 256
+#define PENDING_DEFER (PENDING + DEFER)
+#define PENDING_OK (PENDING + OK)
+
+
+#ifndef DISABLE_TLS
+/* Flags structure for validity of TLS configuration */
+
+typedef struct {
+ BOOL conn_certs:1; /* certificates etc. loaded */
+ BOOL cabundle:1; /* CA certificates loaded */
+ BOOL crl:1; /* CRL loaded */
+ BOOL pri_string:1; /* cipher priority-string cache loaded */
+ BOOL dh:1; /* Diffie-Helman params loaded */
+ BOOL ecdh:1; /* EC Diffie-Helman params loaded */
+
+ BOOL ca_rdn_emulate:1; /* do not advertise usable-cert list */
+ BOOL ocsp_hook:1; /* need hshake callback on session */
+
+ void * libdata0; /* library-dependent preloaded data */
+ void * libdata1; /* library-dependent preloaded data */
+} exim_tlslib_state;
+#endif
+
+
+/* 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_alpn;
+ uschar *hosts_require_auth;
+ uschar *hosts_try_chunking;
+#ifdef SUPPORT_DANE
+ uschar *hosts_try_dane;
+ uschar *hosts_require_dane;
+ uschar *dane_require_tls_ciphers;
+#endif
+ uschar *hosts_try_fastopen;
+#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;
+#ifndef DISABLE_PIPE_CONNECT
+ uschar *hosts_pipe_connect;
+#endif
+ uschar *hosts_avoid_esmtp;
+#ifndef DISABLE_TLS
+ uschar *hosts_nopass_tls;
+ uschar *hosts_noproxy_tls;
+#endif
+ int command_timeout;
+ int connect_timeout;
+ int data_timeout;
+ int final_timeout;
+ int size_addition;
+ int hosts_max_try;
+ int hosts_max_try_hardlimit;
+ int message_linelength_limit;
+ BOOL address_retry_include_sender;
+ BOOL allow_localhost;
+ BOOL authenticated_sender_force;
+ BOOL gethostbyname;
+ BOOL dns_qualify_single;
+ BOOL dns_search_parents;
+ dnssec_domains dnssec;
+ BOOL delay_after_cutoff;
+ BOOL hosts_override;
+ BOOL hosts_randomize;
+ BOOL keepalive;
+ BOOL lmtp_ignore_quota;
+ uschar *expand_retry_include_ip_address;
+ BOOL retry_include_ip_address;
+#ifdef SUPPORT_SOCKS
+ uschar *socks_proxy;
+#endif
+#ifndef DISABLE_TLS
+ uschar *tls_alpn;
+ uschar *tls_certificate;
+ uschar *tls_crl;
+ uschar *tls_privatekey;
+ uschar *tls_require_ciphers;
+# ifndef DISABLE_TLS_RESUME
+ uschar *host_name_extract;
+ uschar *tls_resumption_hosts;
+# endif
+ const 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;
+ uschar *tls_verify_cert_hostnames;
+#endif
+#ifdef SUPPORT_I18N
+ uschar *utf8_downconvert;
+#endif
+#ifndef DISABLE_DKIM
+ struct ob_dkim dkim;
+#endif
+#ifdef EXPERIMENTAL_ARC
+ uschar *arc_sign;
+#endif
+#ifndef DISABLE_TLS
+ exim_tlslib_state tls_preload;
+#endif
+} smtp_transport_options_block;
+
+#define SOB (smtp_transport_options_block *)
+
+
+/* smtp connect context */
+typedef struct {
+ uschar * from_addr;
+ address_item * addrlist;
+
+ smtp_connect_args conn_args;
+ int port;
+
+ BOOL verify:1;
+ BOOL lmtp:1;
+ BOOL smtps:1;
+ BOOL ok:1;
+ BOOL setting_up:1;
+#ifndef DISABLE_PIPE_CONNECT
+ BOOL early_pipe_ok:1;
+ BOOL early_pipe_active:1;
+#endif
+ BOOL esmtp:1;
+ BOOL esmtp_sent:1;
+ BOOL pipelining_used:1;
+#ifndef DISABLE_PRDR
+ BOOL prdr_active:1;
+#endif
+#ifdef SUPPORT_I18N
+ BOOL utf8_needed:1;
+#endif
+ BOOL dsn_all_lasthop:1;
+#if !defined(DISABLE_TLS) && defined(SUPPORT_DANE)
+ BOOL dane_required:1;
+#endif
+#ifndef DISABLE_PIPE_CONNECT
+ BOOL pending_BANNER:1;
+ BOOL pending_EHLO:1;
+#endif
+ BOOL pending_MAIL:1;
+ BOOL pending_BDAT:1;
+ BOOL RCPT_452:1;
+ BOOL good_RCPT:1;
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ BOOL single_rcpt_domain:1;
+#endif
+ BOOL completed_addr:1;
+ BOOL send_rset:1;
+ BOOL send_quit:1;
+ BOOL send_tlsclose:1;
+
+ unsigned peer_offered;
+#ifdef EXPERIMENTAL_ESMTP_LIMITS
+ unsigned peer_limit_mail;
+ unsigned peer_limit_rcpt;
+ unsigned peer_limit_rcptdom;
+#endif
+
+ unsigned max_mail;
+ int max_rcpt;
+ int cmd_count;
+
+ unsigned avoid_option;
+ uschar * igquotstr;
+ uschar * helo_data;
+#ifdef EXPERIMENTAL_DSN_INFO
+ uschar * smtp_greeting;
+ uschar * helo_response;
+#endif
+#ifndef DISABLE_PIPE_CONNECT
+ /* Info about the EHLO response stored to / retrieved from cache. When
+ operating early-pipe, we use the cached values. For each of plaintext and
+ crypted we store bitmaps for ESMTP features and AUTH methods. If the LIMITS
+ extension is built and usable them at least one of the limits values cached
+ is nonzero, and we use the values to constrain the connection. */
+ ehlo_resp_precis ehlo_resp;
+#endif
+
+ struct timeval delivery_start;
+ address_item * first_addr;
+ address_item * next_addr;
+ address_item * sync_addr;
+
+ client_conn_ctx cctx;
+ smtp_inblock inblock;
+ smtp_outblock outblock;
+ uschar buffer[DELIVER_BUFFER_SIZE];
+ uschar inbuffer[4096];
+ uschar outbuffer[4096];
+} smtp_context;
+
+extern int smtp_setup_conn(smtp_context *, BOOL);
+extern int smtp_write_mail_and_rcpt_cmds(smtp_context *, int *);
+extern int smtp_reap_early_pipe(smtp_context *, int *);
+
+
+/* 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 *);
+
+
+
+#ifdef SUPPORT_SOCKS
+extern int socks_sock_connect(host_item *, int, int, uschar *,
+ transport_instance *, int);
+#endif
+
+/* End of transports/smtp.h */
diff --git a/src/transports/smtp_socks.c b/src/transports/smtp_socks.c
new file mode 100644
index 0000000..0e58732
--- /dev/null
+++ b/src/transports/smtp_socks.c
@@ -0,0 +1,415 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/* Copyright (c) Jeremy Harris 2015 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* SOCKS version 5 proxy, client-mode */
+
+#include "../exim.h"
+#include "smtp.h"
+
+#ifdef SUPPORT_SOCKS /* entire file */
+
+#ifndef nelem
+# define nelem(arr) (sizeof(arr)/sizeof(*arr))
+#endif
+
+
+/* Defaults */
+#define SOCKS_PORT 1080
+#define SOCKS_TIMEOUT 5
+#define SOCKS_WEIGHT 1
+#define SOCKS_PRIORITY 1
+
+#define AUTH_NONE 0
+#define AUTH_NAME 2 /* user/password per RFC 1929 */
+#define AUTH_NAME_VER 1
+
+struct socks_err
+ {
+ uschar * reason;
+ int errcode;
+ } socks_errs[] =
+ {
+ {NULL, 0},
+ {US"general SOCKS server failure", EIO},
+ {US"connection not allowed by ruleset", EACCES},
+ {US"Network unreachable", ENETUNREACH},
+ {US"Host unreachable", EHOSTUNREACH},
+ {US"Connection refused", ECONNREFUSED},
+ {US"TTL expired", ECANCELED},
+ {US"Command not supported", EOPNOTSUPP},
+ {US"Address type not supported", EAFNOSUPPORT}
+ };
+
+typedef struct
+ {
+ const uschar * proxy_host;
+ uschar auth_type; /* RFC 1928 encoding */
+ const uschar * auth_name;
+ const uschar * auth_pwd;
+ short port;
+ BOOL is_failed;
+ unsigned timeout;
+ unsigned weight;
+ unsigned priority;
+ } socks_opts;
+
+static void
+socks_option_defaults(socks_opts * sob)
+{
+sob->proxy_host = NULL;
+sob->auth_type = AUTH_NONE;
+sob->auth_name = US"";
+sob->auth_pwd = US"";
+sob->is_failed = FALSE;
+sob->port = SOCKS_PORT;
+sob->timeout = SOCKS_TIMEOUT;
+sob->weight = SOCKS_WEIGHT;
+sob->priority = SOCKS_PRIORITY;
+}
+
+static void
+socks_option(socks_opts * sob, const uschar * opt)
+{
+if (Ustrncmp(opt, "auth=", 5) == 0)
+ {
+ opt += 5;
+ if (Ustrcmp(opt, "none") == 0) sob->auth_type = AUTH_NONE;
+ else if (Ustrcmp(opt, "name") == 0) sob->auth_type = AUTH_NAME;
+ }
+else if (Ustrncmp(opt, "name=", 5) == 0)
+ sob->auth_name = opt + 5;
+else if (Ustrncmp(opt, "pass=", 5) == 0)
+ sob->auth_pwd = opt + 5;
+else if (Ustrncmp(opt, "port=", 5) == 0)
+ sob->port = atoi(CCS opt + 5);
+else if (Ustrncmp(opt, "tmo=", 4) == 0)
+ sob->timeout = atoi(CCS opt + 4);
+else if (Ustrncmp(opt, "pri=", 4) == 0)
+ sob->priority = atoi(CCS opt + 4);
+else if (Ustrncmp(opt, "weight=", 7) == 0)
+ sob->weight = atoi(CCS opt + 7);
+return;
+}
+
+static int
+socks_auth(int fd, int method, socks_opts * sob, time_t tmo)
+{
+uschar * s;
+int len, i, j;
+
+switch(method)
+ {
+ default:
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "Unrecognised socks auth method %d", method);
+ return FAIL;
+ case AUTH_NONE:
+ return OK;
+ case AUTH_NAME:
+ HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" socks auth NAME '%s' '%s'\n",
+ sob->auth_name, sob->auth_pwd);
+ i = Ustrlen(sob->auth_name);
+ j = Ustrlen(sob->auth_pwd);
+ s = string_sprintf("%c%c%.255s%c%.255s", AUTH_NAME_VER,
+ i, sob->auth_name, j, sob->auth_pwd);
+ len = i + j + 3;
+ HDEBUG(D_transport|D_acl|D_v)
+ {
+ debug_printf_indent(" SOCKS>>");
+ for (int i = 0; i<len; i++) debug_printf(" %02x", s[i]);
+ debug_printf("\n");
+ }
+ if (send(fd, s, len, 0) < 0)
+ return FAIL;
+#ifdef TCP_QUICKACK
+ (void) setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, US &off, sizeof(off));
+#endif
+ if (!fd_ready(fd, tmo) || read(fd, s, 2) != 2)
+ return FAIL;
+ HDEBUG(D_transport|D_acl|D_v)
+ debug_printf_indent(" SOCKS<< %02x %02x\n", s[0], s[1]);
+ if (s[0] == AUTH_NAME_VER && s[1] == 0)
+ {
+ HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" socks auth OK\n");
+ return OK;
+ }
+
+ log_write(0, LOG_MAIN|LOG_PANIC, "socks auth failed");
+ errno = EPROTO;
+ return FAIL;
+ }
+}
+
+
+
+/* Find a suitable proxy to use from the list.
+Possible common code with spamd_get_server() ?
+
+Return: index into proxy spec array, or -1
+*/
+
+static int
+socks_get_proxy(socks_opts * proxies, unsigned nproxies)
+{
+unsigned int i;
+socks_opts * sd;
+socks_opts * lim = &proxies[nproxies];
+long rnd, weights;
+unsigned pri;
+
+if (nproxies == 1) /* shortcut, if we have only 1 server */
+ return (proxies[0].is_failed ? -1 : 0);
+
+/* scan for highest pri */
+for (pri = 0, sd = proxies; sd < lim; sd++)
+ if (!sd->is_failed && sd->priority > pri)
+ pri = sd->priority;
+
+/* get sum of weights at this pri */
+for (weights = 0, sd = proxies; sd < lim; sd++)
+ if (!sd->is_failed && sd->priority == pri)
+ weights += sd->weight;
+if (weights == 0) /* all servers failed */
+ return -1;
+
+for (rnd = random_number(weights), i = 0; i < nproxies; i++)
+ {
+ sd = &proxies[i];
+ if (!sd->is_failed && sd->priority == pri)
+ if ((rnd -= sd->weight) < 0)
+ return i;
+ }
+
+log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s unknown error (memory/cpu corruption?)", __FUNCTION__);
+return -1;
+}
+
+
+
+/* Make a connection via a socks proxy
+
+Arguments:
+ host smtp target host
+ host_af address family
+ port remote tcp port number
+ interface local interface
+ tb transport
+ timeout connection timeout (zero for indefinite)
+
+Return value:
+ 0 on success; -1 on failure, with errno set
+*/
+
+int
+socks_sock_connect(host_item * host, int host_af, int port, uschar * interface,
+ transport_instance * tb, int timeout)
+{
+smtp_transport_options_block * ob =
+ (smtp_transport_options_block *)tb->options_block;
+const uschar * proxy_list;
+const uschar * proxy_spec;
+int sep = 0;
+int fd;
+time_t tmo;
+const uschar * state;
+uschar buf[24];
+socks_opts proxies[32]; /* max #proxies handled */
+unsigned nproxies;
+socks_opts * sob = NULL;
+unsigned size;
+blob early_data;
+
+if (!timeout) timeout = 24*60*60; /* use 1 day for "indefinite" */
+tmo = time(NULL) + timeout;
+
+if (!(proxy_list = expand_string(ob->socks_proxy)))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "Bad expansion for socks_proxy in %s",
+ tb->name);
+ return -1;
+ }
+
+/* Read proxy list */
+
+for (nproxies = 0;
+ nproxies < nelem(proxies)
+ && (proxy_spec = string_nextinlist(&proxy_list, &sep, NULL, 0));
+ nproxies++)
+ {
+ int subsep = -' ';
+ const uschar * option;
+
+ socks_option_defaults(sob = &proxies[nproxies]);
+
+ if (!(sob->proxy_host = string_nextinlist(&proxy_spec, &subsep, NULL, 0)))
+ {
+ /* paniclog config error */
+ return -1;
+ }
+
+ /*XXX consider global options eg. "hide socks_password = wibble" on the tpt */
+ /* extract any further per-proxy options */
+ while ((option = string_nextinlist(&proxy_spec, &subsep, NULL, 0)))
+ socks_option(sob, option);
+ }
+if (!sob) return -1;
+
+/* Set up the socks protocol method-selection message,
+for sending on connection */
+
+state = US"method select";
+buf[0] = 5; buf[1] = 1; buf[2] = sob->auth_type;
+early_data.data = buf;
+early_data.len = 3;
+
+/* Try proxies until a connection succeeds */
+
+for(;;)
+ {
+ int idx;
+ host_item proxy;
+ smtp_connect_args sc = {.sock = -1};
+
+ if ((idx = socks_get_proxy(proxies, nproxies)) < 0)
+ {
+ HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" no proxies left\n");
+ errno = EBUSY;
+ return -1;
+ }
+ sob = &proxies[idx];
+
+ /* bodge up a host struct for the proxy */
+ proxy.address = proxy.name = sob->proxy_host;
+ proxy.port = sob->port;
+
+ sc.tblock = tb;
+ sc.ob = ob;
+ sc.host = &proxy;
+ sc.host_af = Ustrchr(sob->proxy_host, ':') ? AF_INET6 : AF_INET;
+ sc.interface = interface;
+
+ /*XXX we trust that the method-select command is idempotent */
+ if ((fd = smtp_sock_connect(&sc, sob->timeout, &early_data)) >= 0)
+ {
+ proxy_local_address = string_copy(proxy.address);
+ proxy_local_port = sob->port;
+ break;
+ }
+
+ log_write(0, LOG_MAIN, "%s: %s", __FUNCTION__, strerror(errno));
+ sob->is_failed = TRUE;
+ }
+
+/* Do the socks protocol stuff */
+
+HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" SOCKS>> 05 01 %02x\n", sob->auth_type);
+
+/* expect method response */
+
+#ifdef TCP_QUICKACK
+(void) setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, US &off, sizeof(off));
+#endif
+
+if ( !fd_ready(fd, tmo)
+ || read(fd, buf, 2) != 2
+ )
+ goto rcv_err;
+HDEBUG(D_transport|D_acl|D_v)
+ debug_printf_indent(" SOCKS<< %02x %02x\n", buf[0], buf[1]);
+if ( buf[0] != 5
+ || socks_auth(fd, buf[1], sob, tmo) != OK
+ )
+ goto proxy_err;
+
+ {
+ union sockaddr_46 sin;
+ (void) ip_addr(&sin, host_af, host->address, port);
+
+ /* send connect (ipver, ipaddr, port) */
+
+ buf[0] = 5; buf[1] = 1; buf[2] = 0; buf[3] = host_af == AF_INET6 ? 4 : 1;
+ #if HAVE_IPV6
+ if (host_af == AF_INET6)
+ {
+ memcpy(buf+4, &sin.v6.sin6_addr, sizeof(sin.v6.sin6_addr));
+ memcpy(buf+4+sizeof(sin.v6.sin6_addr),
+ &sin.v6.sin6_port, sizeof(sin.v6.sin6_port));
+ size = 4+sizeof(sin.v6.sin6_addr)+sizeof(sin.v6.sin6_port);
+ }
+ else
+ #endif
+ {
+ memcpy(buf+4, &sin.v4.sin_addr.s_addr, sizeof(sin.v4.sin_addr.s_addr));
+ memcpy(buf+4+sizeof(sin.v4.sin_addr.s_addr),
+ &sin.v4.sin_port, sizeof(sin.v4.sin_port));
+ size = 4+sizeof(sin.v4.sin_addr.s_addr)+sizeof(sin.v4.sin_port);
+ }
+ }
+
+state = US"connect";
+HDEBUG(D_transport|D_acl|D_v)
+ {
+ debug_printf_indent(" SOCKS>>");
+ for (int i = 0; i<size; i++) debug_printf(" %02x", buf[i]);
+ debug_printf("\n");
+ }
+if (send(fd, buf, size, 0) < 0)
+ goto snd_err;
+
+/* expect conn-reply (success, local(ipver, addr, port))
+of same length as conn-request, or non-success fail code */
+
+if ( !fd_ready(fd, tmo)
+ || (size = read(fd, buf, size)) < 2
+ )
+ goto rcv_err;
+HDEBUG(D_transport|D_acl|D_v)
+ {
+ debug_printf_indent(" SOCKS>>");
+ for (int i = 0; i<size; i++) debug_printf(" %02x", buf[i]);
+ debug_printf("\n");
+ }
+if ( buf[0] != 5
+ || buf[1] != 0
+ )
+ goto proxy_err;
+
+proxy_external_address = string_copy(
+ host_ntoa(buf[3] == 4 ? AF_INET6 : AF_INET, buf+4, NULL, NULL));
+proxy_external_port = ntohs(*((uint16_t *)(buf + (buf[3] == 4 ? 20 : 8))));
+proxy_session = TRUE;
+
+HDEBUG(D_transport|D_acl|D_v)
+ debug_printf_indent(" proxy farside: [%s]:%d\n", proxy_external_address, proxy_external_port);
+
+return fd;
+
+snd_err:
+ HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" proxy snd_err %s: %s\n", state, strerror(errno));
+ return -1;
+
+proxy_err:
+ {
+ struct socks_err * se =
+ buf[1] > nelem(socks_errs) ? NULL : socks_errs + buf[1];
+ HDEBUG(D_transport|D_acl|D_v)
+ debug_printf_indent(" proxy %s: %s\n", state, se ? se->reason : US"unknown error code received");
+ errno = se ? se->errcode : EPROTO;
+ }
+
+rcv_err:
+ HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" proxy rcv_err %s: %s\n", state, strerror(errno));
+ if (!errno) errno = EPROTO;
+ else if (errno == ENOENT) errno = ECONNABORTED;
+ return -1;
+}
+
+#endif /* entire file */
+/* vi: aw ai sw=2
+*/
diff --git a/src/transports/tf_maildir.c b/src/transports/tf_maildir.c
new file mode 100644
index 0000000..a83fc6f
--- /dev/null
+++ b/src/transports/tf_maildir.c
@@ -0,0 +1,585 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* Copyright (c) The Exim Maintainers 2020 - 2021 */
+/* 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)
+ {
+ int err;
+ PCRE2_SIZE offset;
+ const pcre2_code * re;
+
+ DEBUG(D_transport) debug_printf("checking for maildirfolder requirement\n");
+
+ if (!(re = pcre2_compile((PCRE2_SPTR)maildirfolder_create_regex,
+ PCRE2_ZERO_TERMINATED, PCRE_COPT, &err, &offset, pcre_cmp_ctx)))
+ {
+ uschar errbuf[128];
+ pcre2_get_error_message(err, errbuf, sizeof(errbuf));
+ addr->message = string_sprintf("appendfile: regular expression "
+ "error: %s at offset %ld while compiling %s", errbuf, (long)offset,
+ maildirfolder_create_regex);
+ return FALSE;
+ }
+
+ if (regex_match(re, path, -1, NULL))
+ {
+ 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);
+if (lseek(fd, 0, SEEK_END) >= 0)
+ {
+ 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 pcre2_code *regex, const pcre2_code *dir_regex, BOOL timestamp_only)
+{
+DIR *dir;
+off_t sum = 0;
+
+if (!(dir = exim_opendir(path)))
+ return 0;
+
+for (struct dirent *ent; ent = readdir(dir); )
+ {
+ uschar * s, * name = US ent->d_name;
+ struct stat statbuf;
+
+ 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 && !regex_match(dir_regex, name, -1, NULL))
+ {
+ DEBUG(D_transport)
+ debug_printf("skipping %s/%s: dir_regex does not match\n", path, name);
+ continue;
+ }
+
+ /* The name is OK; stat it. */
+
+ s = string_sprintf("%s/%s", path, name);
+ if (Ustat(s, &statbuf) < 0)
+ {
+ DEBUG(D_transport)
+ debug_printf("maildir_compute_size: stat error %d for %s: %s\n", errno,
+ s, strerror(errno));
+ continue;
+ }
+
+ if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
+ {
+ DEBUG(D_transport)
+ debug_printf("skipping %s/%s: not a directory\n", s, 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(s, 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(s, 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 directory 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 pcre2_code *regex, const pcre2_code *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);
+if ((fd = Uopen(filename, O_RDWR|O_APPEND, ob->mode ? ob->mode : 0600)) < 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. */
+
+if ((count = read(fd, buffer, sizeof(buffer))) >= 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/" TIME_T_FMT ".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
index 0000000..b3707b1
--- /dev/null
+++ b/src/transports/tf_maildir.h
@@ -0,0 +1,21 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* Copyright (c) The Exim Maintainers 2021 */
+/* 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 pcre2_code *,
+ const pcre2_code *, BOOL);
+extern BOOL maildir_ensure_directories(uschar *, address_item *, BOOL, int,
+ uschar *);
+extern int maildir_ensure_sizefile(uschar *,
+ appendfile_transport_options_block *, const pcre2_code *,
+ const pcre2_code *, 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
index 0000000..bb8ad44
--- /dev/null
+++ b/src/tree.c
@@ -0,0 +1,393 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2021 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2015 */
+/* 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(const uschar *s)
+{
+rmark rpoint = store_mark();
+tree_node * node = store_get(sizeof(tree_node) + Ustrlen(s), s);
+Ustrcpy(node->name, s);
+node->data.ptr = NULL;
+if (!tree_insertnode(&tree_nonrecipients, node)) store_reset(rpoint);
+}
+
+
+
+/*************************************************
+* 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(const uschar *s, address_item *addr)
+{
+rmark rpoint = store_mark();
+tree_node * node = store_get(sizeof(tree_node) + Ustrlen(s), s);
+Ustrcpy(node->name, s);
+node->data.ptr = addr;
+if (!tree_insertnode(&tree_duplicates, node)) store_reset(rpoint);
+}
+
+
+
+/*************************************************
+* Add entry to unusable addresses tree *
+*************************************************/
+
+/* Duplicates are simply discarded.
+
+Argument: the host item
+Returns: nothing
+*/
+
+void
+tree_add_unusable(const host_item *h)
+{
+rmark rpoint = store_mark();
+tree_node *node;
+uschar s[256];
+sprintf(CS s, "T:%.200s:%s", h->name, h->address);
+node = store_get(sizeof(tree_node) + Ustrlen(s),
+ is_tainted(h->name) || is_tainted(h->address) ? GET_TAINTED : GET_UNTAINTED);
+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(rpoint);
+}
+
+
+
+/*************************************************
+* 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, const uschar *name)
+{
+while (p)
+ {
+ 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) return;
+f(p->name, p->data.ptr, ctx);
+tree_walk(p->left, f, ctx);
+tree_walk(p->right, f, ctx);
+}
+
+
+
+/* Add a node to a tree, ignoring possibility of duplicates */
+
+static void
+tree_add_var(uschar * name, uschar * val, void * ctx)
+{
+tree_node ** root = ctx;
+tree_node * node = store_get(sizeof(tree_node) + Ustrlen(name), name);
+Ustrcpy(node->name, name);
+node->data.ptr = val;
+(void) tree_insertnode(root, node);
+}
+
+/* Duplicate a tree */
+
+void
+tree_dup(tree_node ** dstp, tree_node * src)
+{
+tree_walk(src, tree_add_var, dstp);
+}
+
+
+
+/* End of tree.c */
diff --git a/src/utf8.c b/src/utf8.c
new file mode 100644
index 0000000..bc7adb8
--- /dev/null
+++ b/src/utf8.c
@@ -0,0 +1,278 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2022 */
+/* Copyright (c) Jeremy Harris 2015 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "exim.h"
+
+#ifdef SUPPORT_I18N
+
+#ifdef SUPPORT_I18N_2008
+# include <idn2.h>
+#else
+# include <idna.h>
+#endif
+
+#include <punycode.h>
+#include <stringprep.h>
+
+static uschar *
+string_localpart_alabel_to_utf8_(const uschar * alabel, uschar ** err);
+
+/**************************************************/
+
+BOOL
+string_is_utf8(const uschar * s)
+{
+uschar c;
+if (s) while ((c = *s++)) if (c & 0x80) return TRUE;
+return FALSE;
+}
+
+static BOOL
+string_is_alabel(const uschar * s)
+{
+return s[0] == 'x' && s[1] == 'n' && s[2] == '-' && s[3] == '-';
+}
+
+/**************************************************/
+/* Domain conversions.
+The *err string pointer should be null before the call
+
+Return NULL for error, with optional errstr pointer filled in
+*/
+
+uschar *
+string_domain_utf8_to_alabel(const uschar * utf8, uschar ** err)
+{
+uschar * s1, * s;
+int rc;
+
+#ifdef SUPPORT_I18N_2008
+/* Avoid lowercasing plain-ascii domains */
+if (!string_is_utf8(utf8))
+ return string_copy(utf8);
+
+/* Only lowercase is accepted by the library call. A pity since we lose
+any mixed-case annotation. This does not really matter for a domain. */
+ {
+ uschar c;
+ for (s1 = s = US utf8; (c = *s1); s1++) if (!(c & 0x80) && isupper(c))
+ {
+ s = string_copy(utf8);
+ for (s1 = s + (s1 - utf8); (c = *s1); s1++) if (!(c & 0x80) && isupper(c))
+ *s1 = tolower(c);
+ break;
+ }
+ }
+if ((rc = idn2_lookup_u8((const uint8_t *) s, &s1, IDN2_NFC_INPUT)) != IDN2_OK)
+ {
+ if (err) *err = US idn2_strerror(rc);
+ return NULL;
+ }
+#else
+s = US stringprep_utf8_nfkc_normalize(CCS utf8, -1);
+if ( (rc = idna_to_ascii_8z(CCS s, CSS &s1, IDNA_ALLOW_UNASSIGNED))
+ != IDNA_SUCCESS)
+ {
+ free(s);
+ if (err) *err = US idna_strerror(rc);
+ return NULL;
+ }
+free(s);
+#endif
+s = string_copy(s1);
+free(s1);
+return s;
+}
+
+
+
+uschar *
+string_domain_alabel_to_utf8(const uschar * alabel, uschar ** err)
+{
+#ifdef SUPPORT_I18N_2008
+const uschar * label;
+int sep = '.';
+gstring * g = NULL;
+
+while (label = string_nextinlist(&alabel, &sep, NULL, 0))
+ if ( string_is_alabel(label)
+ && !(label = string_localpart_alabel_to_utf8_(label, err))
+ )
+ return NULL;
+ else
+ g = string_append_listele(g, '.', label);
+return string_from_gstring(g);
+
+#else
+
+uschar * s1, * s;
+int rc;
+
+if ( (rc = idna_to_unicode_8z8z(CCS alabel, CSS &s1, IDNA_USE_STD3_ASCII_RULES))
+ != IDNA_SUCCESS)
+ {
+ if (err) *err = US idna_strerror(rc);
+ return NULL;
+ }
+s = string_copy(s1);
+free(s1);
+return s;
+#endif
+}
+
+/**************************************************/
+/* localpart conversions */
+/* the *err string pointer should be null before the call */
+
+
+uschar *
+string_localpart_utf8_to_alabel(const uschar * utf8, uschar ** err)
+{
+size_t ucs4_len = 0;
+punycode_uint * p;
+size_t p_len;
+uschar * res;
+int rc;
+
+if (!string_is_utf8(utf8)) return string_copy(utf8);
+
+p = (punycode_uint *) stringprep_utf8_to_ucs4(CCS utf8, -1, &ucs4_len);
+if (!p || !ucs4_len)
+ {
+ if (err) *err = US"l_u2a: bad UTF-8 input";
+ return NULL;
+ }
+p_len = ucs4_len*4; /* this multiplier is pure guesswork */
+res = store_get(p_len+5, utf8);
+
+res[0] = 'x'; res[1] = 'n'; res[2] = res[3] = '-';
+
+if ((rc = punycode_encode(ucs4_len, p, NULL, &p_len, CS res+4)) != PUNYCODE_SUCCESS)
+ {
+ DEBUG(D_expand) debug_printf("l_u2a: bad '%s'\n", punycode_strerror(rc));
+ free(p);
+ if (err) *err = US punycode_strerror(rc);
+ return NULL;
+ }
+p_len += 4;
+free(p);
+res[p_len] = '\0';
+return res;
+}
+
+
+static uschar *
+string_localpart_alabel_to_utf8_(const uschar * alabel, uschar ** err)
+{
+size_t p_len;
+punycode_uint * p;
+int rc;
+uschar * s, * res;
+
+DEBUG(D_expand) debug_printf("l_a2u: '%s'\n", alabel);
+alabel += 4;
+p_len = Ustrlen(alabel);
+p = store_get((p_len+1) * sizeof(*p), alabel);
+
+if ((rc = punycode_decode(p_len, CCS alabel, &p_len, p, NULL)) != PUNYCODE_SUCCESS)
+ {
+ if (err) *err = US punycode_strerror(rc);
+ return NULL;
+ }
+
+s = US stringprep_ucs4_to_utf8(p, p_len, NULL, &p_len);
+res = string_copyn(s, p_len);
+free(s);
+return res;
+}
+
+
+uschar *
+string_localpart_alabel_to_utf8(const uschar * alabel, uschar ** err)
+{
+if (string_is_alabel(alabel))
+ return string_localpart_alabel_to_utf8_(alabel, err);
+
+if (err) *err = US"bad alabel prefix";
+return NULL;
+}
+
+
+/**************************************************/
+/* Whole address conversion.
+The *err string pointer should be null before the call.
+
+Return NULL on error, with (optional) errstring pointer filled in
+*/
+
+uschar *
+string_address_utf8_to_alabel(const uschar * utf8, uschar ** err)
+{
+uschar * l, * d;
+
+if (!*utf8) return string_copy(utf8);
+
+DEBUG(D_expand) debug_printf("addr from utf8 <%s>", utf8);
+
+for (const uschar * s = utf8; *s; s++)
+ if (*s == '@')
+ {
+ l = string_copyn(utf8, s - utf8);
+ if ( !(l = string_localpart_utf8_to_alabel(l, err))
+ || !(d = string_domain_utf8_to_alabel(++s, err))
+ )
+ return NULL;
+ l = string_sprintf("%s@%s", l, d);
+ DEBUG(D_expand) debug_printf(" -> <%s>\n", l);
+ return l;
+ }
+
+l = string_localpart_utf8_to_alabel(utf8, err);
+DEBUG(D_expand) debug_printf(" -> <%s>\n", l);
+return l;
+}
+
+
+
+/*************************************************
+* Report the library versions. *
+*************************************************/
+
+/* See a description in tls-openssl.c for an explanation of why this exists.
+
+Arguments: string to append to
+Returns: string
+*/
+
+gstring *
+utf8_version_report(gstring * g)
+{
+#ifdef SUPPORT_I18N_2008
+g = string_fmt_append(g, "Library version: IDN2: Compile: %s\n"
+ " Runtime: %s\n",
+ IDN2_VERSION,
+ idn2_check_version(NULL));
+g = string_fmt_append(g, "Library version: Stringprep: Compile: %s\n"
+ " Runtime: %s\n",
+ STRINGPREP_VERSION,
+ stringprep_check_version(NULL));
+#else
+g = string_fmt_append(g, "Library version: IDN: Compile: %s\n"
+ " Runtime: %s\n",
+ STRINGPREP_VERSION,
+ stringprep_check_version(NULL));
+#endif
+return g;
+}
+
+#endif /* whole file */
+
+/* vi: aw ai sw=2
+*/
+/* End of utf8.c */
diff --git a/src/valgrind.h b/src/valgrind.h
new file mode 100644
index 0000000..01c49da
--- /dev/null
+++ b/src/valgrind.h
@@ -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
+ nastiness 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 arbitrary 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
index 0000000..b4c2b9a
--- /dev/null
+++ b/src/verify.c
@@ -0,0 +1,3610 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* 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 */
+static smtp_context ctctx;
+uschar ctbuffer[8192];
+
+
+static uschar cutthrough_response(client_conn_ctx *, char, uschar **, int);
+
+
+
+/*************************************************
+* 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, const uschar *key, uschar *type,
+ int positive_expire, int negative_expire)
+{
+BOOL negative;
+int length, expire;
+time_t now;
+dbdata_callout_cache *cache_record;
+
+if (!(cache_record = dbfn_read_with_length(dbm_file, key, &length)))
+ {
+ HDEBUG(D_verify) debug_printf_indent("callout cache: no %s record found for %s\n", type, key);
+ 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_indent("callout cache: %s record expired for %s\n", type, key);
+ 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), GET_UNTAINTED);
+ 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_indent("callout cache: found %s record for %s\n", type, key);
+return cache_record;
+}
+
+
+
+/* Check the callout cache.
+Options * pm_mailfrom may be modified by cache partial results.
+
+Return: TRUE if result found
+*/
+
+static BOOL
+cached_callout_lookup(address_item * addr, uschar * address_key,
+ uschar * from_address, int * opt_ptr, uschar ** pm_ptr,
+ int * yield, uschar ** failure_ptr,
+ dbdata_callout_cache * new_domain_record, int * old_domain_res)
+{
+int options = *opt_ptr;
+open_db dbblock;
+open_db *dbm_file = NULL;
+
+/* Open the callout cache database, if it exists, for reading only at this
+stage, unless caching has been disabled. */
+
+if (options & vopt_callout_no_cache)
+ {
+ HDEBUG(D_verify) debug_printf_indent("callout cache: disabled by no_cache\n");
+ }
+else if (!(dbm_file = dbfn_open(US"callout", O_RDWR, &dbblock, FALSE, TRUE)))
+ {
+ HDEBUG(D_verify) debug_printf_indent("callout cache: not available\n");
+ }
+else
+ {
+ /* 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. */
+
+ 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)
+ {
+ /* 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_res = cache_record->result;
+
+ if ( cache_record->result == ccache_reject
+ || *from_address == 0 && cache_record->result == ccache_reject_mfnull)
+ {
+ HDEBUG(D_verify)
+ debug_printf_indent("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";
+ dbfn_close(dbm_file);
+ return TRUE;
+ }
+
+ /* 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 (options & vopt_callout_random) switch(cache_record->random_result)
+ {
+ case ccache_accept:
+ HDEBUG(D_verify)
+ debug_printf_indent("callout cache: domain accepts random addresses\n");
+ *failure_ptr = US"random";
+ dbfn_close(dbm_file);
+ return TRUE; /* Default yield is OK */
+
+ case ccache_reject:
+ HDEBUG(D_verify)
+ debug_printf_indent("callout cache: domain rejects random addresses\n");
+ *opt_ptr = options & ~vopt_callout_random;
+ new_domain_record->random_result = ccache_reject;
+ new_domain_record->random_stamp = cache_record->random_stamp;
+ break;
+
+ default:
+ HDEBUG(D_verify)
+ debug_printf_indent("callout cache: need to check random address handling "
+ "(not cached or cache expired)\n");
+ dbfn_close(dbm_file);
+ return FALSE;
+ }
+
+ /* 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_ptr)
+ {
+ if (cache_record->postmaster_result == ccache_reject)
+ {
+ setflag(addr, af_verify_pmfail);
+ HDEBUG(D_verify)
+ debug_printf_indent("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).";
+ dbfn_close(dbm_file);
+ return TRUE;
+ }
+ if (cache_record->postmaster_result == ccache_unknown)
+ {
+ HDEBUG(D_verify)
+ debug_printf_indent("callout cache: need to check RCPT "
+ "TO:<postmaster@domain> (not cached or cache expired)\n");
+ dbfn_close(dbm_file);
+ return FALSE;
+ }
+
+ /* 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_indent("callout cache: domain accepts RCPT "
+ "TO:<postmaster@domain>\n");
+ *pm_ptr = 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).
+ */
+
+ if (!(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)))
+ {
+ dbfn_close(dbm_file);
+ return FALSE;
+ }
+
+ if (cache_address_record->result == ccache_accept)
+ {
+ HDEBUG(D_verify)
+ debug_printf_indent("callout cache: address record is positive\n");
+ }
+ else
+ {
+ HDEBUG(D_verify)
+ debug_printf_indent("callout cache: address record is negative\n");
+ addr->user_message = US"Previous (cached) callout verification failure";
+ *failure_ptr = US"recipient";
+ *yield = FAIL;
+ }
+
+ /* Close the cache database while we actually do the callout for real. */
+
+ dbfn_close(dbm_file);
+ return TRUE;
+ }
+return FALSE;
+}
+
+
+/* Write results to callout cache
+*/
+static void
+cache_callout_write(dbdata_callout_cache * dom_rec, const uschar * domain,
+ int done, dbdata_callout_cache_address * addr_rec, uschar * address_key)
+{
+open_db dbblock;
+open_db *dbm_file = NULL;
+
+/* 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 (dom_rec->result != ccache_unknown)
+ if (!(dbm_file = dbfn_open(US"callout", O_RDWR|O_CREAT, &dbblock, FALSE, TRUE)))
+ {
+ HDEBUG(D_verify) debug_printf_indent("callout cache: not available\n");
+ }
+ else
+ {
+ (void)dbfn_write(dbm_file, domain, dom_rec,
+ (int)sizeof(dbdata_callout_cache));
+ HDEBUG(D_verify) debug_printf_indent("wrote callout cache domain record for %s:\n"
+ " result=%d postmaster=%d random=%d\n",
+ domain,
+ dom_rec->result,
+ dom_rec->postmaster_result,
+ dom_rec->random_result);
+ }
+
+/* If a definite result was obtained for the callout, cache it unless caching
+is disabled. */
+
+if (done && addr_rec->result != ccache_unknown)
+ {
+ if (!dbm_file)
+ dbm_file = dbfn_open(US"callout", O_RDWR|O_CREAT, &dbblock, FALSE, TRUE);
+ if (!dbm_file)
+ {
+ HDEBUG(D_verify) debug_printf_indent("no callout cache available\n");
+ }
+ else
+ {
+ (void)dbfn_write(dbm_file, address_key, addr_rec,
+ (int)sizeof(dbdata_callout_cache_address));
+ HDEBUG(D_verify) debug_printf_indent("wrote %s callout cache address record for %s\n",
+ addr_rec->result == ccache_accept ? "positive" : "negative",
+ address_key);
+ }
+ }
+
+if (dbm_file) dbfn_close(dbm_file);
+}
+
+
+/* Cutthrough-multi. If the existing cached cutthrough connection matches
+the one we would make for a subsequent recipient, use it. Send the RCPT TO
+and check the result, nonpipelined as it may be wanted immediately for
+recipient-verification.
+
+It seems simpler to deal with this case separately from the main callout loop.
+We will need to remember it has sent, or not, so that rcpt-acl tail code
+can do it there for the non-rcpt-verify case. For this we keep an addresscount.
+
+Return: TRUE for a definitive result for the recipient
+*/
+static int
+cutthrough_multi(address_item * addr, host_item * host_list,
+ transport_feedback * tf, int * yield)
+{
+BOOL done = FALSE;
+
+if (addr->transport == cutthrough.addr.transport)
+ for (host_item * host = host_list; host; host = host->next)
+ if (Ustrcmp(host->address, cutthrough.host.address) == 0)
+ {
+ int host_af;
+ uschar *interface = NULL; /* Outgoing interface to use; NULL => any */
+ int port = 25;
+
+ deliver_host = host->name;
+ deliver_host_address = host->address;
+ deliver_host_port = host->port;
+ deliver_domain = addr->domain;
+ transport_name = addr->transport->name;
+
+ host_af = Ustrchr(host->address, ':') ? AF_INET6 : AF_INET;
+
+ if ( !smtp_get_interface(tf->interface, host_af, addr, &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);
+
+ smtp_port_for_connect(host, port);
+
+ if ( ( interface == cutthrough.interface
+ || ( interface
+ && cutthrough.interface
+ && Ustrcmp(interface, cutthrough.interface) == 0
+ ) )
+ && host->port == cutthrough.host.port
+ )
+ {
+ uschar * resp = NULL;
+
+ /* Match! Send the RCPT TO, set done from the response */
+ done =
+ smtp_write_command(&ctctx, SCMD_FLUSH, "RCPT TO:<%.1000s>\r\n",
+ transport_rcpt_address(addr,
+ addr->transport->rcpt_include_affixes)) >= 0
+ && cutthrough_response(&cutthrough.cctx, '2', &resp,
+ CUTTHROUGH_DATA_TIMEOUT) == '2';
+
+ /* This would go horribly wrong if a callout fail was ignored by ACL.
+ We punt by abandoning cutthrough on a reject, like the
+ first-rcpt does. */
+
+ if (done)
+ {
+ address_item * na = store_get(sizeof(address_item), GET_UNTAINTED);
+ *na = cutthrough.addr;
+ cutthrough.addr = *addr;
+ cutthrough.addr.host_used = &cutthrough.host;
+ cutthrough.addr.next = na;
+
+ cutthrough.nrcpt++;
+ }
+ else
+ {
+ cancel_cutthrough_connection(TRUE, US"recipient rejected");
+ if (!resp || errno == ETIMEDOUT)
+ {
+ HDEBUG(D_verify) debug_printf("SMTP timeout\n");
+ }
+ else if (errno == 0)
+ {
+ if (*resp == 0)
+ Ustrcpy(resp, US"connection dropped");
+
+ addr->message =
+ string_sprintf("response to \"%s\" was: %s",
+ big_buffer, string_printing(resp));
+
+ addr->user_message =
+ string_sprintf("Callout verification failed:\n%s", resp);
+
+ /* Hard rejection ends the process */
+
+ if (resp[0] == '5') /* Address rejected */
+ {
+ *yield = FAIL;
+ done = TRUE;
+ }
+ }
+ }
+ }
+ break; /* host_list */
+ }
+if (!done)
+ cancel_cutthrough_connection(TRUE, US"incompatible connection");
+return done;
+}
+
+
+
+
+/* A rcpt callout, or cached record of one, verified the address.
+Set $domain_data and $local_part_data to detainted versions.
+*/
+static void
+callout_verified_rcpt(const address_item * addr)
+{
+address_item a = {.address = addr->address};
+if (deliver_split_address(&a) != OK) return;
+deliver_localpart_data = string_copy_taint(a.local_part, GET_UNTAINTED);
+deliver_domain_data = string_copy_taint(a.domain, GET_UNTAINTED);
+}
+
+
+/*************************************************
+* 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
+ vopt_callout_hold => lazy close connection
+ 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)
+{
+int yield = OK;
+int old_domain_cache_result = ccache_accept;
+BOOL done = FALSE;
+uschar *address_key;
+uschar *from_address;
+uschar *random_local_part = NULL;
+const uschar *save_deliver_domain = deliver_domain;
+uschar **failure_ptr = options & vopt_is_recipient
+ ? &recipient_verify_failure : &sender_verify_failure;
+dbdata_callout_cache new_domain_record;
+dbdata_callout_cache_address new_address_record;
+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. */
+
+if (options & vopt_is_recipient)
+ if (options & vopt_callout_recipsender)
+ {
+ from_address = sender_address;
+ address_key = string_sprintf("%s/<%s>", addr->address, sender_address);
+ if (cutthrough.delivery) options |= vopt_callout_no_cache;
+ }
+ else if (options & vopt_callout_recippmaster)
+ {
+ from_address = string_sprintf("postmaster@%s", qualify_domain_sender);
+ address_key = string_sprintf("%s/<postmaster@%s>", addr->address,
+ qualify_domain_sender);
+ }
+ else
+ {
+ from_address = US"";
+ address_key = addr->address;
+ }
+
+/* For a sender callout, we must adjust the key if the mailfrom address is not
+empty. */
+
+else
+ {
+ from_address = se_mailfrom ? se_mailfrom : US"";
+ address_key = *from_address
+ ? string_sprintf("%s/<%s>", addr->address, from_address) : addr->address;
+ }
+
+if (cached_callout_lookup(addr, address_key, from_address,
+ &options, &pm_mailfrom, &yield, failure_ptr,
+ &new_domain_record, &old_domain_cache_result))
+ {
+ cancel_cutthrough_connection(TRUE, US"cache-hit");
+ goto END_CALLOUT;
+ }
+
+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;
+ smtp_context * sx = NULL;
+
+ /* 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 randomising. */
+
+ if (options & vopt_callout_random && callout_random_local_part)
+ if (!(random_local_part = expand_string(callout_random_local_part)))
+ log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand "
+ "callout_random_local_part: %s", expand_string_message);
+
+ /* Compile regex' used by client-side smtp */
+
+ smtp_deliver_init();
+
+ /* 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 && !f.disable_callout_flush) mac_smtp_fflush();
+
+ clearflag(addr, af_verify_pmfail); /* postmaster callout flag */
+ clearflag(addr, af_verify_nsfail); /* null sender callout flag */
+
+/* cutthrough-multi: if a nonfirst rcpt has the same routing as the first,
+and we are holding a cutthrough conn open, we can just append the rcpt to
+that conn for verification purposes (and later delivery also). Simplest
+coding means skipping this whole loop and doing the append separately. */
+
+ /* Can we re-use an open cutthrough connection? */
+ if ( cutthrough.cctx.sock >= 0
+ && (options & (vopt_callout_recipsender | vopt_callout_recippmaster))
+ == vopt_callout_recipsender
+ && !random_local_part
+ && !pm_mailfrom
+ )
+ done = cutthrough_multi(addr, host_list, tf, &yield);
+
+ /* If we did not use a cached connection, make connections to the hosts
+ and do real callouts. The list of hosts is passed in as an argument. */
+
+ for (host_item * host = host_list; host && !done; host = host->next)
+ {
+ int host_af;
+ int port = 25;
+ uschar * interface = NULL; /* Outgoing interface to use; NULL => any */
+
+ if (!host->address)
+ {
+ 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, ':') ? AF_INET6 : AF_INET;
+
+ /* 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_host_port = host->port;
+ deliver_domain = addr->domain;
+ transport_name = addr->transport->name;
+
+ if ( !smtp_get_interface(tf->interface, host_af, addr, &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);
+
+ if (!sx) sx = store_get(sizeof(*sx), GET_TAINTED); /* tainted buffers */
+ memset(sx, 0, sizeof(*sx));
+
+ sx->addrlist = sx->first_addr = addr;
+ sx->conn_args.host = host;
+ sx->conn_args.host_af = host_af,
+ sx->port = port;
+ sx->conn_args.interface = interface;
+ sx->helo_data = tf->helo_data;
+ sx->conn_args.tblock = addr->transport;
+ sx->conn_args.sock = -1;
+ sx->verify = TRUE;
+
+tls_retry_connection:
+ /* Set the address state so that errors are recorded in it */
+
+ addr->transport_return = PENDING_DEFER;
+ ob->connect_timeout = callout_connect;
+ ob->command_timeout = callout;
+
+ /* Get the channel set up ready for a message (MAIL FROM being the next
+ SMTP command to send. If we tried TLS but it failed, try again without
+ if permitted */
+
+ yield = smtp_setup_conn(sx, FALSE);
+#ifndef DISABLE_TLS
+ if ( yield == DEFER
+ && addr->basic_errno == ERRNO_TLSFAILURE
+ && ob->tls_tempfail_tryclear
+ && verify_check_given_host(CUSS &ob->hosts_require_tls, host) != OK
+ )
+ {
+ log_write(0, LOG_MAIN,
+ "%s: callout unencrypted to %s [%s] (not in hosts_require_tls)",
+ addr->message, host->name, host->address);
+ addr->transport_return = PENDING_DEFER;
+ yield = smtp_setup_conn(sx, TRUE);
+ }
+#endif
+ if (yield != OK)
+ {
+ errno = addr->basic_errno;
+ transport_name = NULL;
+ deliver_host = deliver_host_address = NULL;
+ deliver_domain = save_deliver_domain;
+
+ /* Failure to accept HELO is cached; this blocks the whole domain for all
+ senders. I/O errors and defer responses are not cached. */
+
+ if (yield == FAIL && (errno == 0 || errno == ERRNO_SMTPCLOSED))
+ {
+ setflag(addr, af_verify_nsfail);
+ new_domain_record.result = ccache_reject;
+ done = TRUE;
+ }
+ else
+ done = FALSE;
+ goto no_conn;
+ }
+
+ /* If we needed to authenticate, smtp_setup_conn() did that. Copy
+ the AUTH info for logging */
+
+ addr->authenticator = client_authenticator;
+ addr->auth_id = client_authenticated_id;
+
+ sx->from_addr = from_address;
+ sx->first_addr = sx->sync_addr = addr;
+ sx->ok = FALSE; /*XXX these 3 last might not be needed for verify? */
+ sx->send_rset = TRUE;
+ sx->completed_addr = FALSE;
+
+ new_domain_record.result = old_domain_cache_result == ccache_reject_mfnull
+ ? ccache_reject_mfnull : ccache_accept;
+
+ /* Do the random local part check first. Temporarily replace the recipient
+ with the "random" value */
+
+ if (random_local_part)
+ {
+ uschar * main_address = addr->address;
+ const uschar * rcpt_domain = addr->domain;
+
+#ifdef SUPPORT_I18N
+ uschar * errstr = NULL;
+ if ( testflag(addr, af_utf8_downcvt)
+ && (rcpt_domain = string_domain_utf8_to_alabel(rcpt_domain,
+ &errstr), errstr)
+ )
+ {
+ addr->message = errstr;
+ errno = ERRNO_EXPANDFAIL;
+ setflag(addr, af_verify_nsfail);
+ done = FALSE;
+ rcpt_domain = US""; /*XXX errorhandling! */
+ }
+#endif
+
+ /* This would be ok for 1st rcpt of a cutthrough (the case handled here;
+ subsequents are done in cutthrough_multi()), but no way to
+ handle a subsequent because of the RSET vaporising the MAIL FROM.
+ So refuse to support any. Most cutthrough use will not involve
+ random_local_part, so no loss. */
+ cancel_cutthrough_connection(TRUE, US"random-recipient");
+
+ addr->address = string_sprintf("%s@%.1000s",
+ random_local_part, rcpt_domain);
+ done = FALSE;
+
+ /* If accepted, we aren't going to do any further tests below.
+ 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.
+ However, some servers drop the connection after responding to an
+ invalid recipient, so on (any) error we drop and remake the connection.
+ XXX We don't care about that for postmaster_full. Should we?
+
+ XXX could we add another flag to the context, and have the common
+ code emit the RSET too? Even pipelined after the RCPT...
+ Then the main-verify call could use it if there's to be a subsequent
+ postmaster-verify.
+ The sync_responses() would need to be taught about it and we'd
+ need another return code filtering out to here.
+
+ Avoid using a SIZE option on the MAIL for all random-rcpt checks.
+ */
+
+ sx->avoid_option = OPTION_SIZE;
+
+ /* Remember when we last did a random test */
+ new_domain_record.random_stamp = time(NULL);
+
+ if (smtp_write_mail_and_rcpt_cmds(sx, &yield) == 0)
+ switch(addr->transport_return)
+ {
+ case PENDING_OK: /* random was accepted, unfortunately */
+ new_domain_record.random_result = ccache_accept;
+ yield = OK; /* Only usable verify result we can return */
+ done = TRUE;
+ *failure_ptr = US"random";
+ goto no_conn;
+ case FAIL: /* rejected: the preferred result */
+ new_domain_record.random_result = ccache_reject;
+ sx->avoid_option = 0;
+
+ /* Between each check, issue RSET, because some servers accept only
+ one recipient after MAIL FROM:<>.
+ XXX We don't care about that for postmaster_full. Should we? */
+
+ if ((done =
+ smtp_write_command(sx, SCMD_FLUSH, "RSET\r\n") >= 0 &&
+ smtp_read_response(sx, sx->buffer, sizeof(sx->buffer), '2', callout)))
+ break;
+
+ HDEBUG(D_acl|D_v)
+ debug_printf_indent("problem after random/rset/mfrom; reopen conn\n");
+ random_local_part = NULL;
+#ifndef DISABLE_TLS
+ tls_close(sx->cctx.tls_ctx, TLS_SHUTDOWN_NOWAIT);
+#endif
+ HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" SMTP(close)>>\n");
+ (void)close(sx->cctx.sock);
+ sx->cctx.sock = -1;
+#ifndef DISABLE_EVENT
+ (void) event_raise(addr->transport->event_action,
+ US"tcp:close", NULL, NULL);
+#endif
+ addr->address = main_address;
+ addr->transport_return = PENDING_DEFER;
+ sx->first_addr = sx->sync_addr = addr;
+ sx->ok = FALSE;
+ sx->send_rset = TRUE;
+ sx->completed_addr = FALSE;
+ goto tls_retry_connection;
+ case DEFER: /* 4xx response to random */
+ break; /* Just to be clear. ccache_unknown, !done. */
+ }
+
+ /* Re-setup for main verify, or for the error message when failing */
+ addr->address = main_address;
+ addr->transport_return = PENDING_DEFER;
+ sx->first_addr = sx->sync_addr = addr;
+ sx->ok = FALSE;
+ sx->send_rset = TRUE;
+ sx->completed_addr = FALSE;
+ }
+ else
+ done = TRUE;
+
+ /* Main verify. For rcpt-verify use SIZE if we know it and we're not cacheing;
+ for sndr-verify never use it. */
+
+ if (done)
+ {
+ if (!(options & vopt_is_recipient && options & vopt_callout_no_cache))
+ sx->avoid_option = OPTION_SIZE;
+
+ done = FALSE;
+ switch(smtp_write_mail_and_rcpt_cmds(sx, &yield))
+ {
+ case 0: switch(addr->transport_return) /* ok so far */
+ {
+ case PENDING_OK: done = TRUE;
+ new_address_record.result = ccache_accept;
+ break;
+ case FAIL: done = TRUE;
+ yield = FAIL;
+ *failure_ptr = US"recipient";
+ new_address_record.result = ccache_reject;
+ break;
+ default: break;
+ }
+ break;
+
+ case -1: /* MAIL response error */
+ *failure_ptr = US"mail";
+ if (errno == 0 && sx->buffer[0] == '5')
+ {
+ setflag(addr, af_verify_nsfail);
+ if (from_address[0] == 0)
+ new_domain_record.result = ccache_reject_mfnull;
+ }
+ break;
+ /* non-MAIL read i/o error */
+ /* non-MAIL response timeout */
+ /* internal error; channel still usable */
+ default: break; /* transmit failed */
+ }
+ }
+
+ addr->auth_sndr = client_authenticated_sender;
+
+ deliver_host = deliver_host_address = NULL;
+ deliver_domain = save_deliver_domain;
+
+ /* 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)
+ {
+ /* Could possibly shift before main verify, just above, and be ok
+ for cutthrough. But no way to handle a subsequent rcpt, so just
+ refuse any */
+ cancel_cutthrough_connection(TRUE, US"postmaster verify");
+ HDEBUG(D_acl|D_v) debug_printf_indent("Cutthrough cancelled by presence of postmaster verify\n");
+
+ done = smtp_write_command(sx, SCMD_FLUSH, "RSET\r\n") >= 0
+ && smtp_read_response(sx, sx->buffer, sizeof(sx->buffer), '2', callout);
+
+ if (done)
+ {
+ uschar * main_address = addr->address;
+
+ /*XXX oops, affixes */
+ addr->address = string_sprintf("postmaster@%.1000s", addr->domain);
+ addr->transport_return = PENDING_DEFER;
+
+ sx->from_addr = pm_mailfrom;
+ sx->first_addr = sx->sync_addr = addr;
+ sx->ok = FALSE;
+ sx->send_rset = TRUE;
+ sx->completed_addr = FALSE;
+ sx->avoid_option = OPTION_SIZE;
+
+ if( smtp_write_mail_and_rcpt_cmds(sx, &yield) == 0
+ && addr->transport_return == PENDING_OK
+ )
+ done = TRUE;
+ else
+ done = (options & vopt_callout_fullpm) != 0
+ && smtp_write_command(sx, SCMD_FLUSH,
+ "RCPT TO:<postmaster>\r\n") >= 0
+ && smtp_read_response(sx, sx->buffer,
+ sizeof(sx->buffer), '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 && sx->buffer[0] == '5')
+ {
+ *failure_ptr = US"postmaster";
+ setflag(addr, af_verify_pmfail);
+ new_domain_record.postmaster_result = ccache_reject;
+ }
+
+ addr->address = main_address;
+ }
+ }
+ /* 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. */
+
+no_conn:
+ switch(errno)
+ {
+ case ETIMEDOUT:
+ HDEBUG(D_verify) debug_printf("SMTP timeout\n");
+ sx->send_quit = FALSE;
+ break;
+
+#ifdef SUPPORT_I18N
+ case ERRNO_UTF8_FWD:
+ {
+ extern int acl_where; /* src/acl.c */
+ errno = 0;
+ addr->message = US"response to \"EHLO\" did not include SMTPUTF8";
+ addr->user_message = acl_where == ACL_WHERE_RCPT
+ ? US"533 no support for internationalised mailbox name"
+ : US"550 mailbox unavailable";
+ yield = FAIL;
+ done = TRUE;
+ }
+ break;
+#endif
+ case ECONNREFUSED:
+ sx->send_quit = FALSE;
+ break;
+
+ case 0:
+ if (*sx->buffer == 0) Ustrcpy(sx->buffer, US"connection dropped");
+
+ /*XXX test here is ugly; seem to have a split of responsibility for
+ building this message. Need to rationalise. Where is it done
+ before here, and when not?
+ Not == 5xx resp to MAIL on main-verify
+ */
+ if (!addr->message) addr->message =
+ string_sprintf("response to \"%s\" was: %s",
+ big_buffer, string_printing(sx->buffer));
+
+ /* RFC 5321 section 4.2: the text portion of the response may have only
+ HT, SP, Printable US-ASCII. Deal with awkward chars by cutting the
+ received message off before passing it onward. Newlines are ok; they
+ just become a multiline response (but wrapped in the error code we
+ produce). */
+
+ for (uschar * s = sx->buffer;
+ *s && s < sx->buffer + sizeof(sx->buffer);
+ s++)
+ {
+ uschar c = *s;
+ if (c != '\t' && c != '\n' && (c < ' ' || c > '~'))
+ {
+ if (s - sx->buffer < sizeof(sx->buffer) - 12)
+ memcpy(s, "(truncated)", 12);
+ else
+ *s = '\0';
+ break;
+ }
+ }
+ addr->user_message = options & vopt_is_recipient
+ ? string_sprintf("Callout verification failed:\n%s", sx->buffer)
+ : string_sprintf("Called: %s\nSent: %s\nResponse: %s",
+ host->address, big_buffer, sx->buffer);
+
+ /* Hard rejection ends the process */
+
+ if (sx->buffer[0] == '5') /* Address rejected */
+ {
+ yield = FAIL;
+ done = TRUE;
+ }
+ break;
+ }
+
+ /* End the SMTP conversation and close the connection. */
+
+ /* Cutthrough - on a successful connect and recipient-verify with
+ use-sender and we are 1st rcpt and have no cutthrough conn so far
+ here is where we want to leave the conn open. Ditto for a lazy-close
+ verify. */
+
+ 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.dkim_domain)
+ {
+ cutthrough.delivery= FALSE;
+ HDEBUG(D_acl|D_v) debug_printf("Cutthrough cancelled by presence of DKIM signing\n");
+ }
+#endif
+#ifdef EXPERIMENTAL_ARC
+ if (ob->arc_sign)
+ {
+ cutthrough.delivery= FALSE;
+ HDEBUG(D_acl|D_v) debug_printf("Cutthrough cancelled by presence of ARC signing\n");
+ }
+#endif
+ }
+
+ if ( (cutthrough.delivery || options & vopt_callout_hold)
+ && rcpt_count == 1
+ && done
+ && yield == OK
+ && (options & (vopt_callout_recipsender|vopt_callout_recippmaster|vopt_success_on_redirect))
+ == vopt_callout_recipsender
+ && !random_local_part
+ && !pm_mailfrom
+ && cutthrough.cctx.sock < 0
+ && !sx->lmtp
+ )
+ {
+ HDEBUG(D_acl|D_v) debug_printf_indent("holding verify callout open for %s\n",
+ cutthrough.delivery
+ ? "cutthrough delivery" : "potential further verifies and delivery");
+
+ cutthrough.callout_hold_only = !cutthrough.delivery;
+ cutthrough.is_tls = tls_out.active.sock >= 0;
+ /* We assume no buffer in use in the outblock */
+ cutthrough.cctx = sx->cctx;
+ cutthrough.nrcpt = 1;
+ cutthrough.transport = addr->transport->name;
+ cutthrough.interface = interface;
+ cutthrough.snd_port = sending_port;
+ cutthrough.peer_options = smtp_peer_options;
+ cutthrough.host = *host;
+ {
+ int oldpool = store_pool;
+ store_pool = POOL_PERM;
+ cutthrough.snd_ip = string_copy(sending_ip_address);
+ cutthrough.host.name = string_copy(host->name);
+ cutthrough.host.address = string_copy(host->address);
+ store_pool = oldpool;
+ }
+
+ /* Save the address_item and parent chain for later logging */
+ cutthrough.addr = *addr;
+ cutthrough.addr.next = NULL;
+ cutthrough.addr.host_used = &cutthrough.host;
+ for (address_item * caddr = &cutthrough.addr, * parent = addr->parent;
+ parent;
+ caddr = caddr->parent, parent = parent->parent)
+ *(caddr->parent = store_get(sizeof(address_item), GET_UNTAINTED)) = *parent;
+
+ ctctx.outblock.buffer = ctbuffer;
+ ctctx.outblock.buffersize = sizeof(ctbuffer);
+ ctctx.outblock.ptr = ctbuffer;
+ /* ctctx.outblock.cmd_count = 0; ctctx.outblock.authenticating = FALSE; */
+ ctctx.outblock.cctx = &cutthrough.cctx;
+ }
+ else
+ {
+ /* Ensure no cutthrough on multiple verifies that were incompatible */
+ if (options & vopt_callout_recipsender)
+ cancel_cutthrough_connection(TRUE, US"not usable for cutthrough");
+ if (sx->send_quit)
+ if (smtp_write_command(sx, SCMD_FLUSH, "QUIT\r\n") != -1)
+ /* Wait a short time for response, and discard it */
+ smtp_read_response(sx, sx->buffer, sizeof(sx->buffer), '2', 1);
+
+ if (sx->cctx.sock >= 0)
+ {
+#ifndef DISABLE_TLS
+ if (sx->cctx.tls_ctx)
+ {
+ tls_close(sx->cctx.tls_ctx, TLS_SHUTDOWN_NOWAIT);
+ sx->cctx.tls_ctx = NULL;
+ }
+#endif
+ HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" SMTP(close)>>\n");
+ (void)close(sx->cctx.sock);
+ sx->cctx.sock = -1;
+ smtp_debug_cmd_report();
+#ifndef DISABLE_EVENT
+ (void) event_raise(addr->transport->event_action, US"tcp:close", NULL, NULL);
+#endif
+ }
+ }
+
+ if (!done || yield != OK)
+ addr->message = string_sprintf("%s [%s] : %s", host->name, host->address,
+ addr->message);
+ } /* 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. */
+
+if (!(options & vopt_callout_no_cache))
+ cache_callout_write(&new_domain_record, addr->domain,
+ done, &new_address_record, address_key);
+
+/* 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. */
+
+if (!done)
+ {
+ uschar * dullmsg = string_sprintf("Could not complete %s verify callout",
+ options & vopt_is_recipient ? "recipient" : "sender");
+ yield = DEFER;
+
+ addr->message = host_list->next || !addr->message
+ ? dullmsg : string_sprintf("%s: %s", dullmsg, addr->message);
+
+ addr->user_message = smtp_return_error_details
+ ? 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,
+ options & vopt_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.")
+ : dullmsg;
+
+ /* Force a specific error code */
+
+ addr->basic_errno = ERRNO_CALLOUTDEFER;
+ }
+
+/* Come here from within the cache-reading code on fast-track exit. */
+
+END_CALLOUT:
+tls_modify_variables(&tls_in); /* return variables to inbound values */
+return yield;
+}
+
+
+
+/* Called after recipient-acl to get a cutthrough connection open when
+ one was requested and a recipient-verify wasn't subsequently done.
+*/
+int
+open_cutthrough_connection(address_item * addr)
+{
+address_item addr2;
+int rc;
+
+/* 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_indent("----------- %s cutthrough setup ------------\n",
+ rcpt_count > 1 ? "more" : "start");
+rc = verify_address(&addr2, NULL,
+ vopt_is_recipient | vopt_callout_recipsender | vopt_callout_no_cache,
+ CUTTHROUGH_CMD_TIMEOUT, -1, -1,
+ NULL, NULL, NULL);
+addr->message = addr2.message;
+addr->user_message = addr2.user_message;
+HDEBUG(D_acl) debug_printf_indent("----------- end cutthrough setup ------------\n");
+return rc;
+}
+
+
+
+/* Send given number of bytes from the buffer */
+static BOOL
+cutthrough_send(int n)
+{
+if(cutthrough.cctx.sock < 0)
+ return TRUE;
+
+if(
+#ifndef DISABLE_TLS
+ cutthrough.is_tls
+ ? tls_write(cutthrough.cctx.tls_ctx, ctctx.outblock.buffer, n, FALSE)
+ :
+#endif
+ send(cutthrough.cctx.sock, ctctx.outblock.buffer, n, 0) > 0
+ )
+{
+ transport_count += n;
+ ctctx.outblock.ptr= ctctx.outblock.buffer;
+ return TRUE;
+}
+
+HDEBUG(D_transport|D_acl) debug_printf_indent("cutthrough_send failed: %s\n", strerror(errno));
+return FALSE;
+}
+
+
+
+static BOOL
+_cutthrough_puts(uschar * cp, int n)
+{
+while(n--)
+ {
+ if(ctctx.outblock.ptr >= ctctx.outblock.buffer+ctctx.outblock.buffersize)
+ if(!cutthrough_send(ctctx.outblock.buffersize))
+ return FALSE;
+
+ *ctctx.outblock.ptr++ = *cp++;
+ }
+return TRUE;
+}
+
+/* Buffered output of counted data block. Return boolean success */
+static BOOL
+cutthrough_puts(uschar * cp, int n)
+{
+if (cutthrough.cctx.sock < 0) return TRUE;
+if (_cutthrough_puts(cp, n)) return TRUE;
+cancel_cutthrough_connection(TRUE, US"transmit failed");
+return FALSE;
+}
+
+void
+cutthrough_data_puts(uschar * cp, int n)
+{
+if (cutthrough.delivery) (void) cutthrough_puts(cp, n);
+return;
+}
+
+
+static BOOL
+_cutthrough_flush_send(void)
+{
+int n = ctctx.outblock.ptr - ctctx.outblock.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(TRUE, US"transmit failed");
+return FALSE;
+}
+
+
+static BOOL
+cutthrough_put_nl(void)
+{
+return cutthrough_puts(US"\r\n", 2);
+}
+
+
+void
+cutthrough_data_put_nl(void)
+{
+cutthrough_data_puts(US"\r\n", 2);
+}
+
+
+/* Get and check response from cutthrough target */
+static uschar
+cutthrough_response(client_conn_ctx * cctx, char expect, uschar ** copy, int timeout)
+{
+smtp_context sx = {0};
+uschar inbuffer[4096];
+uschar responsebuffer[4096];
+
+sx.inblock.buffer = inbuffer;
+sx.inblock.buffersize = sizeof(inbuffer);
+sx.inblock.ptr = inbuffer;
+sx.inblock.ptrend = inbuffer;
+sx.inblock.cctx = cctx;
+if(!smtp_read_response(&sx, responsebuffer, sizeof(responsebuffer), expect, timeout))
+ cancel_cutthrough_connection(TRUE, US"target timeout on read");
+
+if(copy)
+ {
+ 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.cctx.sock < 0 || cutthrough.callout_hold_only)
+ return FALSE;
+
+smtp_debug_cmd(US"DATA", 0);
+cutthrough_puts(US"DATA\r\n", 6);
+cutthrough_flush_send();
+
+/* Assume nothing buffered. If it was it gets ignored. */
+return cutthrough_response(&cutthrough.cctx, '3', NULL, CUTTHROUGH_DATA_TIMEOUT) == '3';
+}
+
+
+/* tctx arg only to match write_chunk() */
+static BOOL
+cutthrough_write_chunk(transport_ctx * tctx, uschar * s, int len)
+{
+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)
+{
+transport_ctx tctx;
+
+if(cutthrough.cctx.sock < 0 || cutthrough.callout_hold_only)
+ 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_indent("----------- start cutthrough headers send -----------\n");
+
+tctx.u.fd = cutthrough.cctx.sock;
+tctx.tblock = cutthrough.addr.transport;
+tctx.addr = &cutthrough.addr;
+tctx.check_string = US".";
+tctx.escape_string = US"..";
+/*XXX check under spool_files_wireformat. Might be irrelevant */
+tctx.options = topt_use_crlf;
+
+if (!transport_headers_send(&tctx, &cutthrough_write_chunk))
+ return FALSE;
+
+HDEBUG(D_acl) debug_printf_indent("----------- done cutthrough headers send ------------\n");
+return TRUE;
+}
+
+
+static void
+close_cutthrough_connection(const uschar * why)
+{
+int fd = cutthrough.cctx.sock;
+if(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.
+ */
+ client_conn_ctx tmp_ctx = cutthrough.cctx;
+ ctctx.outblock.ptr = ctbuffer;
+ smtp_debug_cmd(US"QUIT", 0);
+ _cutthrough_puts(US"QUIT\r\n", 6); /* avoid recursion */
+ _cutthrough_flush_send();
+ cutthrough.cctx.sock = -1; /* avoid recursion via read timeout */
+ cutthrough.nrcpt = 0; /* permit re-cutthrough on subsequent message */
+
+ /* Wait a short time for response, and discard it */
+ cutthrough_response(&tmp_ctx, '2', NULL, 1);
+
+#ifndef DISABLE_TLS
+ if (cutthrough.is_tls)
+ {
+ tls_close(cutthrough.cctx.tls_ctx, TLS_SHUTDOWN_NOWAIT);
+ cutthrough.cctx.tls_ctx = NULL;
+ cutthrough.is_tls = FALSE;
+ }
+#endif
+ HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" SMTP(close)>>\n");
+ (void)close(fd);
+ smtp_debug_cmd_report();
+ HDEBUG(D_acl) debug_printf_indent("----------- cutthrough shutdown (%s) ------------\n", why);
+ }
+ctctx.outblock.ptr = ctbuffer;
+}
+
+void
+cancel_cutthrough_connection(BOOL close_noncutthrough_verifies, const uschar * why)
+{
+if (cutthrough.delivery || close_noncutthrough_verifies)
+ close_cutthrough_connection(why);
+cutthrough.delivery = cutthrough.callout_hold_only = FALSE;
+}
+
+
+void
+release_cutthrough_connection(const uschar * why)
+{
+if (cutthrough.cctx.sock < 0) return;
+HDEBUG(D_acl) debug_printf_indent("release cutthrough conn: %s\n", why);
+cutthrough.cctx.sock = -1;
+cutthrough.cctx.tls_ctx = NULL;
+cutthrough.delivery = cutthrough.callout_hold_only = 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)
+{
+uschar res;
+HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" SMTP>> .\n");
+
+/* Assume data finshed with new-line */
+if( !cutthrough_puts(US".", 1)
+ || !cutthrough_put_nl()
+ || !cutthrough_flush_send()
+ )
+ return cutthrough.addr.message;
+
+res = cutthrough_response(&cutthrough.cctx, '2', &cutthrough.addr.message,
+ CUTTHROUGH_DATA_TIMEOUT);
+for (address_item * addr = &cutthrough.addr; addr; addr = addr->next)
+ {
+ addr->message = cutthrough.addr.message;
+ switch(res)
+ {
+ case '2':
+ delivery_log(LOG_MAIN, addr, (int)'>', NULL);
+ close_cutthrough_connection(US"delivered");
+ break;
+
+ case '4':
+ delivery_log(LOG_MAIN, addr, 0,
+ US"tmp-reject from cutthrough after DATA:");
+ break;
+
+ case '5':
+ delivery_log(LOG_MAIN|LOG_REJECT, 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->prop.address_data = addr->prop.address_data;
+ vaddr->prop.variables = NULL;
+ tree_dup((tree_node **)&vaddr->prop.variables, addr->prop.variables);
+ 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, FALSE, 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
+ fp 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 * fp, int options, int callout,
+ int callout_overall, int callout_connect, uschar * se_mailfrom,
+ uschar *pm_mailfrom, BOOL *routed)
+{
+BOOL allok = TRUE;
+BOOL full_info = fp ? debug_selector != 0 : FALSE;
+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 :
+ f.address_test_mode ? v_none :
+ options & vopt_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 = options & vopt_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))
+ {
+ if (fp)
+ respond_printf(fp, "%sA domain is required for \"%s\"%s\n",
+ ko_prefix, address, cr);
+ *failure_ptr = US"qualify";
+ return FAIL;
+ }
+ /* deconst ok as address was not const */
+ address = US rewrite_address_qualify(address, options & vopt_is_recipient);
+ }
+
+DEBUG(D_verify)
+ {
+ debug_printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+ debug_printf("%s %s\n", f.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)
+ {
+ uschar *old = address;
+ /* deconst ok as address was not const */
+ address = US rewrite_address(address, options & vopt_is_recipient, FALSE,
+ global_rewrite_rules, rewrite_existflags);
+ if (address != old)
+ {
+ for (int i = 0; i < (MAX_NAMED_LIST * 2)/32; i++) vaddr->localpart_cache[i] = 0;
+ for (int i = 0; i < (MAX_NAMED_LIST * 2)/32; i++) vaddr->domain_cache[i] = 0;
+ if (fp && !expn) fprintf(fp, "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)))
+ 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]) 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 (so no returns allowed from here on). */
+
+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;
+
+/* Observability variable for router/transport use */
+
+verify_mode = options & vopt_is_recipient ? US"R" : US"S";
+
+/* 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)
+ {
+ 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 (fp)
+ {
+ BOOL allow;
+
+ if (addr->address[0] == '>')
+ {
+ allow = testflag(addr, af_allow_reply);
+ fprintf(fp, "%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(fp, "%s -> %s", addr->parent->address, addr->address);
+ }
+
+ if (addr->basic_errno == ERRNO_BADTRANSPORT)
+ fprintf(fp, "\n*** Error in setting up pipe, file, or autoreply:\n"
+ "%s\n", addr->message);
+ else if (allow)
+ fprintf(fp, "\n transport = %s\n", addr->transport->name);
+ else
+ fprintf(fp, " *** forbidden ***\n");
+ }
+ continue;
+ }
+
+ /* Just in case some router parameter refers to it. */
+
+ return_path = addr->prop.errors_address
+ ? addr->prop.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) *routed = FALSE;
+ if ((rc = deliver_split_address(addr)) == OK)
+ {
+ if (!(options & vopt_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)
+ {
+ BOOL local_verify = FALSE;
+
+ if (routed) *routed = TRUE;
+ if (callout > 0)
+ {
+ transport_instance * tp;
+ 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 = {
+ .interface = NULL, /* interface (=> any) */
+ .port = US"smtp",
+ .protocol = US"smtp",
+ .hosts = NULL,
+ .helo_data = US"$smtp_active_hostname",
+ .hosts_override = FALSE,
+ .hosts_randomize = FALSE,
+ .gethostbyname = FALSE,
+ .qualify_single = TRUE,
+ .search_parents = FALSE
+ };
+
+ /* 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 ((tp = addr->transport))
+ if (!tp->info->local)
+ {
+ (void)(tp->setup)(tp, 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 && (!host_list || tf.hosts_override))
+ {
+ uschar *s;
+ const 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)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand list of hosts "
+ "\"%s\" in %s transport for callout: %s", tf.hosts,
+ tp->name, expand_string_message);
+ }
+ else
+ {
+ int flags;
+ 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 | HOST_FIND_BY_AAAA;
+ if (tf.qualify_single) flags |= HOST_FIND_QUALIFY_SINGLE;
+ if (tf.search_parents) flags |= HOST_FIND_SEARCH_PARENTS;
+
+ for (host_item * host = host_list, * nexthost; host; host = nexthost)
+ {
+ nexthost = host->next;
+ if (tf.gethostbyname ||
+ string_is_ip_address(host->name, NULL) != 0)
+ (void)host_find_byname(host, NULL, flags, NULL, TRUE);
+ else
+ {
+ const dnssec_domains * dsp = NULL;
+ if (Ustrcmp(tp->driver_name, "smtp") == 0)
+ {
+ smtp_transport_options_block * ob =
+ (smtp_transport_options_block *) tp->options_block;
+ dsp = &ob->dnssec;
+ }
+
+ (void) host_find_bydns(host, NULL, flags, NULL, NULL, NULL,
+ dsp, NULL, NULL);
+ }
+ }
+ }
+ }
+ }
+ else if ( options & vopt_quota
+ && Ustrcmp(tp->driver_name, "appendfile") == 0)
+ local_verify = TRUE;
+
+ /* 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)
+ {
+ HDEBUG(D_verify) debug_printf("Attempting full verification using callout\n");
+ if (host_checking && !f.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
+ {
+#ifndef DISABLE_TLS
+ deliver_set_expansions(addr);
+#endif
+ rc = do_callout(addr, host_list, &tf, callout, callout_overall,
+ callout_connect, options, se_mailfrom, pm_mailfrom);
+#ifndef DISABLE_TLS
+ deliver_set_expansions(NULL);
+#endif
+ if ( options & vopt_is_recipient
+ && rc == OK
+ /* set to "random", with OK, for an accepted random */
+ && !recipient_verify_failure
+ )
+ callout_verified_rcpt(addr);
+ }
+ }
+ else if (local_verify)
+ {
+ HDEBUG(D_verify) debug_printf("Attempting quota verification\n");
+
+ deliver_set_expansions(addr);
+ deliver_local(addr, TRUE);
+ rc = addr->transport_return;
+ }
+ else
+ HDEBUG(D_verify) debug_printf("Cannot do callout: neither router nor "
+ "transport provided a host list, or transport is not smtp\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 (fp)
+ {
+ address_item *p = addr->parent;
+
+ respond_printf(fp, "%s%s %s", ko_prefix,
+ full_info ? addr->address : address,
+ f.address_test_mode ? "is undeliverable" : "failed to verify");
+ if (!expn && f.admin_user)
+ {
+ if (addr->basic_errno > 0)
+ respond_printf(fp, ": %s", strerror(addr->basic_errno));
+ if (addr->message)
+ respond_printf(fp, ": %s", addr->message);
+ }
+
+ /* Show parents iff doing full info */
+
+ if (full_info) while (p)
+ {
+ respond_printf(fp, "%s\n <-- %s", cr, p->address);
+ p = p->parent;
+ }
+ respond_printf(fp, "%s\n", cr);
+ }
+ cancel_cutthrough_connection(TRUE, US"routing hard fail");
+
+ if (!full_info)
+ {
+ yield = copy_error(vaddr, addr, FAIL);
+ goto out;
+ }
+ yield = FAIL;
+ }
+
+ /* Soft failure */
+
+ else if (rc == DEFER)
+ {
+ allok = FALSE;
+ if (fp)
+ {
+ address_item *p = addr->parent;
+ respond_printf(fp, "%s%s cannot be resolved at this time", ko_prefix,
+ full_info? addr->address : address);
+ if (!expn && f.admin_user)
+ {
+ if (addr->basic_errno > 0)
+ respond_printf(fp, ": %s", strerror(addr->basic_errno));
+ if (addr->message)
+ respond_printf(fp, ": %s", addr->message);
+ else if (addr->basic_errno <= 0)
+ respond_printf(fp, ": unknown error");
+ }
+
+ /* Show parents iff doing full info */
+
+ if (full_info) while (p)
+ {
+ respond_printf(fp, "%s\n <-- %s", cr, p->address);
+ p = p->parent;
+ }
+ respond_printf(fp, "%s\n", cr);
+ }
+ cancel_cutthrough_connection(TRUE, US"routing soft fail");
+
+ if (!full_info)
+ {
+ yield = copy_error(vaddr, addr, DEFER);
+ goto out;
+ }
+ 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)
+ if (!addr_local && !addr_remote)
+ respond_printf(fp, "250 mail to <%s> is discarded\r\n", address);
+ else
+ respond_printf(fp, "250 <%s>\r\n", address);
+
+ else do
+ {
+ address_item *addr2 = addr_new;
+ addr_new = addr2->next;
+ if (!addr_new) ok_prefix = US"250 ";
+ respond_printf(fp, "%s<%s>\r\n", ok_prefix, addr2->address);
+ } while (addr_new);
+ 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 /* No new address OR */
+ || addr_new->next /* More than one new address OR */
+ || testflag(addr_new, af_pfr) /* New address is pfr */
+ )
+ || /* OR */
+ ( addr_new /* At least one new address AND */
+ && success_on_redirect /* success_on_redirect is set */
+ ) )
+ )
+ {
+ if (fp) fprintf(fp, "%s %s\n",
+ address, f.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->prop.address_data = addr->prop.address_data;
+ vaddr->prop.variables = NULL;
+ tree_dup((tree_node **)&vaddr->prop.variables, addr->prop.variables);
+
+ /* If stopped because more than one new address, cannot cutthrough */
+
+ if (addr_new && addr_new->next)
+ cancel_cutthrough_connection(TRUE, US"multiple addresses from routing");
+
+ 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 fp 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 && !addr_remote)
+ {
+ fprintf(fp, "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)
+ {
+ address_item *addr = addr_list;
+ transport_instance * tp = addr->transport;
+
+ addr_list = addr->next;
+
+ fprintf(fp, "%s", CS addr->address);
+
+ /* 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)))
+ fprintf(fp, " [duplicate, would not be delivered]");
+ else tree_add_duplicate(addr->unique, addr);
+ }
+
+ /* Now show its parents */
+
+ for (address_item * p = addr->parent; p; p = p->parent)
+ fprintf(fp, "\n <-- %s", p->address);
+ fprintf(fp, "\n ");
+
+ /* Show router, and transport */
+
+ fprintf(fp, "router = %s, transport = %s\n",
+ addr->router->name, tp ? tp->name : US"unset");
+
+ /* 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 && tp && !tp->overrides_hosts)
+ {
+ int maxlen = 0;
+ int maxaddlen = 0;
+ for (host_item * h = addr->host_list; h; h = h->next)
+ { /* get max lengths of host names, addrs */
+ int len = Ustrlen(h->name);
+ if (len > maxlen) maxlen = len;
+ len = h->address ? Ustrlen(h->address) : 7;
+ if (len > maxaddlen) maxaddlen = len;
+ }
+ for (host_item * h = addr->host_list; h; h = h->next)
+ {
+ fprintf(fp, " host %-*s ", maxlen, h->name);
+
+ if (h->address)
+ fprintf(fp, "[%s%-*c", h->address, maxaddlen+1 - Ustrlen(h->address), ']');
+ else if (tp->info->local)
+ fprintf(fp, " %-*s ", maxaddlen, ""); /* Omit [unknown] for local */
+ else
+ fprintf(fp, "[%s%-*c", "unknown", maxaddlen+1 - 7, ']');
+
+ if (h->mx >= 0) fprintf(fp, " MX=%d", h->mx);
+ if (h->port != PORT_NONE) fprintf(fp, " port=%d", h->port);
+ if (f.running_in_test_harness && h->dnssec == DS_YES) fputs(" AD", fp);
+ if (h->status == hstatus_unusable) fputs(" ** unusable **", fp);
+ fputc('\n', fp);
+ }
+ }
+ }
+
+/* Yield will be DEFER or FAIL if any one address has, only for full_info (which is
+the -bv or -bt case). */
+
+out:
+verify_mode = NULL;
+tls_modify_variables(&tls_in); /* return variables to inbound values */
+
+return yield;
+}
+
+
+
+
+/*************************************************
+* Check headers for syntax errors *
+*************************************************/
+
+/* This function checks those header lines that contain addresses, and verifies
+that all the addresses therein are 5322-syntactially correct.
+
+Arguments:
+ msgptr where to put an error message
+
+Returns: OK
+ FAIL
+*/
+
+int
+verify_check_headers(uschar **msgptr)
+{
+uschar *colon, *s;
+int yield = OK;
+
+for (header_line * h = header_list; h && 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;
+ Uskip_whitespace(&s);
+
+ /* Loop for multiple addresses in the header, enabling group syntax. Note
+ that we have to reset this after the header has been scanned. */
+
+ f.parse_allow_group = TRUE;
+
+ while (*s)
+ {
+ 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 && !domain)
+ {
+ if (h->type == htype_from || h->type == htype_sender)
+ {
+ if (!f.allow_unqualified_sender) recipient = NULL;
+ }
+ else
+ {
+ if (!f.allow_unqualified_recipient) recipient = NULL;
+ }
+ if (!recipient) 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 && 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";
+ }
+
+ /* deconst cast ok as we're passing a non-const to string_printing() */
+ *msgptr = US string_printing(
+ string_sprintf("%s: failing address in \"%.*s:\" header %s: %.*s",
+ errmess, (int)(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);
+ Uskip_whitespace(&s);
+ } /* Next address */
+
+ f.parse_allow_group = FALSE;
+ f.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 characters 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)
+{
+uschar *colon;
+
+for (header_line * h = header_list; h; h = h->next)
+ {
+ colon = Ustrchr(h->text, ':');
+ for(uschar * s = h->text; s < colon; s++)
+ if ((*s < 33) || (*s > 126))
+ {
+ *msgptr = string_sprintf("Invalid character in header \"%.*s\" found",
+ (int)(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: case_sensitive true if case sensitive matching should be used
+Returns: OK if there are no blind recipients
+ FAIL if there is at least one blind recipient
+*/
+
+int
+verify_check_notblind(BOOL case_sensitive)
+{
+for (int i = 0; i < recipients_count; i++)
+ {
+ BOOL found = FALSE;
+ uschar *address = recipients_list[i].address;
+
+ for (header_line * h = header_list; !found && h; h = h->next)
+ {
+ uschar *colon, *s;
+
+ if (h->type != htype_to && h->type != htype_cc) continue;
+
+ colon = Ustrchr(h->text, ':');
+ s = colon + 1;
+ Uskip_whitespace(&s);
+
+ /* Loop for multiple addresses in the header, enabling group syntax. Note
+ that we have to reset this after the header has been scanned. */
+
+ f.parse_allow_group = TRUE;
+
+ while (*s)
+ {
+ 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 with case-sensitivity
+ according to the routine arg, 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 && domain != 0)
+ if ((found = (case_sensitive
+ ? Ustrncmp(recipient, address, domain) == 0
+ : strncmpic(recipient, address, domain) == 0)
+ && strcmpic(recipient + domain, address + domain) == 0))
+ break;
+
+ /* Advance to the next address */
+
+ s = ss + (terminator ? 1:0);
+ Uskip_whitespace(&s);
+ } /* Next address */
+
+ f.parse_allow_group = FALSE;
+ f.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)
+{
+for (address_item * addr = sender_verified_list; addr; addr = addr->next)
+ if (Ustrcmp(sender, addr->address) == 0) return addr;
+return NULL;
+}
+
+
+
+
+
+/*************************************************
+* 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;
+
+for (int i = 0; i < 3 && !done; i++)
+ for (header_line * 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. */
+
+ f.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 && 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)
+ {
+ 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\"",
+ (int)(endname - h->text), h->text, *log_msgptr, (int)(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",
+ (int)(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 */
+
+ f.parse_allow_group = FALSE;
+ f.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)
+{
+client_conn_ctx ident_conn_ctx = {0};
+int host_af, qlen;
+int received_sender_port, received_interface_port, n;
+uschar *p;
+blob early_data;
+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;
+if ((ident_conn_ctx.sock = ip_socket(SOCK_STREAM, host_af)) < 0) return;
+
+if (ip_bind(ident_conn_ctx.sock, host_af, interface_address, 0) < 0)
+ {
+ DEBUG(D_ident) debug_printf("bind socket for ident failed: %s\n",
+ strerror(errno));
+ goto END_OFF;
+ }
+
+/* Construct and send the query. */
+
+qlen = snprintf(CS buffer, sizeof(buffer), "%d , %d\r\n",
+ sender_host_port, interface_port);
+early_data.data = buffer;
+early_data.len = qlen;
+
+/*XXX we trust that the query is idempotent */
+if (ip_connect(ident_conn_ctx.sock, host_af, sender_host_address, port,
+ rfc1413_query_timeout, &early_data) < 0)
+ {
+ if (errno == ETIMEDOUT && LOGGING(ident_timeout))
+ 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;
+ }
+
+/* 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(&ident_conn_ctx, p, size, time(NULL) + 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. The deconst cast is ok as we fed a nonconst to string_printing() */
+
+sender_ident = US string_printing(string_copyn(p, 127));
+DEBUG(D_ident) debug_printf("sender_ident = %s\n", sender_ident);
+
+END_OFF:
+(void)close(ident_conn_ctx.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, const uschar * ss, const uschar ** valueptr, uschar ** error)
+{
+check_host_block * cb = (check_host_block *)arg;
+int mlen = -1;
+int maskoffset;
+BOOL iplookup = FALSE, isquery = FALSE;
+BOOL isiponly = cb->host_name && !cb->host_name[0];
+const uschar * t;
+uschar * semicolon, * endname, * opts;
+uschar ** aliases;
+
+/* Optimize for the special case when the pattern is "*". */
+
+if (*ss == '*' && !ss[1]) 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]) return *ss ? FAIL : OK;
+if (!*ss) 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)
+ {
+ for (ip_address_item * ip = host_find_interfaces(); ip; 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) comparison 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 || (*t == '/' && t != ss))
+ {
+ *error = US"malformed IPv4 address or address mask";
+ return ERROR;
+ }
+
+/* See if there is a semicolon in the pattern, separating a searchtype
+prefix. If there is one then check for comma-sep options. */
+
+if ((semicolon = Ustrchr(ss, ';')))
+ if ((opts = Ustrchr(ss, ',')) && opts < semicolon)
+ {
+ endname = opts++;
+ opts = string_copyn(opts, semicolon - opts);
+ }
+ else
+ {
+ endname = semicolon;
+ opts = NULL;
+ }
+
+/* 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)
+ {
+ 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, endname - 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. */
+
+ if (!(handle = search_open(filename, search_type, 0, NULL, NULL)))
+ log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s", search_error_message);
+
+ result = search_find(handle, filename, key, -1, NULL, 0, 0, NULL, opts);
+ if (valueptr) *valueptr = result;
+ return result ? OK : f.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;
+
+ /* Using byname rather than bydns here means we cannot determine dnssec
+ status. On the other hand it is unclear how that could be either
+ propagated up or enforced. */
+
+ rc = host_find_byname(&h, NULL, HOST_FIND_QUALIFY_SINGLE, NULL, FALSE);
+ if (rc == HOST_FOUND || rc == HOST_FOUND_LOCAL)
+ {
+ for (host_item * hh = &h; hh; 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) /* 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, ';')))
+ {
+ const uschar * affix, * opts;
+ int partial, affixlen, starflags, id;
+
+ *semicolon = 0;
+ id = search_findtype_partial(ss, &partial, &affix, &affixlen, &starflags,
+ &opts);
+ *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)
+ {
+ 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)
+ 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(const uschar **listptr, unsigned int *cache_bits,
+ const uschar *host_name, const uschar *host_address, const uschar **valueptr)
+{
+int rc;
+unsigned int *local_cache_bits = cache_bits;
+const uschar *save_host_address = deliver_host_address;
+check_host_block cb = { .host_name = host_name, .host_address = host_address };
+
+if (valueptr) *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 given host item matches a list *
+*************************************************/
+int
+verify_check_given_host(const uschar **listptr, const host_item *host)
+{
+return verify_check_this_host(listptr, NULL, host->name, host->address, NULL);
+}
+
+/*************************************************
+* 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(CUSS listptr, sender_host_cache, NULL,
+ sender_host_address ? sender_host_address : US"", 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 x = bin[0];
+ for (int 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
+ for (int j = 3; j >= 0; j--)
+ {
+ int x = bin[j];
+ for (int 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;
+}
+
+
+
+/****************************************************
+ Verify a local user account for quota sufficiency
+****************************************************/
+
+/* The real work, done via a re-exec for privs, calls
+down to the transport for the quota check.
+
+Route and transport (in recipient-verify mode) the
+given recipient.
+
+A routing result indicating any transport type other than appendfile
+results in a fail.
+
+Return, on stdout, a result string containing:
+- highlevel result code (OK, DEFER, FAIL)
+- errno
+- where string
+- message string
+*/
+
+void
+verify_quota(uschar * address)
+{
+address_item vaddr = {.address = address};
+BOOL routed;
+uschar * msg = US"\0";
+int rc, len = 1;
+
+if ((rc = verify_address(&vaddr, NULL, vopt_is_recipient | vopt_quota,
+ 1, 0, 0, NULL, NULL, &routed)) != OK)
+ {
+ uschar * where = recipient_verify_failure;
+ msg = acl_verify_message ? acl_verify_message : vaddr.message;
+ if (!msg) msg = US"";
+ if (rc == DEFER && vaddr.basic_errno == ERRNO_EXIMQUOTA)
+ {
+ rc = FAIL; /* DEFER -> FAIL */
+ where = US"quota";
+ vaddr.basic_errno = 0;
+ }
+ else if (!where) where = US"";
+
+ len = 5 + Ustrlen(msg) + 1 + Ustrlen(where);
+ msg = string_sprintf("%c%c%c%c%c%s%c%s", (uschar)rc,
+ (vaddr.basic_errno >> 24) & 0xff, (vaddr.basic_errno >> 16) & 0xff,
+ (vaddr.basic_errno >> 8) & 0xff, vaddr.basic_errno & 0xff,
+ where, '\0', msg);
+ }
+
+DEBUG(D_verify) debug_printf_indent("verify_quota: len %d\n", len);
+write(1, msg, len);
+return;
+}
+
+
+/******************************************************************************/
+
+/* Quota cache lookup. We use the callout hints db also for the quota cache.
+Return TRUE if a nonexpired record was found, having filled in the yield
+argument.
+*/
+
+static BOOL
+cached_quota_lookup(const uschar * rcpt, int * yield,
+ int pos_cache, int neg_cache)
+{
+open_db dbblock, *dbm_file = NULL;
+dbdata_callout_cache_address * cache_address_record;
+
+if (!pos_cache && !neg_cache)
+ return FALSE;
+if (!(dbm_file = dbfn_open(US"callout", O_RDWR, &dbblock, FALSE, TRUE)))
+ {
+ HDEBUG(D_verify) debug_printf_indent("quota cache: not available\n");
+ return FALSE;
+ }
+if (!(cache_address_record = (dbdata_callout_cache_address *)
+ get_callout_cache_record(dbm_file, rcpt, US"address",
+ pos_cache, neg_cache)))
+ {
+ dbfn_close(dbm_file);
+ return FALSE;
+ }
+if (cache_address_record->result == ccache_accept)
+ *yield = OK;
+dbfn_close(dbm_file);
+return TRUE;
+}
+
+/* Quota cache write */
+
+static void
+cache_quota_write(const uschar * rcpt, int yield, int pos_cache, int neg_cache)
+{
+open_db dbblock, *dbm_file = NULL;
+dbdata_callout_cache_address cache_address_record;
+
+if (!pos_cache && !neg_cache)
+ return;
+if (!(dbm_file = dbfn_open(US"callout", O_RDWR|O_CREAT, &dbblock, FALSE, TRUE)))
+ {
+ HDEBUG(D_verify) debug_printf_indent("quota cache: not available\n");
+ return;
+ }
+
+cache_address_record.result = yield == OK ? ccache_accept : ccache_reject;
+
+(void)dbfn_write(dbm_file, rcpt, &cache_address_record,
+ (int)sizeof(dbdata_callout_cache_address));
+HDEBUG(D_verify) debug_printf_indent("wrote %s quota cache record for %s\n",
+ yield == OK ? "positive" : "negative", rcpt);
+
+dbfn_close(dbm_file);
+return;
+}
+
+
+/* To evaluate a local user's quota, starting in ACL, we need to
+fork & exec to regain privileges, to that we can change to the user's
+identity for access to their files.
+
+Arguments:
+ rcpt Recipient account
+ pos_cache Number of seconds to cache a positive result (delivery
+ to be accepted). Zero to disable caching.
+ neg_cache Number of seconds to cache a negative result. Zero to disable.
+ msg Pointer to result string pointer
+
+Return: OK/DEFER/FAIL code
+*/
+
+int
+verify_quota_call(const uschar * rcpt, int pos_cache, int neg_cache,
+ uschar ** msg)
+{
+int pfd[2], pid, save_errno, yield = FAIL;
+void (*oldsignal)(int);
+const uschar * where = US"socketpair";
+
+*msg = NULL;
+
+if (cached_quota_lookup(rcpt, &yield, pos_cache, neg_cache))
+ {
+ HDEBUG(D_verify) debug_printf_indent("quota cache: address record is %s\n",
+ yield == OK ? "positive" : "negative");
+ if (yield != OK)
+ {
+ recipient_verify_failure = US"quota";
+ acl_verify_message = *msg =
+ US"Previous (cached) quota verification failure";
+ }
+ return yield;
+ }
+
+if (pipe(pfd) != 0)
+ goto fail;
+
+where = US"fork";
+oldsignal = signal(SIGCHLD, SIG_DFL);
+if ((pid = exim_fork(US"quota-verify")) < 0)
+ {
+ save_errno = errno;
+ close(pfd[pipe_write]);
+ close(pfd[pipe_read]);
+ errno = save_errno;
+ goto fail;
+ }
+
+if (pid == 0) /* child */
+ {
+ close(pfd[pipe_read]);
+ force_fd(pfd[pipe_write], 1); /* stdout to pipe */
+ close(pfd[pipe_write]);
+ dup2(1, 0);
+ if (debug_fd > 0) force_fd(debug_fd, 2);
+
+ child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 3,
+ US"-MCq", string_sprintf("%d", message_size), rcpt);
+ /*NOTREACHED*/
+ }
+
+save_errno = errno;
+close(pfd[pipe_write]);
+
+if (pid < 0)
+ {
+ DEBUG(D_verify) debug_printf_indent(" fork: %s\n", strerror(save_errno));
+ }
+else
+ {
+ uschar buf[128];
+ int n = read(pfd[pipe_read], buf, sizeof(buf));
+ int status;
+
+ waitpid(pid, &status, 0);
+ if (status == 0)
+ {
+ uschar * s;
+
+ if (n > 0) yield = buf[0];
+ if (n > 4)
+ save_errno = (buf[1] << 24) | (buf[2] << 16) | (buf[3] << 8) | buf[4];
+ if ((recipient_verify_failure = n > 5
+ ? string_copyn_taint(buf+5, n-5, GET_UNTAINTED) : NULL))
+ {
+ int m;
+ s = buf + 5 + Ustrlen(recipient_verify_failure) + 1;
+ m = n - (s - buf);
+ acl_verify_message = *msg =
+ m > 0 ? string_copyn_taint(s, m, GET_UNTAINTED) : NULL;
+ }
+
+ DEBUG(D_verify) debug_printf_indent("verify call response:"
+ " len %d yield %s errno '%s' where '%s' msg '%s'\n",
+ n, rc_names[yield], strerror(save_errno), recipient_verify_failure, *msg);
+
+ if ( yield == OK
+ || save_errno == 0 && Ustrcmp(recipient_verify_failure, "quota") == 0)
+ cache_quota_write(rcpt, yield, pos_cache, neg_cache);
+ else DEBUG(D_verify)
+ debug_printf_indent("result not cacheable\n");
+ }
+ else
+ {
+ DEBUG(D_verify)
+ debug_printf_indent("verify call response: waitpid status 0x%04x\n", status);
+ }
+ }
+
+close(pfd[pipe_read]);
+signal(SIGCHLD, oldsignal);
+errno = save_errno;
+return yield;
+
+fail:
+DEBUG(D_verify) debug_printf_indent("verify_quota_call fail in %s\n", where);
+return yield;
+}
+
+
+/* vi: aw ai sw=2
+*/
+/* End of verify.c */
diff --git a/src/version.c b/src/version.c
new file mode 100644
index 0000000..118ebbd
--- /dev/null
+++ b/src/version.c
@@ -0,0 +1,68 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* Copyright (c) The Exim Maintainers 2010 - 2018 */
+/* 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>>";
+
+#ifdef EXIM_BUILD_DATE_OVERRIDE
+/* Reproducible build support; build tooling should have given us something looking like
+ * "25-Feb-2017 20:15:40" in EXIM_BUILD_DATE_OVERRIDE based on $SOURCE_DATE_EPOCH in environ
+ * per <https://reproducible-builds.org/specs/source-date-epoch/>
+ */
+version_date = date_buffer;
+version_date[0] = 0;
+Ustrncat(version_date, EXIM_BUILD_DATE_OVERRIDE, sizeof(date_buffer));
+
+#else
+Ustrcpy(today, US __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, US" ");
+Ustrcat(version_date, US __TIME__);
+#endif
+}
+
+/* End of version.c */
diff --git a/src/version.h b/src/version.h
new file mode 100644
index 0000000..26e1a81
--- /dev/null
+++ b/src/version.h
@@ -0,0 +1,7 @@
+/* automatically generated file - see ../scripts/reversion */
+#define EXIM_RELEASE_VERSION "4.96"
+#ifdef EXIM_VARIANT_VERSION
+#define EXIM_VERSION_STR EXIM_RELEASE_VERSION "-" EXIM_VARIANT_VERSION
+#else
+#define EXIM_VERSION_STR EXIM_RELEASE_VERSION
+#endif
diff --git a/src/version.sh b/src/version.sh
new file mode 100644
index 0000000..bd8c7f3
--- /dev/null
+++ b/src/version.sh
@@ -0,0 +1,3 @@
+# automatically generated file - see ../scripts/reversion
+EXIM_RELEASE_VERSION="4.96"
+EXIM_COMPILE_NUMBER="1"
diff --git a/util/.gitignore b/util/.gitignore
new file mode 100644
index 0000000..5d49724
--- /dev/null
+++ b/util/.gitignore
@@ -0,0 +1,2 @@
+# Compiled programs:
+gen_pkcs3
diff --git a/util/README b/util/README
new file mode 100644
index 0000000..bff3378
--- /dev/null
+++ b/util/README
@@ -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/chunking_fixqueue_finalnewlines.pl b/util/chunking_fixqueue_finalnewlines.pl
new file mode 100755
index 0000000..5dddfa5
--- /dev/null
+++ b/util/chunking_fixqueue_finalnewlines.pl
@@ -0,0 +1,160 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+BEGIN { pop @INC if $INC[-1] eq '.' };
+
+use Fcntl qw(:DEFAULT :flock :seek);
+use File::Find;
+use File::Spec;
+
+use constant MIN_AGE => 60; # seconds
+my $exim = exists $ENV{'EXIM_BINARY'} ? $ENV{'EXIM_BINARY'} : 'exim';
+
+my %known_okay = map {$_=>1} qw( linux darwin freebsd );
+unless (exists $known_okay{$^O}) {
+ warn "for ease, this perl uses flock, not fcntl, assuming they're the same\n";
+ warn "this is not known by this author to be the case on $^O\n";
+ warn "please investigate and either add to allowed-list in script, or rewrite\n";
+ die "bailing out";
+
+ # Another approach to rewriting script: stop all exim receivers and
+ # queue-runners, prevent them from starting, then add your OS to the list and
+ # run, even though the locking type is wrong, relying upon not actually
+ # contending.
+}
+
+my $spool_dir = `$exim -n -bP spool_directory`;
+chomp $spool_dir;
+
+chdir(File::Spec->catfile($spool_dir, 'input'))
+ or die "chdir($spool_dir/input) failed: $!\n";
+
+my $exim_msgid_r = qr/(?:[0-9A-Za-z]{6}-[0-9A-Za-z]{6}-[0-9A-Za-z]{2})/;
+my $spool_dfile_r = qr/^(($exim_msgid_r)-D)\z/o;
+
+sub fh_ends_newline {
+ my ($fh, $dfn, $verbose) = @_;
+ seek($fh, -1, 2) or do { warn "seek(file($dfn)) failed: $!\n"; return -1 };
+ my $count = read $fh, my $ch, 1;
+ if ($count == -1) { warn "failed to read last byte of $dfn\n"; return -1 };
+ if ($count == 0) { warn "file shrunk by one?? problem with $dfn\n"; return -1 };
+ if ($ch eq "\n") { print "okay!\n" if $verbose; return 1 }
+ print "PROBLEM: $dfn missing final newline (got $ch)\n" if $verbose;
+ return 0;
+}
+
+
+sub each_found_file {
+ return unless $_ =~ $spool_dfile_r;
+ my ($msgid, $dfn) = ($2, $1);
+
+ # We should have already upgraded Exim before invoking us, thus any spool
+ # files will be old and we can reduce spending time trying to lock files
+ # still being written to, etc.
+ my @st = lstat($dfn) or return;
+ if ($^T - $st[9] < MIN_AGE) { return };
+ -f "./${msgid}-H" || return;
+
+ print "consider: $dfn\n";
+ open(my $fh, '+<:raw', $dfn) or do {
+ warn "open($dfn) failed: $!\n";
+ return;
+ };
+ # return with a lexical FH in modern Perl should guarantee close, AIUI
+
+ # we do our first check without a lock, so that we can scan past messages
+ # being handled by Exim quickly, and only lock up on those which Exim is
+ # trying and failing to deliver. However, since Exim will be hung on remote
+ # hosts, this is likely. Thus best to kill queue-runners first.
+
+ return if fh_ends_newline($fh, $dfn, 0); # also returns on error
+ print "Problem? $msgid probably missing newline, locking to be sure ...\n";
+ flock($fh, LOCK_EX) or do { warn "flock(file($dfn)) failed: $!\n"; return };
+ return if fh_ends_newline($fh, $dfn, 1); # also returns on error
+
+ fixup_message($msgid, $dfn, $fh);
+
+ close($fh) or warn "close($dfn) failed: $!\n";
+};
+
+sub fixup_message {
+ my ($msgid, $dfn, $fh) = @_;
+ # we can't freeze the message, our lock stops that, which is good!
+
+ seek($fh, 0, 2) or do { warn "seek(file($dfn)) failed: $!\n"; return -1 };
+
+ my $r = inc_message_header_linecount($msgid);
+ if ($r < 0) {
+ warn "failed to fix message headers in ${msgid}-H so not editing message\n";
+ return;
+ }
+
+ print {$fh} "\n";
+
+ print "${msgid}: added newline\n";
+};
+
+sub inc_message_header_linecount {
+ my ($msgid) = @_;
+ my $name_in = "${msgid}-H";
+ my $name_out = "${msgid}-chunkfix";
+
+ open(my $in, '<:perlio', $name_in) or do { warn "open(${name_in}) failed: $!\n"; return -1 };
+ open(my $out, '>:perlio', $name_out) or do { warn "write-open(${name_out}) failed: $!\n"; return -1 };
+ my $seen = 0;
+ my $lc;
+ foreach (<$in>) {
+ if ($seen) {
+ print {$out} $_;
+ next;
+ }
+ if (/^(-body_linecount\s+)(\d+)(\s*)$/) {
+ $lc = $2 + 1;
+ print {$out} "${1}${lc}${3}";
+ $seen = 1;
+ next;
+ }
+ print {$out} $_;
+ }
+ close($in) or do {
+ warn "read-close(${msgid}-H) failed, assuming incomplete: $!\n";
+ close($out);
+ unlink $name_out;
+ return -1;
+ };
+ close($out) or do {
+ warn "write-close(${msgid}-chunkfix) failed, aborting: $!\n";
+ unlink $name_out;
+ return -1;
+ };
+
+ my @target = stat($name_in) or do { warn "stat($name_in) failed: $!\n"; unlink $name_out; return -1 };
+ my @created = stat($name_out) or do { warn "stat($name_out) failed: $!\n"; unlink $name_out; return -1 };
+ # 4=uid, 5=gid, 2=mode
+ if (($created[5] != $target[5]) or ($created[4] != $target[4])) {
+ chown $target[4], $target[5], $name_out or do {
+ warn "chown($name_out) failed: $!\n";
+ unlink $name_out;
+ return -1;
+ };
+ }
+ if (($created[2]&07777) != ($target[2]&0x7777)) {
+ chmod $target[2]&0x7777, $name_out or do {
+ warn "chmod($name_out) failed: $!\n";
+ unlink $name_out;
+ return -1;
+ };
+ }
+
+ rename $name_out, $name_in or do {
+ warn "rename '${msgid}-chunkfix' -> '${msgid}-H' failed: $!\n";
+ unlink $name_out;
+ return -1;
+ };
+
+ print "${msgid}: linecount set to $lc\n";
+ return 1;
+}
+
+find({wanted => \&each_found_file}, '.');
diff --git a/util/cramtest.pl b/util/cramtest.pl
new file mode 100755
index 0000000..48f989a
--- /dev/null
+++ b/util/cramtest.pl
@@ -0,0 +1,60 @@
+#!/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.
+
+BEGIN { pop @INC if $INC[-1] eq '.' };
+
+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
index 0000000..2fe9a6c
--- /dev/null
+++ b/util/gen_pkcs3.c
@@ -0,0 +1,268 @@
+/* Copyright (C) 2012,2016 Phil Pennock.
+ * Copyright (c) The Exim Maintainers 2021
+ * 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];
+
+ 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 + 1);
+ if (!spaceless)
+ die("malloc(%zu) failed: %s", len + 1, strerror(errno));
+
+ for (p = spaceless, q = text, end = text + len;
+ q < end;
+ ++q) {
+ if (!isspace(*q))
+ *p++ = *q;
+ }
+ len = p - spaceless;
+ *p++ = '\0';
+
+ b = NULL;
+ rc = BN_hex2bn(&b, spaceless);
+
+ if (rc != (int)len)
+ die("BN_hex2bn did not convert entire input; took %d of %zu bytes",
+ rc, len);
+
+ 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\"%s\n", p, (nl == end - 1 ? ";" : ""));
+ p = nl + 1;
+ }
+}
+
+
+void __attribute__((__noreturn__))
+usage(FILE *stream, int exitcode)
+{
+ fprintf(stream, "Usage: %s [-CPcst] <dh_p> <dh_g> [<dh_q>]\n"
+"Both dh_p and dh_g should be hex strings representing the numbers\n"
+"The same applies to the optional dh_q (prime-order subgroup).\n"
+"They may contain whitespace.\n"
+"Older values, dh_g is often just '2', not a long string.\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, *q;
+ 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;
+ bool given_q = 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) || (argc > 4)) {
+ fprintf(stderr, "argc: %d\n", argc);
+ usage(stderr, 1);
+ }
+
+ // If we use DH_set0_pqg instead of setting dh fields directly; the q value
+ // is optional and may be NULL.
+ // Just blank them all.
+ p = g = q = NULL;
+
+ p = bn_from_text(argv[1]);
+ g = bn_from_text(argv[2]);
+ if (argc >= 4) {
+ q = bn_from_text(argv[3]);
+ given_q = true;
+ }
+
+ if (show_numbers) {
+ printf("p = ");
+ BN_print_fp(stdout, p);
+ printf("\ng = ");
+ BN_print_fp(stdout, g);
+ if (given_q) {
+ printf("\nq = ");
+ BN_print_fp(stdout, q);
+ }
+ printf("\n");
+ }
+
+ dh = DH_new();
+ // The documented method for setting q appeared in OpenSSL 1.1.0.
+#if OPENSSL_VERSION_NUMBER >= 0x1010000f
+ // NULL okay for q; yes, the optional value is in the middle.
+ if (DH_set0_pqg(dh, p, q, g) != 1) {
+ die_openssl_err("initialising DH pqg values failed");
+ }
+#else
+ dh->p = p;
+ dh->g = g;
+ if (given_q) {
+ dh->q = q;
+ }
+#endif
+
+ 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 (& q if non-NULL) too */
+ return 0;
+}
diff --git a/util/logargs.sh b/util/logargs.sh
new file mode 100755
index 0000000..87369a6
--- /dev/null
+++ b/util/logargs.sh
@@ -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
index 0000000..691849d
--- /dev/null
+++ b/util/mkcdb.pl
@@ -0,0 +1,93 @@
+#!/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 arbitrary 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;
+
+BEGIN { pop @INC if $INC[-1] eq '.' };
+$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
index 0000000..08ca4cb
--- /dev/null
+++ b/util/ocsp_fetch.pl
@@ -0,0 +1,84 @@
+#!/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;
+BEGIN { pop @INC if $INC[-1] eq '.' };
+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
index 0000000..67a171d
--- /dev/null
+++ b/util/proxy_protocol_client.pl
@@ -0,0 +1,251 @@
+#!/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;
+BEGIN { pop @INC if $INC[-1] eq '.' };
+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
index 0000000..e212fa2
--- /dev/null
+++ b/util/ratelimit.pl
@@ -0,0 +1,159 @@
+#!/usr/bin/perl -wT
+
+use strict;
+
+BEGIN { pop @INC if $INC[-1] eq '.' };
+
+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/renew-opendmarc-tlds.sh b/util/renew-opendmarc-tlds.sh
new file mode 100755
index 0000000..9967018
--- /dev/null
+++ b/util/renew-opendmarc-tlds.sh
@@ -0,0 +1,128 @@
+#!/bin/sh -eu
+#
+# Short version of this script:
+# curl -f -o /var/cache/exim/opendmarc.tlds https://publicsuffix.org/list/public_suffix_list.dat
+# but run as Exim runtime user, writing to a place it can write to, and with
+# sanity checks and atomic replacement.
+#
+# For now, we deliberately leave the invalid file around for analysis
+# with .<pid> suffix.
+#
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~8< cut here >8~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#
+# Create a cron-job as the Exim run-time user to invoke this daily, with a
+# single parameter, 'cron'. Eg:
+#
+# 3 4 * * * /usr/local/sbin/renew-opendmarc-tlds.sh cron
+#
+# That will, at 3 minutes past the 4th hour (in whatever timezone cron is
+# running it) invoke this script with 'cron'; we will then sleep between 10 and
+# 50 seconds, before continuing.
+#
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~8< cut here >8~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#
+# This should be "pretty portable"; the only things it depends upon are:
+# * a POSIX shell which additionally implements 'local' (dash works)
+# * the 'curl' command; change the fetch_candidate() function to replace that
+# * the 'stat' command, to get the size of a file; else Perl
+# + change size_of() if need be; it's defined per-OS
+# * the 'hexdump' command and /dev/urandom existing
+# + used when invoked with 'cron', to avoid retrieving on a minute boundary
+# and contending with many other automated systems.
+# + with bash/zsh, can replace with: $(( 10 + ( RANDOM % 40 ) ))
+# + on Debian/Ubuntu systems, hexdump is in the 'bsdmainutils' package.
+
+# Consider putting an email address inside the parentheses, something like
+# noc@example.org or other reachable address, so that if something goes wrong
+# and the server operators need to step in, they can see from logs who to
+# contact instead of just blocking your IP:
+readonly CurlUserAgent='renew-opendmarc-tlds/0.1 (distributed with Exim)'
+
+# change this to your Exim run-time user (exim -n -bP exim_user) :
+readonly RuntimeUser='_exim'
+
+# Do not make this a directory which untrusted users can write to:
+readonly StateDir='/var/cache/exim'
+
+readonly URL='https://publicsuffix.org/list/public_suffix_list.dat'
+
+readonly TargetShortFile='opendmarc.tlds'
+
+# When replacing, new file must be at least this percentage the size of
+# the old one or it's an error:
+readonly MinNewSizeRation=90
+
+# Each of these regexps must be matched by the file, or it's an error:
+readonly MustExistRegexps='
+ ^ac\.uk$
+ ^org$
+ ^tech$
+ '
+
+# =======================8< end of configuration >8=======================
+
+set -eu
+
+readonly FullTargetPath="${StateDir}/${TargetShortFile}"
+readonly WorkingFile="${FullTargetPath}.$$"
+
+progname="$(basename "$0")"
+note() { printf >&2 '%s: %s\n' "$progname" "$*"; }
+die() { note "$@"; exit 1; }
+
+# guard against stomping on file-permissions
+[ ".$(id -un)" = ".${RuntimeUser:?}" ] || \
+ die "must be invoked as ${RuntimeUser}"
+
+fetch_candidate() {
+ curl --user-agent "$CurlUserAgent" -fSs -o "${WorkingFile}" "${URL}"
+}
+
+case $(uname -s) in
+*BSD|Darwin)
+ size_of() { stat -f %z "$1"; }
+ ;;
+Linux)
+ size_of() { stat -c %s "$1"; }
+ ;;
+*)
+ # why do we live in a world where Perl is the safe portable solution
+ # to getting the size of a file?
+ size_of() { perl -le 'print((stat($ARGV[0]))[7])' -- "$1"; }
+ ;;
+esac
+
+sanity_check_candidate() {
+ local new_size prev_size re
+ new_size="$(size_of "$WorkingFile")"
+
+ for re in $MustExistRegexps; do
+ grep -qs "$re" -- "$WorkingFile" || \
+ die "regexp $re not found in $WorkingFile"
+ done
+
+ if ! prev_size="$(size_of "$FullTargetPath")"; then
+ note "missing previous file, can't size-compare: $FullTargetPath"
+ # We're sane by definition, probably initial fetch, and the
+ # stat failure and this note will be printed. That's fine; if
+ # a cron invocation is missing the file then something has gone
+ # badly wrong.
+ return 0
+ fi
+ local ratio
+ ratio=$(expr $new_size \* 100 / $prev_size)
+ if [ $ratio -lt $MinNewSizeRation ]; then
+ die "New $TargetShortFile candidate only ${ratio}% size of old; $new_size vs $prev_size"
+ fi
+}
+
+if [ "${1:-.}" = "cron" ]; then
+ shift
+ # Don't pull on-the-minute, wait for off-cycle-peak
+ sleep $(( ($(dd if=/dev/urandom bs=1 count=1 2>/dev/null | hexdump -e '1/1 "%u"') % 40) + 10))
+fi
+
+umask 022
+fetch_candidate
+sanity_check_candidate
+mv -- "$WorkingFile" "$FullTargetPath"
diff --git a/util/unknownuser.sh b/util/unknownuser.sh
new file mode 100755
index 0000000..fe04dc6
--- /dev/null
+++ b/util/unknownuser.sh
@@ -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
+
+
+