ping[6]: accept and ignore -n. Code shrink
[oweals/busybox.git] / networking / nc_bloaty.c
index 4ba726eb5145c9b49ffc75b46a5e64c61d7299b1..00ba6f114657cf7660132fb670b4b53637e61aab 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2007 Denys Vlasenko.
  *
- * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 
 /* Author's comments from nc 1.10:
  * - source routing
  * - multiple DNS checks
  * Functionalty which is different from nc 1.10:
- * - Prog in '-e prog' can have prog's parameters and options.
+ * - PROG in '-e PROG' can have ARGS (and options).
  *   Because of this -e option must be last.
- * - nc doesn't redirect stderr to the network socket for the -e prog.
+//TODO: remove -e incompatibility?
+ * - we don't redirect stderr to the network socket for the -e PROG.
+ *   (PROG can do it itself if needed, but sometimes it is NOT wanted!)
  * - numeric addresses are printed in (), not [] (IPv6 looks better),
  *   port numbers are inside (): (1.2.3.4:5678)
  * - network read errors are reported on verbose levels > 1
  *   (nc 1.10 treats them as EOF)
  * - TCP connects from wrong ip/ports (if peer ip:port is specified
  *   on the command line, but accept() says that it came from different addr)
- *   are closed, but nc doesn't exit - continues to listen/accept.
+ *   are closed, but we don't exit - we continue to listen/accept.
  */
 
 /* done in nc.c: #include "libbb.h" */
 
