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