D-Bus 1.15.8
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 * SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
7 *
8 * Licensed under the Academic Free License version 2.1
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 */
25
26#include <config.h>
27#include "dbus-keyring.h"
28#include "dbus-protocol.h"
29#include <dbus/dbus-string.h>
30#include <dbus/dbus-list.h>
31#include <dbus/dbus-sysdeps.h>
32#include <dbus/dbus-test-tap.h>
33
70#define NEW_KEY_TIMEOUT_SECONDS (60*5)
76#define EXPIRE_KEYS_TIMEOUT_SECONDS (NEW_KEY_TIMEOUT_SECONDS + (60*2))
80#define MAX_TIME_TRAVEL_SECONDS (60*5)
81
86#ifdef DBUS_ENABLE_EMBEDDED_TESTS
87#define MAX_KEYS_IN_FILE 10
88#else
89#define MAX_KEYS_IN_FILE 256
90#endif
91
95typedef struct
96{
106} DBusKey;
107
115{
121 int n_keys;
123};
124
125static DBusKeyring*
126_dbus_keyring_new (void)
127{
128 DBusKeyring *keyring;
129
130 keyring = dbus_new0 (DBusKeyring, 1);
131 if (keyring == NULL)
132 goto out_0;
133
134 if (!_dbus_string_init (&keyring->directory))
135 goto out_1;
136
137 if (!_dbus_string_init (&keyring->filename))
138 goto out_2;
139
140 if (!_dbus_string_init (&keyring->filename_lock))
141 goto out_3;
142
143 keyring->refcount = 1;
144 keyring->keys = NULL;
145 keyring->n_keys = 0;
146
147 return keyring;
148
149 out_3:
150 _dbus_string_free (&keyring->filename);
151 out_2:
152 _dbus_string_free (&keyring->directory);
153 out_1:
154 dbus_free (keyring);
155 out_0:
156 return NULL;
157}
158
159static void
160free_keys (DBusKey *keys,
161 int n_keys)
162{
163 int i;
164
165 /* should be safe for args NULL, 0 */
166
167 i = 0;
168 while (i < n_keys)
169 {
170 _dbus_string_free (&keys[i].secret);
171 ++i;
172 }
173
174 dbus_free (keys);
175}
176
177/* Our locking scheme is highly unreliable. However, there is
178 * unfortunately no reliable locking scheme in user home directories;
179 * between bugs in Linux NFS, people using Tru64 or other total crap
180 * NFS, AFS, random-file-system-of-the-week, and so forth, fcntl() in
181 * homedirs simply generates tons of bug reports. This has been
182 * learned through hard experience with GConf, unfortunately.
183 *
184 * This bad hack might work better for the kind of lock we have here,
185 * which we don't expect to hold for any length of time. Crashing
186 * while we hold it should be unlikely, and timing out such that we
187 * delete a stale lock should also be unlikely except when the
188 * filesystem is running really slowly. Stuff might break in corner
189 * cases but as long as it's not a security-level breakage it should
190 * be OK.
191 */
192
194#define MAX_LOCK_TIMEOUTS 32
196#define LOCK_TIMEOUT_MILLISECONDS 250
197
198static dbus_bool_t
199_dbus_keyring_lock (DBusKeyring *keyring)
200{
201 int n_timeouts;
202
203 n_timeouts = 0;
204 while (n_timeouts < MAX_LOCK_TIMEOUTS)
205 {
207
209 &error))
210 break;
211
212 _dbus_verbose ("Did not get lock file, sleeping %d milliseconds (%s)\n",
214 dbus_error_free (&error);
215
217
218 ++n_timeouts;
219 }
220
221 if (n_timeouts == MAX_LOCK_TIMEOUTS)
222 {
224
225 _dbus_verbose ("Lock file timed out %d times, assuming stale\n",
226 n_timeouts);
227
228 if (!_dbus_delete_file (&keyring->filename_lock, &error))
229 {
230 _dbus_verbose ("Couldn't delete old lock file: %s\n",
231 error.message);
232 dbus_error_free (&error);
233 return FALSE;
234 }
235
237 &error))
238 {
239 _dbus_verbose ("Couldn't create lock file after deleting stale one: %s\n",
240 error.message);
241 dbus_error_free (&error);
242 return FALSE;
243 }
244 }
245
246 return TRUE;
247}
248
249static void
250_dbus_keyring_unlock (DBusKeyring *keyring)
251{
253
254 if (!_dbus_delete_file (&keyring->filename_lock, &error))
255 {
256 _dbus_warn ("Failed to delete lock file: %s",
257 error.message);
258 dbus_error_free (&error);
259 }
260}
261
262static DBusKey*
263find_key_by_id (DBusKey *keys,
264 int n_keys,
265 int id)
266{
267 int i;
268
269 i = 0;
270 while (i < n_keys)
271 {
272 if (keys[i].id == id)
273 return &keys[i];
274
275 ++i;
276 }
277
278 return NULL;
279}
280
281static dbus_bool_t
282add_new_key (DBusKey **keys_p,
283 int *n_keys_p,
284 DBusError *error)
285{
286 DBusKey *new;
287 DBusString bytes;
288 int id;
289 long timestamp;
290 const unsigned char *s;
291 dbus_bool_t retval;
292 DBusKey *keys;
293 int n_keys;
294
295 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
296
297 if (!_dbus_string_init (&bytes))
298 {
300 return FALSE;
301 }
302
303 keys = *keys_p;
304 n_keys = *n_keys_p;
305 retval = FALSE;
306
307 /* Generate an integer ID and then the actual key. */
308 retry:
309
310 if (!_dbus_generate_random_bytes (&bytes, 4, error))
311 goto out;
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] & 0x7f) << 24);
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
390static 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
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
665{
666 keyring->refcount += 1;
667
668 return keyring;
669}
670
677void
679{
680 keyring->refcount -= 1;
681
682 if (keyring->refcount == 0)
683 {
684 if (keyring->credentials)
686
687 _dbus_string_free (&keyring->filename);
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 {
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;
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)
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
909static DBusKey*
910find_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
945int
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 {
971 "No recent-enough key found in keyring, and unable to create a new key");
972 return -1;
973 }
974}
975
986 DBusCredentials *credentials)
987{
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 (const char *test_data_dir _DBUS_GNUC_UNUSED)
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_credentials_same_user(DBusCredentials *credentials, DBusCredentials *other_credentials)
Check whether the user-identifying credentials in two credentials objects are identical.
DBusCredentials * _dbus_credentials_copy(DBusCredentials *credentials)
Copy a credentials object.
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...
void _dbus_credentials_unref(DBusCredentials *credentials)
Decrement refcount on credentials.
dbus_bool_t _dbus_credentials_are_anonymous(DBusCredentials *credentials)
Checks whether a credentials object contains a user identity.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:64
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:245
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
Definition: dbus-errors.c:190
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:356
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:213
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
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_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_file_get_contents(DBusString *str, const DBusString *filename, DBusError *error)
Appends the contents of the given file to the string, returning error code.
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
#define _DBUS_INT32_MAX
Maximum value of type "int32".
#define LOCK_TIMEOUT_MILLISECONDS
Length of each timeout while waiting for a lock.
Definition: dbus-keyring.c:196
#define MAX_TIME_TRAVEL_SECONDS
The maximum amount of time a key can be in the future.
Definition: dbus-keyring.c:80
#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:70
#define MAX_KEYS_IN_FILE
Maximum number of keys in the keyring before we just ignore the rest.
Definition: dbus-keyring.c:89
#define MAX_LOCK_TIMEOUTS
Maximum number of timeouts waiting for lock before we decide it's stale.
Definition: dbus-keyring.c:194
#define EXPIRE_KEYS_TIMEOUT_SECONDS
The time after which we drop a key from the secrets file.
Definition: dbus-keyring.c:76
int _dbus_keyring_get_best_key(DBusKeyring *keyring, DBusError *error)
Gets a recent key to use for authentication.
Definition: dbus-keyring.c:946
dbus_bool_t _dbus_keyring_validate_context(const DBusString *context)
Checks whether the context is a valid context.
Definition: dbus-keyring.c:850
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
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.
DBusKeyring * _dbus_keyring_ref(DBusKeyring *keyring)
Increments reference count of the keyring.
Definition: dbus-keyring.c:664
void _dbus_keyring_unref(DBusKeyring *keyring)
Decrements refcount and finalizes if it reaches zero.
Definition: dbus-keyring.c:678
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:694
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:594
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:60
#define DBUS_ERROR_NOT_SUPPORTED
Requested operation isn't supported (like ENOSYS on UNIX).
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:847
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:2432
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:980
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:182
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:197
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's copied to the d...
Definition: dbus-string.c:1345
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_append_int(DBusString *str, long value)
Appends an integer to a DBusString.
Definition: dbus-sysdeps.c:365
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:1865
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:1666
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:1827
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:278
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:1971
void _dbus_string_zero(DBusString *str)
Clears all allocated bytes in the string to zero.
Definition: dbus-string.c:2808
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:446
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:2573
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
Definition: dbus-string.c:784
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:1190
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:2382
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
Definition: dbus-string.c:513
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:1321
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
Definition: dbus-string.c:2075
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.
dbus_bool_t _dbus_check_setuid(void)
NOTE: If you modify this function, please also consider making the corresponding change in GLib.
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
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.
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...
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.
void _dbus_get_real_time(long *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
dbus_bool_t _dbus_ensure_directory(const DBusString *filename, DBusError *error)
Creates a directory; succeeds if the directory is created or already existed.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:37
int dbus_int32_t
A 32-bit signed integer on all platforms.
Object representing an exception.
Definition: dbus-errors.h:51
const char * name
public error name field
Definition: dbus-errors.h:52
const char * message
public error message field
Definition: dbus-errors.h:53
A single key from the cookie file.
Definition: dbus-keyring.c:96
DBusString secret
the actual key
Definition: dbus-keyring.c:104
long creation_time
when the key was generated, as unix timestamp.
Definition: dbus-keyring.c:99
dbus_int32_t id
identifier used to refer to the key
Definition: dbus-keyring.c:97
Internals of DBusKeyring.
Definition: dbus-keyring.c:115
DBusKey * keys
Keys loaded from the file.
Definition: dbus-keyring.c:120
DBusString filename
Keyring filename.
Definition: dbus-keyring.c:118
DBusCredentials * credentials
Credentials containing user the keyring is for.
Definition: dbus-keyring.c:122
int n_keys
Number of keys.
Definition: dbus-keyring.c:121
int refcount
Reference count.
Definition: dbus-keyring.c:116
DBusString directory
Directory the below two items are inside.
Definition: dbus-keyring.c:117
DBusString filename_lock
Name of lockfile.
Definition: dbus-keyring.c:119