Merge "Optional autogen.sh flag --enable-kdbus-transport added allowing to compile...
[platform/upstream/dbus.git] / dbus / dbus-nonce.c
index 3e5070f..ef037ef 100644 (file)
 
 #include <stdio.h>
 
-#ifdef HAVE_ERRNO_H
-# include <errno.h>
-#endif
-
 static dbus_bool_t
 do_check_nonce (int fd, const DBusString *nonce, DBusError *error)
 {
@@ -141,7 +137,8 @@ _dbus_accept_with_noncefile (int listen_fd, const DBusNonceFile *noncefile)
   DBusString nonce;
 
   _dbus_assert (noncefile != NULL);
-  _dbus_string_init (&nonce);
+  if (!_dbus_string_init (&nonce))
+    return -1;
   //PENDING(kdab): set better errors
   if (_dbus_read_nonce (_dbus_noncefile_get_path(noncefile), &nonce, NULL) != TRUE)
     return -1;
@@ -165,7 +162,11 @@ generate_and_write_nonce (const DBusString *filename, DBusError *error)
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-  _dbus_string_init (&nonce);
+  if (!_dbus_string_init (&nonce))
+    {
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      return FALSE;
+    }
 
   if (!_dbus_generate_random_bytes (&nonce, 16))
     {
@@ -174,7 +175,7 @@ generate_and_write_nonce (const DBusString *filename, DBusError *error)
       return FALSE;
     }
 
-  ret = _dbus_string_save_to_file (&nonce, filename, error);
+  ret = _dbus_string_save_to_file (&nonce, filename, FALSE, error);
 
   _dbus_string_free (&nonce);
 
@@ -191,11 +192,10 @@ generate_and_write_nonce (const DBusString *filename, DBusError *error)
  * indicate whether the server accepted the nonce.
  */
 dbus_bool_t
-_dbus_send_nonce(int fd, const DBusString *noncefile, DBusError *error)
+_dbus_send_nonce (int fd, const DBusString *noncefile, DBusError *error)
 {
   dbus_bool_t read_result;
   int send_result;
-  size_t sendLen;
   DBusString nonce;
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
@@ -203,36 +203,33 @@ _dbus_send_nonce(int fd, const DBusString *noncefile, DBusError *error)
   if (_dbus_string_get_length (noncefile) == 0)
     return FALSE;
 
-  if ( !_dbus_string_init (&nonce) )
+  if (!_dbus_string_init (&nonce))
     {
       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
       return FALSE;
-  }
-
-  read_result = _dbus_read_nonce (noncefile, &nonce, NULL);
+    }
 
+  read_result = _dbus_read_nonce (noncefile, &nonce, error);
   if (!read_result)
     {
-      dbus_set_error (error,
-                      _dbus_error_from_errno (errno),
-                      "Could not read nonce from file %s (%s)",
-                      _dbus_string_get_const_data (noncefile), _dbus_strerror(errno));
+      _DBUS_ASSERT_ERROR_IS_SET (error);
       _dbus_string_free (&nonce);
       return FALSE;
     }
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   send_result = _dbus_write_socket (fd, &nonce, 0, _dbus_string_get_length (&nonce));
 
   _dbus_string_free (&nonce);
 
   if (send_result == -1)
-  {
-    dbus_set_error (error,
-                    _dbus_error_from_errno (errno),
-                    "Failed to send nonce (fd=%d): %s",
-                    fd, _dbus_strerror(errno));
-    return FALSE;
-  }
+    {
+      dbus_set_error (error,
+                      _dbus_error_from_system_errno (),
+                      "Failed to send nonce (fd=%d): %s",
+                      fd, _dbus_strerror_from_errno ());
+      return FALSE;
+    }
 
   return TRUE;
 }
@@ -242,8 +239,8 @@ do_noncefile_create (DBusNonceFile *noncefile,
                      DBusError *error,
                      dbus_bool_t use_subdir)
 {
-    dbus_bool_t ret;
     DBusString randomStr;
+    const char *tmp;
 
     _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
@@ -261,8 +258,11 @@ do_noncefile_create (DBusNonceFile *noncefile,
         goto on_error;
       }
 
+    tmp = _dbus_get_tmpdir ();
+
     if (!_dbus_string_init (&noncefile->dir)
-        || !_dbus_string_append (&noncefile->dir, _dbus_get_tmpdir()))
+        || tmp == NULL
+        || !_dbus_string_append (&noncefile->dir, tmp))
       {
         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
         goto on_error;
@@ -277,15 +277,17 @@ do_noncefile_create (DBusNonceFile *noncefile,
           }
         if (!_dbus_string_init (&noncefile->path)
             || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
-            || !_dbus_string_append (&noncefile->dir, "/nonce"))
+            || !_dbus_string_append (&noncefile->path, "/nonce"))
           {
             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
             goto on_error;
           }
         if (!_dbus_create_directory (&noncefile->dir, error))
           {
+            _DBUS_ASSERT_ERROR_IS_SET (error);
             goto on_error;
           }
+        _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
       }
     else
@@ -303,10 +305,12 @@ do_noncefile_create (DBusNonceFile *noncefile,
 
     if (!generate_and_write_nonce (&noncefile->path, error))
       {
+        _DBUS_ASSERT_ERROR_IS_SET (error);
         if (use_subdir)
           _dbus_delete_directory (&noncefile->dir, NULL); //we ignore possible errors deleting the dir and return the write error instead
         goto on_error;
       }
+    _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
     _dbus_string_free (&randomStr);