|
D-Bus
1.8.20
|
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ 00002 /* dbus-keyring.c Store secret cookies in your homedir 00003 * 00004 * Copyright (C) 2003, 2004 Red Hat Inc. 00005 * 00006 * Licensed under the Academic Free License version 2.1 00007 * 00008 * This program is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 * 00022 */ 00023 00024 #include <config.h> 00025 #include "dbus-keyring.h" 00026 #include "dbus-protocol.h" 00027 #include <dbus/dbus-string.h> 00028 #include <dbus/dbus-list.h> 00029 #include <dbus/dbus-sysdeps.h> 00030 00067 #define NEW_KEY_TIMEOUT_SECONDS (60*5) 00068 00073 #define EXPIRE_KEYS_TIMEOUT_SECONDS (NEW_KEY_TIMEOUT_SECONDS + (60*2)) 00074 00077 #define MAX_TIME_TRAVEL_SECONDS (60*5) 00078 00083 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 00084 #define MAX_KEYS_IN_FILE 10 00085 #else 00086 #define MAX_KEYS_IN_FILE 256 00087 #endif 00088 00092 typedef struct 00093 { 00094 dbus_int32_t id; 00096 long creation_time; 00101 DBusString secret; 00103 } DBusKey; 00104 00111 struct DBusKeyring 00112 { 00113 int refcount; 00114 DBusString directory; 00115 DBusString filename; 00116 DBusString filename_lock; 00117 DBusKey *keys; 00118 int n_keys; 00119 DBusCredentials *credentials; 00120 }; 00121 00122 static DBusKeyring* 00123 _dbus_keyring_new (void) 00124 { 00125 DBusKeyring *keyring; 00126 00127 keyring = dbus_new0 (DBusKeyring, 1); 00128 if (keyring == NULL) 00129 goto out_0; 00130 00131 if (!_dbus_string_init (&keyring->directory)) 00132 goto out_1; 00133 00134 if (!_dbus_string_init (&keyring->filename)) 00135 goto out_2; 00136 00137 if (!_dbus_string_init (&keyring->filename_lock)) 00138 goto out_3; 00139 00140 keyring->refcount = 1; 00141 keyring->keys = NULL; 00142 keyring->n_keys = 0; 00143 00144 return keyring; 00145 00146 out_3: 00147 _dbus_string_free (&keyring->filename); 00148 out_2: 00149 _dbus_string_free (&keyring->directory); 00150 out_1: 00151 dbus_free (keyring); 00152 out_0: 00153 return NULL; 00154 } 00155 00156 static void 00157 free_keys (DBusKey *keys, 00158 int n_keys) 00159 { 00160 int i; 00161 00162 /* should be safe for args NULL, 0 */ 00163 00164 i = 0; 00165 while (i < n_keys) 00166 { 00167 _dbus_string_free (&keys[i].secret); 00168 ++i; 00169 } 00170 00171 dbus_free (keys); 00172 } 00173 00174 /* Our locking scheme is highly unreliable. However, there is 00175 * unfortunately no reliable locking scheme in user home directories; 00176 * between bugs in Linux NFS, people using Tru64 or other total crap 00177 * NFS, AFS, random-file-system-of-the-week, and so forth, fcntl() in 00178 * homedirs simply generates tons of bug reports. This has been 00179 * learned through hard experience with GConf, unfortunately. 00180 * 00181 * This bad hack might work better for the kind of lock we have here, 00182 * which we don't expect to hold for any length of time. Crashing 00183 * while we hold it should be unlikely, and timing out such that we 00184 * delete a stale lock should also be unlikely except when the 00185 * filesystem is running really slowly. Stuff might break in corner 00186 * cases but as long as it's not a security-level breakage it should 00187 * be OK. 00188 */ 00189 00191 #define MAX_LOCK_TIMEOUTS 32 00192 00193 #define LOCK_TIMEOUT_MILLISECONDS 250 00194 00195 static dbus_bool_t 00196 _dbus_keyring_lock (DBusKeyring *keyring) 00197 { 00198 int n_timeouts; 00199 00200 n_timeouts = 0; 00201 while (n_timeouts < MAX_LOCK_TIMEOUTS) 00202 { 00203 DBusError error = DBUS_ERROR_INIT; 00204 00205 if (_dbus_create_file_exclusively (&keyring->filename_lock, 00206 &error)) 00207 break; 00208 00209 _dbus_verbose ("Did not get lock file, sleeping %d milliseconds (%s)\n", 00210 LOCK_TIMEOUT_MILLISECONDS, error.message); 00211 dbus_error_free (&error); 00212 00213 _dbus_sleep_milliseconds (LOCK_TIMEOUT_MILLISECONDS); 00214 00215 ++n_timeouts; 00216 } 00217 00218 if (n_timeouts == MAX_LOCK_TIMEOUTS) 00219 { 00220 DBusError error = DBUS_ERROR_INIT; 00221 00222 _dbus_verbose ("Lock file timed out %d times, assuming stale\n", 00223 n_timeouts); 00224 00225 if (!_dbus_delete_file (&keyring->filename_lock, &error)) 00226 { 00227 _dbus_verbose ("Couldn't delete old lock file: %s\n", 00228 error.message); 00229 dbus_error_free (&error); 00230 return FALSE; 00231 } 00232 00233 if (!_dbus_create_file_exclusively (&keyring->filename_lock, 00234 &error)) 00235 { 00236 _dbus_verbose ("Couldn't create lock file after deleting stale one: %s\n", 00237 error.message); 00238 dbus_error_free (&error); 00239 return FALSE; 00240 } 00241 } 00242 00243 return TRUE; 00244 } 00245 00246 static void 00247 _dbus_keyring_unlock (DBusKeyring *keyring) 00248 { 00249 DBusError error = DBUS_ERROR_INIT; 00250 00251 if (!_dbus_delete_file (&keyring->filename_lock, &error)) 00252 { 00253 _dbus_warn ("Failed to delete lock file: %s\n", 00254 error.message); 00255 dbus_error_free (&error); 00256 } 00257 } 00258 00259 static DBusKey* 00260 find_key_by_id (DBusKey *keys, 00261 int n_keys, 00262 int id) 00263 { 00264 int i; 00265 00266 i = 0; 00267 while (i < n_keys) 00268 { 00269 if (keys[i].id == id) 00270 return &keys[i]; 00271 00272 ++i; 00273 } 00274 00275 return NULL; 00276 } 00277 00278 static dbus_bool_t 00279 add_new_key (DBusKey **keys_p, 00280 int *n_keys_p, 00281 DBusError *error) 00282 { 00283 DBusKey *new; 00284 DBusString bytes; 00285 int id; 00286 long timestamp; 00287 const unsigned char *s; 00288 dbus_bool_t retval; 00289 DBusKey *keys; 00290 int n_keys; 00291 00292 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00293 00294 if (!_dbus_string_init (&bytes)) 00295 { 00296 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00297 return FALSE; 00298 } 00299 00300 keys = *keys_p; 00301 n_keys = *n_keys_p; 00302 retval = FALSE; 00303 00304 /* Generate an integer ID and then the actual key. */ 00305 retry: 00306 00307 if (!_dbus_generate_random_bytes (&bytes, 4)) 00308 { 00309 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00310 goto out; 00311 } 00312 00313 s = (const unsigned char*) _dbus_string_get_const_data (&bytes); 00314 00315 id = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24); 00316 if (id < 0) 00317 id = - id; 00318 _dbus_assert (id >= 0); 00319 00320 if (find_key_by_id (keys, n_keys, id) != NULL) 00321 { 00322 _dbus_string_set_length (&bytes, 0); 00323 _dbus_verbose ("Key ID %d already existed, trying another one\n", 00324 id); 00325 goto retry; 00326 } 00327 00328 _dbus_verbose ("Creating key with ID %d\n", id); 00329 00330 #define KEY_LENGTH_BYTES 24 00331 _dbus_string_set_length (&bytes, 0); 00332 if (!_dbus_generate_random_bytes (&bytes, KEY_LENGTH_BYTES)) 00333 { 00334 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00335 goto out; 00336 } 00337 00338 new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1)); 00339 if (new == NULL) 00340 { 00341 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00342 goto out; 00343 } 00344 00345 keys = new; 00346 *keys_p = keys; /* otherwise *keys_p ends up invalid */ 00347 n_keys += 1; 00348 00349 if (!_dbus_string_init (&keys[n_keys-1].secret)) 00350 { 00351 n_keys -= 1; /* we don't want to free the one we didn't init */ 00352 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00353 goto out; 00354 } 00355 00356 _dbus_get_real_time (×tamp, NULL); 00357 00358 keys[n_keys-1].id = id; 00359 keys[n_keys-1].creation_time = timestamp; 00360 if (!_dbus_string_move (&bytes, 0, 00361 &keys[n_keys-1].secret, 00362 0)) 00363 { 00364 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00365 _dbus_string_free (&keys[n_keys-1].secret); 00366 n_keys -= 1; 00367 goto out; 00368 } 00369 00370 retval = TRUE; 00371 00372 out: 00373 *n_keys_p = n_keys; 00374 00375 _dbus_string_free (&bytes); 00376 return retval; 00377 } 00378 00393 static dbus_bool_t 00394 _dbus_keyring_reload (DBusKeyring *keyring, 00395 dbus_bool_t add_new, 00396 DBusError *error) 00397 { 00398 DBusString contents; 00399 DBusString line; 00400 dbus_bool_t retval; 00401 dbus_bool_t have_lock; 00402 DBusKey *keys; 00403 int n_keys; 00404 int i; 00405 long now; 00406 DBusError tmp_error; 00407 00408 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00409 00410 if (!_dbus_check_dir_is_private_to_user (&keyring->directory, error)) 00411 return FALSE; 00412 00413 if (!_dbus_string_init (&contents)) 00414 { 00415 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00416 return FALSE; 00417 } 00418 00419 if (!_dbus_string_init (&line)) 00420 { 00421 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00422 _dbus_string_free (&contents); 00423 return FALSE; 00424 } 00425 00426 keys = NULL; 00427 n_keys = 0; 00428 retval = FALSE; 00429 have_lock = FALSE; 00430 00431 _dbus_get_real_time (&now, NULL); 00432 00433 if (add_new) 00434 { 00435 if (!_dbus_keyring_lock (keyring)) 00436 { 00437 dbus_set_error (error, DBUS_ERROR_FAILED, 00438 "Could not lock keyring file to add to it"); 00439 goto out; 00440 } 00441 00442 have_lock = TRUE; 00443 } 00444 00445 dbus_error_init (&tmp_error); 00446 if (!_dbus_file_get_contents (&contents, 00447 &keyring->filename, 00448 &tmp_error)) 00449 { 00450 _dbus_verbose ("Failed to load keyring file: %s\n", 00451 tmp_error.message); 00452 /* continue with empty keyring file, so we recreate it */ 00453 dbus_error_free (&tmp_error); 00454 } 00455 00456 if (!_dbus_string_validate_ascii (&contents, 0, 00457 _dbus_string_get_length (&contents))) 00458 { 00459 _dbus_warn ("Secret keyring file contains non-ASCII! Ignoring existing contents\n"); 00460 _dbus_string_set_length (&contents, 0); 00461 } 00462 00463 /* FIXME this is badly inefficient for large keyring files 00464 * (not that large keyring files exist outside of test suites) 00465 */ 00466 while (_dbus_string_pop_line (&contents, &line)) 00467 { 00468 int next; 00469 long val; 00470 int id; 00471 long timestamp; 00472 int len; 00473 int end; 00474 DBusKey *new; 00475 00476 /* Don't load more than the max. */ 00477 if (n_keys >= (add_new ? MAX_KEYS_IN_FILE - 1 : MAX_KEYS_IN_FILE)) 00478 break; 00479 00480 next = 0; 00481 if (!_dbus_string_parse_int (&line, 0, &val, &next)) 00482 { 00483 _dbus_verbose ("could not parse secret key ID at start of line\n"); 00484 continue; 00485 } 00486 00487 if (val > _DBUS_INT32_MAX || val < 0) 00488 { 00489 _dbus_verbose ("invalid secret key ID at start of line\n"); 00490 continue; 00491 } 00492 00493 id = val; 00494 00495 _dbus_string_skip_blank (&line, next, &next); 00496 00497 if (!_dbus_string_parse_int (&line, next, ×tamp, &next)) 00498 { 00499 _dbus_verbose ("could not parse secret key timestamp\n"); 00500 continue; 00501 } 00502 00503 if (timestamp < 0 || 00504 (now + MAX_TIME_TRAVEL_SECONDS) < timestamp || 00505 (now - EXPIRE_KEYS_TIMEOUT_SECONDS) > timestamp) 00506 { 00507 _dbus_verbose ("dropping/ignoring %ld-seconds old key with timestamp %ld as current time is %ld\n", 00508 now - timestamp, timestamp, now); 00509 continue; 00510 } 00511 00512 _dbus_string_skip_blank (&line, next, &next); 00513 00514 len = _dbus_string_get_length (&line); 00515 00516 if ((len - next) == 0) 00517 { 00518 _dbus_verbose ("no secret key after ID and timestamp\n"); 00519 continue; 00520 } 00521 00522 /* We have all three parts */ 00523 new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1)); 00524 if (new == NULL) 00525 { 00526 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00527 goto out; 00528 } 00529 00530 keys = new; 00531 n_keys += 1; 00532 00533 if (!_dbus_string_init (&keys[n_keys-1].secret)) 00534 { 00535 n_keys -= 1; /* we don't want to free the one we didn't init */ 00536 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00537 goto out; 00538 } 00539 00540 keys[n_keys-1].id = id; 00541 keys[n_keys-1].creation_time = timestamp; 00542 if (!_dbus_string_hex_decode (&line, next, &end, 00543 &keys[n_keys-1].secret, 0)) 00544 { 00545 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00546 goto out; 00547 } 00548 00549 if (_dbus_string_get_length (&line) != end) 00550 { 00551 _dbus_verbose ("invalid hex encoding in keyring file\n"); 00552 _dbus_string_free (&keys[n_keys - 1].secret); 00553 n_keys -= 1; 00554 continue; 00555 } 00556 } 00557 00558 _dbus_verbose ("Successfully loaded %d existing keys\n", 00559 n_keys); 00560 00561 if (add_new) 00562 { 00563 if (!add_new_key (&keys, &n_keys, error)) 00564 { 00565 _dbus_verbose ("Failed to generate new key: %s\n", 00566 error ? error->message : "(unknown)"); 00567 goto out; 00568 } 00569 00570 _dbus_string_set_length (&contents, 0); 00571 00572 i = 0; 00573 while (i < n_keys) 00574 { 00575 if (!_dbus_string_append_int (&contents, 00576 keys[i].id)) 00577 goto nomem; 00578 00579 if (!_dbus_string_append_byte (&contents, ' ')) 00580 goto nomem; 00581 00582 if (!_dbus_string_append_int (&contents, 00583 keys[i].creation_time)) 00584 goto nomem; 00585 00586 if (!_dbus_string_append_byte (&contents, ' ')) 00587 goto nomem; 00588 00589 if (!_dbus_string_hex_encode (&keys[i].secret, 0, 00590 &contents, 00591 _dbus_string_get_length (&contents))) 00592 goto nomem; 00593 00594 if (!_dbus_string_append_byte (&contents, '\n')) 00595 goto nomem; 00596 00597 ++i; 00598 continue; 00599 00600 nomem: 00601 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00602 goto out; 00603 } 00604 00605 if (!_dbus_string_save_to_file (&contents, &keyring->filename, 00606 FALSE, error)) 00607 goto out; 00608 } 00609 00610 if (keyring->keys) 00611 free_keys (keyring->keys, keyring->n_keys); 00612 keyring->keys = keys; 00613 keyring->n_keys = n_keys; 00614 keys = NULL; 00615 n_keys = 0; 00616 00617 retval = TRUE; 00618 00619 out: 00620 if (have_lock) 00621 _dbus_keyring_unlock (keyring); 00622 00623 if (! ((retval == TRUE && (error == NULL || error->name == NULL)) || 00624 (retval == FALSE && (error == NULL || error->name != NULL)))) 00625 { 00626 if (error && error->name) 00627 _dbus_verbose ("error is %s: %s\n", error->name, error->message); 00628 _dbus_warn ("returning %d but error pointer %p name %s\n", 00629 retval, error, error->name ? error->name : "(none)"); 00630 _dbus_assert_not_reached ("didn't handle errors properly"); 00631 } 00632 00633 if (keys != NULL) 00634 { 00635 i = 0; 00636 while (i < n_keys) 00637 { 00638 _dbus_string_zero (&keys[i].secret); 00639 _dbus_string_free (&keys[i].secret); 00640 ++i; 00641 } 00642 00643 dbus_free (keys); 00644 } 00645 00646 _dbus_string_free (&contents); 00647 _dbus_string_free (&line); 00648 00649 return retval; 00650 } 00651 /* end of internals */ 00653 00666 DBusKeyring * 00667 _dbus_keyring_ref (DBusKeyring *keyring) 00668 { 00669 keyring->refcount += 1; 00670 00671 return keyring; 00672 } 00673 00680 void 00681 _dbus_keyring_unref (DBusKeyring *keyring) 00682 { 00683 keyring->refcount -= 1; 00684 00685 if (keyring->refcount == 0) 00686 { 00687 if (keyring->credentials) 00688 _dbus_credentials_unref (keyring->credentials); 00689 00690 _dbus_string_free (&keyring->filename); 00691 _dbus_string_free (&keyring->filename_lock); 00692 _dbus_string_free (&keyring->directory); 00693 free_keys (keyring->keys, keyring->n_keys); 00694 dbus_free (keyring); 00695 } 00696 } 00697 00708 DBusKeyring* 00709 _dbus_keyring_new_for_credentials (DBusCredentials *credentials, 00710 const DBusString *context, 00711 DBusError *error) 00712 { 00713 DBusString ringdir; 00714 DBusKeyring *keyring; 00715 dbus_bool_t error_set; 00716 DBusError tmp_error; 00717 DBusCredentials *our_credentials; 00718 00719 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00720 00721 00722 keyring = NULL; 00723 error_set = FALSE; 00724 our_credentials = NULL; 00725 00726 if (!_dbus_string_init (&ringdir)) 00727 { 00728 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00729 return NULL; 00730 } 00731 00732 if (credentials != NULL) 00733 { 00734 our_credentials = _dbus_credentials_copy (credentials); 00735 } 00736 else 00737 { 00738 our_credentials = _dbus_credentials_new_from_current_process (); 00739 } 00740 00741 if (our_credentials == NULL) 00742 goto failed; 00743 00744 if (_dbus_credentials_are_anonymous (our_credentials)) 00745 { 00746 if (!_dbus_credentials_add_from_current_process (our_credentials)) 00747 goto failed; 00748 } 00749 00750 if (!_dbus_append_keyring_directory_for_credentials (&ringdir, 00751 our_credentials)) 00752 goto failed; 00753 00754 keyring = _dbus_keyring_new (); 00755 if (keyring == NULL) 00756 goto failed; 00757 00758 _dbus_assert (keyring->credentials == NULL); 00759 keyring->credentials = our_credentials; 00760 our_credentials = NULL; /* so we don't unref it again later */ 00761 00762 /* should have been validated already, but paranoia check here */ 00763 if (!_dbus_keyring_validate_context (context)) 00764 { 00765 error_set = TRUE; 00766 dbus_set_error_const (error, 00767 DBUS_ERROR_FAILED, 00768 "Invalid context in keyring creation"); 00769 goto failed; 00770 } 00771 00772 /* Save keyring dir in the keyring object */ 00773 if (!_dbus_string_copy (&ringdir, 0, 00774 &keyring->directory, 0)) 00775 goto failed; 00776 00777 /* Create keyring->filename based on keyring dir and context */ 00778 if (!_dbus_string_copy (&keyring->directory, 0, 00779 &keyring->filename, 0)) 00780 goto failed; 00781 00782 if (!_dbus_concat_dir_and_file (&keyring->filename, 00783 context)) 00784 goto failed; 00785 00786 /* Create lockfile name */ 00787 if (!_dbus_string_copy (&keyring->filename, 0, 00788 &keyring->filename_lock, 0)) 00789 goto failed; 00790 00791 if (!_dbus_string_append (&keyring->filename_lock, ".lock")) 00792 goto failed; 00793 00794 /* Reload keyring */ 00795 dbus_error_init (&tmp_error); 00796 if (!_dbus_keyring_reload (keyring, FALSE, &tmp_error)) 00797 { 00798 _dbus_verbose ("didn't load an existing keyring: %s\n", 00799 tmp_error.message); 00800 dbus_error_free (&tmp_error); 00801 } 00802 00803 /* We don't fail fatally if we can't create the directory, 00804 * but the keyring will probably always be empty 00805 * unless someone else manages to create it 00806 */ 00807 dbus_error_init (&tmp_error); 00808 if (!_dbus_create_directory (&keyring->directory, 00809 &tmp_error)) 00810 { 00811 _dbus_verbose ("Creating keyring directory: %s\n", 00812 tmp_error.message); 00813 dbus_error_free (&tmp_error); 00814 } 00815 00816 _dbus_string_free (&ringdir); 00817 00818 return keyring; 00819 00820 failed: 00821 if (!error_set) 00822 dbus_set_error_const (error, 00823 DBUS_ERROR_NO_MEMORY, 00824 NULL); 00825 if (our_credentials) 00826 _dbus_credentials_unref (our_credentials); 00827 if (keyring) 00828 _dbus_keyring_unref (keyring); 00829 _dbus_string_free (&ringdir); 00830 return NULL; 00831 00832 } 00833 00846 dbus_bool_t 00847 _dbus_keyring_validate_context (const DBusString *context) 00848 { 00849 if (_dbus_string_get_length (context) == 0) 00850 { 00851 _dbus_verbose ("context is zero-length\n"); 00852 return FALSE; 00853 } 00854 00855 if (!_dbus_string_validate_ascii (context, 0, 00856 _dbus_string_get_length (context))) 00857 { 00858 _dbus_verbose ("context not valid ascii\n"); 00859 return FALSE; 00860 } 00861 00862 /* no directory separators */ 00863 if (_dbus_string_find (context, 0, "/", NULL)) 00864 { 00865 _dbus_verbose ("context contains a slash\n"); 00866 return FALSE; 00867 } 00868 00869 if (_dbus_string_find (context, 0, "\\", NULL)) 00870 { 00871 _dbus_verbose ("context contains a backslash\n"); 00872 return FALSE; 00873 } 00874 00875 /* prevent attempts to use dotfiles or ".." or ".lock" 00876 * all of which might allow some kind of attack 00877 */ 00878 if (_dbus_string_find (context, 0, ".", NULL)) 00879 { 00880 _dbus_verbose ("context contains a dot\n"); 00881 return FALSE; 00882 } 00883 00884 /* no spaces/tabs, those are used for separators in the protocol */ 00885 if (_dbus_string_find_blank (context, 0, NULL)) 00886 { 00887 _dbus_verbose ("context contains a blank\n"); 00888 return FALSE; 00889 } 00890 00891 if (_dbus_string_find (context, 0, "\n", NULL)) 00892 { 00893 _dbus_verbose ("context contains a newline\n"); 00894 return FALSE; 00895 } 00896 00897 if (_dbus_string_find (context, 0, "\r", NULL)) 00898 { 00899 _dbus_verbose ("context contains a carriage return\n"); 00900 return FALSE; 00901 } 00902 00903 return TRUE; 00904 } 00905 00906 static DBusKey* 00907 find_recent_key (DBusKeyring *keyring) 00908 { 00909 int i; 00910 long tv_sec, tv_usec; 00911 00912 _dbus_get_real_time (&tv_sec, &tv_usec); 00913 00914 i = 0; 00915 while (i < keyring->n_keys) 00916 { 00917 DBusKey *key = &keyring->keys[i]; 00918 00919 _dbus_verbose ("Key %d is %ld seconds old\n", 00920 i, tv_sec - key->creation_time); 00921 00922 if ((tv_sec - NEW_KEY_TIMEOUT_SECONDS) < key->creation_time) 00923 return key; 00924 00925 ++i; 00926 } 00927 00928 return NULL; 00929 } 00930 00942 int 00943 _dbus_keyring_get_best_key (DBusKeyring *keyring, 00944 DBusError *error) 00945 { 00946 DBusKey *key; 00947 00948 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00949 00950 key = find_recent_key (keyring); 00951 if (key) 00952 return key->id; 00953 00954 /* All our keys are too old, or we've never loaded the 00955 * keyring. Create a new one. 00956 */ 00957 if (!_dbus_keyring_reload (keyring, TRUE, 00958 error)) 00959 return -1; 00960 00961 key = find_recent_key (keyring); 00962 if (key) 00963 return key->id; 00964 else 00965 { 00966 dbus_set_error_const (error, 00967 DBUS_ERROR_FAILED, 00968 "No recent-enough key found in keyring, and unable to create a new key"); 00969 return -1; 00970 } 00971 } 00972 00981 dbus_bool_t 00982 _dbus_keyring_is_for_credentials (DBusKeyring *keyring, 00983 DBusCredentials *credentials) 00984 { 00985 return _dbus_credentials_same_user (keyring->credentials, 00986 credentials); 00987 } 00988 01000 dbus_bool_t 01001 _dbus_keyring_get_hex_key (DBusKeyring *keyring, 01002 int key_id, 01003 DBusString *hex_key) 01004 { 01005 DBusKey *key; 01006 01007 key = find_key_by_id (keyring->keys, 01008 keyring->n_keys, 01009 key_id); 01010 if (key == NULL) 01011 return TRUE; /* had enough memory, so TRUE */ 01012 01013 return _dbus_string_hex_encode (&key->secret, 0, 01014 hex_key, 01015 _dbus_string_get_length (hex_key)); 01016 } 01017 /* end of exposed API */ 01019 01020 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 01021 #include "dbus-test.h" 01022 #include <stdio.h> 01023 01024 dbus_bool_t 01025 _dbus_keyring_test (void) 01026 { 01027 DBusString context; 01028 DBusKeyring *ring1; 01029 DBusKeyring *ring2; 01030 int id; 01031 DBusError error; 01032 int i; 01033 01034 ring1 = NULL; 01035 ring2 = NULL; 01036 01037 /* Context validation */ 01038 01039 _dbus_string_init_const (&context, "foo"); 01040 _dbus_assert (_dbus_keyring_validate_context (&context)); 01041 _dbus_string_init_const (&context, "org_freedesktop_blah"); 01042 _dbus_assert (_dbus_keyring_validate_context (&context)); 01043 01044 _dbus_string_init_const (&context, ""); 01045 _dbus_assert (!_dbus_keyring_validate_context (&context)); 01046 _dbus_string_init_const (&context, ".foo"); 01047 _dbus_assert (!_dbus_keyring_validate_context (&context)); 01048 _dbus_string_init_const (&context, "bar.foo"); 01049 _dbus_assert (!_dbus_keyring_validate_context (&context)); 01050 _dbus_string_init_const (&context, "bar/foo"); 01051 _dbus_assert (!_dbus_keyring_validate_context (&context)); 01052 _dbus_string_init_const (&context, "bar\\foo"); 01053 _dbus_assert (!_dbus_keyring_validate_context (&context)); 01054 _dbus_string_init_const (&context, "foo\xfa\xf0"); 01055 _dbus_assert (!_dbus_keyring_validate_context (&context)); 01056 _dbus_string_init_const (&context, "foo\x80"); 01057 _dbus_assert (!_dbus_keyring_validate_context (&context)); 01058 _dbus_string_init_const (&context, "foo\x7f"); 01059 _dbus_assert (_dbus_keyring_validate_context (&context)); 01060 _dbus_string_init_const (&context, "foo bar"); 01061 _dbus_assert (!_dbus_keyring_validate_context (&context)); 01062 01063 if (!_dbus_string_init (&context)) 01064 _dbus_assert_not_reached ("no memory"); 01065 if (!_dbus_string_append_byte (&context, '\0')) 01066 _dbus_assert_not_reached ("no memory"); 01067 _dbus_assert (!_dbus_keyring_validate_context (&context)); 01068 _dbus_string_free (&context); 01069 01070 /* Now verify that if we create a key in keyring 1, 01071 * it is properly loaded in keyring 2 01072 */ 01073 01074 _dbus_string_init_const (&context, "org_freedesktop_dbus_testsuite"); 01075 dbus_error_init (&error); 01076 ring1 = _dbus_keyring_new_for_credentials (NULL, &context, 01077 &error); 01078 _dbus_assert (ring1 != NULL); 01079 _dbus_assert (error.name == NULL); 01080 01081 id = _dbus_keyring_get_best_key (ring1, &error); 01082 if (id < 0) 01083 { 01084 fprintf (stderr, "Could not load keyring: %s\n", error.message); 01085 dbus_error_free (&error); 01086 goto failure; 01087 } 01088 01089 ring2 = _dbus_keyring_new_for_credentials (NULL, &context, &error); 01090 _dbus_assert (ring2 != NULL); 01091 _dbus_assert (error.name == NULL); 01092 01093 if (ring1->n_keys != ring2->n_keys) 01094 { 01095 fprintf (stderr, "Different number of keys in keyrings\n"); 01096 goto failure; 01097 } 01098 01099 /* We guarantee we load and save keeping keys in a fixed 01100 * order 01101 */ 01102 i = 0; 01103 while (i < ring1->n_keys) 01104 { 01105 if (ring1->keys[i].id != ring2->keys[i].id) 01106 { 01107 fprintf (stderr, "Keyring 1 has first key ID %d and keyring 2 has %d\n", 01108 ring1->keys[i].id, ring2->keys[i].id); 01109 goto failure; 01110 } 01111 01112 if (ring1->keys[i].creation_time != ring2->keys[i].creation_time) 01113 { 01114 fprintf (stderr, "Keyring 1 has first key time %ld and keyring 2 has %ld\n", 01115 ring1->keys[i].creation_time, ring2->keys[i].creation_time); 01116 goto failure; 01117 } 01118 01119 if (!_dbus_string_equal (&ring1->keys[i].secret, 01120 &ring2->keys[i].secret)) 01121 { 01122 fprintf (stderr, "Keyrings 1 and 2 have different secrets for same ID/timestamp\n"); 01123 goto failure; 01124 } 01125 01126 ++i; 01127 } 01128 01129 printf (" %d keys in test\n", ring1->n_keys); 01130 01131 /* Test ref/unref */ 01132 _dbus_keyring_ref (ring1); 01133 _dbus_keyring_ref (ring2); 01134 _dbus_keyring_unref (ring1); 01135 _dbus_keyring_unref (ring2); 01136 01137 01138 /* really unref */ 01139 _dbus_keyring_unref (ring1); 01140 _dbus_keyring_unref (ring2); 01141 01142 return TRUE; 01143 01144 failure: 01145 if (ring1) 01146 _dbus_keyring_unref (ring1); 01147 if (ring2) 01148 _dbus_keyring_unref (ring2); 01149 01150 return FALSE; 01151 } 01152 01153 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */ 01154
1.7.6.1