25#include "dbus-keyring.h"
26#include "dbus-protocol.h"
27#include <dbus/dbus-string.h>
28#include <dbus/dbus-list.h>
29#include <dbus/dbus-sysdeps.h>
30#include <dbus/dbus-test-tap.h>
68#define NEW_KEY_TIMEOUT_SECONDS (60*5)
74#define EXPIRE_KEYS_TIMEOUT_SECONDS (NEW_KEY_TIMEOUT_SECONDS + (60*2))
78#define MAX_TIME_TRAVEL_SECONDS (60*5)
84#ifdef DBUS_ENABLE_EMBEDDED_TESTS
85#define MAX_KEYS_IN_FILE 10
87#define MAX_KEYS_IN_FILE 256
124_dbus_keyring_new (
void)
192#define MAX_LOCK_TIMEOUTS 32
194#define LOCK_TIMEOUT_MILLISECONDS 250
210 _dbus_verbose (
"Did not get lock file, sleeping %d milliseconds (%s)\n",
223 _dbus_verbose (
"Lock file timed out %d times, assuming stale\n",
228 _dbus_verbose (
"Couldn't delete old lock file: %s\n",
237 _dbus_verbose (
"Couldn't create lock file after deleting stale one: %s\n",
270 if (keys[i].
id ==
id)
288 const unsigned char *s;
293 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
311 s = (
const unsigned char*) _dbus_string_get_const_data (&bytes);
313 id = s[0] | (s[1] << 8) | (s[2] << 16) | ((s[3] & 0x7f) << 24);
316 if (find_key_by_id (keys, n_keys,
id) !=
NULL)
319 _dbus_verbose (
"Key ID %d already existed, trying another one\n",
324 _dbus_verbose (
"Creating key with ID %d\n",
id);
326#define KEY_LENGTH_BYTES 24
353 keys[n_keys-1].
id = id;
356 &keys[n_keys-1].secret,
403 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
430 if (!_dbus_keyring_lock (keyring))
433 "Could not lock keyring file to add to it");
445 _dbus_verbose (
"Failed to load keyring file: %s\n",
452 _dbus_string_get_length (&contents)))
454 _dbus_warn (
"Secret keyring file contains non-ASCII! Ignoring existing contents");
478 _dbus_verbose (
"could not parse secret key ID at start of line\n");
484 _dbus_verbose (
"invalid secret key ID at start of line\n");
494 _dbus_verbose (
"could not parse secret key timestamp\n");
502 _dbus_verbose (
"dropping/ignoring %ld-seconds old key with timestamp %ld as current time is %ld\n",
503 now - timestamp, timestamp, now);
509 len = _dbus_string_get_length (&line);
511 if ((len - next) == 0)
513 _dbus_verbose (
"no secret key after ID and timestamp\n");
535 keys[n_keys-1].
id = id;
538 &keys[n_keys-1].secret, 0))
544 if (_dbus_string_get_length (&line) != end)
546 _dbus_verbose (
"invalid hex encoding in keyring file\n");
553 _dbus_verbose (
"Successfully loaded %d existing keys\n",
558 if (!add_new_key (&keys, &n_keys, error))
560 _dbus_verbose (
"Failed to generate new key: %s\n",
561 error ? error->
message :
"(unknown)");
578 keys[i].creation_time))
586 _dbus_string_get_length (&contents)))
607 keyring->
keys = keys;
616 _dbus_keyring_unlock (keyring);
621 if (error && error->
name)
622 _dbus_verbose (
"error is %s: %s\n", error->
name, error->
message);
623 _dbus_warn (
"returning %d but error pointer %p name %s",
624 retval, error, error->
name ? error->
name :
"(none)");
714 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
719 "Unable to create DBus keyring when setuid");
725 our_credentials =
NULL;
733 if (credentials !=
NULL)
742 if (our_credentials ==
NULL)
755 keyring = _dbus_keyring_new ();
761 our_credentials =
NULL;
769 "Invalid context in keyring creation");
797 if (!_dbus_keyring_reload (keyring,
FALSE, &tmp_error))
799 _dbus_verbose (
"didn't load an existing keyring: %s\n",
812 _dbus_verbose (
"Creating keyring directory: %s\n",
850 if (_dbus_string_get_length (context) == 0)
852 _dbus_verbose (
"context is zero-length\n");
857 _dbus_string_get_length (context)))
859 _dbus_verbose (
"context not valid ascii\n");
866 _dbus_verbose (
"context contains a slash\n");
872 _dbus_verbose (
"context contains a backslash\n");
881 _dbus_verbose (
"context contains a dot\n");
888 _dbus_verbose (
"context contains a blank\n");
894 _dbus_verbose (
"context contains a newline\n");
900 _dbus_verbose (
"context contains a carriage return\n");
911 long tv_sec, tv_usec;
916 while (i < keyring->n_keys)
920 _dbus_verbose (
"Key %d is %ld seconds old\n",
949 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
951 key = find_recent_key (keyring);
958 if (!_dbus_keyring_reload (keyring,
TRUE,
962 key = find_recent_key (keyring);
969 "No recent-enough key found in keyring, and unable to create a new key");
1008 key = find_key_by_id (keyring->
keys,
1016 _dbus_string_get_length (hex_key));
1021#ifdef DBUS_ENABLE_EMBEDDED_TESTS
1022#include "dbus-test.h"
1026_dbus_keyring_test (
const char *test_data_dir _DBUS_GNUC_UNUSED)
1065 _dbus_test_fatal (
"no memory");
1067 _dbus_test_fatal (
"no memory");
1085 fprintf (stderr,
"Could not load keyring: %s\n", error.
message);
1096 fprintf (stderr,
"Different number of keys in keyrings\n");
1104 while (i < ring1->n_keys)
1108 fprintf (stderr,
"Keyring 1 has first key ID %d and keyring 2 has %d\n",
1115 fprintf (stderr,
"Keyring 1 has first key time %ld and keyring 2 has %ld\n",
1123 fprintf (stderr,
"Keyrings 1 and 2 have different secrets for same ID/timestamp\n");
1130 _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_append_int(DBusString *str, long value)
Appends an integer to 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_I...
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_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.
void _dbus_get_real_time(long *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
dbus_bool_t _dbus_ensure_directory(const DBusString *filename, DBusError *error)
Creates a directory; succeeds if the directory is created or already existed.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
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
long creation_time
when the key was generated, as unix timestamp.
dbus_int32_t id
identifier used to refer to the key
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.