Debian Codegraph @

src/common.h

raw
1 /*
2 * stunnel TLS offloading and load-balancing proxy
3 * Copyright (C) 1998-2026 Michal Trojnara <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses>.
17 *
18 * Linking stunnel statically or dynamically with other modules is making
19 * a combined work based on stunnel. Thus, the terms and conditions of
20 * the GNU General Public License cover the whole combination.
21 *
22 * In addition, as a special exception, the copyright holder of stunnel
23 * gives you permission to combine stunnel with free software programs or
24 * libraries that are released under the GNU LGPL and with code included
25 * in the standard release of OpenSSL under the OpenSSL License (or
26 * modified versions of such code, with unchanged license). You may copy
27 * and distribute such a system following the terms of the GNU GPL for
28 * stunnel and the licenses of the other code concerned.
29 *
30 * Note that people who make modified versions of stunnel are not obligated
31 * to grant this special exception for their modified versions; it is their
32 * choice whether to do so. The GNU General Public License gives permission
33 * to release a modified version without this exception; this exception
34 * also makes it possible to release a modified version which carries
35 * forward this exception.
36 */
37
38 #ifndef COMMON_H
39 #define COMMON_H
40
41 #include "version.h"
42
43 /**************************************** common constants */
44
45 #define DEFAULT_SECURITY_LEVEL 2
46
47 #define LIBWRAP_CLIENTS 5
48
49 /* CPU stack size */
50 #if defined(__ILP32__) && !defined(__NetBSD__)
51 #define DEFAULT_STACK_SIZE 65536
52 #else
53 #define DEFAULT_STACK_SIZE 131072
54 #endif
55 /* #define DEBUG_STACK_SIZE */
56
57 /* I/O buffer size: 18432 (0x4800) is the maximum size of TLS record payload */
58 #define BUFFSIZE 18432
59
60 /* how many bytes of random input to read from files for PRNG */
61 /* security margin is huge to compensate for flawed entropy */
62 #define RANDOM_BYTES 1024
63
64 /**************************************** debugging */
65
66 /* for FormatGuard */
67 /* #define __NO_FORMATGUARD_ */
68
69 /* additional diagnostic messages */
70 /* #define DEBUG_FD_ALLOC */
71
72 #ifdef DEBUG_INFO
73 #define NOEXPORT
74 #else
75 #define NOEXPORT static
76 #endif
77
78 #ifdef __GNUC__
79 #define NORETURN __attribute__((noreturn))
80 #else
81 #define NORETURN
82 #endif /* __GNUC__ */
83
84 /**************************************** platform */
85
86 #ifdef _WIN32
87 #define USE_WIN32
88 #endif
89
90 #ifdef _WIN32_WCE
91 #define USE_WIN32
92 typedef int socklen_t;
93 #endif
94
95 #ifdef USE_WIN32
96 typedef signed char int8_t;
97 typedef signed short int16_t;
98 typedef signed int int32_t;
99 typedef signed long long int64_t;
100 typedef unsigned char uint8_t;
101 typedef unsigned short uint16_t;
102 typedef unsigned int uint32_t;
103 typedef unsigned long long uint64_t;
104 #ifndef __MINGW32__
105 #ifdef _WIN64
106 typedef __int64 ssize_t;
107 #else /* _WIN64 */
108 typedef int ssize_t;
109 #endif /* _WIN64 */
110 #endif /* !__MINGW32__ */
111 #define USE_IPV6
112 #define _CRT_SECURE_NO_DEPRECATE
113 #define _CRT_NONSTDC_NO_DEPRECATE
114 #define _CRT_NON_CONFORMING_SWPRINTFS
115 /* prevent including wincrypt.h, as it defines its own OCSP_RESPONSE */
116 #define __WINCRYPT_H__
117 #define S_EADDRINUSE WSAEADDRINUSE
118 /* winsock does not define WSAEAGAIN */
119 /* in most (but not all!) BSD implementations EAGAIN==EWOULDBLOCK */
120 #define S_EAGAIN WSAEWOULDBLOCK
121 #define S_ECONNRESET WSAECONNRESET
122 #define S_EINPROGRESS WSAEINPROGRESS
123 #define S_EINTR WSAEINTR
124 #define S_EINVAL WSAEINVAL
125 #define S_EISCONN WSAEISCONN
126 #define S_EMFILE WSAEMFILE
127 /* winsock does not define WSAENFILE */
128 #define S_ENOBUFS WSAENOBUFS
129 /* winsock does not define WSAENOMEM */
130 #define S_ENOPROTOOPT WSAENOPROTOOPT
131 #define S_ENOTSOCK WSAENOTSOCK
132 #define S_EOPNOTSUPP WSAEOPNOTSUPP
133 #define S_EWOULDBLOCK WSAEWOULDBLOCK
134 #define S_ECONNABORTED WSAECONNABORTED
135 #else /* USE_WIN32 */
136 #define S_EADDRINUSE EADDRINUSE
137 #define S_EAGAIN EAGAIN
138 #define S_ECONNRESET ECONNRESET
139 #define S_EINPROGRESS EINPROGRESS
140 #define S_EINTR EINTR
141 #define S_EINVAL EINVAL
142 #define S_EISCONN EISCONN
143 #define S_EMFILE EMFILE
144 #ifdef ENFILE
145 #define S_ENFILE ENFILE
146 #endif
147 #ifdef ENOBUFS
148 #define S_ENOBUFS ENOBUFS
149 #endif
150 #ifdef ENOMEM
151 #define S_ENOMEM ENOMEM
152 #endif
153 #define S_ENOPROTOOPT ENOPROTOOPT
154 #define S_ENOTSOCK ENOTSOCK
155 #define S_EOPNOTSUPP EOPNOTSUPP
156 #define S_EWOULDBLOCK EWOULDBLOCK
157 #define S_ECONNABORTED ECONNABORTED
158 #endif /* USE_WIN32 */
159
160 /**************************************** generic headers */
161
162 #ifdef __vms
163 #include <starlet.h>
164 #endif /* __vms */
165
166 /* for nsr-tandem-nsk architecture */
167 #ifdef __TANDEM
168 #include <floss.h>
169 #endif
170
171 /* threads model */
172 #ifdef USE_UCONTEXT
173 #define __MAKECONTEXT_V2_SOURCE
174 #include <ucontext.h>
175 #endif
176
177 #ifdef USE_PTHREAD
178 #ifndef THREADS
179 #define THREADS
180 #endif
181 #ifndef _REENTRANT
182 /* _REENTRANT is required for thread-safe errno on Solaris */
183 #define _REENTRANT
184 #endif
185 #ifndef _THREAD_SAFE
186 #define _THREAD_SAFE
187 #endif
188 #include <pthread.h>
189 #endif
190
191 /* systemd */
192 #ifdef USE_SYSTEMD
193 #include <systemd/sd-daemon.h>
194 #endif
195
196 #ifdef HAVE_STDINT_H
197 #include <stdint.h>
198 #endif
199
200 #ifdef HAVE_INTTYPES_H
201 #include <inttypes.h>
202 #endif
203
204 /* must be included before sys/stat.h for Ultrix */
205 /* must be included before sys/socket.h for OpenBSD */
206 #include <sys/types.h> /* u_short, u_long */
207 /* general headers */
208 #include <stdio.h>
209 /* must be included before sys/stat.h for Ultrix */
210 #ifndef _WIN32_WCE
211 #include <errno.h>
212 #endif
213 #include <stdlib.h>
214 #include <stdarg.h> /* va_ */
215 #include <string.h>
216 #include <ctype.h> /* isalnum */
217 #include <time.h>
218 #include <sys/stat.h> /* stat */
219 #include <setjmp.h>
220 #include <fcntl.h>
221
222 /**************************************** WIN32 headers */
223
224 #ifdef USE_WIN32
225
226 #define HAVE_STRUCT_ADDRINFO
227 #define HAVE_SNPRINTF
228 #define snprintf _snprintf
229 #define HAVE_VSNPRINTF
230 #define vsnprintf _vsnprintf
231 #define strcasecmp _stricmp
232 #define strncasecmp _strnicmp
233
234 #define get_last_socket_error() WSAGetLastError()
235 #define set_last_socket_error(e) WSASetLastError(e)
236 #define get_last_error() GetLastError()
237 #define set_last_error(e) SetLastError(e)
238 #define readsocket(s,b,n) recv((s),(b),(int)(n),0)
239 #define writesocket(s,b,n) send((s),(b),(int)(n),0)
240
241 /* #define Win32_Winsock */
242 #define __USE_W32_SOCKETS
243
244 /* Winsock2 header for IPv6 definitions */
245 #include <winsock2.h>
246 #include <ws2tcpip.h>
247
248 #include <windows.h>
249 #include <windowsx.h>
250
251 #include <malloc.h> /* _alloca */
252
253 #include <process.h> /* _beginthread */
254 #include <shlobj.h> /* SHGetFolderPath */
255 #include <tchar.h>
256
257 #ifndef alloca
258 #define alloca _alloca
259 #endif
260
261 #include "resources.h"
262
263 #ifndef PRIX64
264 #define PRIX64 "I64X"
265 #endif
266
267 /**************************************** non-WIN32 headers */
268
269 #else /* USE_WIN32 */
270
271 #ifdef __INNOTEK_LIBC__
272 #define socklen_t __socklen_t
273 #define strcasecmp stricmp
274 #define strncasecmp strnicmp
275 #define NI_NUMERICHOST 1
276 #define NI_NUMERICSERV 2
277 #define get_last_socket_error() sock_errno()
278 #define set_last_socket_error(e) ()
279 #define get_last_error() errno
280 #define set_last_error(e) (errno=(e))
281 #define readsocket(s,b,n) recv((s),(b),(n),0)
282 #define writesocket(s,b,n) send((s),(b),(n),0)
283 #define closesocket(s) close(s)
284 #define ioctlsocket(a,b,c) so_ioctl((a),(b),(c))
285 #else
286 #define get_last_socket_error() errno
287 #define set_last_socket_error(e) (errno=(e))
288 #define get_last_error() errno
289 #define set_last_error(e) (errno=(e))
290 #define readsocket(s,b,n) read((s),(b),(n))
291 #define writesocket(s,b,n) write((s),(b),(n))
292 #define closesocket(s) close(s)
293 #define ioctlsocket(a,b,c) ioctl((a),(b),(c))
294 #endif
295
296 typedef int SOCKET;
297 #define INVALID_SOCKET (-1)
298
299 /* OpenVMS compatibility */
300 #ifdef __vms
301 #define LIBDIR "__NA__"
302 #ifdef __alpha
303 #define HOST "alpha-openvms"
304 #else
305 #define HOST "vax-openvms"
306 #endif
307 #include <inet.h>
308 #include <unistd.h>
309 #else /* __vms */
310 #include <syslog.h>
311 #endif /* __vms */
312
313 /* Unix-specific headers */
314 #include <signal.h> /* signal */
315 #include <sys/wait.h> /* wait */
316 #ifdef HAVE_LIMITS_H
317 #include <limits.h> /* INT_MAX */
318 #endif
319 #ifdef HAVE_SYS_RESOURCE_H
320 #include <sys/resource.h> /* getrlimit */
321 #endif
322 #ifdef HAVE_UNISTD_H
323 #include <unistd.h> /* getpid, fork, execvp, exit */
324 #endif
325 #ifdef HAVE_STROPTS_H
326 #include <stropts.h>
327 #endif
328 #ifdef HAVE_MALLOC_H
329 #include <malloc.h> /* mallopt */
330 #endif
331 #ifdef HAVE_SYS_SELECT_H
332 #include <sys/select.h> /* for aix */
333 #endif
334 #include <dirent.h>
335 #ifdef HAVE_SYS_PARAM_H
336 #include <sys/param.h> /* MAXPATHLEN */
337 #endif
338
339 #if defined(HAVE_POLL) && !defined(BROKEN_POLL)
340 #ifdef HAVE_POLL_H
341 #include <poll.h>
342 #define USE_POLL
343 #else /* HAVE_POLL_H */
344 #ifdef HAVE_SYS_POLL_H
345 #include <sys/poll.h>
346 #define USE_POLL
347 #endif /* HAVE_SYS_POLL_H */
348 #endif /* HAVE_POLL_H */
349 #endif /* HAVE_POLL && !BROKEN_POLL */
350
351 #ifdef HAVE_SYS_FILIO_H
352 #include <sys/filio.h> /* for FIONBIO */
353 #endif
354 #include <pwd.h>
355 #ifdef HAVE_GRP_H
356 #include <grp.h>
357 #endif
358 #ifdef __BEOS__
359 #include <posix/grp.h>
360 #endif
361
362 #ifdef HAVE_SYS_UIO_H
363 #include <sys/uio.h> /* struct iovec */
364 #endif /* HAVE_SYS_UIO_H */
365
366 /* BSD sockets */
367 #include <netinet/in.h> /* struct sockaddr_in */
368 #include <sys/socket.h> /* getpeername */
369 #include <arpa/inet.h> /* inet_ntoa */
370 #include <sys/time.h> /* select */
371 #include <sys/ioctl.h> /* ioctl */
372 #ifdef HAVE_SYS_UN_H
373 #include <sys/un.h>
374 #endif
375 #include <netinet/tcp.h>
376 #include <netdb.h>
377 #ifndef INADDR_ANY
378 #define INADDR_ANY (u32)0x00000000
379 #endif
380 #ifndef INADDR_LOOPBACK
381 #define INADDR_LOOPBACK (u32)0x7F000001
382 #endif
383
384 /* SunOS 4 */
385 #if defined(sun) && !defined(__svr4__) && !defined(__SVR4)
386 #define atexit(a) on_exit((a), NULL)
387 extern int sys_nerr;
388 extern char *sys_errlist[];
389 #define strerror(num) ((num)==0 ? "No error" : \
390 ((num)>=sys_nerr ? "Unknown error" : sys_errlist[num]))
391 #endif /* SunOS 4 */
392
393 /* AIX does not have SOL_TCP defined */
394 #ifndef SOL_TCP
395 #define SOL_TCP SOL_SOCKET
396 #endif /* SOL_TCP */
397
398 /* Linux */
399 #ifdef __linux__
400 #ifndef IP_FREEBIND
401 /* kernel headers without IP_FREEBIND definition */
402 #define IP_FREEBIND 15
403 #endif /* IP_FREEBIND */
404 #ifndef IP_TRANSPARENT
405 /* kernel headers without IP_TRANSPARENT definition */
406 #define IP_TRANSPARENT 19
407 #endif /* IP_TRANSPARENT */
408 #ifdef HAVE_LINUX_NETFILTER_IPV4_H
409 #include <limits.h>
410 #include <linux/types.h>
411 #include <linux/netfilter_ipv4.h>
412 #endif /* HAVE_LINUX_NETFILTER_IPV4_H */
413 #endif /* __linux__ */
414 #ifdef HAVE_SYS_SYSCALL_H
415 #include <sys/syscall.h> /* SYS_gettid */
416 #endif
417 #ifdef HAVE_LINUX_SCHED_H
418 #include <linux/sched.h> /* SCHED_BATCH */
419 #endif
420
421 #endif /* USE_WIN32 */
422
423 #ifndef S_ISREG
424 #define S_ISREG(m) (((m)&S_IFMT)==S_IFREG)
425 #endif
426
427 /**************************************** OpenSSL headers */
428
429 #define OPENSSL_THREAD_DEFINES
430 #include <openssl/opensslconf.h>
431 /* opensslv.h requires prior opensslconf.h to include -fips in version string */
432 #include <openssl/opensslv.h>
433
434 #if OPENSSL_VERSION_NUMBER<0x0090800fL
435 #error OpenSSL 0.9.8 or later is required
436 #endif /* OpenSSL older than 0.9.8 */
437
438 #if defined(USE_PTHREAD) && !defined(OPENSSL_THREADS)
439 #error OpenSSL library compiled without thread support
440 #endif /* !OPENSSL_THREADS && USE_PTHREAD */
441
442 /* non-blocking OCSP API is not available before OpenSSL 0.9.8h */
443 #if OPENSSL_VERSION_NUMBER<0x00908080L
444 #ifndef OPENSSL_NO_OCSP
445 #define OPENSSL_NO_OCSP
446 #endif /* !defined(OPENSSL_NO_OCSP) */
447 #endif /* OpenSSL older than 0.9.8h */
448
449 #if OPENSSL_VERSION_NUMBER<0x00908060L
450 #define OPENSSL_NO_TLSEXT
451 #endif /* OpenSSL older than 0.9.8f */
452
453 #if OPENSSL_VERSION_NUMBER<0x10000000L
454 #define OPENSSL_NO_PSK
455 #endif /* OpenSSL older than 1.0.0 */
456
457 #if OPENSSL_VERSION_NUMBER<0x10001000L || defined(OPENSSL_NO_TLS1)
458 #define OPENSSL_NO_TLS1_1
459 #define OPENSSL_NO_TLS1_2
460 #endif /* OpenSSL older than 1.0.1 || defined(OPENSSL_NO_TLS1) */
461
462 #if OPENSSL_VERSION_NUMBER>=0x10100000L
463 #ifndef OPENSSL_NO_SSL2
464 #define OPENSSL_NO_SSL2
465 #endif /* !defined(OPENSSL_NO_SSL2) */
466 #else /* OpenSSL older than 1.1.0 */
467 #define X509_STORE_CTX_get0_chain(x) X509_STORE_CTX_get_chain(x)
468 #define OPENSSL_hexstr2buf string_to_hex
469 #endif /* OpenSSL 1.1.0 or newer */
470
471 #if OPENSSL_VERSION_NUMBER<0x10101000L
472 #define OPENSSL_NO_TLS1_3
473 #endif /* OpenSSL older than 1.1.1 */
474
475 #ifdef USE_WIN32
476 #define USE_FIPS
477 #endif
478
479 #include <openssl/conf.h>
480 #include <openssl/lhash.h>
481 #include <openssl/ssl.h>
482 #include <openssl/ui.h>
483 #include <openssl/err.h>
484 #include <openssl/crypto.h> /* for CRYPTO_* and SSLeay_version */
485 #include <openssl/x509v3.h>
486 #include <openssl/rand.h>
487 #include <openssl/bn.h>
488 #include <openssl/pkcs12.h>
489 #ifndef OPENSSL_NO_MD4
490 #include <openssl/md4.h>
491 #endif /* !defined(OPENSSL_NO_MD4) */
492 #include <openssl/des.h>
493 #ifndef OPENSSL_NO_DH
494 #include <openssl/dh.h>
495 #endif /* !defined(OPENSSL_NO_DH) */
496 #ifndef OPENSSL_NO_ENGINE
497 #include <openssl/engine.h>
498 #endif /* !defined(OPENSSL_NO_ENGINE) */
499 #ifndef OPENSSL_NO_OCSP
500 #include <openssl/ocsp.h>
501 #endif /* !defined(OPENSSL_NO_OCSP) */
502 #ifndef OPENSSL_NO_COMP
503 /* not defined in public headers before OpenSSL 0.9.8 */
504 STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void);
505 #endif /* !defined(OPENSSL_NO_COMP) */
506 #if OPENSSL_VERSION_NUMBER>=0x10101000L
507 #include <openssl/store.h>
508 #include <openssl/storeerr.h>
509 #endif /* OPENSSL_VERSION_NUMBER>=0x10101000L */
510 #if OPENSSL_VERSION_NUMBER>=0x30000000L
511 #include <openssl/provider.h>
512 #include <openssl/proverr.h>
513 #endif /* OPENSSL_VERSION_NUMBER>=0x30000000L */
514
515 #ifndef OPENSSL_VERSION
516 #define OPENSSL_VERSION SSLEAY_VERSION
517 #define OpenSSL_version_num() SSLeay()
518 #define OpenSSL_version(x) SSLeay_version(x)
519 #endif
520
521 /**************************************** other defines */
522
523 /* always use IPv4 defaults! */
524 #define DEFAULT_LOOPBACK "127.0.0.1"
525 #define DEFAULT_ANY "0.0.0.0"
526 #if 0
527 #define DEFAULT_LOOPBACK "::1"
528 #define DEFAULT_ANY "::"
529 #endif
530
531 #if defined (USE_WIN32) || defined (__vms)
532 #define LOG_EMERG 0
533 #define LOG_ALERT 1
534 #define LOG_CRIT 2
535 #define LOG_ERR 3
536 #define LOG_WARNING 4
537 #define LOG_NOTICE 5
538 #define LOG_INFO 6
539 #define LOG_DEBUG 7
540 #endif /* defined (USE_WIN32) || defined (__vms) */
541
542 #ifndef offsetof
543 #define offsetof(T, F) ((unsigned)((char *)&((T *)0L)->F - (char *)0L))
544 #endif
545
546 #define is_prefix(a, b) (strncasecmp((a), (b), strlen(b))==0)
547
548 #ifndef va_copy
549 #ifdef __va_copy
550 #define va_copy(dst, src) __va_copy((dst), (src))
551 #else /* __va_copy */
552 #define va_copy(dst, src) memcpy(&(dst), &(src), sizeof(va_list))
553 #endif /* __va_copy */
554 #endif /* va_copy */
555
556 #endif /* defined COMMON_H */
557
558 /* end of common.h */