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