*
* @param str the string to write out
* @param filename the file to save string to
+ * @param world_readable If set, ensure the file is world readable
* @param error error to be filled in on failure
* @returns #FALSE on failure
*/
dbus_bool_t
_dbus_string_save_to_file (const DBusString *str,
const DBusString *filename,
+ dbus_bool_t world_readable,
DBusError *error)
{
int fd;
tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
fd = open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
- 0600);
+ world_readable ? 0644 : 0600);
if (fd < 0)
{
dbus_set_error (error, _dbus_error_from_errno (errno),
_dbus_strerror (errno));
goto out;
}
+ if (world_readable)
+ {
+ /* Ensure the file is world readable even in the presence of
+ * possibly restrictive umasks;
+ * see http://lists.freedesktop.org/archives/dbus/2010-September/013367.html
+ */
+ if (fchmod (fd, 0644) < 0)
+ {
+ dbus_set_error (error, _dbus_error_from_errno (errno),
+ "Could not chmod %s: %s", tmp_filename_c,
+ _dbus_strerror (errno));
+ goto out;
+ }
+ }
_dbus_verbose ("tmp file fd %d opened\n", fd);
*
* @param str the string to write out
* @param filename the file to save string to
+ * @param world_readable if true, ensure file is world readable
* @param error error to be filled in on failure
* @returns #FALSE on failure
*/
dbus_bool_t
_dbus_string_save_to_file (const DBusString *str,
const DBusString *filename,
+ dbus_bool_t world_readable,
DBusError *error)
{
HANDLE hnd;
filename_c = _dbus_string_get_const_data (filename);
tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
+ /* TODO - support world-readable in an atomic fashion */
hnd = CreateFileA (tmp_filename_c, GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
_dbus_win_free_error_string (emsg);
goto out;
}
+ if (world_readable)
+ _dbus_make_file_world_readable (tmp_filename_c);
_dbus_verbose ("tmp file %s hnd %p opened\n", tmp_filename_c, hnd);
DBusError *error);
dbus_bool_t _dbus_string_save_to_file (const DBusString *str,
const DBusString *filename,
+ dbus_bool_t world_readable,
DBusError *error);
dbus_bool_t _dbus_make_file_world_readable (const DBusString *filename,
goto error;
}
- /* FIXME this is racy; we need a save_file_exclusively
- * function. But in practice this should be fine for now.
- *
- * - first be sure we can create the file and it
- * doesn't exist by creating it empty with O_EXCL
- * - then create it by creating a temporary file and
- * overwriting atomically with rename()
- */
- if (!_dbus_create_file_exclusively (filename, error))
- goto error;
-
if (!_dbus_string_append_byte (&encoded, '\n'))
{
_DBUS_SET_OOM (error);
goto error;
}
- if (!_dbus_string_save_to_file (&encoded, filename, error))
- goto error;
-
- if (!_dbus_make_file_world_readable (filename, error))
+ if (!_dbus_string_save_to_file (&encoded, filename, TRUE, error))
goto error;
_dbus_string_free (&encoded);
}
if (!_dbus_string_save_to_file (&contents, &keyring->filename,
- error))
+ FALSE, error))
goto out;
}
return FALSE;
}
- ret = _dbus_string_save_to_file (&nonce, filename, error);
+ ret = _dbus_string_save_to_file (&nonce, filename, FALSE, error);
_dbus_string_free (&nonce);
printf ("Child failed, writing %s\n", _dbus_string_get_const_data (&filename));
dbus_error_init (&error);
- if (!_dbus_string_save_to_file (data, &filename, &error))
+ if (!_dbus_string_save_to_file (data, &filename, FALSE, &error))
{
fprintf (stderr, "Failed to save failed message data: %s\n",
error.message);