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