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