/* -*- 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>
* file in the user's homedir. However they are transient (only used
* by a single server instance for a fixed period of time, then
* discarded). Also, the keys are not sent over the wire.
+ *
+ * @todo there's a memory leak on some codepath in here, I saw it once
+ * when running make check - probably some specific initial cookies
+ * present in the cookie file, then depending on what we do with them.
*/
/**
*/
#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");
if (username == NULL)
{
const DBusString *const_homedir;
-
- if (!_dbus_user_info_from_current_process (&username,
- &const_homedir,
- NULL))
+
+ if (!_dbus_username_from_current_process (&username) ||
+ !_dbus_homedir_from_current_process (&const_homedir))
goto failed;
if (!_dbus_string_copy (const_homedir, 0,
goto failed;
}
+#ifdef DBUS_BUILD_TESTS
+ {
+ const char *override;
+
+ override = _dbus_getenv ("DBUS_TEST_HOMEDIR");
+ if (override != NULL && *override != '\0')
+ {
+ _dbus_string_set_length (&homedir, 0);
+ if (!_dbus_string_append (&homedir, override))
+ goto failed;
+
+ _dbus_verbose ("Using fake homedir for testing: %s\n",
+ _dbus_string_get_const_data (&homedir));
+ }
+ else
+ {
+ static dbus_bool_t already_warned = FALSE;
+ if (!already_warned)
+ {
+ _dbus_warn ("Using your real home directory for testing, set DBUS_TEST_HOMEDIR to avoid\n");
+ already_warned = TRUE;
+ }
+ }
+ }
+#endif
+
_dbus_assert (username != NULL);
keyring = _dbus_keyring_new ();
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);