/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-keyring.c Store secret cookies in your homedir
*
- * Copyright (C) 2003 Red Hat Inc.
+ * Copyright (C) 2003, 2004 Red Hat Inc.
*
- * Licensed under the Academic Free License version 1.2
+ * Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include "dbus-keyring.h"
#include "dbus-userdb.h"
+#include "dbus-protocol.h"
#include <dbus/dbus-string.h>
#include <dbus/dbus-list.h>
#include <dbus/dbus-sysdeps.h>
*/
#define MAX_TIME_TRAVEL_SECONDS (60*5)
+/**
+ * Maximum number of keys in the keyring before
+ * we just ignore the rest
+ */
+#ifdef DBUS_BUILD_TESTS
+#define MAX_KEYS_IN_FILE 10
+#else
+#define MAX_KEYS_IN_FILE 256
+#endif
+
+/**
+ * A single key from the cookie file
+ */
typedef struct
{
dbus_int32_t id; /**< identifier used to refer to the key */
return keyring;
out_4:
- _dbus_string_free (&keyring->username);
+ _dbus_string_free (&keyring->filename_lock);
out_3:
_dbus_string_free (&keyring->filename);
out_2:
DBusKey *new;
DBusString bytes;
int id;
- unsigned long timestamp;
+ long timestamp;
const unsigned char *s;
dbus_bool_t retval;
DBusKey *keys;
}
keys = new;
+ *keys_p = keys; /* otherwise *keys_p ends up invalid */
n_keys += 1;
if (!_dbus_string_init (&keys[n_keys-1].secret))
0))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+ _dbus_string_free (&keys[n_keys-1].secret);
+ n_keys -= 1;
goto out;
}
retval = TRUE;
out:
- if (retval)
- {
- *n_keys_p = n_keys;
- *keys_p = keys;
- }
+ *n_keys_p = n_keys;
_dbus_string_free (&bytes);
return retval;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
+ if (!_dbus_check_dir_is_private_to_user (&keyring->directory, error))
+ return FALSE;
+
if (!_dbus_string_init (&contents))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
_dbus_string_free (&contents);
return FALSE;
}
-
+
keys = NULL;
n_keys = 0;
retval = FALSE;
_dbus_warn ("Secret keyring file contains non-ASCII! Ignoring existing contents\n");
_dbus_string_set_length (&contents, 0);
}
-
+
+ /* FIXME this is badly inefficient for large keyring files
+ * (not that large keyring files exist outside of test suites)
+ */
while (_dbus_string_pop_line (&contents, &line))
{
int next;
int id;
long timestamp;
int len;
+ int end;
DBusKey *new;
+
+ /* Don't load more than the max. */
+ if (n_keys >= (add_new ? MAX_KEYS_IN_FILE - 1 : MAX_KEYS_IN_FILE))
+ break;
next = 0;
if (!_dbus_string_parse_int (&line, 0, &val, &next))
continue;
}
- if (val > _DBUS_INT_MAX || val < 0)
+ if (val > _DBUS_INT32_MAX || val < 0)
{
_dbus_verbose ("invalid secret key ID at start of line\n");
continue;
keys[n_keys-1].id = id;
keys[n_keys-1].creation_time = timestamp;
- if (!_dbus_string_hex_decode (&line, next,
- &keys[n_keys-1].secret,
- 0))
- {
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
- goto out;
- }
+ if (!_dbus_string_hex_decode (&line, next, &end,
+ &keys[n_keys-1].secret, 0))
+ {
+ dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+ goto out;
+ }
+
+ if (_dbus_string_get_length (&line) != end)
+ {
+ _dbus_verbose ("invalid hex encoding in keyring file\n");
+ _dbus_string_free (&keys[n_keys - 1].secret);
+ n_keys -= 1;
+ continue;
+ }
}
_dbus_verbose ("Successfully loaded %d existing keys\n",
if (!add_new_key (&keys, &n_keys, error))
{
_dbus_verbose ("Failed to generate new key: %s\n",
- error ? "(unknown)" : error->message);
+ error ? error->message : "(unknown)");
goto out;
}
goto out;
}
- dbus_free (keyring->keys);
+ if (keyring->keys)
+ free_keys (keyring->keys, keyring->n_keys);
keyring->keys = keys;
keyring->n_keys = n_keys;
keys = NULL;
* Increments reference count of the keyring
*
* @param keyring the keyring
+ * @returns the keyring
*/
-void
+DBusKeyring *
_dbus_keyring_ref (DBusKeyring *keyring)
{
keyring->refcount += 1;
+
+ return keyring;
}
/**
error_set = FALSE;
if (!_dbus_string_init (&homedir))
- return FALSE;
+ {
+ dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+ return NULL;
+ }
_dbus_string_init_const (&dotdir, ".dbus-keyrings");
{
_dbus_string_set_length (&homedir, 0);
if (!_dbus_string_append (&homedir, override))
- _dbus_assert_not_reached ("no memory");
+ goto failed;
_dbus_verbose ("Using fake homedir for testing: %s\n",
_dbus_string_get_const_data (&homedir));
if (!_dbus_string_copy (&homedir, 0,
&keyring->directory, 0))
goto failed;
-
- _dbus_string_free (&homedir);
if (!_dbus_concat_dir_and_file (&keyring->directory,
&dotdir))
tmp_error.message);
dbus_error_free (&tmp_error);
}
+
+ _dbus_string_free (&homedir);
return keyring;
if (!error_set)
dbus_set_error_const (error,
DBUS_ERROR_NO_MEMORY,
- "No memory to create keyring");
+ NULL);
if (keyring)
_dbus_keyring_unref (keyring);
_dbus_string_free (&homedir);
- return FALSE;
+ return NULL;
}
printf (" %d keys in test\n", ring1->n_keys);
+ /* Test ref/unref */
+ _dbus_keyring_ref (ring1);
+ _dbus_keyring_ref (ring2);
+ _dbus_keyring_unref (ring1);
+ _dbus_keyring_unref (ring2);
+
+
+ /* really unref */
_dbus_keyring_unref (ring1);
_dbus_keyring_unref (ring2);