Commit | Line | Data |
---|---|---|
059ec3d9 PH |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
f9ba5e22 | 5 | /* Copyright (c) University of Cambridge 1995 - 2018 */ |
059ec3d9 PH |
6 | /* See the file NOTICE for conditions of use and distribution. */ |
7 | ||
8 | ||
9 | /* This header file contains type definitions and macros that I use as | |
10 | "standard" in the code of Exim and its utilities. Make it idempotent because | |
11 | local_scan.h includes it and exim.h includes them both (to get this earlier). */ | |
12 | ||
13 | #ifndef MYTYPES_H | |
14 | #define MYTYPES_H | |
15 | ||
f3ebb786 JH |
16 | # include <string.h> |
17 | ||
d1406d48 | 18 | #ifndef FALSE |
2f4df477 | 19 | # define FALSE 0 |
d1406d48 NM |
20 | #endif |
21 | ||
22 | #ifndef TRUE | |
2f4df477 | 23 | # define TRUE 1 |
d1406d48 NM |
24 | #endif |
25 | ||
26 | #ifndef TRUE_UNSET | |
2f4df477 | 27 | # define TRUE_UNSET 2 |
d1406d48 | 28 | #endif |
059ec3d9 PH |
29 | |
30 | ||
31 | /* If gcc is being used to compile Exim, we can use its facility for checking | |
32 | the arguments of printf-like functions. This is done by a macro. */ | |
33 | ||
b3c261f7 | 34 | #if defined(__GNUC__) || defined(__clang__) |
268d00ff JH |
35 | # define PRINTF_FUNCTION(A,B) __attribute__((format(printf,A,B))) |
36 | # define ARG_UNUSED __attribute__((__unused__)) | |
37 | # define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__)) | |
1d1e7973 JH |
38 | # define ALLOC __attribute__((malloc)) |
39 | # define ALLOC_SIZE(A) __attribute__((alloc_size(A))) | |
40 | # define NORETURN __attribute__((noreturn)) | |
059ec3d9 | 41 | #else |
2f4df477 | 42 | # define PRINTF_FUNCTION(A,B) |
1d1e7973 JH |
43 | # define ARG_UNUSED /**/ |
44 | # define WARN_UNUSED_RESULT /**/ | |
45 | # define ALLOC /**/ | |
46 | # define ALLOC_SIZE(A) /**/ | |
47 | # define NORETURN /**/ | |
059ec3d9 PH |
48 | #endif |
49 | ||
81f91683 | 50 | #ifdef WANT_DEEPER_PRINTF_CHECKS |
2f4df477 | 51 | # define ALMOST_PRINTF(A, B) PRINTF_FUNCTION(A, B) |
81f91683 | 52 | #else |
2f4df477 | 53 | # define ALMOST_PRINTF(A, B) |
81f91683 PP |
54 | #endif |
55 | ||
059ec3d9 PH |
56 | |
57 | /* Some operating systems (naughtily, imo) include a definition for "uchar" in | |
58 | the standard header files, so we use "uschar". Solaris has u_char in | |
59 | sys/types.h. This is just a typing convenience, of course. */ | |
60 | ||
059ec3d9 | 61 | typedef unsigned char uschar; |
a207d5dd | 62 | typedef unsigned BOOL; |
cd59ab18 PP |
63 | /* We also have SIGNAL_BOOL, which requires signal.h be included, so is defined |
64 | elsewhere */ | |
059ec3d9 PH |
65 | |
66 | ||
67 | /* These macros save typing for the casting that is needed to cope with the | |
68 | mess that is "char" in ISO/ANSI C. Having now been bitten enough times by | |
69 | systems where "char" is actually signed, I've converted Exim to use entirely | |
70 | unsigned chars, except in a few special places such as arguments that are | |
71 | almost always literal strings. */ | |
72 | ||
73 | #define CS (char *) | |
74 | #define CCS (const char *) | |
75 | #define CSS (char **) | |
76 | #define US (unsigned char *) | |
77 | #define CUS (const unsigned char *) | |
78 | #define USS (unsigned char **) | |
55414b25 | 79 | #define CUSS (const unsigned char **) |
059ec3d9 PH |
80 | |
81 | /* The C library string functions expect "char *" arguments. Use macros to | |
82 | avoid having to write a cast each time. We do this for string and file | |
83 | functions that are called quite often; for other calls to external libraries | |
84 | (which are on the whole special-purpose) we just use individual casts. */ | |
85 | ||
86 | #define Uatoi(s) atoi(CCS(s)) | |
87 | #define Uatol(s) atol(CCS(s)) | |
88 | #define Uchdir(s) chdir(CCS(s)) | |
89 | #define Uchmod(s,n) chmod(CCS(s),n) | |
059ec3d9 PH |
90 | #define Ufgets(b,n,f) fgets(CS(b),n,f) |
91 | #define Ufopen(s,t) fopen(CCS(s),CCS(t)) | |
92 | #define Ulink(s,t) link(CCS(s),CCS(t)) | |
93 | #define Ulstat(s,t) lstat(CCS(s),t) | |
94 | ||
95 | #ifdef O_BINARY /* This is for Cygwin, */ | |
96 | #define Uopen(s,n,m) open(CCS(s),(n)|O_BINARY,m) /* where all files must */ | |
97 | #else /* be opened as binary */ | |
98 | #define Uopen(s,n,m) open(CCS(s),n,m) /* to avoid problems */ | |
99 | #endif /* with CRLF endings. */ | |
100 | #define Uread(f,b,l) read(f,CS(b),l) | |
101 | #define Urename(s,t) rename(CCS(s),CCS(t)) | |
102 | #define Ustat(s,t) stat(CCS(s),t) | |
81a559c8 | 103 | #define Ustrcat(s,t) __Ustrcat(s, CUS(t), __FUNCTION__, __LINE__) |
059ec3d9 PH |
104 | #define Ustrchr(s,n) US strchr(CCS(s),n) |
105 | #define CUstrchr(s,n) CUS strchr(CCS(s),n) | |
106 | #define CUstrerror(n) CUS strerror(n) | |
107 | #define Ustrcmp(s,t) strcmp(CCS(s),CCS(t)) | |
81a559c8 | 108 | #define Ustrcpy(s,t) __Ustrcpy(s, CUS(t), __FUNCTION__, __LINE__) |
f3ebb786 | 109 | #define Ustrcpy_nt(s,t) strcpy(CS s, CCS t) /* no taint check */ |
059ec3d9 PH |
110 | #define Ustrcspn(s,t) strcspn(CCS(s),CCS(t)) |
111 | #define Ustrftime(s,m,f,t) strftime(CS(s),m,f,t) | |
112 | #define Ustrlen(s) (int)strlen(CCS(s)) | |
81a559c8 | 113 | #define Ustrncat(s,t,n) __Ustrncat(s, CUS(t),n, __FUNCTION__, __LINE__) |
059ec3d9 | 114 | #define Ustrncmp(s,t,n) strncmp(CCS(s),CCS(t),n) |
81a559c8 | 115 | #define Ustrncpy(s,t,n) __Ustrncpy(s, CUS(t),n, __FUNCTION__, __LINE__) |
f3ebb786 | 116 | #define Ustrncpy_nt(s,t,n) strncpy(CS s, CCS t, n) /* no taint check */ |
059ec3d9 PH |
117 | #define Ustrpbrk(s,t) strpbrk(CCS(s),CCS(t)) |
118 | #define Ustrrchr(s,n) US strrchr(CCS(s),n) | |
119 | #define CUstrrchr(s,n) CUS strrchr(CCS(s),n) | |
120 | #define Ustrspn(s,t) strspn(CCS(s),CCS(t)) | |
121 | #define Ustrstr(s,t) US strstr(CCS(s),CCS(t)) | |
122 | #define CUstrstr(s,t) CUS strstr(CCS(s),CCS(t)) | |
123 | #define Ustrtod(s,t) strtod(CCS(s),CSS(t)) | |
124 | #define Ustrtol(s,t,b) strtol(CCS(s),CSS(t),b) | |
125 | #define Ustrtoul(s,t,b) strtoul(CCS(s),CSS(t),b) | |
126 | #define Uunlink(s) unlink(CCS(s)) | |
e59797e3 | 127 | |
f3ebb786 JH |
128 | extern void die_tainted(const uschar *, const uschar *, int); |
129 | ||
6d5f5caf JH |
130 | /* Predicate: if an address is in a tainted pool. |
131 | By extension, a variable pointing to this address is tainted. | |
132 | */ | |
133 | ||
134 | static inline BOOL | |
135 | is_tainted(const void * p) | |
136 | { | |
1a44d9d7 | 137 | #if defined(COMPILE_UTILITY) || defined(MACRO_PREDEF) |
aaabfafe | 138 | return FALSE; |
14ca5d2a JH |
139 | |
140 | #elif defined(TAINT_CHECK_SLOW) | |
237613d0 | 141 | extern BOOL is_tainted_fn(const void *); |
14ca5d2a JH |
142 | return is_tainted_fn(p); |
143 | ||
aaabfafe | 144 | #else |
6d5f5caf JH |
145 | extern void * tainted_base, * tainted_top; |
146 | return p >= tainted_base && p < tainted_top; | |
aaabfafe | 147 | #endif |
6d5f5caf JH |
148 | } |
149 | ||
f3ebb786 JH |
150 | static inline uschar * __Ustrcat(uschar * dst, const uschar * src, const char * func, int line) |
151 | { | |
69d0c29b | 152 | #if !defined(COMPILE_UTILITY) && !defined(MACRO_PREDEF) |
f3ebb786 JH |
153 | if (!is_tainted(dst) && is_tainted(src)) die_tainted(US"Ustrcat", CUS func, line); |
154 | #endif | |
155 | return US strcat(CS dst, CCS src); | |
156 | } | |
157 | static inline uschar * __Ustrcpy(uschar * dst, const uschar * src, const char * func, int line) | |
158 | { | |
69d0c29b | 159 | #if !defined(COMPILE_UTILITY) && !defined(MACRO_PREDEF) |
f3ebb786 | 160 | if (!is_tainted(dst) && is_tainted(src)) die_tainted(US"Ustrcpy", CUS func, line); |
059ec3d9 | 161 | #endif |
f3ebb786 JH |
162 | return US strcpy(CS dst, CCS src); |
163 | } | |
164 | static inline uschar * __Ustrncat(uschar * dst, const uschar * src, size_t n, const char * func, int line) | |
165 | { | |
69d0c29b | 166 | #if !defined(COMPILE_UTILITY) && !defined(MACRO_PREDEF) |
f3ebb786 JH |
167 | if (!is_tainted(dst) && is_tainted(src)) die_tainted(US"Ustrncat", CUS func, line); |
168 | #endif | |
169 | return US strncat(CS dst, CCS src, n); | |
170 | } | |
171 | static inline uschar * __Ustrncpy(uschar * dst, const uschar * src, size_t n, const char * func, int line) | |
172 | { | |
69d0c29b | 173 | #if !defined(COMPILE_UTILITY) && !defined(MACRO_PREDEF) |
f3ebb786 JH |
174 | if (!is_tainted(dst) && is_tainted(src)) die_tainted(US"Ustrncpy", CUS func, line); |
175 | #endif | |
176 | return US strncpy(CS dst, CCS src, n); | |
177 | } | |
178 | /*XXX will likely need unchecked copy also */ | |
059ec3d9 | 179 | |
f3ebb786 | 180 | #endif |
059ec3d9 | 181 | /* End of mytypes.h */ |