/* * Copyright 2011 Stefan Lankes * Chair for Operating Systems, RWTH Aachen University * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #include #include #include #include #include #include #include #include #include #include #include #define PORT 4711 int main(int argc, char* argv[]) { int sockd, sockd2; unsigned int addrlen; struct sockaddr_in my_name, peer_name; int status; pid_t pid; /* create a socket */ sockd = socket(AF_INET, SOCK_STREAM, 0); if (sockd == -1) { perror("Socket creation error"); exit(1); } memset(&my_name, 0x00, sizeof(my_name)); /* server address */ my_name.sin_family = AF_INET; my_name.sin_addr.s_addr = INADDR_ANY; my_name.sin_port = htons(PORT); status = bind(sockd, (struct sockaddr*)&my_name, sizeof(my_name)); if (status == -1) { perror("Binding error"); exit(1); } status = listen(sockd, 5); if (status == -1) { perror("Listening error"); exit(1); } while(1) { /* wait for a connection */ addrlen = sizeof(peer_name); sockd2 = accept(sockd, (struct sockaddr*)&peer_name, &addrlen); if (sockd2 == -1) { perror("Wrong connection"); exit(1); } pid = fork(); if (pid == 0) { // child char* newargs[] = {"/bin/mshell", NULL}; char* newenv[] = {"USER=root", "PATH=/bin:/sbin:/usr/bin", "PWD=/", "TEMP=/tmp", NULL}; // set stdout, stdin and stderr to the socket descriptor dup2(sockd2, STDIN_FILENO); dup2(sockd2, STDOUT_FILENO); dup2(sockd2, STDERR_FILENO); close(sockd2); execve("/bin/mshell", newargs, newenv); return errno; } close(sockd2); } return 0; }