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