Updated ui-socket to use the new dialogue

This commit is contained in:
fedir 2025-05-01 20:55:05 +02:00
parent fe84daecfe
commit 8cb7721e39
Signed by: fedir
GPG Key ID: C959EE85F0C9362C

View File

@ -11,6 +11,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <time.h> #include <time.h>
#define _GNU_SOURCE #define _GNU_SOURCE
#include "cc.h"
#include "perm_permissions_table.h" #include "perm_permissions_table.h"
#include "real_filename.h" #include "real_filename.h"
#include "temp_permissions_table.h" #include "temp_permissions_table.h"
@ -23,7 +24,9 @@
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#define ZENITY_TEMP_ALLOW_MESSAGE "Allow this time\n" #define ZENITY_YES 0
#define ZENITY_NO 1
#define ZENITY_PERM 2
int init_ui_socket(const char *perm_permissions_db_filename) { int init_ui_socket(const char *perm_permissions_db_filename) {
FILE *fp = NULL; FILE *fp = NULL;
@ -66,12 +69,8 @@ void destroy_ui_socket(void) {
access_t ask_access(const char *filename, struct process_info proc_info) { access_t ask_access(const char *filename, struct process_info proc_info) {
FILE *fp = NULL; FILE *fp = NULL;
char *command = NULL; char *command = NULL;
int ret = int ret = asprintf(&command, "zenity \"%d\" \"%s\" \"%s\" \"%s\"",
asprintf(&command, proc_info.PID, proc_info.name, filename, get_mountpoint());
"zenity --question --extra-button \"Allow this time\" --title "
"\"Allow Access?\" --text \"Allow process "
"<tt>%s</tt> with PID <tt>%d</tt> to access <tt>%s</tt>\"",
proc_info.name, proc_info.PID, filename, get_mountpoint());
if (ret < 0) { if (ret < 0) {
// If asprintf fails, the contents of command are undefined (see man // If asprintf fails, the contents of command are undefined (see man
@ -92,24 +91,30 @@ access_t ask_access(const char *filename, struct process_info proc_info) {
return DENY; return DENY;
} }
// if the user clicks the "Allow this time" button, `zenity` will only str(char) zenity_output;
// write it to `stdout`, but the exit code will still be `1`. So, we need init(&zenity_output);
// to manually check the output.
char buffer[sizeof(ZENITY_TEMP_ALLOW_MESSAGE) + 1]; size_t total_read = 0;
while (fgets(buffer, sizeof(buffer), fp)) { char line[1024]; // Buffer to read individual lines
printf("%s", buffer);
if (strcmp(buffer, ZENITY_TEMP_ALLOW_MESSAGE) == 0) { // Read the command output line by line
pclose(fp); while (fgets(line, sizeof(line), fp)) {
return ALLOW_TEMP; size_t line_len = strlen(line);
} push_fmt(&zenity_output, line);
} }
int zenity_exit_code = WEXITSTATUS(pclose(fp)); int zenity_exit_code = WEXITSTATUS(pclose(fp));
fprintf(stderr, "zenity wrote out %s\n", first(&zenity_output));
fprintf(stderr, "zenity returned %d\n", zenity_exit_code); fprintf(stderr, "zenity returned %d\n", zenity_exit_code);
// zenity returns 1 on "No" >:(
if (zenity_exit_code == 0) { cleanup(&zenity_output);
if (zenity_exit_code == (ZENITY_YES | ZENITY_PERM)) {
return ALLOW; return ALLOW;
} }
if (zenity_exit_code == ZENITY_YES) {
return ALLOW_TEMP;
}
return DENY; return DENY;
} }