From 0710f3c93cb530bb52274e501002c2a3c6254080 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 9 Dec 2013 21:17:57 +0000 Subject: [PATCH] Move PRNG seeding function to the util set. --- src/main.c | 16 ++-------------- src/util.c | 29 +++++++++++++++++++++++++++++ src/util.h | 12 ++---------- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/main.c b/src/main.c index 11da41921..7bbe63199 100644 --- a/src/main.c +++ b/src/main.c @@ -991,10 +991,6 @@ main (gint argc, gchar **argv, gchar **env) GList *l; worker_t **pworker; GQuark type; -#ifdef HAVE_OPENSSL - gchar rand_bytes[sizeof (guint32)]; - guint32 rand_seed; -#endif #ifdef HAVE_SA_SIGINFO signals_info = g_queue_new (); @@ -1049,21 +1045,13 @@ main (gint argc, gchar **argv, gchar **env) #ifdef HAVE_OPENSSL ERR_load_crypto_strings (); - /* Init random generator */ - if (RAND_bytes (rand_bytes, sizeof (rand_bytes)) != 1) { - msg_err ("cannot seed random generator using openssl: %s, using time", ERR_error_string (ERR_get_error (), NULL)); - g_random_set_seed (time (NULL)); - } - else { - memcpy (&rand_seed, rand_bytes, sizeof (guint32)); - g_random_set_seed (rand_seed); - } - OpenSSL_add_all_algorithms (); OpenSSL_add_all_digests (); OpenSSL_add_all_ciphers (); #endif + rspamd_prng_seed (); + /* First set logger to console logger */ rspamd_set_logger (RSPAMD_LOG_CONSOLE, type, rspamd_main); (void)open_log (rspamd_main->logger); diff --git a/src/util.c b/src/util.c index bc2206579..53d73b53a 100644 --- a/src/util.c +++ b/src/util.c @@ -31,6 +31,11 @@ #include "filter.h" #include "message.h" +#ifdef HAVE_OPENSSL +#include +#include +#endif + /* Check log messages intensity once per minute */ #define CHECK_TIME 60 /* More than 2 log messages per second */ @@ -2386,6 +2391,30 @@ restart: return p - buf; #endif } + +void +rspamd_prng_seed (void) +{ + guint32 rand_seed = 0; +#ifdef HAVE_OPENSSL + gchar rand_bytes[sizeof (guint32)]; + + /* Init random generator */ + if (RAND_bytes (rand_bytes, sizeof (rand_bytes)) != 1) { + msg_err ("cannot seed random generator using openssl: %s, using time", + ERR_error_string (ERR_get_error (), NULL)); + rand_seed = time (NULL); + } + else { + memcpy (&rand_seed, rand_bytes, sizeof (guint32)); + } +#else + rand_seed = time (NULL); +#endif + + g_random_set_seed (rand_seed); +} + /* * vi:ts=4 */ diff --git a/src/util.h b/src/util.h index f2b7132af..4edf5cb95 100644 --- a/src/util.h +++ b/src/util.h @@ -440,16 +440,8 @@ time_t parse_http_date (const gchar *header, gsize len); gint rspamd_read_passphrase (gchar *buf, gint size, gint rwflag, gpointer key); /** - * Expand path that may contain configuration variables: - * $CONFDIR - configuration directory - * $RUNDIR - local states directory - * $DBDIR - databases dir - * $LOGDIR - logs dir - * $PLUGINSDIR - plugins dir - * $PREFIX - installation prefix - * $VERSION - rspamd version - * @param pool to use - * @param path path to expand + * Seed glib prng using openssl if possible */ +void rspamd_prng_seed (void); #endif -- 2.39.5