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