[rename] renamed kdbus related macros
[platform/upstream/dbus.git] / dbus / dbus-keyring.c
index 17c6b17..f0c64de 100644 (file)
@@ -1,4 +1,4 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* dbus-keyring.c Store secret cookies in your homedir
  *
  * Copyright (C) 2003, 2004  Red Hat Inc.
  * 
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 
+#include <config.h>
 #include "dbus-keyring.h"
 #include "dbus-protocol.h"
 #include <dbus/dbus-string.h>
@@ -79,7 +80,7 @@
  * Maximum number of keys in the keyring before
  * we just ignore the rest
  */
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 #define MAX_KEYS_IN_FILE 10
 #else
 #define MAX_KEYS_IN_FILE 256
@@ -110,12 +111,12 @@ typedef struct
 struct DBusKeyring
 {
   int refcount;             /**< Reference count */
-  DBusString username;      /**< Username keyring is for */
   DBusString directory;     /**< Directory the below two items are inside */
   DBusString filename;      /**< Keyring filename */
   DBusString filename_lock; /**< Name of lockfile */
   DBusKey *keys; /**< Keys loaded from the file */
   int n_keys;    /**< Number of keys */
+  DBusCredentials *credentials; /**< Credentials containing user the keyring is for */
 };
 
 static DBusKeyring*
@@ -135,9 +136,6 @@ _dbus_keyring_new (void)
 
   if (!_dbus_string_init (&keyring->filename_lock))
     goto out_3;
-
-  if (!_dbus_string_init (&keyring->username))
-    goto out_4;
   
   keyring->refcount = 1;
   keyring->keys = NULL;
@@ -145,8 +143,6 @@ _dbus_keyring_new (void)
 
   return keyring;
 
- out_4:
-  _dbus_string_free (&keyring->filename_lock);
  out_3:
   _dbus_string_free (&keyring->filename);
  out_2:
