blob: bbcbc0d13e5a75067a2420035a0e99130daa7d99 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00002/* nc: mini-netcat - built from the ground up for LRP
Eric Andersencc8ed391999-10-05 16:24:54 +00003 Copyright (C) 1998 Charles P. Wright
4
5 0.0.1 6K It works.
6 0.0.2 5K Smaller and you can also check the exit condition if you wish.
Eric Andersenc7bda1c2004-03-15 08:29:22 +00007 0.0.3 Uses select()
Eric Andersencc8ed391999-10-05 16:24:54 +00008
9 19980918 Busy Boxed! Dave Cinege
Eric Andersen2ce1edc1999-10-12 15:42:48 +000010 19990512 Uses Select. Charles P. Wright
11 19990513 Fixes stdin stupidity and uses buffers. Charles P. Wright
Eric Andersencc8ed391999-10-05 16:24:54 +000012
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
27*/
Eric Andersen67059862001-01-22 22:48:42 +000028
Eric Andersencc8ed391999-10-05 16:24:54 +000029#include <stdio.h>
30#include <stdlib.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000031#include <string.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000032#include <unistd.h>
Mike Frysinger60a5c382005-05-06 04:45:38 +000033#include <signal.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000034
35#include <sys/types.h>
36#include <sys/socket.h>
37#include <netinet/in.h>
38#include <arpa/inet.h>
39#include <netdb.h>
40#include <sys/time.h>
41#include <sys/ioctl.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000042#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000043
Eric Andersen1323c942002-04-26 23:59:12 +000044#define GAPING_SECURITY_HOLE
45
Mike Frysinger60a5c382005-05-06 04:45:38 +000046static void timeout(int signum)
47{
48 bb_error_msg_and_die("Timed out");
49}
50
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000051int nc_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000052{
Mike Frysinger60a5c382005-05-06 04:45:38 +000053 int do_listen = 0, lport = 0, delay = 0, wsecs = 0, tmpfd, opt, sfd, x;
Matt Kraaibfa79672000-12-15 22:34:34 +000054 char buf[BUFSIZ];
Eric Andersen1323c942002-04-26 23:59:12 +000055#ifdef GAPING_SECURITY_HOLE
Eric Andersenc7bda1c2004-03-15 08:29:22 +000056 char * pr00gie = NULL;
Eric Andersen1323c942002-04-26 23:59:12 +000057#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000058
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 struct sockaddr_in address;
60 struct hostent *hostinfo;
Eric Andersencc8ed391999-10-05 16:24:54 +000061
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 fd_set readfds, testfds;
63
Mike Frysinger60a5c382005-05-06 04:45:38 +000064 while ((opt = getopt(argc, argv, "lp:i:e:w:")) > 0) {
Matt Kraai1d702672001-02-07 04:09:23 +000065 switch (opt) {
66 case 'l':
67 do_listen++;
68 break;
69 case 'p':
Glenn L McGrath036dbaa2004-01-17 05:03:31 +000070 lport = bb_lookup_port(optarg, "tcp", 0);
Matt Kraai1d702672001-02-07 04:09:23 +000071 break;
Eric Andersen1323c942002-04-26 23:59:12 +000072 case 'i':
73 delay = atoi(optarg);
74 break;
75#ifdef GAPING_SECURITY_HOLE
76 case 'e':
77 pr00gie = optarg;
78 break;
79#endif
Mike Frysinger60a5c382005-05-06 04:45:38 +000080 case 'w':
81 wsecs = atoi(optarg);
82 break;
Matt Kraai1d702672001-02-07 04:09:23 +000083 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000084 bb_show_usage();
Matt Kraai1d702672001-02-07 04:09:23 +000085 }
Erik Andersen5e1189e2000-04-15 16:34:54 +000086 }
Eric Andersenb6a44b81999-11-13 04:47:09 +000087
Eric Andersen1323c942002-04-26 23:59:12 +000088#ifdef GAPING_SECURITY_HOLE
89 if (pr00gie) {
90 /* won't need stdin */
Eric Andersen70060d22004-03-27 10:02:48 +000091 close (STDIN_FILENO);
Eric Andersen1323c942002-04-26 23:59:12 +000092 }
93#endif /* GAPING_SECURITY_HOLE */
94
95
Matt Kraai1d702672001-02-07 04:09:23 +000096 if ((do_listen && optind != argc) || (!do_listen && optind + 2 != argc))
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 bb_show_usage();
Matt Kraai1d702672001-02-07 04:09:23 +000098
Matt Kraaibfa79672000-12-15 22:34:34 +000099 if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 bb_perror_msg_and_die("socket");
Eric Andersenf63a20a2002-05-05 03:40:14 +0000101 x = 1;
102 if (setsockopt (sfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof (x)) == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103 bb_perror_msg_and_die ("reuseaddr failed");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000104 address.sin_family = AF_INET;
Eric Andersencc8ed391999-10-05 16:24:54 +0000105
Mike Frysinger60a5c382005-05-06 04:45:38 +0000106 if (wsecs) {
107 signal(SIGALRM, timeout);
108 alarm(wsecs);
109 }
110
Matt Kraai1d702672001-02-07 04:09:23 +0000111 if (lport != 0) {
112 memset(&address.sin_addr, 0, sizeof(address.sin_addr));
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000113 address.sin_port = lport;
Matt Kraai1d702672001-02-07 04:09:23 +0000114
115 if (bind(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116 bb_perror_msg_and_die("bind");
Matt Kraai1d702672001-02-07 04:09:23 +0000117 }
118
119 if (do_listen) {
Matt Kraaibe9f44a2001-05-15 03:05:39 +0000120 socklen_t addrlen = sizeof(address);
121
Matt Kraai1d702672001-02-07 04:09:23 +0000122 if (listen(sfd, 1) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123 bb_perror_msg_and_die("listen");
Matt Kraai1d702672001-02-07 04:09:23 +0000124
Matt Kraaibe9f44a2001-05-15 03:05:39 +0000125 if ((tmpfd = accept(sfd, (struct sockaddr *) &address, &addrlen)) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 bb_perror_msg_and_die("accept");
Matt Kraai1d702672001-02-07 04:09:23 +0000127
128 close(sfd);
129 sfd = tmpfd;
130 } else {
Matt Kraaic55b8d42001-05-16 15:40:51 +0000131 hostinfo = xgethostbyname(argv[optind]);
Matt Kraai1d702672001-02-07 04:09:23 +0000132
133 address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000134 address.sin_port = bb_lookup_port(argv[optind+1], "tcp", 0);
Matt Kraai1d702672001-02-07 04:09:23 +0000135
136 if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000137 bb_perror_msg_and_die("connect");
Matt Kraai1d702672001-02-07 04:09:23 +0000138 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000139
Mike Frysinger60a5c382005-05-06 04:45:38 +0000140 if (wsecs) {
141 alarm(0);
142 signal(SIGALRM, SIG_DFL);
143 }
144
Eric Andersen1323c942002-04-26 23:59:12 +0000145#ifdef GAPING_SECURITY_HOLE
146 /* -e given? */
147 if (pr00gie) {
148 dup2(sfd, 0);
149 close(sfd);
150 dup2 (0, 1);
151 dup2 (0, 2);
152 execl (pr00gie, pr00gie, NULL);
153 /* Don't print stuff or it will go over the wire.... */
154 _exit(-1);
155 }
156#endif /* GAPING_SECURITY_HOLE */
157
158
Erik Andersene49d5ec2000-02-08 19:58:47 +0000159 FD_ZERO(&readfds);
160 FD_SET(sfd, &readfds);
Matt Kraaibfa79672000-12-15 22:34:34 +0000161 FD_SET(STDIN_FILENO, &readfds);
Eric Andersencc8ed391999-10-05 16:24:54 +0000162
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 while (1) {
164 int fd;
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000165 int ofd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000166 int nread;
Eric Andersencc8ed391999-10-05 16:24:54 +0000167
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168 testfds = readfds;
Eric Andersencc8ed391999-10-05 16:24:54 +0000169
Matt Kraaibfa79672000-12-15 22:34:34 +0000170 if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171 bb_perror_msg_and_die("select");
Eric Andersencc8ed391999-10-05 16:24:54 +0000172
Erik Andersene49d5ec2000-02-08 19:58:47 +0000173 for (fd = 0; fd < FD_SETSIZE; fd++) {
174 if (FD_ISSET(fd, &testfds)) {
Matt Kraaibfa79672000-12-15 22:34:34 +0000175 if ((nread = safe_read(fd, buf, sizeof(buf))) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000176 bb_perror_msg_and_die("read");
Eric Andersencc8ed391999-10-05 16:24:54 +0000177
Erik Andersene49d5ec2000-02-08 19:58:47 +0000178 if (fd == sfd) {
179 if (nread == 0)
180 exit(0);
Matt Kraaibfa79672000-12-15 22:34:34 +0000181 ofd = STDOUT_FILENO;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182 } else {
Matt Kraai95fa0ea2000-12-14 04:34:58 +0000183 if (nread == 0)
184 shutdown(sfd, 1);
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000185 ofd = sfd;
186 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000187
Manuel Novoa III cad53642003-03-19 09:13:01 +0000188 if (bb_full_write(ofd, buf, nread) < 0)
189 bb_perror_msg_and_die("write");
Eric Andersen1323c942002-04-26 23:59:12 +0000190 if (delay > 0) {
191 sleep(delay);
192 }
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000193 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000194 }
195 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000196}