26RCSID(
"$Id: f3afd9867014695e7f6bb46fe3f1d45b704e584f $")
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);
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);
295static int tls_log_vperror(
request_t *
request,
char const *prefix,
char const *
msg, va_list ap)
299 ret = fr_tls_strerror_vprintf(
msg, ap);
325 ret = tls_log_vperror(
request,
"(TLS) ",
msg, ap);
359void fr_tls_log_clear(
void)
361 while (ERR_get_error() != 0);
367static int tls_log_request_bio_create_cb(BIO *bio)
370 BIO_set_init(bio, 1);
382static int tls_log_request_bio_write_cb(BIO *bio,
char const *
in,
int len)
384 fr_tls_log_bio_t *lb = talloc_get_type_abort(BIO_get_data(bio), fr_tls_log_bio_t);
410 REDEBUG2(
"(TLS) Failed copying %d bytes into TLS log aggregation buffer - buffer is too small.",
len);
423 if (le == NULL)
break;
429 func(lb->type, lb->lvl,
request, __FILE__, __LINE__,
"%pV",
452static int tls_log_request_bio_puts_cb(BIO *bio,
char const *
in)
454 return tls_log_request_bio_write_cb(bio,
in, strlen(
in));
460static int tls_log_request_bio_free_cb(BIO *bio)
463 BIO_set_init(bio, 0);
470static int tls_log_global_bio_create_cb(BIO *bio)
473 BIO_set_init(bio, 1);
485static int tls_log_global_bio_write_cb(BIO *bio,
char const *
in,
int len)
487 fr_tls_log_bio_t *lb = talloc_get_type_abort(BIO_get_data(bio), fr_tls_log_bio_t);
506 if (le == NULL)
break;
536static int tls_log_global_bio_puts_cb(BIO *bio,
char const *
in)
538 return tls_log_global_bio_write_cb(bio,
in, strlen(
in));
544static int tls_log_global_bio_free_cb(BIO *bio)
547 BIO_set_init(bio, 0);
554static int _fr_tls_log_bio_free(
void *log_bio)
556 fr_tls_log_bio_t *our_log_bio = talloc_get_type_abort(log_bio, fr_tls_log_bio_t);
558 BIO_free(our_log_bio->bio);
559 our_log_bio->bio = NULL;
581 fr_tls_log_bio_t *lb;
583 MEM(lb = talloc(NULL, fr_tls_log_bio_t));
584 *lb = (fr_tls_log_bio_t) {
585 .bio = BIO_new(tls_request_log_meth),
593 BIO_set_data(lb->bio, lb);
594 fr_sbuff_init_talloc(lb, &lb->sbuff, &lb->tctx, 1024, 10 * 1024);
596 fr_sbuff_marker(&lb->logged_m, &lb->sbuff);
600 fr_sbuff_marker(&request_log_bio->logged_m, &request_log_bio->sbuff);
601 request_log_bio->request =
request;
602 request_log_bio->type =
type;
603 request_log_bio->lvl = lvl;
604 request_log_bio->file =
file;
605 request_log_bio->line =
line;
607 return request_log_bio->bio;
627 fr_tls_log_bio_t *lb;
629 MEM(lb = talloc(NULL, fr_tls_log_bio_t));
630 *lb = (fr_tls_log_bio_t) {
631 .bio = BIO_new(tls_global_log_meth),
638 BIO_set_data(lb->bio, lb);
639 fr_sbuff_init_talloc(lb, &lb->sbuff, &lb->tctx, 1024, 10 * 1024);
641 fr_sbuff_marker(&lb->logged_m, &lb->sbuff);
646 fr_sbuff_marker(&global_log_bio->logged_m, &global_log_bio->sbuff);
647 global_log_bio->type =
type;
648 global_log_bio->lvl = lvl;
649 global_log_bio->file =
file;
650 global_log_bio->line =
line;
652 return global_log_bio->bio;
658int fr_tls_log_init(
void)
670 tls_request_log_meth = BIO_meth_new(BIO_get_new_index() | BIO_TYPE_SOURCE_SINK,
"fr_tls_request_log");
671 if (
unlikely(!tls_request_log_meth))
return -1;
673 BIO_meth_set_create(tls_request_log_meth, tls_log_request_bio_create_cb);
674 BIO_meth_set_write(tls_request_log_meth, tls_log_request_bio_write_cb);
675 BIO_meth_set_puts(tls_request_log_meth, tls_log_request_bio_puts_cb);
676 BIO_meth_set_destroy(tls_request_log_meth, tls_log_request_bio_free_cb);
678 tls_global_log_meth = BIO_meth_new(BIO_get_new_index() | BIO_TYPE_SOURCE_SINK,
"fr_tls_global_log");
679 if (
unlikely(!tls_global_log_meth)) {
680 BIO_meth_free(tls_request_log_meth);
681 tls_request_log_meth = NULL;
685 BIO_meth_set_create(tls_global_log_meth, tls_log_global_bio_create_cb);
686 BIO_meth_set_write(tls_global_log_meth, tls_log_global_bio_write_cb);
687 BIO_meth_set_puts(tls_global_log_meth, tls_log_global_bio_puts_cb);
688 BIO_meth_set_destroy(tls_global_log_meth, tls_log_global_bio_free_cb);
696void fr_tls_log_free(
void)
706 if (tls_request_log_meth) {
707 BIO_meth_free(tls_request_log_meth);
708 tls_request_log_meth = NULL;
711 if (tls_global_log_meth) {
712 BIO_meth_free(tls_global_log_meth);
713 tls_global_log_meth = NULL;
static int const char * fmt
#define fr_atexit_thread_local(_name, _free, _uctx)
static uint8_t request[4]
#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.
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.
static fr_sbuff_err_t char size_t fr_sbuff_t * in
#define fr_box_strvalue_len(_val, _len)
static fr_sbuff_err_t char size_t * len