door32.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include <netdb.h>
  2. #include <netinet/in.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <termios.h>
  7. #include <unistd.h>
  8. // auto zombie reaping
  9. #include <errno.h>
  10. #include <signal.h>
  11. #include <sys/wait.h>
  12. void doprocessing(int sock);
  13. char *cmds[10];
  14. void set_raw(int fd) {
  15. struct termios tio_raw;
  16. tcgetattr(fd, &tio_raw);
  17. cfmakeraw(&tio_raw);
  18. tcsetattr(fd, TCSANOW, &tio_raw);
  19. }
  20. int drain(int fd) {
  21. struct timeval tv;
  22. fd_set set;
  23. int ret;
  24. char buffer[21];
  25. int total = 0;
  26. printf("draining... \n");
  27. do {
  28. FD_ZERO(&set);
  29. FD_SET(fd, &set);
  30. tv.tv_sec = 0;
  31. // 50,000 works for syncterm
  32. tv.tv_usec = 50000;
  33. ret = select(fd + 1, &set, NULL, NULL, &tv);
  34. if (ret == 1) {
  35. total += read(fd, buffer, 20);
  36. }
  37. } while (ret != 0);
  38. printf(" [%d]\n", total);
  39. return total;
  40. }
  41. int main(int argc, char *argv[]) {
  42. int sockfd, newsockfd, portno, clilen;
  43. char buffer[256];
  44. struct sockaddr_in serv_addr, cli_addr;
  45. int n, pid;
  46. // Automatically reap zombies.
  47. if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
  48. perror(0);
  49. exit(1);
  50. }
  51. /* First call to socket() function */
  52. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  53. if (sockfd < 0) {
  54. perror("ERROR opening socket");
  55. exit(1);
  56. }
  57. if (argc != 3) {
  58. printf("I need a listening port number, and command/door to call.\n");
  59. exit(2);
  60. }
  61. int listen_port = atoi(argv[1]);
  62. char *command = strdup(argv[2]);
  63. printf("Port %d Cmd [%s]\n", listen_port, command);
  64. char *cp;
  65. int cmd_count = 0;
  66. cp = command;
  67. cmds[cmd_count] = cp;
  68. cmd_count++;
  69. while (*cp != 0) {
  70. if (*cp == ' ') {
  71. *cp = 0;
  72. cp++;
  73. cmds[cmd_count] = cp;
  74. cmd_count++;
  75. continue;
  76. }
  77. cp++;
  78. }
  79. cmds[cmd_count] = (char *)NULL;
  80. if (listen_port == 0) {
  81. printf("I need a listening port number as a parameter.\n");
  82. exit(2);
  83. }
  84. /* Initialize socket structure */
  85. bzero((char *)&serv_addr, sizeof(serv_addr));
  86. portno = listen_port;
  87. serv_addr.sin_family = AF_INET;
  88. serv_addr.sin_addr.s_addr = INADDR_ANY;
  89. serv_addr.sin_port = htons(portno);
  90. int flag = 1;
  91. if (-1 == setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag))) {
  92. perror("setsockopt fail");
  93. }
  94. /* Now bind the host address using bind() call.*/
  95. if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
  96. perror("ERROR on binding");
  97. exit(1);
  98. }
  99. /* Now start listening for the clients, here
  100. * process will go in sleep mode and will wait
  101. * for the incoming connection
  102. */
  103. printf("Listening for connections on %d\n", listen_port);
  104. listen(sockfd, 5);
  105. clilen = sizeof(cli_addr);
  106. while (1) {
  107. newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
  108. if (newsockfd < 0) {
  109. perror("ERROR on accept");
  110. exit(1);
  111. }
  112. set_raw(newsockfd);
  113. /* Create child process */
  114. pid = fork();
  115. if (pid < 0) {
  116. perror("ERROR on fork");
  117. exit(1);
  118. }
  119. if (pid == 0) {
  120. /* This is the client process */
  121. close(sockfd);
  122. doprocessing(newsockfd);
  123. exit(0);
  124. } else {
  125. close(newsockfd);
  126. }
  127. } /* end of while */
  128. free((void *)command);
  129. }
  130. #define CRNL "\r\n"
  131. void doprocessing(int sock) {
  132. int n;
  133. char buffer[256];
  134. // raw mode
  135. // set_raw(sock);
  136. /*
  137. std::string ayt = std::string((const char *)"\x00\xff\xfd\xf6", 4);
  138. std::string ayt_resp = std::string((const char *)"\xff\xfb\x00", 3);
  139. std::string ayt2 = std::string((const char *)"\xff\xfb\x00", 3);
  140. std::string ayt2_resp = std::string((const char *)"\xff\xfd\x00", 3);
  141. */
  142. // write(sock, "\x00\xff\xfd\xf6", 4);
  143. // write(sock, "\xff\xfb\x00", 3);
  144. /*
  145. https://www.rfc-editor.org/rfc/rfc658.html
  146. https://www.rfc-editor.org/rfc/rfc652.html
  147. https://www.iana.org/assignments/telnet-options/telnet-options.xhtml
  148. https://stackoverflow.com/questions/273261/force-telnet-client-into-character-mode
  149. https://www.omnisecu.com/tcpip/telnet-modes-of-operation.php
  150. https://www.omnisecu.com/tcpip/iac-interpret-as-command-telnet.php
  151. https://www.omnisecu.com/tcpip/telnet-negotiation.php
  152. https://stackoverflow.com/questions/10413963/telnet-iac-command-answering
  153. http://ryobbs.com/doku.php/terminal_iac
  154. https://www.omnisecu.com/tcpip/telnet-commands-and-options.php
  155. */
  156. write(sock, "\xff\xfb\x01", 3);
  157. // drain(sock);
  158. write(sock, "\xff\xfb\x03", 3);
  159. // drain(sock);
  160. write(sock, "\xff\xfd\x10", 3);
  161. // write(sock, "\xff\xfc\x22", 3);
  162. // syncterm is slow. It needs time to drain. :()
  163. drain(sock);
  164. // drain(sock);
  165. /*
  166. struct termios tio_raw;
  167. tcgetattr(sock, &tio_raw);
  168. cfmakeraw(&tio_raw);
  169. tcsetattr(sock, TCSANOW, &tio_raw);
  170. */
  171. // telnet client into character mode
  172. // sprintf(buffer, "\377\375\042\377\373\001Welcome socket %d\n\r", sock);
  173. sprintf(buffer, "Welcome socket %d" CRNL, sock);
  174. write(sock, buffer, strlen(buffer));
  175. // can I read the buffer until empty, maybe?
  176. FILE *fp;
  177. fp = fopen("door32.sys", "w");
  178. if (fp == NULL) {
  179. return;
  180. }
  181. fprintf(fp, "%d\n", 2);
  182. fprintf(fp, "%d\n", sock);
  183. fprintf(fp, "38400\n");
  184. fprintf(fp, "Fake Door32 BBS\n");
  185. fprintf(fp, "1\n");
  186. fprintf(fp, "%s\n%s\n", "Bugz Laundry", "Bugz");
  187. fprintf(fp, "%d\n", 100);
  188. fprintf(fp, "%d\n", 120);
  189. fprintf(fp, "1\n1\n");
  190. fclose(fp);
  191. // This resets the termios ??
  192. // execl("./testdoor", "testdoor", "-d", "door32.sys", (const char *)NULL);
  193. execv(cmds[0], cmds);
  194. // step1: write out door32.sys file
  195. // exec / replace this with our door
  196. /*
  197. bzero(buffer,256);
  198. n = read(sock,buffer,255);
  199. if (n < 0) {
  200. perror("ERROR reading from socket");
  201. exit(1);
  202. }
  203. printf("Here is the message: %s\n",buffer);
  204. n = write(sock,"I got your message",18);
  205. if (n < 0) {
  206. perror("ERROR writing to socket");
  207. exit(1);
  208. }
  209. */
  210. }