26RCSID(
"$Id: b4d40b2750dfdea3cf3a112fd62e696586dd63d1 $")
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;
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 - buffer is too small.", len);
374 if (le == NULL)
break;
380 func(lb->type, lb->lvl, request, __FILE__, __LINE__,
"%pV",
403static int tls_log_request_bio_puts_cb(BIO *bio,
char const *
in)
405 return tls_log_request_bio_write_cb(bio,
in, strlen(
in));
411static int tls_log_request_bio_free_cb(BIO *bio)
414 BIO_set_init(bio, 0);
421static int tls_log_global_bio_create_cb(BIO *bio)
424 BIO_set_init(bio, 1);
436static int tls_log_global_bio_write_cb(BIO *bio,
char const *
in,
int len)
438 fr_tls_log_bio_t *lb = talloc_get_type_abort(BIO_get_data(bio), fr_tls_log_bio_t);
457 if (le == NULL)
break;
487static int tls_log_global_bio_puts_cb(BIO *bio,
char const *
in)
489 return tls_log_global_bio_write_cb(bio,
in, strlen(
in));
495static int tls_log_global_bio_free_cb(BIO *bio)
498 BIO_set_init(bio, 0);
505static int _fr_tls_log_bio_free(
void *log_bio)
507 fr_tls_log_bio_t *our_log_bio = talloc_get_type_abort(log_bio, fr_tls_log_bio_t);
509 BIO_free(our_log_bio->bio);
510 our_log_bio->bio = NULL;
532 fr_tls_log_bio_t *lb;
534 MEM(lb = talloc(NULL, fr_tls_log_bio_t));
535 *lb = (fr_tls_log_bio_t) {
536 .bio = BIO_new(tls_request_log_meth),
544 BIO_set_data(lb->bio, lb);
545 fr_sbuff_init_talloc(lb, &lb->sbuff, &lb->tctx, 1024, 10 * 1024);
547 fr_sbuff_marker(&lb->logged_m, &lb->sbuff);
551 fr_sbuff_marker(&request_log_bio->logged_m, &request_log_bio->sbuff);
552 request_log_bio->request = request;
553 request_log_bio->type =
type;
554 request_log_bio->lvl = lvl;
555 request_log_bio->file =
file;
556 request_log_bio->line =
line;
558 return request_log_bio->bio;
578 fr_tls_log_bio_t *lb;
580 MEM(lb = talloc(NULL, fr_tls_log_bio_t));
581 *lb = (fr_tls_log_bio_t) {
582 .bio = BIO_new(tls_global_log_meth),
589 BIO_set_data(lb->bio, lb);
590 fr_sbuff_init_talloc(lb, &lb->sbuff, &lb->tctx, 1024, 10 * 1024);
592 fr_sbuff_marker(&lb->logged_m, &lb->sbuff);
597 fr_sbuff_marker(&global_log_bio->logged_m, &global_log_bio->sbuff);
598 global_log_bio->type =
type;
599 global_log_bio->lvl = lvl;
600 global_log_bio->file =
file;
601 global_log_bio->line =
line;
603 return global_log_bio->bio;
609int fr_tls_log_init(
void)
621 tls_request_log_meth = BIO_meth_new(BIO_get_new_index() | BIO_TYPE_SOURCE_SINK,
"fr_tls_request_log");
622 if (
unlikely(!tls_request_log_meth))
return -1;
624 BIO_meth_set_create(tls_request_log_meth, tls_log_request_bio_create_cb);
625 BIO_meth_set_write(tls_request_log_meth, tls_log_request_bio_write_cb);
626 BIO_meth_set_puts(tls_request_log_meth, tls_log_request_bio_puts_cb);
627 BIO_meth_set_destroy(tls_request_log_meth, tls_log_request_bio_free_cb);
629 tls_global_log_meth = BIO_meth_new(BIO_get_new_index() | BIO_TYPE_SOURCE_SINK,
"fr_tls_global_log");
630 if (
unlikely(!tls_global_log_meth)) {
631 BIO_meth_free(tls_request_log_meth);
632 tls_request_log_meth = NULL;
636 BIO_meth_set_create(tls_global_log_meth, tls_log_global_bio_create_cb);
637 BIO_meth_set_write(tls_global_log_meth, tls_log_global_bio_write_cb);
638 BIO_meth_set_puts(tls_global_log_meth, tls_log_global_bio_puts_cb);
639 BIO_meth_set_destroy(tls_global_log_meth, tls_log_global_bio_free_cb);
647void fr_tls_log_free(
void)
657 if (tls_request_log_meth) {
658 BIO_meth_free(tls_request_log_meth);
659 tls_request_log_meth = NULL;
662 if (tls_global_log_meth) {
663 BIO_meth_free(tls_global_log_meth);
664 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,...)
#define fr_log(_log, _lvl, _file, _line, _fmt,...)
@ 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.
#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.
char * fr_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap)
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)