@@ -204,9 +200,8 @@ _dbus_keyring_lock (DBusKeyring *keyring)
   n_timeouts = 0;
   while (n_timeouts < MAX_LOCK_TIMEOUTS)
     {
-      DBusError error;
+      DBusError error = DBUS_ERROR_INIT;
 
-      dbus_error_init (&error);
       if (_dbus_create_file_exclusively (&keyring->filename_lock,
                                          &error))
         break;
@@ -222,13 +217,11 @@ _dbus_keyring_lock (DBusKeyring *keyring)
 
   if (n_timeouts == MAX_LOCK_TIMEOUTS)
     {
-      DBusError error;
-      
+      DBusError error = DBUS_ERROR_INIT;
+
       _dbus_verbose ("Lock file timed out %d times, assuming stale\n",
                      n_timeouts);
 
-      dbus_error_init (&error);
-
       if (!_dbus_delete_file (&keyring->filename_lock, &error))
         {
           _dbus_verbose ("Couldn't delete old lock file: %s\n",
@@ -253,8 +246,8 @@ _dbus_keyring_lock (DBusKeyring *keyring)
 static void
 _dbus_keyring_unlock (DBusKeyring *keyring)
 {
-  DBusError error;
-  dbus_error_init (&error);
+  DBusError error = DBUS_ERROR_INIT;
+
   if (!_dbus_delete_file (&keyring->filename_lock, &error))
     {
       _dbus_warn ("Failed to delete lock file: %s\n",
@@ -360,7 +353,7 @@ add_new_key (DBusKey  **keys_p,
       goto out;
     }
 
-  _dbus_get_current_time (&timestamp, NULL);
+  _dbus_get_real_time (&timestamp, NULL);
       
   keys[n_keys-1].id = id;
   keys[n_keys-1].creation_time = timestamp;
@@ -435,7 +428,7 @@ _dbus_keyring_reload (DBusKeyring *keyring,
   retval = FALSE;
   have_lock = FALSE;
 
-  _dbus_get_current_time (&now, NULL);
+  _dbus_get_real_time (&now, NULL);
   
   if (add_new)
     {
@@ -610,7 +603,7 @@ _dbus_keyring_reload (DBusKeyring *keyring,
         }
       
       if (!_dbus_string_save_to_file (&contents, &keyring->filename,
-                                      error))
+                                      FALSE, error))
         goto out;
     }
 
@@ -691,7 +684,9 @@ _dbus_keyring_unref (DBusKeyring *keyring)
 
   if (keyring->refcount == 0)
     {
-      _dbus_string_free (&keyring->username);
+      if (keyring->credentials)
+        _dbus_credentials_unref (keyring->credentials);
+
       _dbus_string_free (&keyring->filename);
       _dbus_string_free (&keyring->filename_lock);
       _dbus_string_free (&keyring->directory);
@@ -701,89 +696,75 @@ _dbus_keyring_unref (DBusKeyring *keyring)
 }
 
 /**
- * Creates a new keyring that lives in the ~/.dbus-keyrings
- * directory of the given user. If the username is #NULL,
- * uses the user owning the current process.
+ * Creates a new keyring that lives in the ~/.dbus-keyrings directory
+ * of the user represented by @p credentials. If the @p credentials are
+ * #NULL or empty, uses those of the current process.
  *
- * @param username username to get keyring for, or #NULL
+ * @param credentials a set of credentials representing a user or #NULL
  * @param context which keyring to get
  * @param error return location for errors
  * @returns the keyring or #NULL on error
  */
 DBusKeyring*
-_dbus_keyring_new_homedir (const DBusString *username,
-                           const DBusString *context,
-                           DBusError        *error)
+_dbus_keyring_new_for_credentials (DBusCredentials  *credentials,
+                                   const DBusString *context,
+                                   DBusError        *error)
 {
-  DBusString homedir;
+  DBusString ringdir;
   DBusKeyring *keyring;
   dbus_bool_t error_set;
-  DBusString dotdir;
   DBusError tmp_error;
-
+  DBusCredentials *our_credentials;
+  
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+
+  if (_dbus_check_setuid ())
+    {
+      dbus_set_error_const (error, DBUS_ERROR_NOT_SUPPORTED,
+                            "Unable to create DBus keyring when setuid");
+      return NULL;
+    }
   
   keyring = NULL;
   error_set = FALSE;
+  our_credentials = NULL;
   
-  if (!_dbus_string_init (&homedir))
+  if (!_dbus_string_init (&ringdir))
     {
       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
       return NULL;
     }
 
-  _dbus_string_init_const (&dotdir, ".dbus-keyrings");
-  
-  if (username == NULL)
+  if (credentials != NULL)
     {
-      const DBusString *const_homedir;
-
-      if (!_dbus_username_from_current_process (&username) ||
-          !_dbus_homedir_from_current_process (&const_homedir))
-        goto failed;
-
-      if (!_dbus_string_copy (const_homedir, 0,
-                              &homedir, 0))
-        goto failed;
+      our_credentials = _dbus_credentials_copy (credentials);
     }
   else
     {
-      if (!_dbus_homedir_from_username (username, &homedir))
-        goto failed;
+      our_credentials = _dbus_credentials_new_from_current_process ();
     }
+  
+  if (our_credentials == NULL)
+    goto failed;
 
-#ifdef DBUS_BUILD_TESTS
- {
-   const char *override;
-
-   override = _dbus_getenv ("DBUS_TEST_HOMEDIR");
-   if (override != NULL && *override != '\0')
-     {
-       _dbus_string_set_length (&homedir, 0);
-       if (!_dbus_string_append (&homedir, override))
-         goto failed;
-
-       _dbus_verbose ("Using fake homedir for testing: %s\n",
-                      _dbus_string_get_const_data (&homedir));
-     }
-   else
-     {
-       static dbus_bool_t already_warned = FALSE;
-       if (!already_warned)
-         {
-           _dbus_warn ("Using your real home directory for testing, set DBUS_TEST_HOMEDIR to avoid\n");
-           already_warned = TRUE;
-         }
-     }
- }
-#endif
+  if (_dbus_credentials_are_anonymous (our_credentials))
+    {
+      if (!_dbus_credentials_add_from_current_process (our_credentials))
+        goto failed;
+    }
   
-  _dbus_assert (username != NULL);    
+  if (!_dbus_append_keyring_directory_for_credentials (&ringdir,
+                                                       our_credentials))
+    goto failed;
   
   keyring = _dbus_keyring_new ();
   if (keyring == NULL)
     goto failed;
 
+  _dbus_assert (keyring->credentials == NULL);
+  keyring->credentials = our_credentials;
+  our_credentials = NULL; /* so we don't unref it again later */
+  
   /* should have been validated already, but paranoia check here */
   if (!_dbus_keyring_validate_context (context))
     {
@@ -794,18 +775,12 @@ _dbus_keyring_new_homedir (const DBusString *username,
       goto failed;
     }
 
-  if (!_dbus_string_copy (username, 0,
-                          &keyring->username, 0))
-    goto failed;
-  
-  if (!_dbus_string_copy (&homedir, 0,
+  /* Save keyring dir in the keyring object */
+  if (!_dbus_string_copy (&ringdir, 0,
                           &keyring->directory, 0))
-    goto failed;
-  
-  if (!_dbus_concat_dir_and_file (&keyring->directory,
-                                  &dotdir))
-    goto failed;
+    goto failed;  
 
+  /* Create keyring->filename based on keyring dir and context */
   if (!_dbus_string_copy (&keyring->directory, 0,
                           &keyring->filename, 0))
     goto failed;
@@ -814,6 +789,7 @@ _dbus_keyring_new_homedir (const DBusString *username,
                                   context))
     goto failed;
 
+  /* Create lockfile name */
   if (!_dbus_string_copy (&keyring->filename, 0,
                           &keyring->filename_lock, 0))
     goto failed;
@@ -821,6 +797,7 @@ _dbus_keyring_new_homedir (const DBusString *username,
   if (!_dbus_string_append (&keyring->filename_lock, ".lock"))
     goto failed;
 
+  /* Reload keyring */
   dbus_error_init (&tmp_error);
   if (!_dbus_keyring_reload (keyring, FALSE, &tmp_error))
     {
@@ -842,7 +819,7 @@ _dbus_keyring_new_homedir (const DBusString *username,
       dbus_error_free (&tmp_error);
     }
 
-  _dbus_string_free (&homedir);
+  _dbus_string_free (&ringdir);
   
   return keyring;
   
@@ -851,9 +828,11 @@ _dbus_keyring_new_homedir (const DBusString *username,
     dbus_set_error_const (error,
                           DBUS_ERROR_NO_MEMORY,
                           NULL);
+  if (our_credentials)
+    _dbus_credentials_unref (our_credentials);
   if (keyring)
     _dbus_keyring_unref (keyring);
-  _dbus_string_free (&homedir);
+  _dbus_string_free (&ringdir);
   return NULL;
 
 }
@@ -936,7 +915,7 @@ find_recent_key (DBusKeyring *keyring)
   int i;
   long tv_sec, tv_usec;
 
-  _dbus_get_current_time (&tv_sec, &tv_usec);
+  _dbus_get_real_time (&tv_sec, &tv_usec);
   
   i = 0;
   while (i < keyring->n_keys)
@@ -998,19 +977,19 @@ _dbus_keyring_get_best_key (DBusKeyring  *keyring,
 }
 
 /**
- * Checks whether the keyring is for the given username.
+ * Checks whether the keyring is for the same user as the given credentials.
  *
  * @param keyring the keyring
- * @param username the username to check
+ * @param credentials the credentials to check
  *
  * @returns #TRUE if the keyring belongs to the given user
  */
 dbus_bool_t
-_dbus_keyring_is_for_user (DBusKeyring       *keyring,
-                           const DBusString  *username)
+_dbus_keyring_is_for_credentials (DBusKeyring           *keyring,
+                                  DBusCredentials       *credentials)
 {
-  return _dbus_string_equal (&keyring->username,
-                             username);
+  return _dbus_credentials_same_user (keyring->credentials,
+                                      credentials);
 }
 
 /**
@@ -1044,7 +1023,7 @@ _dbus_keyring_get_hex_key (DBusKeyring       *keyring,
 
 /** @} */ /* end of exposed API */
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 #include "dbus-test.h"
 #include <stdio.h>
 
@@ -1100,9 +1079,9 @@ _dbus_keyring_test (void)
 
   _dbus_string_init_const (&context, "org_freedesktop_dbus_testsuite");
   dbus_error_init (&error);
-  ring1 = _dbus_keyring_new_homedir (NULL, &context,
-                                     &error);
-  _dbus_assert (ring1);
+  ring1 = _dbus_keyring_new_for_credentials (NULL, &context,
+                                             &error);
+  _dbus_assert (ring1 != NULL);
   _dbus_assert (error.name == NULL);
 
   id = _dbus_keyring_get_best_key (ring1, &error);
@@ -1113,8 +1092,8 @@ _dbus_keyring_test (void)
       goto failure;
     }
 
-  ring2 = _dbus_keyring_new_homedir (NULL, &context, &error);
-  _dbus_assert (ring2);
+  ring2 = _dbus_keyring_new_for_credentials (NULL, &context, &error);
+  _dbus_assert (ring2 != NULL);
   _dbus_assert (error.name == NULL);
   
   if (ring1->n_keys != ring2->n_keys)
@@ -1177,5 +1156,5 @@ _dbus_keyring_test (void)
   return FALSE;
 }
 
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */