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