Annotation of src/usr.bin/nc/netcat.c, Revision 1.167
1.167 ! beck 1: /* $OpenBSD: netcat.c,v 1.166 2016/11/03 15:54:39 beck Exp $ */
1.21 ericj 2: /*
3: * Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
1.133 beck 4: * Copyright (c) 2015 Bob Beck. All rights reserved.
1.7 deraadt 5: *
1.21 ericj 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.7 deraadt 9: *
1.21 ericj 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. The name of the author may not be used to endorse or promote products
16: * derived from this software without specific prior written permission.
1.7 deraadt 17: *
1.21 ericj 18: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: */
1.1 deraadt 29:
1.24 ericj 30: /*
31: * Re-written nc(1) for OpenBSD. Original implementation by
1.21 ericj 32: * *Hobbit* <hobbit@avian.org>.
33: */
1.1 deraadt 34:
1.7 deraadt 35: #include <sys/types.h>
1.21 ericj 36: #include <sys/socket.h>
1.113 djm 37: #include <sys/uio.h>
1.42 ericj 38: #include <sys/un.h>
1.21 ericj 39:
1.7 deraadt 40: #include <netinet/in.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.129 tobias 47: #include <limits.h>
1.21 ericj 48: #include <netdb.h>
49: #include <poll.h>
1.129 tobias 50: #include <signal.h>
1.13 ericj 51: #include <stdarg.h>
1.21 ericj 52: #include <stdio.h>
1.1 deraadt 53: #include <stdlib.h>
1.21 ericj 54: #include <string.h>
1.149 bcook 55: #include <time.h>
1.5 art 56: #include <unistd.h>
1.133 beck 57: #include <tls.h>
1.79 avsm 58: #include "atomicio.h"
1.51 vincent 59:
1.55 fgsch 60: #define PORT_MAX 65535
1.99 jeremy 61: #define UNIX_DG_TMP_SOCKET_SIZE 19
1.31 ericj 62:
1.125 tedu 63: #define POLL_STDIN 0
64: #define POLL_NETOUT 1
65: #define POLL_NETIN 2
66: #define POLL_STDOUT 3
1.126 tedu 67: #define BUFSIZE 16384
1.159 jsing 68: #define DEFAULT_CA_FILE "/etc/ssl/cert.pem"
1.133 beck 69:
70: #define TLS_LEGACY (1 << 1)
71: #define TLS_NOVERIFY (1 << 2)
72: #define TLS_NONAME (1 << 3)
73: #define TLS_CCERT (1 << 4)
1.167 ! beck 74: #define TLS_MUSTSTAPLE (1 << 5)
1.125 tedu 75:
1.21 ericj 76: /* Command Line Options */
1.68 tedu 77: int dflag; /* detached, no stdin */
1.113 djm 78: int Fflag; /* fdpass sock to stdout */
1.88 ray 79: unsigned int iflag; /* Interval Flag */
1.21 ericj 80: int kflag; /* More than one connect */
81: int lflag; /* Bind to local port */
1.111 sthen 82: int Nflag; /* shutdown() network socket */
1.67 jmc 83: int nflag; /* Don't do name look up */
1.86 djm 84: char *Pflag; /* Proxy username */
1.21 ericj 85: char *pflag; /* Localport flag */
86: int rflag; /* Random ports flag */
87: char *sflag; /* Source Address */
88: int tflag; /* Telnet Emulation */
89: int uflag; /* UDP - Default to TCP */
90: int vflag; /* Verbosity */
1.34 jakob 91: int xflag; /* Socks proxy */
1.21 ericj 92: int zflag; /* Port Scan Flag */
1.73 markus 93: int Dflag; /* sodebug */
1.90 djm 94: int Iflag; /* TCP receive buffer size */
95: int Oflag; /* TCP send buffer size */
1.65 markus 96: int Sflag; /* TCP MD5 signature option */
1.83 dtucker 97: int Tflag = -1; /* IP Type of Service */
1.117 sthen 98: int rtableid = -1;
1.21 ericj 99:
1.133 beck 100: int usetls; /* use TLS */
101: char *Cflag; /* Public cert file */
102: char *Kflag; /* Private key file */
1.159 jsing 103: char *Rflag = DEFAULT_CA_FILE; /* Root CA file */
1.133 beck 104: int tls_cachanged; /* Using non-default CA file */
105: int TLSopt; /* TLS options */
106: char *tls_expectname; /* required name in peer cert */
107: char *tls_expecthash; /* required hash of peer cert */
108:
1.49 hugh 109: int timeout = -1;
1.21 ericj 110: int family = AF_UNSPEC;
1.63 miod 111: char *portlist[PORT_MAX+1];
1.99 jeremy 112: char *unix_dg_tmp_socket;
1.156 jca 113: int ttl = -1;
114: int minttl = -1;
1.21 ericj 115:
1.40 millert 116: void atelnet(int, unsigned char *, unsigned int);
117: void build_ports(char *);
118: void help(void);
119: int local_listen(char *, char *, struct addrinfo);
1.133 beck 120: void readwrite(int, struct tls *);
1.113 djm 121: void fdpass(int nfd) __attribute__((noreturn));
1.77 otto 122: int remote_connect(const char *, const char *, struct addrinfo);
1.103 fgsch 123: int timeout_connect(int, const struct sockaddr *, socklen_t);
1.86 djm 124: int socks_connect(const char *, const char *, struct addrinfo,
125: const char *, const char *, struct addrinfo, int, const char *);
1.40 millert 126: int udptest(int);
1.136 deraadt 127: int unix_bind(char *, int);
1.42 ericj 128: int unix_connect(char *);
129: int unix_listen(char *);
1.127 jca 130: void set_common_sockopts(int, int);
1.102 haesbaer 131: int map_tos(char *, int *);
1.133 beck 132: int map_tls(char *, int *);
1.151 beck 133: void report_connect(const struct sockaddr *, socklen_t, char *);
1.134 deraadt 134: void report_tls(struct tls *tls_ctx, char * host, char *tls_expectname);
1.40 millert 135: void usage(int);
1.133 beck 136: ssize_t drainbuf(int, unsigned char *, size_t *, struct tls *);
137: ssize_t fillbuf(int, unsigned char *, size_t *, struct tls *);
138: void tls_setup_client(struct tls *, int, char *);
1.134 deraadt 139: struct tls *tls_setup_server(struct tls *, int, char *);
1.1 deraadt 140:
1.21 ericj 141: int
1.37 jakob 142: main(int argc, char *argv[])
1.21 ericj 143: {
1.154 deraadt 144: int ch, s = -1, ret, socksv;
1.88 ray 145: char *host, *uport;
1.21 ericj 146: struct addrinfo hints;
1.29 smart 147: struct servent *sv;
1.21 ericj 148: socklen_t len;
1.76 hshoexer 149: struct sockaddr_storage cliaddr;
1.34 jakob 150: char *proxy;
1.88 ray 151: const char *errstr, *proxyhost = "", *proxyport = NULL;
1.34 jakob 152: struct addrinfo proxyhints;
1.99 jeremy 153: char unix_dg_tmp_socket_buf[UNIX_DG_TMP_SOCKET_SIZE];
1.133 beck 154: struct tls_config *tls_cfg = NULL;
155: struct tls *tls_ctx = NULL;
1.11 ericj 156:
1.29 smart 157: ret = 1;
1.46 markus 158: socksv = 5;
1.29 smart 159: host = NULL;
160: uport = NULL;
161: sv = NULL;
1.129 tobias 162:
163: signal(SIGPIPE, SIG_IGN);
1.29 smart 164:
1.80 mcbride 165: while ((ch = getopt(argc, argv,
1.156 jca 166: "46C:cDde:FH:hI:i:K:klM:m:NnO:P:p:R:rSs:T:tUuV:vw:X:x:z")) != -1) {
1.21 ericj 167: switch (ch) {
168: case '4':
169: family = AF_INET;
170: break;
171: case '6':
172: family = AF_INET6;
173: break;
1.42 ericj 174: case 'U':
175: family = AF_UNIX;
176: break;
1.46 markus 177: case 'X':
1.75 djm 178: if (strcasecmp(optarg, "connect") == 0)
179: socksv = -1; /* HTTP proxy CONNECT */
180: else if (strcmp(optarg, "4") == 0)
181: socksv = 4; /* SOCKS v.4 */
182: else if (strcmp(optarg, "5") == 0)
183: socksv = 5; /* SOCKS v.5 */
184: else
185: errx(1, "unsupported proxy protocol");
1.46 markus 186: break;
1.133 beck 187: case 'C':
188: Cflag = optarg;
189: break;
190: case 'c':
191: usetls = 1;
192: break;
1.68 tedu 193: case 'd':
194: dflag = 1;
195: break;
1.133 beck 196: case 'e':
197: tls_expectname = optarg;
198: break;
1.113 djm 199: case 'F':
200: Fflag = 1;
201: break;
1.133 beck 202: case 'H':
203: tls_expecthash = optarg;
204: break;
1.21 ericj 205: case 'h':
206: help();
207: break;
208: case 'i':
1.88 ray 209: iflag = strtonum(optarg, 0, UINT_MAX, &errstr);
210: if (errstr)
211: errx(1, "interval %s: %s", errstr, optarg);
1.21 ericj 212: break;
1.133 beck 213: case 'K':
214: Kflag = optarg;
215: break;
1.21 ericj 216: case 'k':
217: kflag = 1;
218: break;
219: case 'l':
220: lflag = 1;
221: break;
1.156 jca 222: case 'M':
223: ttl = strtonum(optarg, 0, 255, &errstr);
224: if (errstr)
225: errx(1, "ttl is %s", errstr);
226: break;
227: case 'm':
228: minttl = strtonum(optarg, 0, 255, &errstr);
229: if (errstr)
230: errx(1, "minttl is %s", errstr);
231: break;
1.111 sthen 232: case 'N':
233: Nflag = 1;
234: break;
1.21 ericj 235: case 'n':
236: nflag = 1;
237: break;
1.86 djm 238: case 'P':
239: Pflag = optarg;
240: break;
1.21 ericj 241: case 'p':
242: pflag = optarg;
243: break;
1.133 beck 244: case 'R':
245: tls_cachanged = 1;
246: Rflag = optarg;
247: break;
1.21 ericj 248: case 'r':
249: rflag = 1;
250: break;
251: case 's':
252: sflag = optarg;
253: break;
254: case 't':
255: tflag = 1;
256: break;
257: case 'u':
258: uflag = 1;
259: break;
1.93 claudio 260: case 'V':
1.117 sthen 261: rtableid = (int)strtonum(optarg, 0,
1.93 claudio 262: RT_TABLEID_MAX, &errstr);
263: if (errstr)
1.98 guenther 264: errx(1, "rtable %s: %s", errstr, optarg);
1.93 claudio 265: break;
1.21 ericj 266: case 'v':
267: vflag = 1;
268: break;
1.70 deraadt 269: case 'w':
1.88 ray 270: timeout = strtonum(optarg, 0, INT_MAX / 1000, &errstr);
271: if (errstr)
272: errx(1, "timeout %s: %s", errstr, optarg);
1.49 hugh 273: timeout *= 1000;
1.21 ericj 274: break;
1.34 jakob 275: case 'x':
276: xflag = 1;
1.64 deraadt 277: if ((proxy = strdup(optarg)) == NULL)
278: err(1, NULL);
1.34 jakob 279: break;
1.21 ericj 280: case 'z':
281: zflag = 1;
282: break;
1.73 markus 283: case 'D':
284: Dflag = 1;
285: break;
1.90 djm 286: case 'I':
287: Iflag = strtonum(optarg, 1, 65536 << 14, &errstr);
288: if (errstr != NULL)
289: errx(1, "TCP receive window %s: %s",
290: errstr, optarg);
291: break;
292: case 'O':
293: Oflag = strtonum(optarg, 1, 65536 << 14, &errstr);
294: if (errstr != NULL)
295: errx(1, "TCP send window %s: %s",
296: errstr, optarg);
297: break;
1.65 markus 298: case 'S':
299: Sflag = 1;
300: break;
1.83 dtucker 301: case 'T':
1.102 haesbaer 302: errstr = NULL;
303: errno = 0;
304: if (map_tos(optarg, &Tflag))
305: break;
1.133 beck 306: if (map_tls(optarg, &TLSopt))
307: break;
1.102 haesbaer 308: if (strlen(optarg) > 1 && optarg[0] == '0' &&
309: optarg[1] == 'x')
310: Tflag = (int)strtol(optarg, NULL, 16);
311: else
312: Tflag = (int)strtonum(optarg, 0, 255,
313: &errstr);
314: if (Tflag < 0 || Tflag > 255 || errstr || errno)
1.133 beck 315: errx(1, "illegal tos/tls value %s", optarg);
1.83 dtucker 316: break;
1.21 ericj 317: default:
318: usage(1);
319: }
320: }
321: argc -= optind;
322: argv += optind;
1.11 ericj 323:
1.143 deraadt 324: if (rtableid >= 0)
1.142 benno 325: if (setrtable(rtableid) == -1)
326: err(1, "setrtable");
1.143 deraadt 327:
1.142 benno 328: if (family == AF_UNIX) {
1.140 beck 329: if (pledge("stdio rpath wpath cpath tmppath unix", NULL) == -1)
330: err(1, "pledge");
1.143 deraadt 331: } else if (Fflag) {
1.152 beck 332: if (Pflag) {
333: if (pledge("stdio inet dns sendfd tty", NULL) == -1)
334: err(1, "pledge");
335: } else if (pledge("stdio inet dns sendfd", NULL) == -1)
336: err(1, "pledge");
337: } else if (Pflag) {
338: if (pledge("stdio inet dns tty", NULL) == -1)
1.140 beck 339: err(1, "pledge");
1.143 deraadt 340: } else if (usetls) {
1.140 beck 341: if (pledge("stdio rpath inet dns", NULL) == -1)
342: err(1, "pledge");
1.143 deraadt 343: } else if (pledge("stdio inet dns", NULL) == -1)
1.140 beck 344: err(1, "pledge");
345:
1.21 ericj 346: /* Cruft to make sure options are clean, and used properly. */
1.42 ericj 347: if (argv[0] && !argv[1] && family == AF_UNIX) {
348: host = argv[0];
349: uport = NULL;
350: } else if (argv[0] && !argv[1]) {
1.21 ericj 351: if (!lflag)
352: usage(1);
353: uport = argv[0];
354: host = NULL;
355: } else if (argv[0] && argv[1]) {
356: host = argv[0];
357: uport = argv[1];
358: } else
359: usage(1);
1.1 deraadt 360:
1.21 ericj 361: if (lflag && sflag)
362: errx(1, "cannot use -s and -l");
363: if (lflag && pflag)
364: errx(1, "cannot use -p and -l");
365: if (lflag && zflag)
1.32 ericj 366: errx(1, "cannot use -z and -l");
1.21 ericj 367: if (!lflag && kflag)
1.32 ericj 368: errx(1, "must use -l with -k");
1.133 beck 369: if (uflag && usetls)
370: errx(1, "cannot use -c and -u");
371: if ((family == AF_UNIX) && usetls)
372: errx(1, "cannot use -c and -U");
1.140 beck 373: if ((family == AF_UNIX) && Fflag)
374: errx(1, "cannot use -F and -U");
375: if (Fflag && usetls)
376: errx(1, "cannot use -c and -F");
1.133 beck 377: if (TLSopt && !usetls)
378: errx(1, "you must specify -c to use TLS options");
379: if (Cflag && !usetls)
380: errx(1, "you must specify -c to use -C");
381: if (Kflag && !usetls)
382: errx(1, "you must specify -c to use -K");
383: if (tls_cachanged && !usetls)
384: errx(1, "you must specify -c to use -R");
385: if (tls_expecthash && !usetls)
386: errx(1, "you must specify -c to use -H");
387: if (tls_expectname && !usetls)
388: errx(1, "you must specify -c to use -e");
1.21 ericj 389:
1.99 jeremy 390: /* Get name of temporary socket for unix datagram client */
391: if ((family == AF_UNIX) && uflag && !lflag) {
392: if (sflag) {
393: unix_dg_tmp_socket = sflag;
394: } else {
395: strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX",
1.136 deraadt 396: UNIX_DG_TMP_SOCKET_SIZE);
1.99 jeremy 397: if (mktemp(unix_dg_tmp_socket_buf) == NULL)
398: err(1, "mktemp");
399: unix_dg_tmp_socket = unix_dg_tmp_socket_buf;
400: }
401: }
402:
1.67 jmc 403: /* Initialize addrinfo structure. */
1.42 ericj 404: if (family != AF_UNIX) {
405: memset(&hints, 0, sizeof(struct addrinfo));
406: hints.ai_family = family;
407: hints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
408: hints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
409: if (nflag)
410: hints.ai_flags |= AI_NUMERICHOST;
411: }
1.1 deraadt 412:
1.34 jakob 413: if (xflag) {
414: if (uflag)
415: errx(1, "no proxy support for UDP mode");
416:
417: if (lflag)
418: errx(1, "no proxy support for listen");
419:
1.42 ericj 420: if (family == AF_UNIX)
421: errx(1, "no proxy support for unix sockets");
422:
1.34 jakob 423: /* XXX IPv6 transport to proxy would probably work */
424: if (family == AF_INET6)
425: errx(1, "no proxy support for IPv6");
426:
427: if (sflag)
428: errx(1, "no proxy support for local source address");
429:
430: proxyhost = strsep(&proxy, ":");
431: proxyport = proxy;
432:
433: memset(&proxyhints, 0, sizeof(struct addrinfo));
434: proxyhints.ai_family = family;
435: proxyhints.ai_socktype = SOCK_STREAM;
436: proxyhints.ai_protocol = IPPROTO_TCP;
437: if (nflag)
438: proxyhints.ai_flags |= AI_NUMERICHOST;
439: }
440:
1.133 beck 441: if (usetls) {
1.152 beck 442: if (Pflag) {
1.162 jsing 443: if (pledge("stdio inet dns tty rpath", NULL) == -1)
1.152 beck 444: err(1, "pledge");
1.162 jsing 445: } else if (pledge("stdio inet dns rpath", NULL) == -1)
1.146 beck 446: err(1, "pledge");
447:
1.133 beck 448: if (tls_init() == -1)
449: errx(1, "unable to initialize TLS");
450: if ((tls_cfg = tls_config_new()) == NULL)
451: errx(1, "unable to allocate TLS config");
1.162 jsing 452: if (Rflag && tls_config_set_ca_file(tls_cfg, Rflag) == -1)
453: errx(1, "%s", tls_config_error(tls_cfg));
454: if (Cflag && tls_config_set_cert_file(tls_cfg, Cflag) == -1)
455: errx(1, "%s", tls_config_error(tls_cfg));
456: if (Kflag && tls_config_set_key_file(tls_cfg, Kflag) == -1)
457: errx(1, "%s", tls_config_error(tls_cfg));
1.133 beck 458: if (TLSopt & TLS_LEGACY) {
459: tls_config_set_protocols(tls_cfg, TLS_PROTOCOLS_ALL);
1.160 jsing 460: tls_config_set_ciphers(tls_cfg, "all");
1.133 beck 461: }
462: if (!lflag && (TLSopt & TLS_CCERT))
463: errx(1, "clientcert is only valid with -l");
464: if (TLSopt & TLS_NONAME)
465: tls_config_insecure_noverifyname(tls_cfg);
466: if (TLSopt & TLS_NOVERIFY) {
467: if (tls_expecthash != NULL)
468: errx(1, "-H and -T noverify may not be used"
469: "together");
470: tls_config_insecure_noverifycert(tls_cfg);
471: }
1.167 ! beck 472: if (TLSopt & TLS_MUSTSTAPLE)
! 473: tls_config_ocsp_require_stapling(tls_cfg);
1.162 jsing 474:
475: if (Pflag) {
476: if (pledge("stdio inet dns tty", NULL) == -1)
477: err(1, "pledge");
478: } else if (pledge("stdio inet dns", NULL) == -1)
479: err(1, "pledge");
1.133 beck 480: }
1.21 ericj 481: if (lflag) {
1.133 beck 482: struct tls *tls_cctx = NULL;
1.21 ericj 483: int connfd;
1.27 ericj 484: ret = 0;
1.1 deraadt 485:
1.99 jeremy 486: if (family == AF_UNIX) {
487: if (uflag)
1.136 deraadt 488: s = unix_bind(host, 0);
1.99 jeremy 489: else
490: s = unix_listen(host);
491: }
1.42 ericj 492:
1.133 beck 493: if (usetls) {
494: tls_config_verify_client_optional(tls_cfg);
495: if ((tls_ctx = tls_server()) == NULL)
496: errx(1, "tls server creation failed");
497: if (tls_configure(tls_ctx, tls_cfg) == -1)
498: errx(1, "tls configuration failed (%s)",
499: tls_error(tls_ctx));
500: }
1.67 jmc 501: /* Allow only one connection at a time, but stay alive. */
1.21 ericj 502: for (;;) {
1.42 ericj 503: if (family != AF_UNIX)
504: s = local_listen(host, uport, hints);
505: if (s < 0)
1.30 smart 506: err(1, NULL);
1.21 ericj 507: /*
1.109 haesbaer 508: * For UDP and -k, don't connect the socket, let it
509: * receive datagrams from multiple socket pairs.
1.21 ericj 510: */
1.109 haesbaer 511: if (uflag && kflag)
1.133 beck 512: readwrite(s, NULL);
1.109 haesbaer 513: /*
514: * For UDP and not -k, we will use recvfrom() initially
515: * to wait for a caller, then use the regular functions
516: * to talk to the caller.
517: */
518: else if (uflag && !kflag) {
1.80 mcbride 519: int rv, plen;
1.97 nicm 520: char buf[16384];
1.21 ericj 521: struct sockaddr_storage z;
522:
523: len = sizeof(z);
1.106 dlg 524: plen = 2048;
1.80 mcbride 525: rv = recvfrom(s, buf, plen, MSG_PEEK,
1.37 jakob 526: (struct sockaddr *)&z, &len);
1.23 ericj 527: if (rv < 0)
1.57 stevesk 528: err(1, "recvfrom");
1.21 ericj 529:
1.37 jakob 530: rv = connect(s, (struct sockaddr *)&z, len);
1.23 ericj 531: if (rv < 0)
1.57 stevesk 532: err(1, "connect");
1.1 deraadt 533:
1.108 haesbaer 534: if (vflag)
1.151 beck 535: report_connect((struct sockaddr *)&z, len, NULL);
1.108 haesbaer 536:
1.133 beck 537: readwrite(s, NULL);
1.21 ericj 538: } else {
1.78 otto 539: len = sizeof(cliaddr);
1.132 bluhm 540: connfd = accept4(s, (struct sockaddr *)&cliaddr,
541: &len, SOCK_NONBLOCK);
1.110 deraadt 542: if (connfd == -1) {
543: /* For now, all errnos are fatal */
1.125 tedu 544: err(1, "accept");
1.110 deraadt 545: }
1.108 haesbaer 546: if (vflag)
1.151 beck 547: report_connect((struct sockaddr *)&cliaddr, len,
548: family == AF_UNIX ? host : NULL);
1.133 beck 549: if ((usetls) &&
1.134 deraadt 550: (tls_cctx = tls_setup_server(tls_ctx, connfd, host)))
1.133 beck 551: readwrite(connfd, tls_cctx);
552: if (!usetls)
553: readwrite(connfd, NULL);
554: if (tls_cctx) {
555: int i;
1.136 deraadt 556:
1.133 beck 557: do {
558: i = tls_close(tls_cctx);
559: } while (i == TLS_WANT_POLLIN ||
560: i == TLS_WANT_POLLOUT);
561: tls_free(tls_cctx);
562: tls_cctx = NULL;
563: }
1.99 jeremy 564: close(connfd);
1.21 ericj 565: }
1.42 ericj 566: if (family != AF_UNIX)
567: close(s);
1.99 jeremy 568: else if (uflag) {
569: if (connect(s, NULL, 0) < 0)
570: err(1, "connect");
571: }
1.27 ericj 572:
1.21 ericj 573: if (!kflag)
574: break;
1.11 ericj 575: }
1.42 ericj 576: } else if (family == AF_UNIX) {
577: ret = 0;
578:
579: if ((s = unix_connect(host)) > 0 && !zflag) {
1.133 beck 580: readwrite(s, NULL);
1.42 ericj 581: close(s);
582: } else
583: ret = 1;
584:
1.99 jeremy 585: if (uflag)
586: unlink(unix_dg_tmp_socket);
1.42 ericj 587: exit(ret);
588:
1.21 ericj 589: } else {
590: int i = 0;
1.6 deraadt 591:
1.67 jmc 592: /* Construct the portlist[] array. */
1.21 ericj 593: build_ports(uport);
1.1 deraadt 594:
1.67 jmc 595: /* Cycle through portlist, connecting to each port. */
1.154 deraadt 596: for (s = -1, i = 0; portlist[i] != NULL; i++) {
597: if (s != -1)
1.21 ericj 598: close(s);
1.34 jakob 599:
1.133 beck 600: if (usetls) {
601: if ((tls_ctx = tls_client()) == NULL)
602: errx(1, "tls client creation failed");
603: if (tls_configure(tls_ctx, tls_cfg) == -1)
604: errx(1, "tls configuration failed (%s)",
605: tls_error(tls_ctx));
606: }
1.34 jakob 607: if (xflag)
608: s = socks_connect(host, portlist[i], hints,
1.86 djm 609: proxyhost, proxyport, proxyhints, socksv,
610: Pflag);
1.34 jakob 611: else
612: s = remote_connect(host, portlist[i], hints);
613:
1.154 deraadt 614: if (s == -1)
1.21 ericj 615: continue;
1.1 deraadt 616:
1.21 ericj 617: ret = 0;
618: if (vflag || zflag) {
1.67 jmc 619: /* For UDP, make sure we are connected. */
1.21 ericj 620: if (uflag) {
1.50 vincent 621: if (udptest(s) == -1) {
1.21 ericj 622: ret = 1;
623: continue;
624: }
625: }
1.1 deraadt 626:
1.67 jmc 627: /* Don't look up port if -n. */
1.21 ericj 628: if (nflag)
629: sv = NULL;
630: else {
631: sv = getservbyport(
1.37 jakob 632: ntohs(atoi(portlist[i])),
633: uflag ? "udp" : "tcp");
1.21 ericj 634: }
1.50 vincent 635:
1.94 mpf 636: fprintf(stderr,
637: "Connection to %s %s port [%s/%s] "
638: "succeeded!\n", host, portlist[i],
639: uflag ? "udp" : "tcp",
1.37 jakob 640: sv ? sv->s_name : "*");
1.21 ericj 641: }
1.113 djm 642: if (Fflag)
643: fdpass(s);
1.133 beck 644: else {
645: if (usetls)
646: tls_setup_client(tls_ctx, s, host);
647: if (!zflag)
648: readwrite(s, tls_ctx);
649: if (tls_ctx) {
650: int j;
1.136 deraadt 651:
1.133 beck 652: do {
653: j = tls_close(tls_ctx);
654: } while (j == TLS_WANT_POLLIN ||
655: j == TLS_WANT_POLLOUT);
656: tls_free(tls_ctx);
657: tls_ctx = NULL;
658: }
659: }
1.7 deraadt 660: }
1.11 ericj 661: }
1.1 deraadt 662:
1.154 deraadt 663: if (s != -1)
1.21 ericj 664: close(s);
665:
1.133 beck 666: tls_config_free(tls_cfg);
667:
1.21 ericj 668: exit(ret);
1.7 deraadt 669: }
1.1 deraadt 670:
1.11 ericj 671: /*
1.99 jeremy 672: * unix_bind()
673: * Returns a unix socket bound to the given path
1.42 ericj 674: */
675: int
1.136 deraadt 676: unix_bind(char *path, int flags)
1.42 ericj 677: {
1.144 bcook 678: struct sockaddr_un s_un;
1.155 deraadt 679: int s, save_errno;
1.42 ericj 680:
1.99 jeremy 681: /* Create unix domain socket. */
1.136 deraadt 682: if ((s = socket(AF_UNIX, flags | (uflag ? SOCK_DGRAM : SOCK_STREAM),
683: 0)) < 0)
1.50 vincent 684: return (-1);
1.42 ericj 685:
1.144 bcook 686: memset(&s_un, 0, sizeof(struct sockaddr_un));
687: s_un.sun_family = AF_UNIX;
1.60 avsm 688:
1.144 bcook 689: if (strlcpy(s_un.sun_path, path, sizeof(s_un.sun_path)) >=
690: sizeof(s_un.sun_path)) {
1.60 avsm 691: close(s);
692: errno = ENAMETOOLONG;
693: return (-1);
694: }
1.99 jeremy 695:
1.144 bcook 696: if (bind(s, (struct sockaddr *)&s_un, sizeof(s_un)) < 0) {
1.155 deraadt 697: save_errno = errno;
1.50 vincent 698: close(s);
1.155 deraadt 699: errno = save_errno;
1.50 vincent 700: return (-1);
1.42 ericj 701: }
702: return (s);
703: }
704:
1.133 beck 705: void
706: tls_setup_client(struct tls *tls_ctx, int s, char *host)
707: {
708: int i;
1.136 deraadt 709:
1.133 beck 710: if (tls_connect_socket(tls_ctx, s,
711: tls_expectname ? tls_expectname : host) == -1) {
712: errx(1, "tls connection failed (%s)",
713: tls_error(tls_ctx));
714: }
715: do {
716: if ((i = tls_handshake(tls_ctx)) == -1)
717: errx(1, "tls handshake failed (%s)",
718: tls_error(tls_ctx));
719: } while (i == TLS_WANT_POLLIN || i == TLS_WANT_POLLOUT);
720: if (vflag)
721: report_tls(tls_ctx, host, tls_expectname);
1.137 beck 722: if (tls_expecthash && tls_peer_cert_hash(tls_ctx) &&
723: strcmp(tls_expecthash, tls_peer_cert_hash(tls_ctx)) != 0)
1.133 beck 724: errx(1, "peer certificate is not %s", tls_expecthash);
725: }
1.141 deraadt 726:
1.133 beck 727: struct tls *
728: tls_setup_server(struct tls *tls_ctx, int connfd, char *host)
729: {
730: struct tls *tls_cctx;
1.136 deraadt 731:
1.133 beck 732: if (tls_accept_socket(tls_ctx, &tls_cctx,
733: connfd) == -1) {
734: warnx("tls accept failed (%s)",
735: tls_error(tls_ctx));
736: tls_cctx = NULL;
737: } else {
738: int i;
1.136 deraadt 739:
1.133 beck 740: do {
741: if ((i = tls_handshake(tls_cctx)) == -1)
742: warnx("tls handshake failed (%s)",
743: tls_error(tls_cctx));
744: } while(i == TLS_WANT_POLLIN || i == TLS_WANT_POLLOUT);
745: }
746: if (tls_cctx) {
747: int gotcert = tls_peer_cert_provided(tls_cctx);
1.136 deraadt 748:
1.133 beck 749: if (vflag && gotcert)
750: report_tls(tls_cctx, host, tls_expectname);
751: if ((TLSopt & TLS_CCERT) && !gotcert)
752: warnx("No client certificate provided");
1.137 beck 753: else if (gotcert && tls_peer_cert_hash(tls_ctx) && tls_expecthash &&
754: strcmp(tls_expecthash, tls_peer_cert_hash(tls_ctx)) != 0)
1.133 beck 755: warnx("peer certificate is not %s", tls_expecthash);
756: else if (gotcert && tls_expectname &&
1.134 deraadt 757: (!tls_peer_cert_contains_name(tls_cctx, tls_expectname)))
1.133 beck 758: warnx("name (%s) not found in client cert",
759: tls_expectname);
760: else {
761: return tls_cctx;
762: }
763: }
764: return NULL;
765: }
1.141 deraadt 766:
1.42 ericj 767: /*
1.99 jeremy 768: * unix_connect()
769: * Returns a socket connected to a local unix socket. Returns -1 on failure.
1.42 ericj 770: */
771: int
1.99 jeremy 772: unix_connect(char *path)
1.42 ericj 773: {
1.144 bcook 774: struct sockaddr_un s_un;
1.155 deraadt 775: int s, save_errno;
1.42 ericj 776:
1.99 jeremy 777: if (uflag) {
1.136 deraadt 778: if ((s = unix_bind(unix_dg_tmp_socket, SOCK_CLOEXEC)) < 0)
1.99 jeremy 779: return (-1);
780: } else {
1.136 deraadt 781: if ((s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0)
1.99 jeremy 782: return (-1);
783: }
1.42 ericj 784:
1.144 bcook 785: memset(&s_un, 0, sizeof(struct sockaddr_un));
786: s_un.sun_family = AF_UNIX;
1.60 avsm 787:
1.144 bcook 788: if (strlcpy(s_un.sun_path, path, sizeof(s_un.sun_path)) >=
789: sizeof(s_un.sun_path)) {
1.60 avsm 790: close(s);
791: errno = ENAMETOOLONG;
792: return (-1);
793: }
1.144 bcook 794: if (connect(s, (struct sockaddr *)&s_un, sizeof(s_un)) < 0) {
1.155 deraadt 795: save_errno = errno;
1.42 ericj 796: close(s);
1.155 deraadt 797: errno = save_errno;
1.42 ericj 798: return (-1);
799: }
1.99 jeremy 800: return (s);
801:
802: }
803:
804: /*
805: * unix_listen()
806: * Create a unix domain socket, and listen on it.
807: */
808: int
809: unix_listen(char *path)
810: {
811: int s;
1.136 deraadt 812: if ((s = unix_bind(path, 0)) < 0)
1.99 jeremy 813: return (-1);
1.42 ericj 814:
815: if (listen(s, 5) < 0) {
816: close(s);
817: return (-1);
818: }
819: return (s);
820: }
821:
822: /*
1.21 ericj 823: * remote_connect()
1.67 jmc 824: * Returns a socket connected to a remote host. Properly binds to a local
825: * port or source address if needed. Returns -1 on failure.
1.11 ericj 826: */
1.21 ericj 827: int
1.77 otto 828: remote_connect(const char *host, const char *port, struct addrinfo hints)
1.21 ericj 829: {
830: struct addrinfo *res, *res0;
1.163 bcook 831: int s = -1, error, on = 1, save_errno;
1.21 ericj 832:
1.161 halex 833: if ((error = getaddrinfo(host, port, &hints, &res0)))
1.56 stevesk 834: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.21 ericj 835:
1.161 halex 836: for (res = res0; res; res = res->ai_next) {
837: if ((s = socket(res->ai_family, res->ai_socktype |
838: SOCK_NONBLOCK, res->ai_protocol)) < 0)
1.21 ericj 839: continue;
840:
1.67 jmc 841: /* Bind to a local port or source address if specified. */
1.21 ericj 842: if (sflag || pflag) {
843: struct addrinfo ahints, *ares;
1.6 deraadt 844:
1.91 markus 845: /* try SO_BINDANY, but don't insist */
846: setsockopt(s, SOL_SOCKET, SO_BINDANY, &on, sizeof(on));
1.21 ericj 847: memset(&ahints, 0, sizeof(struct addrinfo));
1.161 halex 848: ahints.ai_family = res->ai_family;
1.21 ericj 849: ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
850: ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
1.25 ericj 851: ahints.ai_flags = AI_PASSIVE;
1.38 jakob 852: if ((error = getaddrinfo(sflag, pflag, &ahints, &ares)))
1.56 stevesk 853: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.21 ericj 854:
855: if (bind(s, (struct sockaddr *)ares->ai_addr,
1.62 millert 856: ares->ai_addrlen) < 0)
1.119 guenther 857: err(1, "bind failed");
1.21 ericj 858: freeaddrinfo(ares);
1.6 deraadt 859: }
1.81 marius 860:
1.161 halex 861: set_common_sockopts(s, res->ai_family);
1.6 deraadt 862:
1.161 halex 863: if (timeout_connect(s, res->ai_addr, res->ai_addrlen) == 0)
1.6 deraadt 864: break;
1.143 deraadt 865: if (vflag)
1.71 mcbride 866: warn("connect to %s port %s (%s) failed", host, port,
867: uflag ? "udp" : "tcp");
1.34 jakob 868:
1.155 deraadt 869: save_errno = errno;
1.21 ericj 870: close(s);
1.155 deraadt 871: errno = save_errno;
1.21 ericj 872: s = -1;
1.161 halex 873: }
1.21 ericj 874:
1.161 halex 875: freeaddrinfo(res0);
1.1 deraadt 876:
1.21 ericj 877: return (s);
1.103 fgsch 878: }
879:
880: int
881: timeout_connect(int s, const struct sockaddr *name, socklen_t namelen)
882: {
883: struct pollfd pfd;
884: socklen_t optlen;
1.132 bluhm 885: int optval;
1.103 fgsch 886: int ret;
887:
888: if ((ret = connect(s, name, namelen)) != 0 && errno == EINPROGRESS) {
889: pfd.fd = s;
890: pfd.events = POLLOUT;
891: if ((ret = poll(&pfd, 1, timeout)) == 1) {
892: optlen = sizeof(optval);
893: if ((ret = getsockopt(s, SOL_SOCKET, SO_ERROR,
894: &optval, &optlen)) == 0) {
895: errno = optval;
896: ret = optval == 0 ? 0 : -1;
897: }
898: } else if (ret == 0) {
899: errno = ETIMEDOUT;
900: ret = -1;
901: } else
902: err(1, "poll failed");
903: }
904:
905: return (ret);
1.7 deraadt 906: }
1.1 deraadt 907:
1.11 ericj 908: /*
1.21 ericj 909: * local_listen()
1.67 jmc 910: * Returns a socket listening on a local port, binds to specified source
911: * address. Returns -1 on failure.
1.11 ericj 912: */
1.21 ericj 913: int
1.37 jakob 914: local_listen(char *host, char *port, struct addrinfo hints)
1.21 ericj 915: {
916: struct addrinfo *res, *res0;
1.163 bcook 917: int s = -1, ret, x = 1, save_errno;
1.21 ericj 918: int error;
1.6 deraadt 919:
1.67 jmc 920: /* Allow nodename to be null. */
1.21 ericj 921: hints.ai_flags |= AI_PASSIVE;
1.7 deraadt 922:
1.21 ericj 923: /*
924: * In the case of binding to a wildcard address
925: * default to binding to an ipv4 address.
926: */
927: if (host == NULL && hints.ai_family == AF_UNSPEC)
928: hints.ai_family = AF_INET;
1.1 deraadt 929:
1.161 halex 930: if ((error = getaddrinfo(host, port, &hints, &res0)))
1.70 deraadt 931: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.14 ericj 932:
1.161 halex 933: for (res = res0; res; res = res->ai_next) {
934: if ((s = socket(res->ai_family, res->ai_socktype,
935: res->ai_protocol)) < 0)
1.21 ericj 936: continue;
1.93 claudio 937:
1.21 ericj 938: ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
939: if (ret == -1)
1.30 smart 940: err(1, NULL);
1.81 marius 941:
1.161 halex 942: set_common_sockopts(s, res->ai_family);
1.1 deraadt 943:
1.161 halex 944: if (bind(s, (struct sockaddr *)res->ai_addr,
945: res->ai_addrlen) == 0)
1.21 ericj 946: break;
1.1 deraadt 947:
1.155 deraadt 948: save_errno = errno;
1.21 ericj 949: close(s);
1.155 deraadt 950: errno = save_errno;
1.21 ericj 951: s = -1;
1.161 halex 952: }
1.1 deraadt 953:
1.47 ericj 954: if (!uflag && s != -1) {
1.21 ericj 955: if (listen(s, 1) < 0)
1.57 stevesk 956: err(1, "listen");
1.12 ericj 957: }
1.1 deraadt 958:
1.161 halex 959: freeaddrinfo(res0);
1.1 deraadt 960:
1.21 ericj 961: return (s);
1.7 deraadt 962: }
963:
1.11 ericj 964: /*
1.21 ericj 965: * readwrite()
966: * Loop that polls on the network file descriptor and stdin.
1.11 ericj 967: */
1.21 ericj 968: void
1.133 beck 969: readwrite(int net_fd, struct tls *tls_ctx)
1.6 deraadt 970: {
1.125 tedu 971: struct pollfd pfd[4];
972: int stdin_fd = STDIN_FILENO;
973: int stdout_fd = STDOUT_FILENO;
974: unsigned char netinbuf[BUFSIZE];
975: size_t netinbufpos = 0;
976: unsigned char stdinbuf[BUFSIZE];
977: size_t stdinbufpos = 0;
1.130 chl 978: int n, num_fds;
1.125 tedu 979: ssize_t ret;
980:
981: /* don't read from stdin if requested */
982: if (dflag)
983: stdin_fd = -1;
984:
985: /* stdin */
986: pfd[POLL_STDIN].fd = stdin_fd;
987: pfd[POLL_STDIN].events = POLLIN;
988:
989: /* network out */
990: pfd[POLL_NETOUT].fd = net_fd;
991: pfd[POLL_NETOUT].events = 0;
992:
993: /* network in */
994: pfd[POLL_NETIN].fd = net_fd;
995: pfd[POLL_NETIN].events = POLLIN;
996:
997: /* stdout */
998: pfd[POLL_STDOUT].fd = stdout_fd;
999: pfd[POLL_STDOUT].events = 0;
1000:
1001: while (1) {
1002: /* both inputs are gone, buffers are empty, we are done */
1.134 deraadt 1003: if (pfd[POLL_STDIN].fd == -1 && pfd[POLL_NETIN].fd == -1 &&
1004: stdinbufpos == 0 && netinbufpos == 0) {
1.125 tedu 1005: close(net_fd);
1006: return;
1007: }
1008: /* both outputs are gone, we can't continue */
1009: if (pfd[POLL_NETOUT].fd == -1 && pfd[POLL_STDOUT].fd == -1) {
1010: close(net_fd);
1011: return;
1012: }
1013: /* listen and net in gone, queues empty, done */
1.134 deraadt 1014: if (lflag && pfd[POLL_NETIN].fd == -1 &&
1015: stdinbufpos == 0 && netinbufpos == 0) {
1.125 tedu 1016: close(net_fd);
1017: return;
1018: }
1.21 ericj 1019:
1.125 tedu 1020: /* help says -i is for "wait between lines sent". We read and
1021: * write arbitrary amounts of data, and we don't want to start
1022: * scanning for newlines, so this is as good as it gets */
1.21 ericj 1023: if (iflag)
1024: sleep(iflag);
1025:
1.125 tedu 1026: /* poll */
1027: num_fds = poll(pfd, 4, timeout);
1028:
1029: /* treat poll errors */
1030: if (num_fds == -1) {
1031: close(net_fd);
1032: err(1, "polling error");
1.21 ericj 1033: }
1.49 hugh 1034:
1.125 tedu 1035: /* timeout happened */
1036: if (num_fds == 0)
1.49 hugh 1037: return;
1.21 ericj 1038:
1.125 tedu 1039: /* treat socket error conditions */
1040: for (n = 0; n < 4; n++) {
1041: if (pfd[n].revents & (POLLERR|POLLNVAL)) {
1042: pfd[n].fd = -1;
1.6 deraadt 1043: }
1.21 ericj 1044: }
1.125 tedu 1045: /* reading is possible after HUP */
1046: if (pfd[POLL_STDIN].events & POLLIN &&
1047: pfd[POLL_STDIN].revents & POLLHUP &&
1.134 deraadt 1048: !(pfd[POLL_STDIN].revents & POLLIN))
1049: pfd[POLL_STDIN].fd = -1;
1.125 tedu 1050:
1051: if (pfd[POLL_NETIN].events & POLLIN &&
1052: pfd[POLL_NETIN].revents & POLLHUP &&
1.134 deraadt 1053: !(pfd[POLL_NETIN].revents & POLLIN))
1054: pfd[POLL_NETIN].fd = -1;
1.125 tedu 1055:
1056: if (pfd[POLL_NETOUT].revents & POLLHUP) {
1057: if (Nflag)
1058: shutdown(pfd[POLL_NETOUT].fd, SHUT_WR);
1059: pfd[POLL_NETOUT].fd = -1;
1060: }
1061: /* if HUP, stop watching stdout */
1062: if (pfd[POLL_STDOUT].revents & POLLHUP)
1063: pfd[POLL_STDOUT].fd = -1;
1064: /* if no net out, stop watching stdin */
1065: if (pfd[POLL_NETOUT].fd == -1)
1066: pfd[POLL_STDIN].fd = -1;
1067: /* if no stdout, stop watching net in */
1068: if (pfd[POLL_STDOUT].fd == -1) {
1069: if (pfd[POLL_NETIN].fd != -1)
1070: shutdown(pfd[POLL_NETIN].fd, SHUT_RD);
1071: pfd[POLL_NETIN].fd = -1;
1072: }
1.21 ericj 1073:
1.125 tedu 1074: /* try to read from stdin */
1075: if (pfd[POLL_STDIN].revents & POLLIN && stdinbufpos < BUFSIZE) {
1076: ret = fillbuf(pfd[POLL_STDIN].fd, stdinbuf,
1.133 beck 1077: &stdinbufpos, NULL);
1078: if (ret == TLS_WANT_POLLIN)
1079: pfd[POLL_STDIN].events = POLLIN;
1080: else if (ret == TLS_WANT_POLLOUT)
1081: pfd[POLL_STDIN].events = POLLOUT;
1082: else if (ret == 0 || ret == -1)
1.125 tedu 1083: pfd[POLL_STDIN].fd = -1;
1084: /* read something - poll net out */
1085: if (stdinbufpos > 0)
1086: pfd[POLL_NETOUT].events = POLLOUT;
1087: /* filled buffer - remove self from polling */
1088: if (stdinbufpos == BUFSIZE)
1089: pfd[POLL_STDIN].events = 0;
1090: }
1091: /* try to write to network */
1092: if (pfd[POLL_NETOUT].revents & POLLOUT && stdinbufpos > 0) {
1093: ret = drainbuf(pfd[POLL_NETOUT].fd, stdinbuf,
1.133 beck 1094: &stdinbufpos, tls_ctx);
1095: if (ret == TLS_WANT_POLLIN)
1096: pfd[POLL_NETOUT].events = POLLIN;
1097: else if (ret == TLS_WANT_POLLOUT)
1098: pfd[POLL_NETOUT].events = POLLOUT;
1099: else if (ret == -1)
1.125 tedu 1100: pfd[POLL_NETOUT].fd = -1;
1101: /* buffer empty - remove self from polling */
1102: if (stdinbufpos == 0)
1103: pfd[POLL_NETOUT].events = 0;
1104: /* buffer no longer full - poll stdin again */
1105: if (stdinbufpos < BUFSIZE)
1106: pfd[POLL_STDIN].events = POLLIN;
1107: }
1108: /* try to read from network */
1109: if (pfd[POLL_NETIN].revents & POLLIN && netinbufpos < BUFSIZE) {
1110: ret = fillbuf(pfd[POLL_NETIN].fd, netinbuf,
1.133 beck 1111: &netinbufpos, tls_ctx);
1112: if (ret == TLS_WANT_POLLIN)
1113: pfd[POLL_NETIN].events = POLLIN;
1114: else if (ret == TLS_WANT_POLLOUT)
1115: pfd[POLL_NETIN].events = POLLOUT;
1116: else if (ret == -1)
1.125 tedu 1117: pfd[POLL_NETIN].fd = -1;
1118: /* eof on net in - remove from pfd */
1119: if (ret == 0) {
1120: shutdown(pfd[POLL_NETIN].fd, SHUT_RD);
1121: pfd[POLL_NETIN].fd = -1;
1.50 vincent 1122: }
1.125 tedu 1123: /* read something - poll stdout */
1124: if (netinbufpos > 0)
1125: pfd[POLL_STDOUT].events = POLLOUT;
1126: /* filled buffer - remove self from polling */
1127: if (netinbufpos == BUFSIZE)
1128: pfd[POLL_NETIN].events = 0;
1129: /* handle telnet */
1130: if (tflag)
1131: atelnet(pfd[POLL_NETIN].fd, netinbuf,
1132: netinbufpos);
1133: }
1134: /* try to write to stdout */
1135: if (pfd[POLL_STDOUT].revents & POLLOUT && netinbufpos > 0) {
1136: ret = drainbuf(pfd[POLL_STDOUT].fd, netinbuf,
1.133 beck 1137: &netinbufpos, NULL);
1138: if (ret == TLS_WANT_POLLIN)
1139: pfd[POLL_STDOUT].events = POLLIN;
1140: else if (ret == TLS_WANT_POLLOUT)
1141: pfd[POLL_STDOUT].events = POLLOUT;
1142: else if (ret == -1)
1.125 tedu 1143: pfd[POLL_STDOUT].fd = -1;
1144: /* buffer empty - remove self from polling */
1145: if (netinbufpos == 0)
1146: pfd[POLL_STDOUT].events = 0;
1147: /* buffer no longer full - poll net in again */
1148: if (netinbufpos < BUFSIZE)
1149: pfd[POLL_NETIN].events = POLLIN;
1150: }
1151:
1152: /* stdin gone and queue empty? */
1153: if (pfd[POLL_STDIN].fd == -1 && stdinbufpos == 0) {
1154: if (pfd[POLL_NETOUT].fd != -1 && Nflag)
1155: shutdown(pfd[POLL_NETOUT].fd, SHUT_WR);
1156: pfd[POLL_NETOUT].fd = -1;
1157: }
1158: /* net in gone and queue empty? */
1159: if (pfd[POLL_NETIN].fd == -1 && netinbufpos == 0) {
1160: pfd[POLL_STDOUT].fd = -1;
1.21 ericj 1161: }
1.11 ericj 1162: }
1.125 tedu 1163: }
1164:
1165: ssize_t
1.133 beck 1166: drainbuf(int fd, unsigned char *buf, size_t *bufpos, struct tls *tls)
1.125 tedu 1167: {
1168: ssize_t n;
1169: ssize_t adjust;
1170:
1.133 beck 1171: if (tls)
1172: n = tls_write(tls, buf, *bufpos);
1173: else {
1174: n = write(fd, buf, *bufpos);
1175: /* don't treat EAGAIN, EINTR as error */
1176: if (n == -1 && (errno == EAGAIN || errno == EINTR))
1177: n = TLS_WANT_POLLOUT;
1178: }
1.125 tedu 1179: if (n <= 0)
1180: return n;
1181: /* adjust buffer */
1182: adjust = *bufpos - n;
1183: if (adjust > 0)
1184: memmove(buf, buf + n, adjust);
1185: *bufpos -= n;
1186: return n;
1187: }
1188:
1189: ssize_t
1.133 beck 1190: fillbuf(int fd, unsigned char *buf, size_t *bufpos, struct tls *tls)
1.125 tedu 1191: {
1192: size_t num = BUFSIZE - *bufpos;
1193: ssize_t n;
1194:
1.133 beck 1195: if (tls)
1196: n = tls_read(tls, buf + *bufpos, num);
1197: else {
1198: n = read(fd, buf + *bufpos, num);
1199: /* don't treat EAGAIN, EINTR as error */
1200: if (n == -1 && (errno == EAGAIN || errno == EINTR))
1201: n = TLS_WANT_POLLIN;
1202: }
1.125 tedu 1203: if (n <= 0)
1204: return n;
1205: *bufpos += n;
1206: return n;
1.113 djm 1207: }
1208:
1209: /*
1210: * fdpass()
1211: * Pass the connected file descriptor to stdout and exit.
1212: */
1213: void
1214: fdpass(int nfd)
1215: {
1216: struct msghdr mh;
1217: union {
1218: struct cmsghdr hdr;
1219: char buf[CMSG_SPACE(sizeof(int))];
1220: } cmsgbuf;
1221: struct cmsghdr *cmsg;
1222: struct iovec iov;
1223: char c = '\0';
1224: ssize_t r;
1225: struct pollfd pfd;
1226:
1227: /* Avoid obvious stupidity */
1228: if (isatty(STDOUT_FILENO))
1229: errx(1, "Cannot pass file descriptor to tty");
1230:
1231: bzero(&mh, sizeof(mh));
1232: bzero(&cmsgbuf, sizeof(cmsgbuf));
1233: bzero(&iov, sizeof(iov));
1234:
1235: mh.msg_control = (caddr_t)&cmsgbuf.buf;
1236: mh.msg_controllen = sizeof(cmsgbuf.buf);
1237: cmsg = CMSG_FIRSTHDR(&mh);
1238: cmsg->cmsg_len = CMSG_LEN(sizeof(int));
1239: cmsg->cmsg_level = SOL_SOCKET;
1240: cmsg->cmsg_type = SCM_RIGHTS;
1241: *(int *)CMSG_DATA(cmsg) = nfd;
1242:
1243: iov.iov_base = &c;
1244: iov.iov_len = 1;
1245: mh.msg_iov = &iov;
1246: mh.msg_iovlen = 1;
1247:
1248: bzero(&pfd, sizeof(pfd));
1249: pfd.fd = STDOUT_FILENO;
1.128 tobias 1250: pfd.events = POLLOUT;
1.113 djm 1251: for (;;) {
1252: r = sendmsg(STDOUT_FILENO, &mh, 0);
1253: if (r == -1) {
1254: if (errno == EAGAIN || errno == EINTR) {
1255: if (poll(&pfd, 1, -1) == -1)
1256: err(1, "poll");
1257: continue;
1258: }
1259: err(1, "sendmsg");
1.128 tobias 1260: } else if (r != 1)
1.113 djm 1261: errx(1, "sendmsg: unexpected return value %zd", r);
1262: else
1263: break;
1264: }
1265: exit(0);
1.7 deraadt 1266: }
1.50 vincent 1267:
1.67 jmc 1268: /* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */
1.21 ericj 1269: void
1.37 jakob 1270: atelnet(int nfd, unsigned char *buf, unsigned int size)
1.6 deraadt 1271: {
1.24 ericj 1272: unsigned char *p, *end;
1273: unsigned char obuf[4];
1274:
1.95 nicm 1275: if (size < 3)
1276: return;
1277: end = buf + size - 2;
1.24 ericj 1278:
1279: for (p = buf; p < end; p++) {
1.21 ericj 1280: if (*p != IAC)
1.95 nicm 1281: continue;
1.24 ericj 1282:
1.25 ericj 1283: obuf[0] = IAC;
1.24 ericj 1284: p++;
1.50 vincent 1285: if ((*p == WILL) || (*p == WONT))
1.24 ericj 1286: obuf[1] = DONT;
1.95 nicm 1287: else if ((*p == DO) || (*p == DONT))
1.24 ericj 1288: obuf[1] = WONT;
1.95 nicm 1289: else
1290: continue;
1291:
1292: p++;
1293: obuf[2] = *p;
1294: if (atomicio(vwrite, nfd, obuf, 3) != 3)
1295: warn("Write Error!");
1.11 ericj 1296: }
1.7 deraadt 1297: }
1298:
1.153 beck 1299:
1300: int
1301: strtoport(char *portstr, int udp)
1302: {
1303: struct servent *entry;
1304: const char *errstr;
1305: char *proto;
1306: int port = -1;
1307:
1308: proto = udp ? "udp" : "tcp";
1309:
1310: port = strtonum(portstr, 1, PORT_MAX, &errstr);
1311: if (errstr == NULL)
1312: return port;
1313: if (errno != EINVAL)
1314: errx(1, "port number %s: %s", errstr, portstr);
1315: if ((entry = getservbyname(portstr, proto)) == NULL)
1316: errx(1, "service \"%s\" unknown", portstr);
1317: return ntohs(entry->s_port);
1318: }
1319:
1.11 ericj 1320: /*
1.21 ericj 1321: * build_ports()
1.105 lum 1322: * Build an array of ports in portlist[], listing each port
1.67 jmc 1323: * that we should try to connect to.
1.11 ericj 1324: */
1.21 ericj 1325: void
1.37 jakob 1326: build_ports(char *p)
1.6 deraadt 1327: {
1.88 ray 1328: char *n;
1.21 ericj 1329: int hi, lo, cp;
1330: int x = 0;
1331:
1332: if ((n = strchr(p, '-')) != NULL) {
1333: *n = '\0';
1334: n++;
1335:
1.67 jmc 1336: /* Make sure the ports are in order: lowest->highest. */
1.153 beck 1337: hi = strtoport(n, uflag);
1338: lo = strtoport(p, uflag);
1.21 ericj 1339: if (lo > hi) {
1340: cp = hi;
1341: hi = lo;
1342: lo = cp;
1343: }
1344:
1.145 tb 1345: /*
1346: * Initialize portlist with a random permutation. Based on
1347: * Knuth, as in ip_randomid() in sys/netinet/ip_id.c.
1348: */
1.21 ericj 1349: if (rflag) {
1.145 tb 1350: for (x = 0; x <= hi - lo; x++) {
1351: cp = arc4random_uniform(x + 1);
1352: portlist[x] = portlist[cp];
1353: if (asprintf(&portlist[cp], "%d", x + lo) < 0)
1354: err(1, "asprintf");
1355: }
1356: } else { /* Load ports sequentially. */
1357: for (cp = lo; cp <= hi; cp++) {
1358: if (asprintf(&portlist[x], "%d", cp) < 0)
1359: err(1, "asprintf");
1360: x++;
1.6 deraadt 1361: }
1.11 ericj 1362: }
1.21 ericj 1363: } else {
1.153 beck 1364: char *tmp;
1365:
1366: hi = strtoport(p, uflag);
1367: if (asprintf(&tmp, "%d", hi) != -1)
1368: portlist[0] = tmp;
1369: else
1.55 fgsch 1370: err(1, NULL);
1.11 ericj 1371: }
1.13 ericj 1372: }
1373:
1374: /*
1.21 ericj 1375: * udptest()
1376: * Do a few writes to see if the UDP port is there.
1.105 lum 1377: * Fails once PF state table is full.
1.13 ericj 1378: */
1.21 ericj 1379: int
1.37 jakob 1380: udptest(int s)
1.13 ericj 1381: {
1.74 deraadt 1382: int i, ret;
1.13 ericj 1383:
1.52 vincent 1384: for (i = 0; i <= 3; i++) {
1.74 deraadt 1385: if (write(s, "X", 1) == 1)
1.21 ericj 1386: ret = 1;
1.14 ericj 1387: else
1.21 ericj 1388: ret = -1;
1.14 ericj 1389: }
1.21 ericj 1390: return (ret);
1.81 marius 1391: }
1392:
1.84 dtucker 1393: void
1.127 jca 1394: set_common_sockopts(int s, int af)
1.81 marius 1395: {
1396: int x = 1;
1397:
1398: if (Sflag) {
1399: if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
1400: &x, sizeof(x)) == -1)
1401: err(1, NULL);
1402: }
1403: if (Dflag) {
1404: if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
1405: &x, sizeof(x)) == -1)
1406: err(1, NULL);
1407: }
1.83 dtucker 1408: if (Tflag != -1) {
1.157 bcook 1409: if (af == AF_INET && setsockopt(s, IPPROTO_IP,
1410: IP_TOS, &Tflag, sizeof(Tflag)) == -1)
1411: err(1, "set IP ToS");
1.127 jca 1412:
1.157 bcook 1413: else if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
1414: IPV6_TCLASS, &Tflag, sizeof(Tflag)) == -1)
1415: err(1, "set IPv6 traffic class");
1.83 dtucker 1416: }
1.90 djm 1417: if (Iflag) {
1418: if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
1419: &Iflag, sizeof(Iflag)) == -1)
1420: err(1, "set TCP receive buffer size");
1421: }
1422: if (Oflag) {
1423: if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
1424: &Oflag, sizeof(Oflag)) == -1)
1425: err(1, "set TCP send buffer size");
1426: }
1.157 bcook 1427:
1428: if (ttl != -1) {
1429: if (af == AF_INET && setsockopt(s, IPPROTO_IP,
1430: IP_TTL, &ttl, sizeof(ttl)))
1431: err(1, "set IP TTL");
1432:
1433: else if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
1434: IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)))
1435: err(1, "set IPv6 unicast hops");
1436: }
1437:
1438: if (minttl != -1) {
1439: if (af == AF_INET && setsockopt(s, IPPROTO_IP,
1440: IP_MINTTL, &minttl, sizeof(minttl)))
1441: err(1, "set IP min TTL");
1442:
1443: else if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
1444: IPV6_MINHOPCOUNT, &minttl, sizeof(minttl)))
1445: err(1, "set IPv6 min hop count");
1.156 jca 1446: }
1.83 dtucker 1447: }
1448:
1449: int
1.102 haesbaer 1450: map_tos(char *s, int *val)
1.83 dtucker 1451: {
1.102 haesbaer 1452: /* DiffServ Codepoints and other TOS mappings */
1453: const struct toskeywords {
1454: const char *keyword;
1455: int val;
1456: } *t, toskeywords[] = {
1457: { "af11", IPTOS_DSCP_AF11 },
1458: { "af12", IPTOS_DSCP_AF12 },
1459: { "af13", IPTOS_DSCP_AF13 },
1460: { "af21", IPTOS_DSCP_AF21 },
1461: { "af22", IPTOS_DSCP_AF22 },
1462: { "af23", IPTOS_DSCP_AF23 },
1463: { "af31", IPTOS_DSCP_AF31 },
1464: { "af32", IPTOS_DSCP_AF32 },
1465: { "af33", IPTOS_DSCP_AF33 },
1466: { "af41", IPTOS_DSCP_AF41 },
1467: { "af42", IPTOS_DSCP_AF42 },
1468: { "af43", IPTOS_DSCP_AF43 },
1469: { "critical", IPTOS_PREC_CRITIC_ECP },
1470: { "cs0", IPTOS_DSCP_CS0 },
1471: { "cs1", IPTOS_DSCP_CS1 },
1472: { "cs2", IPTOS_DSCP_CS2 },
1473: { "cs3", IPTOS_DSCP_CS3 },
1474: { "cs4", IPTOS_DSCP_CS4 },
1475: { "cs5", IPTOS_DSCP_CS5 },
1476: { "cs6", IPTOS_DSCP_CS6 },
1477: { "cs7", IPTOS_DSCP_CS7 },
1478: { "ef", IPTOS_DSCP_EF },
1479: { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
1480: { "lowdelay", IPTOS_LOWDELAY },
1481: { "netcontrol", IPTOS_PREC_NETCONTROL },
1482: { "reliability", IPTOS_RELIABILITY },
1483: { "throughput", IPTOS_THROUGHPUT },
1.134 deraadt 1484: { NULL, -1 },
1.102 haesbaer 1485: };
1486:
1487: for (t = toskeywords; t->keyword != NULL; t++) {
1488: if (strcmp(s, t->keyword) == 0) {
1489: *val = t->val;
1490: return (1);
1491: }
1492: }
1.83 dtucker 1493:
1.102 haesbaer 1494: return (0);
1.108 haesbaer 1495: }
1496:
1.133 beck 1497: int
1498: map_tls(char *s, int *val)
1499: {
1500: const struct tlskeywords {
1501: const char *keyword;
1502: int val;
1503: } *t, tlskeywords[] = {
1504: { "tlslegacy", TLS_LEGACY },
1505: { "noverify", TLS_NOVERIFY },
1506: { "noname", TLS_NONAME },
1507: { "clientcert", TLS_CCERT},
1.167 ! beck 1508: { "muststaple", TLS_MUSTSTAPLE},
1.134 deraadt 1509: { NULL, -1 },
1.133 beck 1510: };
1511:
1512: for (t = tlskeywords; t->keyword != NULL; t++) {
1513: if (strcmp(s, t->keyword) == 0) {
1514: *val |= t->val;
1515: return (1);
1516: }
1517: }
1518: return (0);
1519: }
1520:
1521: void
1522: report_tls(struct tls * tls_ctx, char * host, char *tls_expectname)
1523: {
1.147 beck 1524: time_t t;
1.164 beck 1525: const char *ocsp_url;
1526:
1.138 beck 1527: fprintf(stderr, "TLS handshake negotiated %s/%s with host %s\n",
1528: tls_conn_version(tls_ctx), tls_conn_cipher(tls_ctx), host);
1.148 mmcc 1529: fprintf(stderr, "Peer name: %s\n",
1.133 beck 1530: tls_expectname ? tls_expectname : host);
1.137 beck 1531: if (tls_peer_cert_subject(tls_ctx))
1532: fprintf(stderr, "Subject: %s\n",
1533: tls_peer_cert_subject(tls_ctx));
1534: if (tls_peer_cert_issuer(tls_ctx))
1535: fprintf(stderr, "Issuer: %s\n",
1536: tls_peer_cert_issuer(tls_ctx));
1.147 beck 1537: if ((t = tls_peer_cert_notbefore(tls_ctx)) != -1)
1538: fprintf(stderr, "Valid From: %s", ctime(&t));
1539: if ((t = tls_peer_cert_notafter(tls_ctx)) != -1)
1540: fprintf(stderr, "Valid Until: %s", ctime(&t));
1.137 beck 1541: if (tls_peer_cert_hash(tls_ctx))
1542: fprintf(stderr, "Cert Hash: %s\n",
1543: tls_peer_cert_hash(tls_ctx));
1.164 beck 1544: ocsp_url = tls_peer_ocsp_url(tls_ctx);
1.166 beck 1545: if (ocsp_url != NULL)
1546: fprintf(stderr, "OCSP URL: %s\n", ocsp_url);
1.164 beck 1547: switch (tls_peer_ocsp_response_status(tls_ctx)) {
1548: case TLS_OCSP_RESPONSE_SUCCESSFUL:
1.165 beck 1549: fprintf(stderr, "OCSP Stapling: %s\n",
1.164 beck 1550: tls_peer_ocsp_result(tls_ctx) == NULL ? "" :
1551: tls_peer_ocsp_result(tls_ctx));
1552: fprintf(stderr,
1553: " response_status=%d cert_status=%d crl_reason=%d\n",
1554: tls_peer_ocsp_response_status(tls_ctx),
1555: tls_peer_ocsp_cert_status(tls_ctx),
1556: tls_peer_ocsp_crl_reason(tls_ctx));
1557: t = tls_peer_ocsp_this_update(tls_ctx);
1558: fprintf(stderr, " this update: %s",
1559: t != -1 ? ctime(&t) : "\n");
1560: t = tls_peer_ocsp_next_update(tls_ctx);
1561: fprintf(stderr, " next update: %s",
1562: t != -1 ? ctime(&t) : "\n");
1563: t = tls_peer_ocsp_revocation_time(tls_ctx);
1564: fprintf(stderr, " revocation: %s",
1565: t != -1 ? ctime(&t) : "\n");
1566: break;
1567: case -1:
1568: break;
1569: default:
1.165 beck 1570: fprintf(stderr, "OCSP Stapling: failure - response_status %d (%s)\n",
1.164 beck 1571: tls_peer_ocsp_response_status(tls_ctx),
1572: tls_peer_ocsp_result(tls_ctx) == NULL ? "" :
1573: tls_peer_ocsp_result(tls_ctx));
1574: break;
1575:
1576: }
1.133 beck 1577: }
1.147 beck 1578:
1.108 haesbaer 1579: void
1.151 beck 1580: report_connect(const struct sockaddr *sa, socklen_t salen, char *path)
1.108 haesbaer 1581: {
1582: char remote_host[NI_MAXHOST];
1583: char remote_port[NI_MAXSERV];
1584: int herr;
1585: int flags = NI_NUMERICSERV;
1.151 beck 1586:
1587: if (path != NULL) {
1588: fprintf(stderr, "Connection on %s received!\n", path);
1589: return;
1590: }
1.134 deraadt 1591:
1.108 haesbaer 1592: if (nflag)
1593: flags |= NI_NUMERICHOST;
1.134 deraadt 1594:
1.108 haesbaer 1595: if ((herr = getnameinfo(sa, salen,
1596: remote_host, sizeof(remote_host),
1597: remote_port, sizeof(remote_port),
1598: flags)) != 0) {
1599: if (herr == EAI_SYSTEM)
1600: err(1, "getnameinfo");
1601: else
1602: errx(1, "getnameinfo: %s", gai_strerror(herr));
1603: }
1.134 deraadt 1604:
1.108 haesbaer 1605: fprintf(stderr,
1606: "Connection from %s %s "
1607: "received!\n", remote_host, remote_port);
1.7 deraadt 1608: }
1.1 deraadt 1609:
1.11 ericj 1610: void
1.58 deraadt 1611: help(void)
1.1 deraadt 1612: {
1.21 ericj 1613: usage(0);
1614: fprintf(stderr, "\tCommand Summary:\n\
1615: \t-4 Use IPv4\n\
1616: \t-6 Use IPv6\n\
1.135 jmc 1617: \t-C certfile Public key file\n\
1618: \t-c Use TLS\n\
1.73 markus 1619: \t-D Enable the debug socket option\n\
1.69 tedu 1620: \t-d Detach from stdin\n\
1.135 jmc 1621: \t-e name\t Required name in peer certificate\n\
1.114 jmc 1622: \t-F Pass socket fd\n\
1.135 jmc 1623: \t-H hash\t Hash string of peer certificate\n\
1.21 ericj 1624: \t-h This help text\n\
1.90 djm 1625: \t-I length TCP receive buffer length\n\
1.135 jmc 1626: \t-i interval Delay interval for lines sent, ports scanned\n\
1627: \t-K keyfile Private key file\n\
1.21 ericj 1628: \t-k Keep inbound sockets open for multiple connects\n\
1629: \t-l Listen mode, for inbound connects\n\
1.156 jca 1630: \t-M ttl Outgoing TTL / Hop Limit\n\
1631: \t-m minttl Minimum incoming TTL / Hop Limit\n\
1.111 sthen 1632: \t-N Shutdown the network socket after EOF on stdin\n\
1.22 jasoni 1633: \t-n Suppress name/port resolutions\n\
1.90 djm 1634: \t-O length TCP send buffer length\n\
1.86 djm 1635: \t-P proxyuser\tUsername for proxy authentication\n\
1.36 jakob 1636: \t-p port\t Specify local port for remote connects\n\
1.135 jmc 1637: \t-R CAfile CA bundle\n\
1.21 ericj 1638: \t-r Randomize remote ports\n\
1.67 jmc 1639: \t-S Enable the TCP MD5 signature option\n\
1.135 jmc 1640: \t-s source Local source address\n\
1641: \t-T keyword TOS value or TLS options\n\
1.21 ericj 1642: \t-t Answer TELNET negotiation\n\
1.67 jmc 1643: \t-U Use UNIX domain socket\n\
1.21 ericj 1644: \t-u UDP mode\n\
1.98 guenther 1645: \t-V rtable Specify alternate routing table\n\
1.21 ericj 1646: \t-v Verbose\n\
1.135 jmc 1647: \t-w timeout Timeout for connects and final net reads\n\
1.75 djm 1648: \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\
1649: \t-x addr[:port]\tSpecify proxy address and port\n\
1.21 ericj 1650: \t-z Zero-I/O mode [used for scanning]\n\
1651: Port numbers can be individual or ranges: lo-hi [inclusive]\n");
1652: exit(1);
1.11 ericj 1653: }
1654:
1655: void
1.37 jakob 1656: usage(int ret)
1.11 ericj 1657: {
1.92 sobrado 1658: fprintf(stderr,
1.135 jmc 1659: "usage: nc [-46cDdFhklNnrStUuvz] [-C certfile] [-e name] "
1660: "[-H hash] [-I length]\n"
1.156 jca 1661: "\t [-i interval] [-K keyfile] [-M ttl] [-m minttl] [-O length]\n"
1662: "\t [-P proxy_username] [-p source_port] [-R CAfile] [-s source]\n"
1663: "\t [-T keyword] [-V rtable] [-w timeout] [-X proxy_protocol]\n"
1664: "\t [-x proxy_address[:port]] [destination] [port]\n");
1.21 ericj 1665: if (ret)
1666: exit(1);
1.7 deraadt 1667: }