Consistently include <config.h> in all C source files and never in header files.
[platform/upstream/dbus.git] / dbus / dbus-nonce.c
index 58e723c..3e5070f 100644 (file)
@@ -21,6 +21,7 @@
  *
  */
 
+#include <config.h>
 // major sections of this file are modified code from libassuan, (C) FSF
 #include "dbus-nonce.h"
 #include "dbus-internals.h"
@@ -51,7 +52,7 @@ do_check_nonce (int fd, const DBusString *nonce, DBusError *error)
         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
         _dbus_string_free (&p);
         _dbus_string_free (&buffer);
-        return;
+        return FALSE;
       }
 
   while (nleft)
@@ -92,16 +93,23 @@ do_check_nonce (int fd, const DBusString *nonce, DBusError *error)
   return result;
 }
 
-//PENDING(kdab) document
+/**
+ * reads the nonce from the nonce file and stores it in a string
+ *
+ * @param fname the file to read the nonce from
+ * @param nonce returns the nonce. Must be an initialized string, the nonce will be appended.
+ * @param error error object to report possible errors
+ * @return FALSE iff reading the nonce fails (error is set then)
+ */
 dbus_bool_t
 _dbus_read_nonce (const DBusString *fname, DBusString *nonce, DBusError* error)
 {
-  //PENDING(kdab) replace errno by DBusError
   FILE *fp;
   char buffer[17];
-  buffer[sizeof buffer - 1] = '\0';
   size_t nread;
 
+  buffer[sizeof buffer - 1] = '\0';
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   _dbus_verbose ("reading nonce from file: %s\n", _dbus_string_get_const_data (fname));
@@ -126,12 +134,6 @@ _dbus_read_nonce (const DBusString *fname, DBusString *nonce, DBusError* error)
   return TRUE;
 }
 
-static int
-accept_with_nonce (int listen_fd, const DBusString *nonce)
-{
-
-}
-
 int
 _dbus_accept_with_noncefile (int listen_fd, const DBusNonceFile *noncefile)
 {
@@ -155,33 +157,6 @@ _dbus_accept_with_noncefile (int listen_fd, const DBusNonceFile *noncefile)
   return fd;
 }
 
-dbus_bool_t
-_dbus_generate_noncefilename (DBusString *buf, DBusError *error)
-{
-  dbus_bool_t ret;
-  DBusString randomStr;
-
-  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
-  ret = _dbus_string_init (&randomStr);
-  if (!ret)
-    goto oom;
-  ret = _dbus_generate_random_ascii (&randomStr, 8);
-  if (!ret)
-    goto oom;
-  if (!_dbus_string_append (buf, _dbus_get_tmpdir())
-      || !_dbus_string_append (buf, DBUS_DIR_SEPARATOR "dbus_nonce-")
-      || !_dbus_string_append (buf, _dbus_string_get_const_data (&randomStr)) )
-    goto oom;
-
-  _dbus_string_free (&randomStr);
-  return TRUE;
-oom:
-  dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-  _dbus_string_free (&randomStr);
-  return FALSE;
-}
-
 static dbus_bool_t
 generate_and_write_nonce (const DBusString *filename, DBusError *error)
 {
@@ -206,6 +181,15 @@ generate_and_write_nonce (const DBusString *filename, DBusError *error)
   return ret;
 }
 
