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