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