123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "Client.h"
- #include <AK/ByteBuffer.h>
- #include <AK/HashMap.h>
- #include <AK/IPv4Address.h>
- #include <AK/String.h>
- #include <AK/StringBuilder.h>
- #include <AK/Types.h>
- #include <LibCore/EventLoop.h>
- #include <LibCore/TCPServer.h>
- #include <LibCore/TCPSocket.h>
- #include <fcntl.h>
- #include <getopt.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/ioctl.h>
- static void run_command(int ptm_fd, String command)
- {
- pid_t pid = fork();
- if (pid == 0) {
- const char* tty_name = ptsname(ptm_fd);
- if (!tty_name) {
- perror("ptsname");
- exit(1);
- }
- close(ptm_fd);
- int pts_fd = open(tty_name, O_RDWR);
- if (pts_fd < 0) {
- perror("open");
- exit(1);
- }
- // NOTE: It's okay if this fails.
- (void)ioctl(0, TIOCNOTTY);
- close(0);
- close(1);
- close(2);
- int rc = dup2(pts_fd, 0);
- if (rc < 0) {
- perror("dup2");
- exit(1);
- }
- rc = dup2(pts_fd, 1);
- if (rc < 0) {
- perror("dup2");
- exit(1);
- }
- rc = dup2(pts_fd, 2);
- if (rc < 0) {
- perror("dup2");
- exit(1);
- }
- rc = close(pts_fd);
- if (rc < 0) {
- perror("close");
- exit(1);
- }
- rc = ioctl(0, TIOCSCTTY);
- if (rc < 0) {
- perror("ioctl(TIOCSCTTY)");
- exit(1);
- }
- const char* args[4] = { "/bin/Shell", nullptr, nullptr, nullptr };
- if (!command.is_empty()) {
- args[1] = "-c";
- args[2] = command.characters();
- }
- const char* envs[] = { "TERM=xterm", "PATH=/bin:/usr/bin:/usr/local/bin", nullptr };
- rc = execve("/bin/Shell", const_cast<char**>(args), const_cast<char**>(envs));
- if (rc < 0) {
- perror("execve");
- exit(1);
- }
- ASSERT_NOT_REACHED();
- }
- }
- int main(int argc, char** argv)
- {
- Core::EventLoop event_loop;
- auto server = Core::TCPServer::construct();
- int opt;
- u16 port = 23;
- const char* command = "";
- while ((opt = getopt(argc, argv, "p:c:")) != -1) {
- switch (opt) {
- case 'p':
- port = atoi(optarg);
- break;
- case 'c':
- command = optarg;
- break;
- default:
- fprintf(stderr, "Usage: %s [-p port] [-c command]", argv[0]);
- exit(1);
- }
- }
- if (!server->listen({}, port)) {
- warnln("Listening on 0.0.0.0:{} failed", port);
- exit(1);
- }
- HashMap<int, NonnullRefPtr<Client>> clients;
- int next_id = 0;
- server->on_ready_to_accept = [&next_id, &clients, &server, command] {
- int id = next_id++;
- auto client_socket = server->accept();
- if (!client_socket) {
- perror("accept");
- return;
- }
- int ptm_fd = posix_openpt(O_RDWR);
- if (ptm_fd < 0) {
- perror("posix_openpt");
- client_socket->close();
- return;
- }
- if (grantpt(ptm_fd) < 0) {
- perror("grantpt");
- client_socket->close();
- return;
- }
- if (unlockpt(ptm_fd) < 0) {
- perror("unlockpt");
- client_socket->close();
- return;
- }
- run_command(ptm_fd, command);
- auto client = Client::create(id, move(client_socket), ptm_fd);
- client->on_exit = [&clients, id] { clients.remove(id); };
- clients.set(id, client);
- };
- int rc = event_loop.exec();
- if (rc != 0) {
- fprintf(stderr, "event loop exited badly; rc=%d", rc);
- exit(1);
- }
- return 0;
- }
|