diff options
-rw-r--r-- | main.c | 32 |
1 files changed, 20 insertions, 12 deletions
@@ -64,10 +64,6 @@ static bool file_set_capabilities(const char *filepath, const cap_value_t *caps_ return res == 0; } -static bool file_set_permissions(const char *filepath, unsigned int permissions) { - return chmod(filepath, permissions) == 0; -} - static bool create_local_kms_server_proxy_directory(const char *home) { char path[PATH_MAX]; int err; @@ -127,14 +123,26 @@ static void usage(void) { exit(1); } -static bool set_kms_server_proxy_permissions_and_capabilities(const char *kms_server_proxy_filepath) { - /* owner: read/write/execute, group: read/execute, public: read/execute */ - if(!file_set_permissions(kms_server_proxy_filepath, 0755)) { - fprintf(stderr, "Error: failed to set kms-server-proxy permissions\n"); - return false; +/* |gsr_kms_server_local_filepath| and |gsr_global_hotkeys_local_filepath| can be NULL */ +static bool set_gsr_files_set_permissions_and_capabilities(const char *kms_server_proxy_local_filepath, const char *gsr_kms_server_local_filepath, const char *gsr_global_hotkeys_local_filepath) { + const char *filepaths[3] = {kms_server_proxy_local_filepath, gsr_kms_server_local_filepath, gsr_global_hotkeys_local_filepath}; + for(int i = 0; i < 3; ++i) { + if(!filepaths[i]) + continue; + + /* owner: read/write/execute, group: read/execute, public: read/execute */ + if(chmod(filepaths[i], 0755) != 0) { + fprintf(stderr, "Error: failed to set %s permissions\n", filepaths[i]); + return false; + } + + if(chown(filepaths[i], 0, 0) != 0) { + fprintf(stderr, "Error: failed to set %s ownership\n", filepaths[i]); + return false; + } } - if(!file_set_capabilities(kms_server_proxy_filepath, (const cap_value_t[]){ CAP_SYS_ADMIN, CAP_SETFCAP, CAP_SETUID }, 3)) { + if(!file_set_capabilities(kms_server_proxy_local_filepath, (const cap_value_t[]){ CAP_SYS_ADMIN, CAP_SETFCAP, CAP_SETUID }, 3)) { fprintf(stderr, "Error: failed to set kms-server-proxy capabilities\n"); return false; } @@ -193,7 +201,7 @@ static int setup_gsr_ui(const char *user_homepath) { snprintf(gsr_global_hotkeys_local_filepath, sizeof(gsr_global_hotkeys_local_filepath), "%s/.local/share/gpu-screen-recorder/gsr-global-hotkeys", user_homepath); if(geteuid() == 0) { /* is current user root? */ - if(!set_kms_server_proxy_permissions_and_capabilities(kms_server_proxy_local_filepath)) + if(!set_gsr_files_set_permissions_and_capabilities(kms_server_proxy_local_filepath, gsr_kms_server_local_filepath, gsr_global_hotkeys_local_filepath)) return 1; return 0; @@ -240,7 +248,7 @@ static int launch_gsr_kms_server(const char *initial_socket_path, const char *ca const char *args[] = { "pkexec", gsr_kms_server_local_filepath, initial_socket_path, card_path, NULL }; return execvp(args[0], (char *const*)args); } else if(geteuid() == 0) { /* is current user root? */ - if(!set_kms_server_proxy_permissions_and_capabilities(kms_server_proxy_local_filepath)) + if(!set_gsr_files_set_permissions_and_capabilities(kms_server_proxy_local_filepath, gsr_kms_server_local_filepath, NULL)) return 1; const char *args[] = { gsr_kms_server_local_filepath, initial_socket_path, card_path, NULL }; |