door32.c 3.8 KB

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