2024-12-25 17:07:52 +01:00
|
|
|
#define _GNU_SOURCE
|
2024-12-25 11:00:35 +01:00
|
|
|
#include "ui-socket.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
2024-12-25 17:07:52 +01:00
|
|
|
#include <stdlib.h>
|
2024-12-25 11:00:35 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2024-12-25 17:07:52 +01:00
|
|
|
#define MAX_MESSAGE_SIZE 1024
|
|
|
|
|
|
|
|
// Mutex for thread safety
|
2024-12-25 11:00:35 +01:00
|
|
|
static pthread_mutex_t socket_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2024-12-25 17:07:52 +01:00
|
|
|
static int ui_socket_fd = -1; // Global socket file descriptor
|
2024-12-25 11:00:35 +01:00
|
|
|
|
|
|
|
int init_ui_socket(const char *filename) {
|
2024-12-25 17:07:52 +01:00
|
|
|
if (!filename) {
|
|
|
|
return -1;
|
|
|
|
}
|
2024-12-25 11:00:35 +01:00
|
|
|
|
2024-12-25 17:07:52 +01:00
|
|
|
// Create the socket
|
|
|
|
ui_socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (ui_socket_fd == -1) {
|
2024-12-25 11:00:35 +01:00
|
|
|
perror("socket");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-12-25 17:07:52 +01:00
|
|
|
// Remove the socket file if it already exists
|
|
|
|
if (unlink(filename) == -1 &&
|
|
|
|
errno != ENOENT) { // ENOENT means the file does not exist, which is fine
|
2024-12-25 11:00:35 +01:00
|
|
|
perror("unlink");
|
2024-12-25 17:07:52 +01:00
|
|
|
close(ui_socket_fd);
|
|
|
|
ui_socket_fd = -1;
|
2024-12-25 11:00:35 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-12-25 17:07:52 +01:00
|
|
|
// Set up the socket address structure
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
memset(&addr, 0, sizeof(struct sockaddr_un));
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1);
|
|
|
|
|
|
|
|
// Bind the socket
|
|
|
|
if (bind(ui_socket_fd, (struct sockaddr *)&addr,
|
|
|
|
sizeof(struct sockaddr_un)) == -1) {
|
2024-12-25 11:00:35 +01:00
|
|
|
perror("bind");
|
2024-12-25 17:07:52 +01:00
|
|
|
close(ui_socket_fd);
|
|
|
|
ui_socket_fd = -1;
|
2024-12-25 11:00:35 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-12-25 17:07:52 +01:00
|
|
|
// Listen for incoming connections
|
|
|
|
if (listen(ui_socket_fd, 5) == -1) {
|
2024-12-25 11:00:35 +01:00
|
|
|
perror("listen");
|
2024-12-25 17:07:52 +01:00
|
|
|
close(ui_socket_fd);
|
|
|
|
ui_socket_fd = -1;
|
2024-12-25 11:00:35 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ask_access(const char *filename, struct process_info pi) {
|
2024-12-25 17:07:52 +01:00
|
|
|
if (!filename || ui_socket_fd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock the mutex for thread safety
|
|
|
|
pthread_mutex_lock(&socket_mutex);
|
|
|
|
|
|
|
|
int client_fd = accept(ui_socket_fd, NULL, NULL);
|
|
|
|
if (client_fd == -1) {
|
2024-12-25 11:00:35 +01:00
|
|
|
perror("accept");
|
2024-12-25 17:07:52 +01:00
|
|
|
pthread_mutex_unlock(&socket_mutex);
|
2024-12-25 11:00:35 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-12-25 17:07:52 +01:00
|
|
|
// Prepare the message to send to the GUI
|
|
|
|
char message[MAX_MESSAGE_SIZE];
|
|
|
|
int len_filename = strlen(filename);
|
|
|
|
int len_name = strlen(pi.name);
|
2024-12-25 11:00:35 +01:00
|
|
|
|
2024-12-25 17:07:52 +01:00
|
|
|
snprintf(message, sizeof(message), "r%04d%s%04d%04d%s%04d", len_filename,
|
|
|
|
filename, pi.PID, len_name, pi.name, pi.UID);
|
2024-12-25 11:00:35 +01:00
|
|
|
|
2024-12-25 17:07:52 +01:00
|
|
|
// Send the message to the GUI
|
|
|
|
if (send(client_fd, message, strlen(message), 0) == -1) {
|
2024-12-25 11:00:35 +01:00
|
|
|
perror("send");
|
|
|
|
close(client_fd);
|
2024-12-25 17:07:52 +01:00
|
|
|
pthread_mutex_unlock(&socket_mutex);
|
2024-12-25 11:00:35 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receive the response from the GUI
|
2024-12-25 17:07:52 +01:00
|
|
|
char response[2];
|
|
|
|
if (recv(client_fd, response, sizeof(response), 0) == -1) {
|
2024-12-25 11:00:35 +01:00
|
|
|
perror("recv");
|
|
|
|
close(client_fd);
|
2024-12-25 17:07:52 +01:00
|
|
|
pthread_mutex_unlock(&socket_mutex);
|
2024-12-25 11:00:35 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(client_fd);
|
2024-12-25 17:07:52 +01:00
|
|
|
pthread_mutex_unlock(&socket_mutex);
|
2024-12-25 11:00:35 +01:00
|
|
|
|
2024-12-25 17:07:52 +01:00
|
|
|
// Process the response
|
|
|
|
if (response[0] == 'a' && response[1] == 'y') {
|
2024-12-25 11:00:35 +01:00
|
|
|
return 0; // Access granted
|
2024-12-25 17:07:52 +01:00
|
|
|
} else if (response[0] == 'a' && response[1] == 'n') {
|
2024-12-25 11:00:35 +01:00
|
|
|
return 1; // Access denied
|
|
|
|
}
|
2024-12-25 17:07:52 +01:00
|
|
|
|
|
|
|
return -1; // Invalid response
|
2024-12-25 11:00:35 +01:00
|
|
|
}
|