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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-keyring.h"
25 #include "dbus-protocol.h"
26 #include <dbus/dbus-string.h>
27 #include <dbus/dbus-list.h>
28 #include <dbus/dbus-sysdeps.h>
31 * @defgroup DBusKeyring keyring class
32 * @ingroup DBusInternals
33 * @brief DBusKeyring data structure
35 * Types and functions related to DBusKeyring. DBusKeyring is intended
36 * to manage cookies used to authenticate clients to servers. This is
37 * essentially the "verify that client can read the user's homedir"
38 * authentication mechanism. Both client and server must have access
41 * The secret keys are not kept in locked memory, and are written to a
42 * file in the user's homedir. However they are transient (only used
43 * by a single server instance for a fixed period of time, then
44 * discarded). Also, the keys are not sent over the wire.
46 * @todo there's a memory leak on some codepath in here, I saw it once
47 * when running make check - probably some specific initial cookies
48 * present in the cookie file, then depending on what we do with them.
52 * @defgroup DBusKeyringInternals DBusKeyring implementation details
53 * @ingroup DBusInternals
54 * @brief DBusKeyring implementation details
56 * The guts of DBusKeyring.
61 /** The maximum age of a key before we create a new key to use in
62 * challenges. This isn't super-reliably enforced, since system
63 * clocks can change or be wrong, but we make a best effort to only
64 * use keys for a short time.
66 #define NEW_KEY_TIMEOUT_SECONDS (60*5)
68 * The time after which we drop a key from the secrets file.
69 * The EXPIRE_KEYS_TIMEOUT_SECONDS - NEW_KEY_TIMEOUT_SECONDS is the minimum
70 * time window a client has to complete authentication.
72 #define EXPIRE_KEYS_TIMEOUT_SECONDS (NEW_KEY_TIMEOUT_SECONDS + (60*2))
74 * The maximum amount of time a key can be in the future.
76 #define MAX_TIME_TRAVEL_SECONDS (60*5)
79 * Maximum number of keys in the keyring before
80 * we just ignore the rest
82 #ifdef DBUS_BUILD_TESTS
83 #define MAX_KEYS_IN_FILE 10
85 #define MAX_KEYS_IN_FILE 256
89 * A single key from the cookie file
93 dbus_int32_t id; /**< identifier used to refer to the key */
95 long creation_time; /**< when the key was generated,
96 * as unix timestamp. signed long
97 * matches struct timeval.
100 DBusString secret; /**< the actual key */
105 * @brief Internals of DBusKeyring.
107 * DBusKeyring internals. DBusKeyring is an opaque object, it must be
108 * used via accessor functions.
112 int refcount; /**< Reference count */
113 DBusString directory; /**< Directory the below two items are inside */
114 DBusString filename; /**< Keyring filename */
115 DBusString filename_lock; /**< Name of lockfile */
116 DBusKey *keys; /**< Keys loaded from the file */
117 int n_keys; /**< Number of keys */
118 DBusCredentials *credentials; /**< Credentials containing user the keyring is for */
122 _dbus_keyring_new (void)
124 DBusKeyring *keyring;
126 keyring = dbus_new0 (DBusKeyring, 1);
130 if (!_dbus_string_init (&keyring->directory))
133 if (!_dbus_string_init (&keyring->filename))
136 if (!_dbus_string_init (&keyring->filename_lock))
139 keyring->refcount = 1;
140 keyring->keys = NULL;
146 _dbus_string_free (&keyring->filename_lock);
148 _dbus_string_free (&keyring->filename);
150 _dbus_string_free (&keyring->directory);
158 free_keys (DBusKey *keys,
163 /* should be safe for args NULL, 0 */
168 _dbus_string_free (&keys[i].secret);
175 /* Our locking scheme is highly unreliable. However, there is
176 * unfortunately no reliable locking scheme in user home directories;
177 * between bugs in Linux NFS, people using Tru64 or other total crap
178 * NFS, AFS, random-file-system-of-the-week, and so forth, fcntl() in
179 * homedirs simply generates tons of bug reports. This has been
180 * learned through hard experience with GConf, unfortunately.
182 * This bad hack might work better for the kind of lock we have here,
183 * which we don't expect to hold for any length of time. Crashing
184 * while we hold it should be unlikely, and timing out such that we
185 * delete a stale lock should also be unlikely except when the
186 * filesystem is running really slowly. Stuff might break in corner
187 * cases but as long as it's not a security-level breakage it should
191 /** Maximum number of timeouts waiting for lock before we decide it's stale */
192 #define MAX_LOCK_TIMEOUTS 32
193 /** Length of each timeout while waiting for a lock */
194 #define LOCK_TIMEOUT_MILLISECONDS 250
197 _dbus_keyring_lock (DBusKeyring *keyring)
202 while (n_timeouts < MAX_LOCK_TIMEOUTS)
206 dbus_error_init (&error);
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)
224 _dbus_verbose ("Lock file timed out %d times, assuming stale\n",
227 dbus_error_init (&error);
229 if (!_dbus_delete_file (&keyring->filename_lock, &error))
231 _dbus_verbose ("Couldn't delete old lock file: %s\n",
233 dbus_error_free (&error);
237 if (!_dbus_create_file_exclusively (&keyring->filename_lock,
240 _dbus_verbose ("Couldn't create lock file after deleting stale one: %s\n",
242 dbus_error_free (&error);
251 _dbus_keyring_unlock (DBusKeyring *keyring)
254 dbus_error_init (&error);
255 if (!_dbus_delete_file (&keyring->filename_lock, &error))
257 _dbus_warn ("Failed to delete lock file: %s\n",
259 dbus_error_free (&error);
264 find_key_by_id (DBusKey *keys,
273 if (keys[i].id == id)
283 add_new_key (DBusKey **keys_p,
291 const unsigned char *s;
296 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
298 if (!_dbus_string_init (&bytes))
300 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
308 /* Generate an integer ID and then the actual key. */
311 if (!_dbus_generate_random_bytes (&bytes, 4))
313 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
317 s = (const unsigned char*) _dbus_string_get_const_data (&bytes);
319 id = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24);
322 _dbus_assert (id >= 0);
324 if (find_key_by_id (keys, n_keys, id) != NULL)
326 _dbus_string_set_length (&bytes, 0);
327 _dbus_verbose ("Key ID %d already existed, trying another one\n",
332 _dbus_verbose ("Creating key with ID %d\n", id);
334 #define KEY_LENGTH_BYTES 24
335 _dbus_string_set_length (&bytes, 0);
336 if (!_dbus_generate_random_bytes (&bytes, KEY_LENGTH_BYTES))
338 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
342 new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
345 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
350 *keys_p = keys; /* otherwise *keys_p ends up invalid */
353 if (!_dbus_string_init (&keys[n_keys-1].secret))
355 n_keys -= 1; /* we don't want to free the one we didn't init */
356 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
360 _dbus_get_current_time (×tamp, NULL);
362 keys[n_keys-1].id = id;
363 keys[n_keys-1].creation_time = timestamp;
364 if (!_dbus_string_move (&bytes, 0,
365 &keys[n_keys-1].secret,
368 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
369 _dbus_string_free (&keys[n_keys-1].secret);
379 _dbus_string_free (&bytes);
384 * Reloads the keyring file, optionally adds one new key to the file,
385 * removes all expired keys from the file iff a key was added, then
386 * resaves the file. Stores the keys from the file in keyring->keys.
387 * Note that the file is only resaved (written to) if a key is added,
388 * this means that only servers ever write to the file and need to
389 * lock it, which avoids a lot of lock contention at login time and
392 * @param keyring the keyring
393 * @param add_new #TRUE to add a new key to the file, expire keys, and resave
394 * @param error return location for errors
395 * @returns #FALSE on failure
398 _dbus_keyring_reload (DBusKeyring *keyring,
405 dbus_bool_t have_lock;
412 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
414 if (!_dbus_check_dir_is_private_to_user (&keyring->directory, error))
417 if (!_dbus_string_init (&contents))
419 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
423 if (!_dbus_string_init (&line))
425 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
426 _dbus_string_free (&contents);
435 _dbus_get_current_time (&now, NULL);
439 if (!_dbus_keyring_lock (keyring))
441 dbus_set_error (error, DBUS_ERROR_FAILED,
442 "Could not lock keyring file to add to it");
449 dbus_error_init (&tmp_error);
450 if (!_dbus_file_get_contents (&contents,
454 _dbus_verbose ("Failed to load keyring file: %s\n",
456 /* continue with empty keyring file, so we recreate it */
457 dbus_error_free (&tmp_error);
460 if (!_dbus_string_validate_ascii (&contents, 0,
461 _dbus_string_get_length (&contents)))
463 _dbus_warn ("Secret keyring file contains non-ASCII! Ignoring existing contents\n");
464 _dbus_string_set_length (&contents, 0);
467 /* FIXME this is badly inefficient for large keyring files
468 * (not that large keyring files exist outside of test suites)
470 while (_dbus_string_pop_line (&contents, &line))
480 /* Don't load more than the max. */
481 if (n_keys >= (add_new ? MAX_KEYS_IN_FILE - 1 : MAX_KEYS_IN_FILE))
485 if (!_dbus_string_parse_int (&line, 0, &val, &next))
487 _dbus_verbose ("could not parse secret key ID at start of line\n");
491 if (val > _DBUS_INT32_MAX || val < 0)
493 _dbus_verbose ("invalid secret key ID at start of line\n");
499 _dbus_string_skip_blank (&line, next, &next);
501 if (!_dbus_string_parse_int (&line, next, ×tamp, &next))
503 _dbus_verbose ("could not parse secret key timestamp\n");
508 (now + MAX_TIME_TRAVEL_SECONDS) < timestamp ||
509 (now - EXPIRE_KEYS_TIMEOUT_SECONDS) > timestamp)
511 _dbus_verbose ("dropping/ignoring %ld-seconds old key with timestamp %ld as current time is %ld\n",
512 now - timestamp, timestamp, now);
516 _dbus_string_skip_blank (&line, next, &next);
518 len = _dbus_string_get_length (&line);
520 if ((len - next) == 0)
522 _dbus_verbose ("no secret key after ID and timestamp\n");
526 /* We have all three parts */
527 new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
530 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
537 if (!_dbus_string_init (&keys[n_keys-1].secret))
539 n_keys -= 1; /* we don't want to free the one we didn't init */
540 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
544 keys[n_keys-1].id = id;
545 keys[n_keys-1].creation_time = timestamp;
546 if (!_dbus_string_hex_decode (&line, next, &end,
547 &keys[n_keys-1].secret, 0))
549 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
553 if (_dbus_string_get_length (&line) != end)
555 _dbus_verbose ("invalid hex encoding in keyring file\n");
556 _dbus_string_free (&keys[n_keys - 1].secret);
562 _dbus_verbose ("Successfully loaded %d existing keys\n",
567 if (!add_new_key (&keys, &n_keys, error))
569 _dbus_verbose ("Failed to generate new key: %s\n",
570 error ? error->message : "(unknown)");
574 _dbus_string_set_length (&contents, 0);
579 if (!_dbus_string_append_int (&contents,
583 if (!_dbus_string_append_byte (&contents, ' '))
586 if (!_dbus_string_append_int (&contents,
587 keys[i].creation_time))
590 if (!_dbus_string_append_byte (&contents, ' '))
593 if (!_dbus_string_hex_encode (&keys[i].secret, 0,
595 _dbus_string_get_length (&contents)))
598 if (!_dbus_string_append_byte (&contents, '\n'))
605 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
609 if (!_dbus_string_save_to_file (&contents, &keyring->filename,
615 free_keys (keyring->keys, keyring->n_keys);
616 keyring->keys = keys;
617 keyring->n_keys = n_keys;
625 _dbus_keyring_unlock (keyring);
627 if (! ((retval == TRUE && (error == NULL || error->name == NULL)) ||
628 (retval == FALSE && (error == NULL || error->name != NULL))))
630 if (error && error->name)
631 _dbus_verbose ("error is %s: %s\n", error->name, error->message);
632 _dbus_warn ("returning %d but error pointer %p name %s\n",
633 retval, error, error->name ? error->name : "(none)");
634 _dbus_assert_not_reached ("didn't handle errors properly");
642 _dbus_string_zero (&keys[i].secret);
643 _dbus_string_free (&keys[i].secret);
650 _dbus_string_free (&contents);
651 _dbus_string_free (&line);
656 /** @} */ /* end of internals */
659 * @addtogroup DBusKeyring
665 * Increments reference count of the keyring
667 * @param keyring the keyring
668 * @returns the keyring
671 _dbus_keyring_ref (DBusKeyring *keyring)
673 keyring->refcount += 1;
679 * Decrements refcount and finalizes if it reaches
682 * @param keyring the keyring
685 _dbus_keyring_unref (DBusKeyring *keyring)
687 keyring->refcount -= 1;
689 if (keyring->refcount == 0)
691 if (keyring->credentials)
692 _dbus_credentials_unref (keyring->credentials);
694 _dbus_string_free (&keyring->filename);
695 _dbus_string_free (&keyring->filename_lock);
696 _dbus_string_free (&keyring->directory);
697 free_keys (keyring->keys, keyring->n_keys);
703 * Creates a new keyring that lives in the ~/.dbus-keyrings directory
704 * of the given user credentials. If the credentials are #NULL or
705 * empty, uses those of the current process.
707 * @param username username to get keyring for, or #NULL
708 * @param context which keyring to get
709 * @param error return location for errors
710 * @returns the keyring or #NULL on error
713 _dbus_keyring_new_for_credentials (DBusCredentials *credentials,
714 const DBusString *context,
718 DBusKeyring *keyring;
719 dbus_bool_t error_set;
721 DBusCredentials *our_credentials;
723 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
727 our_credentials = NULL;
729 if (!_dbus_string_init (&ringdir))
731 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
735 if (credentials != NULL)
737 our_credentials = _dbus_credentials_copy (credentials);
741 our_credentials = _dbus_credentials_new_from_current_process ();
744 if (our_credentials == NULL)
747 if (_dbus_credentials_are_anonymous (our_credentials))
749 if (!_dbus_credentials_add_from_current_process (our_credentials))
753 if (!_dbus_append_keyring_directory_for_credentials (&ringdir,
757 keyring = _dbus_keyring_new ();
761 _dbus_assert (keyring->credentials == NULL);
762 keyring->credentials = our_credentials;
763 our_credentials = NULL; /* so we don't unref it again later */
765 /* should have been validated already, but paranoia check here */
766 if (!_dbus_keyring_validate_context (context))
769 dbus_set_error_const (error,
771 "Invalid context in keyring creation");
775 /* Save keyring dir in the keyring object */
776 if (!_dbus_string_copy (&ringdir, 0,
777 &keyring->directory, 0))
780 /* Create keyring->filename based on keyring dir and context */
781 if (!_dbus_string_copy (&keyring->directory, 0,
782 &keyring->filename, 0))
785 if (!_dbus_concat_dir_and_file (&keyring->filename,
789 /* Create lockfile name */
790 if (!_dbus_string_copy (&keyring->filename, 0,
791 &keyring->filename_lock, 0))
794 if (!_dbus_string_append (&keyring->filename_lock, ".lock"))
798 dbus_error_init (&tmp_error);
799 if (!_dbus_keyring_reload (keyring, FALSE, &tmp_error))
801 _dbus_verbose ("didn't load an existing keyring: %s\n",
803 dbus_error_free (&tmp_error);
806 /* We don't fail fatally if we can't create the directory,
807 * but the keyring will probably always be empty
808 * unless someone else manages to create it
810 dbus_error_init (&tmp_error);
811 if (!_dbus_create_directory (&keyring->directory,
814 _dbus_verbose ("Creating keyring directory: %s\n",
816 dbus_error_free (&tmp_error);
819 _dbus_string_free (&ringdir);
825 dbus_set_error_const (error,
826 DBUS_ERROR_NO_MEMORY,
829 _dbus_credentials_unref (our_credentials);
831 _dbus_keyring_unref (keyring);
832 _dbus_string_free (&ringdir);
838 * Checks whether the context is a valid context.
839 * Contexts that might cause confusion when used
840 * in filenames are not allowed (contexts can't
841 * start with a dot or contain dir separators).
843 * @todo this is the most inefficient implementation
846 * @param context the context
847 * @returns #TRUE if valid
850 _dbus_keyring_validate_context (const DBusString *context)
852 if (_dbus_string_get_length (context) == 0)
854 _dbus_verbose ("context is zero-length\n");
858 if (!_dbus_string_validate_ascii (context, 0,
859 _dbus_string_get_length (context)))
861 _dbus_verbose ("context not valid ascii\n");
865 /* no directory separators */
866 if (_dbus_string_find (context, 0, "/", NULL))
868 _dbus_verbose ("context contains a slash\n");
872 if (_dbus_string_find (context, 0, "\\", NULL))
874 _dbus_verbose ("context contains a backslash\n");
878 /* prevent attempts to use dotfiles or ".." or ".lock"
879 * all of which might allow some kind of attack
881 if (_dbus_string_find (context, 0, ".", NULL))
883 _dbus_verbose ("context contains a dot\n");
887 /* no spaces/tabs, those are used for separators in the protocol */
888 if (_dbus_string_find_blank (context, 0, NULL))
890 _dbus_verbose ("context contains a blank\n");
894 if (_dbus_string_find (context, 0, "\n", NULL))
896 _dbus_verbose ("context contains a newline\n");
900 if (_dbus_string_find (context, 0, "\r", NULL))
902 _dbus_verbose ("context contains a carriage return\n");
910 find_recent_key (DBusKeyring *keyring)
913 long tv_sec, tv_usec;
915 _dbus_get_current_time (&tv_sec, &tv_usec);
918 while (i < keyring->n_keys)
920 DBusKey *key = &keyring->keys[i];
922 _dbus_verbose ("Key %d is %ld seconds old\n",
923 i, tv_sec - key->creation_time);
925 if ((tv_sec - NEW_KEY_TIMEOUT_SECONDS) < key->creation_time)
935 * Gets a recent key to use for authentication.
936 * If no recent key exists, creates one. Returns
937 * the key ID. If a key can't be written to the keyring
938 * file so no recent key can be created, returns -1.
939 * All valid keys are > 0.
941 * @param keyring the keyring
942 * @param error error on failure
943 * @returns key ID to use for auth, or -1 on failure
946 _dbus_keyring_get_best_key (DBusKeyring *keyring,
951 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
953 key = find_recent_key (keyring);
957 /* All our keys are too old, or we've never loaded the
958 * keyring. Create a new one.
960 if (!_dbus_keyring_reload (keyring, TRUE,
964 key = find_recent_key (keyring);
969 dbus_set_error_const (error,
971 "No recent-enough key found in keyring, and unable to create a new key");
977 * Checks whether the keyring is for the same user as the given credentials.
979 * @param keyring the keyring
980 * @param credentials the credentials to check
982 * @returns #TRUE if the keyring belongs to the given user
985 _dbus_keyring_is_for_credentials (DBusKeyring *keyring,
986 DBusCredentials *credentials)
988 return _dbus_credentials_same_user (keyring->credentials,
993 * Gets the hex-encoded secret key for the given ID.
994 * Returns #FALSE if not enough memory. Returns #TRUE
995 * but empty key on any other error such as unknown
998 * @param keyring the keyring
999 * @param key_id the key ID
1000 * @param hex_key string to append hex-encoded key to
1001 * @returns #TRUE if we had enough memory
1004 _dbus_keyring_get_hex_key (DBusKeyring *keyring,
1006 DBusString *hex_key)
1010 key = find_key_by_id (keyring->keys,
1014 return TRUE; /* had enough memory, so TRUE */
1016 return _dbus_string_hex_encode (&key->secret, 0,
1018 _dbus_string_get_length (hex_key));
1021 /** @} */ /* end of exposed API */
1023 #ifdef DBUS_BUILD_TESTS
1024 #include "dbus-test.h"
1028 _dbus_keyring_test (void)
1040 /* Context validation */
1042 _dbus_string_init_const (&context, "foo");
1043 _dbus_assert (_dbus_keyring_validate_context (&context));
1044 _dbus_string_init_const (&context, "org_freedesktop_blah");
1045 _dbus_assert (_dbus_keyring_validate_context (&context));
1047 _dbus_string_init_const (&context, "");
1048 _dbus_assert (!_dbus_keyring_validate_context (&context));
1049 _dbus_string_init_const (&context, ".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, "bar\\foo");
1056 _dbus_assert (!_dbus_keyring_validate_context (&context));
1057 _dbus_string_init_const (&context, "foo\xfa\xf0");
1058 _dbus_assert (!_dbus_keyring_validate_context (&context));
1059 _dbus_string_init_const (&context, "foo\x80");
1060 _dbus_assert (!_dbus_keyring_validate_context (&context));
1061 _dbus_string_init_const (&context, "foo\x7f");
1062 _dbus_assert (_dbus_keyring_validate_context (&context));
1063 _dbus_string_init_const (&context, "foo bar");
1064 _dbus_assert (!_dbus_keyring_validate_context (&context));
1066 if (!_dbus_string_init (&context))
1067 _dbus_assert_not_reached ("no memory");
1068 if (!_dbus_string_append_byte (&context, '\0'))
1069 _dbus_assert_not_reached ("no memory");
1070 _dbus_assert (!_dbus_keyring_validate_context (&context));
1071 _dbus_string_free (&context);
1073 /* Now verify that if we create a key in keyring 1,
1074 * it is properly loaded in keyring 2
1077 _dbus_string_init_const (&context, "org_freedesktop_dbus_testsuite");
1078 dbus_error_init (&error);
1079 ring1 = _dbus_keyring_new_for_credentials (NULL, &context,
1081 _dbus_assert (ring1);
1082 _dbus_assert (error.name == NULL);
1084 id = _dbus_keyring_get_best_key (ring1, &error);
1087 fprintf (stderr, "Could not load keyring: %s\n", error.message);
1088 dbus_error_free (&error);
1092 ring2 = _dbus_keyring_new_for_credentials (NULL, &context, &error);
1093 _dbus_assert (ring2);
1094 _dbus_assert (error.name == NULL);
1096 if (ring1->n_keys != ring2->n_keys)
1098 fprintf (stderr, "Different number of keys in keyrings\n");
1102 /* We guarantee we load and save keeping keys in a fixed
1106 while (i < ring1->n_keys)
1108 if (ring1->keys[i].id != ring2->keys[i].id)
1110 fprintf (stderr, "Keyring 1 has first key ID %d and keyring 2 has %d\n",
1111 ring1->keys[i].id, ring2->keys[i].id);
1115 if (ring1->keys[i].creation_time != ring2->keys[i].creation_time)
1117 fprintf (stderr, "Keyring 1 has first key time %ld and keyring 2 has %ld\n",
1118 ring1->keys[i].creation_time, ring2->keys[i].creation_time);
1122 if (!_dbus_string_equal (&ring1->keys[i].secret,
1123 &ring2->keys[i].secret))
1125 fprintf (stderr, "Keyrings 1 and 2 have different secrets for same ID/timestamp\n");
1132 printf (" %d keys in test\n", ring1->n_keys);
1134 /* Test ref/unref */
1135 _dbus_keyring_ref (ring1);
1136 _dbus_keyring_ref (ring2);
1137 _dbus_keyring_unref (ring1);
1138 _dbus_keyring_unref (ring2);
1142 _dbus_keyring_unref (ring1);
1143 _dbus_keyring_unref (ring2);
1149 _dbus_keyring_unref (ring1);
1151 _dbus_keyring_unref (ring2);
1156 #endif /* DBUS_BUILD_TESTS */