D-Bus  1.13.7
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 #include <dbus/dbus-test-tap.h>
31 
68 #define NEW_KEY_TIMEOUT_SECONDS (60*5)
69 
74 #define EXPIRE_KEYS_TIMEOUT_SECONDS (NEW_KEY_TIMEOUT_SECONDS + (60*2))
75 
78 #define MAX_TIME_TRAVEL_SECONDS (60*5)
79 
84 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
85 #define MAX_KEYS_IN_FILE 10
86 #else
87 #define MAX_KEYS_IN_FILE 256
88 #endif
89 
93 typedef struct
94 {
95  dbus_int32_t id;
104 } DBusKey;
105 
113 {
114  int refcount;
119  int n_keys;
121 };
122 
123 static DBusKeyring*
124 _dbus_keyring_new (void)
125 {
126  DBusKeyring *keyring;
127 
128  keyring = dbus_new0 (DBusKeyring, 1);
129  if (keyring == NULL)
130  goto out_0;
131 
132  if (!_dbus_string_init (&keyring->directory))
133  goto out_1;
134 
135  if (!_dbus_string_init (&keyring->filename))
136  goto out_2;
137 
138  if (!_dbus_string_init (&keyring->filename_lock))
139  goto out_3;
140 
141  keyring->refcount = 1;
142  keyring->keys = NULL;
143  keyring->n_keys = 0;
144 
145  return keyring;
146 
147  out_3:
148  _dbus_string_free (&keyring->filename);
149  out_2:
150  _dbus_string_free (&keyring->directory);
151  out_1:
152  dbus_free (keyring);
153  out_0:
154  return NULL;
155 }
156 
157 static void
158 free_keys (DBusKey *keys,
159  int n_keys)
160 {
161  int i;
162 
163  /* should be safe for args NULL, 0 */
164 
165  i = 0;
166  while (i < n_keys)
167  {
168  _dbus_string_free (&keys[i].secret);
169  ++i;
170  }
171 
172  dbus_free (keys);
173 }
174 
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.
181  *
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
188  * be OK.
189  */
190 
192 #define MAX_LOCK_TIMEOUTS 32
193 
194 #define LOCK_TIMEOUT_MILLISECONDS 250
195 
196 static dbus_bool_t
197 _dbus_keyring_lock (DBusKeyring *keyring)
198 {
199  int n_timeouts;
200 
201  n_timeouts = 0;
202  while (n_timeouts < MAX_LOCK_TIMEOUTS)
203  {
204  DBusError error = DBUS_ERROR_INIT;
205 
207  &error))
208  break;
209 
210  _dbus_verbose ("Did not get lock file, sleeping %d milliseconds (%s)\n",
212  dbus_error_free (&error);
213 
215 
216  ++n_timeouts;
217  }
218 
219  if (n_timeouts == MAX_LOCK_TIMEOUTS)
220  {
221  DBusError error = DBUS_ERROR_INIT;
222 
223  _dbus_verbose ("Lock file timed out %d times, assuming stale\n",
224  n_timeouts);
225 
226  if (!_dbus_delete_file (&keyring->filename_lock, &error))
227  {
228  _dbus_verbose ("Couldn't delete old lock file: %s\n",
229  error.message);
230  dbus_error_free (&error);
231  return FALSE;
232  }
233 
235  &error))
236  {
237  _dbus_verbose ("Couldn't create lock file after deleting stale one: %s\n",
238  error.message);
239  dbus_error_free (&error);
240  return FALSE;
241  }
242  }
243 
244  return TRUE;
245 }
246 
247 static void
248 _dbus_keyring_unlock (DBusKeyring *keyring)
249 {
250  DBusError error = DBUS_ERROR_INIT;
251 
252  if (!_dbus_delete_file (&keyring->filename_lock, &error))
253  {
254  _dbus_warn ("Failed to delete lock file: %s",
255  error.message);
256  dbus_error_free (&error);
257  }
258 }
259 
260 static DBusKey*
261 find_key_by_id (DBusKey *keys,
262  int n_keys,
263  int id)
264 {
265  int i;
266 
267  i = 0;
268  while (i < n_keys)
269  {
270  if (keys[i].id == id)
271  return &keys[i];
272 
273  ++i;
274  }
275 
276  return NULL;
277 }
278 
279 static dbus_bool_t
280 add_new_key (DBusKey **keys_p,
281  int *n_keys_p,
282  DBusError *error)
283 {
284  DBusKey *new;
285  DBusString bytes;
286  int id;
287  long timestamp;
288  const unsigned char *s;
289  dbus_bool_t retval;
290  DBusKey *keys;
291  int n_keys;
292 
293  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
294 
295  if (!_dbus_string_init (&bytes))
296  {
298  return FALSE;
299  }
300 
301  keys = *keys_p;
302  n_keys = *n_keys_p;
303  retval = FALSE;
304 
305  /* Generate an integer ID and then the actual key. */
306  retry:
307 
308  if (!_dbus_generate_random_bytes (&bytes, 4, error))
309  goto out;
310 
311  s = (const unsigned char*) _dbus_string_get_const_data (&bytes);
312 
313  id = s[0] | (s[1] << 8) | (s[2] << 16) | ((s[3] & 0x7f) << 24);
314  _dbus_assert (id >= 0);
315 
316  if (find_key_by_id (keys, n_keys, id) != NULL)
317  {
318  _dbus_string_set_length (&bytes, 0);
319  _dbus_verbose ("Key ID %d already existed, trying another one\n",
320  id);
321  goto retry;
322  }
323 
324  _dbus_verbose ("Creating key with ID %d\n", id);
325 
326 #define KEY_LENGTH_BYTES 24
327  _dbus_string_set_length (&bytes, 0);
328  if (!_dbus_generate_random_bytes (&bytes, KEY_LENGTH_BYTES, error))
329  {
330  goto out;
331  }
332 
333  new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
334  if (new == NULL)
335  {
337  goto out;
338  }
339 
340  keys = new;
341  *keys_p = keys; /* otherwise *keys_p ends up invalid */
342  n_keys += 1;
343 
344  if (!_dbus_string_init (&keys[n_keys-1].secret))
345  {
346  n_keys -= 1; /* we don't want to free the one we didn't init */
348  goto out;
349  }
350 
351  _dbus_get_real_time (&timestamp, NULL);
352 
353  keys[n_keys-1].id = id;
354  keys[n_keys-1].creation_time = timestamp;
355  if (!_dbus_string_move (&bytes, 0,
356  &keys[n_keys-1].secret,
357  0))
358  {
360  _dbus_string_free (&keys[n_keys-1].secret);
361  n_keys -= 1;
362  goto out;
363  }
364 
365  retval = TRUE;
366 
367  out:
368  *n_keys_p = n_keys;
369 
370  _dbus_string_free (&bytes);
371  return retval;
372 }
373 
388 static dbus_bool_t
389 _dbus_keyring_reload (DBusKeyring *keyring,
390  dbus_bool_t add_new,
391  DBusError *error)
392 {
393  DBusString contents;
394  DBusString line;
395  dbus_bool_t retval;
396  dbus_bool_t have_lock;
397  DBusKey *keys;
398  int n_keys;
399  int i;
400  long now;
401  DBusError tmp_error;
402 
403  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
404 
405  if (!_dbus_check_dir_is_private_to_user (&keyring->directory, error))
406  return FALSE;
407 
408  if (!_dbus_string_init (&contents))
409  {
411  return FALSE;
412  }
413 
414  if (!_dbus_string_init (&line))
415  {
417  _dbus_string_free (&contents);
418  return FALSE;
419  }
420 
421  keys = NULL;
422  n_keys = 0;
423  retval = FALSE;
424  have_lock = FALSE;
425 
426  _dbus_get_real_time (&now, NULL);
427 
428  if (add_new)
429  {
430  if (!_dbus_keyring_lock (keyring))
431  {
433  "Could not lock keyring file to add to it");
434  goto out;
435  }
436 
437  have_lock = TRUE;
438  }
439 
440  dbus_error_init (&tmp_error);
441  if (!_dbus_file_get_contents (&contents,
442  &keyring->filename,
443  &tmp_error))
444  {
445  _dbus_verbose ("Failed to load keyring file: %s\n",
446  tmp_error.message);
447  /* continue with empty keyring file, so we recreate it */
448  dbus_error_free (&tmp_error);
449  }
450 
451  if (!_dbus_string_validate_ascii (&contents, 0,
452  _dbus_string_get_length (&contents)))
453  {
454  _dbus_warn ("Secret keyring file contains non-ASCII! Ignoring existing contents");
455  _dbus_string_set_length (&contents, 0);
456  }
457 
458  /* FIXME this is badly inefficient for large keyring files
459  * (not that large keyring files exist outside of test suites)
460  */
461  while (_dbus_string_pop_line (&contents, &line))
462  {
463  int next;
464  long val;
465  int id;
466  long timestamp;
467  int len;
468  int end;
469  DBusKey *new;
470 
471  /* Don't load more than the max. */
472  if (n_keys >= (add_new ? MAX_KEYS_IN_FILE - 1 : MAX_KEYS_IN_FILE))
473  break;
474 
475  next = 0;
476  if (!_dbus_string_parse_int (&line, 0, &val, &next))
477  {
478  _dbus_verbose ("could not parse secret key ID at start of line\n");
479  continue;
480  }
481 
482  if (val > _DBUS_INT32_MAX || val < 0)
483  {
484  _dbus_verbose ("invalid secret key ID at start of line\n");
485  continue;
486  }
487 
488  id = val;
489 
490  _dbus_string_skip_blank (&line, next, &next);
491 
492  if (!_dbus_string_parse_int (&line, next, &timestamp, &next))
493  {
494  _dbus_verbose ("could not parse secret key timestamp\n");
495  continue;
496  }
497 
498  if (timestamp < 0 ||
499  (now + MAX_TIME_TRAVEL_SECONDS) < timestamp ||
500  (now - EXPIRE_KEYS_TIMEOUT_SECONDS) > timestamp)
501  {
502  _dbus_verbose ("dropping/ignoring %ld-seconds old key with timestamp %ld as current time is %ld\n",
503  now - timestamp, timestamp, now);
504  continue;
505  }
506 
507  _dbus_string_skip_blank (&line, next, &next);
508 
509  len = _dbus_string_get_length (&line);
510 
511  if ((len - next) == 0)
512  {
513  _dbus_verbose ("no secret key after ID and timestamp\n");
514  continue;
515  }
516 
517  /* We have all three parts */
518  new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
519  if (new == NULL)
520  {
522  goto out;
523  }
524 
525  keys = new;
526  n_keys += 1;
527 
528  if (!_dbus_string_init (&keys[n_keys-1].secret))
529  {
530  n_keys -= 1; /* we don't want to free the one we didn't init */
532  goto out;
533  }
534 
535  keys[n_keys-1].id = id;
536  keys[n_keys-1].creation_time = timestamp;
537  if (!_dbus_string_hex_decode (&line, next, &end,
538  &keys[n_keys-1].secret, 0))
539  {
541  goto out;
542  }
543 
544  if (_dbus_string_get_length (&line) != end)
545  {
546  _dbus_verbose ("invalid hex encoding in keyring file\n");
547  _dbus_string_free (&keys[n_keys - 1].secret);
548  n_keys -= 1;
549  continue;
550  }
551  }
552 
553  _dbus_verbose ("Successfully loaded %d existing keys\n",
554  n_keys);
555 
556  if (add_new)
557  {
558  if (!add_new_key (&keys, &n_keys, error))
559  {
560  _dbus_verbose ("Failed to generate new key: %s\n",
561  error ? error->message : "(unknown)");
562  goto out;
563  }
564 
565  _dbus_string_set_length (&contents, 0);
566 
567  i = 0;
568  while (i < n_keys)
569  {
570  if (!_dbus_string_append_int (&contents,
571  keys[i].id))
572  goto nomem;
573 
574  if (!_dbus_string_append_byte (&contents, ' '))
575  goto nomem;
576 
577  if (!_dbus_string_append_int (&contents,
578  keys[i].creation_time))
579  goto nomem;
580 
581  if (!_dbus_string_append_byte (&contents, ' '))
582  goto nomem;
583 
584  if (!_dbus_string_hex_encode (&keys[i].secret, 0,
585  &contents,
586  _dbus_string_get_length (&contents)))
587  goto nomem;
588 
589  if (!_dbus_string_append_byte (&contents, '\n'))
590  goto nomem;
591 
592  ++i;
593  continue;
594 
595  nomem:
597  goto out;
598  }
599 
600  if (!_dbus_string_save_to_file (&contents, &keyring->filename,
601  FALSE, error))
602  goto out;
603  }
604 
605  if (keyring->keys)
606  free_keys (keyring->keys, keyring->n_keys);
607  keyring->keys = keys;
608  keyring->n_keys = n_keys;
609  keys = NULL;
610  n_keys = 0;
611 
612  retval = TRUE;
613 
614  out:
615  if (have_lock)
616  _dbus_keyring_unlock (keyring);
617 
618  if (! ((retval == TRUE && (error == NULL || error->name == NULL)) ||
619  (retval == FALSE && (error == NULL || error->name != NULL))))
620  {
621  if (error && error->name)
622  _dbus_verbose ("error is %s: %s\n", error->name, error->message);
623  _dbus_warn ("returning %d but error pointer %p name %s",
624  retval, error, error->name ? error->name : "(none)");
625  _dbus_assert_not_reached ("didn't handle errors properly");
626  }
627 
628  if (keys != NULL)
629  {
630  i = 0;
631  while (i < n_keys)
632  {
633  _dbus_string_zero (&keys[i].secret);
634  _dbus_string_free (&keys[i].secret);
635  ++i;
636  }
637 
638  dbus_free (keys);
639  }
640 
641  _dbus_string_free (&contents);
642  _dbus_string_free (&line);
643 
644  return retval;
645 }
646  /* end of internals */
648 
661 DBusKeyring *
663 {
664  keyring->refcount += 1;
665 
666  return keyring;
667 }
668 
675 void
677 {
678  keyring->refcount -= 1;
679 
680  if (keyring->refcount == 0)
681  {
682  if (keyring->credentials)
684 
685  _dbus_string_free (&keyring->filename);
686  _dbus_string_free (&keyring->filename_lock);
687  _dbus_string_free (&keyring->directory);
688  free_keys (keyring->keys, keyring->n_keys);
689  dbus_free (keyring);
690  }
691 }
692 
705  const DBusString *context,
706  DBusError *error)
707 {
708  DBusString ringdir;
709  DBusKeyring *keyring;
710  dbus_bool_t error_set;
711  DBusError tmp_error;
712  DBusCredentials *our_credentials;
713 
714  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
715 
716  if (_dbus_check_setuid ())
717  {
719  "Unable to create DBus keyring when setuid");
720  return NULL;
721  }
722 
723  keyring = NULL;
724  error_set = FALSE;
725  our_credentials = NULL;
726 
727  if (!_dbus_string_init (&ringdir))
728  {
730  return NULL;
731  }
732 
733  if (credentials != NULL)
734  {
735  our_credentials = _dbus_credentials_copy (credentials);
736  }
737  else
738  {
739  our_credentials = _dbus_credentials_new_from_current_process ();
740  }
741 
742  if (our_credentials == NULL)
743  goto failed;
744 
745  if (_dbus_credentials_are_anonymous (our_credentials))
746  {
747  if (!_dbus_credentials_add_from_current_process (our_credentials))
748  goto failed;
749  }
750 
752  our_credentials))
753  goto failed;
754 
755  keyring = _dbus_keyring_new ();
756  if (keyring == NULL)
757  goto failed;
758 
759  _dbus_assert (keyring->credentials == NULL);
760  keyring->credentials = our_credentials;
761  our_credentials = NULL; /* so we don't unref it again later */
762 
763  /* should have been validated already, but paranoia check here */
764  if (!_dbus_keyring_validate_context (context))
765  {
766  error_set = TRUE;
767  dbus_set_error_const (error,
769  "Invalid context in keyring creation");
770  goto failed;
771  }
772 
773  /* Save keyring dir in the keyring object */
774  if (!_dbus_string_copy (&ringdir, 0,
775  &keyring->directory, 0))
776  goto failed;
777 
778  /* Create keyring->filename based on keyring dir and context */
779  if (!_dbus_string_copy (&keyring->directory, 0,
780  &keyring->filename, 0))
781  goto failed;
782 
783  if (!_dbus_concat_dir_and_file (&keyring->filename,
784  context))
785  goto failed;
786 
787  /* Create lockfile name */
788  if (!_dbus_string_copy (&keyring->filename, 0,
789  &keyring->filename_lock, 0))
790  goto failed;
791 
792  if (!_dbus_string_append (&keyring->filename_lock, ".lock"))
793  goto failed;
794 
795  /* Reload keyring */
796  dbus_error_init (&tmp_error);
797  if (!_dbus_keyring_reload (keyring, FALSE, &tmp_error))
798  {
799  _dbus_verbose ("didn't load an existing keyring: %s\n",
800  tmp_error.message);
801  dbus_error_free (&tmp_error);
802  }
803 
804  /* We don't fail fatally if we can't create the directory,
805  * but the keyring will probably always be empty
806  * unless someone else manages to create it
807  */
808  dbus_error_init (&tmp_error);
809  if (!_dbus_ensure_directory (&keyring->directory,
810  &tmp_error))
811  {
812  _dbus_verbose ("Creating keyring directory: %s\n",
813  tmp_error.message);
814  dbus_error_free (&tmp_error);
815  }
816 
817  _dbus_string_free (&ringdir);
818 
819  return keyring;
820 
821  failed:
822  if (!error_set)
823  dbus_set_error_const (error,
825  NULL);
826  if (our_credentials)
827  _dbus_credentials_unref (our_credentials);
828  if (keyring)
829  _dbus_keyring_unref (keyring);
830  _dbus_string_free (&ringdir);
831  return NULL;
832 
833 }
834 
849 {
850  if (_dbus_string_get_length (context) == 0)
851  {
852  _dbus_verbose ("context is zero-length\n");
853  return FALSE;
854  }
855 
856  if (!_dbus_string_validate_ascii (context, 0,
857  _dbus_string_get_length (context)))
858  {
859  _dbus_verbose ("context not valid ascii\n");
860  return FALSE;
861  }
862 
863  /* no directory separators */
864  if (_dbus_string_find (context, 0, "/", NULL))
865  {
866  _dbus_verbose ("context contains a slash\n");
867  return FALSE;
868  }
869 
870  if (_dbus_string_find (context, 0, "\\", NULL))
871  {
872  _dbus_verbose ("context contains a backslash\n");
873  return FALSE;
874  }
875 
876  /* prevent attempts to use dotfiles or ".." or ".lock"
877  * all of which might allow some kind of attack
878  */
879  if (_dbus_string_find (context, 0, ".", NULL))
880  {
881  _dbus_verbose ("context contains a dot\n");
882  return FALSE;
883  }
884 
885  /* no spaces/tabs, those are used for separators in the protocol */
886  if (_dbus_string_find_blank (context, 0, NULL))
887  {
888  _dbus_verbose ("context contains a blank\n");
889  return FALSE;
890  }
891 
892  if (_dbus_string_find (context, 0, "\n", NULL))
893  {
894  _dbus_verbose ("context contains a newline\n");
895  return FALSE;
896  }
897 
898  if (_dbus_string_find (context, 0, "\r", NULL))
899  {
900  _dbus_verbose ("context contains a carriage return\n");
901  return FALSE;
902  }
903 
904  return TRUE;
905 }
906 
907 static DBusKey*
908 find_recent_key (DBusKeyring *keyring)
909 {
910  int i;
911  long tv_sec, tv_usec;
912 
913  _dbus_get_real_time (&tv_sec, &tv_usec);
914 
915  i = 0;
916  while (i < keyring->n_keys)
917  {
918  DBusKey *key = &keyring->keys[i];
919 
920  _dbus_verbose ("Key %d is %ld seconds old\n",
921  i, tv_sec - key->creation_time);
922 
923  if ((tv_sec - NEW_KEY_TIMEOUT_SECONDS) < key->creation_time)
924  return key;
925 
926  ++i;
927  }
928 
929  return NULL;
930 }
931 
943 int
945  DBusError *error)
946 {
947  DBusKey *key;
948 
949  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
950 
951  key = find_recent_key (keyring);
952  if (key)
953  return key->id;
954 
955  /* All our keys are too old, or we've never loaded the
956  * keyring. Create a new one.
957  */
958  if (!_dbus_keyring_reload (keyring, TRUE,
959  error))
960  return -1;
961 
962  key = find_recent_key (keyring);
963  if (key)
964  return key->id;
965  else
966  {
967  dbus_set_error_const (error,
969  "No recent-enough key found in keyring, and unable to create a new key");
970  return -1;
971  }
972 }
973 
985 {
986  return _dbus_credentials_same_user (keyring->credentials,
987  credentials);
988 }
989 
1003  int key_id,
1004  DBusString *hex_key)
1005 {
1006  DBusKey *key;
1007 
1008  key = find_key_by_id (keyring->keys,
1009  keyring->n_keys,
1010  key_id);
1011  if (key == NULL)
1012  return TRUE; /* had enough memory, so TRUE */
1013 
1014  return _dbus_string_hex_encode (&key->secret, 0,
1015  hex_key,
1016  _dbus_string_get_length (hex_key));
1017 }
1018  /* end of exposed API */
1020 
1021 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1022 #include "dbus-test.h"
1023 #include <stdio.h>
1024 
1026 _dbus_keyring_test (void)
1027 {
1028  DBusString context;
1029  DBusKeyring *ring1;
1030  DBusKeyring *ring2;
1031  int id;
1032  DBusError error;
1033  int i;
1034 
1035  ring1 = NULL;
1036  ring2 = NULL;
1037 
1038  /* Context validation */
1039 
1040  _dbus_string_init_const (&context, "foo");
1042  _dbus_string_init_const (&context, "org_freedesktop_blah");
1044 
1045  _dbus_string_init_const (&context, "");
1047  _dbus_string_init_const (&context, ".foo");
1049  _dbus_string_init_const (&context, "bar.foo");
1051  _dbus_string_init_const (&context, "bar/foo");
1053  _dbus_string_init_const (&context, "bar\\foo");
1055  _dbus_string_init_const (&context, "foo\xfa\xf0");
1057  _dbus_string_init_const (&context, "foo\x80");
1059  _dbus_string_init_const (&context, "foo\x7f");
1061  _dbus_string_init_const (&context, "foo bar");
1063 
1064  if (!_dbus_string_init (&context))
1065  _dbus_test_fatal ("no memory");
1066  if (!_dbus_string_append_byte (&context, '\0'))
1067  _dbus_test_fatal ("no memory");
1069  _dbus_string_free (&context);
1070 
1071  /* Now verify that if we create a key in keyring 1,
1072  * it is properly loaded in keyring 2
1073  */
1074 
1075  _dbus_string_init_const (&context, "org_freedesktop_dbus_testsuite");
1076  dbus_error_init (&error);
1077  ring1 = _dbus_keyring_new_for_credentials (NULL, &context,
1078  &error);
1079  _dbus_assert (ring1 != NULL);
1080  _dbus_assert (error.name == NULL);
1081 
1082  id = _dbus_keyring_get_best_key (ring1, &error);
1083  if (id < 0)
1084  {
1085  fprintf (stderr, "Could not load keyring: %s\n", error.message);
1086  dbus_error_free (&error);
1087  goto failure;
1088  }
1089 
1090  ring2 = _dbus_keyring_new_for_credentials (NULL, &context, &error);
1091  _dbus_assert (ring2 != NULL);
1092  _dbus_assert (error.name == NULL);
1093 
1094  if (ring1->n_keys != ring2->n_keys)
1095  {
1096  fprintf (stderr, "Different number of keys in keyrings\n");
1097  goto failure;
1098  }
1099 
1100  /* We guarantee we load and save keeping keys in a fixed
1101  * order
1102  */
1103  i = 0;
1104  while (i < ring1->n_keys)
1105  {
1106  if (ring1->keys[i].id != ring2->keys[i].id)
1107  {
1108  fprintf (stderr, "Keyring 1 has first key ID %d and keyring 2 has %d\n",
1109  ring1->keys[i].id, ring2->keys[i].id);
1110  goto failure;
1111  }
1112 
1113  if (ring1->keys[i].creation_time != ring2->keys[i].creation_time)
1114  {
1115  fprintf (stderr, "Keyring 1 has first key time %ld and keyring 2 has %ld\n",
1116  ring1->keys[i].creation_time, ring2->keys[i].creation_time);
1117  goto failure;
1118  }
1119 
1120  if (!_dbus_string_equal (&ring1->keys[i].secret,
1121  &ring2->keys[i].secret))
1122  {
1123  fprintf (stderr, "Keyrings 1 and 2 have different secrets for same ID/timestamp\n");
1124  goto failure;
1125  }
1126 
1127  ++i;
1128  }
1129 
1130  _dbus_test_diag (" %d keys in test", ring1->n_keys);
1131 
1132  /* Test ref/unref */
1133  _dbus_keyring_ref (ring1);
1134  _dbus_keyring_ref (ring2);
1135  _dbus_keyring_unref (ring1);
1136  _dbus_keyring_unref (ring2);
1137 
1138 
1139  /* really unref */
1140  _dbus_keyring_unref (ring1);
1141  _dbus_keyring_unref (ring2);
1142 
1143  return TRUE;
1144 
1145  failure:
1146  if (ring1)
1147  _dbus_keyring_unref (ring1);
1148  if (ring2)
1149  _dbus_keyring_unref (ring2);
1150 
1151  return FALSE;
1152 }
1153 
1154 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
1155 
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:952
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_parse_int(const DBusString *str, int start, long *value_return, int *end_return)
Parses an integer contained in a DBusString.
Definition: dbus-sysdeps.c:444
dbus_int32_t id
identifier used to refer to the key
Definition: dbus-keyring.c:95
int refcount
Reference count.
Definition: dbus-keyring.c:114
const char * message
public error message field
Definition: dbus-errors.h:51
#define NULL
A null pointer, defined appropriately for C or C++.
dbus_bool_t _dbus_check_dir_is_private_to_user(DBusString *dir, DBusError *error)
Checks to make sure the given directory is private to the user.
void _dbus_keyring_unref(DBusKeyring *keyring)
Decrements refcount and finalizes if it reaches zero.
Definition: dbus-keyring.c:676
#define MAX_KEYS_IN_FILE
Maximum number of keys in the keyring before we just ignore the rest.
Definition: dbus-keyring.c:87
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
Definition: dbus-string.c:2030
DBusCredentials * _dbus_credentials_copy(DBusCredentials *credentials)
Copy a credentials object.
dbus_bool_t _dbus_create_file_exclusively(const DBusString *filename, DBusError *error)
Creates the given file, failing if the file already exists.
dbus_bool_t _dbus_ensure_directory(const DBusString *filename, DBusError *error)
Creates a directory; succeeds if the directory is created or already existed.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:703
#define EXPIRE_KEYS_TIMEOUT_SECONDS
The time after which we drop a key from the secrets file.
Definition: dbus-keyring.c:74
dbus_bool_t _dbus_string_save_to_file(const DBusString *str, const DBusString *filename, dbus_bool_t world_readable, DBusError *error)
Writes a string out to a file.
dbus_bool_t _dbus_string_hex_encode(const DBusString *source, int start, DBusString *dest, int insert_at)
Encodes a string in hex, the way MD5 and SHA-1 are usually encoded.
Definition: dbus-string.c:2276
#define DBUS_ERROR_NOT_SUPPORTED
Requested operation isn&#39;t supported (like ENOSYS on UNIX).
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_append_int(DBusString *str, long value)
Appends an integer to a DBusString.
Definition: dbus-sysdeps.c:363
Internals of DBusKeyring.
Definition: dbus-keyring.c:112
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
dbus_bool_t _dbus_check_setuid(void)
NOTE: If you modify this function, please also consider making the corresponding change in GLib...
void dbus_error_free(DBusError *error)
Frees an error that&#39;s been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
dbus_bool_t _dbus_file_get_contents(DBusString *str, const DBusString *filename, DBusError *error)
Appends the contents of the given file to the string, returning error code.
dbus_bool_t _dbus_keyring_get_hex_key(DBusKeyring *keyring, int key_id, DBusString *hex_key)
Gets the hex-encoded secret key for the given ID.
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
int n_keys
Number of keys.
Definition: dbus-keyring.c:119
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that&#39;s copied to the d...
Definition: dbus-string.c:1300
dbus_bool_t _dbus_string_find(const DBusString *str, int start, const char *substr, int *found)
Finds the given substring in the string, returning TRUE and filling in the byte index where the subst...
Definition: dbus-string.c:1621
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
DBusCredentials * credentials
Credentials containing user the keyring is for.
Definition: dbus-keyring.c:120
dbus_bool_t _dbus_string_move(DBusString *source, int start, DBusString *dest, int insert_at)
Moves the end of one string into another string.
Definition: dbus-string.c:1276
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
dbus_bool_t _dbus_credentials_are_anonymous(DBusCredentials *credentials)
Checks whether a credentials object contains a user identity.
#define LOCK_TIMEOUT_MILLISECONDS
Length of each timeout while waiting for a lock.
Definition: dbus-keyring.c:194
DBusString filename
Keyring filename.
Definition: dbus-keyring.c:116
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
void _dbus_string_skip_blank(const DBusString *str, int start, int *end)
Skips blanks from start, storing the first non-blank in *end (blank is space or tab).
Definition: dbus-string.c:1820
long creation_time
when the key was generated, as unix timestamp.
Definition: dbus-keyring.c:97
DBusCredentials * _dbus_credentials_new_from_current_process(void)
Creates a new object with the most important credentials (user ID and process ID) from the current pr...
dbus_bool_t _dbus_string_pop_line(DBusString *source, DBusString *dest)
Assigns a newline-terminated or \r\n-terminated line from the front of the string to the given dest s...
Definition: dbus-string.c:1926
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
dbus_bool_t _dbus_append_keyring_directory_for_credentials(DBusString *directory, DBusCredentials *credentials)
Appends the directory in which a keyring for the given credentials should be stored.
void _dbus_string_zero(DBusString *str)
Clears all allocated bytes in the string to zero.
Definition: dbus-string.c:2702
Object representing an exception.
Definition: dbus-errors.h:48
int _dbus_keyring_get_best_key(DBusKeyring *keyring, DBusError *error)
Gets a recent key to use for authentication.
Definition: dbus-keyring.c:944
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
DBusKey * keys
Keys loaded from the file.
Definition: dbus-keyring.c:118
#define NEW_KEY_TIMEOUT_SECONDS
The maximum age of a key before we create a new key to use in challenges.
Definition: dbus-keyring.c:68
dbus_bool_t _dbus_string_append_byte(DBusString *str, unsigned char byte)
Appends a single byte to the string, returning FALSE if not enough memory.
Definition: dbus-string.c:1174
DBusString directory
Directory the below two items are inside.
Definition: dbus-keyring.c:115
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:264
dbus_bool_t _dbus_credentials_add_from_current_process(DBusCredentials *credentials)
Adds the most important credentials of the current process (the uid and pid) to the passed-in credent...
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
const char * name
public error name field
Definition: dbus-errors.h:50
#define MAX_LOCK_TIMEOUTS
Maximum number of timeouts waiting for lock before we decide it&#39;s stale.
Definition: dbus-keyring.c:192
DBusString filename_lock
Name of lockfile.
Definition: dbus-keyring.c:117
dbus_bool_t _dbus_string_find_blank(const DBusString *str, int start, int *found)
Finds a blank (space or tab) in the string.
Definition: dbus-string.c:1782
dbus_bool_t _dbus_keyring_is_for_credentials(DBusKeyring *keyring, DBusCredentials *credentials)
Checks whether the keyring is for the same user as the given credentials.
Definition: dbus-keyring.c:983
DBusKeyring * _dbus_keyring_new_for_credentials(DBusCredentials *credentials, const DBusString *context, DBusError *error)
Creates a new keyring that lives in the ~/.dbus-keyrings directory of the user represented by credent...
Definition: dbus-keyring.c:704
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
Definition: dbus-errors.c:188
dbus_bool_t _dbus_string_hex_decode(const DBusString *source, int start, int *end_return, DBusString *dest, int insert_at)
Decodes a string from hex encoding.
Definition: dbus-string.c:2326
dbus_bool_t _dbus_generate_random_bytes(DBusString *str, int n_bytes, DBusError *error)
Generates the given number of securely random bytes, using the best mechanism we can come up with...
#define MAX_TIME_TRAVEL_SECONDS
The maximum amount of time a key can be in the future.
Definition: dbus-keyring.c:78
DBusString secret
the actual key
Definition: dbus-keyring.c:102
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define _DBUS_INT32_MAX
Maximum value of type "int32".
void _dbus_credentials_unref(DBusCredentials *credentials)
Decrement refcount on credentials.
#define FALSE
Expands to "0".
dbus_bool_t _dbus_credentials_same_user(DBusCredentials *credentials, DBusCredentials *other_credentials)
Check whether the user-identifying credentials in two credentials objects are identical.
void dbus_set_error_const(DBusError *error, const char *name, const char *message)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:243
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:819
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
void * dbus_realloc(void *memory, size_t bytes)
Resizes a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:603
dbus_bool_t _dbus_string_validate_ascii(const DBusString *str, int start, int len)
Checks that the given range of the string is valid ASCII with no nul bytes.
Definition: dbus-string.c:2467
dbus_bool_t _dbus_keyring_validate_context(const DBusString *context)
Checks whether the context is a valid context.
Definition: dbus-keyring.c:848
void _dbus_get_real_time(long *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
DBusKeyring * _dbus_keyring_ref(DBusKeyring *keyring)
Increments reference count of the keyring.
Definition: dbus-keyring.c:662
A single key from the cookie file.
Definition: dbus-keyring.c:93