+/**
+ * sends the nonce over a given socket. Blocks while doing so.
+ *
+ * @param fd the file descriptor to write the nonce data to (usually a socket)
+ * @param noncefile the noncefile location to read the nonce from
+ * @param error contains error details if FALSE is returned
+ * @return TRUE iff the nonce was successfully sent. Note that this does not
+ * indicate whether the server accepted the nonce.
+ */
 dbus_bool_t
 _dbus_send_nonce(int fd, const DBusString *noncefile, DBusError *error)
 {
@@ -285,7 +269,7 @@ do_noncefile_create (DBusNonceFile *noncefile,
       }
     if (use_subdir)
       {
-        if (!_dbus_string_append (&noncefile->dir, DBUS_DIR_SEPARATOR "dbus_nonce-")
+        if (!_dbus_string_append (&noncefile->dir, "/dbus_nonce-")
             || !_dbus_string_append (&noncefile->dir, _dbus_string_get_const_data (&randomStr)) )
           {
             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
@@ -293,7 +277,7 @@ 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, DBUS_DIR_SEPARATOR "nonce"))
+            || !_dbus_string_append (&noncefile->dir, "/nonce"))
           {
             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
             goto on_error;
@@ -308,7 +292,7 @@ do_noncefile_create (DBusNonceFile *noncefile,
       {
         if (!_dbus_string_init (&noncefile->path)
             || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
-            || !_dbus_string_append (&noncefile->path, DBUS_DIR_SEPARATOR "dbus_nonce-")
+            || !_dbus_string_append (&noncefile->path, "/dbus_nonce-")
             || !_dbus_string_append (&noncefile->path, _dbus_string_get_const_data (&randomStr)))
           {
             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
@@ -337,6 +321,13 @@ do_noncefile_create (DBusNonceFile *noncefile,
 }
 
 #ifdef DBUS_WIN
+/**
+ * creates a nonce file in a user-readable location and writes a generated nonce to it
+ *
+ * @param noncefile returns the nonce file location
+ * @param error error details if creating the nonce file fails
+ * @return TRUE iff the nonce file was successfully created
+ */
 dbus_bool_t
 _dbus_noncefile_create (DBusNonceFile *noncefile,
                         DBusError *error)
@@ -344,6 +335,13 @@ _dbus_noncefile_create (DBusNonceFile *noncefile,
     return do_noncefile_create (noncefile, error, /*use_subdir=*/FALSE);
 }
 
+/**
+ * deletes the noncefile and frees the DBusNonceFile object.
+ *
+ * @param noncefile the nonce file to delete. Contents will be freed.
+ * @param error error details if the nonce file could not be deleted
+ * @return TRUE
+ */
 dbus_bool_t
 _dbus_noncefile_delete (DBusNonceFile *noncefile,
                         DBusError *error)
@@ -353,9 +351,18 @@ _dbus_noncefile_delete (DBusNonceFile *noncefile,
     _dbus_delete_file (&noncefile->path, error);
     _dbus_string_free (&noncefile->dir);
     _dbus_string_free (&noncefile->path);
+    return TRUE;
 }
 
 #else
+/**
+ * creates a nonce file in a user-readable location and writes a generated nonce to it.
+ * Initializes the noncefile object.
+ *
+ * @param noncefile returns the nonce file location
+ * @param error error details if creating the nonce file fails
+ * @return TRUE iff the nonce file was successfully created
+ */
 dbus_bool_t
 _dbus_noncefile_create (DBusNonceFile *noncefile,
                         DBusError *error)
@@ -363,6 +370,13 @@ _dbus_noncefile_create (DBusNonceFile *noncefile,
     return do_noncefile_create (noncefile, error, /*use_subdir=*/TRUE);
 }
 
+/**
+ * deletes the noncefile and frees the DBusNonceFile object.
+ *
+ * @param noncefile the nonce file to delete. Contents will be freed.
+ * @param error error details if the nonce file could not be deleted
+ * @return TRUE
+ */
 dbus_bool_t
 _dbus_noncefile_delete (DBusNonceFile *noncefile,
                         DBusError *error)
@@ -372,10 +386,17 @@ _dbus_noncefile_delete (DBusNonceFile *noncefile,
     _dbus_delete_directory (&noncefile->dir, error);
     _dbus_string_free (&noncefile->dir);
     _dbus_string_free (&noncefile->path);
+    return TRUE;
 }
 #endif
 
 
+/**
+ * returns the absolute file path of the nonce file
+ *
+ * @param noncefile an initialized noncefile object
+ * @return the absolute path of the nonce file
+ */
 const DBusString*
 _dbus_noncefile_get_path (const DBusNonceFile *noncefile)
 {
@@ -383,6 +404,16 @@ _dbus_noncefile_get_path (const DBusNonceFile *noncefile)
     return &noncefile->path;
 }
 
+/**
+ * reads data from a file descriptor and checks if the received data matches
+ * the data in the given noncefile.
+ *
+ * @param fd the file descriptor to read the nonce from
+ * @param noncefile the nonce file to check the received data against
+ * @param error error details on fail
+ * @return TRUE iff a nonce could be successfully read from the file descriptor
+ * and matches the nonce from the given nonce file
+ */
 dbus_bool_t
 _dbus_noncefile_check_nonce (int fd,
                              const DBusNonceFile *noncefile,