netcat.c (141262) | netcat.c (141394) |
---|---|
1/* $OpenBSD: netcat.c,v 1.76 2004/12/10 16:51:31 hshoexer Exp $ */ | |
2/* 3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright --- 9 unchanged lines hidden (view full) --- 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 1/* 2 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright --- 9 unchanged lines hidden (view full) --- 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 * 27 * From: $OpenBSD: netcat.c,v 1.76 2004/12/10 16:51:31 hshoexer Exp $ 28 * $FreeBSD: head/contrib/netcat/netcat.c 141394 2005-02-06 14:44:27Z delphij $ |
|
27 */ 28 29/* 30 * Re-written nc(1) for OpenBSD. Original implementation by 31 * *Hobbit* <hobbit@avian.org>. 32 */ 33 | 29 */ 30 31/* 32 * Re-written nc(1) for OpenBSD. Original implementation by 33 * *Hobbit* <hobbit@avian.org>. 34 */ 35 |
36#include <sys/limits.h> |
|
34#include <sys/types.h> 35#include <sys/socket.h> 36#include <sys/time.h> 37#include <sys/un.h> 38 39#include <netinet/in.h> | 37#include <sys/types.h> 38#include <sys/socket.h> 39#include <sys/time.h> 40#include <sys/un.h> 41 42#include <netinet/in.h> |
43#ifdef IPSEC 44#include <netinet6/ipsec.h> 45#endif |
|
40#include <netinet/tcp.h> 41#include <arpa/telnet.h> 42 43#include <err.h> 44#include <errno.h> 45#include <netdb.h> 46#include <poll.h> 47#include <stdarg.h> --- 7 unchanged lines hidden (view full) --- 55#define SUN_LEN(su) \ 56 (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path)) 57#endif 58 59#define PORT_MAX 65535 60#define PORT_MAX_LEN 6 61 62/* Command Line Options */ | 46#include <netinet/tcp.h> 47#include <arpa/telnet.h> 48 49#include <err.h> 50#include <errno.h> 51#include <netdb.h> 52#include <poll.h> 53#include <stdarg.h> --- 7 unchanged lines hidden (view full) --- 61#define SUN_LEN(su) \ 62 (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path)) 63#endif 64 65#define PORT_MAX 65535 66#define PORT_MAX_LEN 6 67 68/* Command Line Options */ |
69int Eflag; /* Use IPsec ESP */ |
|
63int dflag; /* detached, no stdin */ 64int iflag; /* Interval Flag */ 65int kflag; /* More than one connect */ 66int lflag; /* Bind to local port */ 67int nflag; /* Don't do name look up */ | 70int dflag; /* detached, no stdin */ 71int iflag; /* Interval Flag */ 72int kflag; /* More than one connect */ 73int lflag; /* Bind to local port */ 74int nflag; /* Don't do name look up */ |
75int oflag; /* Once only: stop on EOF */ |
|
68char *pflag; /* Localport flag */ 69int rflag; /* Random ports flag */ 70char *sflag; /* Source Address */ 71int tflag; /* Telnet Emulation */ 72int uflag; /* UDP - Default to TCP */ 73int vflag; /* Verbosity */ 74int xflag; /* Socks proxy */ 75int zflag; /* Port Scan Flag */ --- 13 unchanged lines hidden (view full) --- 89int remote_connect(char *, char *, struct addrinfo); 90int socks_connect(char *, char *, struct addrinfo, char *, char *, 91 struct addrinfo, int); 92int udptest(int); 93int unix_connect(char *); 94int unix_listen(char *); 95void usage(int); 96 | 76char *pflag; /* Localport flag */ 77int rflag; /* Random ports flag */ 78char *sflag; /* Source Address */ 79int tflag; /* Telnet Emulation */ 80int uflag; /* UDP - Default to TCP */ 81int vflag; /* Verbosity */ 82int xflag; /* Socks proxy */ 83int zflag; /* Port Scan Flag */ --- 13 unchanged lines hidden (view full) --- 97int remote_connect(char *, char *, struct addrinfo); 98int socks_connect(char *, char *, struct addrinfo, char *, char *, 99 struct addrinfo, int); 100int udptest(int); 101int unix_connect(char *); 102int unix_listen(char *); 103void usage(int); 104 |
105#ifdef IPSEC 106void add_ipsec_policy(int, char *); 107 108char *ipsec_policy[2]; 109#endif 110 |
|
97int 98main(int argc, char *argv[]) 99{ | 111int 112main(int argc, char *argv[]) 113{ |
100 int ch, s, ret, socksv; | 114 int ch, s, ret, socksv, ipsec_count; |
101 char *host, *uport, *endp; 102 struct addrinfo hints; 103 struct servent *sv; 104 socklen_t len; 105 struct sockaddr_storage cliaddr; 106 char *proxy; 107 char *proxyhost = "", *proxyport = NULL; 108 struct addrinfo proxyhints; 109 110 ret = 1; | 115 char *host, *uport, *endp; 116 struct addrinfo hints; 117 struct servent *sv; 118 socklen_t len; 119 struct sockaddr_storage cliaddr; 120 char *proxy; 121 char *proxyhost = "", *proxyport = NULL; 122 struct addrinfo proxyhints; 123 124 ret = 1; |
125 ipsec_count = 0; |
|
111 s = 0; 112 socksv = 5; 113 host = NULL; 114 uport = NULL; 115 endp = NULL; 116 sv = NULL; 117 | 126 s = 0; 127 socksv = 5; 128 host = NULL; 129 uport = NULL; 130 endp = NULL; 131 sv = NULL; 132 |
118 while ((ch = getopt(argc, argv, "46Ddhi:klnp:rSs:tUuvw:X:x:z")) != -1) { | 133 while ((ch = getopt(argc, argv, "46e:DEdhi:klnop:rSs:tUuvw:X:x:z")) != -1) { |
119 switch (ch) { 120 case '4': 121 family = AF_INET; 122 break; 123 case '6': 124 family = AF_INET6; 125 break; 126 case 'U': --- 7 unchanged lines hidden (view full) --- 134 else if (strcmp(optarg, "5") == 0) 135 socksv = 5; /* SOCKS v.5 */ 136 else 137 errx(1, "unsupported proxy protocol"); 138 break; 139 case 'd': 140 dflag = 1; 141 break; | 134 switch (ch) { 135 case '4': 136 family = AF_INET; 137 break; 138 case '6': 139 family = AF_INET6; 140 break; 141 case 'U': --- 7 unchanged lines hidden (view full) --- 149 else if (strcmp(optarg, "5") == 0) 150 socksv = 5; /* SOCKS v.5 */ 151 else 152 errx(1, "unsupported proxy protocol"); 153 break; 154 case 'd': 155 dflag = 1; 156 break; |
157 case 'e': 158#ifdef IPSEC 159 ipsec_policy[ipsec_count++ % 2] = optarg; 160#else 161 errx(1, "IPsec support unavailable."); 162#endif 163 break; 164 case 'E': 165#ifdef IPSEC 166 ipsec_policy[0] = "in ipsec esp/transport//require"; 167 ipsec_policy[1] = "out ipsec esp/transport//require"; 168#else 169 errx(1, "IPsec support unavailable."); 170#endif 171 break; |
|
142 case 'h': 143 help(); 144 break; 145 case 'i': 146 iflag = (int)strtoul(optarg, &endp, 10); 147 if (iflag < 0 || *endp != '\0') 148 errx(1, "interval cannot be negative"); 149 break; 150 case 'k': 151 kflag = 1; 152 break; 153 case 'l': 154 lflag = 1; 155 break; 156 case 'n': 157 nflag = 1; 158 break; | 172 case 'h': 173 help(); 174 break; 175 case 'i': 176 iflag = (int)strtoul(optarg, &endp, 10); 177 if (iflag < 0 || *endp != '\0') 178 errx(1, "interval cannot be negative"); 179 break; 180 case 'k': 181 kflag = 1; 182 break; 183 case 'l': 184 lflag = 1; 185 break; 186 case 'n': 187 nflag = 1; 188 break; |
189 case 'o': 190 oflag = 1; 191 break; |
|
159 case 'p': 160 pflag = optarg; 161 break; 162 case 'r': 163 rflag = 1; 164 break; 165 case 's': 166 sflag = optarg; --- 293 unchanged lines hidden (view full) --- 460 if ((error = getaddrinfo(host, port, &hints, &res))) 461 errx(1, "getaddrinfo: %s", gai_strerror(error)); 462 463 res0 = res; 464 do { 465 if ((s = socket(res0->ai_family, res0->ai_socktype, 466 res0->ai_protocol)) < 0) 467 continue; | 192 case 'p': 193 pflag = optarg; 194 break; 195 case 'r': 196 rflag = 1; 197 break; 198 case 's': 199 sflag = optarg; --- 293 unchanged lines hidden (view full) --- 493 if ((error = getaddrinfo(host, port, &hints, &res))) 494 errx(1, "getaddrinfo: %s", gai_strerror(error)); 495 496 res0 = res; 497 do { 498 if ((s = socket(res0->ai_family, res0->ai_socktype, 499 res0->ai_protocol)) < 0) 500 continue; |
501#ifdef IPSEC 502 if (ipsec_policy[0] != NULL) 503 add_ipsec_policy(s, ipsec_policy[0]); 504 if (ipsec_policy[1] != NULL) 505 add_ipsec_policy(s, ipsec_policy[1]); 506#endif |
|
468 469 /* Bind to a local port or source address if specified. */ 470 if (sflag || pflag) { 471 struct addrinfo ahints, *ares; 472 473 if (!(sflag && pflag)) { 474 if (!sflag) 475 sflag = NULL; --- 69 unchanged lines hidden (view full) --- 545 do { 546 if ((s = socket(res0->ai_family, res0->ai_socktype, 547 res0->ai_protocol)) == 0) 548 continue; 549 550 ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x)); 551 if (ret == -1) 552 err(1, NULL); | 507 508 /* Bind to a local port or source address if specified. */ 509 if (sflag || pflag) { 510 struct addrinfo ahints, *ares; 511 512 if (!(sflag && pflag)) { 513 if (!sflag) 514 sflag = NULL; --- 69 unchanged lines hidden (view full) --- 584 do { 585 if ((s = socket(res0->ai_family, res0->ai_socktype, 586 res0->ai_protocol)) == 0) 587 continue; 588 589 ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x)); 590 if (ret == -1) 591 err(1, NULL); |
592#ifdef IPSEC 593 if (ipsec_policy[0] != NULL) 594 add_ipsec_policy(s, ipsec_policy[0]); 595 if (ipsec_policy[1] != NULL) 596 add_ipsec_policy(s, ipsec_policy[1]); 597#endif |
|
553 if (Sflag) { 554 ret = setsockopt(s, IPPROTO_TCP, TCP_MD5SIG, 555 &x, sizeof(x)); 556 if (ret == -1) 557 err(1, NULL); 558 } 559 if (Dflag) { 560 if (setsockopt(s, SOL_SOCKET, SO_DEBUG, --- 63 unchanged lines hidden (view full) --- 624 atelnet(nfd, buf, n); 625 if (atomicio((ssize_t (*)(int, void *, size_t))write, 626 lfd, buf, n) != n) 627 return; 628 } 629 } 630 631 if (!dflag && pfd[1].revents & POLLIN) { | 598 if (Sflag) { 599 ret = setsockopt(s, IPPROTO_TCP, TCP_MD5SIG, 600 &x, sizeof(x)); 601 if (ret == -1) 602 err(1, NULL); 603 } 604 if (Dflag) { 605 if (setsockopt(s, SOL_SOCKET, SO_DEBUG, --- 63 unchanged lines hidden (view full) --- 669 atelnet(nfd, buf, n); 670 if (atomicio((ssize_t (*)(int, void *, size_t))write, 671 lfd, buf, n) != n) 672 return; 673 } 674 } 675 676 if (!dflag && pfd[1].revents & POLLIN) { |
632 if ((n = read(wfd, buf, sizeof(buf))) < 0) | 677 if ((n = read(wfd, buf, sizeof(buf))) < 0 || 678 (oflag && n == 0)) { |
633 return; | 679 return; |
634 else if (n == 0) { | 680 } else if (n == 0) { |
635 shutdown(nfd, SHUT_WR); 636 pfd[1].fd = -1; 637 pfd[1].events = 0; 638 } else { 639 if (atomicio((ssize_t (*)(int, void *, size_t))write, 640 nfd, buf, n) != n) 641 return; 642 } --- 119 unchanged lines hidden (view full) --- 762} 763 764void 765help(void) 766{ 767 usage(0); 768 fprintf(stderr, "\tCommand Summary:\n\ 769 \t-4 Use IPv4\n\ | 681 shutdown(nfd, SHUT_WR); 682 pfd[1].fd = -1; 683 pfd[1].events = 0; 684 } else { 685 if (atomicio((ssize_t (*)(int, void *, size_t))write, 686 nfd, buf, n) != n) 687 return; 688 } --- 119 unchanged lines hidden (view full) --- 808} 809 810void 811help(void) 812{ 813 usage(0); 814 fprintf(stderr, "\tCommand Summary:\n\ 815 \t-4 Use IPv4\n\ |
770 \t-6 Use IPv6\n\ | 816 \t-6 Use IPv6\n"); 817#ifdef IPSEC 818 fprintf(stderr, "\ 819 \t-e policy Use specified IPsec policy\n\ 820 \t-E Use IPsec ESP\n"); 821#endif 822 fprintf(stderr, "\ |
771 \t-D Enable the debug socket option\n\ 772 \t-d Detach from stdin\n\ 773 \t-h This help text\n\ 774 \t-i secs\t Delay interval for lines sent, ports scanned\n\ 775 \t-k Keep inbound sockets open for multiple connects\n\ 776 \t-l Listen mode, for inbound connects\n\ 777 \t-n Suppress name/port resolutions\n\ 778 \t-p port\t Specify local port for remote connects\n\ --- 4 unchanged lines hidden (view full) --- 783 \t-U Use UNIX domain socket\n\ 784 \t-u UDP mode\n\ 785 \t-v Verbose\n\ 786 \t-w secs\t Timeout for connects and final net reads\n\ 787 \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\ 788 \t-x addr[:port]\tSpecify proxy address and port\n\ 789 \t-z Zero-I/O mode [used for scanning]\n\ 790 Port numbers can be individual or ranges: lo-hi [inclusive]\n"); | 823 \t-D Enable the debug socket option\n\ 824 \t-d Detach from stdin\n\ 825 \t-h This help text\n\ 826 \t-i secs\t Delay interval for lines sent, ports scanned\n\ 827 \t-k Keep inbound sockets open for multiple connects\n\ 828 \t-l Listen mode, for inbound connects\n\ 829 \t-n Suppress name/port resolutions\n\ 830 \t-p port\t Specify local port for remote connects\n\ --- 4 unchanged lines hidden (view full) --- 835 \t-U Use UNIX domain socket\n\ 836 \t-u UDP mode\n\ 837 \t-v Verbose\n\ 838 \t-w secs\t Timeout for connects and final net reads\n\ 839 \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\ 840 \t-x addr[:port]\tSpecify proxy address and port\n\ 841 \t-z Zero-I/O mode [used for scanning]\n\ 842 Port numbers can be individual or ranges: lo-hi [inclusive]\n"); |
843#ifdef IPSEC 844 fprintf(stderr, "See ipsec_set_policy(3) for -e argument format\n"); 845#endif |
|
791 exit(1); 792} 793 | 846 exit(1); 847} 848 |
849#ifdef IPSEC |
|
794void | 850void |
851add_ipsec_policy(int s, char *policy) 852{ 853 char *raw; 854 int e; 855 856 raw = ipsec_set_policy(policy, strlen(policy)); 857 if (raw == NULL) 858 errx(1, "ipsec_set_policy `%s': %s", policy, 859 ipsec_strerror()); 860 e = setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, raw, 861 ipsec_get_policylen(raw)); 862 if (e < 0) 863 err(1, "ipsec policy cannot be configured"); 864 free(raw); 865 if (vflag) 866 fprintf(stderr, "ipsec policy configured: `%s'\n", policy); 867 return; 868} 869#endif /* IPSEC */ 870 871void |
|
795usage(int ret) 796{ | 872usage(int ret) 873{ |
874 875#ifdef IPSEC 876 fprintf(stderr, "usage: nc [-46DEdhklnrStUuvz] [-e policy] [-i interval] [-p source_port]\n"); 877#else |
|
797 fprintf(stderr, "usage: nc [-46DdhklnrStUuvz] [-i interval] [-p source_port]\n"); | 878 fprintf(stderr, "usage: nc [-46DdhklnrStUuvz] [-i interval] [-p source_port]\n"); |
879#endif |
|
798 fprintf(stderr, "\t [-s source_ip_address] [-w timeout] [-X proxy_version]\n"); 799 fprintf(stderr, "\t [-x proxy_address[:port]] [hostname] [port[s]]\n"); 800 if (ret) 801 exit(1); 802} | 880 fprintf(stderr, "\t [-s source_ip_address] [-w timeout] [-X proxy_version]\n"); 881 fprintf(stderr, "\t [-x proxy_address[:port]] [hostname] [port[s]]\n"); 882 if (ret) 883 exit(1); 884} |