#include "../../libs/communication/communication.h" #include "../../libs/protocol/protocol.h" #include #include #include #include #include #include #include #include int main() { // Open the FIFO for writing chroot("/opt/djumbai/"); const char *send_fifo_path = "fifos/send_fifo"; if (access(send_fifo_path, F_OK) != -1) { // FIFO exists, delete it if (unlink(send_fifo_path) == -1) { perror("unlink"); exit(EXIT_FAILURE); } printf("Existing FIFO deleted.\n"); } // Open the FIFO for reading if (mkfifo(send_fifo_path, 0420) == -1) { perror("mkfifo"); exit(EXIT_FAILURE); } int send_fifo_fd; send_fifo_fd = open(send_fifo_path, O_RDONLY); if (send_fifo_fd == -1) { if (errno == ENOENT) { // FIFO does not exist printf("FIFO '%s' does not exist. Exiting...\n", send_fifo_path); return 1; } else { perror("open"); return 1; } } while (1) { // Read message from the send_fifo unsigned char buffer[MESSAGE_SIZE]; read(send_fifo_fd, buffer, MESSAGE_SIZE); // Deserialize the message message msg; deserialize_message(buffer, MESSAGE_SIZE, &msg); int pipe_to_child[2]; if (pipe(pipe_to_child) == -1) { perror("pipe"); exit(EXIT_FAILURE); } pid_t pid = fork(); if (pid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (pid == 0) { // Child process close(pipe_to_child[1]); // Close write end of pipe if (msg.header.isgroup) { // Message receiver is a group // Get the UID of the nobody user. const char *nobody_username = "nobody"; struct passwd *pw = getpwnam(nobody_username); if (pw == NULL) { fprintf(stderr, "User %s not found\n", nobody_username); exit(EXIT_FAILURE); } // Set UID to nobody if (setuid(pw->pw_uid) == -1) { perror("setuid"); exit(EXIT_FAILURE); } // Set gid to receiver if (setgid(msg.header.receiver) == -1) { perror("setgid"); exit(EXIT_FAILURE); } } else { // Message receiver is a user // Change UID receiver if (setuid(msg.header.receiver) == -1) { perror("setuid"); exit(EXIT_FAILURE); } struct passwd *pw = getpwuid(msg.header.receiver); if (pw == NULL) { fprintf(stderr, "User with uid %d not found\n", msg.header.receiver); exit(EXIT_FAILURE); } if (setgid(pw->pw_gid) == -1) { perror("setgid"); exit(EXIT_FAILURE); } } // Redirect stdin to read from pipe_to_child dup2(pipe_to_child[0], STDIN_FILENO); execlp("./bin/djumbai_client_receive", "djumbai_client_receive", NULL); // If execlp fails perror("execlp"); close(pipe_to_child[0]); exit(EXIT_FAILURE); } else { // Parent process close(pipe_to_child[0]); // Close read end of pipe write(pipe_to_child[1], buffer, sizeof(buffer)); // Close the write end of the pipe close(pipe_to_child[1]); // Wait for the child process to finish wait(NULL); } } close(send_fifo_fd); unlink(send_fifo_path); }