Annotation of src/usr.bin/nc/netcat.c, Revision 1.111
1.111 ! sthen 1: /* $OpenBSD: netcat.c,v 1.110 2013/03/12 02:57:37 deraadt Exp $ */
1.21 ericj 2: /*
3: * Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
1.7 deraadt 4: *
1.21 ericj 5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
1.7 deraadt 8: *
1.21 ericj 9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: * 3. The name of the author may not be used to endorse or promote products
15: * derived from this software without specific prior written permission.
1.7 deraadt 16: *
1.21 ericj 17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
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.
27: */
1.1 deraadt 28:
1.24 ericj 29: /*
30: * Re-written nc(1) for OpenBSD. Original implementation by
1.21 ericj 31: * *Hobbit* <hobbit@avian.org>.
32: */
1.1 deraadt 33:
1.7 deraadt 34: #include <sys/types.h>
1.21 ericj 35: #include <sys/socket.h>
1.7 deraadt 36: #include <sys/time.h>
1.42 ericj 37: #include <sys/un.h>
1.21 ericj 38:
1.7 deraadt 39: #include <netinet/in.h>
1.83 dtucker 40: #include <netinet/in_systm.h>
1.65 markus 41: #include <netinet/tcp.h>
1.83 dtucker 42: #include <netinet/ip.h>
1.21 ericj 43: #include <arpa/telnet.h>
1.29 smart 44:
1.11 ericj 45: #include <err.h>
1.7 deraadt 46: #include <errno.h>
1.21 ericj 47: #include <netdb.h>
48: #include <poll.h>
1.13 ericj 49: #include <stdarg.h>
1.21 ericj 50: #include <stdio.h>
1.1 deraadt 51: #include <stdlib.h>
1.21 ericj 52: #include <string.h>
1.5 art 53: #include <unistd.h>
1.42 ericj 54: #include <fcntl.h>
1.85 millert 55: #include <limits.h>
1.79 avsm 56: #include "atomicio.h"
1.51 vincent 57:
58: #ifndef SUN_LEN
59: #define SUN_LEN(su) \
60: (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
61: #endif
1.1 deraadt 62:
1.55 fgsch 63: #define PORT_MAX 65535
64: #define PORT_MAX_LEN 6
1.99 jeremy 65: #define UNIX_DG_TMP_SOCKET_SIZE 19
1.31 ericj 66:
1.21 ericj 67: /* Command Line Options */
1.68 tedu 68: int dflag; /* detached, no stdin */
1.88 ray 69: unsigned int iflag; /* Interval Flag */
1.21 ericj 70: int kflag; /* More than one connect */
71: int lflag; /* Bind to local port */
1.111 ! sthen 72: int Nflag; /* shutdown() network socket */
1.67 jmc 73: int nflag; /* Don't do name look up */
1.86 djm 74: char *Pflag; /* Proxy username */
1.21 ericj 75: char *pflag; /* Localport flag */
76: int rflag; /* Random ports flag */
77: char *sflag; /* Source Address */
78: int tflag; /* Telnet Emulation */
79: int uflag; /* UDP - Default to TCP */
80: int vflag; /* Verbosity */
1.34 jakob 81: int xflag; /* Socks proxy */
1.21 ericj 82: int zflag; /* Port Scan Flag */
1.73 markus 83: int Dflag; /* sodebug */
1.90 djm 84: int Iflag; /* TCP receive buffer size */
85: int Oflag; /* TCP send buffer size */
1.65 markus 86: int Sflag; /* TCP MD5 signature option */
1.83 dtucker 87: int Tflag = -1; /* IP Type of Service */
1.98 guenther 88: u_int rtableid;
1.21 ericj 89:
1.49 hugh 90: int timeout = -1;
1.21 ericj 91: int family = AF_UNSPEC;
1.63 miod 92: char *portlist[PORT_MAX+1];
1.99 jeremy 93: char *unix_dg_tmp_socket;
1.21 ericj 94:
1.40 millert 95: void atelnet(int, unsigned char *, unsigned int);
96: void build_ports(char *);
97: void help(void);
98: int local_listen(char *, char *, struct addrinfo);
99: void readwrite(int);
1.77 otto 100: int remote_connect(const char *, const char *, struct addrinfo);
1.103 fgsch 101: int timeout_connect(int, const struct sockaddr *, socklen_t);
1.86 djm 102: int socks_connect(const char *, const char *, struct addrinfo,
103: const char *, const char *, struct addrinfo, int, const char *);
1.40 millert 104: int udptest(int);
1.99 jeremy 105: int unix_bind(char *);
1.42 ericj 106: int unix_connect(char *);
107: int unix_listen(char *);
1.84 dtucker 108: void set_common_sockopts(int);
1.102 haesbaer 109: int map_tos(char *, int *);
1.108 haesbaer 110: void report_connect(const struct sockaddr *, socklen_t);
1.40 millert 111: void usage(int);
1.1 deraadt 112:
1.21 ericj 113: int
1.37 jakob 114: main(int argc, char *argv[])
1.21 ericj 115: {
1.46 markus 116: int ch, s, ret, socksv;
1.88 ray 117: char *host, *uport;
1.21 ericj 118: struct addrinfo hints;
1.29 smart 119: struct servent *sv;
1.21 ericj 120: socklen_t len;
1.76 hshoexer 121: struct sockaddr_storage cliaddr;
1.34 jakob 122: char *proxy;
1.88 ray 123: const char *errstr, *proxyhost = "", *proxyport = NULL;
1.34 jakob 124: struct addrinfo proxyhints;
1.99 jeremy 125: char unix_dg_tmp_socket_buf[UNIX_DG_TMP_SOCKET_SIZE];
1.11 ericj 126:
1.29 smart 127: ret = 1;
128: s = 0;
1.46 markus 129: socksv = 5;
1.29 smart 130: host = NULL;
131: uport = NULL;
132: sv = NULL;
133:
1.80 mcbride 134: while ((ch = getopt(argc, argv,
1.111 ! sthen 135: "46DdhI:i:klNnO:P:p:rSs:tT:UuV:vw:X:x:z")) != -1) {
1.21 ericj 136: switch (ch) {
137: case '4':
138: family = AF_INET;
139: break;
140: case '6':
141: family = AF_INET6;
142: break;
1.42 ericj 143: case 'U':
144: family = AF_UNIX;
145: break;
1.46 markus 146: case 'X':
1.75 djm 147: if (strcasecmp(optarg, "connect") == 0)
148: socksv = -1; /* HTTP proxy CONNECT */
149: else if (strcmp(optarg, "4") == 0)
150: socksv = 4; /* SOCKS v.4 */
151: else if (strcmp(optarg, "5") == 0)
152: socksv = 5; /* SOCKS v.5 */
153: else
154: errx(1, "unsupported proxy protocol");
1.46 markus 155: break;
1.68 tedu 156: case 'd':
157: dflag = 1;
158: break;
1.21 ericj 159: case 'h':
160: help();
161: break;
162: case 'i':
1.88 ray 163: iflag = strtonum(optarg, 0, UINT_MAX, &errstr);
164: if (errstr)
165: errx(1, "interval %s: %s", errstr, optarg);
1.21 ericj 166: break;
167: case 'k':
168: kflag = 1;
169: break;
170: case 'l':
171: lflag = 1;
172: break;
1.111 ! sthen 173: case 'N':
! 174: Nflag = 1;
! 175: break;
1.21 ericj 176: case 'n':
177: nflag = 1;
178: break;
1.86 djm 179: case 'P':
180: Pflag = optarg;
181: break;
1.21 ericj 182: case 'p':
183: pflag = optarg;
184: break;
185: case 'r':
186: rflag = 1;
187: break;
188: case 's':
189: sflag = optarg;
190: break;
191: case 't':
192: tflag = 1;
193: break;
194: case 'u':
195: uflag = 1;
196: break;
1.93 claudio 197: case 'V':
1.98 guenther 198: rtableid = (unsigned int)strtonum(optarg, 0,
1.93 claudio 199: RT_TABLEID_MAX, &errstr);
200: if (errstr)
1.98 guenther 201: errx(1, "rtable %s: %s", errstr, optarg);
1.93 claudio 202: break;
1.21 ericj 203: case 'v':
204: vflag = 1;
205: break;
1.70 deraadt 206: case 'w':
1.88 ray 207: timeout = strtonum(optarg, 0, INT_MAX / 1000, &errstr);
208: if (errstr)
209: errx(1, "timeout %s: %s", errstr, optarg);
1.49 hugh 210: timeout *= 1000;
1.21 ericj 211: break;
1.34 jakob 212: case 'x':
213: xflag = 1;
1.64 deraadt 214: if ((proxy = strdup(optarg)) == NULL)
215: err(1, NULL);
1.34 jakob 216: break;
1.21 ericj 217: case 'z':
218: zflag = 1;
219: break;
1.73 markus 220: case 'D':
221: Dflag = 1;
222: break;
1.90 djm 223: case 'I':
224: Iflag = strtonum(optarg, 1, 65536 << 14, &errstr);
225: if (errstr != NULL)
226: errx(1, "TCP receive window %s: %s",
227: errstr, optarg);
228: break;
229: case 'O':
230: Oflag = strtonum(optarg, 1, 65536 << 14, &errstr);
231: if (errstr != NULL)
232: errx(1, "TCP send window %s: %s",
233: errstr, optarg);
234: break;
1.65 markus 235: case 'S':
236: Sflag = 1;
237: break;
1.83 dtucker 238: case 'T':
1.102 haesbaer 239: errstr = NULL;
240: errno = 0;
241: if (map_tos(optarg, &Tflag))
242: break;
243: if (strlen(optarg) > 1 && optarg[0] == '0' &&
244: optarg[1] == 'x')
245: Tflag = (int)strtol(optarg, NULL, 16);
246: else
247: Tflag = (int)strtonum(optarg, 0, 255,
248: &errstr);
249: if (Tflag < 0 || Tflag > 255 || errstr || errno)
250: errx(1, "illegal tos value %s", optarg);
1.83 dtucker 251: break;
1.21 ericj 252: default:
253: usage(1);
254: }
255: }
256: argc -= optind;
257: argv += optind;
1.11 ericj 258:
1.21 ericj 259: /* Cruft to make sure options are clean, and used properly. */
1.42 ericj 260: if (argv[0] && !argv[1] && family == AF_UNIX) {
261: host = argv[0];
262: uport = NULL;
263: } else if (argv[0] && !argv[1]) {
1.21 ericj 264: if (!lflag)
265: usage(1);
266: uport = argv[0];
267: host = NULL;
268: } else if (argv[0] && argv[1]) {
269: host = argv[0];
270: uport = argv[1];
271: } else
272: usage(1);
1.1 deraadt 273:
1.21 ericj 274: if (lflag && sflag)
275: errx(1, "cannot use -s and -l");
276: if (lflag && pflag)
277: errx(1, "cannot use -p and -l");
278: if (lflag && zflag)
1.32 ericj 279: errx(1, "cannot use -z and -l");
1.21 ericj 280: if (!lflag && kflag)
1.32 ericj 281: errx(1, "must use -l with -k");
1.21 ericj 282:
1.99 jeremy 283: /* Get name of temporary socket for unix datagram client */
284: if ((family == AF_UNIX) && uflag && !lflag) {
285: if (sflag) {
286: unix_dg_tmp_socket = sflag;
287: } else {
288: strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX",
289: UNIX_DG_TMP_SOCKET_SIZE);
290: if (mktemp(unix_dg_tmp_socket_buf) == NULL)
291: err(1, "mktemp");
292: unix_dg_tmp_socket = unix_dg_tmp_socket_buf;
293: }
294: }
295:
1.67 jmc 296: /* Initialize addrinfo structure. */
1.42 ericj 297: if (family != AF_UNIX) {
298: memset(&hints, 0, sizeof(struct addrinfo));
299: hints.ai_family = family;
300: hints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
301: hints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
302: if (nflag)
303: hints.ai_flags |= AI_NUMERICHOST;
304: }
1.1 deraadt 305:
1.34 jakob 306: if (xflag) {
307: if (uflag)
308: errx(1, "no proxy support for UDP mode");
309:
310: if (lflag)
311: errx(1, "no proxy support for listen");
312:
1.42 ericj 313: if (family == AF_UNIX)
314: errx(1, "no proxy support for unix sockets");
315:
1.34 jakob 316: /* XXX IPv6 transport to proxy would probably work */
317: if (family == AF_INET6)
318: errx(1, "no proxy support for IPv6");
319:
320: if (sflag)
321: errx(1, "no proxy support for local source address");
322:
323: proxyhost = strsep(&proxy, ":");
324: proxyport = proxy;
325:
326: memset(&proxyhints, 0, sizeof(struct addrinfo));
327: proxyhints.ai_family = family;
328: proxyhints.ai_socktype = SOCK_STREAM;
329: proxyhints.ai_protocol = IPPROTO_TCP;
330: if (nflag)
331: proxyhints.ai_flags |= AI_NUMERICHOST;
332: }
333:
1.21 ericj 334: if (lflag) {
335: int connfd;
1.27 ericj 336: ret = 0;
1.1 deraadt 337:
1.99 jeremy 338: if (family == AF_UNIX) {
339: if (uflag)
340: s = unix_bind(host);
341: else
342: s = unix_listen(host);
343: }
1.42 ericj 344:
1.67 jmc 345: /* Allow only one connection at a time, but stay alive. */
1.21 ericj 346: for (;;) {
1.42 ericj 347: if (family != AF_UNIX)
348: s = local_listen(host, uport, hints);
349: if (s < 0)
1.30 smart 350: err(1, NULL);
1.21 ericj 351: /*
1.109 haesbaer 352: * For UDP and -k, don't connect the socket, let it
353: * receive datagrams from multiple socket pairs.
1.21 ericj 354: */
1.109 haesbaer 355: if (uflag && kflag)
356: readwrite(s);
357: /*
358: * For UDP and not -k, we will use recvfrom() initially
359: * to wait for a caller, then use the regular functions
360: * to talk to the caller.
361: */
362: else if (uflag && !kflag) {
1.80 mcbride 363: int rv, plen;
1.97 nicm 364: char buf[16384];
1.21 ericj 365: struct sockaddr_storage z;
366:
367: len = sizeof(z);
1.106 dlg 368: plen = 2048;
1.80 mcbride 369: rv = recvfrom(s, buf, plen, MSG_PEEK,
1.37 jakob 370: (struct sockaddr *)&z, &len);
1.23 ericj 371: if (rv < 0)
1.57 stevesk 372: err(1, "recvfrom");
1.21 ericj 373:
1.37 jakob 374: rv = connect(s, (struct sockaddr *)&z, len);
1.23 ericj 375: if (rv < 0)
1.57 stevesk 376: err(1, "connect");
1.1 deraadt 377:
1.108 haesbaer 378: if (vflag)
379: report_connect((struct sockaddr *)&z, len);
380:
1.99 jeremy 381: readwrite(s);
1.21 ericj 382: } else {
1.78 otto 383: len = sizeof(cliaddr);
1.21 ericj 384: connfd = accept(s, (struct sockaddr *)&cliaddr,
1.37 jakob 385: &len);
1.110 deraadt 386: if (connfd == -1) {
387: /* For now, all errnos are fatal */
388: err(1, "accept");
389: }
1.108 haesbaer 390: if (vflag)
391: report_connect((struct sockaddr *)&cliaddr, len);
392:
1.99 jeremy 393: readwrite(connfd);
394: close(connfd);
1.21 ericj 395: }
1.1 deraadt 396:
1.42 ericj 397: if (family != AF_UNIX)
398: close(s);
1.99 jeremy 399: else if (uflag) {
400: if (connect(s, NULL, 0) < 0)
401: err(1, "connect");
402: }
1.27 ericj 403:
1.21 ericj 404: if (!kflag)
405: break;
1.11 ericj 406: }
1.42 ericj 407: } else if (family == AF_UNIX) {
408: ret = 0;
409:
410: if ((s = unix_connect(host)) > 0 && !zflag) {
411: readwrite(s);
412: close(s);
413: } else
414: ret = 1;
415:
1.99 jeremy 416: if (uflag)
417: unlink(unix_dg_tmp_socket);
1.42 ericj 418: exit(ret);
419:
1.21 ericj 420: } else {
421: int i = 0;
1.6 deraadt 422:
1.67 jmc 423: /* Construct the portlist[] array. */
1.21 ericj 424: build_ports(uport);
1.1 deraadt 425:
1.67 jmc 426: /* Cycle through portlist, connecting to each port. */
1.21 ericj 427: for (i = 0; portlist[i] != NULL; i++) {
428: if (s)
429: close(s);
1.34 jakob 430:
431: if (xflag)
432: s = socks_connect(host, portlist[i], hints,
1.86 djm 433: proxyhost, proxyport, proxyhints, socksv,
434: Pflag);
1.34 jakob 435: else
436: s = remote_connect(host, portlist[i], hints);
437:
438: if (s < 0)
1.21 ericj 439: continue;
1.1 deraadt 440:
1.21 ericj 441: ret = 0;
442: if (vflag || zflag) {
1.67 jmc 443: /* For UDP, make sure we are connected. */
1.21 ericj 444: if (uflag) {
1.50 vincent 445: if (udptest(s) == -1) {
1.21 ericj 446: ret = 1;
447: continue;
448: }
449: }
1.1 deraadt 450:
1.67 jmc 451: /* Don't look up port if -n. */
1.21 ericj 452: if (nflag)
453: sv = NULL;
454: else {
455: sv = getservbyport(
1.37 jakob 456: ntohs(atoi(portlist[i])),
457: uflag ? "udp" : "tcp");
1.21 ericj 458: }
1.50 vincent 459:
1.94 mpf 460: fprintf(stderr,
461: "Connection to %s %s port [%s/%s] "
462: "succeeded!\n", host, portlist[i],
463: uflag ? "udp" : "tcp",
1.37 jakob 464: sv ? sv->s_name : "*");
1.21 ericj 465: }
466: if (!zflag)
467: readwrite(s);
1.7 deraadt 468: }
1.11 ericj 469: }
1.1 deraadt 470:
1.21 ericj 471: if (s)
472: close(s);
473:
474: exit(ret);
1.7 deraadt 475: }
1.1 deraadt 476:
1.11 ericj 477: /*
1.99 jeremy 478: * unix_bind()
479: * Returns a unix socket bound to the given path
1.42 ericj 480: */
481: int
1.99 jeremy 482: unix_bind(char *path)
1.42 ericj 483: {
484: struct sockaddr_un sun;
485: int s;
486:
1.99 jeremy 487: /* Create unix domain socket. */
488: if ((s = socket(AF_UNIX, uflag ? SOCK_DGRAM : SOCK_STREAM,
489: 0)) < 0)
1.50 vincent 490: return (-1);
1.42 ericj 491:
492: memset(&sun, 0, sizeof(struct sockaddr_un));
493: sun.sun_family = AF_UNIX;
1.60 avsm 494:
495: if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
496: sizeof(sun.sun_path)) {
497: close(s);
498: errno = ENAMETOOLONG;
499: return (-1);
500: }
1.99 jeremy 501:
502: if (bind(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
1.50 vincent 503: close(s);
504: return (-1);
1.42 ericj 505: }
506: return (s);
507: }
508:
509: /*
1.99 jeremy 510: * unix_connect()
511: * Returns a socket connected to a local unix socket. Returns -1 on failure.
1.42 ericj 512: */
513: int
1.99 jeremy 514: unix_connect(char *path)
1.42 ericj 515: {
516: struct sockaddr_un sun;
517: int s;
518:
1.99 jeremy 519: if (uflag) {
520: if ((s = unix_bind(unix_dg_tmp_socket)) < 0)
521: return (-1);
522: } else {
523: if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
524: return (-1);
525: }
526: (void)fcntl(s, F_SETFD, 1);
1.42 ericj 527:
1.60 avsm 528: memset(&sun, 0, sizeof(struct sockaddr_un));
1.42 ericj 529: sun.sun_family = AF_UNIX;
1.60 avsm 530:
531: if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
532: sizeof(sun.sun_path)) {
533: close(s);
534: errno = ENAMETOOLONG;
535: return (-1);
536: }
1.99 jeremy 537: if (connect(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
1.42 ericj 538: close(s);
539: return (-1);
540: }
1.99 jeremy 541: return (s);
542:
543: }
544:
545: /*
546: * unix_listen()
547: * Create a unix domain socket, and listen on it.
548: */
549: int
550: unix_listen(char *path)
551: {
552: int s;
553: if ((s = unix_bind(path)) < 0)
554: return (-1);
1.42 ericj 555:
556: if (listen(s, 5) < 0) {
557: close(s);
558: return (-1);
559: }
560: return (s);
561: }
562:
563: /*
1.21 ericj 564: * remote_connect()
1.67 jmc 565: * Returns a socket connected to a remote host. Properly binds to a local
566: * port or source address if needed. Returns -1 on failure.
1.11 ericj 567: */
1.21 ericj 568: int
1.77 otto 569: remote_connect(const char *host, const char *port, struct addrinfo hints)
1.21 ericj 570: {
571: struct addrinfo *res, *res0;
1.91 markus 572: int s, error, on = 1;
1.21 ericj 573:
574: if ((error = getaddrinfo(host, port, &hints, &res)))
1.56 stevesk 575: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.21 ericj 576:
577: res0 = res;
578: do {
579: if ((s = socket(res0->ai_family, res0->ai_socktype,
1.37 jakob 580: res0->ai_protocol)) < 0)
1.21 ericj 581: continue;
582:
1.98 guenther 583: if (rtableid) {
1.101 mikeb 584: if (setsockopt(s, SOL_SOCKET, SO_RTABLE, &rtableid,
1.98 guenther 585: sizeof(rtableid)) == -1)
586: err(1, "setsockopt SO_RTABLE");
1.93 claudio 587: }
588:
1.67 jmc 589: /* Bind to a local port or source address if specified. */
1.21 ericj 590: if (sflag || pflag) {
591: struct addrinfo ahints, *ares;
1.6 deraadt 592:
1.91 markus 593: /* try SO_BINDANY, but don't insist */
594: setsockopt(s, SOL_SOCKET, SO_BINDANY, &on, sizeof(on));
1.21 ericj 595: memset(&ahints, 0, sizeof(struct addrinfo));
596: ahints.ai_family = res0->ai_family;
597: ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
598: ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
1.25 ericj 599: ahints.ai_flags = AI_PASSIVE;
1.38 jakob 600: if ((error = getaddrinfo(sflag, pflag, &ahints, &ares)))
1.56 stevesk 601: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.21 ericj 602:
603: if (bind(s, (struct sockaddr *)ares->ai_addr,
1.62 millert 604: ares->ai_addrlen) < 0)
1.21 ericj 605: errx(1, "bind failed: %s", strerror(errno));
606: freeaddrinfo(ares);
1.6 deraadt 607: }
1.81 marius 608:
609: set_common_sockopts(s);
1.6 deraadt 610:
1.103 fgsch 611: if (timeout_connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
1.6 deraadt 612: break;
1.71 mcbride 613: else if (vflag)
614: warn("connect to %s port %s (%s) failed", host, port,
615: uflag ? "udp" : "tcp");
1.34 jakob 616:
1.21 ericj 617: close(s);
618: s = -1;
619: } while ((res0 = res0->ai_next) != NULL);
620:
621: freeaddrinfo(res);
1.1 deraadt 622:
1.21 ericj 623: return (s);
1.103 fgsch 624: }
625:
626: int
627: timeout_connect(int s, const struct sockaddr *name, socklen_t namelen)
628: {
629: struct pollfd pfd;
630: socklen_t optlen;
631: int flags, optval;
632: int ret;
633:
634: if (timeout != -1) {
635: flags = fcntl(s, F_GETFL, 0);
636: if (fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1)
637: err(1, "set non-blocking mode");
638: }
639:
640: if ((ret = connect(s, name, namelen)) != 0 && errno == EINPROGRESS) {
641: pfd.fd = s;
642: pfd.events = POLLOUT;
643: if ((ret = poll(&pfd, 1, timeout)) == 1) {
644: optlen = sizeof(optval);
645: if ((ret = getsockopt(s, SOL_SOCKET, SO_ERROR,
646: &optval, &optlen)) == 0) {
647: errno = optval;
648: ret = optval == 0 ? 0 : -1;
649: }
650: } else if (ret == 0) {
651: errno = ETIMEDOUT;
652: ret = -1;
653: } else
654: err(1, "poll failed");
655: }
656:
657: if (timeout != -1 && fcntl(s, F_SETFL, flags) == -1)
658: err(1, "restoring flags");
659:
660: return (ret);
1.7 deraadt 661: }
1.1 deraadt 662:
1.11 ericj 663: /*
1.21 ericj 664: * local_listen()
1.67 jmc 665: * Returns a socket listening on a local port, binds to specified source
666: * address. Returns -1 on failure.
1.11 ericj 667: */
1.21 ericj 668: int
1.37 jakob 669: local_listen(char *host, char *port, struct addrinfo hints)
1.21 ericj 670: {
671: struct addrinfo *res, *res0;
672: int s, ret, x = 1;
673: int error;
1.6 deraadt 674:
1.67 jmc 675: /* Allow nodename to be null. */
1.21 ericj 676: hints.ai_flags |= AI_PASSIVE;
1.7 deraadt 677:
1.21 ericj 678: /*
679: * In the case of binding to a wildcard address
680: * default to binding to an ipv4 address.
681: */
682: if (host == NULL && hints.ai_family == AF_UNSPEC)
683: hints.ai_family = AF_INET;
1.1 deraadt 684:
1.21 ericj 685: if ((error = getaddrinfo(host, port, &hints, &res)))
1.70 deraadt 686: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.14 ericj 687:
1.21 ericj 688: res0 = res;
689: do {
690: if ((s = socket(res0->ai_family, res0->ai_socktype,
1.82 marius 691: res0->ai_protocol)) < 0)
1.21 ericj 692: continue;
1.1 deraadt 693:
1.98 guenther 694: if (rtableid) {
695: if (setsockopt(s, IPPROTO_IP, SO_RTABLE, &rtableid,
696: sizeof(rtableid)) == -1)
697: err(1, "setsockopt SO_RTABLE");
1.93 claudio 698: }
699:
1.21 ericj 700: ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
701: if (ret == -1)
1.30 smart 702: err(1, NULL);
1.81 marius 703:
704: set_common_sockopts(s);
1.1 deraadt 705:
1.21 ericj 706: if (bind(s, (struct sockaddr *)res0->ai_addr,
1.37 jakob 707: res0->ai_addrlen) == 0)
1.21 ericj 708: break;
1.1 deraadt 709:
1.21 ericj 710: close(s);
711: s = -1;
712: } while ((res0 = res0->ai_next) != NULL);
1.1 deraadt 713:
1.47 ericj 714: if (!uflag && s != -1) {
1.21 ericj 715: if (listen(s, 1) < 0)
1.57 stevesk 716: err(1, "listen");
1.12 ericj 717: }
1.1 deraadt 718:
1.21 ericj 719: freeaddrinfo(res);
1.1 deraadt 720:
1.21 ericj 721: return (s);
1.7 deraadt 722: }
723:
1.11 ericj 724: /*
1.21 ericj 725: * readwrite()
726: * Loop that polls on the network file descriptor and stdin.
1.11 ericj 727: */
1.21 ericj 728: void
1.37 jakob 729: readwrite(int nfd)
1.6 deraadt 730: {
1.52 vincent 731: struct pollfd pfd[2];
1.97 nicm 732: unsigned char buf[16384];
1.79 avsm 733: int n, wfd = fileno(stdin);
1.21 ericj 734: int lfd = fileno(stdout);
1.80 mcbride 735: int plen;
736:
1.106 dlg 737: plen = 2048;
1.21 ericj 738:
739: /* Setup Network FD */
740: pfd[0].fd = nfd;
741: pfd[0].events = POLLIN;
742:
1.67 jmc 743: /* Set up STDIN FD. */
1.21 ericj 744: pfd[1].fd = wfd;
745: pfd[1].events = POLLIN;
746:
1.54 aaron 747: while (pfd[0].fd != -1) {
1.21 ericj 748: if (iflag)
749: sleep(iflag);
750:
1.68 tedu 751: if ((n = poll(pfd, 2 - dflag, timeout)) < 0) {
1.21 ericj 752: close(nfd);
1.52 vincent 753: err(1, "Polling Error");
1.21 ericj 754: }
1.49 hugh 755:
756: if (n == 0)
757: return;
1.21 ericj 758:
759: if (pfd[0].revents & POLLIN) {
1.80 mcbride 760: if ((n = read(nfd, buf, plen)) < 0)
1.21 ericj 761: return;
1.52 vincent 762: else if (n == 0) {
763: shutdown(nfd, SHUT_RD);
764: pfd[0].fd = -1;
765: pfd[0].events = 0;
1.21 ericj 766: } else {
767: if (tflag)
768: atelnet(nfd, buf, n);
1.79 avsm 769: if (atomicio(vwrite, lfd, buf, n) != n)
1.21 ericj 770: return;
1.6 deraadt 771: }
1.21 ericj 772: }
773:
1.68 tedu 774: if (!dflag && pfd[1].revents & POLLIN) {
1.80 mcbride 775: if ((n = read(wfd, buf, plen)) < 0)
1.21 ericj 776: return;
1.52 vincent 777: else if (n == 0) {
1.111 ! sthen 778: if (Nflag)
! 779: shutdown(nfd, SHUT_WR);
1.52 vincent 780: pfd[1].fd = -1;
781: pfd[1].events = 0;
782: } else {
1.79 avsm 783: if (atomicio(vwrite, nfd, buf, n) != n)
1.21 ericj 784: return;
1.50 vincent 785: }
1.21 ericj 786: }
1.11 ericj 787: }
1.7 deraadt 788: }
1.50 vincent 789:
1.67 jmc 790: /* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */
1.21 ericj 791: void
1.37 jakob 792: atelnet(int nfd, unsigned char *buf, unsigned int size)
1.6 deraadt 793: {
1.24 ericj 794: unsigned char *p, *end;
795: unsigned char obuf[4];
796:
1.95 nicm 797: if (size < 3)
798: return;
799: end = buf + size - 2;
1.24 ericj 800:
801: for (p = buf; p < end; p++) {
1.21 ericj 802: if (*p != IAC)
1.95 nicm 803: continue;
1.24 ericj 804:
1.25 ericj 805: obuf[0] = IAC;
1.24 ericj 806: p++;
1.50 vincent 807: if ((*p == WILL) || (*p == WONT))
1.24 ericj 808: obuf[1] = DONT;
1.95 nicm 809: else if ((*p == DO) || (*p == DONT))
1.24 ericj 810: obuf[1] = WONT;
1.95 nicm 811: else
812: continue;
813:
814: p++;
815: obuf[2] = *p;
816: if (atomicio(vwrite, nfd, obuf, 3) != 3)
817: warn("Write Error!");
1.11 ericj 818: }
1.7 deraadt 819: }
820:
1.11 ericj 821: /*
1.21 ericj 822: * build_ports()
1.105 lum 823: * Build an array of ports in portlist[], listing each port
1.67 jmc 824: * that we should try to connect to.
1.11 ericj 825: */
1.21 ericj 826: void
1.37 jakob 827: build_ports(char *p)
1.6 deraadt 828: {
1.88 ray 829: const char *errstr;
830: char *n;
1.21 ericj 831: int hi, lo, cp;
832: int x = 0;
833:
834: if ((n = strchr(p, '-')) != NULL) {
835: *n = '\0';
836: n++;
837:
1.67 jmc 838: /* Make sure the ports are in order: lowest->highest. */
1.88 ray 839: hi = strtonum(n, 1, PORT_MAX, &errstr);
840: if (errstr)
841: errx(1, "port number %s: %s", errstr, n);
842: lo = strtonum(p, 1, PORT_MAX, &errstr);
843: if (errstr)
844: errx(1, "port number %s: %s", errstr, p);
1.21 ericj 845:
846: if (lo > hi) {
847: cp = hi;
848: hi = lo;
849: lo = cp;
850: }
851:
1.67 jmc 852: /* Load ports sequentially. */
1.21 ericj 853: for (cp = lo; cp <= hi; cp++) {
1.55 fgsch 854: portlist[x] = calloc(1, PORT_MAX_LEN);
855: if (portlist[x] == NULL)
856: err(1, NULL);
857: snprintf(portlist[x], PORT_MAX_LEN, "%d", cp);
1.21 ericj 858: x++;
859: }
860:
1.67 jmc 861: /* Randomly swap ports. */
1.21 ericj 862: if (rflag) {
863: int y;
864: char *c;
865:
866: for (x = 0; x <= (hi - lo); x++) {
867: y = (arc4random() & 0xFFFF) % (hi - lo);
868: c = portlist[x];
869: portlist[x] = portlist[y];
870: portlist[y] = c;
1.6 deraadt 871: }
1.11 ericj 872: }
1.21 ericj 873: } else {
1.88 ray 874: hi = strtonum(p, 1, PORT_MAX, &errstr);
875: if (errstr)
876: errx(1, "port number %s: %s", errstr, p);
1.96 nicm 877: portlist[0] = strdup(p);
1.55 fgsch 878: if (portlist[0] == NULL)
879: err(1, NULL);
1.11 ericj 880: }
1.13 ericj 881: }
882:
883: /*
1.21 ericj 884: * udptest()
885: * Do a few writes to see if the UDP port is there.
1.105 lum 886: * Fails once PF state table is full.
1.13 ericj 887: */
1.21 ericj 888: int
1.37 jakob 889: udptest(int s)
1.13 ericj 890: {
1.74 deraadt 891: int i, ret;
1.13 ericj 892:
1.52 vincent 893: for (i = 0; i <= 3; i++) {
1.74 deraadt 894: if (write(s, "X", 1) == 1)
1.21 ericj 895: ret = 1;
1.14 ericj 896: else
1.21 ericj 897: ret = -1;
1.14 ericj 898: }
1.21 ericj 899: return (ret);
1.81 marius 900: }
901:
1.84 dtucker 902: void
1.81 marius 903: set_common_sockopts(int s)
904: {
905: int x = 1;
906:
907: if (Sflag) {
908: if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
909: &x, sizeof(x)) == -1)
910: err(1, NULL);
911: }
912: if (Dflag) {
913: if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
914: &x, sizeof(x)) == -1)
915: err(1, NULL);
916: }
1.83 dtucker 917: if (Tflag != -1) {
918: if (setsockopt(s, IPPROTO_IP, IP_TOS,
919: &Tflag, sizeof(Tflag)) == -1)
920: err(1, "set IP ToS");
921: }
1.90 djm 922: if (Iflag) {
923: if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
924: &Iflag, sizeof(Iflag)) == -1)
925: err(1, "set TCP receive buffer size");
926: }
927: if (Oflag) {
928: if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
929: &Oflag, sizeof(Oflag)) == -1)
930: err(1, "set TCP send buffer size");
931: }
1.83 dtucker 932: }
933:
934: int
1.102 haesbaer 935: map_tos(char *s, int *val)
1.83 dtucker 936: {
1.102 haesbaer 937: /* DiffServ Codepoints and other TOS mappings */
938: const struct toskeywords {
939: const char *keyword;
940: int val;
941: } *t, toskeywords[] = {
942: { "af11", IPTOS_DSCP_AF11 },
943: { "af12", IPTOS_DSCP_AF12 },
944: { "af13", IPTOS_DSCP_AF13 },
945: { "af21", IPTOS_DSCP_AF21 },
946: { "af22", IPTOS_DSCP_AF22 },
947: { "af23", IPTOS_DSCP_AF23 },
948: { "af31", IPTOS_DSCP_AF31 },
949: { "af32", IPTOS_DSCP_AF32 },
950: { "af33", IPTOS_DSCP_AF33 },
951: { "af41", IPTOS_DSCP_AF41 },
952: { "af42", IPTOS_DSCP_AF42 },
953: { "af43", IPTOS_DSCP_AF43 },
954: { "critical", IPTOS_PREC_CRITIC_ECP },
955: { "cs0", IPTOS_DSCP_CS0 },
956: { "cs1", IPTOS_DSCP_CS1 },
957: { "cs2", IPTOS_DSCP_CS2 },
958: { "cs3", IPTOS_DSCP_CS3 },
959: { "cs4", IPTOS_DSCP_CS4 },
960: { "cs5", IPTOS_DSCP_CS5 },
961: { "cs6", IPTOS_DSCP_CS6 },
962: { "cs7", IPTOS_DSCP_CS7 },
963: { "ef", IPTOS_DSCP_EF },
964: { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
965: { "lowdelay", IPTOS_LOWDELAY },
966: { "netcontrol", IPTOS_PREC_NETCONTROL },
967: { "reliability", IPTOS_RELIABILITY },
968: { "throughput", IPTOS_THROUGHPUT },
969: { NULL, -1 },
970: };
971:
972: for (t = toskeywords; t->keyword != NULL; t++) {
973: if (strcmp(s, t->keyword) == 0) {
974: *val = t->val;
975: return (1);
976: }
977: }
1.83 dtucker 978:
1.102 haesbaer 979: return (0);
1.108 haesbaer 980: }
981:
982: void
983: report_connect(const struct sockaddr *sa, socklen_t salen)
984: {
985: char remote_host[NI_MAXHOST];
986: char remote_port[NI_MAXSERV];
987: int herr;
988: int flags = NI_NUMERICSERV;
989:
990: if (nflag)
991: flags |= NI_NUMERICHOST;
992:
993: if ((herr = getnameinfo(sa, salen,
994: remote_host, sizeof(remote_host),
995: remote_port, sizeof(remote_port),
996: flags)) != 0) {
997: if (herr == EAI_SYSTEM)
998: err(1, "getnameinfo");
999: else
1000: errx(1, "getnameinfo: %s", gai_strerror(herr));
1001: }
1002:
1003: fprintf(stderr,
1004: "Connection from %s %s "
1005: "received!\n", remote_host, remote_port);
1.7 deraadt 1006: }
1.1 deraadt 1007:
1.11 ericj 1008: void
1.58 deraadt 1009: help(void)
1.1 deraadt 1010: {
1.21 ericj 1011: usage(0);
1012: fprintf(stderr, "\tCommand Summary:\n\
1013: \t-4 Use IPv4\n\
1014: \t-6 Use IPv6\n\
1.73 markus 1015: \t-D Enable the debug socket option\n\
1.69 tedu 1016: \t-d Detach from stdin\n\
1.21 ericj 1017: \t-h This help text\n\
1.90 djm 1018: \t-I length TCP receive buffer length\n\
1.21 ericj 1019: \t-i secs\t Delay interval for lines sent, ports scanned\n\
1020: \t-k Keep inbound sockets open for multiple connects\n\
1021: \t-l Listen mode, for inbound connects\n\
1.111 ! sthen 1022: \t-N Shutdown the network socket after EOF on stdin\n\
1.22 jasoni 1023: \t-n Suppress name/port resolutions\n\
1.90 djm 1024: \t-O length TCP send buffer length\n\
1.86 djm 1025: \t-P proxyuser\tUsername for proxy authentication\n\
1.36 jakob 1026: \t-p port\t Specify local port for remote connects\n\
1.21 ericj 1027: \t-r Randomize remote ports\n\
1.67 jmc 1028: \t-S Enable the TCP MD5 signature option\n\
1.21 ericj 1029: \t-s addr\t Local source address\n\
1.102 haesbaer 1030: \t-T toskeyword\tSet IP Type of Service\n\
1.21 ericj 1031: \t-t Answer TELNET negotiation\n\
1.67 jmc 1032: \t-U Use UNIX domain socket\n\
1.21 ericj 1033: \t-u UDP mode\n\
1.98 guenther 1034: \t-V rtable Specify alternate routing table\n\
1.21 ericj 1035: \t-v Verbose\n\
1036: \t-w secs\t Timeout for connects and final net reads\n\
1.75 djm 1037: \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\
1038: \t-x addr[:port]\tSpecify proxy address and port\n\
1.21 ericj 1039: \t-z Zero-I/O mode [used for scanning]\n\
1040: Port numbers can be individual or ranges: lo-hi [inclusive]\n");
1041: exit(1);
1.11 ericj 1042: }
1043:
1044: void
1.37 jakob 1045: usage(int ret)
1.11 ericj 1046: {
1.92 sobrado 1047: fprintf(stderr,
1.111 ! sthen 1048: "usage: nc [-46DdhklNnrStUuvz] [-I length] [-i interval] [-O length]\n"
1.100 jeremy 1049: "\t [-P proxy_username] [-p source_port] [-s source] [-T ToS]\n"
1.98 guenther 1050: "\t [-V rtable] [-w timeout] [-X proxy_protocol]\n"
1.100 jeremy 1051: "\t [-x proxy_address[:port]] [destination] [port]\n");
1.21 ericj 1052: if (ret)
1053: exit(1);
1.7 deraadt 1054: }