DBusSocket _dbus_accept_with_noncefile (DBusSocket listen_fd, const DBusNonceFile *noncefile) { DBusSocket fd = _dbus_socket_get_invalid (); DBusString nonce; _dbus_assert (noncefile != NULL); /* Make it valid to "free" this even if _dbus_string_init() runs * out of memory: see comment in do_check_nonce() */ _dbus_string_init_const (&nonce, ""); if (!_dbus_string_init (&nonce)) goto out; //PENDING(kdab): set better errors if (_dbus_read_nonce (_dbus_noncefile_get_path(noncefile), &nonce, NULL) != TRUE) goto out; fd = _dbus_accept (listen_fd); if (!_dbus_socket_is_valid (fd)) goto out; if (do_check_nonce(fd, &nonce, NULL) != TRUE) { _dbus_verbose ("nonce check failed. Closing socket.\n"); _dbus_close_socket(fd, NULL); _dbus_socket_invalidate (&fd); goto out; } out: _dbus_string_free (&nonce); return fd; }
/** * 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 (DBusSocket fd, const DBusString *noncefile, DBusError *error) { dbus_bool_t read_result; int send_result; DBusString nonce; _DBUS_ASSERT_ERROR_IS_CLEAR (error); if (_dbus_string_get_length (noncefile) == 0) return FALSE; if (!_dbus_string_init (&nonce)) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return FALSE; } read_result = _dbus_read_nonce (noncefile, &nonce, error); if (!read_result) { _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_system_errno (), "Failed to send nonce (fd=%" DBUS_SOCKET_FORMAT "): %s", _dbus_socket_printable (fd), _dbus_strerror_from_errno ()); return FALSE; } return TRUE; }
int _dbus_accept_with_noncefile (int listen_fd, const DBusNonceFile *noncefile) { int fd; DBusString nonce; _dbus_assert (noncefile != NULL); 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; fd = _dbus_accept (listen_fd); if (_dbus_socket_is_invalid (fd)) return fd; if (do_check_nonce(fd, &nonce, NULL) != TRUE) { _dbus_verbose ("nonce check failed. Closing socket.\n"); _dbus_close_socket(fd, NULL); return -1; } return fd; }