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