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