Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
Provide magic function when compiling against openssl
Browse files Browse the repository at this point in the history
  • Loading branch information
pprindeville committed Oct 14, 2016
1 parent dc23e6e commit ae4f521
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
21 changes: 1 addition & 20 deletions libtac/lib/header.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,7 @@
#include "config.h"
#endif

#if defined(HAVE_OPENSSL_RAND_H) && defined(HAVE_LIBCRYPTO)
# include <openssl/rand.h>
#elif defined(HAVE_GETRANDOM)
# if defined(HAVE_LINUX_RANDOM_H)
# include <linux/random.h>
# elif defined(HAVE_SYS_RANDOM_H)
# include <sys/random.h>
# 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.
Expand Down Expand Up @@ -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);

Expand Down
40 changes: 39 additions & 1 deletion libtac/lib/magic.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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 <openssl/rand.h>

/*
* 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 <linux/random.h>
# elif defined(HAVE_SYS_RANDOM_H)
# include <sys/random.h>
# endif

/*
* magic - Returns the next magic number.
*/
u_int32_t
magic()
{
u_int32_t num;

getrandom(&num, sizeof(num), GRND_NONBLOCK);
return num;
}

#else

#include "magic.h"

Expand Down Expand Up @@ -91,3 +128,4 @@ magic()
}

#endif

0 comments on commit ae4f521

Please sign in to comment.