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