D-Bus  1.13.6
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] << 24);
314  if (id < 0)
315  id = - id;
316  _dbus_assert (id >= 0);
317 
318  if (find_key_by_id (keys, n_keys, id) != NULL)
319  {
320  _dbus_string_set_length (&bytes, 0);
321  _dbus_verbose ("Key ID %d already existed, trying another one\n",
322  id);
323  goto retry;
324  }
325 
326  _dbus_verbose ("Creating key with ID %d\n", id);
327 
328 #define KEY_LENGTH_BYTES 24
329  _dbus_string_set_length (&bytes, 0);
330  if (!_dbus_generate_random_bytes (&bytes, KEY_LENGTH_BYTES, error))
331  {
332  goto out;
333  }
334 
335  new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
336  if (new == NULL)
337  {
339  goto out;
340  }
341 
342  keys = new;
343  *keys_p = keys; /* otherwise *keys_p ends up invalid */
344  n_keys += 1;
345 
346  if (!_dbus_string_init (&keys[n_keys-1].secret))
347  {
348  n_keys -= 1; /* we don't want to free the one we didn't init */
350  goto out;
351  }
352 
353  _dbus_get_real_time (&timestamp, NULL);
354 
355  keys[n_keys-1].id = id;
356  keys[n_keys-1].creation_time = timestamp;
357  if (!_dbus_string_move (&bytes, 0,
358  &keys[n_keys-1].secret,
359  0))
360  {
362  _dbus_string_free (&keys[n_keys-1].secret);
363  n_keys -= 1;
364  goto out;
365  }
366 
367  retval = TRUE;
368 
369  out:
370  *n_keys_p = n_keys;
371 
372  _dbus_string_free (&bytes);
373  return retval;
374 }
375 
390 static dbus_bool_t
391 _dbus_keyring_reload (DBusKeyring *keyring,
392  dbus_bool_t add_new,
393  DBusError *error)
394 {
395  DBusString contents;
396  DBusString line;
397  dbus_bool_t retval;
398  dbus_bool_t have_lock;
399  DBusKey *keys;
400  int n_keys;
401  int i;
402  long now;
403  DBusError tmp_error;
404 
405  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
406 
407  if (!_dbus_check_dir_is_private_to_user (&keyring->directory, error))
408  return FALSE;
409 
410  if (!_dbus_string_init (&contents))
411  {
413  return FALSE;
414  }
415 
416  if (!_dbus_string_init (&line))
417  {
419  _dbus_string_free (&contents);
420  return FALSE;
421  }
422 
423  keys = NULL;
424  n_keys = 0;
425  retval = FALSE;
426  have_lock = FALSE;
427 
428  _dbus_get_real_time (&now, NULL);
429 
430  if (add_new)
431  {
432  if (!_dbus_keyring_lock (keyring))
433  {
435  "Could not lock keyring file to add to it");
436  goto out;
437  }
438 
439  have_lock = TRUE;
440  }
441 
442  dbus_error_init (&tmp_error);
443  if (!_dbus_file_get_contents (&contents,
444  &keyring->filename,
445  &tmp_error))
446  {
447  _dbus_verbose ("Failed to load keyring file: %s\n",
448  tmp_error.message);
449  /* continue with empty keyring file, so we recreate it */
450  dbus_error_free (&tmp_error);
451  }
452 
453  if (!_dbus_string_validate_ascii (&contents, 0,
454  _dbus_string_get_length (&contents)))
455  {
456  _dbus_warn ("Secret keyring file contains non-ASCII! Ignoring existing contents");
457  _dbus_string_set_length (&contents, 0);
458  }
459 
460  /* FIXME this is badly inefficient for large keyring files
461  * (not that large keyring files exist outside of test suites)
462  */
463  while (_dbus_string_pop_line (&contents, &line))
464  {
465  int next;
466  long val;
467  int id;
468  long timestamp;
469  int len;
470  int end;
471  DBusKey *new;
472 
473  /* Don't load more than the max. */
474  if (n_keys >= (add_new ? MAX_KEYS_IN_FILE - 1 : MAX_KEYS_IN_FILE))
475  break;
476 
477  next = 0;
478  if (!_dbus_string_parse_int (&line, 0, &val, &next))
479  {
480  _dbus_verbose ("could not parse secret key ID at start of line\n");
481  continue;
482  }
483 
484  if (val > _DBUS_INT32_MAX || val < 0)
485  {
486  _dbus_verbose ("invalid secret key ID at start of line\n");
487  continue;
488  }
489 
490  id = val;
491 
492  _dbus_string_skip_blank (&line, next, &next);
493 
494  if (!_dbus_string_parse_int (&line, next, &timestamp, &next))
495  {
496  _dbus_verbose ("could not parse secret key timestamp\n");
497  continue;
498  }
499 
500  if (timestamp < 0 ||
501  (now + MAX_TIME_TRAVEL_SECONDS) < timestamp ||
502  (now - EXPIRE_KEYS_TIMEOUT_SECONDS) > timestamp)
503  {
504  _dbus_verbose ("dropping/ignoring %ld-seconds old key with timestamp %ld as current time is %ld\n",
505  now - timestamp, timestamp, now);
506  continue;
507  }
508 
509  _dbus_string_skip_blank (&line, next, &next);
510 
511  len = _dbus_string_get_length (&line);
512 
513  if ((len - next) == 0)
514  {
515  _dbus_verbose ("no secret key after ID and timestamp\n");
516  continue;
517  }
518 
519  /* We have all three parts */
520  new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
521  if (new == NULL)
522  {
524  goto out;
525  }
526 
527  keys = new;
528  n_keys += 1;
529 
530  if (!_dbus_string_init (&keys[n_keys-1].secret))
531  {
532  n_keys -= 1; /* we don't want to free the one we didn't init */
534  goto out;
535  }
536 
537  keys[n_keys-1].id = id;
538  keys[n_keys-1].creation_time = timestamp;
539  if (!_dbus_string_hex_decode (&line, next, &end,
540  &keys[n_keys-1].secret, 0))
541  {
543  goto out;
544  }
545 
546  if (_dbus_string_get_length (&line) != end)
547  {
548  _dbus_verbose ("invalid hex encoding in keyring file\n");
549  _dbus_string_free (&keys[n_keys - 1].secret);
550  n_keys -= 1;
551  continue;
552  }
553  }
554 
555  _dbus_verbose ("Successfully loaded %d existing keys\n",
556  n_keys);
557 
558  if (add_new)
559  {
560  if (!add_new_key (&keys, &n_keys, error))
561  {
562  _dbus_verbose ("Failed to generate new key: %s\n",
563  error ? error->message : "(unknown)");
564  goto out;
565  }
566 
567  _dbus_string_set_length (&contents, 0);
568 
569  i = 0;
570  while (i < n_keys)
571  {
572  if (!_dbus_string_append_int (&contents,
573  keys[i].id))
574  goto nomem;
575 
576  if (!_dbus_string_append_byte (&contents, ' '))
577  goto nomem;
578 
579  if (!_dbus_string_append_int (&contents,
580  keys[i].creation_time))
581  goto nomem;
582 
583  if (!_dbus_string_append_byte (&contents, ' '))
584  goto nomem;
585 
586  if (!_dbus_string_hex_encode (&keys[i].secret, 0,
587  &contents,
588  _dbus_string_get_length (&contents)))
589  goto nomem;
590 
591  if (!_dbus_string_append_byte (&contents, '\n'))
592  goto nomem;
593 
594  ++i;
595  continue;
596 
597  nomem:
599  goto out;
600  }
601 
602  if (!_dbus_string_save_to_file (&contents, &keyring->filename,
603  FALSE, error))
604  goto out;
605  }
606 
607  if (keyring->keys)
608  free_keys (keyring->keys, keyring->n_keys);
609  keyring->keys = keys;
610  keyring->n_keys = n_keys;
611  keys = NULL;
612  n_keys = 0;
613 
614  retval = TRUE;
615 
616  out:
617  if (have_lock)
618  _dbus_keyring_unlock (keyring);
619 
620  if (! ((retval == TRUE && (error == NULL || error->name == NULL)) ||
621  (retval == FALSE && (error == NULL || error->name != NULL))))
622  {
623  if (error && error->name)
624  _dbus_verbose ("error is %s: %s\n", error->name, error->message);
625  _dbus_warn ("returning %d but error pointer %p name %s",
626  retval, error, error->name ? error->name : "(none)");
627  _dbus_assert_not_reached ("didn't handle errors properly");
628  }
629 
630  if (keys != NULL)
631  {
632  i = 0;
633  while (i < n_keys)
634  {
635  _dbus_string_zero (&keys[i].secret);
636  _dbus_string_free (&keys[i].secret);
637  ++i;
638  }
639 
640  dbus_free (keys);
641  }
642 
643  _dbus_string_free (&contents);
644  _dbus_string_free (&line);
645 
646  return retval;
647 }
648  /* end of internals */
650 
663 DBusKeyring *
665 {
666  keyring->refcount += 1;
667 
668  return keyring;
669 }
670 
677 void
679 {
680  keyring->refcount -= 1;
681 
682  if (keyring->refcount == 0)
683  {
684  if (keyring->credentials)
686 
687  _dbus_string_free (&keyring->filename);
688  _dbus_string_free (&keyring->filename_lock);
689  _dbus_string_free (&keyring->directory);
690  free_keys (keyring->keys, keyring->n_keys);
691  dbus_free (keyring);
692  }
693 }
694 
707  const DBusString *context,
708  DBusError *error)
709 {
710  DBusString ringdir;
711  DBusKeyring *keyring;
712  dbus_bool_t error_set;
713  DBusError tmp_error;
714  DBusCredentials *our_credentials;
715 
716  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
717 
718  if (_dbus_check_setuid ())
719  {
721  "Unable to create DBus keyring when setuid");
722  return NULL;
723  }
724 
725  keyring = NULL;
726  error_set = FALSE;
727  our_credentials = NULL;
728 
729  if (!_dbus_string_init (&ringdir))
730  {
732  return NULL;
733  }
734 
735  if (credentials != NULL)
736  {
737  our_credentials = _dbus_credentials_copy (credentials);
738  }
739  else
740  {
741  our_credentials = _dbus_credentials_new_from_current_process ();
742  }
743 
744  if (our_credentials == NULL)
745  goto failed;
746 
747  if (_dbus_credentials_are_anonymous (our_credentials))
748  {
749  if (!_dbus_credentials_add_from_current_process (our_credentials))
750  goto failed;
751  }
752 
754  our_credentials))
755  goto failed;
756 
757  keyring = _dbus_keyring_new ();
758  if (keyring == NULL)
759  goto failed;
760 
761  _dbus_assert (keyring->credentials == NULL);
762  keyring->credentials = our_credentials;
763  our_credentials = NULL; /* so we don't unref it again later */
764 
765  /* should have been validated already, but paranoia check here */
766  if (!_dbus_keyring_validate_context (context))
767  {
768  error_set = TRUE;
769  dbus_set_error_const (error,
771  "Invalid context in keyring creation");
772  goto failed;
773  }
774 
775  /* Save keyring dir in the keyring object */
776  if (!_dbus_string_copy (&ringdir, 0,
777  &keyring->directory, 0))
778  goto failed;
779 
780  /* Create keyring->filename based on keyring dir and context */
781  if (!_dbus_string_copy (&keyring->directory, 0,
782  &keyring->filename, 0))
783  goto failed;
784 
785  if (!_dbus_concat_dir_and_file (&keyring->filename,
786  context))
787  goto failed;
788 
789  /* Create lockfile name */
790  if (!_dbus_string_copy (&keyring->filename, 0,
791  &keyring->filename_lock, 0))
792  goto failed;
793 
794  if (!_dbus_string_append (&keyring->filename_lock, ".lock"))
795  goto failed;
796 
797  /* Reload keyring */
798  dbus_error_init (&tmp_error);
799  if (!_dbus_keyring_reload (keyring, FALSE, &tmp_error))
800  {
801  _dbus_verbose ("didn't load an existing keyring: %s\n",
802  tmp_error.message);
803  dbus_error_free (&tmp_error);
804  }
805 
806  /* We don't fail fatally if we can't create the directory,
807  * but the keyring will probably always be empty
808  * unless someone else manages to create it
809  */
810  dbus_error_init (&tmp_error);
811  if (!_dbus_ensure_directory (&keyring->directory,
812  &tmp_error))
813  {
814  _dbus_verbose ("Creating keyring directory: %s\n",
815  tmp_error.message);
816  dbus_error_free (&tmp_error);
817  }
818 
819  _dbus_string_free (&ringdir);
820 
821  return keyring;
822 
823  failed:
824  if (!error_set)
825  dbus_set_error_const (error,
827  NULL);
828  if (our_credentials)
829  _dbus_credentials_unref (our_credentials);
830  if (keyring)
831  _dbus_keyring_unref (keyring);
832  _dbus_string_free (&ringdir);
833  return NULL;
834 
835 }
836 
851 {
852  if (_dbus_string_get_length (context) == 0)
853  {
854  _dbus_verbose ("context is zero-length\n");
855  return FALSE;
856  }
857 
858  if (!_dbus_string_validate_ascii (context, 0,
859  _dbus_string_get_length (context)))
860  {
861  _dbus_verbose ("context not valid ascii\n");
862  return FALSE;
863  }
864 
865  /* no directory separators */
866  if (_dbus_string_find (context, 0, "/", NULL))
867  {
868  _dbus_verbose ("context contains a slash\n");
869  return FALSE;
870  }
871 
872  if (_dbus_string_find (context, 0, "\\", NULL))
873  {
874  _dbus_verbose ("context contains a backslash\n");
875  return FALSE;
876  }
877 
878  /* prevent attempts to use dotfiles or ".." or ".lock"
879  * all of which might allow some kind of attack
880  */
881  if (_dbus_string_find (context, 0, ".", NULL))
882  {
883  _dbus_verbose ("context contains a dot\n");
884  return FALSE;
885  }
886 
887  /* no spaces/tabs, those are used for separators in the protocol */
888  if (_dbus_string_find_blank (context, 0, NULL))
889  {
890  _dbus_verbose ("context contains a blank\n");
891  return FALSE;
892  }
893 
894  if (_dbus_string_find (context, 0, "\n", NULL))
895  {
896  _dbus_verbose ("context contains a newline\n");
897  return FALSE;
898  }
899 
900  if (_dbus_string_find (context, 0, "\r", NULL))
901  {
902  _dbus_verbose ("context contains a carriage return\n");
903  return FALSE;
904  }
905 
906  return TRUE;
907 }
908 
909 static DBusKey*
910 find_recent_key (DBusKeyring *keyring)
911 {
912  int i;
913  long tv_sec, tv_usec;
914 
915  _dbus_get_real_time (&tv_sec, &tv_usec);
916 
917  i = 0;
918  while (i < keyring->n_keys)
919  {
920  DBusKey *key = &keyring->keys[i];
921 
922  _dbus_verbose ("Key %d is %ld seconds old\n",
923  i, tv_sec - key->creation_time);
924 
925  if ((tv_sec - NEW_KEY_TIMEOUT_SECONDS) < key->creation_time)
926  return key;
927 
928  ++i;
929  }
930 
931  return NULL;
932 }
933 
945 int
947  DBusError *error)
948 {
949  DBusKey *key;
950 
951  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
952 
953  key = find_recent_key (keyring);
954  if (key)
955  return key->id;
956 
957  /* All our keys are too old, or we've never loaded the
958  * keyring. Create a new one.
959  */
960  if (!_dbus_keyring_reload (keyring, TRUE,
961  error))
962  return -1;
963 
964  key = find_recent_key (keyring);
965  if (key)
966  return key->id;
967  else
968  {
969  dbus_set_error_const (error,
971  "No recent-enough key found in keyring, and unable to create a new key");
972  return -1;
973  }
974 }
975 
987 {
988  return _dbus_credentials_same_user (keyring->credentials,
989  credentials);
990 }
991 
1005  int key_id,
1006  DBusString *hex_key)
1007 {
1008  DBusKey *key;
1009 
1010  key = find_key_by_id (keyring->keys,
1011  keyring->n_keys,
1012  key_id);
1013  if (key == NULL)
1014  return TRUE; /* had enough memory, so TRUE */
1015 
1016  return _dbus_string_hex_encode (&key->secret, 0,
1017  hex_key,
1018  _dbus_string_get_length (hex_key));
1019 }
1020  /* end of exposed API */
1022 
1023 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1024 #include "dbus-test.h"
1025 #include <stdio.h>
1026 
1028 _dbus_keyring_test (void)
1029 {
1030  DBusString context;
1031  DBusKeyring *ring1;
1032  DBusKeyring *ring2;
1033  int id;
1034  DBusError error;
1035  int i;
1036 
1037  ring1 = NULL;
1038  ring2 = NULL;
1039 
1040  /* Context validation */
1041 
1042  _dbus_string_init_const (&context, "foo");
1044  _dbus_string_init_const (&context, "org_freedesktop_blah");
1046 
1047  _dbus_string_init_const (&context, "");
1049  _dbus_string_init_const (&context, ".foo");
1051  _dbus_string_init_const (&context, "bar.foo");
1053  _dbus_string_init_const (&context, "bar/foo");
1055  _dbus_string_init_const (&context, "bar\\foo");
1057  _dbus_string_init_const (&context, "foo\xfa\xf0");
1059  _dbus_string_init_const (&context, "foo\x80");
1061  _dbus_string_init_const (&context, "foo\x7f");
1063  _dbus_string_init_const (&context, "foo bar");
1065 
1066  if (!_dbus_string_init (&context))
1067  _dbus_test_fatal ("no memory");
1068  if (!_dbus_string_append_byte (&context, '\0'))
1069  _dbus_test_fatal ("no memory");
1071  _dbus_string_free (&context);
1072 
1073  /* Now verify that if we create a key in keyring 1,
1074  * it is properly loaded in keyring 2
1075  */
1076 
1077  _dbus_string_init_const (&context, "org_freedesktop_dbus_testsuite");
1078  dbus_error_init (&error);
1079  ring1 = _dbus_keyring_new_for_credentials (NULL, &context,
1080  &error);
1081  _dbus_assert (ring1 != NULL);
1082  _dbus_assert (error.name == NULL);
1083 
1084  id = _dbus_keyring_get_best_key (ring1, &error);
1085  if (id < 0)
1086  {
1087  fprintf (stderr, "Could not load keyring: %s\n", error.message);
1088  dbus_error_free (&error);
1089  goto failure;
1090  }
1091 
1092  ring2 = _dbus_keyring_new_for_credentials (NULL, &context, &error);
1093  _dbus_assert (ring2 != NULL);
1094  _dbus_assert (error.name == NULL);
1095 
1096  if (ring1->n_keys != ring2->n_keys)
1097  {
1098  fprintf (stderr, "Different number of keys in keyrings\n");
1099  goto failure;
1100  }
1101 
1102  /* We guarantee we load and save keeping keys in a fixed
1103  * order
1104  */
1105  i = 0;
1106  while (i < ring1->n_keys)
1107  {
1108  if (ring1->keys[i].id != ring2->keys[i].id)
1109  {
1110  fprintf (stderr, "Keyring 1 has first key ID %d and keyring 2 has %d\n",
1111  ring1->keys[i].id, ring2->keys[i].id);
1112  goto failure;
1113  }
1114 
1115  if (ring1->keys[i].creation_time != ring2->keys[i].creation_time)
1116  {
1117  fprintf (stderr, "Keyring 1 has first key time %ld and keyring 2 has %ld\n",
1118  ring1->keys[i].creation_time, ring2->keys[i].creation_time);
1119  goto failure;
1120  }
1121 
1122  if (!_dbus_string_equal (&ring1->keys[i].secret,
1123  &ring2->keys[i].secret))
1124  {
1125  fprintf (stderr, "Keyrings 1 and 2 have different secrets for same ID/timestamp\n");
1126  goto failure;
1127  }
1128 
1129  ++i;
1130  }
1131 
1132  _dbus_test_diag (" %d keys in test", ring1->n_keys);
1133 
1134  /* Test ref/unref */
1135  _dbus_keyring_ref (ring1);
1136  _dbus_keyring_ref (ring2);
1137  _dbus_keyring_unref (ring1);
1138  _dbus_keyring_unref (ring2);
1139 
1140 
1141  /* really unref */
1142  _dbus_keyring_unref (ring1);
1143  _dbus_keyring_unref (ring2);
1144 
1145  return TRUE;
1146 
1147  failure:
1148  if (ring1)
1149  _dbus_keyring_unref (ring1);
1150  if (ring2)
1151  _dbus_keyring_unref (ring2);
1152 
1153  return FALSE;
1154 }
1155 
1156 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
1157 
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:678
#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.
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_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:59
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:946
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:985
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:706
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.
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:850
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:664
A single key from the cookie file.
Definition: dbus-keyring.c:93