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

Commit

Permalink
Merge pull request #80 from pprindeville/reorg-magic
Browse files Browse the repository at this point in the history
Reorg magic
  • Loading branch information
pprindeville authored Dec 21, 2016
2 parents 0a3234c + 6fac250 commit 773a1ca
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 41 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
82 changes: 63 additions & 19 deletions libtac/lib/magic.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@
* See `CHANGES' file for revision history.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* if OpenSSL library is available this legacy code will not be compiled in */
#if !defined(HAVE_OPENSSL_RAND_H) && !defined(HAVE_LIBCRYPTO)

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
Expand All @@ -33,25 +26,81 @@
#include <sys/stat.h>
#include <fcntl.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "magic.h"

static int magic_initialised = 0;
#ifdef _MSC_VER
# pragma section(".CRT$XCU",read)
# define INITIALIZER2_(f,p) \
static void f(void); \
__declspec(allocate(".CRT$XCU")) void (*f##_)(void) = f; \
__pragma(comment(linker,"/include:" p #f "_")) \
static void f(void)
# ifdef _WIN64
# define INITIALIZER(f) INITIALIZER2_(f,"")
# else
# define INITIALIZER(f) INITIALIZER2_(f,"_")
# endif
#else /* __GNUC__ */
# define INITIALIZER(f) \
static void f(void) __attribute__((constructor)); \
static void f(void)
#endif

/* if OpenSSL library is available this legacy code will not be compiled in */
#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

/*
* magic_init - Initialize the magic number generator.
*
* Attempts to compute a random number seed which will not repeat.
*/
void
magic_init()
INITIALIZER(magic_init)
{
struct stat statbuf;
long seed = 0;
struct timeval t;

if (magic_initialised)
return;

// try to initialise seed from urandom
if (!lstat("/dev/urandom", &statbuf) && S_ISCHR(statbuf.st_mode)) {
int rfd = open("/dev/urandom", O_RDONLY);
Expand All @@ -67,21 +116,16 @@ magic_init()

// finally seed the PRNG
srandom(seed);
magic_initialised = 1;
}

#include <pthread.h>
/*
* magic - Returns the next magic number.
*/
u_int32_t
magic()
{
static pthread_once_t magic_control = PTHREAD_ONCE_INIT;

pthread_once(&magic_control, &magic_init);

return (u_int32_t)random();
}

#endif

1 change: 0 additions & 1 deletion libtac/lib/magic.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "libtac.h"

__BEGIN_DECLS
void magic_init __P((void)); /* Initialize the magic number generator */
u_int32_t magic __P((void)); /* Returns the next magic number */
__END_DECLS

Expand Down
2 changes: 1 addition & 1 deletion libtac/lib/md5.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#endif

/* if OpenSSL library is available this legacy code will not be compiled in */
#if !defined(HAVE_OPENSSL_MD5_H) && !defined(HAVE_LIBCRYPTO)
#if !(defined(HAVE_OPENSSL_MD5_H) && defined(HAVE_LIBCRYPTO))

#include <string.h>
#include "md5.h"
Expand Down

0 comments on commit 773a1ca

Please sign in to comment.