+//usage:#if ENABLE_NC_110_COMPAT
+//usage:
+//usage:#define nc_trivial_usage
+//usage:       "[OPTIONS] HOST PORT  - connect"
+//usage:       IF_NC_SERVER("\n"
+//usage:       "nc [OPTIONS] -l -p PORT [HOST] [PORT]  - listen"
+//usage:       )
+//usage:#define nc_full_usage "\n\n"
+//usage:       "       -e PROG Run PROG after connect (must be last)"
+//usage:       IF_NC_SERVER(
+//usage:     "\n       -l      Listen mode, for inbound connects"
+//usage:     "\n       -lk     With -e, provides persistent server"
+/* -ll does the same as -lk, but its our extension, while -k is BSD'd,
+ * presumably more widely known. Therefore we advertise it, not -ll.
+ * I would like to drop -ll support, but our "small" nc supports it,
+ * and Rob uses it.
+ */
+//usage:       )
+//usage:     "\n       -p PORT Local port"
+//usage:     "\n       -s ADDR Local address"
+//usage:     "\n       -w SEC  Timeout for connects and final net reads"
+//usage:       IF_NC_EXTRA(
+//usage:     "\n       -i SEC  Delay interval for lines sent" /* ", ports scanned" */
+//usage:       )
+//usage:     "\n       -n      Don't do DNS resolution"
+//usage:     "\n       -u      UDP mode"
+//usage:     "\n       -v      Verbose"
+//usage:       IF_NC_EXTRA(
+//usage:     "\n       -o FILE Hex dump traffic"
+//usage:     "\n       -z      Zero-I/O mode (scanning)"
+//usage:       )
+//usage:#endif
+
+/*   "\n       -r              Randomize local and remote ports" */
+/*   "\n       -g gateway      Source-routing hop point[s], up to 8" */
+/*   "\n       -G num          Source-routing pointer: 4, 8, 12, ..." */
+/*   "\nport numbers can be individual or ranges: lo-hi [inclusive]" */
+
+/* -e PROG can take ARGS too: "nc ... -e ls -l", but we don't document it
+ * in help text: nc 1.10 does not allow that. We don't want to entice
+ * users to use this incompatibility */
+
 enum {
        SLEAZE_PORT = 31337,               /* for UDP-scan RTT trick, change if ya want */
        BIGSIZ = 8192,                     /* big buffers */
@@ -77,6 +121,7 @@ struct globals {
        unsigned wrote_out;          /* total stdout bytes */
        unsigned wrote_net;          /* total net bytes */
 #endif
+       char *proggie0saved;
        /* ouraddr is never NULL and goes through three states as we progress:
         1 - local address before bind (IP/port possibly zero)
         2 - local address after bind (port is nonzero)
@@ -89,7 +134,6 @@ struct globals {
 
        jmp_buf jbuf;                /* timer crud */
 
-       /* will malloc up the following globals: */
        fd_set ding1;                /* for select loop */
        fd_set ding2;
        char bigbuf_in[BIGSIZ];      /* data buffers */
@@ -121,26 +165,21 @@ struct globals {
 
 /* Must match getopt32 call! */
 enum {
-       OPT_h = (1 << 0),
-       OPT_n = (1 << 1),
-       OPT_p = (1 << 2),
-       OPT_s = (1 << 3),
-       OPT_u = (1 << 4),
-       OPT_v = (1 << 5),
-       OPT_w = (1 << 6),
-       OPT_l = (1 << 7) * ENABLE_NC_SERVER,
-       OPT_i = (1 << (7+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA,
-       OPT_o = (1 << (8+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA,
-       OPT_z = (1 << (9+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA,
+       OPT_n = (1 << 0),
+       OPT_p = (1 << 1),
+       OPT_s = (1 << 2),
+       OPT_u = (1 << 3),
+       OPT_v = (1 << 4),
+       OPT_w = (1 << 5),
+       OPT_l = (1 << 6) * ENABLE_NC_SERVER,
+       OPT_k = (1 << 7) * ENABLE_NC_SERVER,
+       OPT_i = (1 << (7+2*ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA,
+       OPT_o = (1 << (8+2*ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA,
+       OPT_z = (1 << (9+2*ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA,
 };
 
 #define o_nflag   (option_mask32 & OPT_n)
 #define o_udpmode (option_mask32 & OPT_u)
-#if ENABLE_NC_SERVER
-#define o_listen  (option_mask32 & OPT_l)
-#else
-#define o_listen  0
-#endif
 #if ENABLE_NC_EXTRA
 #define o_ofile   (option_mask32 & OPT_o)
 #define o_zero    (option_mask32 & OPT_z)
@@ -152,7 +191,7 @@ enum {
 /* Debug: squirt whatever message and sleep a bit so we can see it go by. */
 /* Beware: writes to stdOUT... */
 #if 0
-#define Debug(...) do { printf(__VA_ARGS__); printf("\n"); fflush(stdout); sleep(1); } while (0)
+#define Debug(...) do { printf(__VA_ARGS__); printf("\n"); fflush_all(); sleep(1); } while (0)
 #else
 #define Debug(...) do { } while (0)
 #endif
@@ -177,7 +216,7 @@ static void unarm(void)
 }
 
 /* timeout and other signal handling cruft */
-static void tmtravel(int sig ATTRIBUTE_UNUSED)
+static void tmtravel(int sig UNUSED_PARAM)
 {
        unarm();
        longjmp(jbuf, 1);
@@ -222,15 +261,16 @@ Debug("findline returning whole thing: %d", siz);
  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!! */
-static int doexec(char **proggie) ATTRIBUTE_NORETURN;
+static int doexec(char **proggie) NORETURN;
 static int doexec(char **proggie)
 {
+       if (G.proggie0saved)
+               proggie[0] = G.proggie0saved;
        xmove_fd(netfd, 0);
        dup2(0, 1);
        /* dup2(0, 2); - do we *really* want this? NO!
         * exec'ed prog can do it yourself, if needed */
-       execvp(proggie[0], proggie);
-       bb_perror_msg_and_die("exec");
+       BB_EXECVP_or_die(proggie);
 }
 
 /* connect_w_timeout:
@@ -260,7 +300,7 @@ static int connect_w_timeout(int fd)
  incoming and returns an open connection *from* someplace.  If we were
  given host/port args, any connections from elsewhere are rejected.  This
  in conjunction with local-address binding should limit things nicely... */
-static void dolisten(void)
+static void dolisten(int is_persistent, char **proggie)
 {
        int rr;
 
@@ -278,9 +318,9 @@ static void dolisten(void)
         random unknown port is probably not very useful without "netstat". */
        if (o_verbose) {
                char *addr;
-               rr = getsockname(netfd, &ouraddr->u.sa, &ouraddr->len);
-               if (rr < 0)
-                       bb_perror_msg_and_die("getsockname after bind");
+               getsockname(netfd, &ouraddr->u.sa, &ouraddr->len);
+               //if (rr < 0)
+               //      bb_perror_msg_and_die("getsockname after bind");
                addr = xmalloc_sockaddr2dotted(&ouraddr->u.sa);
                fprintf(stderr, "listening on %s ...\n", addr);
                free(addr);
@@ -333,6 +373,7 @@ create new one, and bind() it. TODO */
                        xconnect(netfd, &remend.u.sa, ouraddr->len);
        } else {
                /* TCP */
+ another:
                arm(o_wait); /* wrap this in a timer, too; 0 = forever */
                if (setjmp(jbuf) == 0) {
  again:
@@ -340,28 +381,54 @@ create new one, and bind() it. TODO */
                        rr = accept(netfd, &remend.u.sa, &remend.len);
                        if (rr < 0)
                                bb_perror_msg_and_die("accept");
-                       if (themaddr && memcmp(&remend.u.sa, &themaddr->u.sa, remend.len) != 0) {
-                               /* nc 1.10 bails out instead, and its error message
-                                * is not suppressed by o_verbose */
-                               if (o_verbose) {
-                                       char *remaddr = xmalloc_sockaddr2dotted(&remend.u.sa);
-                                       bb_error_msg("connect from wrong ip/port %s ignored", remaddr);
-                                       free(remaddr);
+                       if (themaddr) {
+                               int sv_port, port, r;
+
+                               sv_port = get_nport(&remend.u.sa); /* save */
+                               port = get_nport(&themaddr->u.sa);
+                               if (port == 0) {
+                                       /* "nc -nl -p LPORT RHOST" (w/o RPORT!):
+                                        * we should accept any remote port */
+                                       set_nport(&remend.u.sa, 0); /* blot out remote port# */
+                               }
+                               r = memcmp(&remend.u.sa, &themaddr->u.sa, remend.len);
+                               set_nport(&remend.u.sa, sv_port); /* restore */
+                               if (r != 0) {
+                                       /* nc 1.10 bails out instead, and its error message
+                                        * is not suppressed by o_verbose */
+                                       if (o_verbose) {
+                                               char *remaddr = xmalloc_sockaddr2dotted(&remend.u.sa);
+                                               bb_error_msg("connect from wrong ip/port %s ignored", remaddr);
+                                               free(remaddr);
+                                       }
+                                       close(rr);
+                                       goto again;
                                }
-                               close(rr);
-                               goto again;
                        }
                        unarm();
                } else
                        bb_error_msg_and_die("timeout");
+
+               if (is_persistent && proggie) {
+                       /* -l -k -e PROG */
+                       signal(SIGCHLD, SIG_IGN); /* no zombies please */
+                       if (xvfork() != 0) {
+                               /* parent: go back and accept more connections */
+                               close(rr);
+                               goto another;
+                       }
+                       /* child */
+                       signal(SIGCHLD, SIG_DFL);
+               }
+
                xmove_fd(rr, netfd); /* dump the old socket, here's our new one */
                /* find out what address the connection was *to* on our end, in case we're
                 doing a listen-on-any on a multihomed machine.  This allows one to
                 offer different services via different alias addresses, such as the
                 "virtual web site" hack. */
-               rr = getsockname(netfd, &ouraddr->u.sa, &ouraddr->len);
-               if (rr < 0)
-                       bb_perror_msg_and_die("getsockname after accept");
+               getsockname(netfd, &ouraddr->u.sa, &ouraddr->len);
+               //if (rr < 0)
+               //      bb_perror_msg_and_die("getsockname after accept");
        }
 
        if (o_verbose) {
@@ -377,11 +444,8 @@ create new one, and bind() it. TODO */
                socklen_t x = sizeof(optbuf);
 
                rr = getsockopt(netfd, IPPROTO_IP, IP_OPTIONS, optbuf, &x);
-               if (rr < 0)
-                       bb_perror_msg("getsockopt failed");
-               else if (x) {    /* we've got options, lessee em... */
-                       bin2hex(bigbuf_net, optbuf, x);
-                       bigbuf_net[2*x] = '\0';
+               if (rr >= 0 && x) {    /* we've got options, lessee em... */
+                       *bin2hex(bigbuf_net, optbuf, x) = '\0';
                        fprintf(stderr, "IP options: %s\n", bigbuf_net);
                }
 #endif
@@ -406,6 +470,9 @@ create new one, and bind() it. TODO */
                if (!o_nflag)
                        free(remhostname);
        }
+
+       if (proggie)
+               doexec(proggie);
 }
 
 /* udptest:
@@ -437,7 +504,7 @@ static int udptest(void)
         us to hang forever, and hit it */
                o_wait = 5;                     /* enough that we'll notice?? */
                rr = xsocket(ouraddr->u.sa.sa_family, SOCK_STREAM, 0);
-               set_nport(themaddr, htons(SLEAZE_PORT));
+               set_nport(&themaddr->u.sa, htons(SLEAZE_PORT));
                connect_w_timeout(rr);
                /* don't need to restore themaddr's port, it's not used anymore */
                close(rr);
@@ -562,7 +629,7 @@ static int readwrite(void)
        /* if we have a timeout AND stdin is closed AND we haven't heard anything
         from the net during that time, assume it's dead and close it too. */
                if (rr == 0) {
-                       if (!FD_ISSET(0, &ding1))
+                       if (!FD_ISSET(STDIN_FILENO, &ding1))
                                netretry--;                        /* we actually try a coupla times. */
                        if (!netretry) {
                                if (o_verbose > 1)                /* normally we don't care */
@@ -597,13 +664,16 @@ Debug("got %d from the net, errno %d", rr, errno);
                        goto shovel;
 
        /* okay, suck more stdin */
-               if (FD_ISSET(0, &ding2)) {                /* stdin: ding! */
-                       rr = read(0, bigbuf_in, BIGSIZ);
+               if (FD_ISSET(STDIN_FILENO, &ding2)) {                /* stdin: ding! */
+                       rr = read(STDIN_FILENO, bigbuf_in, BIGSIZ);
        /* Considered making reads here smaller for UDP mode, but 8192-byte
         mobygrams are kinda fun and exercise the reassembler. */
                        if (rr <= 0) {                        /* at end, or fukt, or ... */
-                               FD_CLR(0, &ding1);                /* disable and close stdin */
-                               close(0);
+                               FD_CLR(STDIN_FILENO, &ding1);                /* disable and close stdin */
+                               close(STDIN_FILENO);
+// Does it make sense to shutdown(net_fd, SHUT_WR)
+// to let other side know that we won't write anything anymore?
+// (and what about keeping compat if we do that?)
                        } else {
                                rzleft = rr;
                                zp = bigbuf_in;
@@ -625,7 +695,7 @@ Debug("got %d from the net, errno %d", rr, errno);
                        return 1;
                }
                if (rnleft) {
-                       rr = write(1, np, rnleft);
+                       rr = write(STDOUT_FILENO, np, rnleft);
                        if (rr > 0) {
                                if (o_ofile) /* log the stdout */
                                        oprint('<', (unsigned char *)np, rr);
@@ -672,13 +742,14 @@ Debug("wrote %d to net, errno %d", rr, errno);
 
 /* main: now we pull it all together... */
 int nc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int nc_main(int argc, char **argv)
+int nc_main(int argc UNUSED_PARAM, char **argv)
 {
        char *str_p, *str_s;
-       USE_NC_EXTRA(char *str_i, *str_o;)
-       char *themdotted = themdotted; /* gcc */
+       IF_NC_EXTRA(char *str_i, *str_o;)
+       char *themdotted = themdotted; /* for compiler */
        char **proggie;
        int x;
+       unsigned cnt_l = 0;
        unsigned o_lport = 0;
 
        INIT_G();
@@ -701,26 +772,43 @@ int nc_main(int argc, char **argv)
        while (*++proggie) {
                if (strcmp(*proggie, "-e") == 0) {
                        *proggie = NULL;
-                       argc = proggie - argv;
                        proggie++;
                        goto e_found;
                }
+               /* -<other_opts>e PROG [ARGS] ? */
+               /* (aboriginal linux uses this form) */
+               if (proggie[0][0] == '-') {
+                       char *optpos = *proggie + 1;
+                       /* Skip all valid opts w/o params */
+                       optpos = optpos + strspn(optpos, "nuv"IF_NC_SERVER("lk")IF_NC_EXTRA("z"));
+                       if (*optpos == 'e' && !optpos[1]) {
+                               *optpos = '\0';
+                               proggie++;
+                               G.proggie0saved = *proggie;
+                               *proggie = NULL; /* terminate argv for getopt32 */
+                               goto e_found;
+                       }
+               }
        }
        proggie = NULL;
  e_found:
 
        // -g -G -t -r deleted, unimplemented -a deleted too
-       opt_complementary = "?2:vv:w+"; /* max 2 params; -v is a counter; -w N */
-       getopt32(argv, "hnp:s:uvw:" USE_NC_SERVER("l")
-                       USE_NC_EXTRA("i:o:z"),
+       opt_complementary = "?2:vv:ll:w+"; /* max 2 params; -v and -l are counters; -w N */
+       getopt32(argv, "np:s:uvw:" IF_NC_SERVER("lk")
+                       IF_NC_EXTRA("i:o:z"),
                        &str_p, &str_s, &o_wait
-                       USE_NC_EXTRA(, &str_i, &str_o, &o_verbose));
+                       IF_NC_EXTRA(, &str_i, &str_o), &o_verbose IF_NC_SERVER(, &cnt_l));
        argv += optind;
 #if ENABLE_NC_EXTRA
        if (option_mask32 & OPT_i) /* line-interval time */
                o_interval = xatou_range(str_i, 1, 0xffff);
 #endif
+#if ENABLE_NC_SERVER
        //if (option_mask32 & OPT_l) /* listen mode */
+       if (option_mask32 & OPT_k) /* persistent server mode */
+               cnt_l = 2;
+#endif
        //if (option_mask32 & OPT_n) /* numeric-only, no DNS lookups */
        //if (option_mask32 & OPT_o) /* hexdump log */
        if (option_mask32 & OPT_p) { /* local source port */
@@ -762,13 +850,18 @@ int nc_main(int argc, char **argv)
                                (themaddr ? themaddr->u.sa.sa_family : AF_UNSPEC),
                                x);
                if (o_lport)
-                       set_nport(ouraddr, htons(o_lport));
+                       set_nport(&ouraddr->u.sa, htons(o_lport));
        }
        xmove_fd(x, netfd);
        setsockopt_reuseaddr(netfd);
        if (o_udpmode)
                socket_want_pktinfo(netfd);
-       xbind(netfd, &ouraddr->u.sa, ouraddr->len);
+       if (!ENABLE_FEATURE_UNIX_LOCAL
+        || cnt_l != 0 /* listen */
+        || ouraddr->u.sa.sa_family != AF_UNIX
+       ) {
+               xbind(netfd, &ouraddr->u.sa, ouraddr->len);
+       }
 #if 0
        setsockopt(netfd, SOL_SOCKET, SO_RCVBUF, &o_rcvbuf, sizeof o_rcvbuf);
        setsockopt(netfd, SOL_SOCKET, SO_SNDBUF, &o_sndbuf, sizeof o_sndbuf);
@@ -783,7 +876,7 @@ int nc_main(int argc, char **argv)
        }
 #endif
 
-       FD_SET(0, &ding1);                        /* stdin *is* initially open */
+       FD_SET(STDIN_FILENO, &ding1);                        /* stdin *is* initially open */
        if (proggie) {
                close(0); /* won't need stdin */
                option_mask32 &= ~OPT_o; /* -o with -e is meaningless! */
@@ -793,16 +886,14 @@ int nc_main(int argc, char **argv)
                xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), ofd);
 #endif
 
-       if (o_listen) {
-               dolisten();
+       if (cnt_l != 0) {
+               dolisten((cnt_l - 1), proggie);
                /* dolisten does its own connect reporting */
-               if (proggie) /* -e given? */
-                       doexec(proggie);
                x = readwrite(); /* it even works with UDP! */
        } else {
                /* Outbound connects.  Now we're more picky about args... */
                if (!themaddr)
-                       bb_error_msg_and_die("no destination");
+                       bb_show_usage();
 
                remend = *themaddr;
                if (o_verbose)