door32.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <netdb.h>
  4. #include <netinet/in.h>
  5. #include <termios.h>
  6. #include <unistd.h>
  7. #include <string.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 main( int argc, char *argv[] ) {
  17. int sockfd, newsockfd, portno, clilen;
  18. char buffer[256];
  19. struct sockaddr_in serv_addr, cli_addr;
  20. int n, pid;
  21. /* First call to socket() function */
  22. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  23. if (sockfd < 0) {
  24. perror("ERROR opening socket");
  25. exit(1);
  26. }
  27. if (argc != 3) {
  28. printf("I need a listening port number, and command/door to call.\n");
  29. exit(2);
  30. }
  31. int listen_port = atoi(argv[1]);
  32. char * command = strdup(argv[2]);
  33. char * cp;
  34. int cmd_count;
  35. cp = command;
  36. cmds[cmd_count] = cp;
  37. cmd_count++;
  38. while (*cp != 0) {
  39. if (*cp == ' ') {
  40. *cp = 0;
  41. cp++;
  42. cmds[cmd_count] = cp;
  43. cmd_count++;
  44. continue;
  45. }
  46. cp++;
  47. }
  48. cmds[cmd_count] = (char *)NULL;
  49. if (listen_port ==0) {
  50. printf("I need a listening port number as a parameter.\n");
  51. exit(2);
  52. }
  53. /* Initialize socket structure */
  54. bzero((char *) &serv_addr, sizeof(serv_addr));
  55. portno = listen_port;
  56. serv_addr.sin_family = AF_INET;
  57. serv_addr.sin_addr.s_addr = INADDR_ANY;
  58. serv_addr.sin_port = htons(portno);
  59. int flag = 1;
  60. if (-1 == setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag))) {
  61. perror("setsockopt fail");
  62. }
  63. /* Now bind the host address using bind() call.*/
  64. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  65. perror("ERROR on binding");
  66. exit(1);
  67. }
  68. /* Now start listening for the clients, here
  69. * process will go in sleep mode and will wait
  70. * for the incoming connection
  71. */
  72. printf("Listening for connections on %d\n", listen_port);
  73. listen(sockfd,5);
  74. clilen = sizeof(cli_addr);
  75. while (1) {
  76. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  77. if (newsockfd < 0) {
  78. perror("ERROR on accept");
  79. exit(1);
  80. }
  81. set_raw(newsockfd);
  82. /* Create child process */
  83. pid = fork();
  84. if (pid < 0) {
  85. perror("ERROR on fork");
  86. exit(1);
  87. }
  88. if (pid == 0) {
  89. /* This is the client process */
  90. close(sockfd);
  91. doprocessing(newsockfd);
  92. exit(0);
  93. }
  94. else {
  95. close(newsockfd);
  96. }
  97. } /* end of while */
  98. free((void*)command);
  99. }
  100. void doprocessing (int sock) {
  101. int n;
  102. char buffer[256];
  103. // raw mode
  104. set_raw(sock);
  105. /*
  106. struct termios tio_raw;
  107. tcgetattr(sock, &tio_raw);
  108. cfmakeraw(&tio_raw);
  109. tcsetattr(sock, TCSANOW, &tio_raw);
  110. */
  111. // telnet client into character mode
  112. // sprintf(buffer, "\377\375\042\377\373\001Welcome socket %d\n\r", sock);
  113. sprintf(buffer, "Welcome socket %d\n\r", sock);
  114. write(sock, buffer, strlen(buffer) );
  115. // can I read the buffer until empty, maybe?
  116. FILE * fp;
  117. fp = fopen("door32.sys", "w");
  118. if ( fp == NULL) {
  119. return;
  120. }
  121. fprintf(fp, "%d\n", 2);
  122. fprintf(fp, "%d\n", sock);
  123. fprintf(fp, "38400\n");
  124. fprintf(fp, "Fake Door32 BBS\n");
  125. fprintf(fp, "1\n");
  126. fprintf(fp, "%s\n%s\n", "Bugz Laundry", "Bugz");
  127. fprintf(fp, "%d\n", 100);
  128. fprintf(fp, "%d\n", 120);
  129. fprintf(fp, "1\n1\n");
  130. fclose(fp);
  131. // This resets the termios ??
  132. // execl("./testdoor", "testdoor", "-d", "door32.sys", (const char *)NULL);
  133. execv(cmds[0], cmds);
  134. // step1: write out door32.sys file
  135. // exec / replace this with our door
  136. /*
  137. bzero(buffer,256);
  138. n = read(sock,buffer,255);
  139. if (n < 0) {
  140. perror("ERROR reading from socket");
  141. exit(1);
  142. }
  143. printf("Here is the message: %s\n",buffer);
  144. n = write(sock,"I got your message",18);
  145. if (n < 0) {
  146. perror("ERROR writing to socket");
  147. exit(1);
  148. }
  149. */
  150. }