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