1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-keyring.c Store secret cookies in your homedir
4 * Copyright (C) 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.0
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-userdb.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 username; /**< Username keyring is for */
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 */
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 if (!_dbus_string_init (&keyring->username))
142 keyring->refcount = 1;
143 keyring->keys = NULL;
149 _dbus_string_free (&keyring->filename_lock);
151 _dbus_string_free (&keyring->filename);
153 _dbus_string_free (&keyring->directory);
161 free_keys (DBusKey *keys,
166 /* should be safe for args NULL, 0 */
171 _dbus_string_free (&keys[i].secret);
178 /* Our locking scheme is highly unreliable. However, there is
179 * unfortunately no reliable locking scheme in user home directories;
180 * between bugs in Linux NFS, people using Tru64 or other total crap
181 * NFS, AFS, random-file-system-of-the-week, and so forth, fcntl() in
182 * homedirs simply generates tons of bug reports. This has been
183 * learned through hard experience with GConf, unfortunately.
185 * This bad hack might work better for the kind of lock we have here,
186 * which we don't expect to hold for any length of time. Crashing
187 * while we hold it should be unlikely, and timing out such that we
188 * delete a stale lock should also be unlikely except when the
189 * filesystem is running really slowly. Stuff might break in corner
190 * cases but as long as it's not a security-level breakage it should
194 /** Maximum number of timeouts waiting for lock before we decide it's stale */
195 #define MAX_LOCK_TIMEOUTS 32
196 /** Length of each timeout while waiting for a lock */
197 #define LOCK_TIMEOUT_MILLISECONDS 250
200 _dbus_keyring_lock (DBusKeyring *keyring)
205 while (n_timeouts < MAX_LOCK_TIMEOUTS)
209 dbus_error_init (&error);
210 if (_dbus_create_file_exclusively (&keyring->filename_lock,
214 _dbus_verbose ("Did not get lock file, sleeping %d milliseconds (%s)\n",
215 LOCK_TIMEOUT_MILLISECONDS, error.message);
216 dbus_error_free (&error);
218 _dbus_sleep_milliseconds (LOCK_TIMEOUT_MILLISECONDS);
223 if (n_timeouts == MAX_LOCK_TIMEOUTS)
227 _dbus_verbose ("Lock file timed out %d times, assuming stale\n",
230 dbus_error_init (&error);
232 if (!_dbus_delete_file (&keyring->filename_lock, &error))
234 _dbus_verbose ("Couldn't delete old lock file: %s\n",
236 dbus_error_free (&error);
240 if (!_dbus_create_file_exclusively (&keyring->filename_lock,
243 _dbus_verbose ("Couldn't create lock file after deleting stale one: %s\n",
245 dbus_error_free (&error);
254 _dbus_keyring_unlock (DBusKeyring *keyring)
257 dbus_error_init (&error);
258 if (!_dbus_delete_file (&keyring->filename_lock, &error))
260 _dbus_warn ("Failed to delete lock file: %s\n",
262 dbus_error_free (&error);
267 find_key_by_id (DBusKey *keys,
276 if (keys[i].id == id)
286 add_new_key (DBusKey **keys_p,
293 unsigned long timestamp;
294 const unsigned char *s;
299 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
301 if (!_dbus_string_init (&bytes))
303 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
311 /* Generate an integer ID and then the actual key. */
314 if (!_dbus_generate_random_bytes (&bytes, 4))
316 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
320 s = (const unsigned char*) _dbus_string_get_const_data (&bytes);
322 id = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24);
325 _dbus_assert (id >= 0);
327 if (find_key_by_id (keys, n_keys, id) != NULL)
329 _dbus_string_set_length (&bytes, 0);
330 _dbus_verbose ("Key ID %d already existed, trying another one\n",
335 _dbus_verbose ("Creating key with ID %d\n", id);
337 #define KEY_LENGTH_BYTES 24
338 _dbus_string_set_length (&bytes, 0);
339 if (!_dbus_generate_random_bytes (&bytes, KEY_LENGTH_BYTES))
341 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
345 new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
348 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
353 *keys_p = keys; /* otherwise *keys_p ends up invalid */
356 if (!_dbus_string_init (&keys[n_keys-1].secret))
358 n_keys -= 1; /* we don't want to free the one we didn't init */
359 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
363 _dbus_get_current_time (×tamp, NULL);
365 keys[n_keys-1].id = id;
366 keys[n_keys-1].creation_time = timestamp;
367 if (!_dbus_string_move (&bytes, 0,
368 &keys[n_keys-1].secret,
371 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
372 _dbus_string_free (&keys[n_keys-1].secret);
382 _dbus_string_free (&bytes);
387 * Reloads the keyring file, optionally adds one new key to the file,
388 * removes all expired keys from the file iff a key was added, then
389 * resaves the file. Stores the keys from the file in keyring->keys.
390 * Note that the file is only resaved (written to) if a key is added,
391 * this means that only servers ever write to the file and need to
392 * lock it, which avoids a lot of lock contention at login time and
395 * @param keyring the keyring
396 * @param add_new #TRUE to add a new key to the file, expire keys, and resave
397 * @param error return location for errors
398 * @returns #FALSE on failure
401 _dbus_keyring_reload (DBusKeyring *keyring,
408 dbus_bool_t have_lock;
415 _DBUS_ASSERT_ERROR_IS_CLEAR (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 : MAX_KEYS_IN_FILE - 1))
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_INT_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 ? "(unknown)" : error->message);
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 _dbus_string_free (&keyring->username);
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
702 * directory of the given user. If the username is #NULL,
703 * uses the user owning 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_homedir (const DBusString *username,
712 const DBusString *context,
716 DBusKeyring *keyring;
717 dbus_bool_t error_set;
721 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
726 if (!_dbus_string_init (&homedir))
728 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
732 _dbus_string_init_const (&dotdir, ".dbus-keyrings");
734 if (username == NULL)
736 const DBusString *const_homedir;
738 if (!_dbus_username_from_current_process (&username) ||
739 !_dbus_homedir_from_current_process (&const_homedir))
742 if (!_dbus_string_copy (const_homedir, 0,
748 if (!_dbus_homedir_from_username (username, &homedir))
752 #ifdef DBUS_BUILD_TESTS
754 const char *override;
756 override = _dbus_getenv ("DBUS_TEST_HOMEDIR");
757 if (override != NULL && *override != '\0')
759 _dbus_string_set_length (&homedir, 0);
760 if (!_dbus_string_append (&homedir, override))
761 _dbus_assert_not_reached ("no memory");
763 _dbus_verbose ("Using fake homedir for testing: %s\n",
764 _dbus_string_get_const_data (&homedir));
768 static dbus_bool_t already_warned = FALSE;
771 _dbus_warn ("Using your real home directory for testing, set DBUS_TEST_HOMEDIR to avoid\n");
772 already_warned = TRUE;
778 _dbus_assert (username != NULL);
780 keyring = _dbus_keyring_new ();
784 /* should have been validated already, but paranoia check here */
785 if (!_dbus_keyring_validate_context (context))
788 dbus_set_error_const (error,
790 "Invalid context in keyring creation");
794 if (!_dbus_string_copy (username, 0,
795 &keyring->username, 0))
798 if (!_dbus_string_copy (&homedir, 0,
799 &keyring->directory, 0))
802 if (!_dbus_concat_dir_and_file (&keyring->directory,
806 if (!_dbus_string_copy (&keyring->directory, 0,
807 &keyring->filename, 0))
810 if (!_dbus_concat_dir_and_file (&keyring->filename,
814 if (!_dbus_string_copy (&keyring->filename, 0,
815 &keyring->filename_lock, 0))
818 if (!_dbus_string_append (&keyring->filename_lock, ".lock"))
821 dbus_error_init (&tmp_error);
822 if (!_dbus_keyring_reload (keyring, FALSE, &tmp_error))
824 _dbus_verbose ("didn't load an existing keyring: %s\n",
826 dbus_error_free (&tmp_error);
829 /* We don't fail fatally if we can't create the directory,
830 * but the keyring will probably always be empty
831 * unless someone else manages to create it
833 dbus_error_init (&tmp_error);
834 if (!_dbus_create_directory (&keyring->directory,
837 _dbus_verbose ("Creating keyring directory: %s\n",
839 dbus_error_free (&tmp_error);
842 _dbus_string_free (&homedir);
848 dbus_set_error_const (error,
849 DBUS_ERROR_NO_MEMORY,
852 _dbus_keyring_unref (keyring);
853 _dbus_string_free (&homedir);
859 * Checks whether the context is a valid context.
860 * Contexts that might cause confusion when used
861 * in filenames are not allowed (contexts can't
862 * start with a dot or contain dir separators).
864 * @todo this is the most inefficient implementation
867 * @param context the context
868 * @returns #TRUE if valid
871 _dbus_keyring_validate_context (const DBusString *context)
873 if (_dbus_string_get_length (context) == 0)
875 _dbus_verbose ("context is zero-length\n");
879 if (!_dbus_string_validate_ascii (context, 0,
880 _dbus_string_get_length (context)))
882 _dbus_verbose ("context not valid ascii\n");
886 /* no directory separators */
887 if (_dbus_string_find (context, 0, "/", NULL))
889 _dbus_verbose ("context contains a slash\n");
893 if (_dbus_string_find (context, 0, "\\", NULL))
895 _dbus_verbose ("context contains a backslash\n");
899 /* prevent attempts to use dotfiles or ".." or ".lock"
900 * all of which might allow some kind of attack
902 if (_dbus_string_find (context, 0, ".", NULL))
904 _dbus_verbose ("context contains a dot\n");
908 /* no spaces/tabs, those are used for separators in the protocol */
909 if (_dbus_string_find_blank (context, 0, NULL))
911 _dbus_verbose ("context contains a blank\n");
915 if (_dbus_string_find (context, 0, "\n", NULL))
917 _dbus_verbose ("context contains a newline\n");
921 if (_dbus_string_find (context, 0, "\r", NULL))
923 _dbus_verbose ("context contains a carriage return\n");
931 find_recent_key (DBusKeyring *keyring)
934 long tv_sec, tv_usec;
936 _dbus_get_current_time (&tv_sec, &tv_usec);
939 while (i < keyring->n_keys)
941 DBusKey *key = &keyring->keys[i];
943 _dbus_verbose ("Key %d is %ld seconds old\n",
944 i, tv_sec - key->creation_time);
946 if ((tv_sec - NEW_KEY_TIMEOUT_SECONDS) < key->creation_time)
956 * Gets a recent key to use for authentication.
957 * If no recent key exists, creates one. Returns
958 * the key ID. If a key can't be written to the keyring
959 * file so no recent key can be created, returns -1.
960 * All valid keys are > 0.
962 * @param keyring the keyring
963 * @param error error on failure
964 * @returns key ID to use for auth, or -1 on failure
967 _dbus_keyring_get_best_key (DBusKeyring *keyring,
972 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
974 key = find_recent_key (keyring);
978 /* All our keys are too old, or we've never loaded the
979 * keyring. Create a new one.
981 if (!_dbus_keyring_reload (keyring, TRUE,
985 key = find_recent_key (keyring);
990 dbus_set_error_const (error,
992 "No recent-enough key found in keyring, and unable to create a new key");
998 * Checks whether the keyring is for the given username.
1000 * @param keyring the keyring
1001 * @param username the username to check
1003 * @returns #TRUE if the keyring belongs to the given user
1006 _dbus_keyring_is_for_user (DBusKeyring *keyring,
1007 const DBusString *username)
1009 return _dbus_string_equal (&keyring->username,
1014 * Gets the hex-encoded secret key for the given ID.
1015 * Returns #FALSE if not enough memory. Returns #TRUE
1016 * but empty key on any other error such as unknown
1019 * @param keyring the keyring
1020 * @param key_id the key ID
1021 * @param hex_key string to append hex-encoded key to
1022 * @returns #TRUE if we had enough memory
1025 _dbus_keyring_get_hex_key (DBusKeyring *keyring,
1027 DBusString *hex_key)
1031 key = find_key_by_id (keyring->keys,
1035 return TRUE; /* had enough memory, so TRUE */
1037 return _dbus_string_hex_encode (&key->secret, 0,
1039 _dbus_string_get_length (hex_key));
1042 /** @} */ /* end of exposed API */
1044 #ifdef DBUS_BUILD_TESTS
1045 #include "dbus-test.h"
1049 _dbus_keyring_test (void)
1061 /* Context validation */
1063 _dbus_string_init_const (&context, "foo");
1064 _dbus_assert (_dbus_keyring_validate_context (&context));
1065 _dbus_string_init_const (&context, "org_freedesktop_blah");
1066 _dbus_assert (_dbus_keyring_validate_context (&context));
1068 _dbus_string_init_const (&context, "");
1069 _dbus_assert (!_dbus_keyring_validate_context (&context));
1070 _dbus_string_init_const (&context, ".foo");
1071 _dbus_assert (!_dbus_keyring_validate_context (&context));
1072 _dbus_string_init_const (&context, "bar.foo");
1073 _dbus_assert (!_dbus_keyring_validate_context (&context));
1074 _dbus_string_init_const (&context, "bar/foo");
1075 _dbus_assert (!_dbus_keyring_validate_context (&context));
1076 _dbus_string_init_const (&context, "bar\\foo");
1077 _dbus_assert (!_dbus_keyring_validate_context (&context));
1078 _dbus_string_init_const (&context, "foo\xfa\xf0");
1079 _dbus_assert (!_dbus_keyring_validate_context (&context));
1080 _dbus_string_init_const (&context, "foo\x80");
1081 _dbus_assert (!_dbus_keyring_validate_context (&context));
1082 _dbus_string_init_const (&context, "foo\x7f");
1083 _dbus_assert (_dbus_keyring_validate_context (&context));
1084 _dbus_string_init_const (&context, "foo bar");
1085 _dbus_assert (!_dbus_keyring_validate_context (&context));
1087 if (!_dbus_string_init (&context))
1088 _dbus_assert_not_reached ("no memory");
1089 if (!_dbus_string_append_byte (&context, '\0'))
1090 _dbus_assert_not_reached ("no memory");
1091 _dbus_assert (!_dbus_keyring_validate_context (&context));
1092 _dbus_string_free (&context);
1094 /* Now verify that if we create a key in keyring 1,
1095 * it is properly loaded in keyring 2
1098 _dbus_string_init_const (&context, "org_freedesktop_dbus_testsuite");
1099 dbus_error_init (&error);
1100 ring1 = _dbus_keyring_new_homedir (NULL, &context,
1102 _dbus_assert (ring1);
1103 _dbus_assert (error.name == NULL);
1105 id = _dbus_keyring_get_best_key (ring1, &error);
1108 fprintf (stderr, "Could not load keyring: %s\n", error.message);
1109 dbus_error_free (&error);
1113 ring2 = _dbus_keyring_new_homedir (NULL, &context, &error);
1114 _dbus_assert (ring2);
1115 _dbus_assert (error.name == NULL);
1117 if (ring1->n_keys != ring2->n_keys)
1119 fprintf (stderr, "Different number of keys in keyrings\n");
1123 /* We guarantee we load and save keeping keys in a fixed
1127 while (i < ring1->n_keys)
1129 if (ring1->keys[i].id != ring2->keys[i].id)
1131 fprintf (stderr, "Keyring 1 has first key ID %d and keyring 2 has %d\n",
1132 ring1->keys[i].id, ring2->keys[i].id);
1136 if (ring1->keys[i].creation_time != ring2->keys[i].creation_time)
1138 fprintf (stderr, "Keyring 1 has first key time %ld and keyring 2 has %ld\n",
1139 ring1->keys[i].creation_time, ring2->keys[i].creation_time);
1143 if (!_dbus_string_equal (&ring1->keys[i].secret,
1144 &ring2->keys[i].secret))
1146 fprintf (stderr, "Keyrings 1 and 2 have different secrets for same ID/timestamp\n");
1153 printf (" %d keys in test\n", ring1->n_keys);
1155 /* Test ref/unref */
1156 _dbus_keyring_ref (ring1);
1157 _dbus_keyring_ref (ring2);
1158 _dbus_keyring_unref (ring1);
1159 _dbus_keyring_unref (ring2);
1163 _dbus_keyring_unref (ring1);
1164 _dbus_keyring_unref (ring2);
1170 _dbus_keyring_unref (ring1);
1172 _dbus_keyring_unref (ring2);
1177 #endif /* DBUS_BUILD_TESTS */