Commit 25ceeeb6 authored by Frank Osterfeld's avatar Frank Osterfeld Committed by Ralf Habacker
Browse files

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.
parent 5e2a99c1
......@@ -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);
......
......@@ -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,
......
......@@ -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;
}
}
......
......@@ -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
*
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment