2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2010-08-16 23:44:46 +05:30
|
|
|
/* nc: mini-netcat - built from the ground up for LRP
|
2006-09-17 21:58:10 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Copyright (C) 1998, 1999 Charles P. Wright
|
|
|
|
* Copyright (C) 1998 Dave Cinege
|
2006-09-17 21:58:10 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2006-07-11 01:15:20 +05:30
|
|
|
*/
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2010-06-27 07:52:02 +05:30
|
|
|
//config:config NC
|
|
|
|
//config: bool "nc"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
|
|
|
//config: A simple Unix utility which reads and writes data across network
|
|
|
|
//config: connections.
|
|
|
|
//config:
|
|
|
|
//config:config NC_SERVER
|
|
|
|
//config: bool "Netcat server options (-l)"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on NC
|
|
|
|
//config: help
|
|
|
|
//config: Allow netcat to act as a server.
|
|
|
|
//config:
|
|
|
|
//config:config NC_EXTRA
|
2013-02-28 15:39:14 +05:30
|
|
|
//config: bool "Netcat extensions (-eiw and -f FILE)"
|
2010-06-27 07:52:02 +05:30
|
|
|
//config: default y
|
|
|
|
//config: depends on NC
|
|
|
|
//config: help
|
|
|
|
//config: Add -e (support for executing the rest of the command line after
|
|
|
|
//config: making or receiving a successful connection), -i (delay interval for
|
|
|
|
//config: lines sent), -w (timeout for initial connection).
|
|
|
|
//config:
|
|
|
|
//config:config NC_110_COMPAT
|
|
|
|
//config: bool "Netcat 1.10 compatibility (+2.5k)"
|
2010-07-19 02:32:36 +05:30
|
|
|
//config: default n # off specially for Rob
|
2010-06-27 07:52:02 +05:30
|
|
|
//config: depends on NC
|
|
|
|
//config: help
|
|
|
|
//config: This option makes nc closely follow original nc-1.10.
|
|
|
|
//config: The code is about 2.5k bigger. It enables
|
|
|
|
//config: -s ADDR, -n, -u, -v, -o FILE, -z options, but loses
|
2013-02-28 17:09:27 +05:30
|
|
|
//config: busybox-specific extensions: -f FILE.
|
2010-06-27 07:52:02 +05:30
|
|
|
|
|
|
|
#if ENABLE_NC_110_COMPAT
|
|
|
|
# include "nc_bloaty.c"
|
2007-04-06 01:56:28 +05:30
|
|
|
#else
|
|
|
|
|
2010-06-27 07:52:02 +05:30
|
|
|
//usage:#if !ENABLE_NC_110_COMPAT
|
|
|
|
//usage:
|
|
|
|
//usage:#if ENABLE_NC_SERVER || ENABLE_NC_EXTRA
|
2011-06-05 07:28:28 +05:30
|
|
|
//usage:#define NC_OPTIONS_STR "\n"
|
2010-06-27 07:52:02 +05:30
|
|
|
//usage:#else
|
|
|
|
//usage:#define NC_OPTIONS_STR
|
|
|
|
//usage:#endif
|
|
|
|
//usage:
|
|
|
|
//usage:#define nc_trivial_usage
|
|
|
|
//usage: IF_NC_EXTRA("[-iN] [-wN] ")IF_NC_SERVER("[-l] [-p PORT] ")
|
|
|
|
//usage: "["IF_NC_EXTRA("-f FILE|")"IPADDR PORT]"IF_NC_EXTRA(" [-e PROG]")
|
|
|
|
//usage:#define nc_full_usage "\n\n"
|
|
|
|
//usage: "Open a pipe to IP:PORT" IF_NC_EXTRA(" or FILE")
|
|
|
|
//usage: NC_OPTIONS_STR
|
|
|
|
//usage: IF_NC_SERVER(
|
|
|
|
//usage: "\n -l Listen mode, for inbound connects"
|
|
|
|
//usage: IF_NC_EXTRA(
|
2013-02-28 15:39:14 +05:30
|
|
|
//usage: "\n (use -ll with -e for persistent server)"
|
|
|
|
//usage: )
|
2010-06-27 07:52:02 +05:30
|
|
|
//usage: "\n -p PORT Local port"
|
|
|
|
//usage: )
|
2013-02-28 15:39:14 +05:30
|
|
|
//usage: IF_NC_EXTRA(
|
|
|
|
//usage: "\n -w SEC Connect timeout"
|
2010-06-27 07:52:02 +05:30
|
|
|
//usage: "\n -i SEC Delay interval for lines sent"
|
|
|
|
//usage: "\n -f FILE Use file (ala /dev/ttyS0) instead of network"
|
2013-02-28 15:39:14 +05:30
|
|
|
//usage: "\n -e PROG Run PROG after connect"
|
2010-06-27 07:52:02 +05:30
|
|
|
//usage: )
|
|
|
|
//usage:
|
|
|
|
//usage:#define nc_notes_usage ""
|
|
|
|
//usage: IF_NC_EXTRA(
|
|
|
|
//usage: "To use netcat as a terminal emulator on a serial port:\n\n"
|
|
|
|
//usage: "$ stty 115200 -F /dev/ttyS0\n"
|
|
|
|
//usage: "$ stty raw -echo -ctlecho && nc -f /dev/ttyS0\n"
|
|
|
|
//usage: )
|
|
|
|
//usage:
|
|
|
|
//usage:#define nc_example_usage
|
|
|
|
//usage: "$ nc foobar.somedomain.com 25\n"
|
|
|
|
//usage: "220 foobar ESMTP Exim 3.12 #1 Sat, 15 Apr 2000 00:03:02 -0600\n"
|
|
|
|
//usage: "help\n"
|
|
|
|
//usage: "214-Commands supported:\n"
|
|
|
|
//usage: "214- HELO EHLO MAIL RCPT DATA AUTH\n"
|
|
|
|
//usage: "214 NOOP QUIT RSET HELP\n"
|
|
|
|
//usage: "quit\n"
|
|
|
|
//usage: "221 foobar closing connection\n"
|
|
|
|
//usage:
|
|
|
|
//usage:#endif
|
|
|
|
|
2007-01-13 02:29:31 +05:30
|
|
|
/* Lots of small differences in features
|
|
|
|
* when compared to "standard" nc
|
|
|
|
*/
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void timeout(int signum UNUSED_PARAM)
|
2005-05-06 10:15:38 +05:30
|
|
|
{
|
2006-10-08 18:19:22 +05:30
|
|
|
bb_error_msg_and_die("timed out");
|
2005-05-06 10:15:38 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int nc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2000-05-13 01:11:47 +05:30
|
|
|
int nc_main(int argc, char **argv)
|
1999-10-05 21:54:54 +05:30
|
|
|
{
|
2007-01-13 02:29:31 +05:30
|
|
|
/* sfd sits _here_ only because of "repeat" option (-l -l). */
|
|
|
|
int sfd = sfd; /* for gcc */
|
2006-10-16 06:40:28 +05:30
|
|
|
int cfd = 0;
|
2007-01-20 22:24:19 +05:30
|
|
|
unsigned lport = 0;
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_NOT_NC_SERVER(const) unsigned do_listen = 0;
|
|
|
|
IF_NOT_NC_EXTRA (const) unsigned wsecs = 0;
|
|
|
|
IF_NOT_NC_EXTRA (const) unsigned delay = 0;
|
|
|
|
IF_NOT_NC_EXTRA (const int execparam = 0;)
|
|
|
|
IF_NC_EXTRA (char **execparam = NULL;)
|
2000-02-09 01:28:47 +05:30
|
|
|
fd_set readfds, testfds;
|
2006-10-16 06:40:28 +05:30
|
|
|
int opt; /* must be signed (getopt returns -1) */
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2006-09-17 21:58:10 +05:30
|
|
|
if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) {
|
2006-10-16 06:40:28 +05:30
|
|
|
/* getopt32 is _almost_ usable:
|
2010-06-27 07:52:02 +05:30
|
|
|
** it cannot handle "... -e PROG -prog-opt" */
|
2006-10-16 06:40:28 +05:30
|
|
|
while ((opt = getopt(argc, argv,
|
2013-01-14 06:04:48 +05:30
|
|
|
"" IF_NC_SERVER("lp:") IF_NC_EXTRA("w:i:f:e:") )) > 0
|
2006-10-16 06:40:28 +05:30
|
|
|
) {
|
2009-04-25 18:46:53 +05:30
|
|
|
if (ENABLE_NC_SERVER && opt == 'l')
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_NC_SERVER(do_listen++);
|
2009-04-25 18:46:53 +05:30
|
|
|
else if (ENABLE_NC_SERVER && opt == 'p')
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_NC_SERVER(lport = bb_lookup_port(optarg, "tcp", 0));
|
2009-04-25 18:46:53 +05:30
|
|
|
else if (ENABLE_NC_EXTRA && opt == 'w')
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_NC_EXTRA( wsecs = xatou(optarg));
|
2009-04-25 18:46:53 +05:30
|
|
|
else if (ENABLE_NC_EXTRA && opt == 'i')
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_NC_EXTRA( delay = xatou(optarg));
|
2009-04-25 18:46:53 +05:30
|
|
|
else if (ENABLE_NC_EXTRA && opt == 'f')
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_NC_EXTRA( cfd = xopen(optarg, O_RDWR));
|
2009-04-25 18:46:53 +05:30
|
|
|
else if (ENABLE_NC_EXTRA && opt == 'e' && optind <= argc) {
|
2006-10-16 06:40:28 +05:30
|
|
|
/* We cannot just 'break'. We should let getopt finish.
|
|
|
|
** Or else we won't be able to find where
|
|
|
|
** 'host' and 'port' params are
|
2010-06-27 07:52:02 +05:30
|
|
|
** (think "nc -w 60 host port -e PROG"). */
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_NC_EXTRA(
|
2006-10-16 06:40:28 +05:30
|
|
|
char **p;
|
|
|
|
// +2: one for progname (optarg) and one for NULL
|
|
|
|
execparam = xzalloc(sizeof(char*) * (argc - optind + 2));
|
|
|
|
p = execparam;
|
|
|
|
*p++ = optarg;
|
|
|
|
while (optind < argc) {
|
|
|
|
*p++ = argv[optind++];
|
|
|
|
}
|
|
|
|
)
|
2013-02-28 15:39:14 +05:30
|
|
|
/* optind points to argv[argc] (NULL) now.
|
2006-10-16 06:40:28 +05:30
|
|
|
** FIXME: we assume that getopt will not count options
|
2010-06-27 07:52:02 +05:30
|
|
|
** possibly present on "-e PROG ARGS" and will not
|
2006-10-16 06:40:28 +05:30
|
|
|
** include them into final value of optind
|
|
|
|
** which is to be used ... */
|
2006-07-11 01:15:20 +05:30
|
|
|
} else bb_show_usage();
|
2001-02-07 09:39:23 +05:30
|
|
|
}
|
2006-10-16 06:40:28 +05:30
|
|
|
argv += optind; /* ... here! */
|
|
|
|
argc -= optind;
|
|
|
|
// -l and -f don't mix
|
|
|
|
if (do_listen && cfd) bb_show_usage();
|
2009-04-25 18:46:53 +05:30
|
|
|
// File mode needs need zero arguments, listen mode needs zero or one,
|
|
|
|
// client mode needs one or two
|
|
|
|
if (cfd) {
|
2007-01-13 02:29:31 +05:30
|
|
|
if (argc) bb_show_usage();
|
2009-04-25 18:46:53 +05:30
|
|
|
} else if (do_listen) {
|
|
|
|
if (argc > 1) bb_show_usage();
|
2007-01-13 02:29:31 +05:30
|
|
|
} else {
|
|
|
|
if (!argc || argc > 2) bb_show_usage();
|
|
|
|
}
|
2006-10-16 06:40:28 +05:30
|
|
|
} else {
|
|
|
|
if (argc != 3) bb_show_usage();
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2005-05-06 10:15:38 +05:30
|
|
|
if (wsecs) {
|
|
|
|
signal(SIGALRM, timeout);
|
|
|
|
alarm(wsecs);
|
|
|
|
}
|
2006-09-17 21:58:10 +05:30
|
|
|
|
2006-10-16 06:40:28 +05:30
|
|
|
if (!cfd) {
|
2006-07-11 01:15:20 +05:30
|
|
|
if (do_listen) {
|
2009-04-25 18:46:53 +05:30
|
|
|
sfd = create_and_bind_stream_or_die(argv[0], lport);
|
2007-01-13 02:29:31 +05:30
|
|
|
xlisten(sfd, do_listen); /* can be > 1 */
|
2009-04-25 18:46:53 +05:30
|
|
|
#if 0 /* nc-1.10 does not do this (without -v) */
|
2007-01-13 02:29:31 +05:30
|
|
|
/* If we didn't specify a port number,
|
|
|
|
* query and print it after listen() */
|
2006-07-11 01:15:20 +05:30
|
|
|
if (!lport) {
|
2009-04-25 18:46:53 +05:30
|
|
|
len_and_sockaddr lsa;
|
|
|
|
lsa.len = LSA_SIZEOF_SA;
|
|
|
|
getsockname(sfd, &lsa.u.sa, &lsa.len);
|
|
|
|
lport = get_nport(&lsa.u.sa);
|
2007-01-13 02:29:31 +05:30
|
|
|
fdprintf(2, "%d\n", ntohs(lport));
|
2006-07-11 01:15:20 +05:30
|
|
|
}
|
2009-04-25 18:46:53 +05:30
|
|
|
#endif
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(sfd);
|
2007-01-13 02:29:31 +05:30
|
|
|
accept_again:
|
2007-01-22 19:42:08 +05:30
|
|
|
cfd = accept(sfd, NULL, 0);
|
2006-10-08 18:19:22 +05:30
|
|
|
if (cfd < 0)
|
2006-07-11 01:15:20 +05:30
|
|
|
bb_perror_msg_and_die("accept");
|
2007-01-13 02:29:31 +05:30
|
|
|
if (!execparam)
|
|
|
|
close(sfd);
|
2006-07-11 01:15:20 +05:30
|
|
|
} else {
|
2007-01-13 02:29:31 +05:30
|
|
|
cfd = create_and_connect_stream_or_die(argv[0],
|
|
|
|
argv[1] ? bb_lookup_port(argv[1], "tcp", 0) : 0);
|
2006-07-11 01:15:20 +05:30
|
|
|
}
|
2001-02-07 09:39:23 +05:30
|
|
|
}
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2005-05-06 10:15:38 +05:30
|
|
|
if (wsecs) {
|
|
|
|
alarm(0);
|
2009-06-01 14:56:30 +05:30
|
|
|
/* Non-ignored signals revert to SIG_DFL on exec anyway */
|
2008-11-09 05:45:11 +05:30
|
|
|
/*signal(SIGALRM, SIG_DFL);*/
|
2005-05-06 10:15:38 +05:30
|
|
|
}
|
|
|
|
|
2002-04-27 05:29:12 +05:30
|
|
|
/* -e given? */
|
2006-10-16 06:40:28 +05:30
|
|
|
if (execparam) {
|
2009-08-03 06:29:22 +05:30
|
|
|
pid_t pid;
|
|
|
|
/* With more than one -l, repeatedly act as server */
|
2010-07-04 19:02:38 +05:30
|
|
|
if (do_listen > 1 && (pid = xvfork()) != 0) {
|
|
|
|
/* parent */
|
2009-08-03 06:29:22 +05:30
|
|
|
/* prevent zombies */
|
|
|
|
signal(SIGCHLD, SIG_IGN);
|
|
|
|
close(cfd);
|
2007-01-13 02:29:31 +05:30
|
|
|
goto accept_again;
|
2006-07-11 01:15:20 +05:30
|
|
|
}
|
2009-08-03 06:29:22 +05:30
|
|
|
/* child, or main thread if only one -l */
|
2008-07-01 21:39:07 +05:30
|
|
|
xmove_fd(cfd, 0);
|
|
|
|
xdup2(0, 1);
|
2013-02-28 15:39:14 +05:30
|
|
|
/*xdup2(0, 2); - original nc 1.10 does this, we don't */
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_NC_EXTRA(BB_EXECVP(execparam[0], execparam);)
|
2013-03-18 03:29:51 +05:30
|
|
|
IF_NC_EXTRA(bb_perror_msg_and_die("can't execute '%s'", execparam[0]);)
|
2002-04-27 05:29:12 +05:30
|
|
|
}
|
|
|
|
|
2009-08-03 06:29:22 +05:30
|
|
|
/* Select loop copying stdin to cfd, and cfd to stdout */
|
2006-09-17 21:58:10 +05:30
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
FD_ZERO(&readfds);
|
2006-07-11 01:15:20 +05:30
|
|
|
FD_SET(cfd, &readfds);
|
2000-12-16 04:04:34 +05:30
|
|
|
FD_SET(STDIN_FILENO, &readfds);
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2016-04-21 22:08:51 +05:30
|
|
|
#define iobuf bb_common_bufsiz1
|
|
|
|
setup_common_bufsiz();
|
2006-07-11 01:15:20 +05:30
|
|
|
for (;;) {
|
2000-02-09 01:28:47 +05:30
|
|
|
int fd;
|
1999-10-12 21:12:48 +05:30
|
|
|
int ofd;
|
2000-02-09 01:28:47 +05:30
|
|
|
int nread;
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
testfds = readfds;
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2009-08-03 06:29:22 +05:30
|
|
|
if (select(cfd + 1, &testfds, NULL, NULL, NULL) < 0)
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_perror_msg_and_die("select");
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2009-08-03 06:29:22 +05:30
|
|
|
fd = STDIN_FILENO;
|
|
|
|
while (1) {
|
2000-02-09 01:28:47 +05:30
|
|
|
if (FD_ISSET(fd, &testfds)) {
|
2016-04-21 19:56:30 +05:30
|
|
|
nread = safe_read(fd, iobuf, COMMON_BUFSIZE);
|
2006-07-11 01:15:20 +05:30
|
|
|
if (fd == cfd) {
|
2007-01-13 02:29:31 +05:30
|
|
|
if (nread < 1)
|
2008-05-19 14:59:47 +05:30
|
|
|
exit(EXIT_SUCCESS);
|
2000-12-16 04:04:34 +05:30
|
|
|
ofd = STDOUT_FILENO;
|
2000-02-09 01:28:47 +05:30
|
|
|
} else {
|
2009-08-03 06:29:22 +05:30
|
|
|
if (nread < 1) {
|
|
|
|
/* Close outgoing half-connection so they get EOF,
|
|
|
|
* but leave incoming alone so we can see response */
|
2013-07-29 00:38:37 +05:30
|
|
|
shutdown(cfd, SHUT_WR);
|
2005-07-19 03:53:16 +05:30
|
|
|
FD_CLR(STDIN_FILENO, &readfds);
|
|
|
|
}
|
2006-07-11 01:15:20 +05:30
|
|
|
ofd = cfd;
|
1999-10-12 21:12:48 +05:30
|
|
|
}
|
2007-06-04 15:46:52 +05:30
|
|
|
xwrite(ofd, iobuf, nread);
|
2009-08-03 06:29:22 +05:30
|
|
|
if (delay > 0)
|
|
|
|
sleep(delay);
|
1999-10-12 21:12:48 +05:30
|
|
|
}
|
2009-08-03 06:29:22 +05:30
|
|
|
if (fd == cfd)
|
|
|
|
break;
|
|
|
|
fd = cfd;
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
|
|
|
}
|
1999-10-05 21:54:54 +05:30
|
|
|
}
|
2007-04-06 01:56:28 +05:30
|
|
|
#endif
|