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