$NetBSD: patch-ad,v 1.7 2025/04/29 09:50:22 tnn Exp $ --- netcat.c.orig 1996-03-21 00:38:04.000000000 +0000 +++ netcat.c @@ -44,12 +44,7 @@ #include #endif -/* have to do this *before* including types.h. xxx: Linux still has it wrong */ -#ifdef FD_SETSIZE /* should be in types.h, butcha never know. */ -#undef FD_SETSIZE /* if we ever need more than 16 active */ -#endif /* fd's, something is horribly wrong! */ -#define FD_SETSIZE 16 /* <-- this'll give us a long anyways, wtf */ -#include /* *now* do it. Sigh, this is broken */ +#include #ifdef HAVE_RANDOM /* aficionados of ?rand48() should realize */ #define SRAND srandom /* that this doesn't need *strong* random */ @@ -73,6 +68,8 @@ #include #include #include /* O_WRONLY et al */ +#include +#include /* handy stuff: */ #define SA struct sockaddr /* socket overgeneralization braindeath */ @@ -151,6 +148,7 @@ unsigned char * stage = NULL; /* hexdump /* global cmd flags: */ USHORT o_alla = 0; +USHORT o_allowbroad = 0; unsigned int o_interval = 0; USHORT o_listen = 0; USHORT o_nflag = 0; @@ -160,6 +158,7 @@ USHORT o_udpmode = 0; USHORT o_verbose = 0; unsigned int o_wait = 0; USHORT o_zero = 0; +int o_quit = -1; /* 0 == quit-now; >0 == quit after o_quit seconds */ /* o_tn in optional section */ /* Debug macro: squirt whatever message and sleep a bit so we can see it go @@ -171,6 +170,7 @@ USHORT o_zero = 0; #define Debug(x) /* nil... */ #endif +void helpme(); /* support routines -- the bulk of this thing. Placed in such an order that we don't have to forward-declare anything: */ @@ -217,7 +217,7 @@ void bail (str, p1, p2, p3, p4, p5, p6) /* catch : no-brainer interrupt handler */ -void catch () +void catch (int signum) { errno = 0; if (o_verbose > 1) /* normally we don't care */ @@ -225,8 +225,16 @@ void catch () bail (" punt!"); } +/* quit : + handler for a "-q" timeout (exit 0 instead of 1) */ +void quit(int signum) +{ + close(netfd); + exit(0); +} + /* timeout and other signal handling cruft */ -void tmtravel () +void tmtravel (int signum) { signal (SIGALRM, SIG_IGN); alarm (0); @@ -333,6 +341,7 @@ HINF * gethostpoop (name, numeric) struct in_addr iaddr; register HINF * poop = NULL; register int x; + int rc; /* I really want to strangle the twit who dreamed up all these sockaddr and hostent abstractions, and then forced them all to be incompatible with @@ -361,9 +370,9 @@ HINF * gethostpoop (name, numeric) bail ("gethostpoop fuxored"); strcpy (poop->name, unknown); /* preload it */ /* see wzv:workarounds.c for dg/ux return-a-struct inet_addr lossage */ - iaddr.s_addr = inet_addr (name); + rc = inet_aton(name, &iaddr); - if (iaddr.s_addr == INADDR_NONE) { /* here's the great split: names... */ + if (rc == 0) { /* here's the great split: names... */ if (numeric) bail ("Can't parse %s as an IP address", name); hostent = gethostbyname (name); @@ -568,6 +577,7 @@ char * pr00gie = NULL; /* global ptr t that would be security-critical, which is why it's ifdefed out by default. Use at your own hairy risk; if you leave shells lying around behind open listening ports you deserve to lose!! */ +void doexec (fd) int fd; { @@ -620,6 +630,13 @@ newskt: rr = setsockopt (nnetfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof (x)); if (rr == -1) holler ("nnetfd reuseaddr failed"); /* ??? */ +#ifdef SO_BROADCAST + if (o_allowbroad) { + rr = setsockopt (nnetfd, SOL_SOCKET, SO_BROADCAST, &x, sizeof (x)); + if (rr == -1) + holler ("nnetfd reuseaddr failed"); /* ??? */ + } +#endif #ifdef SO_REUSEPORT /* doesnt exist everywhere... */ rr = setsockopt (nnetfd, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x)); if (rr == -1) @@ -944,6 +961,7 @@ dol_err: Use the time delay between writes if given, otherwise use the "tcp ping" trick for getting the RTT. [I got that idea from pluvius, and warped it.] Return either the original fd, or clean up and return -1. */ +int udptest (fd, where) int fd; IA * where; @@ -1216,6 +1234,18 @@ Debug (("got %d from the net, errno %d", if (rr <= 0) { /* at end, or fukt, or ... */ FD_CLR (0, ding1); /* disable and close stdin */ close (0); + /* if the user asked to exit on EOF, do it */ + if (o_quit == 0) { + shutdown(netfd, 1); + close (fd); + exit (0); + } + /* if user asked to die after a while, arrange for it */ + if (o_quit > 0) { + shutdown(netfd, 1); + signal (SIGALRM, quit); + alarm(o_quit); + } } else { rzleft = rr; zp = bigbuf_in; @@ -1292,6 +1322,7 @@ Debug (("wrote %d to net, errno %d", rr, /* main : now we pull it all together... */ +int main (argc, argv) int argc; char ** argv; @@ -1314,10 +1345,6 @@ main (argc, argv) USHORT curport = 0; char * randports = NULL; -#ifdef HAVE_BIND -/* can *you* say "cc -yaddayadda netcat.c -lresolv -l44bsd" on SunLOSs? */ - res_init(); -#endif /* I was in this barbershop quartet in Skokie IL ... */ /* round up the usual suspects, i.e. malloc up all the stuff we need */ lclend = (SAI *) Hmalloc (sizeof (SA)); @@ -1354,7 +1381,8 @@ main (argc, argv) argv[1] = cp; /* head of new arg block */ fprintf (stderr, "Cmd line: "); fflush (stderr); /* I dont care if it's unbuffered or not! */ - insaved = read (0, cp, BIGSIZ); /* we're gonna fake fgets() here */ + insaved = read (0, cp, BIGSIZ-1); /* we're gonna fake fgets() here */ + cp[BIGSIZ-1] = '\0'; if (insaved <= 0) bail ("wrong"); x = findline (cp, insaved); @@ -1389,12 +1417,14 @@ main (argc, argv) /* If your shitbox doesn't have getopt, step into the nineties already. */ /* optarg, optind = next-argv-component [i.e. flag arg]; optopt = last-char */ - while ((x = getopt (argc, argv, "ae:g:G:hi:lno:p:rs:tuvw:z")) != EOF) { + while ((x = getopt (argc, argv, "abe:g:G:hi:lno:p:q:rs:tuvw:z")) != EOF) { /* Debug (("in go: x now %c, optarg %x optind %d", x, optarg, optind)) */ switch (x) { case 'a': bail ("all-A-records NIY"); o_alla++; break; + case 'b': + o_allowbroad++; break; #ifdef GAPING_SECURITY_HOLE case 'e': /* prog to exec */ pr00gie = optarg; @@ -1443,6 +1473,8 @@ main (argc, argv) break; case 'r': /* randomize various things */ o_random++; break; + case 'q': /* quit after stdin does EOF */ + o_quit = atoi(optarg); break; case 's': /* local source address */ /* do a full lookup [since everything else goes through the same mill], unless -n was previously specified. In fact, careful placement of -n can @@ -1627,6 +1659,7 @@ Debug (("netfd %d from port %d to port % #ifdef HAVE_HELP /* unless we wanna be *really* cryptic */ /* helpme : the obvious */ +void helpme() { o_verbose = 1; @@ -1642,6 +1675,7 @@ options:"); -e prog program to exec after connect [dangerous!!]"); #endif holler ("\ + -b allow broadcasts\n\ -g gateway source-routing hop point[s], up to 8\n\ -G num source-routing pointer: 4, 8, 12, ...\n\ -h this cruft\n\ @@ -1651,6 +1685,7 @@ options:"); -o file hex dump of traffic\n\ -p port local port number\n\ -r randomize local and remote ports\n\ + -q secs quit after EOF on stdin and delay of secs\n\ -s addr local source address"); #ifdef TELNET holler ("\