From: Frank Osterfeld Date: Wed, 21 Oct 2009 17:04:01 +0000 (+0300) Subject: Cleanup of nonce code X-Git-Tag: dbus-1.3.1~174 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=25ceeeb6799849e2d1c67b421be2110aee2804ff;p=thirdparty%2Fdbus.git Cleanup of nonce code 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. --- diff --git a/dbus/dbus-nonce.c b/dbus/dbus-nonce.c index e8de05936..bca60e089 100644 --- a/dbus/dbus-nonce.c +++ b/dbus/dbus-nonce.c @@ -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); diff --git a/dbus/dbus-nonce.h b/dbus/dbus-nonce.h index 441a59edf..85cfd7f98 100644 --- a/dbus/dbus-nonce.h +++ b/dbus/dbus-nonce.h @@ -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, diff --git a/dbus/dbus-server-socket.c b/dbus/dbus-server-socket.c index 2c9d81c8a..a142e33c1 100644 --- a/dbus/dbus-server-socket.c +++ b/dbus/dbus-server-socket.c @@ -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; } } diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index fc5170921..26c3aab42 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -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 * diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index 999aade41..1eebfdef7 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -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