1 /* -*- mode: C; c-file-style: "gnu" -*- */
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-userdb.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 username; /**< Username keyring is for */
115 DBusString directory; /**< Directory the below two items are inside */
116 DBusString filename; /**< Keyring filename */
117 DBusString filename_lock; /**< Name of lockfile */
118 DBusKey *keys; /**< Keys loaded from the file */
119 int n_keys; /**< Number of keys */
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 if (!_dbus_string_init (&keyring->username))
143 keyring->refcount = 1;
144 keyring->keys = NULL;
150 _dbus_string_free (&keyring->filename_lock);
152 _dbus_string_free (&keyring->filename);
154 _dbus_string_free (&keyring->directory);
162 free_keys (DBusKey *keys,
167 /* should be safe for args NULL, 0 */
172 _dbus_string_free (&keys[i].secret);
179 /* Our locking scheme is highly unreliable. However, there is
180 * unfortunately no reliable locking scheme in user home directories;
181 * between bugs in Linux NFS, people using Tru64 or other total crap
182 * NFS, AFS, random-file-system-of-the-week, and so forth, fcntl() in
183 * homedirs simply generates tons of bug reports. This has been
184 * learned through hard experience with GConf, unfortunately.
186 * This bad hack might work better for the kind of lock we have here,
187 * which we don't expect to hold for any length of time. Crashing
188 * while we hold it should be unlikely, and timing out such that we
189 * delete a stale lock should also be unlikely except when the
190 * filesystem is running really slowly. Stuff might break in corner
191 * cases but as long as it's not a security-level breakage it should
195 /** Maximum number of timeouts waiting for lock before we decide it's stale */
196 #define MAX_LOCK_TIMEOUTS 32
197 /** Length of each timeout while waiting for a lock */
198 #define LOCK_TIMEOUT_MILLISECONDS 250
201 _dbus_keyring_lock (DBusKeyring *keyring)
206 while (n_timeouts < MAX_LOCK_TIMEOUTS)
210 dbus_error_init (&error);
211 if (_dbus_create_file_exclusively (&keyring->filename_lock,
215 _dbus_verbose ("Did not get lock file, sleeping %d milliseconds (%s)\n",
216 LOCK_TIMEOUT_MILLISECONDS, error.message);
217 dbus_error_free (&error);
219 _dbus_sleep_milliseconds (LOCK_TIMEOUT_MILLISECONDS);
224 if (n_timeouts == MAX_LOCK_TIMEOUTS)
228 _dbus_verbose ("Lock file timed out %d times, assuming stale\n",
231 dbus_error_init (&error);
233 if (!_dbus_delete_file (&keyring->filename_lock, &error))
235 _dbus_verbose ("Couldn't delete old lock file: %s\n",
237 dbus_error_free (&error);
241 if (!_dbus_create_file_exclusively (&keyring->filename_lock,
244 _dbus_verbose ("Couldn't create lock file after deleting stale one: %s\n",
246 dbus_error_free (&error);
255 _dbus_keyring_unlock (DBusKeyring *keyring)
258 dbus_error_init (&error);
259 if (!_dbus_delete_file (&keyring->filename_lock, &error))
261 _dbus_warn ("Failed to delete lock file: %s\n",
263 dbus_error_free (&error);
268 find_key_by_id (DBusKey *keys,
277 if (keys[i].id == id)
287 add_new_key (DBusKey **keys_p,
295 const unsigned char *s;
300 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
302 if (!_dbus_string_init (&bytes))
304 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
312 /* Generate an integer ID and then the actual key. */
315 if (!_dbus_generate_random_bytes (&bytes, 4))
317 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
321 s = (const unsigned char*) _dbus_string_get_const_data (&bytes);
323 id = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24);
326 _dbus_assert (id >= 0);
328 if (find_key_by_id (keys, n_keys, id) != NULL)
330 _dbus_string_set_length (&bytes, 0);
331 _dbus_verbose ("Key ID %d already existed, trying another one\n",
336 _dbus_verbose ("Creating key with ID %d\n", id);
338 #define KEY_LENGTH_BYTES 24
339 _dbus_string_set_length (&bytes, 0);
340 if (!_dbus_generate_random_bytes (&bytes, KEY_LENGTH_BYTES))
342 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
346 new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
349 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
354 *keys_p = keys; /* otherwise *keys_p ends up invalid */
357 if (!_dbus_string_init (&keys[n_keys-1].secret))
359 n_keys -= 1; /* we don't want to free the one we didn't init */
360 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
364 _dbus_get_current_time (×tamp, NULL);
366 keys[n_keys-1].id = id;
367 keys[n_keys-1].creation_time = timestamp;
368 if (!_dbus_string_move (&bytes, 0,
369 &keys[n_keys-1].secret,
372 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
373 _dbus_string_free (&keys[n_keys-1].secret);
383 _dbus_string_free (&bytes);
388 * Reloads the keyring file, optionally adds one new key to the file,
389 * removes all expired keys from the file iff a key was added, then
390 * resaves the file. Stores the keys from the file in keyring->keys.
391 * Note that the file is only resaved (written to) if a key is added,
392 * this means that only servers ever write to the file and need to
393 * lock it, which avoids a lot of lock contention at login time and
396 * @param keyring the keyring
397 * @param add_new #TRUE to add a new key to the file, expire keys, and resave
398 * @param error return location for errors
399 * @returns #FALSE on failure
402 _dbus_keyring_reload (DBusKeyring *keyring,
409 dbus_bool_t have_lock;
416 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
418 if (!_dbus_check_dir_is_private_to_user (&keyring->directory, error))
421 if (!_dbus_string_init (&contents))
423 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
427 if (!_dbus_string_init (&line))
429 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
430 _dbus_string_free (&contents);
439 _dbus_get_current_time (&now, NULL);
443 if (!_dbus_keyring_lock (keyring))
445 dbus_set_error (error, DBUS_ERROR_FAILED,
446 "Could not lock keyring file to add to it");
453 dbus_error_init (&tmp_error);
454 if (!_dbus_file_get_contents (&contents,
458 _dbus_verbose ("Failed to load keyring file: %s\n",
460 /* continue with empty keyring file, so we recreate it */
461 dbus_error_free (&tmp_error);
464 if (!_dbus_string_validate_ascii (&contents, 0,
465 _dbus_string_get_length (&contents)))
467 _dbus_warn ("Secret keyring file contains non-ASCII! Ignoring existing contents\n");
468 _dbus_string_set_length (&contents, 0);
471 /* FIXME this is badly inefficient for large keyring files
472 * (not that large keyring files exist outside of test suites)
474 while (_dbus_string_pop_line (&contents, &line))
484 /* Don't load more than the max. */
485 if (n_keys >= (add_new ? MAX_KEYS_IN_FILE - 1 : MAX_KEYS_IN_FILE))
489 if (!_dbus_string_parse_int (&line, 0, &val, &next))
491 _dbus_verbose ("could not parse secret key ID at start of line\n");
495 if (val > _DBUS_INT32_MAX || val < 0)
497 _dbus_verbose ("invalid secret key ID at start of line\n");
503 _dbus_string_skip_blank (&line, next, &next);
505 if (!_dbus_string_parse_int (&line, next, ×tamp, &next))
507 _dbus_verbose ("could not parse secret key timestamp\n");
512 (now + MAX_TIME_TRAVEL_SECONDS) < timestamp ||
513 (now - EXPIRE_KEYS_TIMEOUT_SECONDS) > timestamp)
515 _dbus_verbose ("dropping/ignoring %ld-seconds old key with timestamp %ld as current time is %ld\n",
516 now - timestamp, timestamp, now);
520 _dbus_string_skip_blank (&line, next, &next);
522 len = _dbus_string_get_length (&line);
524 if ((len - next) == 0)
526 _dbus_verbose ("no secret key after ID and timestamp\n");
530 /* We have all three parts */
531 new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
534 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
541 if (!_dbus_string_init (&keys[n_keys-1].secret))
543 n_keys -= 1; /* we don't want to free the one we didn't init */
544 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
548 keys[n_keys-1].id = id;
549 keys[n_keys-1].creation_time = timestamp;
550 if (!_dbus_string_hex_decode (&line, next, &end,
551 &keys[n_keys-1].secret, 0))
553 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
557 if (_dbus_string_get_length (&line) != end)
559 _dbus_verbose ("invalid hex encoding in keyring file\n");
560 _dbus_string_free (&keys[n_keys - 1].secret);
566 _dbus_verbose ("Successfully loaded %d existing keys\n",
571 if (!add_new_key (&keys, &n_keys, error))
573 _dbus_verbose ("Failed to generate new key: %s\n",
574 error ? error->message : "(unknown)");
578 _dbus_string_set_length (&contents, 0);
583 if (!_dbus_string_append_int (&contents,
587 if (!_dbus_string_append_byte (&contents, ' '))
590 if (!_dbus_string_append_int (&contents,
591 keys[i].creation_time))
594 if (!_dbus_string_append_byte (&contents, ' '))
597 if (!_dbus_string_hex_encode (&keys[i].secret, 0,
599 _dbus_string_get_length (&contents)))
602 if (!_dbus_string_append_byte (&contents, '\n'))
609 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
613 if (!_dbus_string_save_to_file (&contents, &keyring->filename,
619 free_keys (keyring->keys, keyring->n_keys);
620 keyring->keys = keys;
621 keyring->n_keys = n_keys;
629 _dbus_keyring_unlock (keyring);
631 if (! ((retval == TRUE && (error == NULL || error->name == NULL)) ||
632 (retval == FALSE && (error == NULL || error->name != NULL))))
634 if (error && error->name)
635 _dbus_verbose ("error is %s: %s\n", error->name, error->message);
636 _dbus_warn ("returning %d but error pointer %p name %s\n",
637 retval, error, error->name ? error->name : "(none)");
638 _dbus_assert_not_reached ("didn't handle errors properly");
646 _dbus_string_zero (&keys[i].secret);
647 _dbus_string_free (&keys[i].secret);
654 _dbus_string_free (&contents);
655 _dbus_string_free (&line);
660 /** @} */ /* end of internals */
663 * @addtogroup DBusKeyring
669 * Increments reference count of the keyring
671 * @param keyring the keyring
672 * @returns the keyring
675 _dbus_keyring_ref (DBusKeyring *keyring)
677 keyring->refcount += 1;
683 * Decrements refcount and finalizes if it reaches
686 * @param keyring the keyring
689 _dbus_keyring_unref (DBusKeyring *keyring)
691 keyring->refcount -= 1;
693 if (keyring->refcount == 0)
695 _dbus_string_free (&keyring->username);
696 _dbus_string_free (&keyring->filename);
697 _dbus_string_free (&keyring->filename_lock);
698 _dbus_string_free (&keyring->directory);
699 free_keys (keyring->keys, keyring->n_keys);
705 * Creates a new keyring that lives in the ~/.dbus-keyrings
706 * directory of the given user. If the username is #NULL,
707 * uses the user owning the current process.
709 * @param username username to get keyring for, or #NULL
710 * @param context which keyring to get
711 * @param error return location for errors
712 * @returns the keyring or #NULL on error
715 _dbus_keyring_new_homedir (const DBusString *username,
716 const DBusString *context,
720 DBusKeyring *keyring;
721 dbus_bool_t error_set;
725 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
730 if (!_dbus_string_init (&homedir))
732 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
736 _dbus_string_init_const (&dotdir, ".dbus-keyrings");
738 if (username == NULL)
740 const DBusString *const_homedir;
742 if (!_dbus_username_from_current_process (&username) ||
743 !_dbus_homedir_from_current_process (&const_homedir))
746 if (!_dbus_string_copy (const_homedir, 0,
752 if (!_dbus_homedir_from_username (username, &homedir))
756 #ifdef DBUS_BUILD_TESTS
758 const char *override;
760 override = _dbus_getenv ("DBUS_TEST_HOMEDIR");
761 if (override != NULL && *override != '\0')
763 _dbus_string_set_length (&homedir, 0);
764 if (!_dbus_string_append (&homedir, override))
767 _dbus_verbose ("Using fake homedir for testing: %s\n",
768 _dbus_string_get_const_data (&homedir));
772 static dbus_bool_t already_warned = FALSE;
775 _dbus_warn ("Using your real home directory for testing, set DBUS_TEST_HOMEDIR to avoid\n");
776 already_warned = TRUE;
782 _dbus_assert (username != NULL);
784 keyring = _dbus_keyring_new ();
788 /* should have been validated already, but paranoia check here */
789 if (!_dbus_keyring_validate_context (context))
792 dbus_set_error_const (error,
794 "Invalid context in keyring creation");
798 if (!_dbus_string_copy (username, 0,
799 &keyring->username, 0))
802 if (!_dbus_string_copy (&homedir, 0,
803 &keyring->directory, 0))
806 if (!_dbus_concat_dir_and_file (&keyring->directory,
810 if (!_dbus_string_copy (&keyring->directory, 0,
811 &keyring->filename, 0))
814 if (!_dbus_concat_dir_and_file (&keyring->filename,
818 if (!_dbus_string_copy (&keyring->filename, 0,
819 &keyring->filename_lock, 0))
822 if (!_dbus_string_append (&keyring->filename_lock, ".lock"))
825 dbus_error_init (&tmp_error);
826 if (!_dbus_keyring_reload (keyring, FALSE, &tmp_error))
828 _dbus_verbose ("didn't load an existing keyring: %s\n",
830 dbus_error_free (&tmp_error);
833 /* We don't fail fatally if we can't create the directory,
834 * but the keyring will probably always be empty
835 * unless someone else manages to create it
837 dbus_error_init (&tmp_error);
838 if (!_dbus_create_directory (&keyring->directory,
841 _dbus_verbose ("Creating keyring directory: %s\n",
843 dbus_error_free (&tmp_error);
846 _dbus_string_free (&homedir);
852 dbus_set_error_const (error,
853 DBUS_ERROR_NO_MEMORY,
856 _dbus_keyring_unref (keyring);
857 _dbus_string_free (&homedir);
863 * Checks whether the context is a valid context.
864 * Contexts that might cause confusion when used
865 * in filenames are not allowed (contexts can't
866 * start with a dot or contain dir separators).
868 * @todo this is the most inefficient implementation
871 * @param context the context
872 * @returns #TRUE if valid
875 _dbus_keyring_validate_context (const DBusString *context)
877 if (_dbus_string_get_length (context) == 0)
879 _dbus_verbose ("context is zero-length\n");
883 if (!_dbus_string_validate_ascii (context, 0,
884 _dbus_string_get_length (context)))
886 _dbus_verbose ("context not valid ascii\n");
890 /* no directory separators */
891 if (_dbus_string_find (context, 0, "/", NULL))
893 _dbus_verbose ("context contains a slash\n");
897 if (_dbus_string_find (context, 0, "\\", NULL))
899 _dbus_verbose ("context contains a backslash\n");
903 /* prevent attempts to use dotfiles or ".." or ".lock"
904 * all of which might allow some kind of attack
906 if (_dbus_string_find (context, 0, ".", NULL))
908 _dbus_verbose ("context contains a dot\n");
912 /* no spaces/tabs, those are used for separators in the protocol */
913 if (_dbus_string_find_blank (context, 0, NULL))
915 _dbus_verbose ("context contains a blank\n");
919 if (_dbus_string_find (context, 0, "\n", NULL))
921 _dbus_verbose ("context contains a newline\n");
925 if (_dbus_string_find (context, 0, "\r", NULL))
927 _dbus_verbose ("context contains a carriage return\n");
935 find_recent_key (DBusKeyring *keyring)
938 long tv_sec, tv_usec;
940 _dbus_get_current_time (&tv_sec, &tv_usec);
943 while (i < keyring->n_keys)
945 DBusKey *key = &keyring->keys[i];
947 _dbus_verbose ("Key %d is %ld seconds old\n",
948 i, tv_sec - key->creation_time);
950 if ((tv_sec - NEW_KEY_TIMEOUT_SECONDS) < key->creation_time)
960 * Gets a recent key to use for authentication.
961 * If no recent key exists, creates one. Returns
962 * the key ID. If a key can't be written to the keyring
963 * file so no recent key can be created, returns -1.
964 * All valid keys are > 0.
966 * @param keyring the keyring
967 * @param error error on failure
968 * @returns key ID to use for auth, or -1 on failure
971 _dbus_keyring_get_best_key (DBusKeyring *keyring,
976 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
978 key = find_recent_key (keyring);
982 /* All our keys are too old, or we've never loaded the
983 * keyring. Create a new one.
985 if (!_dbus_keyring_reload (keyring, TRUE,
989 key = find_recent_key (keyring);
994 dbus_set_error_const (error,
996 "No recent-enough key found in keyring, and unable to create a new key");
1002 * Checks whether the keyring is for the given username.
1004 * @param keyring the keyring
1005 * @param username the username to check
1007 * @returns #TRUE if the keyring belongs to the given user
1010 _dbus_keyring_is_for_user (DBusKeyring *keyring,
1011 const DBusString *username)
1013 return _dbus_string_equal (&keyring->username,
1018 * Gets the hex-encoded secret key for the given ID.
1019 * Returns #FALSE if not enough memory. Returns #TRUE
1020 * but empty key on any other error such as unknown
1023 * @param keyring the keyring
1024 * @param key_id the key ID
1025 * @param hex_key string to append hex-encoded key to
1026 * @returns #TRUE if we had enough memory
1029 _dbus_keyring_get_hex_key (DBusKeyring *keyring,
1031 DBusString *hex_key)
1035 key = find_key_by_id (keyring->keys,
1039 return TRUE; /* had enough memory, so TRUE */
1041 return _dbus_string_hex_encode (&key->secret, 0,
1043 _dbus_string_get_length (hex_key));
1046 /** @} */ /* end of exposed API */
1048 #ifdef DBUS_BUILD_TESTS
1049 #include "dbus-test.h"
1053 _dbus_keyring_test (void)
1065 /* Context validation */
1067 _dbus_string_init_const (&context, "foo");
1068 _dbus_assert (_dbus_keyring_validate_context (&context));
1069 _dbus_string_init_const (&context, "org_freedesktop_blah");
1070 _dbus_assert (_dbus_keyring_validate_context (&context));
1072 _dbus_string_init_const (&context, "");
1073 _dbus_assert (!_dbus_keyring_validate_context (&context));
1074 _dbus_string_init_const (&context, ".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, "bar/foo");
1079 _dbus_assert (!_dbus_keyring_validate_context (&context));
1080 _dbus_string_init_const (&context, "bar\\foo");
1081 _dbus_assert (!_dbus_keyring_validate_context (&context));
1082 _dbus_string_init_const (&context, "foo\xfa\xf0");
1083 _dbus_assert (!_dbus_keyring_validate_context (&context));
1084 _dbus_string_init_const (&context, "foo\x80");
1085 _dbus_assert (!_dbus_keyring_validate_context (&context));
1086 _dbus_string_init_const (&context, "foo\x7f");
1087 _dbus_assert (_dbus_keyring_validate_context (&context));
1088 _dbus_string_init_const (&context, "foo bar");
1089 _dbus_assert (!_dbus_keyring_validate_context (&context));
1091 if (!_dbus_string_init (&context))
1092 _dbus_assert_not_reached ("no memory");
1093 if (!_dbus_string_append_byte (&context, '\0'))
1094 _dbus_assert_not_reached ("no memory");
1095 _dbus_assert (!_dbus_keyring_validate_context (&context));
1096 _dbus_string_free (&context);
1098 /* Now verify that if we create a key in keyring 1,
1099 * it is properly loaded in keyring 2
1102 _dbus_string_init_const (&context, "org_freedesktop_dbus_testsuite");
1103 dbus_error_init (&error);
1104 ring1 = _dbus_keyring_new_homedir (NULL, &context,
1106 _dbus_assert (ring1);
1107 _dbus_assert (error.name == NULL);
1109 id = _dbus_keyring_get_best_key (ring1, &error);
1112 fprintf (stderr, "Could not load keyring: %s\n", error.message);
1113 dbus_error_free (&error);
1117 ring2 = _dbus_keyring_new_homedir (NULL, &context, &error);
1118 _dbus_assert (ring2);
1119 _dbus_assert (error.name == NULL);
1121 if (ring1->n_keys != ring2->n_keys)
1123 fprintf (stderr, "Different number of keys in keyrings\n");
1127 /* We guarantee we load and save keeping keys in a fixed
1131 while (i < ring1->n_keys)
1133 if (ring1->keys[i].id != ring2->keys[i].id)
1135 fprintf (stderr, "Keyring 1 has first key ID %d and keyring 2 has %d\n",
1136 ring1->keys[i].id, ring2->keys[i].id);
1140 if (ring1->keys[i].creation_time != ring2->keys[i].creation_time)
1142 fprintf (stderr, "Keyring 1 has first key time %ld and keyring 2 has %ld\n",
1143 ring1->keys[i].creation_time, ring2->keys[i].creation_time);
1147 if (!_dbus_string_equal (&ring1->keys[i].secret,
1148 &ring2->keys[i].secret))
1150 fprintf (stderr, "Keyrings 1 and 2 have different secrets for same ID/timestamp\n");
1157 printf (" %d keys in test\n", ring1->n_keys);
1159 /* Test ref/unref */
1160 _dbus_keyring_ref (ring1);
1161 _dbus_keyring_ref (ring2);
1162 _dbus_keyring_unref (ring1);
1163 _dbus_keyring_unref (ring2);
1167 _dbus_keyring_unref (ring1);
1168 _dbus_keyring_unref (ring2);
1174 _dbus_keyring_unref (ring1);
1176 _dbus_keyring_unref (ring2);
1181 #endif /* DBUS_BUILD_TESTS */