main.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Client.h"
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/IPv4Address.h>
  30. #include <AK/String.h>
  31. #include <AK/StringBuilder.h>
  32. #include <AK/Types.h>
  33. #include <LibCore/EventLoop.h>
  34. #include <LibCore/TCPServer.h>
  35. #include <LibCore/TCPSocket.h>
  36. #include <fcntl.h>
  37. #include <getopt.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <sys/ioctl.h>
  41. static void run_command(int ptm_fd, String command)
  42. {
  43. pid_t pid = fork();
  44. if (pid == 0) {
  45. const char* tty_name = ptsname(ptm_fd);
  46. if (!tty_name) {
  47. perror("ptsname");
  48. exit(1);
  49. }
  50. close(ptm_fd);
  51. int pts_fd = open(tty_name, O_RDWR);
  52. if (pts_fd < 0) {
  53. perror("open");
  54. exit(1);
  55. }
  56. // NOTE: It's okay if this fails.
  57. (void)ioctl(0, TIOCNOTTY);
  58. close(0);
  59. close(1);
  60. close(2);
  61. int rc = dup2(pts_fd, 0);
  62. if (rc < 0) {
  63. perror("dup2");
  64. exit(1);
  65. }
  66. rc = dup2(pts_fd, 1);
  67. if (rc < 0) {
  68. perror("dup2");
  69. exit(1);
  70. }
  71. rc = dup2(pts_fd, 2);
  72. if (rc < 0) {
  73. perror("dup2");
  74. exit(1);
  75. }
  76. rc = close(pts_fd);
  77. if (rc < 0) {
  78. perror("close");
  79. exit(1);
  80. }
  81. rc = ioctl(0, TIOCSCTTY);
  82. if (rc < 0) {
  83. perror("ioctl(TIOCSCTTY)");
  84. exit(1);
  85. }
  86. const char* args[4] = { "/bin/Shell", nullptr, nullptr, nullptr };
  87. if (!command.is_empty()) {
  88. args[1] = "-c";
  89. args[2] = command.characters();
  90. }
  91. const char* envs[] = { "TERM=xterm", "PATH=/bin:/usr/bin:/usr/local/bin", nullptr };
  92. rc = execve("/bin/Shell", const_cast<char**>(args), const_cast<char**>(envs));
  93. if (rc < 0) {
  94. perror("execve");
  95. exit(1);
  96. }
  97. ASSERT_NOT_REACHED();
  98. }
  99. }
  100. int main(int argc, char** argv)
  101. {
  102. Core::EventLoop event_loop;
  103. auto server = Core::TCPServer::construct();
  104. int opt;
  105. u16 port = 23;
  106. const char* command = "";
  107. while ((opt = getopt(argc, argv, "p:c:")) != -1) {
  108. switch (opt) {
  109. case 'p':
  110. port = atoi(optarg);
  111. break;
  112. case 'c':
  113. command = optarg;
  114. break;
  115. default:
  116. fprintf(stderr, "Usage: %s [-p port] [-c command]", argv[0]);
  117. exit(1);
  118. }
  119. }
  120. if (!server->listen({}, port)) {
  121. warnln("Listening on 0.0.0.0:{} failed", port);
  122. exit(1);
  123. }
  124. HashMap<int, NonnullRefPtr<Client>> clients;
  125. int next_id = 0;
  126. server->on_ready_to_accept = [&next_id, &clients, &server, command] {
  127. int id = next_id++;
  128. auto client_socket = server->accept();
  129. if (!client_socket) {
  130. perror("accept");
  131. return;
  132. }
  133. int ptm_fd = posix_openpt(O_RDWR);
  134. if (ptm_fd < 0) {
  135. perror("posix_openpt");
  136. client_socket->close();
  137. return;
  138. }
  139. if (grantpt(ptm_fd) < 0) {
  140. perror("grantpt");
  141. client_socket->close();
  142. return;
  143. }
  144. if (unlockpt(ptm_fd) < 0) {
  145. perror("unlockpt");
  146. client_socket->close();
  147. return;
  148. }
  149. run_command(ptm_fd, command);
  150. auto client = Client::create(id, move(client_socket), ptm_fd);
  151. client->on_exit = [&clients, id] { clients.remove(id); };
  152. clients.set(id, client);
  153. };
  154. int rc = event_loop.exec();
  155. if (rc != 0) {
  156. fprintf(stderr, "event loop exited badly; rc=%d", rc);
  157. exit(1);
  158. }
  159. return 0;
  160. }