door32.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. write(sock, "\xff\xfb\x01", 3);
  135. // drain(sock);
  136. write(sock, "\xff\xfb\x03", 3);
  137. // drain(sock);
  138. write(sock, "\xff\xfd\x10", 3);
  139. // write(sock, "\xff\xfc\x22", 3);
  140. // syncterm is slow. It needs time to drain. :()
  141. drain(sock);
  142. // drain(sock);
  143. /*
  144. struct termios tio_raw;
  145. tcgetattr(sock, &tio_raw);
  146. cfmakeraw(&tio_raw);
  147. tcsetattr(sock, TCSANOW, &tio_raw);
  148. */
  149. // telnet client into character mode
  150. // sprintf(buffer, "\377\375\042\377\373\001Welcome socket %d\n\r", sock);
  151. sprintf(buffer, "Welcome socket %d" CRNL, sock);
  152. write(sock, buffer, strlen(buffer));
  153. // can I read the buffer until empty, maybe?
  154. FILE *fp;
  155. fp = fopen("door32.sys", "w");
  156. if (fp == NULL) {
  157. return;
  158. }
  159. fprintf(fp, "%d\n", 2);
  160. fprintf(fp, "%d\n", sock);
  161. fprintf(fp, "38400\n");
  162. fprintf(fp, "Fake Door32 BBS\n");
  163. fprintf(fp, "1\n");
  164. fprintf(fp, "%s\n%s\n", "Bugz Laundry", "Bugz");
  165. fprintf(fp, "%d\n", 100);
  166. fprintf(fp, "%d\n", 120);
  167. fprintf(fp, "1\n1\n");
  168. fclose(fp);
  169. // This resets the termios ??
  170. // execl("./testdoor", "testdoor", "-d", "door32.sys", (const char *)NULL);
  171. execv(cmds[0], cmds);
  172. // step1: write out door32.sys file
  173. // exec / replace this with our door
  174. /*
  175. bzero(buffer,256);
  176. n = read(sock,buffer,255);
  177. if (n < 0) {
  178. perror("ERROR reading from socket");
  179. exit(1);
  180. }
  181. printf("Here is the message: %s\n",buffer);
  182. n = write(sock,"I got your message",18);
  183. if (n < 0) {
  184. perror("ERROR writing to socket");
  185. exit(1);
  186. }
  187. */
  188. }