1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-keyring.c Store secret cookies in your homedir
4 * Copyright (C) 2003, 2004 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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>
32 * @defgroup DBusKeyring keyring class
33 * @ingroup DBusInternals
34 * @brief DBusKeyring data structure
36 * Types and functions related to DBusKeyring. DBusKeyring is intended
37 * to manage cookies used to authenticate clients to servers. This is
38 * essentially the "verify that client can read the user's homedir"
39 * authentication mechanism. Both client and server must have access
42 * The secret keys are not kept in locked memory, and are written to a
43 * file in the user's homedir. However they are transient (only used
44 * by a single server instance for a fixed period of time, then
45 * discarded). Also, the keys are not sent over the wire.
47 * @todo there's a memory leak on some codepath in here, I saw it once
48 * when running make check - probably some specific initial cookies
49 * present in the cookie file, then depending on what we do with them.
53 * @defgroup DBusKeyringInternals DBusKeyring implementation details
54 * @ingroup DBusInternals
55 * @brief DBusKeyring implementation details
57 * The guts of DBusKeyring.
62 /** The maximum age of a key before we create a new key to use in
63 * challenges. This isn't super-reliably enforced, since system
64 * clocks can change or be wrong, but we make a best effort to only
65 * use keys for a short time.
67 #define NEW_KEY_TIMEOUT_SECONDS (60*5)
69 * The time after which we drop a key from the secrets file.
70 * The EXPIRE_KEYS_TIMEOUT_SECONDS - NEW_KEY_TIMEOUT_SECONDS is the minimum
71 * time window a client has to complete authentication.
73 #define EXPIRE_KEYS_TIMEOUT_SECONDS (NEW_KEY_TIMEOUT_SECONDS + (60*2))
75 * The maximum amount of time a key can be in the future.
77 #define MAX_TIME_TRAVEL_SECONDS (60*5)
80 * Maximum number of keys in the keyring before
81 * we just ignore the rest
83 #ifdef DBUS_BUILD_TESTS
84 #define MAX_KEYS_IN_FILE 10
86 #define MAX_KEYS_IN_FILE 256
90 * A single key from the cookie file
94 dbus_int32_t id; /**< identifier used to refer to the key */
96 long creation_time; /**< when the key was generated,
97 * as unix timestamp. signed long
98 * matches struct timeval.
101 DBusString secret; /**< the actual key */
106 * @brief Internals of DBusKeyring.
108 * DBusKeyring internals. DBusKeyring is an opaque object, it must be
109 * used via accessor functions.
113 int refcount; /**< Reference count */
114 DBusString directory; /**< Directory the below two items are inside */
115 DBusString filename; /**< Keyring filename */
116 DBusString filename_lock; /**< Name of lockfile */
117 DBusKey *keys; /**< Keys loaded from the file */
118 int n_keys; /**< Number of keys */
119 DBusCredentials *credentials; /**< Credentials containing user the keyring is for */
123 _dbus_keyring_new (void)
125 DBusKeyring *keyring;
127 keyring = dbus_new0 (DBusKeyring, 1);
131 if (!_dbus_string_init (&keyring->directory))
134 if (!_dbus_string_init (&keyring->filename))
137 if (!_dbus_string_init (&keyring->filename_lock))
140 keyring->refcount = 1;
141 keyring->keys = NULL;
147 _dbus_string_free (&keyring->filename_lock);
149 _dbus_string_free (&keyring->filename);
151 _dbus_string_free (&keyring->directory);
159 free_keys (DBusKey *keys,
164 /* should be safe for args NULL, 0 */
169 _dbus_string_free (&keys[i].secret);
176 /* Our locking scheme is highly unreliable. However, there is
177 * unfortunately no reliable locking scheme in user home directories;
178 * between bugs in Linux NFS, people using Tru64 or other total crap
179 * NFS, AFS, random-file-system-of-the-week, and so forth, fcntl() in
180 * homedirs simply generates tons of bug reports. This has been
181 * learned through hard experience with GConf, unfortunately.
183 * This bad hack might work better for the kind of lock we have here,
184 * which we don't expect to hold for any length of time. Crashing
185 * while we hold it should be unlikely, and timing out such that we
186 * delete a stale lock should also be unlikely except when the
187 * filesystem is running really slowly. Stuff might break in corner
188 * cases but as long as it's not a security-level breakage it should
192 /** Maximum number of timeouts waiting for lock before we decide it's stale */
193 #define MAX_LOCK_TIMEOUTS 32
194 /** Length of each timeout while waiting for a lock */
195 #define LOCK_TIMEOUT_MILLISECONDS 250
198 _dbus_keyring_lock (DBusKeyring *keyring)
203 while (n_timeouts < MAX_LOCK_TIMEOUTS)
205 DBusError error = DBUS_ERROR_INIT;
207 if (_dbus_create_file_exclusively (&keyring->filename_lock,
211 _dbus_verbose ("Did not get lock file, sleeping %d milliseconds (%s)\n",
212 LOCK_TIMEOUT_MILLISECONDS, error.message);
213 dbus_error_free (&error);
215 _dbus_sleep_milliseconds (LOCK_TIMEOUT_MILLISECONDS);
220 if (n_timeouts == MAX_LOCK_TIMEOUTS)
222 DBusError error = DBUS_ERROR_INIT;
224 _dbus_verbose ("Lock file timed out %d times, assuming stale\n",
227 if (!_dbus_delete_file (&keyring->filename_lock, &error))
229 _dbus_verbose ("Couldn't delete old lock file: %s\n",
231 dbus_error_free (&error);
235 if (!_dbus_create_file_exclusively (&keyring->filename_lock,
238 _dbus_verbose ("Couldn't create lock file after deleting stale one: %s\n",
240 dbus_error_free (&error);
249 _dbus_keyring_unlock (DBusKeyring *keyring)
251 DBusError error = DBUS_ERROR_INIT;
253 if (!_dbus_delete_file (&keyring->filename_lock, &error))
255 _dbus_warn ("Failed to delete lock file: %s\n",
257 dbus_error_free (&error);
262 find_key_by_id (DBusKey *keys,
271 if (keys[i].id == id)
281 add_new_key (DBusKey **keys_p,
289 const unsigned char *s;
294 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
296 if (!_dbus_string_init (&bytes))
298 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
306 /* Generate an integer ID and then the actual key. */
309 if (!_dbus_generate_random_bytes (&bytes, 4))
311 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
315 s = (const unsigned char*) _dbus_string_get_const_data (&bytes);
317 id = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24);
320 _dbus_assert (id >= 0);
322 if (find_key_by_id (keys, n_keys, id) != NULL)
324 _dbus_string_set_length (&bytes, 0);
325 _dbus_verbose ("Key ID %d already existed, trying another one\n",
330 _dbus_verbose ("Creating key with ID %d\n", id);
332 #define KEY_LENGTH_BYTES 24
333 _dbus_string_set_length (&bytes, 0);
334 if (!_dbus_generate_random_bytes (&bytes, KEY_LENGTH_BYTES))
336 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
340 new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
343 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
348 *keys_p = keys; /* otherwise *keys_p ends up invalid */
351 if (!_dbus_string_init (&keys[n_keys-1].secret))
353 n_keys -= 1; /* we don't want to free the one we didn't init */
354 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
358 _dbus_get_current_time (×tamp, NULL);
360 keys[n_keys-1].id = id;
361 keys[n_keys-1].creation_time = timestamp;
362 if (!_dbus_string_move (&bytes, 0,
363 &keys[n_keys-1].secret,
366 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
367 _dbus_string_free (&keys[n_keys-1].secret);
377 _dbus_string_free (&bytes);
382 * Reloads the keyring file, optionally adds one new key to the file,
383 * removes all expired keys from the file iff a key was added, then
384 * resaves the file. Stores the keys from the file in keyring->keys.
385 * Note that the file is only resaved (written to) if a key is added,
386 * this means that only servers ever write to the file and need to
387 * lock it, which avoids a lot of lock contention at login time and
390 * @param keyring the keyring
391 * @param add_new #TRUE to add a new key to the file, expire keys, and resave
392 * @param error return location for errors
393 * @returns #FALSE on failure
396 _dbus_keyring_reload (DBusKeyring *keyring,
403 dbus_bool_t have_lock;
410 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
412 if (!_dbus_check_dir_is_private_to_user (&keyring->directory, error))
415 if (!_dbus_string_init (&contents))
417 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
421 if (!_dbus_string_init (&line))
423 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
424 _dbus_string_free (&contents);
433 _dbus_get_current_time (&now, NULL);
437 if (!_dbus_keyring_lock (keyring))
439 dbus_set_error (error, DBUS_ERROR_FAILED,
440 "Could not lock keyring file to add to it");
447 dbus_error_init (&tmp_error);
448 if (!_dbus_file_get_contents (&contents,
452 _dbus_verbose ("Failed to load keyring file: %s\n",
454 /* continue with empty keyring file, so we recreate it */
455 dbus_error_free (&tmp_error);
458 if (!_dbus_string_validate_ascii (&contents, 0,
459 _dbus_string_get_length (&contents)))
461 _dbus_warn ("Secret keyring file contains non-ASCII! Ignoring existing contents\n");
462 _dbus_string_set_length (&contents, 0);
465 /* FIXME this is badly inefficient for large keyring files
466 * (not that large keyring files exist outside of test suites)
468 while (_dbus_string_pop_line (&contents, &line))
478 /* Don't load more than the max. */
479 if (n_keys >= (add_new ? MAX_KEYS_IN_FILE - 1 : MAX_KEYS_IN_FILE))
483 if (!_dbus_string_parse_int (&line, 0, &val, &next))
485 _dbus_verbose ("could not parse secret key ID at start of line\n");
489 if (val > _DBUS_INT32_MAX || val < 0)
491 _dbus_verbose ("invalid secret key ID at start of line\n");
497 _dbus_string_skip_blank (&line, next, &next);
499 if (!_dbus_string_parse_int (&line, next, ×tamp, &next))
501 _dbus_verbose ("could not parse secret key timestamp\n");
506 (now + MAX_TIME_TRAVEL_SECONDS) < timestamp ||
507 (now - EXPIRE_KEYS_TIMEOUT_SECONDS) > timestamp)
509 _dbus_verbose ("dropping/ignoring %ld-seconds old key with timestamp %ld as current time is %ld\n",
510 now - timestamp, timestamp, now);
514 _dbus_string_skip_blank (&line, next, &next);
516 len = _dbus_string_get_length (&line);
518 if ((len - next) == 0)
520 _dbus_verbose ("no secret key after ID and timestamp\n");
524 /* We have all three parts */
525 new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
528 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
535 if (!_dbus_string_init (&keys[n_keys-1].secret))
537 n_keys -= 1; /* we don't want to free the one we didn't init */
538 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
542 keys[n_keys-1].id = id;
543 keys[n_keys-1].creation_time = timestamp;
544 if (!_dbus_string_hex_decode (&line, next, &end,
545 &keys[n_keys-1].secret, 0))
547 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
551 if (_dbus_string_get_length (&line) != end)
553 _dbus_verbose ("invalid hex encoding in keyring file\n");
554 _dbus_string_free (&keys[n_keys - 1].secret);
560 _dbus_verbose ("Successfully loaded %d existing keys\n",
565 if (!add_new_key (&keys, &n_keys, error))
567 _dbus_verbose ("Failed to generate new key: %s\n",
568 error ? error->message : "(unknown)");
572 _dbus_string_set_length (&contents, 0);
577 if (!_dbus_string_append_int (&contents,
581 if (!_dbus_string_append_byte (&contents, ' '))
584 if (!_dbus_string_append_int (&contents,
585 keys[i].creation_time))
588 if (!_dbus_string_append_byte (&contents, ' '))
591 if (!_dbus_string_hex_encode (&keys[i].secret, 0,
593 _dbus_string_get_length (&contents)))
596 if (!_dbus_string_append_byte (&contents, '\n'))
603 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
607 if (!_dbus_string_save_to_file (&contents, &keyring->filename,
613 free_keys (keyring->keys, keyring->n_keys);
614 keyring->keys = keys;
615 keyring->n_keys = n_keys;
623 _dbus_keyring_unlock (keyring);
625 if (! ((retval == TRUE && (error == NULL || error->name == NULL)) ||
626 (retval == FALSE && (error == NULL || error->name != NULL))))
628 if (error && error->name)
629 _dbus_verbose ("error is %s: %s\n", error->name, error->message);
630 _dbus_warn ("returning %d but error pointer %p name %s\n",
631 retval, error, error->name ? error->name : "(none)");
632 _dbus_assert_not_reached ("didn't handle errors properly");
640 _dbus_string_zero (&keys[i].secret);
641 _dbus_string_free (&keys[i].secret);
648 _dbus_string_free (&contents);
649 _dbus_string_free (&line);
654 /** @} */ /* end of internals */
657 * @addtogroup DBusKeyring
663 * Increments reference count of the keyring
665 * @param keyring the keyring
666 * @returns the keyring
669 _dbus_keyring_ref (DBusKeyring *keyring)
671 keyring->refcount += 1;
677 * Decrements refcount and finalizes if it reaches
680 * @param keyring the keyring
683 _dbus_keyring_unref (DBusKeyring *keyring)
685 keyring->refcount -= 1;
687 if (keyring->refcount == 0)
689 if (keyring->credentials)
690 _dbus_credentials_unref (keyring->credentials);
692 _dbus_string_free (&keyring->filename);
693 _dbus_string_free (&keyring->filename_lock);
694 _dbus_string_free (&keyring->directory);
695 free_keys (keyring->keys, keyring->n_keys);
701 * Creates a new keyring that lives in the ~/.dbus-keyrings directory
702 * of the given user credentials. If the credentials are #NULL or
703 * empty, uses those of the current process.
705 * @param username username to get keyring for, or #NULL
706 * @param context which keyring to get
707 * @param error return location for errors
708 * @returns the keyring or #NULL on error
711 _dbus_keyring_new_for_credentials (DBusCredentials *credentials,
712 const DBusString *context,
716 DBusKeyring *keyring;
717 dbus_bool_t error_set;
719 DBusCredentials *our_credentials;
721 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
725 our_credentials = NULL;
727 if (!_dbus_string_init (&ringdir))
729 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
733 if (credentials != NULL)
735 our_credentials = _dbus_credentials_copy (credentials);
739 our_credentials = _dbus_credentials_new_from_current_process ();
742 if (our_credentials == NULL)
745 if (_dbus_credentials_are_anonymous (our_credentials))
747 if (!_dbus_credentials_add_from_current_process (our_credentials))
751 if (!_dbus_append_keyring_directory_for_credentials (&ringdir,
755 keyring = _dbus_keyring_new ();
759 _dbus_assert (keyring->credentials == NULL);
760 keyring->credentials = our_credentials;
761 our_credentials = NULL; /* so we don't unref it again later */
763 /* should have been validated already, but paranoia check here */
764 if (!_dbus_keyring_validate_context (context))
767 dbus_set_error_const (error,
769 "Invalid context in keyring creation");
773 /* Save keyring dir in the keyring object */
774 if (!_dbus_string_copy (&ringdir, 0,
775 &keyring->directory, 0))
778 /* Create keyring->filename based on keyring dir and context */
779 if (!_dbus_string_copy (&keyring->directory, 0,
780 &keyring->filename, 0))
783 if (!_dbus_concat_dir_and_file (&keyring->filename,
787 /* Create lockfile name */
788 if (!_dbus_string_copy (&keyring->filename, 0,
789 &keyring->filename_lock, 0))
792 if (!_dbus_string_append (&keyring->filename_lock, ".lock"))
796 dbus_error_init (&tmp_error);
797 if (!_dbus_keyring_reload (keyring, FALSE, &tmp_error))
799 _dbus_verbose ("didn't load an existing keyring: %s\n",
801 dbus_error_free (&tmp_error);
804 /* We don't fail fatally if we can't create the directory,
805 * but the keyring will probably always be empty
806 * unless someone else manages to create it
808 dbus_error_init (&tmp_error);
809 if (!_dbus_create_directory (&keyring->directory,
812 _dbus_verbose ("Creating keyring directory: %s\n",
814 dbus_error_free (&tmp_error);
817 _dbus_string_free (&ringdir);
823 dbus_set_error_const (error,
824 DBUS_ERROR_NO_MEMORY,
827 _dbus_credentials_unref (our_credentials);
829 _dbus_keyring_unref (keyring);
830 _dbus_string_free (&ringdir);
836 * Checks whether the context is a valid context.
837 * Contexts that might cause confusion when used
838 * in filenames are not allowed (contexts can't
839 * start with a dot or contain dir separators).
841 * @todo this is the most inefficient implementation
844 * @param context the context
845 * @returns #TRUE if valid
848 _dbus_keyring_validate_context (const DBusString *context)
850 if (_dbus_string_get_length (context) == 0)
852 _dbus_verbose ("context is zero-length\n");
856 if (!_dbus_string_validate_ascii (context, 0,
857 _dbus_string_get_length (context)))
859 _dbus_verbose ("context not valid ascii\n");
863 /* no directory separators */
864 if (_dbus_string_find (context, 0, "/", NULL))
866 _dbus_verbose ("context contains a slash\n");
870 if (_dbus_string_find (context, 0, "\\", NULL))
872 _dbus_verbose ("context contains a backslash\n");
876 /* prevent attempts to use dotfiles or ".." or ".lock"
877 * all of which might allow some kind of attack
879 if (_dbus_string_find (context, 0, ".", NULL))
881 _dbus_verbose ("context contains a dot\n");
885 /* no spaces/tabs, those are used for separators in the protocol */
886 if (_dbus_string_find_blank (context, 0, NULL))
888 _dbus_verbose ("context contains a blank\n");
892 if (_dbus_string_find (context, 0, "\n", NULL))
894 _dbus_verbose ("context contains a newline\n");
898 if (_dbus_string_find (context, 0, "\r", NULL))
900 _dbus_verbose ("context contains a carriage return\n");
908 find_recent_key (DBusKeyring *keyring)
911 long tv_sec, tv_usec;
913 _dbus_get_current_time (&tv_sec, &tv_usec);
916 while (i < keyring->n_keys)
918 DBusKey *key = &keyring->keys[i];
920 _dbus_verbose ("Key %d is %ld seconds old\n",
921 i, tv_sec - key->creation_time);
923 if ((tv_sec - NEW_KEY_TIMEOUT_SECONDS) < key->creation_time)
933 * Gets a recent key to use for authentication.
934 * If no recent key exists, creates one. Returns
935 * the key ID. If a key can't be written to the keyring
936 * file so no recent key can be created, returns -1.
937 * All valid keys are > 0.
939 * @param keyring the keyring
940 * @param error error on failure
941 * @returns key ID to use for auth, or -1 on failure
944 _dbus_keyring_get_best_key (DBusKeyring *keyring,
949 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
951 key = find_recent_key (keyring);
955 /* All our keys are too old, or we've never loaded the
956 * keyring. Create a new one.
958 if (!_dbus_keyring_reload (keyring, TRUE,
962 key = find_recent_key (keyring);
967 dbus_set_error_const (error,
969 "No recent-enough key found in keyring, and unable to create a new key");
975 * Checks whether the keyring is for the same user as the given credentials.
977 * @param keyring the keyring
978 * @param credentials the credentials to check
980 * @returns #TRUE if the keyring belongs to the given user
983 _dbus_keyring_is_for_credentials (DBusKeyring *keyring,
984 DBusCredentials *credentials)
986 return _dbus_credentials_same_user (keyring->credentials,
991 * Gets the hex-encoded secret key for the given ID.
992 * Returns #FALSE if not enough memory. Returns #TRUE
993 * but empty key on any other error such as unknown
996 * @param keyring the keyring
997 * @param key_id the key ID
998 * @param hex_key string to append hex-encoded key to
999 * @returns #TRUE if we had enough memory
1002 _dbus_keyring_get_hex_key (DBusKeyring *keyring,
1004 DBusString *hex_key)
1008 key = find_key_by_id (keyring->keys,
1012 return TRUE; /* had enough memory, so TRUE */
1014 return _dbus_string_hex_encode (&key->secret, 0,
1016 _dbus_string_get_length (hex_key));
1019 /** @} */ /* end of exposed API */
1021 #ifdef DBUS_BUILD_TESTS
1022 #include "dbus-test.h"
1026 _dbus_keyring_test (void)
1038 /* Context validation */
1040 _dbus_string_init_const (&context, "foo");
1041 _dbus_assert (_dbus_keyring_validate_context (&context));
1042 _dbus_string_init_const (&context, "org_freedesktop_blah");
1043 _dbus_assert (_dbus_keyring_validate_context (&context));
1045 _dbus_string_init_const (&context, "");
1046 _dbus_assert (!_dbus_keyring_validate_context (&context));
1047 _dbus_string_init_const (&context, ".foo");
1048 _dbus_assert (!_dbus_keyring_validate_context (&context));
1049 _dbus_string_init_const (&context, "bar.foo");
1050 _dbus_assert (!_dbus_keyring_validate_context (&context));
1051 _dbus_string_init_const (&context, "bar/foo");
1052 _dbus_assert (!_dbus_keyring_validate_context (&context));
1053 _dbus_string_init_const (&context, "bar\\foo");
1054 _dbus_assert (!_dbus_keyring_validate_context (&context));
1055 _dbus_string_init_const (&context, "foo\xfa\xf0");
1056 _dbus_assert (!_dbus_keyring_validate_context (&context));
1057 _dbus_string_init_const (&context, "foo\x80");
1058 _dbus_assert (!_dbus_keyring_validate_context (&context));
1059 _dbus_string_init_const (&context, "foo\x7f");
1060 _dbus_assert (_dbus_keyring_validate_context (&context));
1061 _dbus_string_init_const (&context, "foo bar");
1062 _dbus_assert (!_dbus_keyring_validate_context (&context));
1064 if (!_dbus_string_init (&context))
1065 _dbus_assert_not_reached ("no memory");
1066 if (!_dbus_string_append_byte (&context, '\0'))
1067 _dbus_assert_not_reached ("no memory");
1068 _dbus_assert (!_dbus_keyring_validate_context (&context));
1069 _dbus_string_free (&context);
1071 /* Now verify that if we create a key in keyring 1,
1072 * it is properly loaded in keyring 2
1075 _dbus_string_init_const (&context, "org_freedesktop_dbus_testsuite");
1076 dbus_error_init (&error);
1077 ring1 = _dbus_keyring_new_for_credentials (NULL, &context,
1079 _dbus_assert (ring1 != NULL);
1080 _dbus_assert (error.name == NULL);
1082 id = _dbus_keyring_get_best_key (ring1, &error);
1085 fprintf (stderr, "Could not load keyring: %s\n", error.message);
1086 dbus_error_free (&error);
1090 ring2 = _dbus_keyring_new_for_credentials (NULL, &context, &error);
1091 _dbus_assert (ring2 != NULL);
1092 _dbus_assert (error.name == NULL);
1094 if (ring1->n_keys != ring2->n_keys)
1096 fprintf (stderr, "Different number of keys in keyrings\n");
1100 /* We guarantee we load and save keeping keys in a fixed
1104 while (i < ring1->n_keys)
1106 if (ring1->keys[i].id != ring2->keys[i].id)
1108 fprintf (stderr, "Keyring 1 has first key ID %d and keyring 2 has %d\n",
1109 ring1->keys[i].id, ring2->keys[i].id);
1113 if (ring1->keys[i].creation_time != ring2->keys[i].creation_time)
1115 fprintf (stderr, "Keyring 1 has first key time %ld and keyring 2 has %ld\n",
1116 ring1->keys[i].creation_time, ring2->keys[i].creation_time);
1120 if (!_dbus_string_equal (&ring1->keys[i].secret,
1121 &ring2->keys[i].secret))
1123 fprintf (stderr, "Keyrings 1 and 2 have different secrets for same ID/timestamp\n");
1130 printf (" %d keys in test\n", ring1->n_keys);
1132 /* Test ref/unref */
1133 _dbus_keyring_ref (ring1);
1134 _dbus_keyring_ref (ring2);
1135 _dbus_keyring_unref (ring1);
1136 _dbus_keyring_unref (ring2);
1140 _dbus_keyring_unref (ring1);
1141 _dbus_keyring_unref (ring2);
1147 _dbus_keyring_unref (ring1);
1149 _dbus_keyring_unref (ring2);
1154 #endif /* DBUS_BUILD_TESTS */