summaryrefslogtreecommitdiff
path: root/libtac
diff options
context:
space:
mode:
Diffstat (limited to 'libtac')
-rw-r--r--libtac/lib/header.c21
-rw-r--r--libtac/lib/magic.c82
-rw-r--r--libtac/lib/magic.h1
-rw-r--r--libtac/lib/md5.c2
4 files changed, 65 insertions, 41 deletions
diff --git a/libtac/lib/header.c b/libtac/lib/header.c
index 4852ac7..2156d2a 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 <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.
@@ -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 d70a81a..a320df5 100644
--- a/libtac/lib/magic.c
+++ b/libtac/lib/magic.c
@@ -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>
@@ -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);
@@ -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
+
diff --git a/libtac/lib/magic.h b/libtac/lib/magic.h
index c05331d..b23df00 100644
--- a/libtac/lib/magic.h
+++ b/libtac/lib/magic.h
@@ -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
diff --git a/libtac/lib/md5.c b/libtac/lib/md5.c
index 5315450..453448b 100644
--- a/libtac/lib/md5.c
+++ b/libtac/lib/md5.c
@@ -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"