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