| 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 |
/* run the per-day job every 24 hours */ |
| 41 |
#define PER_DAY_PERIOD (24*60*60) |
| 42 |
|
| 43 |
#ifdef USE_OS_THREADS |
| 44 |
THREAD_ID per_second_thread_id=(THREAD_ID)0; |
| 45 |
THREAD_ID per_minute_thread_id=(THREAD_ID)0; |
| 46 |
THREAD_ID per_day_thread_id=(THREAD_ID)0; |
| 47 |
#endif |
| 48 |
|
| 49 |
#ifdef USE_PTHREAD |
| 50 |
NOEXPORT void *per_second_thread(void *arg); |
| 51 |
NOEXPORT void *per_minute_thread(void *arg); |
| 52 |
NOEXPORT void *per_day_thread(void *arg); |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifdef USE_WIN32 |
| 56 |
NOEXPORT unsigned __stdcall per_second_thread(void *arg); |
| 57 |
NOEXPORT unsigned __stdcall per_minute_thread(void *arg); |
| 58 |
NOEXPORT unsigned __stdcall per_day_thread(void *arg); |
| 59 |
#endif |
| 60 |
|
| 61 |
#ifdef USE_OS_THREADS |
| 62 |
NOEXPORT void per_second_worker(void); |
| 63 |
NOEXPORT void per_minute_worker(void); |
| 64 |
NOEXPORT void per_minute_stapling_update(void); |
| 65 |
NOEXPORT void per_day_worker(void); |
| 66 |
#ifndef OPENSSL_NO_DH |
| 67 |
NOEXPORT void per_day_dh_param(BN_GENCB *); |
| 68 |
NOEXPORT BN_GENCB *per_day_bn_gencb(void); |
| 69 |
NOEXPORT int bn_callback(int, int, BN_GENCB *); |
| 70 |
#endif /* OPENSSL_NO_DH */ |
| 71 |
#endif /* USE_OS_THREADS */ |
| 72 |
|
| 73 |
#if defined(USE_PTHREAD) |
| 74 |
|
| 75 |
int cron_init(void) { |
| 76 |
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(__APPLE__) |
| 77 |
sigset_t new_set, old_set; |
| 78 |
#endif /* HAVE_PTHREAD_SIGMASK && !__APPLE__*/ |
| 79 |
|
| 80 |
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(__APPLE__) |
| 81 |
sigfillset(&new_set); |
| 82 |
pthread_sigmask(SIG_SETMASK, &new_set, &old_set); /* block signals */ |
| 83 |
#endif /* HAVE_PTHREAD_SIGMASK && !__APPLE__*/ |
| 84 |
CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_THREAD_LIST]); |
| 85 |
if(pthread_create(&per_second_thread_id, NULL, per_second_thread, NULL)) |
| 86 |
ioerror("pthread_create"); |
| 87 |
if(pthread_create(&per_minute_thread_id, NULL, per_minute_thread, NULL)) |
| 88 |
ioerror("pthread_create"); |
| 89 |
if(pthread_create(&per_day_thread_id, NULL, per_day_thread, NULL)) |
| 90 |
ioerror("pthread_create"); |
| 91 |
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_THREAD_LIST]); |
| 92 |
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(__APPLE__) |
| 93 |
pthread_sigmask(SIG_SETMASK, &old_set, NULL); /* unblock signals */ |
| 94 |
#endif /* HAVE_PTHREAD_SIGMASK && !__APPLE__*/ |
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
NOEXPORT void *per_second_thread(void *arg) { |
| 99 |
(void)arg; /* squash the unused parameter warning */ |
| 100 |
tls_alloc(NULL, NULL, "per-second"); |
| 101 |
per_second_worker(); |
| 102 |
return NULL; /* it should never be executed */ |
| 103 |
} |
| 104 |
|
| 105 |
NOEXPORT void *per_minute_thread(void *arg) { |
| 106 |
(void)arg; /* squash the unused parameter warning */ |
| 107 |
tls_alloc(NULL, NULL, "per-minute"); |
| 108 |
per_minute_worker(); |
| 109 |
return NULL; /* it should never be executed */ |
| 110 |
} |
| 111 |
|
| 112 |
NOEXPORT void *per_day_thread(void *arg) { |
| 113 |
#ifdef SCHED_BATCH |
| 114 |
struct sched_param param; |
| 115 |
#endif |
| 116 |
|
| 117 |
(void)arg; /* squash the unused parameter warning */ |
| 118 |
tls_alloc(NULL, NULL, "per-day"); |
| 119 |
#ifdef SCHED_BATCH |
| 120 |
param.sched_priority=0; |
| 121 |
if(pthread_setschedparam(pthread_self(), SCHED_BATCH, ¶m)) |
| 122 |
ioerror("pthread_getschedparam"); |
| 123 |
#endif |
| 124 |
per_day_worker(); |
| 125 |
return NULL; /* it should never be executed */ |
| 126 |
} |
| 127 |
|
| 128 |
#elif defined(USE_WIN32) |
| 129 |
|
| 130 |
int cron_init(void) { |
| 131 |
CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_THREAD_LIST]); |
| 132 |
per_second_thread_id=(HANDLE)_beginthreadex(NULL, 0, per_second_thread, NULL, 0, NULL); |
| 133 |
per_minute_thread_id=(HANDLE)_beginthreadex(NULL, 0, per_minute_thread, NULL, 0, NULL); |
| 134 |
per_day_thread_id=(HANDLE)_beginthreadex(NULL, 0, per_day_thread, NULL, 0, NULL); |
| 135 |
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_THREAD_LIST]); |
| 136 |
if(!per_day_thread_id || !per_day_thread_id) { |
| 137 |
ioerror("_beginthreadex"); |
| 138 |
return 1; |
| 139 |
} |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
NOEXPORT unsigned __stdcall per_second_thread(void *arg) { |
| 144 |
(void)arg; /* squash the unused parameter warning */ |
| 145 |
|
| 146 |
tls_alloc(NULL, NULL, "per-second"); |
| 147 |
per_second_worker(); |
| 148 |
_endthreadex(0); /* it should never be executed */ |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
NOEXPORT unsigned __stdcall per_minute_thread(void *arg) { |
| 153 |
(void)arg; /* squash the unused parameter warning */ |
| 154 |
|
| 155 |
tls_alloc(NULL, NULL, "per-minute"); |
| 156 |
per_minute_worker(); |
| 157 |
_endthreadex(0); /* it should never be executed */ |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
NOEXPORT unsigned __stdcall per_day_thread(void *arg) { |
| 162 |
(void)arg; /* squash the unused parameter warning */ |
| 163 |
|
| 164 |
tls_alloc(NULL, NULL, "per-day"); |
| 165 |
if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST)) |
| 166 |
ioerror("SetThreadPriority"); |
| 167 |
per_day_worker(); |
| 168 |
_endthreadex(0); /* it should never be executed */ |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
#else /* USE_OS_THREADS */ |
| 173 |
|
| 174 |
int cron_init(void) { |
| 175 |
/* not implemented for now */ |
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
#endif |
| 180 |
|
| 181 |
#ifdef USE_OS_THREADS |
| 182 |
|
| 183 |
NOEXPORT void per_second_worker(void) { |
| 184 |
s_log(LOG_DEBUG, "Per-second thread initialized"); |
| 185 |
for(;;) { |
| 186 |
s_poll_sleep(1, 0); /* 1 second */ |
| 187 |
|
| 188 |
CRYPTO_THREAD_read_lock(stunnel_locks[LOCK_LOG_MODE]); |
| 189 |
file_flush(outfile); |
| 190 |
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_LOG_MODE]); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
NOEXPORT void per_minute_worker(void) { |
| 195 |
s_log(LOG_DEBUG, "Per-minute thread initialized"); |
| 196 |
for(;;) { |
| 197 |
s_poll_sleep(60, 0); /* 1 minute */ |
| 198 |
|
| 199 |
per_minute_stapling_update(); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
NOEXPORT void per_minute_stapling_update(void) { |
| 204 |
#if !defined(OPENSSL_NO_OCSP) && OPENSSL_VERSION_NUMBER>=0x10002000L |
| 205 |
SERVICE_OPTIONS **srv, *opt; |
| 206 |
int num=0; |
| 207 |
|
| 208 |
/* acquire references of server sections */ |
| 209 |
CRYPTO_THREAD_read_lock(stunnel_locks[LOCK_SECTIONS]); |
| 210 |
srv=str_alloc(number_of_sections*sizeof(SERVICE_OPTIONS *)); |
| 211 |
for(opt=service_options.next; opt; opt=opt->next) |
| 212 |
if(!opt->option.client) |
| 213 |
srv[num++]=service_up_ref(opt); |
| 214 |
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_SECTIONS]); |
| 215 |
|
| 216 |
/* update stapling caches and release the references */ |
| 217 |
while(num--) { |
| 218 |
if(SSL_CTX_get0_certificate(srv[num]->ctx)) { |
| 219 |
ocsp_stapling(srv[num]); |
| 220 |
} |
| 221 |
service_free(srv[num]); |
| 222 |
} |
| 223 |
str_free(srv); |
| 224 |
#endif /* !OPENSSL_NO_OCSP && OPENSSL_VERSION_NUMBER>=0x10002000L */ |
| 225 |
} |
| 226 |
|
| 227 |
NOEXPORT void per_day_worker(void) { |
| 228 |
time_t now, then; |
| 229 |
int delay; |
| 230 |
BN_GENCB *bn_gencb; |
| 231 |
|
| 232 |
s_log(LOG_DEBUG, "Per-day thread initialized"); |
| 233 |
bn_gencb=per_day_bn_gencb(); |
| 234 |
time(&then); |
| 235 |
for(;;) { |
| 236 |
s_log(LOG_INFO, "Executing per-day jobs"); |
| 237 |
#ifndef OPENSSL_NO_DH |
| 238 |
per_day_dh_param(bn_gencb); |
| 239 |
#endif /* OPENSSL_NO_DH */ |
| 240 |
time(&now); |
| 241 |
s_log(LOG_INFO, "Per-day jobs completed in %d seconds", (int)(now-then)); |
| 242 |
then+=PER_DAY_PERIOD; |
| 243 |
if(then>now) { |
| 244 |
delay=(int)(then-now); |
| 245 |
} else { |
| 246 |
s_log(LOG_NOTICE, "Per-day backlog cleared (possible hibernation)"); |
| 247 |
delay=PER_DAY_PERIOD-(int)(now-then)%PER_DAY_PERIOD; |
| 248 |
then=now+delay; |
| 249 |
} |
| 250 |
s_log(LOG_DEBUG, "Waiting %d seconds", delay); |
| 251 |
do { /* retry s_poll_sleep() if it was interrupted by a signal */ |
| 252 |
s_poll_sleep(delay, 0); |
| 253 |
time(&now); |
| 254 |
delay=(int)(then-now); |
| 255 |
} while(delay>0); |
| 256 |
s_log(LOG_INFO, "Reopening log file"); |
| 257 |
signal_post(SIGNAL_REOPEN_LOG); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
#ifndef OPENSSL_NO_DH |
| 262 |
|
| 263 |
NOEXPORT void per_day_dh_param(BN_GENCB *bn_gencb) { |
| 264 |
SERVICE_OPTIONS *opt; |
| 265 |
DH *dh; |
| 266 |
|
| 267 |
if(!dh_temp_params || !service_options.next) |
| 268 |
return; |
| 269 |
|
| 270 |
s_log(LOG_NOTICE, "Updating DH parameters"); |
| 271 |
/* generate 2048-bit DH parameters */ |
| 272 |
dh=DH_new(); |
| 273 |
if(!dh) { |
| 274 |
ssl_error(NULL, "DH_new"); |
| 275 |
return; |
| 276 |
} |
| 277 |
if(!DH_generate_parameters_ex(dh, 2048, 2, bn_gencb)) { |
| 278 |
DH_free(dh); |
| 279 |
ssl_error(NULL, "DH_generate_parameters_ex"); |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
/* update global dh_params for future configuration reloads */ |
| 284 |
CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_DH]); |
| 285 |
DH_free(dh_params); |
| 286 |
dh_params=dh; |
| 287 |
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_DH]); |
| 288 |
|
| 289 |
/* set for all sections that require it */ |
| 290 |
CRYPTO_THREAD_read_lock(stunnel_locks[LOCK_SECTIONS]); |
| 291 |
for(opt=service_options.next; opt; opt=opt->next) |
| 292 |
if(opt->option.dh_temp_params) |
| 293 |
SSL_CTX_set_tmp_dh(opt->ctx, dh); |
| 294 |
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_SECTIONS]); |
| 295 |
s_log(LOG_NOTICE, "DH parameters updated"); |
| 296 |
} |
| 297 |
|
| 298 |
NOEXPORT BN_GENCB *per_day_bn_gencb(void) { |
| 299 |
#if OPENSSL_VERSION_NUMBER>=0x10100000L |
| 300 |
BN_GENCB *bn_gencb; |
| 301 |
|
| 302 |
bn_gencb=BN_GENCB_new(); |
| 303 |
if(!bn_gencb) { |
| 304 |
ssl_error(NULL, "BN_GENCB_new"); |
| 305 |
return NULL; |
| 306 |
} |
| 307 |
BN_GENCB_set(bn_gencb, bn_callback, NULL); |
| 308 |
return bn_gencb; |
| 309 |
#else |
| 310 |
static BN_GENCB bn_gencb; |
| 311 |
|
| 312 |
BN_GENCB_set(&bn_gencb, bn_callback, NULL); |
| 313 |
return &bn_gencb; |
| 314 |
#endif |
| 315 |
} |
| 316 |
|
| 317 |
NOEXPORT int bn_callback(int p, int n, BN_GENCB *cb) { |
| 318 |
(void)p; /* squash the unused parameter warning */ |
| 319 |
(void)n; /* squash the unused parameter warning */ |
| 320 |
(void)cb; /* squash the unused parameter warning */ |
| 321 |
s_poll_sleep(0, 100); /* 100ms */ |
| 322 |
return 1; /* return nonzero for success */ |
| 323 |
} |
| 324 |
|
| 325 |
#endif /* OPENSSL_NO_DH */ |
| 326 |
|
| 327 |
#endif /* USE_OS_THREADS */ |
| 328 |
|
| 329 |
/* end of cron.c */ |