diff --git a/sources/main.c b/sources/main.c index 26fc744..bcb8d41 100644 --- a/sources/main.c +++ b/sources/main.c @@ -49,6 +49,7 @@ #include /* flock(2) */ #include "sourcefs.h" +#include "ui-socket.h" const char *mountpoint = NULL; @@ -360,6 +361,16 @@ static int xmp_create(const char *path, mode_t mode, struct fuse_file_info *fi) { int fd; + struct process_info pi; + + struct fuse_context *fc = fuse_get_context(); + + pi.PID = fc->pid; + pi.UID = fc->uid; + pi.name = ""; + + printf("%d", ask_access(path, pi)); + fd = source_create(path, fi->flags, mode); if (fd == -1) return -errno; @@ -371,6 +382,16 @@ static int xmp_create(const char *path, mode_t mode, static int xmp_open(const char *path, struct fuse_file_info *fi) { int fd; + struct process_info pi; + + struct fuse_context *fc = fuse_get_context(); + + pi.PID = fc->pid; + pi.UID = fc->uid; + pi.name = ""; + + printf("%d", ask_access(path, pi)); + fd = source_open(path, fi->flags); if (fd == -1) return -errno; @@ -663,7 +684,13 @@ int main(int argc, char *argv[]) { int ret = source_init(mountpoint); if (ret != 0) { - perror("Failed to initialize filesystem."); + perror("source_init"); + exit(EXIT_FAILURE); + } + + ret = init_ui_socket("/home/fedir/.icfs-sock"); + if (ret != 0) { + perror("init_ui_socket"); exit(EXIT_FAILURE); } diff --git a/sources/ui-socket.c b/sources/ui-socket.c index 7f774e3..f5ea22a 100644 --- a/sources/ui-socket.c +++ b/sources/ui-socket.c @@ -1,104 +1,116 @@ +#define _GNU_SOURCE #include "ui-socket.h" #include #include #include +#include #include #include #include #include -static int socket_fd = -1; +#define MAX_MESSAGE_SIZE 1024 + +// Mutex for thread safety static pthread_mutex_t socket_mutex = PTHREAD_MUTEX_INITIALIZER; +static int ui_socket_fd = -1; // Global socket file descriptor int init_ui_socket(const char *filename) { - struct sockaddr_un addr; - int fd; + if (!filename) { + return -1; + } - if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + // Create the socket + ui_socket_fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (ui_socket_fd == -1) { perror("socket"); return -1; } - memset(&addr, 0, sizeof(addr)); + // Remove the socket file if it already exists + if (unlink(filename) == -1 && + errno != ENOENT) { // ENOENT means the file does not exist, which is fine + perror("unlink"); + close(ui_socket_fd); + ui_socket_fd = -1; + return -1; + } + + // 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); - if (unlink(filename) == -1 && errno != ENOENT) { - perror("unlink"); - close(fd); - return -1; - } - - if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { + // Bind the socket + if (bind(ui_socket_fd, (struct sockaddr *)&addr, + sizeof(struct sockaddr_un)) == -1) { perror("bind"); - close(fd); + close(ui_socket_fd); + ui_socket_fd = -1; return -1; } - if (listen(fd, 5) == -1) { + // Listen for incoming connections + if (listen(ui_socket_fd, 5) == -1) { perror("listen"); - close(fd); + close(ui_socket_fd); + ui_socket_fd = -1; return -1; } - socket_fd = fd; return 0; } int ask_access(const char *filename, struct process_info pi) { - int client_fd; - struct sockaddr_un client_addr; - socklen_t client_len = sizeof(client_addr); - char request[1024]; - char response[4]; - ssize_t bytes_sent, bytes_received; - - // Accept a connection from the GUI - if ((client_fd = accept(socket_fd, (struct sockaddr *)&client_addr, - &client_len)) == -1) { - perror("accept"); + if (!filename || ui_socket_fd == -1) { return -1; } - // Construct the request message - snprintf(request, sizeof(request), "r%s;%d;%s;%d\0", filename, pi.PID, - pi.name, pi.UID); - - // Lock the socket to ensure thread safety + // Lock the mutex for thread safety pthread_mutex_lock(&socket_mutex); - // Send the request message to the GUI - bytes_sent = send(client_fd, request, strlen(request), 0); - if (bytes_sent == -1) { - perror("send"); + int client_fd = accept(ui_socket_fd, NULL, NULL); + if (client_fd == -1) { + perror("accept"); pthread_mutex_unlock(&socket_mutex); + return -1; + } + + // Prepare the message to send to the GUI + char message[MAX_MESSAGE_SIZE]; + int len_filename = strlen(filename); + int len_name = strlen(pi.name); + + snprintf(message, sizeof(message), "r%04d%s%04d%04d%s%04d", len_filename, + filename, pi.PID, len_name, pi.name, pi.UID); + + // Send the message to the GUI + if (send(client_fd, message, strlen(message), 0) == -1) { + perror("send"); close(client_fd); + pthread_mutex_unlock(&socket_mutex); return -1; } // Receive the response from the GUI - bytes_received = recv(client_fd, response, sizeof(response) - 1, 0); - if (bytes_received == -1) { + char response[2]; + if (recv(client_fd, response, sizeof(response), 0) == -1) { perror("recv"); - pthread_mutex_unlock(&socket_mutex); close(client_fd); + pthread_mutex_unlock(&socket_mutex); return -1; } - response[bytes_received] = '\0'; - // Unlock the socket + close(client_fd); pthread_mutex_unlock(&socket_mutex); - // Close the client socket - close(client_fd); - - // Check the response - if (response[0] == 'a' && response[1] == 'y' && response[2] == '\0') { + // Process the response + if (response[0] == 'a' && response[1] == 'y') { return 0; // Access granted - } else if (response[0] == 'a' && response[1] == 'n' && response[2] == '\0') { + } else if (response[0] == 'a' && response[1] == 'n') { return 1; // Access denied - } else { - // fprintf(stderr, "Invalid response from GUI: %s\n", response); - return -1; // Invalid response } + + return -1; // Invalid response }