door32.c 5.5 KB

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