#include #include #include #include #include #include #include #define PORT 1337 int main(int argc, char const *argv[]) { int server_fd, new_socket; struct sockaddr_in address; int opt = 1; int addrlen = sizeof(address); pid_t cpid; setuid(0); // Creating socket file descriptor if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { perror("socket failed"); exit(EXIT_FAILURE); } address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons( PORT ); // Bind address to the file descriptor if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) { perror("bind failed"); exit(EXIT_FAILURE); } // Listen for new connections if (listen(server_fd, 3) < 0) { perror("listen"); exit(EXIT_FAILURE); } // Accept connections if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) { perror("accept"); exit(EXIT_FAILURE); } // Only code below this line will need to be modified char* buf = (char*)calloc(1024, 1); char cmd[2000] = "apt-get install "; char welcome[] = "Welcome to Dave's Magical Emporium!\nWe'll apt-get any packages you want for your remote server!\nWhat package would you like on your server? "; // Send the welcome message send(new_socket, welcome, strlen(welcome), 0); // Concatenate the command read(new_socket, buf, 1023); strcat(cmd, buf); cpid = fork(); if (cpid < 0) exit(1); /* exit if fork() fails */ if ( cpid ) { /* In the parent process: */ close( new_socket ); /* csock is not needed in the parent after the fork */ waitpid( cpid, NULL, 0 ); /* wait for and reap child process */ } else { /* In the child process: */ dup2( new_socket, STDOUT_FILENO ); /* duplicate socket on stdout */ dup2( new_socket, STDIN_FILENO ); /* duplicate socket on stdin */ dup2( new_socket, STDERR_FILENO ); /* duplicate socket on stderr too */ close( new_socket ); /* can close the original after it's duplicated */ system(cmd); } return 0; }