Use compressed form of ipv6 in $sender_host_address under -bh. Bug 3027
[exim.git] / src / src / exim.h
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) The Exim Maintainers 2021 - 2022 */
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 */
9
10
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. */
15
16 #ifndef EXIM_H
17 #define EXIM_H
18
19 /* Assume most systems have statfs() unless os.h undefines this macro */
20
21 #define HAVE_STATFS
22
23 /* Similarly, assume most systems have srandom() unless os.h undefines it.
24 This call dates back at least as far as SUSv2. */
25
26 #define HAVE_SRANDOM
27
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.
31 */
32
33 #define _GNU_SOURCE 1
34
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. */
37
38 #include "os.h"
39
40 /* If it didn't define os_find_running_interfaces, use the common function. */
41
42 #ifndef os_find_running_interfaces
43 # define os_find_running_interfaces os_common_find_running_interfaces
44 #endif
45
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. */
50
51 #ifndef BASE_62
52 # define BASE_62 62
53 #endif
54
55 /* The maximum value of localhost_number depends on the base being used */
56
57 #if BASE_62 == 62
58 # define LOCALHOST_MAX  16
59 #else
60 # define LOCALHOST_MAX  10
61 #endif
62
63 /* If not overridden by os.h, dynamic libraries have filenames ending .so */
64 #ifndef DYNLIB_FN_EXT
65 # define DYNLIB_FN_EXT "so"
66 #endif
67
68 /* ANSI C standard includes */
69
70 #include <ctype.h>
71 #include <locale.h>
72 #include <math.h>
73 #include <signal.h>
74 #include <stdarg.h>
75 #include <stddef.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <time.h>
80
81 /* Unix includes */
82
83 #include <errno.h>
84 #if defined(__svr4__) && defined(__sparc) && ! defined(__EXTENSIONS__)
85 # define __EXTENSIONS__  /* so that SunOS 5 gets NGROUPS_MAX */
86 # include <limits.h>
87 # undef  __EXTENSIONS__
88 #else
89 # include <limits.h>
90 #endif
91
92 #ifdef EXIM_HAVE_INOTIFY
93 # include <sys/inotify.h>
94 #endif
95 #ifdef EXIM_HAVE_KEVENT
96 # include <sys/event.h>
97 #endif
98
99 /* C99 integer types, figure out how to undo this if needed for older systems */
100
101 #include <inttypes.h>
102
103 /* Just in case some aged system doesn't define them... */
104
105 #ifndef INT_MAX
106 # define INT_MAX 2147483647
107 #endif
108
109 #ifndef INT_MIN
110 # define INT_MIN (-INT_MAX - 1)
111 #endif
112
113 #ifndef SHRT_MAX
114 # define SHRT_MAX 32767
115 #endif
116
117 #ifndef UCHAR_MAX
118 # define UCHAR_MAX 255
119 #endif
120
121
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)
125 #endif
126 #ifndef EXIM_ARITH_MIN
127 # define EXIM_ARITH_MIN (-EXIM_ARITH_MAX - 1)
128 #endif
129
130 /* Some systems have PATH_MAX and some have MAX_PATH_LEN. */
131
132 #ifndef PATH_MAX
133 # ifdef MAX_PATH_LEN
134 #  define PATH_MAX MAX_PATH_LEN
135 # else
136 #  define PATH_MAX 1024
137 # endif
138 #endif
139
140 /* RFC 5321 specifies that the maximum length of a local-part is 64 octets
141 and the maximum length of a domain is 255 octets, but then also defines
142 the maximum length of a forward/reverse path as 256 not 64+1+255.
143 For an IP address, the maximum is 45 without a scope and we don't work
144 with scoped addresses, so go with that.  (IPv6 with mapped IPv4).
145
146 A hostname maximum length is in practice the same as the domainname, for
147 the same core reasons (maximum length of a DNS name), but the semantics
148 are different and seeing "DOMAIN" in source is confusing when talking about
149 hostnames; so we define a second macro.  We'll use RFC 2181 as the reference
150 for this one.
151
152 There is no known (to me) specification on the maximum length of a human name
153 in email addresses and we should be careful about imposing such a limit on
154 received email, but in terms of limiting what untrusted callers specify, or
155 local generation, having a limit makes sense.  Err on the side of generosity.
156
157 For a display mail address, we have a human name, an email in brackets,
158 possibly some (Comments), so it needs to be at least 512+3 and some more to
159 avoid extraneous errors.
160 Since the sane SMTP line length limit is 998, constraining such parameters to
161 be 1024 seems generous and unlikely to spuriously reject legitimate
162 invocations.
163
164 The driver name is a name of a router/transport/authenticator etc in the
165 configuration file.  We also use this for some other short strings, such
166 as queue names.
167 Also TLS ciphersuite name (no real known limit since the protocols use
168 integers, but max seen in reality is 45 octets).
169
170 RFC 1413 gives us the 512 limit on IDENT protocol userids.
171 */
172
173 #define EXIM_EMAILADDR_MAX     256
174 #define EXIM_LOCALPART_MAX      64
175 #define EXIM_DOMAINNAME_MAX    255
176 #define EXIM_IPADDR_MAX         45
177 #define EXIM_HOSTNAME_MAX      255
178 #define EXIM_HUMANNAME_MAX     256
179 #define EXIM_DISPLAYMAIL_MAX  1024
180 #define EXIM_DRIVERNAME_MAX     64
181 #define EXIM_CIPHERNAME_MAX     64
182 #define EXIM_IDENTUSER_MAX     512
183
184
185 #include <sys/types.h>
186 #include <sys/file.h>
187 #include <dirent.h>
188 #include <netdb.h>
189 #ifndef NO_POLL_H
190 # include <poll.h>
191 #endif
192 #include <pwd.h>
193 #include <grp.h>
194 #include <syslog.h>
195
196 /* Not all systems have flock() available. Those that do must define LOCK_SH
197 in sys/file.h. */
198
199 #ifndef LOCK_SH
200 # define NO_FLOCK
201 #endif
202
203 #ifndef NO_SYSEXITS        /* some OS don't have this */
204 # include <sysexits.h>
205 #endif
206
207 /* A few OS don't have socklen_t; their os.h files define EXIM_SOCKLEN_T to
208 be size_t or whatever. We used to use SOCKLEN_T, but then it was discovered
209 that this is used by the AIX include files. */
210
211 #ifndef EXIM_SOCKLEN_T
212 # define EXIM_SOCKLEN_T socklen_t
213 #endif
214
215 /* Ensure that the sysexits we reference are defined */
216
217 #ifndef EX_UNAVAILABLE
218 # define EX_UNAVAILABLE 69        /* service unavailable; used for execv fail */
219 #endif
220 #ifndef EX_CANTCREAT
221 # define EX_CANTCREAT   73        /* can't create file: treat as temporary */
222 #endif
223 #ifndef EX_TEMPFAIL
224 # define EX_TEMPFAIL    75        /* temp failure; user is invited to retry */
225 #endif
226 #ifndef EX_CONFIG
227 # define EX_CONFIG      78        /* configuration error */
228 #endif
229
230 /* This one is not in any sysexits file that I've come across */
231
232 #define EX_EXECFAILED 127        /* execve() failed */
233
234
235 #include <sys/time.h>
236 #include <sys/param.h>
237
238 #ifndef NO_SYS_RESOURCE_H  /* QNX doesn't have this */
239 # include <sys/resource.h>
240 #endif
241
242 #include <sys/socket.h>
243
244 /* If we are on an IPv6 system, the macro AF_INET6 will have been defined in
245 the sys/socket.h header. It is helpful to have this defined on an IPv4 system
246 so that it can appear in the code, even if it is never actually used when
247 the code is run. It saves some #ifdef occurrences. */
248
249 #ifndef AF_INET6
250 # define AF_INET6 24
251 #endif
252
253 #include <sys/ioctl.h>
254
255 /* The new standard is statvfs; some OS have statfs. For statvfs the block
256 counts must be multiplied by the "fragment size" f_frsize to get the actual
257 size. In other cases the value seems to be f_bsize (which is sometimes the only
258 block size), so we use a macro to get that instead.
259
260 Also arrange to be able to cut it out altogether for way-out OS that don't have
261 anything. I've indented a bit here to try to make the mess a bit more
262 intelligible. Note that simply defining one name to be another when
263 HAVE_SYS_STATVFS_H is not set will not work if the system has a statvfs macro
264 or a macro with entries f_frsize and f_bsize. */
265
266 #ifdef HAVE_STATFS
267   #ifdef HAVE_SYS_STATVFS_H
268     #include <sys/statvfs.h>
269     #define STATVFS statvfs
270     #define F_FRSIZE f_frsize
271   #else
272     #define STATVFS statfs
273     #define F_FRSIZE f_bsize
274     #ifdef HAVE_SYS_VFS_H
275       #include <sys/vfs.h>
276       #ifdef HAVE_SYS_STATFS_H
277       #include <sys/statfs.h>
278       #endif
279     #endif
280     #ifdef HAVE_SYS_MOUNT_H
281     #include <sys/mount.h>
282     #endif
283   #endif
284
285   /* Macros for the fields for the available space for non-superusers; define
286   these only if the OS header has not. Not all OS have f_favail; those that
287   are known to have it define F_FAVAIL as f_favail. The default is to use
288   f_free. */
289
290   #ifndef F_BAVAIL
291   # define F_BAVAIL f_bavail
292   #endif
293
294   #ifndef F_FAVAIL
295   # define F_FAVAIL f_ffree
296   #endif
297
298   /* All the systems I've been able to look at seem to have F_FILES */
299
300   #ifndef F_FILES
301   # define F_FILES  f_files
302   #endif
303
304 #endif
305
306
307 #ifndef  SIOCGIFCONF   /* HACK for SunOS 5 */
308 # include <sys/sockio.h>
309 #endif
310
311 #include <sys/stat.h>
312 #include <sys/wait.h>
313 #include <sys/utsname.h>
314 #include <fcntl.h>
315
316 /* There's a shambles in IRIX6 - it defines EX_OK in unistd.h which conflicts
317 with the definition in sysexits.h. Exim does not actually use this macro, so we
318 just undefine it. It would be nice to be able to re-instate the definition from
319 sysexits.h if there is no definition in unistd.h, but I do not think there is a
320 way to do this in C because macro definitions are not scanned for other macros
321 at definition time. [The code here used to assume they were, until I was
322 disabused of the notion. Luckily, since EX_OK is not used, it didn't matter.] */
323
324 #ifdef EX_OK
325 # undef EX_OK
326 #endif
327
328 #include <unistd.h>
329
330 #include <utime.h>
331 #ifndef NO_NET_IF_H
332 # include <net/if.h>
333 #endif
334 #include <sys/un.h>
335 #include <netinet/in.h>
336 #include <netinet/tcp.h>
337 #include <arpa/inet.h>
338 #include <arpa/nameser.h>
339
340
341 /* While IPv6 is still young the definitions of T_AAAA and T_A6 may not be
342 included in arpa/nameser.h. Fudge them here. */
343
344 #ifndef T_AAAA
345 #define T_AAAA 28
346 #endif
347
348 #ifndef T_A6
349 #define T_A6 38
350 #endif
351
352 /* Ancient systems (e.g. SunOS4) don't appear to have T_TXT defined in their
353 header files. I don't suppose they have T_SRV either. */
354
355 #ifndef T_TXT
356 # define T_TXT 16
357 #endif
358
359 #ifndef T_SRV
360 # define T_SRV 33
361 #endif
362
363 /* Many systems do not have T_SPF. */
364
365 #ifndef T_SPF
366 # define T_SPF 99
367 #endif
368
369 /* New TLSA record for DANE */
370 #ifndef T_TLSA
371 # define T_TLSA 52
372 #endif
373 #define MAX_TLSA_EXPANDED_SIZE 8192
374
375 /* It seems that some versions of arpa/nameser.h don't define *any* of the
376 T_xxx macros, which seem to be non-standard nowadays. Just to be on the safe
377 side, put in definitions for all the ones that Exim uses. */
378
379 #ifndef T_A
380 # define T_A 1
381 #endif
382
383 #ifndef T_CNAME
384 # define T_CNAME 5
385 #endif
386
387 #ifndef T_SOA
388 # define T_SOA 6
389 #endif
390
391 #ifndef T_MX
392 # define T_MX 15
393 #endif
394
395 #ifndef T_NS
396 # define T_NS 2
397 #endif
398
399 #ifndef T_PTR
400 # define T_PTR 12
401 #endif
402
403
404 /* We define a few private types for special DNS lookups:
405
406  . T_ZNS gets the nameservers of the enclosing zone of a domain
407
408  . T_MXH gets the MX hostnames only (without their priorities)
409
410  . T_CSA gets the domain's Client SMTP Authorization SRV record
411
412  . T_ADDRESSES looks up both AAAA (or A6) and A records
413
414 If any of these names appear in the RRtype list at:
415   <http://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml>
416 then we should rename Exim's private type away from the conflict.
417 */
418
419 #define T_ZNS (-1)
420 #define T_MXH (-2)
421 #define T_CSA (-3)
422 #define T_ADDRESSES (-4)
423
424 /* The resolv.h header defines __P(x) on some Solaris 2.5.1 systems (without
425 checking that it is already defined, in fact). This conflicts with other
426 headers that behave likewise (see below), leading to compiler warnings. Arrange
427 to undefine it if resolv.h defines it. */
428
429 #if defined(__P)
430 # define __P_WAS_DEFINED_BEFORE_RESOLV
431 #endif
432
433 #include <resolv.h>
434
435 #if defined(__P) && ! defined (__P_WAS_DEFINED_BEFORE_RESOLV)
436 # undef __P
437 #endif
438
439 /* If not defined by os.h, we do nothing special to push DNS resolver state
440 back to be available by the classic resolver routines.  Also, provide
441 prototype for our get routine, unless defined away. */
442
443 #ifndef os_put_dns_resolver_res
444 # define os_put_dns_resolver_res(R) do {/**/} while(0)
445 #endif
446 #ifndef os_get_dns_resolver_res
447 res_state os_get_dns_resolver_res(void);
448 #endif
449
450 /* These three are to support the IP option logging code. Linux is
451 different to everyone else and there are also other systems which don't
452 have netinet/ip_var.h, so there's a general macro to control its inclusion. */
453
454 #include <netinet/in_systm.h>
455 #include <netinet/ip.h>
456
457 #ifndef NO_IP_VAR_H
458 # include <netinet/ip_var.h>
459 #endif
460
461 /* Linux (and some others) uses a different type for the 2nd argument of
462 iconv(). It's os.h file defines ICONV_ARG2_TYPE. For the rest, define a default
463 here. */
464
465 #ifndef ICONV_ARG2_TYPE
466 # define ICONV_ARG2_TYPE char **
467 #endif
468
469 /* One OS uses a different type for the 5th argument of getsockopt */
470
471 #ifndef GETSOCKOPT_ARG5_TYPE
472 # define GETSOCKOPT_ARG5_TYPE socklen_t *
473 #endif
474
475 /* One operating system uses a different type for the 2nd argument of select().
476 Its os.h file defines SELECT_ARG2_TYPE. For the rest, define a default here. */
477
478 #ifndef SELECT_ARG2_TYPE
479 # define SELECT_ARG2_TYPE fd_set
480 #endif
481
482 /* One operating system uses a different type for the 4th argument of
483 dn_expand(). Its os.h file defines DN_EXPAND_ARG4_TYPE. For the rest, define a
484 default here. */
485
486 #ifndef DN_EXPAND_ARG4_TYPE
487 # define DN_EXPAND_ARG4_TYPE char *
488 #endif
489
490 /* One operating system defines a different type for the yield of inet_addr().
491 In Exim code, its value is always assigned to the s_addr members of address
492 structures. Casting the yield to the type of s_addr should fix the problem,
493 since the size of the data is correct. Just in case this ever has to be
494 changed, use a macro for the type, and define it here so that it is possible to
495 use different values for specific OS if ever necessary. */
496
497 #ifndef S_ADDR_TYPE
498 # define S_ADDR_TYPE u_long
499 #endif
500
501 /* (At least) one operating system (Solaris) defines a different type for the
502 second argument of pam_converse() - the difference is the absence of "const".
503 Its os.h file defines PAM_CONVERSE_ARG2_TYPE. For the rest, define a default
504 here. */
505
506 #ifndef PAM_CONVERSE_ARG2_TYPE
507 # define PAM_CONVERSE_ARG2_TYPE const struct pam_message
508 #endif
509
510 /* One operating system (SunOS4) defines getc, ungetc, feof, and ferror as
511 macros and not as functions. Exim needs them to be assignable functions. This
512 flag gets set to cause this to be sorted out here. */
513
514 #ifdef FUDGE_GETC_AND_FRIENDS
515 # undef getc
516 extern int getc(FILE *);
517 # undef ungetc
518 extern int ungetc(int, FILE *);
519 # undef feof
520 extern int feof(FILE *);
521 # undef ferror
522 extern int ferror(FILE *);
523 #endif
524
525 /* The header from the PCRE regex package */
526
527 #define PCRE2_CODE_UNIT_WIDTH 8
528 #include <pcre2.h>
529
530 /* Exim includes are in several files. Note that local_scan.h #includes
531 config.h, mytypes.h, and store.h, so we don't need to mention them explicitly.
532 */
533
534 #include "local_scan.h"
535 #include "macros.h"
536 #include "hintsdb.h"
537 #include "hintsdb_structs.h"
538 #include "structs.h"
539 #include "blob.h"
540 #include "hash.h"
541 #include "globals.h"
542 #include "functions.h"
543 #include "dbfunctions.h"
544 #include "osfunctions.h"
545
546 #ifdef EXPERIMENTAL_BRIGHTMAIL
547 # include "bmi_spam.h"
548 #endif
549 #ifdef SUPPORT_SPF
550 # include "spf.h"
551 #endif
552 #ifndef DISABLE_DKIM
553 # include "dkim.h"
554 #endif
555 #ifdef SUPPORT_DMARC
556 # include "dmarc.h"
557 # include <opendmarc/dmarc.h>
558 #endif
559
560 /* The following stuff must follow the inclusion of config.h because it
561 requires various things that are set therein. */
562
563 #if HAVE_ICONV             /* Not all OS have this */
564 # include <iconv.h>
565 #endif
566
567 #if defined(USE_READLINE) || defined(EXPAND_DLFUNC) || defined (LOOKUP_MODULE_DIR)
568 # include <dlfcn.h>
569 #endif
570
571 #ifdef ENABLE_DISABLE_FSYNC
572 # define EXIMfsync(f) (disable_fsync ? 0 : fsync(f))
573 #else
574 # define EXIMfsync(f) fsync(f)
575 #endif
576
577 /* Backward compatibility; LOOKUP_LSEARCH now includes all three */
578
579 #if (!defined LOOKUP_LSEARCH) && (defined LOOKUP_WILDLSEARCH || defined LOOKUP_NWILDLSEARCH)
580 # define LOOKUP_LSEARCH yes
581 #endif
582
583 /* Define a union to hold either an IPv4 or an IPv6 sockaddr structure; this
584 simplifies some of the coding.  We include the sockaddr to reduce type-punning
585 issues in C99. */
586
587 union sockaddr_46 {
588   struct sockaddr_in v4;
589   #if HAVE_IPV6
590   struct sockaddr_in6 v6;
591   #endif
592   struct sockaddr v0;
593 };
594
595 /* If DISABLE_TLS is defined, ensure that USE_GNUTLS is not defined
596 so that if USE_GNUTLS *is* set, we can assume DISABLE_TLS is not set.
597 Ditto USE_OPENSSL.
598 Likewise, OSCP, AUTH_TLS and CERTNAMES cannot be supported. */
599
600 #ifdef DISABLE_TLS
601 # undef USE_OPENSSL
602 # undef USE_GNUTLS
603 # ifndef DISABLE_OCSP
604 #  define DISABLE_OCSP
605 # endif
606 # undef EXPERIMENTAL_CERTNAMES
607 # undef AUTH_TLS
608 #endif
609
610 /* If SPOOL_DIRECTORY, LOG_FILE_PATH or PID_FILE_PATH have not been defined,
611 set them to the null string. */
612
613 #ifndef SPOOL_DIRECTORY
614   #define SPOOL_DIRECTORY ""
615 #endif
616 #ifndef LOG_FILE_PATH
617   #define LOG_FILE_PATH ""
618 #endif
619 #ifndef PID_FILE_PATH
620   #define PID_FILE_PATH ""
621 #endif
622
623 /* The EDQUOT error code isn't universally available, though it is widespread.
624 There is a particular shambles in SunOS5, where it did not exist originally,
625 but got installed with a particular patch for Solaris 2.4. There is a
626 configuration variable for specifying what the system's "over quota" error is,
627 which will end up in config.h if supplied in OS/Makefile-xxx. If it is not set,
628 default to EDQUOT if it exists, otherwise ENOSPC. */
629
630 #ifndef ERRNO_QUOTA
631 # ifdef  EDQUOT
632 #  define ERRNO_QUOTA EDQUOT
633 # else
634 #  define ERRNO_QUOTA ENOSPC
635 # endif
636 #endif
637
638 /* DANE w/o DNSSEC is useless */
639 #if defined(SUPPORT_DANE) && defined(DISABLE_DNSSEC)
640 # error DANE support requires DNSSEC support
641 #endif
642
643 /* Some platforms (FreeBSD, OpenBSD, Solaris) do not seem to define this */
644
645 #ifndef POLLRDHUP
646 # define POLLRDHUP (POLLIN | POLLHUP)
647 #endif
648
649 /* Some platforms (Darwin) have to define a larger limit on groups membership */
650
651 #ifndef EXIM_GROUPLIST_SIZE
652 # define EXIM_GROUPLIST_SIZE NGROUPS_MAX
653 #endif
654
655 /* Linux has TCP_CORK, FreeBSD has TCP_NOPUSH; they do pretty much the same */
656
657 #ifdef TCP_CORK
658 # define EXIM_TCP_CORK TCP_CORK
659 #elif defined(TCP_NOPUSH)
660 # define EXIM_TCP_CORK TCP_NOPUSH
661 #endif
662
663 /* LibreSSL seems to not push out the SMTP response to QUIT with our usual
664 handling which is trying to get the client to FIN first so that the server does
665 not get the TIME_WAIT */
666
667 #if !defined(DISABLE_TLS) && defined(USE_OPENSSL) && defined(LIBRESSL_VERSION_NUMBER)
668 # define SERVERSIDE_CLOSE_NOWAIT
669 #endif
670
671 #endif
672 /* End of exim.h */