2025-03-10 18:09:05 +01:00
|
|
|
/*
|
|
|
|
ICFS: Interactively Controlled File System
|
|
|
|
Copyright (C) 2024-2025 Fedir Kovalov
|
|
|
|
|
|
|
|
This program can be distributed under the terms of the GNU GPLv2.
|
|
|
|
See the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
2025-02-07 12:42:51 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include "ui-socket.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
int init_ui_socket(const char *filename) {
|
|
|
|
char line[256];
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
// Test if Zenity is installed (get version)
|
|
|
|
fp = popen("zenity --version", "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
perror("Pipe returned a error");
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
while (fgets(line, sizeof(line), fp))
|
|
|
|
printf("%s", line);
|
|
|
|
pclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-10 18:01:04 +01:00
|
|
|
/**
|
|
|
|
* @param filename: The file that the process is trying to access
|
|
|
|
* @pram pi: The process information
|
|
|
|
* @return: 0 if access is denied, 1 if access is allowed, 2 if access is allwed
|
|
|
|
* for the runtime of the process
|
2025-02-07 12:42:51 +01:00
|
|
|
*/
|
|
|
|
int ask_access(const char *filename, struct process_info pi) {
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
size_t command_len =
|
|
|
|
139 + sizeof(pid_t) * 8 + strlen(pi.name) + strlen(filename);
|
|
|
|
char *command = (char *)malloc(command_len);
|
|
|
|
snprintf(command, command_len,
|
2025-03-10 18:01:04 +01:00
|
|
|
"zenity --question --extra-button \"Allow this time\" --title "
|
|
|
|
"\"Allow Access?\" --text \"Allow process "
|
2025-02-07 12:42:51 +01:00
|
|
|
"<tt>%s</tt> with PID <tt>%d</tt> to access <tt>%s</tt>\"",
|
|
|
|
pi.name, pi.PID, filename);
|
2025-03-10 18:01:04 +01:00
|
|
|
|
2025-02-07 12:42:51 +01:00
|
|
|
// Zenity Question Message Popup
|
|
|
|
fp = popen(command, "r");
|
|
|
|
free(command);
|
2025-03-10 18:01:04 +01:00
|
|
|
|
2025-02-07 12:42:51 +01:00
|
|
|
if (fp == NULL) {
|
|
|
|
perror("Pipe returned a error");
|
|
|
|
return -1;
|
|
|
|
}
|
2025-03-10 18:01:04 +01:00
|
|
|
|
|
|
|
// if the user clicks the "Allow this time" button, `zenity` will only
|
|
|
|
// write it to `stdout`, but the exit code will still be `1`. So, we need
|
|
|
|
// to manually check the output.
|
|
|
|
char buffer[1024];
|
|
|
|
while (fgets(buffer, sizeof(buffer), fp)) {
|
|
|
|
if (strcmp(buffer, "Allow this time.\n") == 0) {
|
|
|
|
pclose(fp);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int zenity_exit_code = WEXITSTATUS(pclose(fp));
|
|
|
|
return zenity_exit_code;
|
2025-02-07 12:42:51 +01:00
|
|
|
}
|