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