[dovecot-cvs] dovecot/src/lib randgen.c,1.6,1.7

cras at procontrol.fi cras at procontrol.fi
Fri Apr 4 18:40:16 EEST 2003


Update of /home/cvs/dovecot/src/lib
In directory danu:/tmp/cvs-serv18305/src/lib

Modified Files:
	randgen.c 
Log Message:
Support using OpenSSL's pseudo-random generator instead of /dev/urandom. If
neither are found, allow dovecot-auth still to be started because currently
only DIGEST-MD5 requires prng.



Index: randgen.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/randgen.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- randgen.c	18 Dec 2002 15:15:41 -0000	1.6
+++ randgen.c	4 Apr 2003 14:40:14 -0000	1.7
@@ -24,9 +24,11 @@
 */
 
 #include "lib.h"
-#include "fd-close-on-exec.h"
 #include "randgen.h"
 
+#ifdef HAVE_URANDOM
+
+#include "fd-close-on-exec.h"
 #include <unistd.h>
 #include <fcntl.h>
 
@@ -74,3 +76,45 @@
 	(void)close(urandom_fd);
 	urandom_fd = -1;
 }
+
+#elif defined(HAVE_OPENSSL_RAND_H)
+#include <openssl/rand.h>
+#include <openssl/err.h>
+
+static const char *ssl_last_error(void)
+{
+	unsigned long err;
+	char *buf;
+	size_t err_size = 256;
+
+	err = ERR_get_error();
+	if (err == 0)
+		return strerror(errno);
+
+	buf = t_malloc(err_size);
+	buf[err_size-1] = '\0';
+	ERR_error_string_n(err, buf, err_size-1);
+	return buf;
+}
+
+void random_fill(void *buf, size_t size)
+{
+	if (RAND_pseudo_bytes(buf, size) != 1)
+		i_fatal("RAND_pseudo_bytes() failed: %s", ssl_last_error());
+}
+
+void random_init(void) {}
+void random_deinit(void) {}
+
+#else
+#  warning Random generator disabled
+
+void random_fill(void *buf, size_t size)
+{
+	i_fatal("random_fill(): No random source");
+}
+
+void random_init(void) {}
+void random_deinit(void) {}
+
+#endif




More information about the dovecot-cvs mailing list