]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
Cleanup of nonce code
authorFrank Osterfeld <frank@kdab.net>
Wed, 21 Oct 2009 17:04:01 +0000 (20:04 +0300)
committerRalf Habacker <ralf.habacker@freenet.de>
Tue, 1 Dec 2009 07:43:37 +0000 (08:43 +0100)
Remove the write_file function and use the existing
_dbus_string_save_to_file, improve error handling

Cherry picked from commit 0f7b026d01be7e0fd444cdb56e5f9b7a5137a062 in
the dbus4win repository. Edited to apply and fix whitespace issues by
tml@iki.fi.

dbus/dbus-nonce.c
dbus/dbus-nonce.h
dbus/dbus-server-socket.c
dbus/dbus-sysdeps-unix.c
dbus/dbus-sysdeps.h

index e8de0593653f42456542cf4e45e92df10589b2d6..bca60e089186c655c151045cd0ed1368256ed8cf 100644 (file)
@@ -149,13 +149,16 @@ _dbus_accept_with_noncefile (int listen_fd, const DBusString *noncefile)
 }
 
 dbus_bool_t
-_dbus_generate_noncefilename (DBusString *buf)
+_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)
-    return FALSE;
+    goto oom;
   ret = _dbus_generate_random_ascii (&randomStr, 8);
   if (!ret)
     goto oom;
@@ -167,22 +170,29 @@ _dbus_generate_noncefilename (DBusString *buf)
   _dbus_string_free (&randomStr);
   return TRUE;
 oom:
+  dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
   _dbus_string_free (&randomStr);
   return FALSE;
 }
 
-int
-_dbus_generate_and_write_nonce (const DBusString *filename)
+dbus_bool_t
+_dbus_generate_and_write_nonce (const DBusString *filename, DBusError *error)
 {
   DBusString nonce;
-  int ret;
+  dbus_bool_t ret;
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   _dbus_string_init (&nonce);
 
   if (!_dbus_generate_random_bytes (&nonce, 16))
-    return -1;
+    {
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      _dbus_string_free (&nonce);
+      return FALSE;
+    }
 
-  ret = _dbus_write_to_file (_dbus_string_get_const_data (filename), _dbus_string_get_const_data (&nonce), 16);
+  ret = _dbus_string_save_to_file (filename, &nonce, error);
 
   _dbus_string_free (&nonce);
 
index 441a59edf87f9932d66b9e21eeacf1da52f4e37e..85cfd7f9865917476842b92e1dc693fa9f628c4d 100644 (file)
@@ -42,9 +42,11 @@ int _dbus_accept_with_nonce (int listen_fd,
 int _dbus_accept_with_noncefile (int listen_fd,
                                  const DBusString *noncefile);
 
-dbus_bool_t _dbus_generate_noncefilename (DBusString *buf);
+dbus_bool_t _dbus_generate_noncefilename (DBusString *buf,
+                                          DBusError *error);
 
-int _dbus_generate_and_write_nonce (const DBusString *filename);
+dbus_bool_t _dbus_generate_and_write_nonce (const DBusString *filename,
+                                            DBusError *error);
 
 dbus_bool_t _dbus_send_nonce (int fd,
                               const DBusString *noncefile,
index 2c9d81c8ae01884099200779735aa68750dc4f51..a142e33c1a3f4f556cae2cb4ac3624b515a8cd50 100644 (file)
@@ -470,9 +470,8 @@ _dbus_server_new_for_tcp_socket (const char     *host,
           goto failed_2;
         }
 
-      if (_dbus_generate_and_write_nonce (&noncefile) != 0)
+      if (!_dbus_generate_and_write_nonce (&noncefile, error))
         {
-          dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
           goto failed_2;
         }
     }
index fc5170921e000fe777ad1c12b09920e3b27380fd..26c3aab426eb2073c544724e753f877ab7047ac9 100644 (file)
@@ -3908,35 +3908,6 @@ _dbus_get_is_errno_eagain_or_ewouldblock (void)
   return errno == EAGAIN || errno == EWOULDBLOCK;
 }
 
-int
-_dbus_write_to_file (const char* filename, const char* buf, size_t len)
-{
-  int filefd;
-  FILE *fp;
-  size_t written;
-
-  filefd = open (filename,
-                 (O_WRONLY|O_CREAT|O_EXCL|O_BINARY),
-                 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP));
-  if (filefd == -1)
-    {
-      return -1;
-    }
-  fp = fdopen (filefd, "wb");
-  if (!fp)
-    {
-      int save_e = errno;
-      close (filefd);
-      errno = save_e;
-      return -1;
-    }
-
-  written = fwrite (buf, len, 1, fp);
-  fclose (fp);
-
-  return written == 1 ? 0 : -1;
-}
-
 /**
  *  Checks whether file descriptors may be passed via the socket
  *
index 999aade4139011f284a4419fa1ee740540acee88..1eebfdef75219527ff019f4974e958e8881a68e2 100644 (file)
@@ -542,24 +542,6 @@ dbus_pid_t    _dbus_getpid (void);
 
 void _dbus_flush_caches (void);
 
-dbus_bool_t _dbus_generate_noncefilename (DBusString *buf);
-
-int _dbus_generate_and_write_nonce (const DBusString *filename);
-
-int _dbus_generate_nonce (char *buffer, size_t nbytes);
-
-dbus_bool_t _dbus_check_nonce (int fd, const DBusString *nonce);
-
-dbus_bool_t dbus_read_nonce (const DBusString *noncefile, DBusString *nonce);
-
-int _dbus_accept_with_nonce (int listen_fd, const DBusString *nonce);
-
-int _dbus_accept_with_noncefile (int listen_fd, const DBusString *noncefile);
-
-dbus_bool_t _dbus_send_nonce (int fd, const DBusString *noncefile, DBusError* error);
-
-int _dbus_write_to_file (const char *filename, const char *buf, size_t len);
-
 /** @} */
 
 DBUS_END_DECLS