| 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 |
#include "prototypes.h" |
| 39 |
|
| 40 |
/* http://www.openssl.org/support/faq.html#PROG2 */ |
| 41 |
#ifdef USE_WIN32 |
| 42 |
|
| 43 |
#ifdef __GNUC__ |
| 44 |
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 45 |
#pragma GCC diagnostic push |
| 46 |
#endif /* __GNUC__>=4.6 */ |
| 47 |
#pragma GCC diagnostic ignored "-Wpedantic" |
| 48 |
#endif /* __GNUC__ */ |
| 49 |
|
| 50 |
#include <openssl/applink.c> |
| 51 |
|
| 52 |
#ifdef __GNUC__ |
| 53 |
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 54 |
#pragma GCC diagnostic pop |
| 55 |
#endif /* __GNUC__>=4.6 */ |
| 56 |
#endif /* __GNUC__ */ |
| 57 |
|
| 58 |
#endif /* USE_WIN32 */ |
| 59 |
|
| 60 |
/**************************************** prototypes */ |
| 61 |
|
| 62 |
#ifdef __INNOTEK_LIBC__ |
| 63 |
struct sockaddr_un { |
| 64 |
u_char sun_len; /* sockaddr len including null */ |
| 65 |
u_char sun_family; /* AF_OS2 or AF_UNIX */ |
| 66 |
char sun_path[108]; /* path name */ |
| 67 |
}; |
| 68 |
#endif |
| 69 |
|
| 70 |
NOEXPORT void terminate_threads(void); |
| 71 |
#if !defined(USE_WIN32) && !defined(USE_OS2) |
| 72 |
NOEXPORT void pid_status_nohang(const char *); |
| 73 |
NOEXPORT void status_info(int, int, const char *); |
| 74 |
#endif |
| 75 |
NOEXPORT int accept_connection(SERVICE_OPTIONS *, unsigned); |
| 76 |
NOEXPORT int exec_connect_start(void); |
| 77 |
NOEXPORT void unbind_ports(void); |
| 78 |
NOEXPORT void unbind_port(SERVICE_OPTIONS *, unsigned); |
| 79 |
NOEXPORT int bind_ports(void); |
| 80 |
NOEXPORT SOCKET bind_port(SERVICE_OPTIONS *, int, unsigned); |
| 81 |
#ifdef HAVE_CHROOT |
| 82 |
NOEXPORT int change_root(void); |
| 83 |
#endif |
| 84 |
NOEXPORT int pipe_init(SOCKET [2], const char *); |
| 85 |
NOEXPORT int signal_pipe_dispatch(void); |
| 86 |
NOEXPORT void reload_config(void); |
| 87 |
NOEXPORT int process_connections(void); |
| 88 |
NOEXPORT char *signal_name(int); |
| 89 |
|
| 90 |
/**************************************** global variables */ |
| 91 |
|
| 92 |
SOCKET signal_pipe[2]={INVALID_SOCKET, INVALID_SOCKET}; |
| 93 |
#ifdef USE_TERMINATE_PIPE |
| 94 |
SOCKET terminate_pipe[2]={INVALID_SOCKET, INVALID_SOCKET}; |
| 95 |
#endif /* USE_TERMINATE_PIPE */ |
| 96 |
|
| 97 |
#ifndef USE_FORK |
| 98 |
int max_clients=0; |
| 99 |
/* -1 before a valid config is loaded, then the current number of clients */ |
| 100 |
int num_clients=-1; |
| 101 |
#endif |
| 102 |
s_poll_set *fds; /* file descriptors of listening sockets */ |
| 103 |
int systemd_fds; /* number of file descriptors passed by systemd */ |
| 104 |
int listen_fds_start; /* base for systemd-provided file descriptors */ |
| 105 |
|
| 106 |
/**************************************** startup */ |
| 107 |
|
| 108 |
/* this has to be the first function called from ui_*.c */ |
| 109 |
int stunnel_init(void) { /* basic initialization */ |
| 110 |
#ifdef USE_WIN32 |
| 111 |
static struct WSAData wsa_state; |
| 112 |
#endif |
| 113 |
|
| 114 |
tls_init(); /* initialize thread-local storage */ |
| 115 |
str_init(); /* initialize memory allocator */ |
| 116 |
crypto_init(); /* initialize libcrypto */ |
| 117 |
#ifdef USE_WIN32 |
| 118 |
if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) |
| 119 |
return 1; /* error */ |
| 120 |
#endif |
| 121 |
resolver_init(); |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
void main_init(void) { /* one-time initialization */ |
| 126 |
#ifdef USE_SYSTEMD |
| 127 |
int i; |
| 128 |
|
| 129 |
systemd_fds=sd_listen_fds(1); |
| 130 |
if(systemd_fds<0) |
| 131 |
fatal("systemd initialization failed"); |
| 132 |
listen_fds_start=SD_LISTEN_FDS_START; |
| 133 |
/* set non-blocking mode on systemd file descriptors */ |
| 134 |
for(i=0; i<systemd_fds; ++i) |
| 135 |
set_nonblock(listen_fds_start+i, 1); |
| 136 |
#else |
| 137 |
systemd_fds=0; /* no descriptors received */ |
| 138 |
listen_fds_start=3; /* the value is not really important */ |
| 139 |
#endif |
| 140 |
/* basic initialization contains essential functions required for logging |
| 141 |
* subsystem to function properly, thus all errors here are fatal */ |
| 142 |
if(ssl_init()) /* initialize libssl */ |
| 143 |
fatal("TLS initialization failed"); |
| 144 |
if(sthreads_init()) /* initialize critical sections & TLS callbacks */ |
| 145 |
fatal("Threads initialization failed"); |
| 146 |
if(ctx_init()) /* initialize shared data for SSL_CTX */ |
| 147 |
fatal("SSL_CTX initialization failed"); |
| 148 |
options_defaults(); /* initialize defaults */ |
| 149 |
options_apply(); /* apply the defaults */ |
| 150 |
#ifndef USE_FORK |
| 151 |
get_limits(); /* required by setup_fd() */ |
| 152 |
#endif |
| 153 |
fds=s_poll_alloc(); |
| 154 |
if(pipe_init(signal_pipe, "signal_pipe")) |
| 155 |
fatal("Signal pipe initialization failed: " |
| 156 |
"check your personal firewall"); |
| 157 |
#ifdef USE_TERMINATE_PIPE |
| 158 |
if(pipe_init(terminate_pipe, "terminate_pipe")) |
| 159 |
fatal("Terminate pipe initialization failed: " |
| 160 |
"check your personal firewall"); |
| 161 |
#endif /* USE_TERMINATE_PIPE */ |
| 162 |
stunnel_info(LOG_NOTICE); |
| 163 |
if(systemd_fds>0) |
| 164 |
s_log(LOG_INFO, "Systemd socket activation: %d descriptors received", |
| 165 |
systemd_fds); |
| 166 |
} |
| 167 |
|
| 168 |
/* return values: |
| 169 |
0 - configuration accepted |
| 170 |
1 - error |
| 171 |
2 - information printed |
| 172 |
*/ |
| 173 |
|
| 174 |
/* configuration-dependent initialization */ |
| 175 |
int main_configure(char *arg1, char *arg2) { |
| 176 |
int cmdline_status; |
| 177 |
|
| 178 |
log_flush(LOG_MODE_BUFFER); |
| 179 |
cmdline_status=options_cmdline(arg1, arg2); |
| 180 |
if(cmdline_status) { /* cannot proceed */ |
| 181 |
log_flush(LOG_MODE_ERROR); |
| 182 |
return cmdline_status; |
| 183 |
} |
| 184 |
options_free(1); /* free the defaults */ |
| 185 |
options_apply(); /* apply the new options */ |
| 186 |
str_canary_init(); /* needs prng initialization from options_cmdline */ |
| 187 |
/* log_open(SINK_SYSLOG) must be called before change_root() |
| 188 |
* to be able to access /dev/log socket */ |
| 189 |
log_open(SINK_SYSLOG); |
| 190 |
if(bind_ports()) { /* initial binding failed - restoring the defaults */ |
| 191 |
unbind_ports(); /* unbind the successfully bound ports */ |
| 192 |
options_free(1); /* free the current options */ |
| 193 |
options_defaults(); /* initialize defaults */ |
| 194 |
options_apply(); /* apply the defaults */ |
| 195 |
log_flush(LOG_MODE_ERROR); |
| 196 |
return 1; |
| 197 |
} |
| 198 |
|
| 199 |
#ifdef HAVE_CHROOT |
| 200 |
/* change_root() must be called before drop_privileges() |
| 201 |
* since chroot() needs root privileges */ |
| 202 |
if(change_root()) { |
| 203 |
log_flush(LOG_MODE_ERROR); |
| 204 |
return 1; |
| 205 |
} |
| 206 |
#endif /* HAVE_CHROOT */ |
| 207 |
|
| 208 |
if(drop_privileges(1)) { |
| 209 |
log_flush(LOG_MODE_ERROR); |
| 210 |
return 1; |
| 211 |
} |
| 212 |
|
| 213 |
/* log_open(SINK_OUTFILE) must be called after drop_privileges() |
| 214 |
* or logfile rotation won't be possible */ |
| 215 |
if(log_open(SINK_OUTFILE)) { |
| 216 |
log_flush(LOG_MODE_ERROR); |
| 217 |
return 1; |
| 218 |
} |
| 219 |
#ifndef USE_FORK |
| 220 |
num_clients=0; /* the first valid config */ |
| 221 |
#endif |
| 222 |
/* log_flush(LOG_MODE_CONFIGURED) must be called before daemonize() |
| 223 |
* since daemonize() invalidates stderr */ |
| 224 |
log_flush(LOG_MODE_CONFIGURED); |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
int drop_privileges(int critical) { |
| 229 |
#if defined(USE_WIN32) || defined(__vms) || defined(USE_OS2) |
| 230 |
(void)critical; /* squash the unused parameter warning */ |
| 231 |
#else |
| 232 |
#ifdef HAVE_SETGROUPS |
| 233 |
gid_t gr_list[1]; |
| 234 |
#endif |
| 235 |
|
| 236 |
/* set uid and gid */ |
| 237 |
if(service_options.gid) { |
| 238 |
if(setgid(service_options.gid) && critical) { |
| 239 |
sockerror("setgid"); |
| 240 |
return 1; |
| 241 |
} |
| 242 |
#ifdef HAVE_SETGROUPS |
| 243 |
gr_list[0]=service_options.gid; |
| 244 |
if(setgroups(1, gr_list) && critical) { |
| 245 |
sockerror("setgroups"); |
| 246 |
return 1; |
| 247 |
} |
| 248 |
#endif |
| 249 |
} |
| 250 |
if(service_options.uid) { |
| 251 |
if(setuid(service_options.uid) && critical) { |
| 252 |
sockerror("setuid"); |
| 253 |
return 1; |
| 254 |
} |
| 255 |
} |
| 256 |
#endif /* standard Unix */ |
| 257 |
return 0; |
| 258 |
} |
| 259 |
|
| 260 |
void main_cleanup(void) { |
| 261 |
/* stop accepting new connections */ |
| 262 |
unbind_ports(); |
| 263 |
s_poll_free(fds); |
| 264 |
fds=NULL; |
| 265 |
|
| 266 |
/* release per-client resources */ |
| 267 |
terminate_threads(); /* kill and join */ |
| 268 |
options_free(1); /* drop our reference to services */ |
| 269 |
/* TODO: force releasing options for references held by killed threads */ |
| 270 |
|
| 271 |
/* release shared resources */ |
| 272 |
ctx_cleanup(); |
| 273 |
ssl_cleanup(); |
| 274 |
crypto_cleanup(); /* no OpenSSL functionality beyond this point */ |
| 275 |
|
| 276 |
/* disable logging */ |
| 277 |
#if 0 |
| 278 |
str_stats(); /* main thread allocation tracking */ |
| 279 |
#endif |
| 280 |
log_flush(LOG_MODE_BUFFER); /* no more logs */ |
| 281 |
log_close(SINK_SYSLOG|SINK_OUTFILE); |
| 282 |
} |
| 283 |
|
| 284 |
#ifdef __GNUC__ |
| 285 |
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || defined(__clang__) |
| 286 |
#pragma GCC diagnostic push |
| 287 |
#pragma GCC diagnostic ignored "-Wunused-result" |
| 288 |
#endif /* __GNUC__>=4.6 */ |
| 289 |
#endif /* __GNUC__ */ |
| 290 |
NOEXPORT void terminate_threads(void) { |
| 291 |
#ifdef USE_TERMINATE_PIPE |
| 292 |
CLI *c; |
| 293 |
unsigned i, threads; |
| 294 |
THREAD_ID *thread_list; |
| 295 |
|
| 296 |
CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_THREAD_LIST]); |
| 297 |
threads=0; |
| 298 |
for(c=thread_head; c; c=c->thread_next) /* count client threads */ |
| 299 |
threads++; |
| 300 |
thread_list=str_alloc((threads+2)*sizeof(THREAD_ID)); |
| 301 |
i=0; |
| 302 |
for(c=thread_head; c; c=c->thread_next) { /* copy client threads */ |
| 303 |
thread_list[i++]=c->thread_id; |
| 304 |
s_log(LOG_DEBUG, "Terminating a thread for [%s]", c->opt->servname); |
| 305 |
} |
| 306 |
if(per_second_thread_id) { /* append per_second_thread_id if used */ |
| 307 |
thread_list[threads++]=per_second_thread_id; |
| 308 |
s_log(LOG_DEBUG, "Terminating the per-second thread"); |
| 309 |
} |
| 310 |
if(per_day_thread_id) { /* append per_day_thread_id if used */ |
| 311 |
thread_list[threads++]=per_day_thread_id; |
| 312 |
s_log(LOG_DEBUG, "Terminating the per-day thread"); |
| 313 |
} |
| 314 |
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_THREAD_LIST]); |
| 315 |
|
| 316 |
if(threads) { |
| 317 |
s_log(LOG_INFO, "Terminating %u service thread(s)", threads); |
| 318 |
writesocket(terminate_pipe[1], "", 1); |
| 319 |
for(i=0; i<threads; ++i) { /* join client threads */ |
| 320 |
#ifdef USE_PTHREAD |
| 321 |
if(pthread_join(thread_list[i], NULL)) |
| 322 |
s_log(LOG_ERR, "pthread_join() failed"); |
| 323 |
#endif |
| 324 |
#ifdef USE_WIN32 |
| 325 |
if(WaitForSingleObject(thread_list[i], INFINITE)==WAIT_FAILED) |
| 326 |
ioerror("WaitForSingleObject"); |
| 327 |
if(!CloseHandle(thread_list[i])) |
| 328 |
ioerror("CloseHandle"); |
| 329 |
#endif |
| 330 |
} |
| 331 |
s_log(LOG_INFO, "Service threads terminated"); |
| 332 |
} |
| 333 |
|
| 334 |
str_free(thread_list); |
| 335 |
#endif /* USE_TERMINATE_PIPE */ |
| 336 |
} |
| 337 |
#ifdef __GNUC__ |
| 338 |
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 339 |
#pragma GCC diagnostic pop |
| 340 |
#endif /* __GNUC__>=4.6 */ |
| 341 |
#endif /* __GNUC__ */ |
| 342 |
|
| 343 |
/**************************************** Unix-specific initialization */ |
| 344 |
|
| 345 |
#if !defined(USE_WIN32) && !defined(USE_OS2) |
| 346 |
|
| 347 |
NOEXPORT void pid_status_nohang(const char *info) { |
| 348 |
int pid, status; |
| 349 |
|
| 350 |
#ifdef HAVE_WAITPID /* POSIX.1 */ |
| 351 |
s_log(LOG_DEBUG, "Retrieving pid statuses with waitpid()"); |
| 352 |
while((pid=waitpid(-1, &status, WNOHANG))>0) |
| 353 |
status_info(pid, status, info); |
| 354 |
#elif defined(HAVE_WAIT4) /* 4.3BSD */ |
| 355 |
s_log(LOG_DEBUG, "Retrieving pid statuses with wait4()"); |
| 356 |
while((pid=wait4(-1, &status, WNOHANG, NULL))>0) |
| 357 |
status_info(pid, status, info); |
| 358 |
#else /* no support for WNOHANG */ |
| 359 |
pid_status_hang(info); |
| 360 |
#endif |
| 361 |
} |
| 362 |
|
| 363 |
void pid_status_hang(const char *info) { |
| 364 |
int pid, status; |
| 365 |
|
| 366 |
s_log(LOG_DEBUG, "Retrieving a pid status with wait()"); |
| 367 |
if((pid=wait(&status))>0) |
| 368 |
status_info(pid, status, info); |
| 369 |
} |
| 370 |
|
| 371 |
NOEXPORT void status_info(int pid, int status, const char *info) { |
| 372 |
#ifdef WIFSIGNALED |
| 373 |
if(WIFSIGNALED(status)) { |
| 374 |
char *sig_name=signal_name(WTERMSIG(status)); |
| 375 |
s_log(LOG_INFO, "%s %d terminated on %s", info, pid, sig_name); |
| 376 |
str_free(sig_name); |
| 377 |
} else { |
| 378 |
s_log(LOG_INFO, "%s %d finished with code %d", |
| 379 |
info, pid, WEXITSTATUS(status)); |
| 380 |
} |
| 381 |
#else |
| 382 |
s_log(LOG_INFO, "%s %d finished with status %d", info, pid, status); |
| 383 |
#endif |
| 384 |
} |
| 385 |
|
| 386 |
#endif /* !defined(USE_WIN32) && !defined(USE_OS2) */ |
| 387 |
|
| 388 |
/**************************************** main loop accepting connections */ |
| 389 |
|
| 390 |
void daemon_loop(void) { |
| 391 |
if(cron_init()) { /* initialize periodic events */ |
| 392 |
s_log(LOG_CRIT, "Cron initialization failed"); |
| 393 |
exit(1); |
| 394 |
} |
| 395 |
if(exec_connect_start()) { |
| 396 |
s_log(LOG_CRIT, "Failed to start exec+connect services"); |
| 397 |
exit(1); |
| 398 |
} |
| 399 |
s_log(LOG_INFO, "Accepting new connections"); |
| 400 |
while(1) { |
| 401 |
int temporary_lack_of_resources=0; |
| 402 |
int num=s_poll_wait(fds, -1, -1); |
| 403 |
if(num>=0) { |
| 404 |
SERVICE_OPTIONS *opt; |
| 405 |
s_log(LOG_DEBUG, "Found %d ready file descriptor(s)", num); |
| 406 |
if(service_options.log_level>=LOG_DEBUG) /* performance optimization */ |
| 407 |
s_poll_dump(fds, LOG_DEBUG); |
| 408 |
if(s_poll_canread(fds, signal_pipe[0])) |
| 409 |
if(signal_pipe_dispatch()) /* SIGNAL_TERMINATE or error */ |
| 410 |
break; /* terminate daemon_loop */ |
| 411 |
for(opt=service_options.next; opt; opt=opt->next) { |
| 412 |
unsigned i; |
| 413 |
for(i=0; i<opt->local_addr.num; ++i) { |
| 414 |
SOCKET fd=opt->local_fd[i]; |
| 415 |
if(fd!=INVALID_SOCKET && |
| 416 |
s_poll_canread(fds, fd) && |
| 417 |
accept_connection(opt, i)) |
| 418 |
temporary_lack_of_resources=1; |
| 419 |
} |
| 420 |
} |
| 421 |
} else { |
| 422 |
log_error(LOG_NOTICE, get_last_socket_error(), |
| 423 |
"daemon_loop: s_poll_wait"); |
| 424 |
temporary_lack_of_resources=1; |
| 425 |
} |
| 426 |
if(temporary_lack_of_resources) { |
| 427 |
s_log(LOG_NOTICE, |
| 428 |
"Accepting new connections suspended for 1 second"); |
| 429 |
s_poll_sleep(1, 0); /* to avoid log trashing */ |
| 430 |
} |
| 431 |
} |
| 432 |
leak_table_utilization(); |
| 433 |
} |
| 434 |
|
| 435 |
/* return 1 when a short delay is needed before another try */ |
| 436 |
NOEXPORT int accept_connection(SERVICE_OPTIONS *opt, unsigned i) { |
| 437 |
SOCKADDR_UNION addr; |
| 438 |
char *from_address; |
| 439 |
SOCKET s, fd=opt->local_fd[i]; |
| 440 |
socklen_t addrlen; |
| 441 |
|
| 442 |
addrlen=sizeof addr; |
| 443 |
for(;;) { |
| 444 |
s=s_accept(fd, &addr.sa, &addrlen, 1, "local socket"); |
| 445 |
if(s!=INVALID_SOCKET) /* success! */ |
| 446 |
break; |
| 447 |
switch(get_last_socket_error()) { |
| 448 |
case S_EINTR: /* interrupted by a signal */ |
| 449 |
break; /* retry now */ |
| 450 |
case S_EMFILE: |
| 451 |
#ifdef S_ENFILE |
| 452 |
case S_ENFILE: |
| 453 |
#endif |
| 454 |
#ifdef S_ENOBUFS |
| 455 |
case S_ENOBUFS: |
| 456 |
#endif |
| 457 |
#ifdef S_ENOMEM |
| 458 |
case S_ENOMEM: |
| 459 |
#endif |
| 460 |
return 1; /* temporary lack of resources */ |
| 461 |
default: |
| 462 |
return 0; /* any other error */ |
| 463 |
} |
| 464 |
} |
| 465 |
from_address=s_ntop(&addr, addrlen); |
| 466 |
s_log(LOG_DEBUG, "Service [%s] accepted (FD=%ld) from %s", |
| 467 |
opt->servname, (long)s, from_address); |
| 468 |
str_free(from_address); |
| 469 |
#ifdef USE_FORK |
| 470 |
RAND_add("", 1, 0.0); /* each child needs a unique entropy pool */ |
| 471 |
#else |
| 472 |
if(max_clients && num_clients>=max_clients) { |
| 473 |
s_log(LOG_WARNING, "Connection rejected: too many clients (>=%d)", |
| 474 |
max_clients); |
| 475 |
closesocket(s); |
| 476 |
return 0; |
| 477 |
} |
| 478 |
#endif |
| 479 |
#ifndef USE_FORK |
| 480 |
service_up_ref(opt); |
| 481 |
#endif |
| 482 |
if(create_client(fd, s, alloc_client_session(opt, s, s))) { |
| 483 |
s_log(LOG_ERR, "Connection rejected: create_client failed"); |
| 484 |
#ifndef USE_FORK |
| 485 |
service_free(opt); |
| 486 |
#endif |
| 487 |
return 0; |
| 488 |
} |
| 489 |
return 0; |
| 490 |
} |
| 491 |
|
| 492 |
/**************************************** initialization helpers */ |
| 493 |
|
| 494 |
NOEXPORT int exec_connect_start(void) { |
| 495 |
SERVICE_OPTIONS *opt; |
| 496 |
|
| 497 |
for(opt=service_options.next; opt; opt=opt->next) { |
| 498 |
if(opt->exec_name && opt->connect_addr.names) { |
| 499 |
s_log(LOG_DEBUG, "Starting exec+connect service [%s]", |
| 500 |
opt->servname); |
| 501 |
#ifndef USE_FORK |
| 502 |
service_up_ref(opt); |
| 503 |
#endif |
| 504 |
if(create_client(INVALID_SOCKET, INVALID_SOCKET, |
| 505 |
alloc_client_session(opt, INVALID_SOCKET, INVALID_SOCKET))) { |
| 506 |
s_log(LOG_ERR, "Failed to start exec+connect service [%s]", |
| 507 |
opt->servname); |
| 508 |
#ifndef USE_FORK |
| 509 |
service_free(opt); |
| 510 |
#endif |
| 511 |
return 1; /* fatal error */ |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
return 0; /* OK */ |
| 516 |
} |
| 517 |
|
| 518 |
/* clear fds, close old ports */ |
| 519 |
NOEXPORT void unbind_ports(void) { |
| 520 |
SERVICE_OPTIONS *opt; |
| 521 |
|
| 522 |
s_poll_init(fds, 1); |
| 523 |
|
| 524 |
for(opt=service_options.next; opt; opt=opt->next) { |
| 525 |
unsigned i; |
| 526 |
|
| 527 |
s_log(LOG_DEBUG, "Unbinding service [%s]", opt->servname); |
| 528 |
|
| 529 |
/* "accept" service */ |
| 530 |
for(i=0; i<opt->local_addr.num; ++i) |
| 531 |
unbind_port(opt, i); |
| 532 |
|
| 533 |
/* "exec+connect" service */ |
| 534 |
if(opt->exec_name && opt->connect_addr.names) { |
| 535 |
/* create exec+connect services */ |
| 536 |
/* FIXME: this is just a crude workaround */ |
| 537 |
/* is it better to kill the service? */ |
| 538 |
/* FIXME: this won't work with FORK threads */ |
| 539 |
opt->retry=-1; /* disable */ |
| 540 |
} |
| 541 |
|
| 542 |
s_log(LOG_DEBUG, "Service [%s] closed", opt->servname); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
NOEXPORT void unbind_port(SERVICE_OPTIONS *opt, unsigned i) { |
| 547 |
SOCKET fd=opt->local_fd[i]; |
| 548 |
#ifdef HAVE_STRUCT_SOCKADDR_UN |
| 549 |
SOCKADDR_UNION *addr=opt->local_addr.addr+i; |
| 550 |
struct stat sb; /* buffer for lstat() */ |
| 551 |
#endif |
| 552 |
|
| 553 |
if(fd==INVALID_SOCKET) |
| 554 |
return; |
| 555 |
opt->local_fd[i]=INVALID_SOCKET; |
| 556 |
|
| 557 |
if(fd<(SOCKET)listen_fds_start || |
| 558 |
fd>=(SOCKET)(listen_fds_start+systemd_fds)) |
| 559 |
closesocket(fd); |
| 560 |
s_log(LOG_DEBUG, "Service [%s] closed (FD=%ld)", |
| 561 |
opt->servname, (long)fd); |
| 562 |
|
| 563 |
#ifdef HAVE_STRUCT_SOCKADDR_UN |
| 564 |
if(addr->sa.sa_family==AF_UNIX) { |
| 565 |
if(lstat(addr->un.sun_path, &sb)) |
| 566 |
sockerror(addr->un.sun_path); |
| 567 |
else if(!S_ISSOCK(sb.st_mode)) |
| 568 |
s_log(LOG_ERR, "Not a socket: %s", |
| 569 |
addr->un.sun_path); |
| 570 |
else if(unlink(addr->un.sun_path)) |
| 571 |
sockerror(addr->un.sun_path); |
| 572 |
else |
| 573 |
s_log(LOG_DEBUG, "Socket removed: %s", |
| 574 |
addr->un.sun_path); |
| 575 |
} |
| 576 |
#endif |
| 577 |
} |
| 578 |
|
| 579 |
/* open new ports, update fds */ |
| 580 |
NOEXPORT int bind_ports(void) { |
| 581 |
SERVICE_OPTIONS *opt; |
| 582 |
int listening_section; |
| 583 |
|
| 584 |
#ifdef USE_LIBWRAP |
| 585 |
/* execute after options_cmdline() to know service_options.next, |
| 586 |
* but as early as possible to avoid leaking file descriptors */ |
| 587 |
/* retry on each bind_ports() in case stunnel.conf was reloaded |
| 588 |
without "libwrap = no" */ |
| 589 |
libwrap_init(); |
| 590 |
#endif /* USE_LIBWRAP */ |
| 591 |
|
| 592 |
s_poll_init(fds, 1); |
| 593 |
|
| 594 |
/* allow clean unbind_ports() even though |
| 595 |
bind_ports() was not fully performed */ |
| 596 |
for(opt=service_options.next; opt; opt=opt->next) { |
| 597 |
unsigned i; |
| 598 |
for(i=0; i<opt->local_addr.num; ++i) |
| 599 |
opt->local_fd[i]=INVALID_SOCKET; |
| 600 |
} |
| 601 |
|
| 602 |
listening_section=0; |
| 603 |
for(opt=service_options.next; opt; opt=opt->next) { |
| 604 |
opt->bound_ports=0; |
| 605 |
if(opt->local_addr.num) { /* ports to bind for this service */ |
| 606 |
unsigned i; |
| 607 |
s_log(LOG_DEBUG, "Binding service [%s]", opt->servname); |
| 608 |
for(i=0; i<opt->local_addr.num; ++i) { |
| 609 |
SOCKET fd; |
| 610 |
fd=bind_port(opt, listening_section, i); |
| 611 |
opt->local_fd[i]=fd; |
| 612 |
if(fd!=INVALID_SOCKET) { |
| 613 |
s_poll_add(fds, fd, 1, 0); |
| 614 |
++opt->bound_ports; |
| 615 |
} |
| 616 |
} |
| 617 |
if(!opt->bound_ports) { |
| 618 |
s_log(LOG_ERR, "Binding service [%s] failed", opt->servname); |
| 619 |
return 1; |
| 620 |
} |
| 621 |
++listening_section; |
| 622 |
} else if(opt->exec_name && opt->connect_addr.names) { |
| 623 |
s_log(LOG_DEBUG, "Skipped exec+connect service [%s]", opt->servname); |
| 624 |
#ifndef OPENSSL_NO_TLSEXT |
| 625 |
} else if(!opt->option.client && opt->sni) { |
| 626 |
s_log(LOG_DEBUG, "Skipped SNI secondary service [%s]", opt->servname); |
| 627 |
#endif |
| 628 |
} else { /* each service must define two endpoints */ |
| 629 |
s_log(LOG_ERR, "Invalid service [%s]", opt->servname); |
| 630 |
return 1; |
| 631 |
} |
| 632 |
} |
| 633 |
if(listening_section<systemd_fds) { |
| 634 |
s_log(LOG_ERR, |
| 635 |
"Too many listening file descriptors received from systemd, got %d", |
| 636 |
systemd_fds); |
| 637 |
return 1; |
| 638 |
} |
| 639 |
return 0; /* OK */ |
| 640 |
} |
| 641 |
|
| 642 |
NOEXPORT SOCKET bind_port(SERVICE_OPTIONS *opt, int listening_section, unsigned i) { |
| 643 |
SOCKET fd; |
| 644 |
SOCKADDR_UNION *addr=opt->local_addr.addr+i; |
| 645 |
#ifdef HAVE_STRUCT_SOCKADDR_UN |
| 646 |
struct stat sb; /* buffer for lstat() */ |
| 647 |
#endif |
| 648 |
|
| 649 |
if(listening_section<systemd_fds) { |
| 650 |
fd=(SOCKET)(listen_fds_start+listening_section); |
| 651 |
s_log(LOG_DEBUG, |
| 652 |
"Listening file descriptor received from systemd (FD=%ld)", |
| 653 |
(long)fd); |
| 654 |
} else { |
| 655 |
fd=s_socket(addr->sa.sa_family, SOCK_STREAM, 0, 1, "accept socket"); |
| 656 |
if(fd==INVALID_SOCKET) |
| 657 |
return INVALID_SOCKET; |
| 658 |
s_log(LOG_DEBUG, "Listening file descriptor created (FD=%ld)", |
| 659 |
(long)fd); |
| 660 |
} |
| 661 |
|
| 662 |
if(socket_options_set(opt, fd, 0)<0) { |
| 663 |
closesocket(fd); |
| 664 |
return INVALID_SOCKET; |
| 665 |
} |
| 666 |
|
| 667 |
/* we don't bind or listen on a socket inherited from systemd */ |
| 668 |
if(listening_section>=systemd_fds) { |
| 669 |
if(bind(fd, &addr->sa, addr_len(addr))) { |
| 670 |
int err=get_last_socket_error(); |
| 671 |
char *requested_bind_address; |
| 672 |
|
| 673 |
/* local socket can't be unnamed */ |
| 674 |
requested_bind_address=s_ntop(addr, addr_len(addr)); |
| 675 |
s_log(LOG_NOTICE, "Binding service [%s] to %s: %s (%d)", |
| 676 |
opt->servname, requested_bind_address, s_strerror(err), err); |
| 677 |
str_free(requested_bind_address); |
| 678 |
closesocket(fd); |
| 679 |
return INVALID_SOCKET; |
| 680 |
} |
| 681 |
if(listen(fd, SOMAXCONN)) { |
| 682 |
sockerror("listen"); |
| 683 |
closesocket(fd); |
| 684 |
return INVALID_SOCKET; |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
#ifdef HAVE_STRUCT_SOCKADDR_UN |
| 689 |
/* chown the UNIX socket, errors are ignored */ |
| 690 |
if(addr->sa.sa_family==AF_UNIX && |
| 691 |
(opt->uid || opt->gid)) { |
| 692 |
/* fchown() does *not* work on UNIX sockets */ |
| 693 |
if(!lchown(addr->un.sun_path, opt->uid, opt->gid)) |
| 694 |
s_log(LOG_DEBUG, |
| 695 |
"Socket chown succeeded: %s, UID=%u, GID=%u", |
| 696 |
addr->un.sun_path, |
| 697 |
(unsigned)opt->uid, (unsigned)opt->gid); |
| 698 |
else if(lstat(addr->un.sun_path, &sb)) |
| 699 |
sockerror(addr->un.sun_path); |
| 700 |
else if(sb.st_uid==opt->uid && sb.st_gid==opt->gid) |
| 701 |
s_log(LOG_DEBUG, |
| 702 |
"Socket chown unneeded: %s, UID=%u, GID=%u", |
| 703 |
addr->un.sun_path, |
| 704 |
(unsigned)opt->uid, (unsigned)opt->gid); |
| 705 |
else |
| 706 |
s_log(LOG_ERR, "Socket chown failed: %s, UID=%u, GID=%u", |
| 707 |
addr->un.sun_path, |
| 708 |
(unsigned)opt->uid, (unsigned)opt->gid); |
| 709 |
} |
| 710 |
#endif |
| 711 |
|
| 712 |
{ |
| 713 |
SOCKADDR_UNION assigned_addr; |
| 714 |
socklen_t assigned_addr_len=sizeof assigned_addr; |
| 715 |
char *assigned_bind_address; |
| 716 |
|
| 717 |
if(getsockname(fd, &assigned_addr.sa, &assigned_addr_len)) { |
| 718 |
sockerror("getsockname"); |
| 719 |
closesocket(fd); |
| 720 |
return INVALID_SOCKET; |
| 721 |
} |
| 722 |
assigned_bind_address=s_ntop(&assigned_addr, addr_len(&assigned_addr)); |
| 723 |
s_log(LOG_INFO, "Service [%s] (FD=%ld) bound to %s", |
| 724 |
opt->servname, (long)fd, assigned_bind_address); |
| 725 |
str_free(assigned_bind_address); |
| 726 |
} |
| 727 |
return fd; |
| 728 |
} |
| 729 |
|
| 730 |
#ifdef HAVE_CHROOT |
| 731 |
NOEXPORT int change_root(void) { |
| 732 |
if(!global_options.chroot_dir) |
| 733 |
return 0; |
| 734 |
if(chroot(global_options.chroot_dir)) { |
| 735 |
sockerror("chroot"); |
| 736 |
return 1; |
| 737 |
} |
| 738 |
if(chdir("/")) { |
| 739 |
sockerror("chdir"); |
| 740 |
return 1; |
| 741 |
} |
| 742 |
s_log(LOG_NOTICE, "Switched to chroot directory: %s", global_options.chroot_dir); |
| 743 |
return 0; |
| 744 |
} |
| 745 |
#endif /* HAVE_CHROOT */ |
| 746 |
|
| 747 |
/**************************************** signal pipe handling */ |
| 748 |
|
| 749 |
NOEXPORT int pipe_init(SOCKET socket_vector[2], const char *name) { |
| 750 |
#ifdef USE_WIN32 |
| 751 |
(void)name; /* squash the unused parameter warning */ |
| 752 |
|
| 753 |
if(make_sockets(socket_vector)) |
| 754 |
return 1; |
| 755 |
#elif defined(__INNOTEK_LIBC__) |
| 756 |
/* Innotek port of GCC can not use select on a pipe: |
| 757 |
* use local socket instead */ |
| 758 |
struct sockaddr_un un; |
| 759 |
fd_set set_pipe; |
| 760 |
int pipe_in; |
| 761 |
|
| 762 |
FD_ZERO(&set_pipe); |
| 763 |
socket_vector[0]=s_socket(PF_OS2, SOCK_STREAM, 0, 0, "socket#1"); |
| 764 |
socket_vector[1]=s_socket(PF_OS2, SOCK_STREAM, 0, 0, "socket#2"); |
| 765 |
|
| 766 |
/* connect the two endpoints */ |
| 767 |
memset(&un, 0, sizeof un); |
| 768 |
un.sun_len=sizeof un; |
| 769 |
un.sun_family=AF_OS2; |
| 770 |
sprintf(un.sun_path, "\\socket\\stunnel-%s-%u", name, getpid()); |
| 771 |
/* make the first endpoint listen */ |
| 772 |
bind(socket_vector[0], (struct sockaddr *)&un, sizeof un); |
| 773 |
listen(socket_vector[0], 1); |
| 774 |
connect(socket_vector[1], (struct sockaddr *)&un, sizeof un); |
| 775 |
FD_SET(socket_vector[0], &set_pipe); |
| 776 |
if(select(socket_vector[0]+1, &set_pipe, NULL, NULL, NULL)>0) { |
| 777 |
pipe_in=socket_vector[0]; |
| 778 |
socket_vector[0]=s_accept(socket_vector[0], NULL, 0, 0, "accept"); |
| 779 |
closesocket(pipe_in); |
| 780 |
} else { |
| 781 |
sockerror("select"); |
| 782 |
return 1; |
| 783 |
} |
| 784 |
#else /* Unix */ |
| 785 |
if(s_pipe(socket_vector, 1, name)) |
| 786 |
return 1; |
| 787 |
#endif /* USE_WIN32 */ |
| 788 |
return 0; |
| 789 |
} |
| 790 |
|
| 791 |
#ifdef __GNUC__ |
| 792 |
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || defined(__clang__) |
| 793 |
#pragma GCC diagnostic push |
| 794 |
#pragma GCC diagnostic ignored "-Wunused-result" |
| 795 |
#endif /* __GNUC__>=4.6 */ |
| 796 |
#endif /* __GNUC__ */ |
| 797 |
void signal_post(uint8_t sig) { |
| 798 |
/* no meaningful way here to handle the result */ |
| 799 |
writesocket(signal_pipe[1], (char *)&sig, 1); |
| 800 |
} |
| 801 |
#ifdef __GNUC__ |
| 802 |
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 803 |
#pragma GCC diagnostic pop |
| 804 |
#endif /* __GNUC__>=4.6 */ |
| 805 |
#endif /* __GNUC__ */ |
| 806 |
|
| 807 |
/* make a single attempt to dispatch a signal from the signal pipe */ |
| 808 |
/* return 1 on SIGNAL_TERMINATE or a fatal error, 0 otherwise */ |
| 809 |
NOEXPORT int signal_pipe_dispatch(void) { |
| 810 |
uint8_t sig=0xff; |
| 811 |
ssize_t num; |
| 812 |
char *sig_name; |
| 813 |
|
| 814 |
s_log(LOG_DEBUG, "Dispatching a signal from the signal pipe"); |
| 815 |
num=readsocket(signal_pipe[0], (char *)&sig, 1); |
| 816 |
if(num!=1) { |
| 817 |
if(num) { |
| 818 |
if(get_last_socket_error()==S_EWOULDBLOCK) { |
| 819 |
s_log(LOG_DEBUG, "Signal pipe is empty"); |
| 820 |
return 0; |
| 821 |
} |
| 822 |
sockerror("signal pipe read"); |
| 823 |
} else { |
| 824 |
s_log(LOG_ERR, "Signal pipe closed"); |
| 825 |
} |
| 826 |
s_poll_remove(fds, signal_pipe[0]); |
| 827 |
closesocket(signal_pipe[0]); |
| 828 |
closesocket(signal_pipe[1]); |
| 829 |
if(pipe_init(signal_pipe, "signal_pipe")) { |
| 830 |
s_log(LOG_ERR, |
| 831 |
"Signal pipe reinitialization failed; terminating"); |
| 832 |
return 1; |
| 833 |
} |
| 834 |
s_poll_add(fds, signal_pipe[0], 1, 0); |
| 835 |
s_log(LOG_ERR, "Signal pipe reinitialized"); |
| 836 |
return 0; |
| 837 |
} |
| 838 |
|
| 839 |
switch(sig) { |
| 840 |
#ifndef USE_WIN32 |
| 841 |
case SIGCHLD: |
| 842 |
s_log(LOG_DEBUG, "Processing SIGCHLD"); |
| 843 |
#ifdef USE_FORK |
| 844 |
pid_status_nohang("Process"); /* client process */ |
| 845 |
#else /* USE_UCONTEXT || USE_PTHREAD */ |
| 846 |
pid_status_nohang("Child process"); /* 'exec' process */ |
| 847 |
#endif /* defined USE_FORK */ |
| 848 |
return 0; |
| 849 |
#endif /* !defined USE_WIN32 */ |
| 850 |
case SIGNAL_TERMINATE: |
| 851 |
s_log(LOG_DEBUG, "Processing SIGNAL_TERMINATE"); |
| 852 |
s_log(LOG_NOTICE, "Terminated"); |
| 853 |
return 1; |
| 854 |
case SIGNAL_RELOAD_CONFIG: |
| 855 |
s_log(LOG_DEBUG, "Processing SIGNAL_RELOAD_CONFIG"); |
| 856 |
reload_config(); |
| 857 |
return 0; |
| 858 |
case SIGNAL_REOPEN_LOG: |
| 859 |
s_log(LOG_DEBUG, "Processing SIGNAL_REOPEN_LOG"); |
| 860 |
log_flush(LOG_MODE_BUFFER); |
| 861 |
log_close(SINK_OUTFILE); |
| 862 |
log_open(SINK_OUTFILE); |
| 863 |
log_flush(LOG_MODE_CONFIGURED); |
| 864 |
s_log(LOG_NOTICE, "Log file reopened"); |
| 865 |
return 0; |
| 866 |
case SIGNAL_CONNECTIONS: |
| 867 |
return process_connections(); |
| 868 |
default: |
| 869 |
sig_name=signal_name(sig); |
| 870 |
s_log(LOG_ERR, "Received %s; terminating", sig_name); |
| 871 |
str_free(sig_name); |
| 872 |
return 1; |
| 873 |
} |
| 874 |
} |
| 875 |
|
| 876 |
NOEXPORT void reload_config(void) { |
| 877 |
static int delay=10; /* default of 10ms */ |
| 878 |
#ifdef HAVE_CHROOT |
| 879 |
struct stat sb; |
| 880 |
#endif /* HAVE_CHROOT */ |
| 881 |
|
| 882 |
if(options_parse(CONF_RELOAD)) { |
| 883 |
s_log(LOG_ERR, "Failed to reload the configuration file"); |
| 884 |
return; |
| 885 |
} |
| 886 |
unbind_ports(); |
| 887 |
log_flush(LOG_MODE_BUFFER); |
| 888 |
#ifdef HAVE_CHROOT |
| 889 |
/* we don't close SINK_SYSLOG if chroot is enabled and |
| 890 |
* there is no /dev/log inside it, which could allow |
| 891 |
* openlog(3) to reopen the syslog socket later */ |
| 892 |
if(global_options.chroot_dir && stat("/dev/log", &sb)) |
| 893 |
log_close(SINK_OUTFILE); |
| 894 |
else |
| 895 |
#endif /* HAVE_CHROOT */ |
| 896 |
log_close(SINK_SYSLOG|SINK_OUTFILE); |
| 897 |
/* there is no race condition here: |
| 898 |
* client threads are not allowed to use global options */ |
| 899 |
options_free(1); /* free the current options */ |
| 900 |
options_apply(); /* apply the new options */ |
| 901 |
/* we hope that a sane openlog(3) implementation won't |
| 902 |
* attempt to reopen /dev/log if it's already open */ |
| 903 |
log_open(SINK_SYSLOG|SINK_OUTFILE); |
| 904 |
log_flush(LOG_MODE_CONFIGURED); |
| 905 |
ui_config_reloaded(); |
| 906 |
/* we use "|" instead of "||" to attempt initialization of both subsystems */ |
| 907 |
if(bind_ports() | exec_connect_start()) { /* failed */ |
| 908 |
unbind_ports(); |
| 909 |
s_poll_sleep(delay/1000, delay%1000); /* sleep to avoid log trashing */ |
| 910 |
signal_post(SIGNAL_RELOAD_CONFIG); /* retry */ |
| 911 |
delay*=2; |
| 912 |
if(delay > 10000) /* limit to 10s */ |
| 913 |
delay=10000; |
| 914 |
} else { /* success */ |
| 915 |
delay=10; /* reset back to 10ms */ |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
#ifdef __GNUC__ |
| 920 |
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 921 |
#pragma GCC diagnostic push |
| 922 |
#endif /* __GNUC__>=4.6 */ |
| 923 |
#pragma GCC diagnostic ignored "-Wformat" |
| 924 |
#pragma GCC diagnostic ignored "-Wformat-extra-args" |
| 925 |
#endif /* __GNUC__ */ |
| 926 |
NOEXPORT int process_connections(void) { |
| 927 |
#ifndef USE_FORK |
| 928 |
CLI *c; |
| 929 |
int n=0; |
| 930 |
|
| 931 |
s_log(LOG_EMERG, "Active connections:"); |
| 932 |
CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_THREAD_LIST]); |
| 933 |
for(c=thread_head; c; c=c->thread_next) { |
| 934 |
s_log(LOG_EMERG, "Active connection %d: Service [%s], " |
| 935 |
"%llu byte(s) sent to TLS, " |
| 936 |
"%llu byte(s) sent to socket", |
| 937 |
++n, c->opt->servname, |
| 938 |
(unsigned long long)c->ssl_bytes, |
| 939 |
(unsigned long long)c->sock_bytes); |
| 940 |
} |
| 941 |
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_THREAD_LIST]); |
| 942 |
s_log(LOG_EMERG, "Listed %d active connection(s)", n); |
| 943 |
#endif /* USE_FORK */ |
| 944 |
return 0; /* continue execution */ |
| 945 |
} |
| 946 |
#ifdef __GNUC__ |
| 947 |
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 948 |
#pragma GCC diagnostic pop |
| 949 |
#endif /* __GNUC__>=4.6 */ |
| 950 |
#endif /* __GNUC__ */ |
| 951 |
|
| 952 |
/**************************************** signal name decoding */ |
| 953 |
|
| 954 |
#define check_signal(s) if(signum==s) return str_dup(#s); |
| 955 |
|
| 956 |
NOEXPORT char *signal_name(int signum) { |
| 957 |
#ifdef SIGHUP |
| 958 |
check_signal(SIGHUP) |
| 959 |
#endif |
| 960 |
#ifdef SIGINT |
| 961 |
check_signal(SIGINT) |
| 962 |
#endif |
| 963 |
#ifdef SIGQUIT |
| 964 |
check_signal(SIGQUIT) |
| 965 |
#endif |
| 966 |
#ifdef SIGILL |
| 967 |
check_signal(SIGILL) |
| 968 |
#endif |
| 969 |
#ifdef SIGTRAP |
| 970 |
check_signal(SIGTRAP) |
| 971 |
#endif |
| 972 |
#ifdef SIGABRT |
| 973 |
check_signal(SIGABRT) |
| 974 |
#endif |
| 975 |
#ifdef SIGIOT |
| 976 |
check_signal(SIGIOT) |
| 977 |
#endif |
| 978 |
#ifdef SIGBUS |
| 979 |
check_signal(SIGBUS) |
| 980 |
#endif |
| 981 |
#ifdef SIGFPE |
| 982 |
check_signal(SIGFPE) |
| 983 |
#endif |
| 984 |
#ifdef SIGKILL |
| 985 |
check_signal(SIGKILL) |
| 986 |
#endif |
| 987 |
#ifdef SIGUSR1 |
| 988 |
check_signal(SIGUSR1) |
| 989 |
#endif |
| 990 |
#ifdef SIGSEGV |
| 991 |
check_signal(SIGSEGV) |
| 992 |
#endif |
| 993 |
#ifdef SIGUSR2 |
| 994 |
check_signal(SIGUSR2) |
| 995 |
#endif |
| 996 |
#ifdef SIGPIPE |
| 997 |
check_signal(SIGPIPE) |
| 998 |
#endif |
| 999 |
#ifdef SIGALRM |
| 1000 |
check_signal(SIGALRM) |
| 1001 |
#endif |
| 1002 |
#ifdef SIGTERM |
| 1003 |
check_signal(SIGTERM) |
| 1004 |
#endif |
| 1005 |
#ifdef SIGSTKFLT |
| 1006 |
check_signal(SIGSTKFLT) |
| 1007 |
#endif |
| 1008 |
#ifdef SIGCHLD |
| 1009 |
check_signal(SIGCHLD) |
| 1010 |
#endif |
| 1011 |
#ifdef SIGCONT |
| 1012 |
check_signal(SIGCONT) |
| 1013 |
#endif |
| 1014 |
#ifdef SIGSTOP |
| 1015 |
check_signal(SIGSTOP) |
| 1016 |
#endif |
| 1017 |
#ifdef SIGTSTP |
| 1018 |
check_signal(SIGTSTP) |
| 1019 |
#endif |
| 1020 |
#ifdef SIGTTIN |
| 1021 |
check_signal(SIGTTIN) |
| 1022 |
#endif |
| 1023 |
#ifdef SIGTTOU |
| 1024 |
check_signal(SIGTTOU) |
| 1025 |
#endif |
| 1026 |
#ifdef SIGURG |
| 1027 |
check_signal(SIGURG) |
| 1028 |
#endif |
| 1029 |
#ifdef SIGXCPU |
| 1030 |
check_signal(SIGXCPU) |
| 1031 |
#endif |
| 1032 |
#ifdef SIGXFSZ |
| 1033 |
check_signal(SIGXFSZ) |
| 1034 |
#endif |
| 1035 |
#ifdef SIGVTALRM |
| 1036 |
check_signal(SIGVTALRM) |
| 1037 |
#endif |
| 1038 |
#ifdef SIGPROF |
| 1039 |
check_signal(SIGPROF) |
| 1040 |
#endif |
| 1041 |
#ifdef SIGWINCH |
| 1042 |
check_signal(SIGWINCH) |
| 1043 |
#endif |
| 1044 |
#ifdef SIGIO |
| 1045 |
check_signal(SIGIO) |
| 1046 |
#endif |
| 1047 |
#ifdef SIGPOLL |
| 1048 |
check_signal(SIGPOLL) |
| 1049 |
#endif |
| 1050 |
#ifdef SIGLOST |
| 1051 |
check_signal(SIGLOST) |
| 1052 |
#endif |
| 1053 |
#ifdef SIGPWR |
| 1054 |
check_signal(SIGPWR) |
| 1055 |
#endif |
| 1056 |
#ifdef SIGSYS |
| 1057 |
check_signal(SIGSYS) |
| 1058 |
#endif |
| 1059 |
#ifdef SIGUNUSED |
| 1060 |
check_signal(SIGUNUSED) |
| 1061 |
#endif |
| 1062 |
return str_printf("signal %d", signum); |
| 1063 |
} |
| 1064 |
|
| 1065 |
/**************************************** log build details */ |
| 1066 |
|
| 1067 |
static char *str_cat(char *dst, const char *src) { |
| 1068 |
dst=str_realloc(dst, strlen(dst) + strlen(src) + 1); |
| 1069 |
strcat(dst, src); |
| 1070 |
return dst; |
| 1071 |
} |
| 1072 |
|
| 1073 |
void stunnel_info(int level) { |
| 1074 |
int tls_feature_found=0; |
| 1075 |
char *features; |
| 1076 |
|
| 1077 |
s_log(level, "stunnel " STUNNEL_VERSION " on " HOST " platform"); |
| 1078 |
if(strcmp(OPENSSL_VERSION_TEXT, OpenSSL_version(OPENSSL_VERSION))) { |
| 1079 |
s_log(level, "Compiled with " OPENSSL_VERSION_TEXT); |
| 1080 |
s_log(level, "Running with %s", OpenSSL_version(OPENSSL_VERSION)); |
| 1081 |
#if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 1082 |
if((OPENSSL_version_major()<<8 | OPENSSL_version_minor()) != |
| 1083 |
OPENSSL_VERSION_NUMBER>>20) |
| 1084 |
#else /* OpenSSL version < 3.0.0 */ |
| 1085 |
if(OpenSSL_version_num()>>12 != OPENSSL_VERSION_NUMBER>>12) |
| 1086 |
#endif /* OpenSSL version >= 3.0.0 */ |
| 1087 |
s_log(level, "Update OpenSSL shared libraries or rebuild stunnel"); |
| 1088 |
} else { |
| 1089 |
s_log(level, "Compiled/running with " OPENSSL_VERSION_TEXT); |
| 1090 |
} |
| 1091 |
|
| 1092 |
features=str_dup("Threading:"); |
| 1093 |
#ifdef USE_UCONTEXT |
| 1094 |
features=str_cat(features, "UCONTEXT"); |
| 1095 |
#endif |
| 1096 |
#ifdef USE_PTHREAD |
| 1097 |
features=str_cat(features, "PTHREAD"); |
| 1098 |
#endif |
| 1099 |
#ifdef USE_WIN32 |
| 1100 |
features=str_cat(features, "WIN32"); |
| 1101 |
#endif |
| 1102 |
#ifdef USE_FORK |
| 1103 |
features=str_cat(features, "FORK"); |
| 1104 |
#endif |
| 1105 |
|
| 1106 |
features=str_cat(features, " Sockets:"); |
| 1107 |
#ifdef USE_POLL |
| 1108 |
features=str_cat(features, "POLL"); |
| 1109 |
#else /* defined(USE_POLL) */ |
| 1110 |
features=str_cat(features, "SELECT"); |
| 1111 |
#endif /* defined(USE_POLL) */ |
| 1112 |
/* supported IP version parameter */ |
| 1113 |
features=str_cat(features, use_ipv6() ? ",IPv6" : ",IPv4"); |
| 1114 |
#ifdef USE_SYSTEMD |
| 1115 |
features=str_cat(features, ",SYSTEMD"); |
| 1116 |
#endif /* defined(USE_SYSTEMD) */ |
| 1117 |
|
| 1118 |
features=str_cat(features, " TLS:"); |
| 1119 |
#ifndef OPENSSL_NO_ENGINE |
| 1120 |
features=str_cat(features, "ENGINE"); |
| 1121 |
tls_feature_found=1; |
| 1122 |
#endif /* !defined(OPENSSL_NO_ENGINE) */ |
| 1123 |
#ifdef USE_FIPS |
| 1124 |
if(fips_available()) { |
| 1125 |
if(tls_feature_found) |
| 1126 |
features=str_cat(features, ","); |
| 1127 |
features=str_cat(features, "FIPS"); |
| 1128 |
tls_feature_found=1; |
| 1129 |
} |
| 1130 |
#endif /* USE_FIPS */ |
| 1131 |
#ifndef OPENSSL_NO_OCSP |
| 1132 |
if(tls_feature_found) |
| 1133 |
features=str_cat(features, ","); |
| 1134 |
features=str_cat(features, "OCSP"); |
| 1135 |
tls_feature_found=1; |
| 1136 |
#endif /* !defined(OPENSSL_NO_OCSP) */ |
| 1137 |
#ifndef OPENSSL_NO_PSK |
| 1138 |
if(tls_feature_found) |
| 1139 |
features=str_cat(features, ","); |
| 1140 |
features=str_cat(features, "PSK"); |
| 1141 |
tls_feature_found=1; |
| 1142 |
#endif /* !defined(OPENSSL_NO_PSK) */ |
| 1143 |
#ifndef OPENSSL_NO_TLSEXT |
| 1144 |
if(tls_feature_found) |
| 1145 |
features=str_cat(features, ","); |
| 1146 |
features=str_cat(features, "SNI"); |
| 1147 |
tls_feature_found=1; |
| 1148 |
#endif /* !defined(OPENSSL_NO_TLSEXT) */ |
| 1149 |
if(!tls_feature_found) |
| 1150 |
features=str_cat(features, "NONE"); |
| 1151 |
|
| 1152 |
#ifdef USE_LIBWRAP |
| 1153 |
features=str_cat(features, " Auth:LIBWRAP"); |
| 1154 |
#endif |
| 1155 |
|
| 1156 |
s_log(level, "%s", features); |
| 1157 |
str_free(features); |
| 1158 |
|
| 1159 |
#ifdef errno |
| 1160 |
#define xstr(a) str(a) |
| 1161 |
#define str(a) #a |
| 1162 |
s_log(LOG_DEBUG, "errno: " xstr(errno)); |
| 1163 |
#endif /* errno */ |
| 1164 |
} |
| 1165 |
|
| 1166 |
/* end of stunnel.c */ |