Debian Codegraph @

src/prototypes.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 PROTOTYPES_H
39 #define PROTOTYPES_H
40
41 #include "common.h"
42
43 #if defined(USE_PTHREAD) || defined(USE_WIN32)
44 #define USE_OS_THREADS
45 #endif
46
47 #ifdef USE_OS_THREADS
48 #ifndef USE_WIN32
49 /* FIXME: waiting for a single terminate_pipe socket
50 * from multiple threads causes a deadlock on WIN32 */
51 #define USE_TERMINATE_PIPE
52 #endif /* USE_WIN32 */
53 #endif /* USE_OS_THREADS */
54
55 /**************************************** forward declarations */
56
57 typedef struct tls_data_struct TLS_DATA;
58 typedef struct sock_opt_struct SOCK_OPT;
59 typedef struct client_data_struct CLI;
60 typedef struct global_options_struct GLOBAL_OPTIONS;
61 typedef struct service_options_struct SERVICE_OPTIONS;
62 #ifndef OPENSSL_NO_TLSEXT
63 typedef struct servername_list_struct SERVERNAME_LIST;
64 #endif /* !defined(OPENSSL_NO_TLSEXT) */
65
66 /**************************************** data structures */
67
68 #ifdef USE_PTHREAD
69 typedef pthread_t THREAD_ID;
70 #endif
71 #ifdef USE_WIN32
72 typedef HANDLE THREAD_ID;
73 #endif
74
75 #if OPENSSL_VERSION_NUMBER<0x10100004L
76
77 #ifdef USE_OS_THREADS
78
79 struct CRYPTO_dynlock_value {
80 #ifdef USE_PTHREAD
81 pthread_rwlock_t rwlock;
82 #endif
83 #ifdef USE_WIN32
84 CRITICAL_SECTION critical_section;
85 #endif
86 #ifdef DEBUG_LOCKS
87 const char *init_file, *read_lock_file, *write_lock_file,
88 *unlock_file, *destroy_file;
89 int init_line, read_lock_line, write_lock_line, unlock_line, destroy_line;
90 #endif
91 };
92
93 typedef struct CRYPTO_dynlock_value CRYPTO_RWLOCK;
94
95 #else /* USE_OS_THREADS */
96
97 typedef void CRYPTO_RWLOCK;
98
99 #endif /* USE_OS_THREADS */
100
101 #endif /* OPENSSL_VERSION_NUMBER<0x10100004L */
102
103 typedef enum {
104 LOCK_THREAD_LIST, /* sthreads.c */
105 LOCK_SESSION, LOCK_ADDR,
106 LOCK_CLIENTS, LOCK_SSL, /* client.c */
107 LOCK_REF, /* options.c */
108 LOCK_INET, /* resolver.c */
109 #ifndef USE_WIN32
110 LOCK_LIBWRAP, /* libwrap.c */
111 #endif
112 LOCK_LOG_BUFFER, LOCK_LOG_MODE, /* log.c */
113 LOCK_LEAK_HASH, LOCK_LEAK_RESULTS, /* str.c */
114 #ifndef OPENSSL_NO_DH
115 LOCK_DH, /* ctx.c */
116 #endif /* OPENSSL_NO_DH */
117 #ifdef USE_WIN32
118 LOCK_WIN_LOG, /* ui_win_gui.c */
119 #endif
120 LOCK_SECTIONS, /* traversing section list */
121 #if !defined(HAVE_LOCALTIME_R) || !defined(_REENTRANT)
122 LOCK_LOCALTIME, /* unsafe localtime() */
123 #endif
124 STUNNEL_LOCKS /* number of locks */
125 } LOCK_TYPE;
126
127 #if defined (USE_WIN32)
128 #define ICON_IMAGE HICON
129 #elif defined(__APPLE__)
130 #define ICON_IMAGE void *
131 #endif
132
133 typedef enum {
134 ICON_ERROR,
135 ICON_IDLE,
136 ICON_ACTIVE,
137 ICON_NONE /* it has to be the last one */
138 } ICON_TYPE;
139
140 typedef enum {
141 LOG_MODE_BUFFER,
142 LOG_MODE_ERROR,
143 LOG_MODE_INFO,
144 LOG_MODE_CONFIGURED
145 } LOG_MODE;
146
147 typedef enum {
148 LOG_ID_SEQUENTIAL,
149 LOG_ID_UNIQUE,
150 LOG_ID_THREAD,
151 LOG_ID_PROCESS
152 } LOG_ID;
153
154 typedef enum {
155 FILE_MODE_READ,
156 FILE_MODE_APPEND,
157 FILE_MODE_OVERWRITE
158 } FILE_MODE;
159
160 typedef union sockaddr_union {
161 struct sockaddr sa;
162 struct sockaddr_in in;
163 #ifdef USE_IPV6
164 struct sockaddr_in6 in6;
165 #endif
166 #ifdef HAVE_STRUCT_SOCKADDR_UN
167 struct sockaddr_un un;
168 #endif
169 } SOCKADDR_UNION;
170
171 typedef struct name_list_struct {
172 char *name;
173 struct name_list_struct *next;
174 } NAME_LIST;
175
176 typedef struct sockaddr_list { /* list of addresses */
177 struct sockaddr_list *parent; /* used by copies to locate their parent */
178 SOCKADDR_UNION *addr; /* array of resolved addresses */
179 unsigned start; /* initial address for round-robin failover */
180 unsigned num; /* how many addresses are used */
181 int passive; /* listening socket */
182 NAME_LIST *names; /* a list of unresolved names */
183 } SOCKADDR_LIST;
184
185 extern GLOBAL_OPTIONS global_options;
186 #ifndef OPENSSL_NO_COMP
187 typedef enum {
188 COMP_NONE, /* empty compression algorithms set */
189 COMP_DEFLATE, /* default OpenSSL's compression algorithms set */
190 COMP_ZLIB, /* additional historic ZLIB compression algorithm id */
191 #if OPENSSL_VERSION_NUMBER>=0x30200000L
192 COMP_ZSTD, /* non-standard ZSTD compression algorithm id */
193 COMP_BROTLI, /* non-standard BROTLI compression algorithm id */
194 #endif
195 } COMP_TYPE;
196 #endif /* !defined(OPENSSL_NO_COMP) */
197
198 struct global_options_struct {
199 /* some data for TLS initialization in ssl.c */
200 #ifndef OPENSSL_NO_COMP
201 COMP_TYPE compression; /* compression type */
202 #endif /* !defined(OPENSSL_NO_COMP) */
203 char *egd_sock; /* entropy gathering daemon socket */
204 char *rand_file; /* file with random data */
205 long random_bytes; /* how many random bytes to read */
206
207 /* some global data for stunnel.c */
208 #ifndef USE_WIN32
209 #ifdef HAVE_CHROOT
210 char *chroot_dir;
211 #endif
212 char *pidfile;
213 #endif
214
215 /* logging-support data for log.c */
216 #ifndef USE_WIN32
217 int log_facility; /* debug facility for syslog */
218 #endif
219 char *output_file;
220 FILE_MODE log_file_mode;
221
222 /* user interface configuration */
223 #ifdef ICON_IMAGE
224 ICON_IMAGE icon[ICON_NONE]; /* user-specified GUI icons */
225 #endif
226
227 /* on/off switches */
228 struct {
229 unsigned rand_write:1; /* overwrite rand_file */
230 #ifdef USE_WIN32
231 unsigned taskbar:1; /* enable the taskbar icon */
232 #else /* !USE_WIN32 */
233 unsigned foreground:1;
234 unsigned log_stderr:1;
235 unsigned log_syslog:1;
236 #endif
237 #ifdef USE_FIPS
238 unsigned fips:1; /* enable FIPS 140-2 mode */
239 #endif
240 } option;
241 };
242
243 #ifndef OPENSSL_NO_PSK
244 typedef struct psk_keys_struct {
245 const char *identity; /* the OpenSSL API requires const */
246 unsigned char *key_val;
247 unsigned key_len;
248 struct psk_keys_struct *next;
249 } PSK_KEYS;
250 typedef struct psk_table_struct {
251 PSK_KEYS **val;
252 size_t num;
253 } PSK_TABLE;
254 #endif /* !defined(OPENSSL_NO_PSK) */
255
256 #if OPENSSL_VERSION_NUMBER>=0x10000000L
257 typedef struct ticket_key_struct {
258 unsigned char *key_val;
259 int key_len;
260 } TICKET_KEY;
261 #endif /* OpenSSL 1.0.0 or later */
262
263 struct service_options_struct {
264 struct service_options_struct *next; /* next node in the services list */
265 SSL_CTX *ctx; /* TLS context */
266 char *servname; /* service name for logging & permission checking */
267 int ref; /* reference counter for delayed deallocation */
268
269 /* service-specific data for stunnel.c */
270 #ifndef USE_WIN32
271 uid_t uid;
272 gid_t gid;
273 #endif
274 int bound_ports; /* number of ports bound to this service */
275
276 /* service-specific data for log.c */
277 int log_level; /* debug level for logging */
278 LOG_ID log_id; /* logging session id type */
279
280 /* service-specific data for sthreads.c */
281 #ifndef USE_FORK
282 size_t stack_size; /* stack size for this thread */
283 #endif
284
285 /* some global data for network.c */
286 SOCK_OPT *sock_opts;
287
288 /* service-specific data for verify.c */
289 #ifndef OPENSSL_NO_ENGINE
290 NAME_LIST *ca_engine; /* engine-specific CA certificate identifier list */
291 #endif
292 char *ca_dir; /* directory containing hashed CA certs */
293 char *ca_file; /* file containing concatenated CA certs */
294 #if OPENSSL_VERSION_NUMBER>=0x30000000L
295 char *ca_store; /* store of CA certs */
296 #endif
297 char *crl_dir; /* directory containing hashed CRLs */
298 char *crl_file; /* file containing concatenated CRLs */
299 #ifndef OPENSSL_NO_OCSP
300 char *ocsp_url;
301 unsigned long ocsp_flags;
302 CRYPTO_RWLOCK *ocsp_response_lock; /* protect the OCSP response cache */
303 unsigned char *ocsp_response_der; /* OCSP response data */
304 int ocsp_response_len; /* OCSP response length */
305 unsigned stapling_cb_flag:1; /* OCSP stapling callback executed */
306 unsigned verify_cb_flag:1; /* verify callback executed at depth 0 */
307 #endif /* !defined(OPENSSL_NO_OCSP) */
308 #if OPENSSL_VERSION_NUMBER>=0x10002000L
309 NAME_LIST *check_host, *check_email, *check_ip; /* cert subject checks */
310 NAME_LIST *config; /* OpenSSL CONF options */
311 #endif /* OPENSSL_VERSION_NUMBER>=0x10002000L */
312
313 /* service-specific data for ctx.c */
314 char *cipher_list;
315 #ifndef OPENSSL_NO_TLS1_3
316 char *ciphersuites;
317 #endif /* TLS 1.3 */
318 NAME_LIST *cert; /* list of certificate identifiers (URI or filename) */
319 char *key; /* pem (priv key/cert) filename */
320 long session_size, session_timeout;
321 #if OPENSSL_VERSION_NUMBER>=0x10100000L
322 int security_level;
323 #endif /* OpenSSL 1.1.0 or later */
324 uint64_t ssl_options_set;
325 #if OPENSSL_VERSION_NUMBER>=0x009080dfL
326 uint64_t ssl_options_clear;
327 #endif /* OpenSSL 0.9.8m or later */
328 #if OPENSSL_VERSION_NUMBER>=0x10100000L
329 int min_proto_version, max_proto_version;
330 #else /* OPENSSL_VERSION_NUMBER<0x10100000L */
331 SSL_METHOD *client_method, *server_method;
332 #endif /* OPENSSL_VERSION_NUMBER<0x10100000L */
333 SOCKADDR_UNION sessiond_addr;
334 #ifndef OPENSSL_NO_TLSEXT
335 char *sni;
336 SERVERNAME_LIST *servername_list_head, *servername_list_tail;
337 #endif /* !defined(OPENSSL_NO_TLSEXT) */
338 #ifndef OPENSSL_NO_PSK
339 char *psk_identity;
340 PSK_KEYS *psk_keys, *psk_selected;
341 PSK_TABLE psk_sorted;
342 #endif /* !defined(OPENSSL_NO_PSK) */
343 #ifndef OPENSSL_NO_ECDH
344 char *curves;
345 #endif /* !defined(OPENSSL_NO_ECDH) */
346 #ifndef OPENSSL_NO_ENGINE
347 ENGINE *engine; /* engine to read the private key */
348 #endif /* !defined(OPENSSL_NO_ENGINE) */
349 #if OPENSSL_VERSION_NUMBER>=0x10000000L
350 TICKET_KEY *ticket_key; /* key for handling session tickets */
351 TICKET_KEY *ticket_mac; /* key for protecting session tickets */
352 #endif /* OpenSSL 1.0.0 or later */
353
354 /* service-specific data for client.c */
355 char *exec_name; /* program name for local mode */
356 #ifdef USE_WIN32
357 char *exec_args; /* program arguments for local mode */
358 #else
359 char **exec_args; /* program arguments for local mode */
360 #endif
361 SOCKADDR_UNION source_addr;
362 SOCKADDR_LIST local_addr, connect_addr, redirect_addr;
363 SOCKET *local_fd; /* array of accepting file descriptors */
364 SSL_SESSION **connect_session; /* per-destination client session cache */
365 SSL_SESSION *session; /* previous client session for delayed resolver */
366 int timeout_busy; /* maximum waiting for data time */
367 int timeout_close; /* maximum close_notify time */
368 int timeout_connect; /* maximum s_connect() time */
369 int timeout_idle; /* maximum idle connection time */
370 #ifndef OPENSSL_NO_OCSP
371 int timeout_ocsp; /* maximum s_connect() time for OCSP */
372 #endif /* !OPENSSL_NO_OCSP */
373 enum {FAILOVER_RR, FAILOVER_PRIO} failover; /* failover strategy */
374 unsigned rr; /* per-service sequential number for round-robin failover */
375 char *username; /* ident client username */
376 long retry; /* retry delay for remote+program loop or -1 if disabled */
377
378 /* service-specific data for protocol.c */
379 char *protocol;
380 void (*protocol_early)(CLI *);
381 void (*protocol_middle)(CLI *);
382 void (*protocol_late)(CLI *);
383 NAME_LIST *protocol_header;
384 char *protocol_host;
385 char *protocol_domain;
386 char *protocol_username;
387 char *protocol_password;
388 char *protocol_authentication;
389
390 /* service-specific data for ui_*.c */
391 #ifdef USE_WIN32
392 LPTSTR file, help;
393 #endif
394 unsigned section_number;
395 char *chain;
396
397 /* on/off switches */
398 struct {
399 unsigned request_cert:1; /* request a peer certificate */
400 unsigned require_cert:1; /* require a client certificate */
401 unsigned verify_chain:1; /* verify certificate chain */
402 unsigned verify_peer:1; /* verify peer certificate */
403 unsigned accept:1; /* endpoint: accept */
404 unsigned client:1;
405 unsigned delayed_lookup:1;
406 #ifdef USE_LIBWRAP
407 unsigned libwrap:1;
408 #endif
409 unsigned local:1; /* outgoing interface specified */
410 unsigned session_resume:1; /* enable session resumption */
411 unsigned sessiond:1;
412 #ifndef USE_WIN32
413 unsigned pty:1;
414 unsigned transparent_src:1;
415 #endif
416 unsigned transparent_dst:1; /* endpoint: transparent destination */
417 unsigned protocol_endpoint:1; /* dynamic target from the protocol */
418 unsigned reset:1; /* reset sockets on error */
419 unsigned renegotiation:1;
420 unsigned connect_before_ssl:1;
421 #ifndef OPENSSL_NO_OCSP
422 unsigned aia:1; /* Authority Information Access */
423 unsigned nonce:1; /* send and verify OCSP nonce */
424 unsigned ocsp_require:1; /* require a conclusive OCSP response */
425 #endif /* !defined(OPENSSL_NO_OCSP) */
426 #ifndef OPENSSL_NO_DH
427 unsigned dh_temp_params:1;
428 #endif /* OPENSSL_NO_DH */
429 #ifndef USE_WIN32
430 unsigned log_stderr:1; /* a copy of the global switch */
431 #endif /* USE_WIN32 */
432 unsigned default_local_addr:1;
433 unsigned default_ca_engine:1;
434 unsigned default_cert:1;
435 unsigned default_check_email:1;
436 unsigned default_check_host:1;
437 unsigned default_check_ip:1;
438 unsigned default_config:1;
439 unsigned default_connect_addr:1;
440 unsigned default_protocol_header:1;
441 unsigned default_redirect_addr:1;
442 } option;
443 };
444
445 extern SERVICE_OPTIONS service_options;
446
447 #ifndef OPENSSL_NO_TLSEXT
448 struct servername_list_struct {
449 char *servername;
450 SERVICE_OPTIONS *opt;
451 struct servername_list_struct *next;
452 };
453 #endif /* !defined(OPENSSL_NO_TLSEXT) */
454
455 typedef enum {
456 TYPE_NONE, TYPE_FLAG, TYPE_INT, TYPE_LINGER, TYPE_TIMEVAL, TYPE_STRING
457 } VAL_TYPE;
458
459 typedef union {
460 int i_val;
461 long l_val;
462 char c_val[16];
463 struct linger linger_val;
464 struct timeval timeval_val;
465 } OPT_UNION;
466
467 struct sock_opt_struct {
468 const char *opt_str;
469 int opt_level;
470 int opt_name;
471 VAL_TYPE opt_type;
472 OPT_UNION *opt_val[3];
473 };
474
475 typedef enum {
476 CONF_RELOAD, CONF_FILE, CONF_FD
477 } CONF_TYPE;
478
479 /* s_poll_set definition for network.c */
480
481 typedef struct {
482 #if defined(USE_POLL) || defined(USE_WIN32)
483 unsigned capacity;
484 #endif
485 #ifdef USE_POLL
486 struct pollfd *ufds;
487 unsigned nfds;
488 #else /* select */
489 fd_set *irfds, *orfds, *iwfds, *owfds;
490 #ifndef USE_WIN32
491 fd_set *ixfds, *oxfds;
492 #endif
493 SOCKET max;
494 #endif
495 int main_thread;
496 } s_poll_set;
497
498 typedef struct {
499 /* stdio is currently used, but alternative implementations can be added */
500 FILE *f;
501 } DISK_FILE;
502
503 /* definitions for client.c */
504
505 typedef struct {
506 SOCKET fd; /* file descriptor */
507 int is_socket; /* file descriptor is a socket */
508 } FD;
509
510 typedef enum {
511 RENEG_INIT, /* initial state */
512 RENEG_ESTABLISHED, /* initial handshake completed */
513 RENEG_DETECTED /* renegotiation detected */
514 } RENEG_STATE;
515
516 struct client_data_struct {
517 jmp_buf *exception_pointer;
518
519 SSL *ssl; /* TLS connection */
520 SERVICE_OPTIONS *opt;
521 TLS_DATA *tls;
522
523 #ifdef USE_OS_THREADS
524 THREAD_ID thread_id;
525 #endif
526 #ifndef USE_FORK
527 struct client_data_struct *thread_prev, *thread_next;
528 #endif
529
530 SOCKADDR_UNION peer_addr; /* peer address */
531 socklen_t peer_addr_len;
532 char *accepted_address; /* textual representation of the peer address */
533 SOCKADDR_UNION *bind_addr; /* address to bind() the socket */
534 SOCKADDR_LIST connect_addr; /* either copied or resolved dynamically */
535 unsigned idx; /* actually connected address in connect_addr */
536 FD local_rfd, local_wfd; /* read and write local descriptors */
537 FD remote_fd; /* remote file descriptor */
538 unsigned long pid; /* PID of the local process */
539 SOCKET fd; /* temporary file descriptor */
540 int fatal_alert; /* received a fatal alert */
541 RENEG_STATE reneg_state; /* used to track renegotiation attempts */
542 unsigned long long seq; /* sequential thread number for logging */
543 unsigned rr; /* per-client sequential number for round-robin failover */
544
545 /* data for transfer() function */
546 char sock_buff[BUFFSIZE]; /* socket read buffer */
547 char ssl_buff[BUFFSIZE]; /* TLS read buffer */
548 size_t sock_ptr, ssl_ptr; /* index of the first unused byte */
549 FD *sock_rfd, *sock_wfd; /* read and write socket descriptors */
550 FD *ssl_rfd, *ssl_wfd; /* read and write TLS descriptors */
551 uint64_t sock_bytes, ssl_bytes; /* bytes written to socket and TLS */
552 s_poll_set *fds; /* file descriptors */
553 struct {
554 unsigned redirect:1; /* redirect the destination */
555 #ifndef OPENSSL_NO_PSK
556 unsigned psk_found:1; /* PSK identity was found */
557 #endif /* !defined(OPENSSL_NO_PSK) */
558 } flag;
559 };
560
561 /**************************************** prototypes for stunnel.c */
562
563 #ifndef USE_FORK
564 extern int max_clients;
565 extern int num_clients;
566 #endif
567 extern SOCKET signal_pipe[2];
568 extern SOCKET terminate_pipe[2];
569
570 int stunnel_init(void);
571 void main_init(void);
572 int main_configure(char *, char *);
573 void main_cleanup(void);
574 int drop_privileges(int);
575 void daemon_loop(void);
576 void signal_post(uint8_t);
577 #if !defined(USE_WIN32) && !defined(USE_OS2)
578 void pid_status_hang(const char *);
579 #endif
580 void stunnel_info(int);
581
582 /**************************************** prototypes for options.c */
583
584 extern unsigned number_of_sections;
585
586 int options_cmdline(char *, char *);
587 int options_parse(CONF_TYPE);
588 void options_defaults(void);
589 void options_apply(void);
590 void options_free(int);
591
592 SERVICE_OPTIONS *service_up_ref(SERVICE_OPTIONS *);
593 void service_free(SERVICE_OPTIONS *);
594
595 /**************************************** prototypes for fd.c */
596
597 #ifndef USE_FORK
598 void get_limits(void); /* setup global max_clients and max_fds */
599 #endif
600 SOCKET s_socket(int, int, int, int, const char *);
601 int s_pipe(int[2], int, const char *);
602 int s_socketpair(int, int, int, SOCKET[2], int, const char *);
603 SOCKET s_accept(SOCKET, struct sockaddr *, socklen_t *, int, const char *);
604 void set_nonblock(SOCKET, unsigned long);
605
606 /**************************************** prototypes for log.c */
607
608 #define SINK_SYSLOG 1
609 #define SINK_OUTFILE 2
610
611 extern DISK_FILE *outfile;
612
613 int log_open(int);
614 void log_close(int);
615 void log_flush(LOG_MODE);
616 void s_log(int, const char *, ...)
617 #ifdef __GNUC__
618 __attribute__((format(printf, 2, 3)));
619 #else
620 ;
621 #endif
622 void s_vlog(int, const char *, va_list);
623 char *log_id(CLI *);
624 void fatal_debug(const char *, const char *, int) NORETURN;
625 #define fatal(a) fatal_debug((a), __FILE__, __LINE__)
626 void ioerror(const char *);
627 void sockerror(const char *);
628 void log_error(int, int, const char *);
629 char *s_strerror(int);
630 void bin2hexstring(const unsigned char *, size_t, char *, size_t);
631 void safe_localtime(struct tm *, time_t);
632
633 /**************************************** prototypes for pty.c */
634
635 int pty_allocate(int *, int *, char *);
636
637 /**************************************** prototypes for dhparam.c */
638
639 DH *get_dh2048(void);
640
641 /**************************************** prototypes for cron.c */
642
643 #ifdef USE_OS_THREADS
644 extern THREAD_ID per_second_thread_id;
645 extern THREAD_ID per_day_thread_id;
646 #endif
647
648 int cron_init(void);
649
650 /**************************************** prototypes for ssl.c */
651
652 extern int index_ssl_cli, index_ssl_ctx_opt;
653 extern int index_session_authenticated, index_session_connect_address;
654
655 #ifdef USE_FIPS
656 int fips_default(void);
657 int fips_available(void);
658 #endif
659 void crypto_init(void);
660 void crypto_cleanup(void);
661 int ssl_init(void);
662 void ssl_cleanup(void);
663 int ssl_configure(GLOBAL_OPTIONS *);
664
665 /**************************************** prototypes for ctx.c */
666
667 #ifndef OPENSSL_NO_DH
668 extern DH *dh_params;
669 extern int dh_temp_params;
670 #endif /* OPENSSL_NO_DH */
671
672 #if !defined(OPENSSL_NO_ENGINE) || OPENSSL_VERSION_NUMBER>=0x10101000L
673 extern UI_METHOD *ui_stunnel;
674 #endif /* !defined(OPENSSL_NO_ENGINE) || OPENSSL_VERSION_NUMBER>=0x10101000L*/
675
676 extern SERVICE_OPTIONS *current_section;
677
678 int ctx_init(void);
679 void ctx_cleanup(void);
680 int context_init(SERVICE_OPTIONS *);
681 void context_cleanup(SERVICE_OPTIONS *);
682 #if !defined(OPENSSL_NO_DH) && OPENSSL_VERSION_NUMBER<0x10100000L
683 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
684 #endif /* !defined(OPENSSL_NO_DH) && OPENSSL_VERSION_NUMBER<0x10100000L */
685 #ifndef OPENSSL_NO_PSK
686 void psk_sort(PSK_TABLE *, PSK_KEYS *);
687 PSK_KEYS *psk_find(const PSK_TABLE *, const char *);
688 #endif /* !defined(OPENSSL_NO_PSK) */
689 void print_session_id(const char *, SSL_SESSION *);
690 void ssl_error(CLI *, const char *);
691
692 /**************************************** prototypes for verify.c */
693
694 int verify_init(SERVICE_OPTIONS *);
695 #ifndef OPENSSL_NO_ENGINE
696 X509 *engine_get_cert(ENGINE *, const char *);
697 #endif
698 void print_CA_list(const char *, const STACK_OF(X509_NAME) *);
699 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
700 char *X509_NAME2text(const X509_NAME *);
701 #else /* OpenSSL 1.1.0 or newer */
702 char *X509_NAME2text(X509_NAME *);
703 #endif /* OpenSSL 1.1.0 or newer */
704
705 /**************************************** prototypes for ocsp.c */
706
707 #ifndef OPENSSL_NO_OCSP
708 int ocsp_check(CLI *, X509_STORE_CTX *); /* OCSP client-driven checking */
709 int ocsp_init(SERVICE_OPTIONS *); /* OCSP stapling initialization */
710 void ocsp_cleanup(SERVICE_OPTIONS *);
711 #if OPENSSL_VERSION_NUMBER>=0x10002000L
712 int ocsp_stapling(SERVICE_OPTIONS *);
713 #endif /* OPENSSL_VERSION_NUMBER>=0x10002000L */
714 #endif /* !defined(OPENSSL_NO_OCSP) */
715
716 /**************************************** prototypes for network.c */
717
718 s_poll_set *s_poll_alloc(void);
719 void s_poll_free(s_poll_set *);
720 void s_poll_init(s_poll_set *, int);
721 void s_poll_add(s_poll_set *, SOCKET, int, int);
722 void s_poll_remove(s_poll_set *, SOCKET);
723 int s_poll_canread(s_poll_set *, SOCKET);
724 int s_poll_canwrite(s_poll_set *, SOCKET);
725 int s_poll_hup(s_poll_set *, SOCKET);
726 int s_poll_rdhup(s_poll_set *, SOCKET);
727 int s_poll_err(s_poll_set *, SOCKET);
728 int s_poll_wait(s_poll_set *, int, int);
729 void s_poll_dump(s_poll_set *, int);
730 void s_poll_sleep(int, int);
731
732 #ifdef USE_WIN32
733 #define SIGNAL_TERMINATE 1
734 #define SIGNAL_RELOAD_CONFIG 2
735 #define SIGNAL_REOPEN_LOG 3
736 #define SIGNAL_CONNECTIONS 4
737 #else
738 #define SIGNAL_TERMINATE SIGTERM
739 #define SIGNAL_RELOAD_CONFIG SIGHUP
740 #define SIGNAL_REOPEN_LOG SIGUSR1
741 #define SIGNAL_CONNECTIONS SIGUSR2
742 #endif
743
744 int socket_options_set(SERVICE_OPTIONS *, SOCKET, int);
745 int make_sockets(SOCKET[2]);
746 int original_dst(const SOCKET, SOCKADDR_UNION *);
747 int socket_needs_retry(CLI *, const char *);
748
749 /**************************************** prototypes for client.c */
750
751 CLI *alloc_client_session(SERVICE_OPTIONS *, SOCKET, SOCKET);
752 #if defined(USE_WIN32) || defined(USE_OS2)
753 unsigned __stdcall
754 #else
755 void *
756 #endif
757 client_thread(void *);
758 void client_main(CLI *);
759 void client_free(CLI *);
760 void throw_exception(CLI *, int) NORETURN;
761
762 /**************************************** prototypes for network.c */
763
764 int get_socket_error(const SOCKET);
765 int s_connect(CLI *, SOCKADDR_UNION *, socklen_t, int);
766 void s_write(CLI *, SOCKET fd, const void *, size_t);
767 size_t s_read_eof(CLI *, SOCKET fd, void *, size_t);
768 void s_read(CLI *, SOCKET fd, void *, size_t);
769 void fd_putline(CLI *, SOCKET, const char *);
770 char *fd_getline(CLI *, SOCKET);
771 /* descriptor versions of fprintf/fscanf */
772 void fd_printf(CLI *, SOCKET, const char *, ...)
773 #ifdef __GNUC__
774 __attribute__((format(printf, 3, 4)));
775 #else
776 ;
777 #endif
778 void s_ssl_write(CLI *, const void *, int);
779 size_t s_ssl_read_eof(CLI *, void *, int);
780 void s_ssl_read(CLI *, void *, int);
781 char *ssl_getstring(CLI *c);
782 char *ssl_getline(CLI *c);
783 void ssl_putline(CLI *c, const char *);
784 void ssl_printf(CLI *, const char *, ...)
785 #ifdef __GNUC__
786 __attribute__((format(printf, 2, 3)));
787 #else
788 ;
789 #endif
790
791 /**************************************** prototype for protocol.c */
792
793 #ifdef USE_WIN32
794 extern HWND capwin_hwnd;
795 extern LONG capwin_connectivity;
796 #endif
797
798 const char *protocol_init(SERVICE_OPTIONS *);
799
800 /**************************************** prototypes for resolver.c */
801
802 void resolver_init(void);
803 int use_ipv6(void);
804
805 unsigned name2addr(SOCKADDR_UNION *, char *, int);
806 unsigned hostport2addr(SOCKADDR_UNION *, char *, const char *, int);
807
808 unsigned name2addrlist(SOCKADDR_LIST *, char *);
809 unsigned hostport2addrlist(SOCKADDR_LIST *, char *, const char *);
810
811 void addrlist_clear(SOCKADDR_LIST *, int);
812 unsigned addrlist_dup(SOCKADDR_LIST *, const SOCKADDR_LIST *);
813 unsigned addrlist_resolve(SOCKADDR_LIST *);
814
815 char *s_ntop(SOCKADDR_UNION *, socklen_t);
816 socklen_t addr_len(const SOCKADDR_UNION *);
817 const char *s_gai_strerror(int);
818
819 #ifndef HAVE_GETNAMEINFO
820
821 #ifndef NI_NUMERICHOST
822 #define NI_NUMERICHOST 2
823 #endif
824 #ifndef NI_NUMERICSERV
825 #define NI_NUMERICSERV 8
826 #endif
827
828 #ifdef USE_WIN32
829 /* rename some locally shadowed declarations */
830 #define getnameinfo local_getnameinfo
831 #endif /* USE_WIN32 */
832
833 int getnameinfo(const struct sockaddr *, socklen_t,
834 char *, size_t, char *, size_t, int);
835
836 #endif /* !defined HAVE_GETNAMEINFO */
837
838 /**************************************** prototypes for sthreads.c */
839
840 #ifndef USE_FORK
841 extern CLI *thread_head;
842 #endif
843
844 extern CRYPTO_RWLOCK *stunnel_locks[STUNNEL_LOCKS];
845
846 #if OPENSSL_VERSION_NUMBER<0x10100004L
847 /* Emulate the OpenSSL 1.1 locking API for older OpenSSL versions */
848 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void);
849 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *);
850 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *);
851 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *);
852 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *);
853 int CRYPTO_atomic_add(int *, int, int *, CRYPTO_RWLOCK *);
854 #endif
855
856 int sthreads_init(void);
857 unsigned long stunnel_process_id(void);
858 unsigned long stunnel_thread_id(void);
859 int create_client(SOCKET, SOCKET, CLI *);
860
861 #ifdef USE_UCONTEXT
862 typedef struct CONTEXT_STRUCTURE {
863 char *stack; /* CPU stack for this thread */
864 unsigned long id;
865 ucontext_t context;
866 s_poll_set *fds;
867 int ready; /* number of ready file descriptors */
868 time_t finish; /* when to finish poll() for this context */
869 struct CONTEXT_STRUCTURE *next; /* next context on a list */
870 void *tls; /* thread local storage for tls.c */
871 } CONTEXT;
872 extern CONTEXT *ready_head, *ready_tail;
873 extern CONTEXT *waiting_head, *waiting_tail;
874 #endif
875
876 #ifdef _WIN32_WCE
877 long _beginthread(void (*)(void *), int, void *);
878 void _endthread(void);
879 #endif
880
881 #ifdef DEBUG_STACK_SIZE
882 void stack_info(size_t, int);
883 void ignore_value(void *);
884 #endif
885
886 /**************************************** prototypes for file.c */
887
888 #ifndef USE_WIN32
889 DISK_FILE *file_fdopen(int, FILE_MODE mode);
890 #endif
891 DISK_FILE *file_open(char *, FILE_MODE mode);
892 void file_close(DISK_FILE *);
893 ssize_t file_getline(DISK_FILE *, char *, int);
894 ssize_t file_putline_nonewline(DISK_FILE *, char *);
895 ssize_t file_putline_newline(DISK_FILE *, char *);
896 int file_flush(DISK_FILE *);
897 int file_permissions(const char *);
898
899 #ifdef USE_WIN32
900 LPTSTR str2tstr(LPCSTR);
901 LPSTR tstr2str(LPCTSTR);
902 #endif
903
904 /**************************************** prototypes for libwrap.c */
905
906 int libwrap_init(void);
907 void libwrap_auth(CLI *);
908
909 /**************************************** prototypes for tls.c */
910
911 extern volatile int tls_initialized;
912
913 void tls_init(void);
914 TLS_DATA *tls_alloc(CLI *, TLS_DATA *, const char *);
915 void tls_cleanup(void);
916 void tls_set(TLS_DATA *);
917 TLS_DATA *tls_get(void);
918
919 /**************************************** prototypes for str.c */
920
921 extern TLS_DATA *ui_tls;
922 typedef struct alloc_list_struct ALLOC_LIST;
923
924 struct tls_data_struct {
925 ALLOC_LIST *alloc_head;
926 size_t alloc_bytes, alloc_blocks;
927 CLI *c;
928 SERVICE_OPTIONS *opt;
929 const char *id;
930 };
931
932 void str_init(void);
933 void str_thread_init(TLS_DATA *);
934 void str_thread_cleanup(TLS_DATA *);
935 char *str_dup_debug(const char *, const char *, int);
936 #define str_dup(a) str_dup_debug((a), __FILE__, __LINE__)
937 char *str_dup_detached_debug(const char *, const char *, int);
938 #define str_dup_detached(a) str_dup_detached_debug((a), __FILE__, __LINE__)
939 char *str_vprintf(const char *, va_list);
940 char *str_printf(const char *, ...)
941 #ifdef __GNUC__
942 __attribute__((format(printf, 1, 2)));
943 #else
944 ;
945 #endif
946 #ifdef USE_WIN32
947 LPTSTR str_tprintf(LPCTSTR, ...);
948 #endif
949
950 void str_canary_init(void);
951 void str_stats(void);
952
953 void *str_alloc_debug(size_t, const char *, int);
954 #define str_alloc(a) str_alloc_debug((a), __FILE__, __LINE__)
955 void *str_alloc_detached_debug(size_t, const char *, int);
956 #define str_alloc_detached(a) str_alloc_detached_debug((a), __FILE__, __LINE__)
957
958 void *str_realloc_debug(void *, size_t, const char *, int);
959 #define str_realloc(a, b) str_realloc_debug((a), (b), __FILE__, __LINE__)
960 void *str_realloc_detached_debug(void *, size_t, const char *, int);
961 #define str_realloc_detached(a, b) str_realloc_detached_debug((a), (b), __FILE__, __LINE__)
962
963 void str_detach_debug(void *, const char *, int);
964 #define str_detach(a) str_detach_debug((a), __FILE__, __LINE__)
965 void str_detach_const_debug(const void *, const char *, int);
966 #define str_detach_const(a) str_detach_const_debug((a), __FILE__, __LINE__)
967
968 void str_free_debug(void *, const char *, int);
969 #define str_free(a) str_free_debug((a), __FILE__, __LINE__), (a)=NULL
970 #define str_free_expression(a) str_free_debug((a), __FILE__, __LINE__)
971 void str_free_const_debug(const void *, const char *, int);
972 #define str_free_const(a) str_free_const_debug((a), __FILE__, __LINE__), (a)=NULL
973
974 void leak_table_utilization(void);
975
976 int safe_memcmp(const void *, const void *, size_t);
977
978 /**************************************** prototypes for ui_*.c */
979
980 void ui_config_reloaded(void);
981 void ui_new_chain(const unsigned);
982 void ui_clients(const long);
983
984 void ui_new_log(const char *);
985 #ifdef USE_WIN32
986 void message_box(LPCTSTR, const UINT);
987 #endif /* USE_WIN32 */
988
989 int ui_passwd_cb(char *, int, int, void *);
990 #if !defined(OPENSSL_NO_ENGINE) || OPENSSL_VERSION_NUMBER>=0x10101000L
991 int (*ui_get_opener(void)) (UI *);
992 int (*ui_get_writer(void)) (UI *, UI_STRING *);
993 int (*ui_get_reader(void)) (UI *, UI_STRING *);
994 int (*ui_get_closer(void)) (UI *);
995 #endif /* !defined(OPENSSL_NO_ENGINE) || OPENSSL_VERSION_NUMBER>=0x10101000L */
996
997 #ifdef ICON_IMAGE
998 ICON_IMAGE load_icon_default(ICON_TYPE);
999 ICON_IMAGE load_icon_file(const char *);
1000 #endif
1001
1002 #endif /* defined PROTOTYPES_H */
1003
1004 /* end of prototypes.h */