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

Commit 89d559e

Browse files
committed
Provide magic function when compiling against openssl
1 parent 05ef324 commit 89d559e

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

libtac/lib/header.c

+1-20
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,7 @@
2626
#include "config.h"
2727
#endif
2828

29-
#if defined(HAVE_OPENSSL_RAND_H) && defined(HAVE_LIBCRYPTO)
30-
# include <openssl/rand.h>
31-
#elif defined(HAVE_GETRANDOM)
32-
# if defined(HAVE_LINUX_RANDOM_H)
33-
# include <linux/random.h>
34-
# elif defined(HAVE_SYS_RANDOM_H)
35-
# include <sys/random.h>
36-
# endif
37-
#else
38-
# include "magic.h"
39-
#endif
29+
#include "magic.h"
4030

4131
/* Miscellaneous variables that are global, because we need
4232
* store their values between different functions and connections.
@@ -88,16 +78,7 @@ HDR *_tac_req_header(u_char type, int cont_session) {
8878

8979
/* make session_id from pseudo-random number */
9080
if (!cont_session) {
91-
#if defined(HAVE_OPENSSL_RAND_H) && defined(HAVE_LIBCRYPTO)
92-
// the preferred way is to use OpenSSL abstraction as we are linking it anyway for MD5
93-
RAND_pseudo_bytes((unsigned char *) &session_id, sizeof(session_id));
94-
#elif defined(HAVE_GETRANDOM)
95-
// experimental
96-
getrandom((void *) &session_id, sizeof(session_id), GRND_NONBLOCK);
97-
#else
98-
// if everything fails use the legacy code
9981
session_id = magic();
100-
#endif
10182
}
10283
th->session_id = htonl(session_id);
10384

libtac/lib/magic.c

+40-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "config.h"
3131
#endif
3232

33+
#include "magic.h"
34+
3335
#ifdef _MSC_VER
3436
# pragma section(".CRT$XCU",read)
3537
# define INITIALIZER2_(f,p) \
@@ -49,9 +51,44 @@
4951
#endif
5052

5153
/* if OpenSSL library is available this legacy code will not be compiled in */
52-
#if !(defined(HAVE_OPENSSL_RAND_H) && defined(HAVE_LIBCRYPTO))
54+
#if defined(HAVE_OPENSSL_RAND_H) && defined(HAVE_LIBCRYPTO)
5355

54-
#include "magic.h"
56+
#include <openssl/rand.h>
57+
58+
/*
59+
* magic - Returns the next magic number.
60+
*/
61+
u_int32_t
62+
magic()
63+
{
64+
u_int32_t num;
65+
66+
RAND_pseudo_bytes((unsigned char *)&num, sizeof(num));
67+
68+
return num;
69+
}
70+
71+
#elif defined(HAVE_GETRANDOM)
72+
73+
# if defined(HAVE_LINUX_RANDOM_H)
74+
# include <linux/random.h>
75+
# elif defined(HAVE_SYS_RANDOM_H)
76+
# include <sys/random.h>
77+
# endif
78+
79+
/*
80+
* magic - Returns the next magic number.
81+
*/
82+
u_int32_t
83+
magic()
84+
{
85+
u_int32_t num;
86+
87+
getrandom(&num, sizeof(num), GRND_NONBLOCK);
88+
return num;
89+
}
90+
91+
#else
5592

5693
/*
5794
* magic_init - Initialize the magic number generator.
@@ -91,3 +128,4 @@ magic()
91128
}
92129

93130
#endif
131+

0 commit comments

Comments
 (0)