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