diff options
Diffstat (limited to 'common/gdm-common.c')
-rw-r--r-- | common/gdm-common.c | 201 |
1 files changed, 0 insertions, 201 deletions
diff --git a/common/gdm-common.c b/common/gdm-common.c index 59545039..f6075f37 100644 --- a/common/gdm-common.c +++ b/common/gdm-common.c @@ -32,7 +32,6 @@ #include <glib/gstdio.h> #include "gdm-common.h" -#include "gdm-md5.h" void gdm_set_fatal_warnings_if_unstable (void) @@ -273,203 +272,3 @@ gdm_string_hex_decode (const GString *source, return retval; } - -static void -_gdm_generate_pseudorandom_bytes_buffer (char *buffer, - int n_bytes) -{ - int i; - - /* fall back to pseudorandom */ - g_debug ("Falling back to pseudorandom for %d bytes\n", - n_bytes); - - i = 0; - while (i < n_bytes) { - int b; - - b = g_random_int_range (0, 255); - - buffer[i] = b; - - ++i; - } -} - -static gboolean -_gdm_generate_pseudorandom_bytes (GString *str, - int n_bytes) -{ - int old_len; - char *p; - - old_len = str->len; - - str = g_string_set_size (str, old_len + n_bytes); - - p = str->str + old_len; - - _gdm_generate_pseudorandom_bytes_buffer (p, n_bytes); - - return TRUE; -} - - -static int -_gdm_fdread (int fd, - GString *buffer, - int count) -{ - int bytes_read; - int start; - char *data; - - g_assert (count >= 0); - - start = buffer->len; - - buffer = g_string_set_size (buffer, start + count); - - data = buffer->str + start; - - again: - bytes_read = read (fd, data, count); - - if (bytes_read < 0) { - if (errno == EINTR) { - goto again; - } else { - /* put length back (note that this doesn't actually realloc anything) */ - buffer = g_string_set_size (buffer, start); - return -1; - } - } else { - /* put length back (doesn't actually realloc) */ - buffer = g_string_set_size (buffer, start + bytes_read); - - return bytes_read; - } -} - -/** - * Closes a file descriptor. - * - * @param fd the file descriptor - * @param error error object - * @returns #FALSE if error set - */ -static gboolean -_gdm_fdclose (int fd) -{ - again: - if (close (fd) < 0) { - if (errno == EINTR) - goto again; - - g_warning ("Could not close fd %d: %s", - fd, - g_strerror (errno)); - return FALSE; - } - - return TRUE; -} - -/** - * 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 - */ -gboolean -gdm_generate_random_bytes (GString *str, - int n_bytes) -{ - int old_len; - int fd; - - /* FALSE return means "no memory", if it could - * mean something else then we'd need to return - * a DBusError. So we always fall back to pseudorandom - * if the I/O fails. - */ - - old_len = str->len; - fd = -1; - - /* note, urandom on linux will fall back to pseudorandom */ - fd = g_open ("/dev/urandom", O_RDONLY, 0); - if (fd < 0) { - return _gdm_generate_pseudorandom_bytes (str, n_bytes); - } - - if (_gdm_fdread (fd, str, n_bytes) != n_bytes) { - _gdm_fdclose (fd); - str = g_string_set_size (str, old_len); - return _gdm_generate_pseudorandom_bytes (str, n_bytes); - } - - g_debug ("Read %d bytes from /dev/urandom\n", n_bytes); - - _gdm_fdclose (fd); - - return TRUE; -} - -/** - * Computes the ASCII hex-encoded md5sum of the given data and - * appends it to the output string. - * - * @param data input data to be hashed - * @param ascii_output string to append ASCII md5sum to - * @returns #FALSE if not enough memory - */ -static gboolean -gdm_md5_compute (const GString *data, - GString *ascii_output) -{ - GdmMD5Context context; - GString *digest; - - gdm_md5_init (&context); - - gdm_md5_update (&context, data); - - digest = g_string_new (NULL); - if (digest == NULL) - return FALSE; - - if (! gdm_md5_final (&context, digest)) - goto error; - - if (! gdm_string_hex_encode (digest, - 0, - ascii_output, - ascii_output->len)) - goto error; - - g_string_free (digest, TRUE); - - return TRUE; - - error: - g_string_free (digest, TRUE); - - return FALSE; -} - -gboolean -gdm_generate_cookie (GString *result) -{ - gboolean ret; - GString *data; - - data = g_string_new (NULL); - gdm_generate_random_bytes (data, 16); - - ret = gdm_md5_compute (data, result); - g_string_free (data, TRUE); - - return ret; -} |