summaryrefslogtreecommitdiffstats
path: root/dbus/dbus-sysdeps.c
diff options
context:
space:
mode:
authorHavoc Pennington <hp@redhat.com>2003-02-05 05:56:53 +0000
committerHavoc Pennington <hp@redhat.com>2003-02-05 05:56:53 +0000
commita07bc460ae1a3d3582a6dac7e48ed1ea117990ef (patch)
tree951980157d356ce81ef1aadc9b860dce85acd4a9 /dbus/dbus-sysdeps.c
parent69824400caa9ff2a1f24e06728f27e0f50f0033d (diff)
2003-02-02 Havoc Pennington <hp@pobox.com>
* 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.
Diffstat (limited to 'dbus/dbus-sysdeps.c')
-rw-r--r--dbus/dbus-sysdeps.c63
1 files changed, 63 insertions, 0 deletions
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 */