26RCSID(
"$Id: f2a4bedfee68dbe3fc16c8f57f824770de295f0e $")
30#define LOG_PREFIX "tls"
32#include <freeradius-devel/util/debug.h>
33#include <freeradius-devel/util/atexit.h>
34#include <freeradius-devel/tls/strerror.h>
35#include <freeradius-devel/tls/utils.h>
65static BIO_METHOD *tls_request_log_meth;
69static BIO_METHOD *tls_global_log_meth;
83static _Thread_local fr_tls_log_bio_t *request_log_bio;
87static _Thread_local fr_tls_log_bio_t *global_log_bio;
98void _fr_tls_chain_log(
char const *
file,
int line,
100 STACK_OF(X509) *chain, X509 *cert)
106 _fr_tls_strerror_push_chain(
file,
line, chain, cert);
124void _fr_tls_chain_marker_log(
char const *
file,
int line,
126 STACK_OF(X509) *chain, X509 *cert, X509 *marker)
132 _fr_tls_strerror_push_chain_marker(
file,
line, chain, cert, marker);
148void _fr_tls_x509_objects_log(
char const *
file,
int line,
150 STACK_OF(X509_OBJECT) *objects)
154 _fr_tls_strerror_push_x509_objects(
file,
line, objects);
183int fr_tls_log_io_error(
request_t *request,
int err,
char const *
fmt, ...)
186 {
L(
"SSL_ERROR_NONE"), SSL_ERROR_NONE },
187 {
L(
"SSL_ERROR_ZERO_RETURN"), SSL_ERROR_ZERO_RETURN },
188 {
L(
"SSL_ERROR_WANT_READ"), SSL_ERROR_WANT_READ },
189 {
L(
"SSL_ERROR_WANT_WRITE"), SSL_ERROR_WANT_WRITE },
190 {
L(
"SSL_ERROR_WANT_CONNECT"), SSL_ERROR_WANT_CONNECT },
191 {
L(
"SSL_ERROR_WANT_ACCEPT"), SSL_ERROR_WANT_ACCEPT },
192 {
L(
"SSL_ERROR_WANT_X509_LOOKUP"), SSL_ERROR_WANT_X509_LOOKUP },
193 {
L(
"SSL_ERROR_WANT_ASYNC"), SSL_ERROR_WANT_ASYNC },
194 {
L(
"SSL_ERROR_WANT_ASYNC_JOB"), SSL_ERROR_WANT_ASYNC_JOB },
195 {
L(
"SSL_ERROR_WANT_CLIENT_HELLO_CB"), SSL_ERROR_WANT_CLIENT_HELLO_CB },
196 {
L(
"SSL_ERROR_SYSCALL"), SSL_ERROR_SYSCALL },
197 {
L(
"SSL_ERROR_SSL"), SSL_ERROR_SSL }
199 static size_t ssl_io_error_table_len =
NUM_ELEMENTS(ssl_io_error_table);
218 case SSL_ERROR_WANT_READ:
219 case SSL_ERROR_WANT_WRITE:
220 case SSL_ERROR_WANT_X509_LOOKUP:
221 case SSL_ERROR_ZERO_RETURN:
238 case SSL_ERROR_SYSCALL:
255 (void)fr_tls_strerror_vprintf(
fmt, ap);
299 ret = fr_tls_strerror_vprintf(
msg, ap);
310void fr_tls_log_clear(
void)
312 while (ERR_get_error() != 0);
318static int tls_log_request_bio_create_cb(BIO *bio)
321 BIO_set_init(bio, 1);
333static int tls_log_request_bio_write_cb(BIO *bio,
char const *
in,
int len)
335 fr_tls_log_bio_t *lb = talloc_get_type_abort(BIO_get_data(bio), fr_tls_log_bio_t);
361 REDEBUG2(
"Failed copying %u bytes into TLS log aggregation buffer, "
362 "needed %zu more bytes", len, (
size_t)(-(slen)));
375 if (le == NULL)
break;
381 func(lb->type, lb->lvl, request, __FILE__, __LINE__,
"%pV",
404static int tls_log_request_bio_puts_cb(BIO *bio,
char const *
in)
406 return tls_log_request_bio_write_cb(bio,
in, strlen(
in));
412static int tls_log_request_bio_free_cb(BIO *bio)
415 BIO_set_init(bio, 0);
422static int tls_log_global_bio_create_cb(BIO *bio)
425 BIO_set_init(bio, 1);
437static int tls_log_global_bio_write_cb(BIO *bio,
char const *
in,
int len)
439 fr_tls_log_bio_t *lb = talloc_get_type_abort(BIO_get_data(bio), fr_tls_log_bio_t);
458 if (le == NULL)
break;
488static int tls_log_global_bio_puts_cb(BIO *bio,
char const *
in)
490 return tls_log_global_bio_write_cb(bio,
in, strlen(
in));
496static int tls_log_global_bio_free_cb(BIO *bio)
499 BIO_set_init(bio, 0);
506static int _fr_tls_log_bio_free(
void *log_bio)
508 fr_tls_log_bio_t *our_log_bio = talloc_get_type_abort(log_bio, fr_tls_log_bio_t);
510 BIO_free(our_log_bio->bio);
511 our_log_bio->bio = NULL;
533 fr_tls_log_bio_t *lb;
535 MEM(lb = talloc(NULL, fr_tls_log_bio_t));
536 *lb = (fr_tls_log_bio_t) {
537 .bio = BIO_new(tls_request_log_meth),
545 BIO_set_data(lb->bio, lb);
546 fr_sbuff_init_talloc(lb, &lb->sbuff, &lb->tctx, 1024, 10 * 1024);
548 fr_sbuff_marker(&lb->logged_m, &lb->sbuff);
552 fr_sbuff_marker(&request_log_bio->logged_m, &request_log_bio->sbuff);
553 request_log_bio->request = request;
554 request_log_bio->type =
type;
555 request_log_bio->lvl = lvl;
556 request_log_bio->file =
file;
557 request_log_bio->line =
line;
559 return request_log_bio->bio;
579 fr_tls_log_bio_t *lb;
581 MEM(lb = talloc(NULL, fr_tls_log_bio_t));
582 *lb = (fr_tls_log_bio_t) {
583 .bio = BIO_new(tls_global_log_meth),
590 BIO_set_data(lb->bio, lb);
591 fr_sbuff_init_talloc(lb, &lb->sbuff, &lb->tctx, 1024, 10 * 1024);
593 fr_sbuff_marker(&lb->logged_m, &lb->sbuff);
598 fr_sbuff_marker(&global_log_bio->logged_m, &global_log_bio->sbuff);
599 global_log_bio->type =
type;
600 global_log_bio->lvl = lvl;
601 global_log_bio->file =
file;
602 global_log_bio->line =
line;
604 return global_log_bio->bio;
610int fr_tls_log_init(
void)
622 tls_request_log_meth = BIO_meth_new(BIO_get_new_index() | BIO_TYPE_SOURCE_SINK,
"fr_tls_request_log");
623 if (
unlikely(!tls_request_log_meth))
return -1;
625 BIO_meth_set_create(tls_request_log_meth, tls_log_request_bio_create_cb);
626 BIO_meth_set_write(tls_request_log_meth, tls_log_request_bio_write_cb);
627 BIO_meth_set_puts(tls_request_log_meth, tls_log_request_bio_puts_cb);
628 BIO_meth_set_destroy(tls_request_log_meth, tls_log_request_bio_free_cb);
630 tls_global_log_meth = BIO_meth_new(BIO_get_new_index() | BIO_TYPE_SOURCE_SINK,
"fr_tls_global_log");
631 if (
unlikely(!tls_global_log_meth)) {
632 BIO_meth_free(tls_request_log_meth);
633 tls_request_log_meth = NULL;
637 BIO_meth_set_create(tls_global_log_meth, tls_log_global_bio_create_cb);
638 BIO_meth_set_write(tls_global_log_meth, tls_log_global_bio_write_cb);
639 BIO_meth_set_puts(tls_global_log_meth, tls_log_global_bio_puts_cb);
640 BIO_meth_set_destroy(tls_global_log_meth, tls_log_global_bio_free_cb);
648void fr_tls_log_free(
void)
658 if (tls_request_log_meth) {
659 BIO_meth_free(tls_request_log_meth);
660 tls_request_log_meth = NULL;
663 if (tls_global_log_meth) {
664 BIO_meth_free(tls_global_log_meth);
665 tls_global_log_meth = NULL;
static int const char * fmt
#define fr_atexit_thread_local(_name, _free, _uctx)
#define USES_APPLE_DEPRECATED_API
#define L(_str)
Helper for initialising arrays of string literals.
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
void log_request(fr_log_type_t type, fr_log_lvl_t lvl, request_t *request, char const *file, int line, char const *fmt,...)
Marshal variadic log arguments into a va_list and pass to normal logging functions.
void log_request_perror(fr_log_type_t type, fr_log_lvl_t lvl, request_t *request, char const *file, int line, char const *fmt,...)
Drain any outstanding messages from the fr_strerror buffers.
void log_request_error(fr_log_type_t type, fr_log_lvl_t lvl, request_t *request, char const *file, int line, char const *fmt,...)
Marshal variadic log arguments into a va_list and pass to error logging functions.
#define DEBUG_ENABLED2
True if global debug level 1-2 messages are enabled.
#define ROPTIONAL(_l_request, _l_global, _fmt,...)
Use different logging functions depending on whether request is NULL or not.
void(* log_request_func_t)(fr_log_type_t type, fr_log_lvl_t lvl, request_t *request, char const *file, int line, char const *fmt,...)
Function signature for log_request functions.
#define REDEBUG2(fmt,...)
void fr_log(fr_log_t const *log, fr_log_type_t type, char const *file, int line, char const *fmt,...)
Send a server log message to its destination.
@ L_DBG_LVL_OFF
No debug messages.
@ L_DBG_ERR
Error only displayed when debugging is enabled.
@ L_DBG_ERR_REQ
Less severe error only displayed when debugging is enabled.
char * fr_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap)
#define RDEBUG_ENABLED2()
size_t fr_sbuff_shift(fr_sbuff_t *sbuff, size_t shift, bool move_end)
Shift the contents of the sbuff, returning the number of bytes we managed to shift.
int fr_sbuff_reset_talloc(fr_sbuff_t *sbuff)
Reset a talloced buffer to its initial length, clearing any data stored.
ssize_t fr_sbuff_in_bstrncpy(fr_sbuff_t *sbuff, char const *str, size_t len)
Copy bytes into the sbuff up to the first \0.
#define fr_sbuff_set(_dst, _src)
#define fr_sbuff_current(_sbuff_or_marker)
#define fr_sbuff_used(_sbuff_or_marker)
#define fr_sbuff_behind(_sbuff_or_marker)
Talloc sbuff extension structure.
fr_aka_sim_id_type_t type
#define atomic_fetch_sub(object, operand)
#define atomic_load(object)
#define atomic_fetch_add(object, operand)
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
An element in an arbitrarily ordered array of name to num mappings.
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
#define fr_box_strvalue_len(_val, _len)