dbus-keyring.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-keyring.c Store secret cookies in your homedir
*
* Copyright (C) 2003, 2004 Red Hat Inc.
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <config.h>
#include "dbus-keyring.h"
#include "dbus-protocol.h"
#include <dbus/dbus-string.h>
#include <dbus/dbus-list.h>
#include <dbus/dbus-sysdeps.h>
#include <dbus/dbus-test-tap.h>
#define NEW_KEY_TIMEOUT_SECONDS (60*5)
#define EXPIRE_KEYS_TIMEOUT_SECONDS (NEW_KEY_TIMEOUT_SECONDS + (60*2))
#define MAX_TIME_TRAVEL_SECONDS (60*5)
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
#define MAX_KEYS_IN_FILE 10
#else
#define MAX_KEYS_IN_FILE 256
#endif
typedef struct
{
dbus_int32_t id;
dbus_int64_t creation_time;
DBusString secret;
} DBusKey;
struct DBusKeyring
{
int refcount;
DBusString directory;
DBusString filename;
DBusString filename_lock;
DBusKey *keys;
int n_keys;
DBusCredentials *credentials;
};
static DBusKeyring*
_dbus_keyring_new (void)
{
DBusKeyring *keyring;
keyring = dbus_new0 (DBusKeyring, 1);
if (keyring == NULL)
goto out_0;
if (!_dbus_string_init (&keyring->directory))
goto out_1;
if (!_dbus_string_init (&keyring->filename))
goto out_2;
if (!_dbus_string_init (&keyring->filename_lock))
goto out_3;
keyring->refcount = 1;
keyring->keys = NULL;
keyring->n_keys = 0;
return keyring;
out_3:
_dbus_string_free (&keyring->filename);
out_2:
_dbus_string_free (&keyring->directory);
out_1:
dbus_free (keyring);
out_0:
return NULL;
}
static void
free_keys (DBusKey *keys,
int n_keys)
{
int i;
/* should be safe for args NULL, 0 */
i = 0;
while (i < n_keys)
{
_dbus_string_free (&keys[i].secret);
++i;
}
dbus_free (keys);
}
/* Our locking scheme is highly unreliable. However, there is
* unfortunately no reliable locking scheme in user home directories;
* between bugs in Linux NFS, people using Tru64 or other total crap
* NFS, AFS, random-file-system-of-the-week, and so forth, fcntl() in
* homedirs simply generates tons of bug reports. This has been
* learned through hard experience with GConf, unfortunately.
*
* This bad hack might work better for the kind of lock we have here,
* which we don't expect to hold for any length of time. Crashing
* while we hold it should be unlikely, and timing out such that we
* delete a stale lock should also be unlikely except when the
* filesystem is running really slowly. Stuff might break in corner
* cases but as long as it's not a security-level breakage it should
* be OK.
*/
#define MAX_LOCK_TIMEOUTS 32
#define LOCK_TIMEOUT_MILLISECONDS 250
static dbus_bool_t
_dbus_keyring_lock (DBusKeyring *keyring)
{
int n_timeouts;
n_timeouts = 0;
while (n_timeouts < MAX_LOCK_TIMEOUTS)
{
DBusError error = DBUS_ERROR_INIT;
if (_dbus_create_file_exclusively (&keyring->filename_lock,
&error))
break;
_dbus_verbose ("Did not get lock file, sleeping %d milliseconds (%s)\n",
LOCK_TIMEOUT_MILLISECONDS, error.message);
dbus_error_free (&error);
_dbus_sleep_milliseconds (LOCK_TIMEOUT_MILLISECONDS);
++n_timeouts;
}
if (n_timeouts == MAX_LOCK_TIMEOUTS)
{
DBusError error = DBUS_ERROR_INIT;
_dbus_verbose ("Lock file timed out %d times, assuming stale\n",
n_timeouts);
if (!_dbus_delete_file (&keyring->filename_lock, &error))
{
_dbus_verbose ("Couldn't delete old lock file: %s\n",
error.message);
dbus_error_free (&error);
return FALSE;
}
if (!_dbus_create_file_exclusively (&keyring->filename_lock,
&error))
{
_dbus_verbose ("Couldn't create lock file after deleting stale one: %s\n",
error.message);
dbus_error_free (&error);
return FALSE;
}
}
return TRUE;
}
static void
_dbus_keyring_unlock (DBusKeyring *keyring)
{
DBusError error = DBUS_ERROR_INIT;
if (!_dbus_delete_file (&keyring->filename_lock, &error))
{
_dbus_warn ("Failed to delete lock file: %s",
error.message);
dbus_error_free (&error);
}
}
static DBusKey*
find_key_by_id (DBusKey *keys,
int n_keys,
int id)
{
int i;
i = 0;
while (i < n_keys)
{
if (keys[i].id == id)
return &keys[i];
++i;
}
return NULL;
}
static dbus_bool_t
add_new_key (DBusKey **keys_p,
int *n_keys_p,
DBusError *error)
{
DBusKey *new;
DBusString bytes;
int id;
dbus_int64_t timestamp;
const unsigned char *s;
dbus_bool_t retval;
DBusKey *keys;
int n_keys;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
if (!_dbus_string_init (&bytes))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return FALSE;
}
keys = *keys_p;
n_keys = *n_keys_p;
retval = FALSE;
/* Generate an integer ID and then the actual key. */
retry:
if (!_dbus_generate_random_bytes (&bytes, 4, error))
goto out;
s = (const unsigned char*) _dbus_string_get_const_data (&bytes);
id = s[0] | (s[1] << 8) | (s[2] << 16) | ((s[3] & 0x7f) << 24);
_dbus_assert (id >= 0);
if (find_key_by_id (keys, n_keys, id) != NULL)
{
_dbus_string_set_length (&bytes, 0);
_dbus_verbose ("Key ID %d already existed, trying another one\n",
id);
goto retry;
}
_dbus_verbose ("Creating key with ID %d\n", id);
#define KEY_LENGTH_BYTES 24
_dbus_string_set_length (&bytes, 0);
if (!_dbus_generate_random_bytes (&bytes, KEY_LENGTH_BYTES, error))
{
goto out;
}
new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
if (new == NULL)
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto out;
}
keys = new;
*keys_p = keys; /* otherwise *keys_p ends up invalid */
n_keys += 1;
if (!_dbus_string_init (&keys[n_keys-1].secret))
{
n_keys -= 1; /* we don't want to free the one we didn't init */
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto out;
}
_dbus_get_real_time (&timestamp, NULL);
keys[n_keys-1].id = id;
keys[n_keys-1].creation_time = timestamp;
if (!_dbus_string_move (&bytes, 0,
&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:
*n_keys_p = n_keys;
_dbus_string_free (&bytes);
return retval;
}
static dbus_bool_t
_dbus_keyring_reload (DBusKeyring *keyring,
dbus_bool_t add_new,
DBusError *error)
{
DBusString contents;
DBusString line;
dbus_bool_t retval;
dbus_bool_t have_lock;
DBusKey *keys;
int n_keys;
int i;
dbus_int64_t now;
DBusError tmp_error;
_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);
return FALSE;
}
if (!_dbus_string_init (&line))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
_dbus_string_free (&contents);
return FALSE;
}
keys = NULL;
n_keys = 0;
retval = FALSE;
have_lock = FALSE;
_dbus_get_real_time (&now, NULL);
if (add_new)
{
if (!_dbus_keyring_lock (keyring))
{
dbus_set_error (error, DBUS_ERROR_FAILED,
"Could not lock keyring file to add to it");
goto out;
}
have_lock = TRUE;
}
dbus_error_init (&tmp_error);
if (!_dbus_file_get_contents (&contents,
&keyring->filename,
&tmp_error))
{
_dbus_verbose ("Failed to load keyring file: %s\n",
tmp_error.message);
/* continue with empty keyring file, so we recreate it */
dbus_error_free (&tmp_error);
}
if (!_dbus_string_validate_ascii (&contents, 0,
_dbus_string_get_length (&contents)))
{
_dbus_warn ("Secret keyring file contains non-ASCII! Ignoring existing contents");
_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;
long val;
int id;
dbus_int64_t 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))
{
_dbus_verbose ("could not parse secret key ID at start of line\n");
continue;
}
if (val > _DBUS_INT32_MAX || val < 0)
{
_dbus_verbose ("invalid secret key ID at start of line\n");
continue;
}
id = val;
_dbus_string_skip_blank (&line, next, &next);
if (!_dbus_string_parse_int64 (&line, next, &timestamp, &next))
{
_dbus_verbose ("could not parse secret key timestamp\n");
continue;
}
if (timestamp < 0 ||
(now + MAX_TIME_TRAVEL_SECONDS) < timestamp ||
(now - EXPIRE_KEYS_TIMEOUT_SECONDS) > timestamp)
{
_dbus_verbose ("dropping/ignoring %" DBUS_INT64_MODIFIER "d-seconds old key with timestamp %" DBUS_INT64_MODIFIER "d as current time is %" DBUS_INT64_MODIFIER "d\n",
now - timestamp, timestamp, now);
continue;
}
_dbus_string_skip_blank (&line, next, &next);
len = _dbus_string_get_length (&line);
if ((len - next) == 0)
{
_dbus_verbose ("no secret key after ID and timestamp\n");
continue;
}
/* We have all three parts */
new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
if (new == NULL)
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto out;
}
keys = new;
n_keys += 1;
if (!_dbus_string_init (&keys[n_keys-1].secret))
{
n_keys -= 1; /* we don't want to free the one we didn't init */
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto out;
}
keys[n_keys-1].id = id;
keys[n_keys-1].creation_time = timestamp;
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",
n_keys);
if (add_new)
{
if (!add_new_key (&keys, &n_keys, error))
{
_dbus_verbose ("Failed to generate new key: %s\n",
error ? error->message : "(unknown)");
goto out;
}
_dbus_string_set_length (&contents, 0);
i = 0;
while (i < n_keys)
{
if (!_dbus_string_append_printf (&contents, "%d %" DBUS_INT64_MODIFIER "d ",
keys[i].id, keys[i].creation_time))
goto nomem;
if (!_dbus_string_hex_encode (&keys[i].secret, 0,
&contents,
_dbus_string_get_length (&contents)))
goto nomem;
if (!_dbus_string_append_byte (&contents, '\n'))
goto nomem;
++i;
continue;
nomem:
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto out;
}
if (!_dbus_string_save_to_file (&contents, &keyring->filename,
FALSE, error))
goto out;
}
if (keyring->keys)
free_keys (keyring->keys, keyring->n_keys);
keyring->keys = keys;
keyring->n_keys = n_keys;
keys = NULL;
n_keys = 0;
retval = TRUE;
out:
if (have_lock)
_dbus_keyring_unlock (keyring);
if (! ((retval == TRUE && (error == NULL || error->name == NULL)) ||
(retval == FALSE && (error == NULL || error->name != NULL))))
{
if (error && error->name)
_dbus_verbose ("error is %s: %s\n", error->name, error->message);
_dbus_warn ("returning %d but error pointer %p name %s",
retval, error, error->name ? error->name : "(none)");
_dbus_assert_not_reached ("didn't handle errors properly");
}
if (keys != NULL)
{
i = 0;
while (i < n_keys)
{
_dbus_string_zero (&keys[i].secret);
_dbus_string_free (&keys[i].secret);
++i;
}
dbus_free (keys);
}
_dbus_string_free (&contents);
_dbus_string_free (&line);
return retval;
}
/* end of internals */
DBusKeyring *
_dbus_keyring_ref (DBusKeyring *keyring)
{
keyring->refcount += 1;
return keyring;
}
void
_dbus_keyring_unref (DBusKeyring *keyring)
{
keyring->refcount -= 1;
if (keyring->refcount == 0)
{
if (keyring->credentials)
_dbus_credentials_unref (keyring->credentials);
_dbus_string_free (&keyring->filename);
_dbus_string_free (&keyring->filename_lock);
_dbus_string_free (&keyring->directory);
free_keys (keyring->keys, keyring->n_keys);
dbus_free (keyring);
}
}
DBusKeyring*
_dbus_keyring_new_for_credentials (DBusCredentials *credentials,
const DBusString *context,
DBusError *error)
{
DBusString ringdir;
DBusKeyring *keyring;
dbus_bool_t error_set;
DBusError tmp_error;
DBusCredentials *our_credentials;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
if (_dbus_check_setuid ())
{
dbus_set_error_const (error, DBUS_ERROR_NOT_SUPPORTED,
"Unable to create DBus keyring when setuid");
return NULL;
}
keyring = NULL;
error_set = FALSE;
our_credentials = NULL;
if (!_dbus_string_init (&ringdir))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return NULL;
}
if (credentials != NULL)
{
our_credentials = _dbus_credentials_copy (credentials);
}
else
{
our_credentials = _dbus_credentials_new_from_current_process ();
}
if (our_credentials == NULL)
goto failed;
if (_dbus_credentials_are_anonymous (our_credentials))
{
if (!_dbus_credentials_add_from_current_process (our_credentials))
goto failed;
}
if (!_dbus_append_keyring_directory_for_credentials (&ringdir,
our_credentials))
goto failed;
keyring = _dbus_keyring_new ();
if (keyring == NULL)
goto failed;
_dbus_assert (keyring->credentials == NULL);
keyring->credentials = our_credentials;
our_credentials = NULL; /* so we don't unref it again later */
/* should have been validated already, but paranoia check here */
if (!_dbus_keyring_validate_context (context))
{
error_set = TRUE;
dbus_set_error_const (error,
DBUS_ERROR_FAILED,
"Invalid context in keyring creation");
goto failed;
}
/* Save keyring dir in the keyring object */
if (!_dbus_string_copy (&ringdir, 0,
&keyring->directory, 0))
goto failed;
/* Create keyring->filename based on keyring dir and context */
if (!_dbus_string_copy (&keyring->directory, 0,
&keyring->filename, 0))
goto failed;
if (!_dbus_concat_dir_and_file (&keyring->filename,
context))
goto failed;
/* Create lockfile name */
if (!_dbus_string_copy (&keyring->filename, 0,
&keyring->filename_lock, 0))
goto failed;
if (!_dbus_string_append (&keyring->filename_lock, ".lock"))
goto failed;
/* Reload keyring */
dbus_error_init (&tmp_error);
if (!_dbus_keyring_reload (keyring, FALSE, &tmp_error))
{
_dbus_verbose ("didn't load an existing keyring: %s\n",
tmp_error.message);
dbus_error_free (&tmp_error);
}
/* We don't fail fatally if we can't create the directory,
* but the keyring will probably always be empty
* unless someone else manages to create it
*/
dbus_error_init (&tmp_error);
if (!_dbus_ensure_directory (&keyring->directory,
&tmp_error))
{
_dbus_verbose ("Creating keyring directory: %s\n",
tmp_error.message);
dbus_error_free (&tmp_error);
}
_dbus_string_free (&ringdir);
return keyring;
failed:
if (!error_set)
dbus_set_error_const (error,
DBUS_ERROR_NO_MEMORY,
NULL);
if (our_credentials)
_dbus_credentials_unref (our_credentials);
if (keyring)
_dbus_keyring_unref (keyring);
_dbus_string_free (&ringdir);
return NULL;
}
dbus_bool_t
_dbus_keyring_validate_context (const DBusString *context)
{
if (_dbus_string_get_length (context) == 0)
{
_dbus_verbose ("context is zero-length\n");
return FALSE;
}
if (!_dbus_string_validate_ascii (context, 0,
_dbus_string_get_length (context)))
{
_dbus_verbose ("context not valid ascii\n");
return FALSE;
}
/* no directory separators */
if (_dbus_string_find (context, 0, "/", NULL))
{
_dbus_verbose ("context contains a slash\n");
return FALSE;
}
if (_dbus_string_find (context, 0, "\\", NULL))
{
_dbus_verbose ("context contains a backslash\n");
return FALSE;
}
/* prevent attempts to use dotfiles or ".." or ".lock"
* all of which might allow some kind of attack
*/
if (_dbus_string_find (context, 0, ".", NULL))
{
_dbus_verbose ("context contains a dot\n");
return FALSE;
}
/* no spaces/tabs, those are used for separators in the protocol */
if (_dbus_string_find_blank (context, 0, NULL))
{
_dbus_verbose ("context contains a blank\n");
return FALSE;
}
if (_dbus_string_find (context, 0, "\n", NULL))
{
_dbus_verbose ("context contains a newline\n");
return FALSE;
}
if (_dbus_string_find (context, 0, "\r", NULL))
{
_dbus_verbose ("context contains a carriage return\n");
return FALSE;
}
return TRUE;
}
static DBusKey*
find_recent_key (DBusKeyring *keyring)
{
int i;
dbus_int64_t tv_sec;
long tv_usec;
_dbus_get_real_time (&tv_sec, &tv_usec);
i = 0;
while (i < keyring->n_keys)
{
DBusKey *key = &keyring->keys[i];
_dbus_verbose ("Key %d is %" DBUS_INT64_MODIFIER "d seconds old\n",
i, tv_sec - key->creation_time);
if ((tv_sec - NEW_KEY_TIMEOUT_SECONDS) < key->creation_time)
return key;
++i;
}
return NULL;
}
int
_dbus_keyring_get_best_key (DBusKeyring *keyring,
DBusError *error)
{
DBusKey *key;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
key = find_recent_key (keyring);
if (key)
return key->id;
/* All our keys are too old, or we've never loaded the
* keyring. Create a new one.
*/
if (!_dbus_keyring_reload (keyring, TRUE,
error))
return -1;
key = find_recent_key (keyring);
if (key)
return key->id;
else
{
dbus_set_error_const (error,
DBUS_ERROR_FAILED,
"No recent-enough key found in keyring, and unable to create a new key");
return -1;
}
}
dbus_bool_t
_dbus_keyring_is_for_credentials (DBusKeyring *keyring,
DBusCredentials *credentials)
{
return _dbus_credentials_same_user (keyring->credentials,
credentials);
}
dbus_bool_t
_dbus_keyring_get_hex_key (DBusKeyring *keyring,
int key_id,
DBusString *hex_key)
{
DBusKey *key;
key = find_key_by_id (keyring->keys,
keyring->n_keys,
key_id);
if (key == NULL)
return TRUE; /* had enough memory, so TRUE */
return _dbus_string_hex_encode (&key->secret, 0,
hex_key,
_dbus_string_get_length (hex_key));
}
/* end of exposed API */
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
#include "dbus-test.h"
#include <stdio.h>
dbus_bool_t
_dbus_keyring_test (const char *test_data_dir _DBUS_GNUC_UNUSED)
{
DBusString context;
DBusKeyring *ring1;
DBusKeyring *ring2;
int id;
DBusError error;
int i;
ring1 = NULL;
ring2 = NULL;
/* Context validation */
_dbus_string_init_const (&context, "foo");
_dbus_assert (_dbus_keyring_validate_context (&context));
_dbus_string_init_const (&context, "org_freedesktop_blah");
_dbus_assert (_dbus_keyring_validate_context (&context));
_dbus_string_init_const (&context, "");
_dbus_assert (!_dbus_keyring_validate_context (&context));
_dbus_string_init_const (&context, ".foo");
_dbus_assert (!_dbus_keyring_validate_context (&context));
_dbus_string_init_const (&context, "bar.foo");
_dbus_assert (!_dbus_keyring_validate_context (&context));
_dbus_string_init_const (&context, "bar/foo");
_dbus_assert (!_dbus_keyring_validate_context (&context));
_dbus_string_init_const (&context, "bar\\foo");
_dbus_assert (!_dbus_keyring_validate_context (&context));
_dbus_string_init_const (&context, "foo\xfa\xf0");
_dbus_assert (!_dbus_keyring_validate_context (&context));
_dbus_string_init_const (&context, "foo\x80");
_dbus_assert (!_dbus_keyring_validate_context (&context));
_dbus_string_init_const (&context, "foo\x7f");
_dbus_assert (_dbus_keyring_validate_context (&context));
_dbus_string_init_const (&context, "foo bar");
_dbus_assert (!_dbus_keyring_validate_context (&context));
if (!_dbus_string_init (&context))
_dbus_test_fatal ("no memory");
if (!_dbus_string_append_byte (&context, '\0'))
_dbus_test_fatal ("no memory");
_dbus_assert (!_dbus_keyring_validate_context (&context));
_dbus_string_free (&context);
/* Now verify that if we create a key in keyring 1,
* it is properly loaded in keyring 2
*/
_dbus_string_init_const (&context, "org_freedesktop_dbus_testsuite");
dbus_error_init (&error);
ring1 = _dbus_keyring_new_for_credentials (NULL, &context,
&error);
_dbus_assert (ring1 != NULL);
_dbus_assert (error.name == NULL);
id = _dbus_keyring_get_best_key (ring1, &error);
if (id < 0)
{
fprintf (stderr, "Could not load keyring: %s\n", error.message);
dbus_error_free (&error);
goto failure;
}
ring2 = _dbus_keyring_new_for_credentials (NULL, &context, &error);
_dbus_assert (ring2 != NULL);
_dbus_assert (error.name == NULL);
if (ring1->n_keys != ring2->n_keys)
{
fprintf (stderr, "Different number of keys in keyrings\n");
goto failure;
}
/* We guarantee we load and save keeping keys in a fixed
* order
*/
i = 0;
while (i < ring1->n_keys)
{
if (ring1->keys[i].id != ring2->keys[i].id)
{
fprintf (stderr, "Keyring 1 has first key ID %d and keyring 2 has %d\n",
ring1->keys[i].id, ring2->keys[i].id);
goto failure;
}
if (ring1->keys[i].creation_time != ring2->keys[i].creation_time)
{
fprintf (stderr, "Keyring 1 has first key time %" DBUS_INT64_MODIFIER "d and keyring 2 has %" DBUS_INT64_MODIFIER "d\n",
ring1->keys[i].creation_time, ring2->keys[i].creation_time);
goto failure;
}
if (!_dbus_string_equal (&ring1->keys[i].secret,
&ring2->keys[i].secret))
{
fprintf (stderr, "Keyrings 1 and 2 have different secrets for same ID/timestamp\n");
goto failure;
}
++i;
}
_dbus_test_diag (" %d keys in test", 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);
return TRUE;
failure:
if (ring1)
_dbus_keyring_unref (ring1);
if (ring2)
_dbus_keyring_unref (ring2);
return FALSE;
}
#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
_dbus_credentials_same_user
dbus_bool_t _dbus_credentials_same_user(DBusCredentials *credentials, DBusCredentials *other_credentials)
Check whether the user-identifying credentials in two credentials objects are identical.
Definition dbus-credentials.c:747
_dbus_credentials_copy
DBusCredentials * _dbus_credentials_copy(DBusCredentials *credentials)
Copy a credentials object.
Definition dbus-credentials.c:718
_dbus_credentials_new_from_current_process
DBusCredentials * _dbus_credentials_new_from_current_process(void)
Creates a new object with the most important credentials (user ID and process ID) from the current pr...
Definition dbus-credentials.c:113
_dbus_credentials_unref
void _dbus_credentials_unref(DBusCredentials *credentials)
Decrement refcount on credentials.
Definition dbus-credentials.c:148
_dbus_credentials_are_anonymous
dbus_bool_t _dbus_credentials_are_anonymous(DBusCredentials *credentials)
Checks whether a credentials object contains a user identity.
Definition dbus-credentials.c:558
DBUS_ERROR_INIT
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition dbus-errors.h:64
dbus_set_error_const
void dbus_set_error_const(DBusError *error, const char *name, const char *message)
Assigns an error name and message to a DBusError.
Definition dbus-errors.c:245
dbus_error_init
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
Definition dbus-errors.c:190
dbus_set_error
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition dbus-errors.c:356
dbus_error_free
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition dbus-errors.c:213
_dbus_delete_file
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
Definition dbus-file-unix.c:441
_dbus_create_file_exclusively
dbus_bool_t _dbus_create_file_exclusively(const DBusString *filename, DBusError *error)
Creates the given file, failing if the file already exists.
Definition dbus-file-unix.c:395
_dbus_string_save_to_file
dbus_bool_t _dbus_string_save_to_file(const DBusString *str, const DBusString *filename, dbus_bool_t world_readable, DBusError *error)
Writes a string out to a file.
Definition dbus-file-unix.c:208
_dbus_file_get_contents
dbus_bool_t _dbus_file_get_contents(DBusString *str, const DBusString *filename, DBusError *error)
Appends the contents of the given file to the string, returning error code.
Definition dbus-file-unix.c:63
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
Definition dbus-internals.h:164
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
_dbus_warn
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
Definition dbus-internals.c:278
_DBUS_INT32_MAX
#define _DBUS_INT32_MAX
Maximum value of type "int32".
Definition dbus-internals.h:323
LOCK_TIMEOUT_MILLISECONDS
#define LOCK_TIMEOUT_MILLISECONDS
Length of each timeout while waiting for a lock.
Definition dbus-keyring.c:193
MAX_TIME_TRAVEL_SECONDS
#define MAX_TIME_TRAVEL_SECONDS
The maximum amount of time a key can be in the future.
Definition dbus-keyring.c:80
NEW_KEY_TIMEOUT_SECONDS
#define NEW_KEY_TIMEOUT_SECONDS
The maximum age of a key before we create a new key to use in challenges.
Definition dbus-keyring.c:70
MAX_KEYS_IN_FILE
#define MAX_KEYS_IN_FILE
Maximum number of keys in the keyring before we just ignore the rest.
Definition dbus-keyring.c:89
MAX_LOCK_TIMEOUTS
#define MAX_LOCK_TIMEOUTS
Maximum number of timeouts waiting for lock before we decide it's stale.
Definition dbus-keyring.c:191
EXPIRE_KEYS_TIMEOUT_SECONDS
#define EXPIRE_KEYS_TIMEOUT_SECONDS
The time after which we drop a key from the secrets file.
Definition dbus-keyring.c:76
_dbus_keyring_get_best_key
int _dbus_keyring_get_best_key(DBusKeyring *keyring, DBusError *error)
Gets a recent key to use for authentication.
Definition dbus-keyring.c:934
_dbus_keyring_validate_context
dbus_bool_t _dbus_keyring_validate_context(const DBusString *context)
Checks whether the context is a valid context.
Definition dbus-keyring.c:837
_dbus_keyring_is_for_credentials
dbus_bool_t _dbus_keyring_is_for_credentials(DBusKeyring *keyring, DBusCredentials *credentials)
Checks whether the keyring is for the same user as the given credentials.
Definition dbus-keyring.c:973
_dbus_keyring_new_for_credentials
DBusKeyring * _dbus_keyring_new_for_credentials(DBusCredentials *credentials, const DBusString *context, DBusError *error)
Creates a new keyring that lives in the ~/.dbus-keyrings directory of the user represented by credent...
Definition dbus-keyring.c:693
_dbus_keyring_get_hex_key
dbus_bool_t _dbus_keyring_get_hex_key(DBusKeyring *keyring, int key_id, DBusString *hex_key)
Gets the hex-encoded secret key for the given ID.
Definition dbus-keyring.c:992
_dbus_keyring_ref
DBusKeyring * _dbus_keyring_ref(DBusKeyring *keyring)
Increments reference count of the keyring.
Definition dbus-keyring.c:651
_dbus_keyring_unref
void _dbus_keyring_unref(DBusKeyring *keyring)
Decrements refcount and finalizes if it reaches zero.
Definition dbus-keyring.c:665
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:710
dbus_realloc
void * dbus_realloc(void *memory, size_t bytes)
Resizes a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:610
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition dbus-memory.h:60
DBUS_ERROR_NOT_SUPPORTED
#define DBUS_ERROR_NOT_SUPPORTED
Requested operation isn't supported (like ENOSYS on UNIX).
Definition dbus-protocol.h:375
DBUS_ERROR_FAILED
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
Definition dbus-protocol.h:361
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition dbus-protocol.h:363
_dbus_string_set_length
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition dbus-string.c:847
_dbus_string_hex_decode
dbus_bool_t _dbus_string_hex_decode(const DBusString *source, int start, int *end_return, DBusString *dest, int insert_at)
Decodes a string from hex encoding.
Definition dbus-string.c:2432
_dbus_string_append
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition dbus-string.c:980
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition dbus-string.c:182
_dbus_string_init_const
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition dbus-string.c:197
_dbus_string_copy
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that's copied to the d...
Definition dbus-string.c:1345
_dbus_string_parse_int64
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_parse_int64(const DBusString *str, int start, dbus_int64_t *value_return, int *end_return)
Parses a dbus_int64_t integer contained in a DBusString.
Definition dbus-sysdeps.c:449
_dbus_string_skip_blank
void _dbus_string_skip_blank(const DBusString *str, int start, int *end)
Skips blanks from start, storing the first non-blank in *end (blank is space or tab).
Definition dbus-string.c:1865
_dbus_string_find
dbus_bool_t _dbus_string_find(const DBusString *str, int start, const char *substr, int *found)
Finds the given substring in the string, returning TRUE and filling in the byte index where the subst...
Definition dbus-string.c:1666
_dbus_string_find_blank
dbus_bool_t _dbus_string_find_blank(const DBusString *str, int start, int *found)
Finds a blank (space or tab) in the string.
Definition dbus-string.c:1827
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as _DBUS_STRING_IN...
Definition dbus-string.c:278
_dbus_string_pop_line
dbus_bool_t _dbus_string_pop_line(DBusString *source, DBusString *dest)
Assigns a newline-terminated or \r\n-terminated line from the front of the string to the given dest s...
Definition dbus-string.c:1971
_dbus_string_zero
void _dbus_string_zero(DBusString *str)
Clears all allocated bytes in the string to zero.
Definition dbus-string.c:2808
_dbus_string_parse_int
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_parse_int(const DBusString *str, int start, long *value_return, int *end_return)
Parses an integer contained in a DBusString.
Definition dbus-sysdeps.c:371
_dbus_string_validate_ascii
dbus_bool_t _dbus_string_validate_ascii(const DBusString *str, int start, int len)
Checks that the given range of the string is valid ASCII with no nul bytes.
Definition dbus-string.c:2573
_dbus_string_append_byte
dbus_bool_t _dbus_string_append_byte(DBusString *str, unsigned char byte)
Appends a single byte to the string, returning FALSE if not enough memory.
Definition dbus-string.c:1190
_dbus_string_hex_encode
dbus_bool_t _dbus_string_hex_encode(const DBusString *source, int start, DBusString *dest, int insert_at)
Encodes a string in hex, the way MD5 and SHA-1 are usually encoded.
Definition dbus-string.c:2382
_dbus_string_append_printf
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
Definition dbus-string.c:1147
_dbus_string_move
dbus_bool_t _dbus_string_move(DBusString *source, int start, DBusString *dest, int insert_at)
Moves the end of one string into another string.
Definition dbus-string.c:1321
_dbus_string_equal
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
Definition dbus-string.c:2075
_dbus_append_keyring_directory_for_credentials
dbus_bool_t _dbus_append_keyring_directory_for_credentials(DBusString *directory, DBusCredentials *credentials)
Appends the directory in which a keyring for the given credentials should be stored.
Definition dbus-sysdeps-unix.c:4721
_dbus_check_setuid
dbus_bool_t _dbus_check_setuid(void)
NOTE: If you modify this function, please also consider making the corresponding change in GLib.
Definition dbus-sysdeps-unix.c:5002
_dbus_sleep_milliseconds
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
Definition dbus-sysdeps-unix.c:3542
_dbus_check_dir_is_private_to_user
dbus_bool_t _dbus_check_dir_is_private_to_user(DBusString *dir, DBusError *error)
Checks to make sure the given directory is private to the user.
Definition dbus-sysdeps-unix.c:2644
_dbus_credentials_add_from_current_process
dbus_bool_t _dbus_credentials_add_from_current_process(DBusCredentials *credentials)
Adds the most important credentials of the current process (the uid and pid) to the passed-in credent...
Definition dbus-sysdeps-unix.c:3005
_dbus_generate_random_bytes
dbus_bool_t _dbus_generate_random_bytes(DBusString *str, int n_bytes, DBusError *error)
Generates the given number of securely random bytes, using the best mechanism we can come up with.
Definition dbus-sysdeps-unix.c:3572
_dbus_concat_dir_and_file
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
Definition dbus-sysdeps-unix.c:3497
_dbus_get_real_time
void _dbus_get_real_time(dbus_int64_t *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
Definition dbus-sysdeps-unix.c:3412
_dbus_ensure_directory
dbus_bool_t _dbus_ensure_directory(const DBusString *filename, DBusError *error)
Creates a directory; succeeds if the directory is created or already existed.
Definition dbus-sysdeps-unix.c:3434
dbus_int64_t
_DBUS_GNUC_EXTENSION typedef long dbus_int64_t
A 64-bit signed integer.
Definition dbus-arch-deps.h:37
dbus_int32_t
int dbus_int32_t
A 32-bit signed integer on all platforms.
Definition dbus-arch-deps.h:44
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
DBUS_INT64_MODIFIER
#define DBUS_INT64_MODIFIER
A string literal for a length modifier that is appropriate to print the dbus_int64_t and dbus_uint64_...
Definition dbus-arch-deps.h:39
DBusCredentials
Definition dbus-credentials.c:60
DBusError
Object representing an exception.
Definition dbus-errors.h:51
DBusError::name
const char * name
public error name field
Definition dbus-errors.h:52
DBusError::message
const char * message
public error message field
Definition dbus-errors.h:53
DBusKey
A single key from the cookie file.
Definition dbus-keyring.c:96
DBusKey::secret
DBusString secret
the actual key
Definition dbus-keyring.c:101
DBusKey::id
dbus_int32_t id
identifier used to refer to the key
Definition dbus-keyring.c:97
DBusKey::creation_time
dbus_int64_t creation_time
when the key was generated, in seconds since 1970-01-01
Definition dbus-keyring.c:99
DBusKeyring
Internals of DBusKeyring.
Definition dbus-keyring.c:112
DBusKeyring::keys
DBusKey * keys
Keys loaded from the file.
Definition dbus-keyring.c:117
DBusKeyring::filename
DBusString filename
Keyring filename.
Definition dbus-keyring.c:115
DBusKeyring::credentials
DBusCredentials * credentials
Credentials containing user the keyring is for.
Definition dbus-keyring.c:119
DBusKeyring::n_keys
int n_keys
Number of keys.
Definition dbus-keyring.c:118
DBusKeyring::refcount
int refcount
Reference count.
Definition dbus-keyring.c:113
DBusKeyring::directory
DBusString directory
Directory the below two items are inside.
Definition dbus-keyring.c:114
DBusKeyring::filename_lock
DBusString filename_lock
Name of lockfile.
Definition dbus-keyring.c:116
DBusString
Definition dbus-string.h:47