D-Bus: dbus-keyring.c Source File

D-Bus 1.14.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 * 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)
74#define EXPIRE_KEYS_TIMEOUT_SECONDS (NEW_KEY_TIMEOUT_SECONDS + (60*2))
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;
97 long creation_time;
102 DBusString secret;
104} DBusKey;
105
112 struct DBusKeyring
113{
114 int refcount;
115 DBusString directory;
116 DBusString filename;
117 DBusString filename_lock;
118 DBusKey *keys;
119 int n_keys;
120 DBusCredentials *credentials;
121};
122
123static 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
157static void
158free_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
194#define LOCK_TIMEOUT_MILLISECONDS 250
195
196static 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
206 if (_dbus_create_file_exclusively (&keyring->filename_lock,
207 &error))
208 break;
209
210 _dbus_verbose ("Did not get lock file, sleeping %d milliseconds (%s)\n",
211 LOCK_TIMEOUT_MILLISECONDS, error.message);
212 dbus_error_free (&error);
213
214 _dbus_sleep_milliseconds (LOCK_TIMEOUT_MILLISECONDS);
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
234 if (!_dbus_create_file_exclusively (&keyring->filename_lock,
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
247static 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
260static DBusKey*
261find_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
279static dbus_bool_t
280add_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 {
297 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 {
336 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 */
347 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 {
359 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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
388static 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 {
410 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
411 return FALSE;
412 }
413
414 if (!_dbus_string_init (&line))
415 {
416 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 {
432 dbus_set_error (error, DBUS_ERROR_FAILED,
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 {
521 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 */
531 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 {
540 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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:
596 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 *
662_dbus_keyring_ref (DBusKeyring *keyring)
663{
664 keyring->refcount += 1;
665
666 return keyring;
667}
668
675void
676_dbus_keyring_unref (DBusKeyring *keyring)
677{
678 keyring->refcount -= 1;
679
680 if (keyring->refcount == 0)
681 {
682 if (keyring->credentials)
683 _dbus_credentials_unref (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
703 DBusKeyring*
704_dbus_keyring_new_for_credentials (DBusCredentials *credentials,
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 {
718 dbus_set_error_const (error, DBUS_ERROR_NOT_SUPPORTED,
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 {
729 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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
751 if (!_dbus_append_keyring_directory_for_credentials (&ringdir,
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,
768 DBUS_ERROR_FAILED,
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,
824 DBUS_ERROR_NO_MEMORY,
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
847dbus_bool_t
848_dbus_keyring_validate_context (const DBusString *context)
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
907static DBusKey*
908find_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
943int
944_dbus_keyring_get_best_key (DBusKeyring *keyring,
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,
968 DBUS_ERROR_FAILED,
969 "No recent-enough key found in keyring, and unable to create a new key");
970 return -1;
971 }
972}
973
982dbus_bool_t
983_dbus_keyring_is_for_credentials (DBusKeyring *keyring,
984 DBusCredentials *credentials)
985{
986 return _dbus_credentials_same_user (keyring->credentials,
987 credentials);
988}
989
1001dbus_bool_t
1002_dbus_keyring_get_hex_key (DBusKeyring *keyring,
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
1025dbus_bool_t
1026_dbus_keyring_test (const char *test_data_dir _DBUS_GNUC_UNUSED)
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");
1041 _dbus_assert (_dbus_keyring_validate_context (&context));
1042 _dbus_string_init_const (&context, "org_freedesktop_blah");
1043 _dbus_assert (_dbus_keyring_validate_context (&context));
1044
1045 _dbus_string_init_const (&context, "");
1046 _dbus_assert (!_dbus_keyring_validate_context (&context));
1047 _dbus_string_init_const (&context, ".foo");
1048 _dbus_assert (!_dbus_keyring_validate_context (&context));
1049 _dbus_string_init_const (&context, "bar.foo");
1050 _dbus_assert (!_dbus_keyring_validate_context (&context));
1051 _dbus_string_init_const (&context, "bar/foo");
1052 _dbus_assert (!_dbus_keyring_validate_context (&context));
1053 _dbus_string_init_const (&context, "bar\\foo");
1054 _dbus_assert (!_dbus_keyring_validate_context (&context));
1055 _dbus_string_init_const (&context, "foo\xfa\xf0");
1056 _dbus_assert (!_dbus_keyring_validate_context (&context));
1057 _dbus_string_init_const (&context, "foo\x80");
1058 _dbus_assert (!_dbus_keyring_validate_context (&context));
1059 _dbus_string_init_const (&context, "foo\x7f");
1060 _dbus_assert (_dbus_keyring_validate_context (&context));
1061 _dbus_string_init_const (&context, "foo bar");
1062 _dbus_assert (!_dbus_keyring_validate_context (&context));
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");
1068 _dbus_assert (!_dbus_keyring_validate_context (&context));
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_credentials_same_user
dbus_bool_t _dbus_credentials_same_user(DBusCredentials *credentials, DBusCredentials *other_credentials)
Check whether the user-identifying credentials in two credentials objects are identical.
Definition dbus-credentials.c:645
_dbus_credentials_copy
DBusCredentials * _dbus_credentials_copy(DBusCredentials *credentials)
Copy a credentials object.
Definition dbus-credentials.c:616
_dbus_credentials_new_from_current_process
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...
Definition dbus-credentials.c:100
_dbus_credentials_unref
void _dbus_credentials_unref(DBusCredentials *credentials)
Decrement refcount on credentials.
Definition dbus-credentials.c:135
_dbus_credentials_are_anonymous
dbus_bool_t _dbus_credentials_are_anonymous(DBusCredentials *credentials)
Checks whether a credentials object contains a user identity.
Definition dbus-credentials.c:479
DBUS_ERROR_INIT
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition dbus-errors.h:62
dbus_set_error_const
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_error_init
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
Definition dbus-errors.c:188
dbus_set_error
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
dbus_error_free
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:211
_dbus_delete_file
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
Definition dbus-file-unix.c:397
_dbus_create_file_exclusively
dbus_bool_t _dbus_create_file_exclusively(const DBusString *filename, DBusError *error)
Creates the given file, failing if the file already exists.
Definition dbus-file-unix.c:351
_dbus_string_save_to_file
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.
Definition dbus-file-unix.c:164
_dbus_file_get_contents
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.
Definition dbus-file-unix.c:55
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
Definition dbus-internals.h:164
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
_dbus_warn
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
Definition dbus-internals.c:238
_DBUS_INT32_MAX
#define _DBUS_INT32_MAX
Maximum value of type "int32".
Definition dbus-internals.h:307
LOCK_TIMEOUT_MILLISECONDS
#define LOCK_TIMEOUT_MILLISECONDS
Length of each timeout while waiting for a lock.
Definition dbus-keyring.c:194
MAX_TIME_TRAVEL_SECONDS
#define MAX_TIME_TRAVEL_SECONDS
The maximum amount of time a key can be in the future.
Definition dbus-keyring.c:78
NEW_KEY_TIMEOUT_SECONDS
#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
MAX_KEYS_IN_FILE
#define MAX_KEYS_IN_FILE
Maximum number of keys in the keyring before we just ignore the rest.
Definition dbus-keyring.c:87
MAX_LOCK_TIMEOUTS
#define MAX_LOCK_TIMEOUTS
Maximum number of timeouts waiting for lock before we decide it's stale.
Definition dbus-keyring.c:192
EXPIRE_KEYS_TIMEOUT_SECONDS
#define EXPIRE_KEYS_TIMEOUT_SECONDS
The time after which we drop a key from the secrets file.
Definition dbus-keyring.c:74
_dbus_keyring_get_best_key
int _dbus_keyring_get_best_key(DBusKeyring *keyring, DBusError *error)
Gets a recent key to use for authentication.
Definition dbus-keyring.c:944
_dbus_keyring_validate_context
dbus_bool_t _dbus_keyring_validate_context(const DBusString *context)
Checks whether the context is a valid context.
Definition dbus-keyring.c:848
_dbus_keyring_is_for_credentials
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
_dbus_keyring_new_for_credentials
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
_dbus_keyring_get_hex_key
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.
Definition dbus-keyring.c:1002
_dbus_keyring_ref
DBusKeyring * _dbus_keyring_ref(DBusKeyring *keyring)
Increments reference count of the keyring.
Definition dbus-keyring.c:662
_dbus_keyring_unref
void _dbus_keyring_unref(DBusKeyring *keyring)
Decrements refcount and finalizes if it reaches zero.
Definition dbus-keyring.c:676
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:49
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:39
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:42
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:692
dbus_realloc
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:592
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition dbus-memory.h:58
DBUS_ERROR_NOT_SUPPORTED
#define DBUS_ERROR_NOT_SUPPORTED
Requested operation isn't supported (like ENOSYS on UNIX).
Definition dbus-protocol.h:373
DBUS_ERROR_FAILED
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
Definition dbus-protocol.h:359
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition dbus-protocol.h:361
_dbus_string_set_length
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition dbus-string.c:833
_dbus_string_hex_decode
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:2395
_dbus_string_append
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:966
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition dbus-string.c:182
_dbus_string_init_const
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition dbus-string.c:197
_dbus_string_copy
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:1343
_dbus_string_append_int
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
_dbus_string_skip_blank
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:1863
_dbus_string_find
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:1664
_dbus_string_find_blank
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:1825
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as _DBUS_STRING_IN...
Definition dbus-string.c:278
_dbus_string_pop_line
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:1969
_dbus_string_zero
void _dbus_string_zero(DBusString *str)
Clears all allocated bytes in the string to zero.
Definition dbus-string.c:2771
_dbus_string_parse_int
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_string_validate_ascii
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:2536
_dbus_string_append_byte
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:1188
_dbus_string_hex_encode
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:2345
_dbus_string_move
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:1319
_dbus_string_equal
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
Definition dbus-string.c:2073
_dbus_append_keyring_directory_for_credentials
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.
Definition dbus-sysdeps-unix.c:4569
_dbus_check_setuid
dbus_bool_t _dbus_check_setuid(void)
NOTE: If you modify this function, please also consider making the corresponding change in GLib.
Definition dbus-sysdeps-unix.c:4822
_dbus_sleep_milliseconds
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
Definition dbus-sysdeps-unix.c:3381
_dbus_check_dir_is_private_to_user
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.
Definition dbus-sysdeps-unix.c:2596
_dbus_credentials_add_from_current_process
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...
Definition dbus-sysdeps-unix.c:2942
_dbus_generate_random_bytes
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.
Definition dbus-sysdeps-unix.c:3411
_dbus_get_real_time
void _dbus_get_real_time(long *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
Definition dbus-sysdeps-unix.c:3251
_dbus_concat_dir_and_file
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
Definition dbus-sysdeps-unix.c:3336
_dbus_ensure_directory
dbus_bool_t _dbus_ensure_directory(const DBusString *filename, DBusError *error)
Creates a directory; succeeds if the directory is created or already existed.
Definition dbus-sysdeps-unix.c:3273
DBusCredentials
Definition dbus-credentials.c:49
DBusError
Object representing an exception.
Definition dbus-errors.h:49
DBusError::name
const char * name
public error name field
Definition dbus-errors.h:50
DBusError::message
const char * message
public error message field
Definition dbus-errors.h:51
DBusKey
A single key from the cookie file.
Definition dbus-keyring.c:94
DBusKey::secret
DBusString secret
the actual key
Definition dbus-keyring.c:102
DBusKey::creation_time
long creation_time
when the key was generated, as unix timestamp.
Definition dbus-keyring.c:97
DBusKey::id
dbus_int32_t id
identifier used to refer to the key
Definition dbus-keyring.c:95
DBusKeyring
Internals of DBusKeyring.
Definition dbus-keyring.c:113
DBusKeyring::keys
DBusKey * keys
Keys loaded from the file.
Definition dbus-keyring.c:118
DBusKeyring::filename
DBusString filename
Keyring filename.
Definition dbus-keyring.c:116
DBusKeyring::credentials
DBusCredentials * credentials
Credentials containing user the keyring is for.
Definition dbus-keyring.c:120
DBusKeyring::n_keys
int n_keys
Number of keys.
Definition dbus-keyring.c:119
DBusKeyring::refcount
int refcount
Reference count.
Definition dbus-keyring.c:114
DBusKeyring::directory
DBusString directory
Directory the below two items are inside.
Definition dbus-keyring.c:115
DBusKeyring::filename_lock
DBusString filename_lock
Name of lockfile.
Definition dbus-keyring.c:117
DBusString
Definition dbus-string.h:43