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