Annotation of src/usr.bin/nc/netcat.c, Revision 1.82
1.82 ! marius 1: /* $OpenBSD: netcat.c,v 1.81 2005/05/28 16:57:48 marius 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.65 markus 40: #include <netinet/tcp.h>
1.21 ericj 41: #include <arpa/telnet.h>
1.29 smart 42:
1.11 ericj 43: #include <err.h>
1.7 deraadt 44: #include <errno.h>
1.21 ericj 45: #include <netdb.h>
46: #include <poll.h>
1.13 ericj 47: #include <stdarg.h>
1.21 ericj 48: #include <stdio.h>
1.1 deraadt 49: #include <stdlib.h>
1.21 ericj 50: #include <string.h>
1.5 art 51: #include <unistd.h>
1.42 ericj 52: #include <fcntl.h>
1.79 avsm 53: #include "atomicio.h"
1.51 vincent 54:
55: #ifndef SUN_LEN
56: #define SUN_LEN(su) \
57: (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
58: #endif
1.1 deraadt 59:
1.55 fgsch 60: #define PORT_MAX 65535
61: #define PORT_MAX_LEN 6
1.31 ericj 62:
1.21 ericj 63: /* Command Line Options */
1.68 tedu 64: int dflag; /* detached, no stdin */
1.21 ericj 65: int iflag; /* Interval Flag */
1.80 mcbride 66: int jflag; /* use jumbo frames if we can */
1.21 ericj 67: int kflag; /* More than one connect */
68: int lflag; /* Bind to local port */
1.67 jmc 69: int nflag; /* Don't do name look up */
1.21 ericj 70: char *pflag; /* Localport flag */
71: int rflag; /* Random ports flag */
72: char *sflag; /* Source Address */
73: int tflag; /* Telnet Emulation */
74: int uflag; /* UDP - Default to TCP */
75: int vflag; /* Verbosity */
1.34 jakob 76: int xflag; /* Socks proxy */
1.21 ericj 77: int zflag; /* Port Scan Flag */
1.73 markus 78: int Dflag; /* sodebug */
1.65 markus 79: int Sflag; /* TCP MD5 signature option */
1.21 ericj 80:
1.49 hugh 81: int timeout = -1;
1.21 ericj 82: int family = AF_UNSPEC;
1.63 miod 83: char *portlist[PORT_MAX+1];
1.21 ericj 84:
1.40 millert 85: void atelnet(int, unsigned char *, unsigned int);
86: void build_ports(char *);
87: void help(void);
88: int local_listen(char *, char *, struct addrinfo);
89: void readwrite(int);
1.77 otto 90: int remote_connect(const char *, const char *, struct addrinfo);
91: int socks_connect(const char *, const char *, struct addrinfo, const char *, const char *,
1.46 markus 92: struct addrinfo, int);
1.40 millert 93: int udptest(int);
1.42 ericj 94: int unix_connect(char *);
95: int unix_listen(char *);
1.81 marius 96: int set_common_sockopts(int);
1.40 millert 97: void usage(int);
1.1 deraadt 98:
1.21 ericj 99: int
1.37 jakob 100: main(int argc, char *argv[])
1.21 ericj 101: {
1.46 markus 102: int ch, s, ret, socksv;
1.26 ericj 103: char *host, *uport, *endp;
1.21 ericj 104: struct addrinfo hints;
1.29 smart 105: struct servent *sv;
1.21 ericj 106: socklen_t len;
1.76 hshoexer 107: struct sockaddr_storage cliaddr;
1.34 jakob 108: char *proxy;
1.77 otto 109: const char *proxyhost = "", *proxyport = NULL;
1.34 jakob 110: struct addrinfo proxyhints;
1.11 ericj 111:
1.29 smart 112: ret = 1;
113: s = 0;
1.46 markus 114: socksv = 5;
1.29 smart 115: host = NULL;
116: uport = NULL;
117: endp = NULL;
118: sv = NULL;
119:
1.80 mcbride 120: while ((ch = getopt(argc, argv,
121: "46Ddhi:jklnp:rSs:tUuvw:X:x:z")) != -1) {
1.21 ericj 122: switch (ch) {
123: case '4':
124: family = AF_INET;
125: break;
126: case '6':
127: family = AF_INET6;
128: break;
1.42 ericj 129: case 'U':
130: family = AF_UNIX;
131: break;
1.46 markus 132: case 'X':
1.75 djm 133: if (strcasecmp(optarg, "connect") == 0)
134: socksv = -1; /* HTTP proxy CONNECT */
135: else if (strcmp(optarg, "4") == 0)
136: socksv = 4; /* SOCKS v.4 */
137: else if (strcmp(optarg, "5") == 0)
138: socksv = 5; /* SOCKS v.5 */
139: else
140: errx(1, "unsupported proxy protocol");
1.46 markus 141: break;
1.68 tedu 142: case 'd':
143: dflag = 1;
144: break;
1.21 ericj 145: case 'h':
146: help();
147: break;
148: case 'i':
1.26 ericj 149: iflag = (int)strtoul(optarg, &endp, 10);
150: if (iflag < 0 || *endp != '\0')
151: errx(1, "interval cannot be negative");
1.21 ericj 152: break;
1.80 mcbride 153: case 'j':
154: jflag = 1;
155: break;
1.21 ericj 156: case 'k':
157: kflag = 1;
158: break;
159: case 'l':
160: lflag = 1;
161: break;
162: case 'n':
163: nflag = 1;
164: break;
165: case 'p':
166: pflag = optarg;
167: break;
168: case 'r':
169: rflag = 1;
170: break;
171: case 's':
172: sflag = optarg;
173: break;
174: case 't':
175: tflag = 1;
176: break;
177: case 'u':
178: uflag = 1;
179: break;
180: case 'v':
181: vflag = 1;
182: break;
1.70 deraadt 183: case 'w':
1.26 ericj 184: timeout = (int)strtoul(optarg, &endp, 10);
185: if (timeout < 0 || *endp != '\0')
186: errx(1, "timeout cannot be negative");
1.49 hugh 187: if (timeout >= (INT_MAX / 1000))
188: errx(1, "timeout too large");
189: timeout *= 1000;
1.21 ericj 190: break;
1.34 jakob 191: case 'x':
192: xflag = 1;
1.64 deraadt 193: if ((proxy = strdup(optarg)) == NULL)
194: err(1, NULL);
1.34 jakob 195: break;
1.21 ericj 196: case 'z':
197: zflag = 1;
198: break;
1.73 markus 199: case 'D':
200: Dflag = 1;
201: break;
1.65 markus 202: case 'S':
203: Sflag = 1;
204: break;
1.21 ericj 205: default:
206: usage(1);
207: }
208: }
209: argc -= optind;
210: argv += optind;
1.11 ericj 211:
1.21 ericj 212: /* Cruft to make sure options are clean, and used properly. */
1.42 ericj 213: if (argv[0] && !argv[1] && family == AF_UNIX) {
214: if (uflag)
215: errx(1, "cannot use -u and -U");
216: host = argv[0];
217: uport = NULL;
218: } else if (argv[0] && !argv[1]) {
1.21 ericj 219: if (!lflag)
220: usage(1);
221: uport = argv[0];
222: host = NULL;
223: } else if (argv[0] && argv[1]) {
224: host = argv[0];
225: uport = argv[1];
226: } else
227: usage(1);
1.1 deraadt 228:
1.21 ericj 229: if (lflag && sflag)
230: errx(1, "cannot use -s and -l");
231: if (lflag && pflag)
232: errx(1, "cannot use -p and -l");
233: if (lflag && zflag)
1.32 ericj 234: errx(1, "cannot use -z and -l");
1.21 ericj 235: if (!lflag && kflag)
1.32 ericj 236: errx(1, "must use -l with -k");
1.21 ericj 237:
1.67 jmc 238: /* Initialize addrinfo structure. */
1.42 ericj 239: if (family != AF_UNIX) {
240: memset(&hints, 0, sizeof(struct addrinfo));
241: hints.ai_family = family;
242: hints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
243: hints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
244: if (nflag)
245: hints.ai_flags |= AI_NUMERICHOST;
246: }
1.1 deraadt 247:
1.34 jakob 248: if (xflag) {
249: if (uflag)
250: errx(1, "no proxy support for UDP mode");
251:
252: if (lflag)
253: errx(1, "no proxy support for listen");
254:
1.42 ericj 255: if (family == AF_UNIX)
256: errx(1, "no proxy support for unix sockets");
257:
1.34 jakob 258: /* XXX IPv6 transport to proxy would probably work */
259: if (family == AF_INET6)
260: errx(1, "no proxy support for IPv6");
261:
262: if (sflag)
263: errx(1, "no proxy support for local source address");
264:
265: proxyhost = strsep(&proxy, ":");
266: proxyport = proxy;
267:
268: memset(&proxyhints, 0, sizeof(struct addrinfo));
269: proxyhints.ai_family = family;
270: proxyhints.ai_socktype = SOCK_STREAM;
271: proxyhints.ai_protocol = IPPROTO_TCP;
272: if (nflag)
273: proxyhints.ai_flags |= AI_NUMERICHOST;
274: }
275:
1.21 ericj 276: if (lflag) {
277: int connfd;
1.27 ericj 278: ret = 0;
1.1 deraadt 279:
1.42 ericj 280: if (family == AF_UNIX)
281: s = unix_listen(host);
282:
1.67 jmc 283: /* Allow only one connection at a time, but stay alive. */
1.21 ericj 284: for (;;) {
1.42 ericj 285: if (family != AF_UNIX)
286: s = local_listen(host, uport, hints);
287: if (s < 0)
1.30 smart 288: err(1, NULL);
1.21 ericj 289: /*
290: * For UDP, we will use recvfrom() initially
291: * to wait for a caller, then use the regular
292: * functions to talk to the caller.
293: */
294: if (uflag) {
1.80 mcbride 295: int rv, plen;
296: char buf[8192];
1.21 ericj 297: struct sockaddr_storage z;
298:
299: len = sizeof(z);
1.80 mcbride 300: plen = jflag ? 8192 : 1024;
301: rv = recvfrom(s, buf, plen, MSG_PEEK,
1.37 jakob 302: (struct sockaddr *)&z, &len);
1.23 ericj 303: if (rv < 0)
1.57 stevesk 304: err(1, "recvfrom");
1.21 ericj 305:
1.37 jakob 306: rv = connect(s, (struct sockaddr *)&z, len);
1.23 ericj 307: if (rv < 0)
1.57 stevesk 308: err(1, "connect");
1.1 deraadt 309:
1.21 ericj 310: connfd = s;
311: } else {
1.78 otto 312: len = sizeof(cliaddr);
1.21 ericj 313: connfd = accept(s, (struct sockaddr *)&cliaddr,
1.37 jakob 314: &len);
1.21 ericj 315: }
1.1 deraadt 316:
1.21 ericj 317: readwrite(connfd);
318: close(connfd);
1.42 ericj 319: if (family != AF_UNIX)
320: close(s);
1.27 ericj 321:
1.21 ericj 322: if (!kflag)
323: break;
1.11 ericj 324: }
1.42 ericj 325: } else if (family == AF_UNIX) {
326: ret = 0;
327:
328: if ((s = unix_connect(host)) > 0 && !zflag) {
329: readwrite(s);
330: close(s);
331: } else
332: ret = 1;
333:
334: exit(ret);
335:
1.21 ericj 336: } else {
337: int i = 0;
1.6 deraadt 338:
1.67 jmc 339: /* Construct the portlist[] array. */
1.21 ericj 340: build_ports(uport);
1.1 deraadt 341:
1.67 jmc 342: /* Cycle through portlist, connecting to each port. */
1.21 ericj 343: for (i = 0; portlist[i] != NULL; i++) {
344: if (s)
345: close(s);
1.34 jakob 346:
347: if (xflag)
348: s = socks_connect(host, portlist[i], hints,
1.46 markus 349: proxyhost, proxyport, proxyhints, socksv);
1.34 jakob 350: else
351: s = remote_connect(host, portlist[i], hints);
352:
353: if (s < 0)
1.21 ericj 354: continue;
1.1 deraadt 355:
1.21 ericj 356: ret = 0;
357: if (vflag || zflag) {
1.67 jmc 358: /* For UDP, make sure we are connected. */
1.21 ericj 359: if (uflag) {
1.50 vincent 360: if (udptest(s) == -1) {
1.21 ericj 361: ret = 1;
362: continue;
363: }
364: }
1.1 deraadt 365:
1.67 jmc 366: /* Don't look up port if -n. */
1.21 ericj 367: if (nflag)
368: sv = NULL;
369: else {
370: sv = getservbyport(
1.37 jakob 371: ntohs(atoi(portlist[i])),
372: uflag ? "udp" : "tcp");
1.21 ericj 373: }
1.50 vincent 374:
1.21 ericj 375: printf("Connection to %s %s port [%s/%s] succeeded!\n",
1.37 jakob 376: host, portlist[i], uflag ? "udp" : "tcp",
377: sv ? sv->s_name : "*");
1.21 ericj 378: }
379: if (!zflag)
380: readwrite(s);
1.7 deraadt 381: }
1.11 ericj 382: }
1.1 deraadt 383:
1.21 ericj 384: if (s)
385: close(s);
386:
387: exit(ret);
1.7 deraadt 388: }
1.1 deraadt 389:
1.11 ericj 390: /*
1.42 ericj 391: * unix_connect()
1.67 jmc 392: * Returns a socket connected to a local unix socket. Returns -1 on failure.
1.42 ericj 393: */
394: int
395: unix_connect(char *path)
396: {
397: struct sockaddr_un sun;
398: int s;
399:
400: if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
1.50 vincent 401: return (-1);
1.42 ericj 402: (void)fcntl(s, F_SETFD, 1);
403:
404: memset(&sun, 0, sizeof(struct sockaddr_un));
405: sun.sun_family = AF_UNIX;
1.60 avsm 406:
407: if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
408: sizeof(sun.sun_path)) {
409: close(s);
410: errno = ENAMETOOLONG;
411: return (-1);
412: }
1.50 vincent 413: if (connect(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
414: close(s);
415: return (-1);
1.42 ericj 416: }
417: return (s);
1.50 vincent 418:
1.42 ericj 419: }
420:
421: /*
422: * unix_listen()
1.67 jmc 423: * Create a unix domain socket, and listen on it.
1.42 ericj 424: */
425: int
426: unix_listen(char *path)
427: {
428: struct sockaddr_un sun;
429: int s;
430:
1.67 jmc 431: /* Create unix domain socket. */
1.42 ericj 432: if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
433: return (-1);
434:
1.60 avsm 435: memset(&sun, 0, sizeof(struct sockaddr_un));
1.42 ericj 436: sun.sun_family = AF_UNIX;
1.60 avsm 437:
438: if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
439: sizeof(sun.sun_path)) {
440: close(s);
441: errno = ENAMETOOLONG;
442: return (-1);
443: }
444:
1.50 vincent 445: if (bind(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
1.42 ericj 446: close(s);
447: return (-1);
448: }
449:
450: if (listen(s, 5) < 0) {
451: close(s);
452: return (-1);
453: }
454: return (s);
455: }
456:
457: /*
1.21 ericj 458: * remote_connect()
1.67 jmc 459: * Returns a socket connected to a remote host. Properly binds to a local
460: * port or source address if needed. Returns -1 on failure.
1.11 ericj 461: */
1.21 ericj 462: int
1.77 otto 463: remote_connect(const char *host, const char *port, struct addrinfo hints)
1.21 ericj 464: {
465: struct addrinfo *res, *res0;
1.81 marius 466: int s, error;
1.21 ericj 467:
468: if ((error = getaddrinfo(host, port, &hints, &res)))
1.56 stevesk 469: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.21 ericj 470:
471: res0 = res;
472: do {
473: if ((s = socket(res0->ai_family, res0->ai_socktype,
1.37 jakob 474: res0->ai_protocol)) < 0)
1.21 ericj 475: continue;
476:
1.67 jmc 477: /* Bind to a local port or source address if specified. */
1.21 ericj 478: if (sflag || pflag) {
479: struct addrinfo ahints, *ares;
480:
481: if (!(sflag && pflag)) {
482: if (!sflag)
483: sflag = NULL;
484: else
485: pflag = NULL;
486: }
1.6 deraadt 487:
1.21 ericj 488: memset(&ahints, 0, sizeof(struct addrinfo));
489: ahints.ai_family = res0->ai_family;
490: ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
491: ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
1.25 ericj 492: ahints.ai_flags = AI_PASSIVE;
1.38 jakob 493: if ((error = getaddrinfo(sflag, pflag, &ahints, &ares)))
1.56 stevesk 494: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.21 ericj 495:
496: if (bind(s, (struct sockaddr *)ares->ai_addr,
1.62 millert 497: ares->ai_addrlen) < 0)
1.21 ericj 498: errx(1, "bind failed: %s", strerror(errno));
499: freeaddrinfo(ares);
1.6 deraadt 500: }
1.81 marius 501:
502: set_common_sockopts(s);
1.6 deraadt 503:
1.21 ericj 504: if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
1.6 deraadt 505: break;
1.71 mcbride 506: else if (vflag)
507: warn("connect to %s port %s (%s) failed", host, port,
508: uflag ? "udp" : "tcp");
1.34 jakob 509:
1.21 ericj 510: close(s);
511: s = -1;
512: } while ((res0 = res0->ai_next) != NULL);
513:
514: freeaddrinfo(res);
1.1 deraadt 515:
1.21 ericj 516: return (s);
1.7 deraadt 517: }
1.1 deraadt 518:
1.11 ericj 519: /*
1.21 ericj 520: * local_listen()
1.67 jmc 521: * Returns a socket listening on a local port, binds to specified source
522: * address. Returns -1 on failure.
1.11 ericj 523: */
1.21 ericj 524: int
1.37 jakob 525: local_listen(char *host, char *port, struct addrinfo hints)
1.21 ericj 526: {
527: struct addrinfo *res, *res0;
528: int s, ret, x = 1;
529: int error;
1.6 deraadt 530:
1.67 jmc 531: /* Allow nodename to be null. */
1.21 ericj 532: hints.ai_flags |= AI_PASSIVE;
1.7 deraadt 533:
1.21 ericj 534: /*
535: * In the case of binding to a wildcard address
536: * default to binding to an ipv4 address.
537: */
538: if (host == NULL && hints.ai_family == AF_UNSPEC)
539: hints.ai_family = AF_INET;
1.1 deraadt 540:
1.21 ericj 541: if ((error = getaddrinfo(host, port, &hints, &res)))
1.70 deraadt 542: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.14 ericj 543:
1.21 ericj 544: res0 = res;
545: do {
546: if ((s = socket(res0->ai_family, res0->ai_socktype,
1.82 ! marius 547: res0->ai_protocol)) < 0)
1.21 ericj 548: continue;
1.1 deraadt 549:
1.21 ericj 550: ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
551: if (ret == -1)
1.30 smart 552: err(1, NULL);
1.81 marius 553:
554: set_common_sockopts(s);
1.1 deraadt 555:
1.21 ericj 556: if (bind(s, (struct sockaddr *)res0->ai_addr,
1.37 jakob 557: res0->ai_addrlen) == 0)
1.21 ericj 558: break;
1.1 deraadt 559:
1.21 ericj 560: close(s);
561: s = -1;
562: } while ((res0 = res0->ai_next) != NULL);
1.1 deraadt 563:
1.47 ericj 564: if (!uflag && s != -1) {
1.21 ericj 565: if (listen(s, 1) < 0)
1.57 stevesk 566: err(1, "listen");
1.12 ericj 567: }
1.1 deraadt 568:
1.21 ericj 569: freeaddrinfo(res);
1.1 deraadt 570:
1.21 ericj 571: return (s);
1.7 deraadt 572: }
573:
1.11 ericj 574: /*
1.21 ericj 575: * readwrite()
576: * Loop that polls on the network file descriptor and stdin.
1.11 ericj 577: */
1.21 ericj 578: void
1.37 jakob 579: readwrite(int nfd)
1.6 deraadt 580: {
1.52 vincent 581: struct pollfd pfd[2];
1.80 mcbride 582: unsigned char buf[8192];
1.79 avsm 583: int n, wfd = fileno(stdin);
1.21 ericj 584: int lfd = fileno(stdout);
1.80 mcbride 585: int plen;
586:
587: plen = jflag ? 8192 : 1024;
1.21 ericj 588:
589: /* Setup Network FD */
590: pfd[0].fd = nfd;
591: pfd[0].events = POLLIN;
592:
1.67 jmc 593: /* Set up STDIN FD. */
1.21 ericj 594: pfd[1].fd = wfd;
595: pfd[1].events = POLLIN;
596:
1.54 aaron 597: while (pfd[0].fd != -1) {
1.21 ericj 598: if (iflag)
599: sleep(iflag);
600:
1.68 tedu 601: if ((n = poll(pfd, 2 - dflag, timeout)) < 0) {
1.21 ericj 602: close(nfd);
1.52 vincent 603: err(1, "Polling Error");
1.21 ericj 604: }
1.49 hugh 605:
606: if (n == 0)
607: return;
1.21 ericj 608:
609: if (pfd[0].revents & POLLIN) {
1.80 mcbride 610: if ((n = read(nfd, buf, plen)) < 0)
1.21 ericj 611: return;
1.52 vincent 612: else if (n == 0) {
613: shutdown(nfd, SHUT_RD);
614: pfd[0].fd = -1;
615: pfd[0].events = 0;
1.21 ericj 616: } else {
617: if (tflag)
618: atelnet(nfd, buf, n);
1.79 avsm 619: if (atomicio(vwrite, lfd, buf, n) != n)
1.21 ericj 620: return;
1.6 deraadt 621: }
1.21 ericj 622: }
623:
1.68 tedu 624: if (!dflag && pfd[1].revents & POLLIN) {
1.80 mcbride 625: if ((n = read(wfd, buf, plen)) < 0)
1.21 ericj 626: return;
1.52 vincent 627: else if (n == 0) {
628: shutdown(nfd, SHUT_WR);
629: pfd[1].fd = -1;
630: pfd[1].events = 0;
631: } else {
1.79 avsm 632: if (atomicio(vwrite, nfd, buf, n) != n)
1.21 ericj 633: return;
1.50 vincent 634: }
1.21 ericj 635: }
1.11 ericj 636: }
1.7 deraadt 637: }
1.50 vincent 638:
1.67 jmc 639: /* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */
1.21 ericj 640: void
1.37 jakob 641: atelnet(int nfd, unsigned char *buf, unsigned int size)
1.6 deraadt 642: {
1.24 ericj 643: unsigned char *p, *end;
644: unsigned char obuf[4];
645:
646: end = buf + size;
647: obuf[0] = '\0';
648:
649: for (p = buf; p < end; p++) {
1.21 ericj 650: if (*p != IAC)
1.24 ericj 651: break;
652:
1.25 ericj 653: obuf[0] = IAC;
1.24 ericj 654: p++;
1.50 vincent 655: if ((*p == WILL) || (*p == WONT))
1.24 ericj 656: obuf[1] = DONT;
1.50 vincent 657: if ((*p == DO) || (*p == DONT))
1.24 ericj 658: obuf[1] = WONT;
659: if (obuf) {
660: p++;
1.11 ericj 661: obuf[2] = *p;
1.24 ericj 662: obuf[3] = '\0';
1.79 avsm 663: if (atomicio(vwrite, nfd, obuf, 3) != 3)
664: warn("Write Error!");
1.24 ericj 665: obuf[0] = '\0';
1.11 ericj 666: }
667: }
1.7 deraadt 668: }
669:
1.11 ericj 670: /*
1.21 ericj 671: * build_ports()
672: * Build an array or ports in portlist[], listing each port
1.67 jmc 673: * that we should try to connect to.
1.11 ericj 674: */
1.21 ericj 675: void
1.37 jakob 676: build_ports(char *p)
1.6 deraadt 677: {
1.26 ericj 678: char *n, *endp;
1.21 ericj 679: int hi, lo, cp;
680: int x = 0;
681:
682: if ((n = strchr(p, '-')) != NULL) {
683: if (lflag)
684: errx(1, "Cannot use -l with multiple ports!");
685:
686: *n = '\0';
687: n++;
688:
1.67 jmc 689: /* Make sure the ports are in order: lowest->highest. */
1.26 ericj 690: hi = (int)strtoul(n, &endp, 10);
1.31 ericj 691: if (hi <= 0 || hi > PORT_MAX || *endp != '\0')
1.26 ericj 692: errx(1, "port range not valid");
693: lo = (int)strtoul(p, &endp, 10);
1.31 ericj 694: if (lo <= 0 || lo > PORT_MAX || *endp != '\0')
1.26 ericj 695: errx(1, "port range not valid");
1.21 ericj 696:
697: if (lo > hi) {
698: cp = hi;
699: hi = lo;
700: lo = cp;
701: }
702:
1.67 jmc 703: /* Load ports sequentially. */
1.21 ericj 704: for (cp = lo; cp <= hi; cp++) {
1.55 fgsch 705: portlist[x] = calloc(1, PORT_MAX_LEN);
706: if (portlist[x] == NULL)
707: err(1, NULL);
708: snprintf(portlist[x], PORT_MAX_LEN, "%d", cp);
1.21 ericj 709: x++;
710: }
711:
1.67 jmc 712: /* Randomly swap ports. */
1.21 ericj 713: if (rflag) {
714: int y;
715: char *c;
716:
717: for (x = 0; x <= (hi - lo); x++) {
718: y = (arc4random() & 0xFFFF) % (hi - lo);
719: c = portlist[x];
720: portlist[x] = portlist[y];
721: portlist[y] = c;
1.6 deraadt 722: }
1.11 ericj 723: }
1.21 ericj 724: } else {
1.26 ericj 725: hi = (int)strtoul(p, &endp, 10);
1.31 ericj 726: if (hi <= 0 || hi > PORT_MAX || *endp != '\0')
1.26 ericj 727: errx(1, "port range not valid");
1.55 fgsch 728: portlist[0] = calloc(1, PORT_MAX_LEN);
729: if (portlist[0] == NULL)
730: err(1, NULL);
1.21 ericj 731: portlist[0] = p;
1.11 ericj 732: }
1.13 ericj 733: }
734:
735: /*
1.21 ericj 736: * udptest()
737: * Do a few writes to see if the UDP port is there.
1.67 jmc 738: * XXX - Better way of doing this? Doesn't work for IPv6.
1.21 ericj 739: * Also fails after around 100 ports checked.
1.13 ericj 740: */
1.21 ericj 741: int
1.37 jakob 742: udptest(int s)
1.13 ericj 743: {
1.74 deraadt 744: int i, ret;
1.13 ericj 745:
1.52 vincent 746: for (i = 0; i <= 3; i++) {
1.74 deraadt 747: if (write(s, "X", 1) == 1)
1.21 ericj 748: ret = 1;
1.14 ericj 749: else
1.21 ericj 750: ret = -1;
1.14 ericj 751: }
1.21 ericj 752: return (ret);
1.81 marius 753: }
754:
755: int
756: set_common_sockopts(int s)
757: {
758: int x = 1;
759:
760: if (Sflag) {
761: if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
762: &x, sizeof(x)) == -1)
763: err(1, NULL);
764: }
765: if (Dflag) {
766: if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
767: &x, sizeof(x)) == -1)
768: err(1, NULL);
769: }
770: if (jflag) {
771: if (setsockopt(s, SOL_SOCKET, SO_JUMBO,
772: &x, sizeof(x)) == -1)
773: err(1, NULL);
774: }
1.7 deraadt 775: }
1.1 deraadt 776:
1.11 ericj 777: void
1.58 deraadt 778: help(void)
1.1 deraadt 779: {
1.21 ericj 780: usage(0);
781: fprintf(stderr, "\tCommand Summary:\n\
782: \t-4 Use IPv4\n\
783: \t-6 Use IPv6\n\
1.73 markus 784: \t-D Enable the debug socket option\n\
1.69 tedu 785: \t-d Detach from stdin\n\
1.21 ericj 786: \t-h This help text\n\
787: \t-i secs\t Delay interval for lines sent, ports scanned\n\
788: \t-k Keep inbound sockets open for multiple connects\n\
789: \t-l Listen mode, for inbound connects\n\
1.22 jasoni 790: \t-n Suppress name/port resolutions\n\
1.36 jakob 791: \t-p port\t Specify local port for remote connects\n\
1.21 ericj 792: \t-r Randomize remote ports\n\
1.67 jmc 793: \t-S Enable the TCP MD5 signature option\n\
1.21 ericj 794: \t-s addr\t Local source address\n\
795: \t-t Answer TELNET negotiation\n\
1.67 jmc 796: \t-U Use UNIX domain socket\n\
1.21 ericj 797: \t-u UDP mode\n\
798: \t-v Verbose\n\
799: \t-w secs\t Timeout for connects and final net reads\n\
1.75 djm 800: \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\
801: \t-x addr[:port]\tSpecify proxy address and port\n\
1.21 ericj 802: \t-z Zero-I/O mode [used for scanning]\n\
803: Port numbers can be individual or ranges: lo-hi [inclusive]\n");
804: exit(1);
1.11 ericj 805: }
806:
807: void
1.37 jakob 808: usage(int ret)
1.11 ericj 809: {
1.73 markus 810: fprintf(stderr, "usage: nc [-46DdhklnrStUuvz] [-i interval] [-p source_port]\n");
1.75 djm 811: fprintf(stderr, "\t [-s source_ip_address] [-w timeout] [-X proxy_version]\n");
1.72 jmc 812: fprintf(stderr, "\t [-x proxy_address[:port]] [hostname] [port[s]]\n");
1.21 ericj 813: if (ret)
814: exit(1);
1.7 deraadt 815: }