1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) The Exim Maintainers 2021 - 2024 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-or-later */
11 /* Source files for exim all #include this header, which drags in everything
12 that is needed. They don't all need everything, of course, but it's far too
13 messy to have each one importing its own list, and anyway, most of them need
14 most of these includes. */
19 /* Assume most systems have statfs() unless os.h undefines this macro */
23 /* Similarly, assume most systems have srandom() unless os.h undefines it.
24 This call dates back at least as far as SUSv2. */
28 /* This is primarily for the Gnu C library; we define it before os.h so that
29 os.h has a chance to hurriedly undef it, Just In Case. We need C99 for some
30 64-bit math support, and defining _ISOC99_SOURCE breaks <resolv.h> and friends.
35 /* First of all include the os-specific header, which might set things that
36 are needed by any of the other headers, including system headers. */
40 /* If it didn't define os_find_running_interfaces, use the common function. */
42 #ifndef os_find_running_interfaces
43 # define os_find_running_interfaces os_common_find_running_interfaces
46 /* If it didn't define the base for "base 62" numbers, we really do use 62.
47 This is the case for all real Unix and Unix-like OS. It's only Cygwin and
48 Darwin, with their case-insensitive file systems, that can't use base 62 for
49 making unique names. */
55 /* The maximum value of localhost_number depends on the base being used */
58 # define LOCALHOST_MAX 16
60 # define LOCALHOST_MAX 10
63 /* If not overridden by os.h, dynamic libraries have filenames ending .so */
65 # define DYNLIB_FN_EXT "so"
68 /* ANSI C standard includes */
84 #if defined(__svr4__) && defined(__sparc) && ! defined(__EXTENSIONS__)
85 # define __EXTENSIONS__ /* so that SunOS 5 gets NGROUPS_MAX */
87 # undef __EXTENSIONS__
92 #ifdef EXIM_HAVE_INOTIFY
93 # include <sys/inotify.h>
95 #ifdef EXIM_HAVE_KEVENT
96 # include <sys/event.h>
99 /* C99 integer types, figure out how to undo this if needed for older systems */
101 #include <inttypes.h>
103 /* Just in case some aged system doesn't define them... */
106 # define INT_MAX 2147483647
110 # define INT_MIN (-INT_MAX - 1)
114 # define SHRT_MAX 32767
118 # define UCHAR_MAX 255
122 /* To match int_eximarith_t. Define in OS/os.h-<your-system> to override. */
123 #ifndef EXIM_ARITH_MAX
124 # define EXIM_ARITH_MAX ((int_eximarith_t)9223372036854775807LL)
126 #ifndef EXIM_ARITH_MIN
127 # define EXIM_ARITH_MIN (-EXIM_ARITH_MAX - 1)
130 /* RFC 5321 specifies that the maximum length of a local-part is 64 octets
131 and the maximum length of a domain is 255 octets, but then also defines
132 the maximum length of a forward/reverse path as 256 not 64+1+255.
133 For an IP address, the maximum is 45 without a scope and we don't work
134 with scoped addresses, so go with that. (IPv6 with mapped IPv4).
136 A hostname maximum length is in practice the same as the domainname, for
137 the same core reasons (maximum length of a DNS name), but the semantics
138 are different and seeing "DOMAIN" in source is confusing when talking about
139 hostnames; so we define a second macro. We'll use RFC 2181 as the reference
142 There is no known (to me) specification on the maximum length of a human name
143 in email addresses and we should be careful about imposing such a limit on
144 received email, but in terms of limiting what untrusted callers specify, or
145 local generation, having a limit makes sense. Err on the side of generosity.
147 For a display mail address, we have a human name, an email in brackets,
148 possibly some (Comments), so it needs to be at least 512+3 and some more to
149 avoid extraneous errors.
150 Since the sane SMTP line length limit is 998, constraining such parameters to
151 be 1024 seems generous and unlikely to spuriously reject legitimate
154 The driver name is a name of a router/transport/authenticator etc in the
155 configuration file. We also use this for some other short strings, such
157 Also TLS ciphersuite name (no real known limit since the protocols use
158 integers, but max seen in reality is 45 octets).
160 RFC 1413 gives us the 512 limit on IDENT protocol userids.
163 #define EXIM_EMAILADDR_MAX 256
164 #define EXIM_LOCALPART_MAX 64
165 #define EXIM_DOMAINNAME_MAX 255
166 #define EXIM_IPADDR_MAX 45
167 #define EXIM_HOSTNAME_MAX 255
168 #define EXIM_HUMANNAME_MAX 256
169 #define EXIM_DISPLAYMAIL_MAX 1024
170 #define EXIM_DRIVERNAME_MAX 64
171 #define EXIM_CIPHERNAME_MAX 64
172 #define EXIM_IDENTUSER_MAX 512
175 #include <sys/types.h>
176 #include <sys/file.h>
186 /* Not all systems have flock() available. Those that do must define LOCK_SH
193 #ifndef NO_SYSEXITS /* some OS don't have this */
194 # include <sysexits.h>
197 /* A few OS don't have socklen_t; their os.h files define EXIM_SOCKLEN_T to
198 be size_t or whatever. We used to use SOCKLEN_T, but then it was discovered
199 that this is used by the AIX include files. */
201 #ifndef EXIM_SOCKLEN_T
202 # define EXIM_SOCKLEN_T socklen_t
205 /* Ensure that the sysexits we reference are defined */
207 #ifndef EX_UNAVAILABLE
208 # define EX_UNAVAILABLE 69 /* service unavailable; used for execv fail */
211 # define EX_CANTCREAT 73 /* can't create file: treat as temporary */
214 # define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */
217 # define EX_CONFIG 78 /* configuration error */
220 /* This one is not in any sysexits file that I've come across */
222 #define EX_EXECFAILED 127 /* execve() failed */
225 #include <sys/time.h>
226 #include <sys/param.h>
228 #ifndef NO_SYS_RESOURCE_H /* QNX doesn't have this */
229 # include <sys/resource.h>
232 #include <sys/socket.h>
234 /* If we are on an IPv6 system, the macro AF_INET6 will have been defined in
235 the sys/socket.h header. It is helpful to have this defined on an IPv4 system
236 so that it can appear in the code, even if it is never actually used when
237 the code is run. It saves some #ifdef occurrences. */
243 #include <sys/ioctl.h>
245 /* The new standard is statvfs; some OS have statfs. For statvfs the block
246 counts must be multiplied by the "fragment size" f_frsize to get the actual
247 size. In other cases the value seems to be f_bsize (which is sometimes the only
248 block size), so we use a macro to get that instead.
250 Also arrange to be able to cut it out altogether for way-out OS that don't have
251 anything. I've indented a bit here to try to make the mess a bit more
252 intelligible. Note that simply defining one name to be another when
253 HAVE_SYS_STATVFS_H is not set will not work if the system has a statvfs macro
254 or a macro with entries f_frsize and f_bsize. */
257 #ifdef HAVE_SYS_STATVFS_H
258 #include <sys/statvfs.h>
259 #define STATVFS statvfs
260 #define F_FRSIZE f_frsize
262 #define STATVFS statfs
263 #define F_FRSIZE f_bsize
264 #ifdef HAVE_SYS_VFS_H
266 #ifdef HAVE_SYS_STATFS_H
267 #include <sys/statfs.h>
270 #ifdef HAVE_SYS_MOUNT_H
271 #include <sys/mount.h>
275 /* Macros for the fields for the available space for non-superusers; define
276 these only if the OS header has not. Not all OS have f_favail; those that
277 are known to have it define F_FAVAIL as f_favail. The default is to use
281 # define F_BAVAIL f_bavail
285 # define F_FAVAIL f_ffree
288 /* All the systems I've been able to look at seem to have F_FILES */
291 # define F_FILES f_files
297 #ifndef SIOCGIFCONF /* HACK for SunOS 5 */
298 # include <sys/sockio.h>
301 #include <sys/stat.h>
302 #include <sys/wait.h>
303 #include <sys/utsname.h>
306 /* There's a shambles in IRIX6 - it defines EX_OK in unistd.h which conflicts
307 with the definition in sysexits.h. Exim does not actually use this macro, so we
308 just undefine it. It would be nice to be able to re-instate the definition from
309 sysexits.h if there is no definition in unistd.h, but I do not think there is a
310 way to do this in C because macro definitions are not scanned for other macros
311 at definition time. [The code here used to assume they were, until I was
312 disabused of the notion. Luckily, since EX_OK is not used, it didn't matter.] */
325 #include <netinet/in.h>
326 #include <netinet/tcp.h>
327 #include <arpa/inet.h>
328 #include <arpa/nameser.h>
331 /* While IPv6 is still young the definitions of T_AAAA and T_A6 may not be
332 included in arpa/nameser.h. Fudge them here. */
342 /* Ancient systems (e.g. SunOS4) don't appear to have T_TXT defined in their
343 header files. I don't suppose they have T_SRV either. */
353 /* Many systems do not have T_SPF. */
359 /* New TLSA record for DANE */
363 #define MAX_TLSA_EXPANDED_SIZE 8192
365 /* It seems that some versions of arpa/nameser.h don't define *any* of the
366 T_xxx macros, which seem to be non-standard nowadays. Just to be on the safe
367 side, put in definitions for all the ones that Exim uses. */
394 /* We define a few private types for special DNS lookups:
396 . T_ZNS gets the nameservers of the enclosing zone of a domain
398 . T_MXH gets the MX hostnames only (without their priorities)
400 . T_CSA gets the domain's Client SMTP Authorization SRV record
402 . T_ADDRESSES looks up both AAAA (or A6) and A records
404 If any of these names appear in the RRtype list at:
405 <http://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml>
406 then we should rename Exim's private type away from the conflict.
412 #define T_ADDRESSES (-4)
414 /* The resolv.h header defines __P(x) on some Solaris 2.5.1 systems (without
415 checking that it is already defined, in fact). This conflicts with other
416 headers that behave likewise (see below), leading to compiler warnings. Arrange
417 to undefine it if resolv.h defines it. */
420 # define __P_WAS_DEFINED_BEFORE_RESOLV
425 #if defined(__P) && ! defined (__P_WAS_DEFINED_BEFORE_RESOLV)
429 /* If not defined by os.h, we do nothing special to push DNS resolver state
430 back to be available by the classic resolver routines. Also, provide
431 prototype for our get routine, unless defined away. */
433 #ifndef os_put_dns_resolver_res
434 # define os_put_dns_resolver_res(R) do {/**/} while(0)
436 #ifndef os_get_dns_resolver_res
437 res_state os_get_dns_resolver_res(void);
440 /* These three are to support the IP option logging code. Linux is
441 different to everyone else and there are also other systems which don't
442 have netinet/ip_var.h, so there's a general macro to control its inclusion. */
444 #include <netinet/in_systm.h>
445 #include <netinet/ip.h>
448 # include <netinet/ip_var.h>
451 /* Linux (and some others) uses a different type for the 2nd argument of
452 iconv(). It's os.h file defines ICONV_ARG2_TYPE. For the rest, define a default
455 #ifndef ICONV_ARG2_TYPE
456 # define ICONV_ARG2_TYPE char **
459 /* One OS uses a different type for the 5th argument of getsockopt */
461 #ifndef GETSOCKOPT_ARG5_TYPE
462 # define GETSOCKOPT_ARG5_TYPE socklen_t *
465 /* One operating system uses a different type for the 2nd argument of select().
466 Its os.h file defines SELECT_ARG2_TYPE. For the rest, define a default here. */
468 #ifndef SELECT_ARG2_TYPE
469 # define SELECT_ARG2_TYPE fd_set
472 /* One operating system uses a different type for the 4th argument of
473 dn_expand(). Its os.h file defines DN_EXPAND_ARG4_TYPE. For the rest, define a
476 #ifndef DN_EXPAND_ARG4_TYPE
477 # define DN_EXPAND_ARG4_TYPE char *
480 /* One operating system defines a different type for the yield of inet_addr().
481 In Exim code, its value is always assigned to the s_addr members of address
482 structures. Casting the yield to the type of s_addr should fix the problem,
483 since the size of the data is correct. Just in case this ever has to be
484 changed, use a macro for the type, and define it here so that it is possible to
485 use different values for specific OS if ever necessary. */
488 # define S_ADDR_TYPE u_long
491 /* (At least) one operating system (Solaris) defines a different type for the
492 second argument of pam_converse() - the difference is the absence of "const".
493 Its os.h file defines PAM_CONVERSE_ARG2_TYPE. For the rest, define a default
496 #ifndef PAM_CONVERSE_ARG2_TYPE
497 # define PAM_CONVERSE_ARG2_TYPE const struct pam_message
500 /* One operating system (SunOS4) defines getc, ungetc, feof, and ferror as
501 macros and not as functions. Exim needs them to be assignable functions. This
502 flag gets set to cause this to be sorted out here. */
504 #ifdef FUDGE_GETC_AND_FRIENDS
506 extern int getc(FILE *);
508 extern int ungetc(int, FILE *);
510 extern int feof(FILE *);
512 extern int ferror(FILE *);
515 /* The header from the PCRE regex package */
517 #define PCRE2_CODE_UNIT_WIDTH 8
520 /* Exim includes are in several files. Note that local_scan.h #includes
521 config.h, mytypes.h, and store.h, so we don't need to mention them explicitly.
524 #include "local_scan.h"
525 #include "path_max.h"
529 # include "hintsdb.h"
531 #include "hintsdb_structs.h"
536 #include "functions.h"
538 # include "dbfunctions.h"
540 #include "osfunctions.h"
542 #ifdef EXPERIMENTAL_BRIGHTMAIL
543 # include "bmi_spam.h"
546 # include "miscmods/spf.h"
547 # include "miscmods/spf_api.h"
550 # include "miscmods/dkim.h"
551 # include "miscmods/dkim_api.h"
554 # include "miscmods/dmarc.h"
555 # include "miscmods/dmarc_api.h"
556 # include <opendmarc/dmarc.h>
558 #ifdef EXPERIMENTAL_ARC
559 # include "miscmods/arc_api.h"
561 #ifdef RADIUS_CONFIG_FILE
562 # include "miscmods/radius_api.h"
565 # include "miscmods/pam_api.h"
568 # include "miscmods/perl_api.h"
571 /* The following stuff must follow the inclusion of config.h because it
572 requires various things that are set therein. */
574 #if HAVE_ICONV /* Not all OS have this */
578 #if defined(USE_READLINE) || defined(EXPAND_DLFUNC) || defined (LOOKUP_MODULE_DIR)
582 #ifdef ENABLE_DISABLE_FSYNC
583 # define EXIMfsync(f) (disable_fsync ? 0 : fsync(f))
585 # define EXIMfsync(f) fsync(f)
588 /* Backward compatibility; LOOKUP_LSEARCH now includes all three */
590 #if (!defined LOOKUP_LSEARCH) && (defined LOOKUP_WILDLSEARCH || defined LOOKUP_NWILDLSEARCH)
591 # define LOOKUP_LSEARCH yes
594 /* Define a union to hold either an IPv4 or an IPv6 sockaddr structure; this
595 simplifies some of the coding. We include the sockaddr to reduce type-punning
599 struct sockaddr_in v4;
601 struct sockaddr_in6 v6;
606 /* If DISABLE_TLS is defined, ensure that USE_GNUTLS is not defined
607 so that if USE_GNUTLS *is* set, we can assume DISABLE_TLS is not set.
609 Likewise, OSCP, AUTH_TLS and CERTNAMES cannot be supported. */
614 # ifndef DISABLE_OCSP
615 # define DISABLE_OCSP
617 # undef EXPERIMENTAL_CERTNAMES
621 /* If SPOOL_DIRECTORY, LOG_FILE_PATH or PID_FILE_PATH have not been defined,
622 set them to the null string. */
624 #ifndef SPOOL_DIRECTORY
625 #define SPOOL_DIRECTORY ""
627 #ifndef LOG_FILE_PATH
628 #define LOG_FILE_PATH ""
630 #ifndef PID_FILE_PATH
631 #define PID_FILE_PATH ""
634 /* The EDQUOT error code isn't universally available, though it is widespread.
635 There is a particular shambles in SunOS5, where it did not exist originally,
636 but got installed with a particular patch for Solaris 2.4. There is a
637 configuration variable for specifying what the system's "over quota" error is,
638 which will end up in config.h if supplied in OS/Makefile-xxx. If it is not set,
639 default to EDQUOT if it exists, otherwise ENOSPC. */
643 # define ERRNO_QUOTA EDQUOT
645 # define ERRNO_QUOTA ENOSPC
649 /* DANE w/o DNSSEC is useless */
650 #if defined(SUPPORT_DANE) && defined(DISABLE_DNSSEC)
651 # error DANE support requires DNSSEC support
654 /* Some platforms (FreeBSD, OpenBSD, Solaris) do not seem to define this */
657 # define POLLRDHUP (POLLIN | POLLHUP)
660 /* Some platforms (Darwin) have to define a larger limit on groups membership */
662 #ifndef EXIM_GROUPLIST_SIZE
663 # define EXIM_GROUPLIST_SIZE NGROUPS_MAX
666 /* Linux has TCP_CORK, FreeBSD has TCP_NOPUSH; they do pretty much the same */
669 # define EXIM_TCP_CORK TCP_CORK
670 #elif defined(TCP_NOPUSH)
671 # define EXIM_TCP_CORK TCP_NOPUSH
674 /* LibreSSL seems to not push out the SMTP response to QUIT with our usual
675 handling which is trying to get the client to FIN first so that the server does
676 not get the TIME_WAIT */
678 #if !defined(DISABLE_TLS) && defined(USE_OPENSSL) && defined(LIBRESSL_VERSION_NUMBER)
679 # define SERVERSIDE_CLOSE_NOWAIT