From a07bc460ae1a3d3582a6dac7e48ed1ea117990ef Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Wed, 5 Feb 2003 05:56:53 +0000 Subject: 2003-02-02 Havoc Pennington * dbus/dbus-keyring.c, dbus/dbus-keyring.h: template files for code to manage cookies in your home directory * dbus/dbus-sysdeps.c (_dbus_generate_random_bytes): new function * dbus/dbus-auth.c (get_state): impose a maximum number of tries to authenticate, then disconnect the client. --- dbus/dbus-sysdeps.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'dbus/dbus-sysdeps.c') diff --git a/dbus/dbus-sysdeps.c b/dbus/dbus-sysdeps.c index db5118a5..38efb0cd 100644 --- a/dbus/dbus-sysdeps.c +++ b/dbus/dbus-sysdeps.c @@ -1336,5 +1336,68 @@ _dbus_directory_close (DBusDirIter *iter) dbus_free (iter); } +/** + * Generates the given number of random bytes, + * using the best mechanism we can come up with. + * + * @param str the string + * @param n_bytes the number of random bytes to append to string + * @returns #TRUE on success, #FALSE if no memory or other failure + */ +dbus_bool_t +_dbus_generate_random_bytes (DBusString *str, + int n_bytes) +{ + int old_len; + int fd; + + old_len = _dbus_string_get_length (str); + fd = -1; + + /* note, urandom on linux will fall back to pseudorandom */ + fd = open ("/dev/urandom", O_RDONLY); + if (fd < 0) + { + unsigned long tv_usec; + int i; + + /* fall back to pseudorandom */ + + _dbus_get_current_time (NULL, &tv_usec); + srand (tv_usec); + + i = 0; + while (i < n_bytes) + { + double r; + int b; + + r = rand (); + b = (r / (double) RAND_MAX) * 255.0; + + if (!_dbus_string_append_byte (str, b)) + goto failed; + + ++i; + } + + return TRUE; + } + else + { + if (_dbus_read (fd, str, n_bytes) != n_bytes) + goto failed; + + close (fd); + + return TRUE; + } + + failed: + _dbus_string_set_length (str, old_len); + if (fd >= 0) + close (fd); + return FALSE; +} /** @} end of sysdeps */ -- cgit