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