-
Notifications
You must be signed in to change notification settings - Fork 22
Apply restrictive permissions on private key files during creation #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -517,4 +517,76 @@ inf_file_util_delete(const gchar* path, | |
| return inf_file_util_delete_file(path, error); | ||
| } | ||
|
|
||
| /** | ||
| * inf_file_util_write_private_data: | ||
| * @filename: Filename of the file to be written to. | ||
| * @data: Data that should be written to file. | ||
| * @length: Length of data in bytes. | ||
| * @error: Location to store error information, if any, or %NULL. | ||
| * | ||
| * Writes @data to the file pointed to by @filename. On Unix-like systems, | ||
| * @filename is created with 0600 permission. If the function fails, %FALSE is | ||
| * returned and @error is set. | ||
| * | ||
| * Returns: %TRUE on success or %FALSE on error. | ||
| */ | ||
| gboolean | ||
| inf_file_util_write_private_data(const gchar* filename, | ||
| const void* data, | ||
| size_t length, | ||
| GError** error) | ||
| { | ||
| #ifdef G_OS_WIN32 | ||
| return g_file_set_contents( | ||
| filename, | ||
| data, | ||
| length, | ||
| error | ||
| ); | ||
| #else | ||
| gchar *temp_file = g_strconcat(filename, ".XXXXXX", NULL); | ||
| gint fd = g_mkstemp_full( | ||
| temp_file, | ||
| O_WRONLY|O_CREAT|O_TRUNC, | ||
| 0600 | ||
| ); | ||
| if (fd == -1) | ||
| { | ||
| inf_file_util_set_error_from_errno(error, errno); | ||
| g_free(temp_file); | ||
| return FALSE; | ||
| } | ||
| size_t remaining = length; | ||
| while (remaining > 0) | ||
| { | ||
| ssize_t written = write(fd, data, remaining); | ||
| if (written == -1) | ||
| { | ||
| inf_file_util_set_error_from_errno(error, errno); | ||
| g_close(fd, NULL); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there's a |
||
| g_unlink(temp_file); | ||
| g_free(temp_file); | ||
| return FALSE; | ||
| } | ||
| remaining -= written; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| data = ((gchar*)data) + written; | ||
| } | ||
| if (g_close(fd, error) == FALSE) | ||
| { | ||
| g_unlink(temp_file); | ||
| g_free(temp_file); | ||
| return FALSE; | ||
| } | ||
| if (g_rename(temp_file, filename) == -1) | ||
| { | ||
| inf_file_util_set_error_from_errno(error, errno); | ||
| g_unlink(temp_file); | ||
| g_free(temp_file); | ||
| return FALSE; | ||
| } | ||
| g_free(temp_file); | ||
| return TRUE; | ||
| #endif | ||
| } | ||
|
|
||
| /* vim:set et sw=2 ts=2: */ | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This must be called before
g_close, because otherwise the call tog_closecan modifyerrno(for example set it to 0 ifclose()was successful).Also,
g_closeshould either be called with NULL error, or, if you want to handle errors duringclose, a localGErrorand useg_warningto print a warning (I don't think there is much value in somehow trying to return a GError fromg_closeto the caller ifwritealready failed).