From b23ea82b4466b9721e4ee12e62dae636870f2834 Mon Sep 17 00:00:00 2001 From: Philip Prindeville Date: Thu, 6 Oct 2016 18:24:38 -0600 Subject: [PATCH] Provide magic function when compiling against openssl --- libtac/lib/header.c | 21 +-------------------- libtac/lib/magic.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/libtac/lib/header.c b/libtac/lib/header.c index 4852ac7b..2156d2a4 100644 --- a/libtac/lib/header.c +++ b/libtac/lib/header.c @@ -26,17 +26,7 @@ #include "config.h" #endif -#if defined(HAVE_OPENSSL_RAND_H) && defined(HAVE_LIBCRYPTO) -# include -#elif defined(HAVE_GETRANDOM) -# if defined(HAVE_LINUX_RANDOM_H) -# include -# elif defined(HAVE_SYS_RANDOM_H) -# include -# endif -#else -# include "magic.h" -#endif +#include "magic.h" /* Miscellaneous variables that are global, because we need * store their values between different functions and connections. @@ -88,16 +78,7 @@ HDR *_tac_req_header(u_char type, int cont_session) { /* make session_id from pseudo-random number */ if (!cont_session) { -#if defined(HAVE_OPENSSL_RAND_H) && defined(HAVE_LIBCRYPTO) - // the preferred way is to use OpenSSL abstraction as we are linking it anyway for MD5 - RAND_pseudo_bytes((unsigned char *) &session_id, sizeof(session_id)); -#elif defined(HAVE_GETRANDOM) - // experimental - getrandom((void *) &session_id, sizeof(session_id), GRND_NONBLOCK); -#else - // if everything fails use the legacy code session_id = magic(); -#endif } th->session_id = htonl(session_id); diff --git a/libtac/lib/magic.c b/libtac/lib/magic.c index 491856be..a320df59 100644 --- a/libtac/lib/magic.c +++ b/libtac/lib/magic.c @@ -30,6 +30,8 @@ #include "config.h" #endif +#include "magic.h" + #ifdef _MSC_VER # pragma section(".CRT$XCU",read) # define INITIALIZER2_(f,p) \ @@ -49,9 +51,44 @@ #endif /* if OpenSSL library is available this legacy code will not be compiled in */ -#if !(defined(HAVE_OPENSSL_RAND_H) && defined(HAVE_LIBCRYPTO)) +#if defined(HAVE_OPENSSL_RAND_H) && defined(HAVE_LIBCRYPTO) -#include "magic.h" +#include + +/* + * magic - Returns the next magic number. + */ +u_int32_t +magic() +{ + u_int32_t num; + + RAND_pseudo_bytes((unsigned char *)&num, sizeof(num)); + + return num; +} + +#elif defined(HAVE_GETRANDOM) + +# if defined(HAVE_LINUX_RANDOM_H) +# include +# elif defined(HAVE_SYS_RANDOM_H) +# include +# endif + +/* + * magic - Returns the next magic number. + */ +u_int32_t +magic() +{ + u_int32_t num; + + getrandom(&num, sizeof(num), GRND_NONBLOCK); + return num; +} + +#else /* * magic_init - Initialize the magic number generator. @@ -91,3 +128,4 @@ magic() } #endif +