27#include "dbus-keyring.h"
28#include "dbus-protocol.h"
29#include <dbus/dbus-string.h>
30#include <dbus/dbus-list.h>
31#include <dbus/dbus-sysdeps.h>
32#include <dbus/dbus-test-tap.h>
70#define NEW_KEY_TIMEOUT_SECONDS (60*5)
76#define EXPIRE_KEYS_TIMEOUT_SECONDS (NEW_KEY_TIMEOUT_SECONDS + (60*2))
80#define MAX_TIME_TRAVEL_SECONDS (60*5)
86#ifdef DBUS_ENABLE_EMBEDDED_TESTS
87#define MAX_KEYS_IN_FILE 10
89#define MAX_KEYS_IN_FILE 256
123_dbus_keyring_new (
void)
191#define MAX_LOCK_TIMEOUTS 32
193#define LOCK_TIMEOUT_MILLISECONDS 250
209 _dbus_verbose (
"Did not get lock file, sleeping %d milliseconds (%s)\n",
222 _dbus_verbose (
"Lock file timed out %d times, assuming stale\n",
227 _dbus_verbose (
"Couldn't delete old lock file: %s\n",
236 _dbus_verbose (
"Couldn't create lock file after deleting stale one: %s\n",
269 if (keys[i].
id ==
id)
286 dbus_int64_t timestamp;
287 const unsigned char *s;
292 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
310 s = (
const unsigned char*) _dbus_string_get_const_data (&bytes);
312 id = s[0] | (s[1] << 8) | (s[2] << 16) | ((s[3] & 0x7f) << 24);
315 if (find_key_by_id (keys, n_keys,
id) !=
NULL)
318 _dbus_verbose (
"Key ID %d already existed, trying another one\n",
323 _dbus_verbose (
"Creating key with ID %d\n",
id);
325#define KEY_LENGTH_BYTES 24
352 keys[n_keys-1].
id = id;
355 &keys[n_keys-1].secret,
395 dbus_bool_t have_lock;
402 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
429 if (!_dbus_keyring_lock (keyring))
432 "Could not lock keyring file to add to it");
444 _dbus_verbose (
"Failed to load keyring file: %s\n",
451 _dbus_string_get_length (&contents)))
453 _dbus_warn (
"Secret keyring file contains non-ASCII! Ignoring existing contents");
465 dbus_int64_t timestamp;
477 _dbus_verbose (
"could not parse secret key ID at start of line\n");
483 _dbus_verbose (
"invalid secret key ID at start of line\n");
493 _dbus_verbose (
"could not parse secret key timestamp\n");
502 now - timestamp, timestamp, now);
508 len = _dbus_string_get_length (&line);
510 if ((len - next) == 0)
512 _dbus_verbose (
"no secret key after ID and timestamp\n");
534 keys[n_keys-1].
id = id;
537 &keys[n_keys-1].secret, 0))
543 if (_dbus_string_get_length (&line) != end)
545 _dbus_verbose (
"invalid hex encoding in keyring file\n");
552 _dbus_verbose (
"Successfully loaded %d existing keys\n",
557 if (!add_new_key (&keys, &n_keys, error))
559 _dbus_verbose (
"Failed to generate new key: %s\n",
560 error ? error->
message :
"(unknown)");
570 keys[i].
id, keys[i].creation_time))
575 _dbus_string_get_length (&contents)))
596 keyring->
keys = keys;
605 _dbus_keyring_unlock (keyring);
610 if (error && error->
name)
611 _dbus_verbose (
"error is %s: %s\n", error->
name, error->
message);
612 _dbus_warn (
"returning %d but error pointer %p name %s",
613 retval, error, error->
name ? error->
name :
"(none)");
699 dbus_bool_t error_set;
703 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
708 "Unable to create DBus keyring when setuid");
714 our_credentials =
NULL;
722 if (credentials !=
NULL)
731 if (our_credentials ==
NULL)
744 keyring = _dbus_keyring_new ();
750 our_credentials =
NULL;
758 "Invalid context in keyring creation");
786 if (!_dbus_keyring_reload (keyring,
FALSE, &tmp_error))
788 _dbus_verbose (
"didn't load an existing keyring: %s\n",
801 _dbus_verbose (
"Creating keyring directory: %s\n",
839 if (_dbus_string_get_length (context) == 0)
841 _dbus_verbose (
"context is zero-length\n");
846 _dbus_string_get_length (context)))
848 _dbus_verbose (
"context not valid ascii\n");
855 _dbus_verbose (
"context contains a slash\n");
861 _dbus_verbose (
"context contains a backslash\n");
870 _dbus_verbose (
"context contains a dot\n");
877 _dbus_verbose (
"context contains a blank\n");
883 _dbus_verbose (
"context contains a newline\n");
889 _dbus_verbose (
"context contains a carriage return\n");
906 while (i < keyring->n_keys)
939 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
941 key = find_recent_key (keyring);
948 if (!_dbus_keyring_reload (keyring,
TRUE,
952 key = find_recent_key (keyring);
959 "No recent-enough key found in keyring, and unable to create a new key");
998 key = find_key_by_id (keyring->
keys,
1006 _dbus_string_get_length (hex_key));
1011#ifdef DBUS_ENABLE_EMBEDDED_TESTS
1012#include "dbus-test.h"
1016_dbus_keyring_test (
const char *test_data_dir _DBUS_GNUC_UNUSED)
1055 _dbus_test_fatal (
"no memory");
1057 _dbus_test_fatal (
"no memory");
1075 fprintf (stderr,
"Could not load keyring: %s\n", error.
message);
1086 fprintf (stderr,
"Different number of keys in keyrings\n");
1094 while (i < ring1->n_keys)
1098 fprintf (stderr,
"Keyring 1 has first key ID %d and keyring 2 has %d\n",
1113 fprintf (stderr,
"Keyrings 1 and 2 have different secrets for same ID/timestamp\n");
1120 _dbus_test_diag (
" %d keys in test", ring1->
n_keys);
dbus_bool_t _dbus_credentials_same_user(DBusCredentials *credentials, DBusCredentials *other_credentials)
Check whether the user-identifying credentials in two credentials objects are identical.
DBusCredentials * _dbus_credentials_copy(DBusCredentials *credentials)
Copy a credentials object.
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...
void _dbus_credentials_unref(DBusCredentials *credentials)
Decrement refcount on credentials.
dbus_bool_t _dbus_credentials_are_anonymous(DBusCredentials *credentials)
Checks whether a credentials object contains a user identity.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
void dbus_set_error_const(DBusError *error, const char *name, const char *message)
Assigns an error name and message to a DBusError.
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
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...
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
dbus_bool_t _dbus_create_file_exclusively(const DBusString *filename, DBusError *error)
Creates the given file, failing if the file already exists.
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.
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.
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
#define _DBUS_INT32_MAX
Maximum value of type "int32".
#define LOCK_TIMEOUT_MILLISECONDS
Length of each timeout while waiting for a lock.
#define MAX_TIME_TRAVEL_SECONDS
The maximum amount of time a key can be in the future.
#define NEW_KEY_TIMEOUT_SECONDS
The maximum age of a key before we create a new key to use in challenges.
#define MAX_KEYS_IN_FILE
Maximum number of keys in the keyring before we just ignore the rest.
#define MAX_LOCK_TIMEOUTS
Maximum number of timeouts waiting for lock before we decide it's stale.
#define EXPIRE_KEYS_TIMEOUT_SECONDS
The time after which we drop a key from the secrets file.
int _dbus_keyring_get_best_key(DBusKeyring *keyring, DBusError *error)
Gets a recent key to use for authentication.
dbus_bool_t _dbus_keyring_validate_context(const DBusString *context)
Checks whether the context is a valid context.
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.
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...
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.
DBusKeyring * _dbus_keyring_ref(DBusKeyring *keyring)
Increments reference count of the keyring.
void _dbus_keyring_unref(DBusKeyring *keyring)
Decrements refcount and finalizes if it reaches zero.
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
void * dbus_realloc(void *memory, size_t bytes)
Resizes a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
#define DBUS_ERROR_NOT_SUPPORTED
Requested operation isn't supported (like ENOSYS on UNIX).
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
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.
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
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...
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.
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).
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...
dbus_bool_t _dbus_string_find_blank(const DBusString *str, int start, int *found)
Finds a blank (space or tab) in the string.
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...
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...
void _dbus_string_zero(DBusString *str)
Clears all allocated bytes in the string to zero.
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.
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.
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.
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.
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
dbus_bool_t _dbus_string_move(DBusString *source, int start, DBusString *dest, int insert_at)
Moves the end of one string into another string.
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
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.
dbus_bool_t _dbus_check_setuid(void)
NOTE: If you modify this function, please also consider making the corresponding change in GLib.
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
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.
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...
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.
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
void _dbus_get_real_time(dbus_int64_t *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
dbus_bool_t _dbus_ensure_directory(const DBusString *filename, DBusError *error)
Creates a directory; succeeds if the directory is created or already existed.
#define DBUS_INT64_MODIFIER
A string literal for a length modifier that is appropriate to print the dbus_int64_t and dbus_uint64_...
Object representing an exception.
const char * name
public error name field
const char * message
public error message field
A single key from the cookie file.
DBusString secret
the actual key
dbus_int32_t id
identifier used to refer to the key
dbus_int64_t creation_time
when the key was generated, in seconds since 1970-01-01
Internals of DBusKeyring.
DBusKey * keys
Keys loaded from the file.
DBusString filename
Keyring filename.
DBusCredentials * credentials
Credentials containing user the keyring is for.
int n_keys
Number of keys.
int refcount
Reference count.
DBusString directory
Directory the below two items are inside.
DBusString filename_lock
Name of lockfile.