D-Bus  1.6.4
dbus-keyring.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-keyring.c Store secret cookies in your homedir
3  *
4  * Copyright (C) 2003, 2004 Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23 
24 #include <config.h>
25 #include "dbus-keyring.h"
26 #include "dbus-protocol.h"
27 #include <dbus/dbus-string.h>
28 #include <dbus/dbus-list.h>
29 #include <dbus/dbus-sysdeps.h>
30 
67 #define NEW_KEY_TIMEOUT_SECONDS (60*5)
68 
73 #define EXPIRE_KEYS_TIMEOUT_SECONDS (NEW_KEY_TIMEOUT_SECONDS + (60*2))
74 
77 #define MAX_TIME_TRAVEL_SECONDS (60*5)
78 
83 #ifdef DBUS_BUILD_TESTS
84 #define MAX_KEYS_IN_FILE 10
85 #else
86 #define MAX_KEYS_IN_FILE 256
87 #endif
88 
92 typedef struct
93 {
94  dbus_int32_t id;
96  long creation_time;
103 } DBusKey;
104 
112 {
113  int refcount;
118  int n_keys;
120 };
121 
122 static DBusKeyring*
123 _dbus_keyring_new (void)
124 {
125  DBusKeyring *keyring;
126 
127  keyring = dbus_new0 (DBusKeyring, 1);
128  if (keyring == NULL)
129  goto out_0;
130 
131  if (!_dbus_string_init (&keyring->directory))
132  goto out_1;
133 
134  if (!_dbus_string_init (&keyring->filename))
135  goto out_2;
136 
137  if (!_dbus_string_init (&keyring->filename_lock))
138  goto out_3;
139 
140  keyring->refcount = 1;
141  keyring->keys = NULL;
142  keyring->n_keys = 0;
143 
144  return keyring;
145 
146  out_3:
147  _dbus_string_free (&keyring->filename);
148  out_2:
149  _dbus_string_free (&keyring->directory);
150  out_1:
151  dbus_free (keyring);
152  out_0:
153  return NULL;
154 }
155 
156 static void
157 free_keys (DBusKey *keys,
158  int n_keys)
159 {
160  int i;
161 
162  /* should be safe for args NULL, 0 */
163 
164  i = 0;
165  while (i < n_keys)
166  {
167  _dbus_string_free (&keys[i].secret);
168  ++i;
169  }
170 
171  dbus_free (keys);
172 }
173 
174 /* Our locking scheme is highly unreliable. However, there is
175  * unfortunately no reliable locking scheme in user home directories;
176  * between bugs in Linux NFS, people using Tru64 or other total crap
177  * NFS, AFS, random-file-system-of-the-week, and so forth, fcntl() in
178  * homedirs simply generates tons of bug reports. This has been
179  * learned through hard experience with GConf, unfortunately.
180  *
181  * This bad hack might work better for the kind of lock we have here,
182  * which we don't expect to hold for any length of time. Crashing
183  * while we hold it should be unlikely, and timing out such that we
184  * delete a stale lock should also be unlikely except when the
185  * filesystem is running really slowly. Stuff might break in corner
186  * cases but as long as it's not a security-level breakage it should
187  * be OK.
188  */
189 
191 #define MAX_LOCK_TIMEOUTS 32
192 
193 #define LOCK_TIMEOUT_MILLISECONDS 250
194 
195 static dbus_bool_t
196 _dbus_keyring_lock (DBusKeyring *keyring)
197 {
198  int n_timeouts;
199 
200  n_timeouts = 0;
201  while (n_timeouts < MAX_LOCK_TIMEOUTS)
202  {
203  DBusError error = DBUS_ERROR_INIT;
204 
206  &error))
207  break;
208 
209  _dbus_verbose ("Did not get lock file, sleeping %d milliseconds (%s)\n",
211  dbus_error_free (&error);
212 
214 
215  ++n_timeouts;
216  }
217 
218  if (n_timeouts == MAX_LOCK_TIMEOUTS)
219  {
220  DBusError error = DBUS_ERROR_INIT;
221 
222  _dbus_verbose ("Lock file timed out %d times, assuming stale\n",
223  n_timeouts);
224 
225  if (!_dbus_delete_file (&keyring->filename_lock, &error))
226  {
227  _dbus_verbose ("Couldn't delete old lock file: %s\n",
228  error.message);
229  dbus_error_free (&error);
230  return FALSE;
231  }
232 
234  &error))
235  {
236  _dbus_verbose ("Couldn't create lock file after deleting stale one: %s\n",
237  error.message);
238  dbus_error_free (&error);
239  return FALSE;
240  }
241  }
242 
243  return TRUE;
244 }
245 
246 static void
247 _dbus_keyring_unlock (DBusKeyring *keyring)
248 {
249  DBusError error = DBUS_ERROR_INIT;
250 
251  if (!_dbus_delete_file (&keyring->filename_lock, &error))
252  {
253  _dbus_warn ("Failed to delete lock file: %s\n",
254  error.message);
255  dbus_error_free (&error);
256  }
257 }
258 
259 static DBusKey*
260 find_key_by_id (DBusKey *keys,
261  int n_keys,
262  int id)
263 {
264  int i;
265 
266  i = 0;
267  while (i < n_keys)
268  {
269  if (keys[i].id == id)
270  return &keys[i];
271 
272  ++i;
273  }
274 
275  return NULL;
276 }
277 
278 static dbus_bool_t
279 add_new_key (DBusKey **keys_p,
280  int *n_keys_p,
281  DBusError *error)
282 {
283  DBusKey *new;
284  DBusString bytes;
285  int id;
286  long timestamp;
287  const unsigned char *s;
288  dbus_bool_t retval;
289  DBusKey *keys;
290  int n_keys;
291 
292  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
293 
294  if (!_dbus_string_init (&bytes))
295  {
297  return FALSE;
298  }
299 
300  keys = *keys_p;
301  n_keys = *n_keys_p;
302  retval = FALSE;
303 
304  /* Generate an integer ID and then the actual key. */
305  retry:
306 
307  if (!_dbus_generate_random_bytes (&bytes, 4))
308  {
310  goto out;
311  }
312 
313  s = (const unsigned char*) _dbus_string_get_const_data (&bytes);
314 
315  id = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24);
316  if (id < 0)
317  id = - id;
318  _dbus_assert (id >= 0);
319 
320  if (find_key_by_id (keys, n_keys, id) != NULL)
321  {
322  _dbus_string_set_length (&bytes, 0);
323  _dbus_verbose ("Key ID %d already existed, trying another one\n",
324  id);
325  goto retry;
326  }
327 
328  _dbus_verbose ("Creating key with ID %d\n", id);
329 
330 #define KEY_LENGTH_BYTES 24
331  _dbus_string_set_length (&bytes, 0);
332  if (!_dbus_generate_random_bytes (&bytes, KEY_LENGTH_BYTES))
333  {
335  goto out;
336  }
337 
338  new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
339  if (new == NULL)
340  {
342  goto out;
343  }
344 
345  keys = new;
346  *keys_p = keys; /* otherwise *keys_p ends up invalid */
347  n_keys += 1;
348 
349  if (!_dbus_string_init (&keys[n_keys-1].secret))
350  {
351  n_keys -= 1; /* we don't want to free the one we didn't init */
353  goto out;
354  }
355 
356  _dbus_get_real_time (&timestamp, NULL);
357 
358  keys[n_keys-1].id = id;
359  keys[n_keys-1].creation_time = timestamp;
360  if (!_dbus_string_move (&bytes, 0,
361  &keys[n_keys-1].secret,
362  0))
363  {
365  _dbus_string_free (&keys[n_keys-1].secret);
366  n_keys -= 1;
367  goto out;
368  }
369 
370  retval = TRUE;
371 
372  out:
373  *n_keys_p = n_keys;
374 
375  _dbus_string_free (&bytes);
376  return retval;
377 }
378 
393 static dbus_bool_t
394 _dbus_keyring_reload (DBusKeyring *keyring,
395  dbus_bool_t add_new,
396  DBusError *error)
397 {
398  DBusString contents;
399  DBusString line;
400  dbus_bool_t retval;
401  dbus_bool_t have_lock;
402  DBusKey *keys;
403  int n_keys;
404  int i;
405  long now;
406  DBusError tmp_error;
407 
408  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
409 
410  if (!_dbus_check_dir_is_private_to_user (&keyring->directory, error))
411  return FALSE;
412 
413  if (!_dbus_string_init (&contents))
414  {
416  return FALSE;
417  }
418 
419  if (!_dbus_string_init (&line))
420  {
422  _dbus_string_free (&contents);
423  return FALSE;
424  }
425 
426  keys = NULL;
427  n_keys = 0;
428  retval = FALSE;
429  have_lock = FALSE;
430 
431  _dbus_get_real_time (&now, NULL);
432 
433  if (add_new)
434  {
435  if (!_dbus_keyring_lock (keyring))
436  {
438  "Could not lock keyring file to add to it");
439  goto out;
440  }
441 
442  have_lock = TRUE;
443  }
444 
445  dbus_error_init (&tmp_error);
446  if (!_dbus_file_get_contents (&contents,
447  &keyring->filename,
448  &tmp_error))
449  {
450  _dbus_verbose ("Failed to load keyring file: %s\n",
451  tmp_error.message);
452  /* continue with empty keyring file, so we recreate it */
453  dbus_error_free (&tmp_error);
454  }
455 
456  if (!_dbus_string_validate_ascii (&contents, 0,
457  _dbus_string_get_length (&contents)))
458  {
459  _dbus_warn ("Secret keyring file contains non-ASCII! Ignoring existing contents\n");
460  _dbus_string_set_length (&contents, 0);
461  }
462 
463  /* FIXME this is badly inefficient for large keyring files
464  * (not that large keyring files exist outside of test suites)
465  */
466  while (_dbus_string_pop_line (&contents, &line))
467  {
468  int next;
469  long val;
470  int id;
471  long timestamp;
472  int len;
473  int end;
474  DBusKey *new;
475 
476  /* Don't load more than the max. */
477  if (n_keys >= (add_new ? MAX_KEYS_IN_FILE - 1 : MAX_KEYS_IN_FILE))
478  break;
479 
480  next = 0;
481  if (!_dbus_string_parse_int (&line, 0, &val, &next))
482  {
483  _dbus_verbose ("could not parse secret key ID at start of line\n");
484  continue;
485  }
486 
487  if (val > _DBUS_INT32_MAX || val < 0)
488  {
489  _dbus_verbose ("invalid secret key ID at start of line\n");
490  continue;
491  }
492 
493  id = val;
494 
495  _dbus_string_skip_blank (&line, next, &next);
496 
497  if (!_dbus_string_parse_int (&line, next, &timestamp, &next))
498  {
499  _dbus_verbose ("could not parse secret key timestamp\n");
500  continue;
501  }
502 
503  if (timestamp < 0 ||
504  (now + MAX_TIME_TRAVEL_SECONDS) < timestamp ||
505  (now - EXPIRE_KEYS_TIMEOUT_SECONDS) > timestamp)
506  {
507  _dbus_verbose ("dropping/ignoring %ld-seconds old key with timestamp %ld as current time is %ld\n",
508  now - timestamp, timestamp, now);
509  continue;
510  }
511 
512  _dbus_string_skip_blank (&line, next, &next);
513 
514  len = _dbus_string_get_length (&line);
515 
516  if ((len - next) == 0)
517  {
518  _dbus_verbose ("no secret key after ID and timestamp\n");
519  continue;
520  }
521 
522  /* We have all three parts */
523  new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
524  if (new == NULL)
525  {
527  goto out;
528  }
529 
530  keys = new;
531  n_keys += 1;
532 
533  if (!_dbus_string_init (&keys[n_keys-1].secret))
534  {
535  n_keys -= 1; /* we don't want to free the one we didn't init */
537  goto out;
538  }
539 
540  keys[n_keys-1].id = id;
541  keys[n_keys-1].creation_time = timestamp;
542  if (!_dbus_string_hex_decode (&line, next, &end,
543  &keys[n_keys-1].secret, 0))
544  {
546  goto out;
547  }
548 
549  if (_dbus_string_get_length (&line) != end)
550  {
551  _dbus_verbose ("invalid hex encoding in keyring file\n");
552  _dbus_string_free (&keys[n_keys - 1].secret);
553  n_keys -= 1;
554  continue;
555  }
556  }
557 
558  _dbus_verbose ("Successfully loaded %d existing keys\n",
559  n_keys);
560 
561  if (add_new)
562  {
563  if (!add_new_key (&keys, &n_keys, error))
564  {
565  _dbus_verbose ("Failed to generate new key: %s\n",
566  error ? error->message : "(unknown)");
567  goto out;
568  }
569 
570  _dbus_string_set_length (&contents, 0);
571 
572  i = 0;
573  while (i < n_keys)
574  {
575  if (!_dbus_string_append_int (&contents,
576  keys[i].id))
577  goto nomem;
578 
579  if (!_dbus_string_append_byte (&contents, ' '))
580  goto nomem;
581 
582  if (!_dbus_string_append_int (&contents,
583  keys[i].creation_time))
584  goto nomem;
585 
586  if (!_dbus_string_append_byte (&contents, ' '))
587  goto nomem;
588 
589  if (!_dbus_string_hex_encode (&keys[i].secret, 0,
590  &contents,
591  _dbus_string_get_length (&contents)))
592  goto nomem;
593 
594  if (!_dbus_string_append_byte (&contents, '\n'))
595  goto nomem;
596 
597  ++i;
598  continue;
599 
600  nomem:
602  goto out;
603  }
604 
605  if (!_dbus_string_save_to_file (&contents, &keyring->filename,
606  FALSE, error))
607  goto out;
608  }
609 
610  if (keyring->keys)
611  free_keys (keyring->keys, keyring->n_keys);
612  keyring->keys = keys;
613  keyring->n_keys = n_keys;
614  keys = NULL;
615  n_keys = 0;
616 
617  retval = TRUE;
618 
619  out:
620  if (have_lock)
621  _dbus_keyring_unlock (keyring);
622 
623  if (! ((retval == TRUE && (error == NULL || error->name == NULL)) ||
624  (retval == FALSE && (error == NULL || error->name != NULL))))
625  {
626  if (error && error->name)
627  _dbus_verbose ("error is %s: %s\n", error->name, error->message);
628  _dbus_warn ("returning %d but error pointer %p name %s\n",
629  retval, error, error->name ? error->name : "(none)");
630  _dbus_assert_not_reached ("didn't handle errors properly");
631  }
632 
633  if (keys != NULL)
634  {
635  i = 0;
636  while (i < n_keys)
637  {
638  _dbus_string_zero (&keys[i].secret);
639  _dbus_string_free (&keys[i].secret);
640  ++i;
641  }
642 
643  dbus_free (keys);
644  }
645 
646  _dbus_string_free (&contents);
647  _dbus_string_free (&line);
648 
649  return retval;
650 }
651  /* end of internals */
653 
666 DBusKeyring *
668 {
669  keyring->refcount += 1;
670 
671  return keyring;
672 }
673 
680 void
682 {
683  keyring->refcount -= 1;
684 
685  if (keyring->refcount == 0)
686  {
687  if (keyring->credentials)
689 
690  _dbus_string_free (&keyring->filename);
691  _dbus_string_free (&keyring->filename_lock);
692  _dbus_string_free (&keyring->directory);
693  free_keys (keyring->keys, keyring->n_keys);
694  dbus_free (keyring);
695  }
696 }
697 
710  const DBusString *context,
711  DBusError *error)
712 {
713  DBusString ringdir;
714  DBusKeyring *keyring;
715  dbus_bool_t error_set;
716  DBusError tmp_error;
717  DBusCredentials *our_credentials;
718 
719  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
720 
721  keyring = NULL;
722  error_set = FALSE;
723  our_credentials = NULL;
724 
725  if (!_dbus_string_init (&ringdir))
726  {
728  return NULL;
729  }
730 
731  if (credentials != NULL)
732  {
733  our_credentials = _dbus_credentials_copy (credentials);
734  }
735  else
736  {
737  our_credentials = _dbus_credentials_new_from_current_process ();
738  }
739 
740  if (our_credentials == NULL)
741  goto failed;
742 
743  if (_dbus_credentials_are_anonymous (our_credentials))
744  {
745  if (!_dbus_credentials_add_from_current_process (our_credentials))
746  goto failed;
747  }
748 
750  our_credentials))
751  goto failed;
752 
753  keyring = _dbus_keyring_new ();
754  if (keyring == NULL)
755  goto failed;
756 
757  _dbus_assert (keyring->credentials == NULL);
758  keyring->credentials = our_credentials;
759  our_credentials = NULL; /* so we don't unref it again later */
760 
761  /* should have been validated already, but paranoia check here */
762  if (!_dbus_keyring_validate_context (context))
763  {
764  error_set = TRUE;
765  dbus_set_error_const (error,
767  "Invalid context in keyring creation");
768  goto failed;
769  }
770 
771  /* Save keyring dir in the keyring object */
772  if (!_dbus_string_copy (&ringdir, 0,
773  &keyring->directory, 0))
774  goto failed;
775 
776  /* Create keyring->filename based on keyring dir and context */
777  if (!_dbus_string_copy (&keyring->directory, 0,
778  &keyring->filename, 0))
779  goto failed;
780 
781  if (!_dbus_concat_dir_and_file (&keyring->filename,
782  context))
783  goto failed;
784 
785  /* Create lockfile name */
786  if (!_dbus_string_copy (&keyring->filename, 0,
787  &keyring->filename_lock, 0))
788  goto failed;
789 
790  if (!_dbus_string_append (&keyring->filename_lock, ".lock"))
791  goto failed;
792 
793  /* Reload keyring */
794  dbus_error_init (&tmp_error);
795  if (!_dbus_keyring_reload (keyring, FALSE, &tmp_error))
796  {
797  _dbus_verbose ("didn't load an existing keyring: %s\n",
798  tmp_error.message);
799  dbus_error_free (&tmp_error);
800  }
801 
802  /* We don't fail fatally if we can't create the directory,
803  * but the keyring will probably always be empty
804  * unless someone else manages to create it
805  */
806  dbus_error_init (&tmp_error);
807  if (!_dbus_create_directory (&keyring->directory,
808  &tmp_error))
809  {
810  _dbus_verbose ("Creating keyring directory: %s\n",
811  tmp_error.message);
812  dbus_error_free (&tmp_error);
813  }
814 
815  _dbus_string_free (&ringdir);
816 
817  return keyring;
818 
819  failed:
820  if (!error_set)
821  dbus_set_error_const (error,
823  NULL);
824  if (our_credentials)
825  _dbus_credentials_unref (our_credentials);
826  if (keyring)
827  _dbus_keyring_unref (keyring);
828  _dbus_string_free (&ringdir);
829  return NULL;
830 
831 }
832 
847 {
848  if (_dbus_string_get_length (context) == 0)
849  {
850  _dbus_verbose ("context is zero-length\n");
851  return FALSE;
852  }
853 
854  if (!_dbus_string_validate_ascii (context, 0,
855  _dbus_string_get_length (context)))
856  {
857  _dbus_verbose ("context not valid ascii\n");
858  return FALSE;
859  }
860 
861  /* no directory separators */
862  if (_dbus_string_find (context, 0, "/", NULL))
863  {
864  _dbus_verbose ("context contains a slash\n");
865  return FALSE;
866  }
867 
868  if (_dbus_string_find (context, 0, "\\", NULL))
869  {
870  _dbus_verbose ("context contains a backslash\n");
871  return FALSE;
872  }
873 
874  /* prevent attempts to use dotfiles or ".." or ".lock"
875  * all of which might allow some kind of attack
876  */
877  if (_dbus_string_find (context, 0, ".", NULL))
878  {
879  _dbus_verbose ("context contains a dot\n");
880  return FALSE;
881  }
882 
883  /* no spaces/tabs, those are used for separators in the protocol */
884  if (_dbus_string_find_blank (context, 0, NULL))
885  {
886  _dbus_verbose ("context contains a blank\n");
887  return FALSE;
888  }
889 
890  if (_dbus_string_find (context, 0, "\n", NULL))
891  {
892  _dbus_verbose ("context contains a newline\n");
893  return FALSE;
894  }
895 
896  if (_dbus_string_find (context, 0, "\r", NULL))
897  {
898  _dbus_verbose ("context contains a carriage return\n");
899  return FALSE;
900  }
901 
902  return TRUE;
903 }
904 
905 static DBusKey*
906 find_recent_key (DBusKeyring *keyring)
907 {
908  int i;
909  long tv_sec, tv_usec;
910 
911  _dbus_get_real_time (&tv_sec, &tv_usec);
912 
913  i = 0;
914  while (i < keyring->n_keys)
915  {
916  DBusKey *key = &keyring->keys[i];
917 
918  _dbus_verbose ("Key %d is %ld seconds old\n",
919  i, tv_sec - key->creation_time);
920 
921  if ((tv_sec - NEW_KEY_TIMEOUT_SECONDS) < key->creation_time)
922  return key;
923 
924  ++i;
925  }
926 
927  return NULL;
928 }
929 
941 int
943  DBusError *error)
944 {
945  DBusKey *key;
946 
947  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
948 
949  key = find_recent_key (keyring);
950  if (key)
951  return key->id;
952 
953  /* All our keys are too old, or we've never loaded the
954  * keyring. Create a new one.
955  */
956  if (!_dbus_keyring_reload (keyring, TRUE,
957  error))
958  return -1;
959 
960  key = find_recent_key (keyring);
961  if (key)
962  return key->id;
963  else
964  {
965  dbus_set_error_const (error,
967  "No recent-enough key found in keyring, and unable to create a new key");
968  return -1;
969  }
970 }
971 
982  DBusCredentials *credentials)
983 {
984  return _dbus_credentials_same_user (keyring->credentials,
985  credentials);
986 }
987 
1001  int key_id,
1002  DBusString *hex_key)
1003 {
1004  DBusKey *key;
1005 
1006  key = find_key_by_id (keyring->keys,
1007  keyring->n_keys,
1008  key_id);
1009  if (key == NULL)
1010  return TRUE; /* had enough memory, so TRUE */
1011 
1012  return _dbus_string_hex_encode (&key->secret, 0,
1013  hex_key,
1014  _dbus_string_get_length (hex_key));
1015 }
1016  /* end of exposed API */
1018 
1019 #ifdef DBUS_BUILD_TESTS
1020 #include "dbus-test.h"
1021 #include <stdio.h>
1022 
1024 _dbus_keyring_test (void)
1025 {
1026  DBusString context;
1027  DBusKeyring *ring1;
1028  DBusKeyring *ring2;
1029  int id;
1030  DBusError error;
1031  int i;
1032 
1033  ring1 = NULL;
1034  ring2 = NULL;
1035 
1036  /* Context validation */
1037 
1038  _dbus_string_init_const (&context, "foo");
1040  _dbus_string_init_const (&context, "org_freedesktop_blah");
1042 
1043  _dbus_string_init_const (&context, "");
1045  _dbus_string_init_const (&context, ".foo");
1047  _dbus_string_init_const (&context, "bar.foo");
1049  _dbus_string_init_const (&context, "bar/foo");
1051  _dbus_string_init_const (&context, "bar\\foo");
1053  _dbus_string_init_const (&context, "foo\xfa\xf0");
1055  _dbus_string_init_const (&context, "foo\x80");
1057  _dbus_string_init_const (&context, "foo\x7f");
1059  _dbus_string_init_const (&context, "foo bar");
1061 
1062  if (!_dbus_string_init (&context))
1063  _dbus_assert_not_reached ("no memory");
1064  if (!_dbus_string_append_byte (&context, '\0'))
1065  _dbus_assert_not_reached ("no memory");
1067  _dbus_string_free (&context);
1068 
1069  /* Now verify that if we create a key in keyring 1,
1070  * it is properly loaded in keyring 2
1071  */
1072 
1073  _dbus_string_init_const (&context, "org_freedesktop_dbus_testsuite");
1074  dbus_error_init (&error);
1075  ring1 = _dbus_keyring_new_for_credentials (NULL, &context,
1076  &error);
1077  _dbus_assert (ring1 != NULL);
1078  _dbus_assert (error.name == NULL);
1079 
1080  id = _dbus_keyring_get_best_key (ring1, &error);
1081  if (id < 0)
1082  {
1083  fprintf (stderr, "Could not load keyring: %s\n", error.message);
1084  dbus_error_free (&error);
1085  goto failure;
1086  }
1087 
1088  ring2 = _dbus_keyring_new_for_credentials (NULL, &context, &error);
1089  _dbus_assert (ring2 != NULL);
1090  _dbus_assert (error.name == NULL);
1091 
1092  if (ring1->n_keys != ring2->n_keys)
1093  {
1094  fprintf (stderr, "Different number of keys in keyrings\n");
1095  goto failure;
1096  }
1097 
1098  /* We guarantee we load and save keeping keys in a fixed
1099  * order
1100  */
1101  i = 0;
1102  while (i < ring1->n_keys)
1103  {
1104  if (ring1->keys[i].id != ring2->keys[i].id)
1105  {
1106  fprintf (stderr, "Keyring 1 has first key ID %d and keyring 2 has %d\n",
1107  ring1->keys[i].id, ring2->keys[i].id);
1108  goto failure;
1109  }
1110 
1111  if (ring1->keys[i].creation_time != ring2->keys[i].creation_time)
1112  {
1113  fprintf (stderr, "Keyring 1 has first key time %ld and keyring 2 has %ld\n",
1114  ring1->keys[i].creation_time, ring2->keys[i].creation_time);
1115  goto failure;
1116  }
1117 
1118  if (!_dbus_string_equal (&ring1->keys[i].secret,
1119  &ring2->keys[i].secret))
1120  {
1121  fprintf (stderr, "Keyrings 1 and 2 have different secrets for same ID/timestamp\n");
1122  goto failure;
1123  }
1124 
1125  ++i;
1126  }
1127 
1128  printf (" %d keys in test\n", ring1->n_keys);
1129 
1130  /* Test ref/unref */
1131  _dbus_keyring_ref (ring1);
1132  _dbus_keyring_ref (ring2);
1133  _dbus_keyring_unref (ring1);
1134  _dbus_keyring_unref (ring2);
1135 
1136 
1137  /* really unref */
1138  _dbus_keyring_unref (ring1);
1139  _dbus_keyring_unref (ring2);
1140 
1141  return TRUE;
1142 
1143  failure:
1144  if (ring1)
1145  _dbus_keyring_unref (ring1);
1146  if (ring2)
1147  _dbus_keyring_unref (ring2);
1148 
1149  return FALSE;
1150 }
1151 
1152 #endif /* DBUS_BUILD_TESTS */
1153