nc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* vi: set sw=4 ts=4: */
  2. /* nc: mini-netcat - built from the ground up for LRP
  3. Copyright (C) 1998 Charles P. Wright
  4. 0.0.1 6K It works.
  5. 0.0.2 5K Smaller and you can also check the exit condition if you wish.
  6. 0.0.3 Uses select()
  7. 19980918 Busy Boxed! Dave Cinege
  8. 19990512 Uses Select. Charles P. Wright
  9. 19990513 Fixes stdin stupidity and uses buffers. Charles P. Wright
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #include <netdb.h>
  31. #include <sys/time.h>
  32. #include <sys/ioctl.h>
  33. #include "busybox.h"
  34. #define GAPING_SECURITY_HOLE
  35. int nc_main(int argc, char **argv)
  36. {
  37. int do_listen = 0, lport = 0, delay = 0, tmpfd, opt, sfd, x;
  38. char buf[BUFSIZ];
  39. #ifdef GAPING_SECURITY_HOLE
  40. char * pr00gie = NULL;
  41. #endif
  42. struct sockaddr_in address;
  43. struct hostent *hostinfo;
  44. fd_set readfds, testfds;
  45. while ((opt = getopt(argc, argv, "lp:i:e:")) > 0) {
  46. switch (opt) {
  47. case 'l':
  48. do_listen++;
  49. break;
  50. case 'p':
  51. lport = bb_lookup_port(optarg, "tcp", 0);
  52. break;
  53. case 'i':
  54. delay = atoi(optarg);
  55. break;
  56. #ifdef GAPING_SECURITY_HOLE
  57. case 'e':
  58. pr00gie = optarg;
  59. break;
  60. #endif
  61. default:
  62. bb_show_usage();
  63. }
  64. }
  65. #ifdef GAPING_SECURITY_HOLE
  66. if (pr00gie) {
  67. /* won't need stdin */
  68. close(STDIN_FILENO);
  69. }
  70. #endif /* GAPING_SECURITY_HOLE */
  71. if ((do_listen && optind != argc) || (!do_listen && optind + 2 != argc))
  72. bb_show_usage();
  73. if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  74. bb_perror_msg_and_die("socket");
  75. x = 1;
  76. if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof (x)) == -1)
  77. bb_perror_msg_and_die("reuseaddr");
  78. address.sin_family = AF_INET;
  79. if (lport != 0) {
  80. memset(&address.sin_addr, 0, sizeof(address.sin_addr));
  81. address.sin_port = lport;
  82. if (bind(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
  83. bb_perror_msg_and_die("bind");
  84. }
  85. if (do_listen) {
  86. socklen_t addrlen = sizeof(address);
  87. if (listen(sfd, 1) < 0)
  88. bb_perror_msg_and_die("listen");
  89. if ((tmpfd = accept(sfd, (struct sockaddr *) &address, &addrlen)) < 0)
  90. bb_perror_msg_and_die("accept");
  91. close(sfd);
  92. sfd = tmpfd;
  93. } else {
  94. hostinfo = xgethostbyname(argv[optind]);
  95. address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
  96. address.sin_port = bb_lookup_port(argv[optind+1], "tcp", 0);
  97. if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
  98. bb_perror_msg_and_die("connect");
  99. }
  100. #ifdef GAPING_SECURITY_HOLE
  101. /* -e given? */
  102. if (pr00gie) {
  103. dup2(sfd, 0);
  104. close(sfd);
  105. dup2 (0, 1);
  106. dup2 (0, 2);
  107. execl (pr00gie, pr00gie, NULL);
  108. /* Don't print stuff or it will go over the wire.... */
  109. _exit(-1);
  110. }
  111. #endif /* GAPING_SECURITY_HOLE */
  112. FD_ZERO(&readfds);
  113. FD_SET(sfd, &readfds);
  114. FD_SET(STDIN_FILENO, &readfds);
  115. while (1) {
  116. int fd;
  117. int ofd;
  118. int nread;
  119. testfds = readfds;
  120. if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
  121. bb_perror_msg_and_die("select");
  122. for (fd = 0; fd < FD_SETSIZE; fd++) {
  123. if (FD_ISSET(fd, &testfds)) {
  124. if ((nread = safe_read(fd, buf, sizeof(buf))) < 0)
  125. bb_perror_msg_and_die(bb_msg_read_error);
  126. if (fd == sfd) {
  127. if (nread == 0)
  128. exit(0);
  129. ofd = STDOUT_FILENO;
  130. } else {
  131. if (nread <= 0) {
  132. shutdown(sfd, 1 /* send */ );
  133. close(STDIN_FILENO);
  134. FD_CLR(STDIN_FILENO, &readfds);
  135. }
  136. ofd = sfd;
  137. }
  138. if (bb_full_write(ofd, buf, nread) < 0)
  139. bb_perror_msg_and_die(bb_msg_write_error);
  140. if (delay > 0) {
  141. sleep(delay);
  142. }
  143. }
  144. }
  145. }
  146. }