diff options
author | dec05eba <dec05eba@protonmail.com> | 2024-09-17 18:22:19 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2024-09-17 18:22:19 +0200 |
commit | 12bb4a75dbc19e2650eb7a3155ee44ae1048ae79 (patch) | |
tree | 0593cf12b5b48ffe3393a8643cf42caacf1339ff /main.c | |
parent | 00bf604d7368bd49a12b5bdb21ab7b17b1f56937 (diff) |
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 13 |
1 files changed, 9 insertions, 4 deletions
@@ -80,10 +80,12 @@ static int create_local_kms_server_proxy_directory(const char *home) { return 1; } -static int create_local_kms_server_proxy_file(const char *source_path, const char *dest_path) { +static int create_local_kms_server_proxy_file_atomic(const char *source_path, const char *dest_path) { int in_fd = -1; int out_fd = -1; int res = 0; + char tmp_filepath[PATH_MAX]; + snprintf(tmp_filepath, sizeof(tmp_filepath), "%s.tmp", dest_path); in_fd = open(source_path, O_RDONLY); if(in_fd == -1) @@ -93,7 +95,7 @@ static int create_local_kms_server_proxy_file(const char *source_path, const cha if(fstat(in_fd, &st) == -1) goto done; - out_fd = open(dest_path, O_RDWR | O_CREAT | O_TRUNC, 0755); + out_fd = open(tmp_filepath, O_RDWR | O_CREAT | O_TRUNC, 0755); if(out_fd == -1) goto done; @@ -109,7 +111,10 @@ static int create_local_kms_server_proxy_file(const char *source_path, const cha close(out_fd); if(res) - res = file_set_sys_admin_capability(dest_path, 1); + res = file_set_sys_admin_capability(tmp_filepath, 1); + + if(res) + rename(tmp_filepath, dest_path); return res; } @@ -165,7 +170,7 @@ int main(int argc, char **argv) { return execvp(args[0], (char *const*)args); } else if(geteuid() == 0) { /* is current user root */ file_set_sys_admin_capability(gsr_kms_server_filepath, 0); - create_local_kms_server_proxy_file(self_path, kms_server_proxy_local_filepath); + create_local_kms_server_proxy_file_atomic(self_path, kms_server_proxy_local_filepath); const char *args[] = { gsr_kms_server_filepath, initial_socket_path, card_path, NULL }; return execv(args[0], (char *const*)args); |